From 50e7d9797d9bf4b98a056d5df128c24dde6e78bd Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 23 Mar 2023 16:59:02 -0700 Subject: Fix optimization pass not converging. (#2725) * Fix optimization pass not converging. * Fix. * Fix tests. --------- Co-authored-by: Yong He --- source/slang/slang-ir-ssa-simplification.cpp | 33 ++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'source/slang/slang-ir-ssa-simplification.cpp') diff --git a/source/slang/slang-ir-ssa-simplification.cpp b/source/slang/slang-ir-ssa-simplification.cpp index a0acf5082..e6c6e353f 100644 --- a/source/slang/slang-ir-ssa-simplification.cpp +++ b/source/slang/slang-ir-ssa-simplification.cpp @@ -20,26 +20,45 @@ namespace Slang { bool changed = true; const int kMaxIterations = 8; + const int kMaxFuncIterations = 16; int iterationCounter = 0; + while (changed && iterationCounter < kMaxIterations) { changed = false; changed |= hoistConstants(module); changed |= deduplicateGenericChildren(module); - changed |= applySparseConditionalConstantPropagation(module); + changed |= propagateFuncProperties(module); + changed |= removeUnusedGenericParam(module); + changed |= applySparseConditionalConstantPropagationForGlobalScope(module); changed |= peepholeOptimize(module); - changed |= removeRedundancy(module); - changed |= simplifyCFG(module); + + for (auto inst : module->getGlobalInsts()) + { + auto func = as(inst); + if (!func) + continue; + bool funcChanged = true; + int funcIterationCount = 0; + while (funcChanged && funcIterationCount < kMaxFuncIterations) + { + funcChanged = false; + funcChanged |= applySparseConditionalConstantPropagation(func); + funcChanged |= peepholeOptimize(func); + funcChanged |= removeRedundancyInFunc(func); + funcChanged |= simplifyCFG(func); + eliminateDeadCode(func); + funcChanged |= constructSSA(func); + changed |= funcChanged; + funcIterationCount++; + } + } // Note: we disregard the `changed` state from dead code elimination pass since // SCCP pass could be generating temporarily evaluated constant values and never actually use them. // DCE will always remove those nearly generated consts and always returns true here. eliminateDeadCode(module); - changed |= propagateFuncProperties(module); - - changed |= constructSSA(module); - changed |= removeUnusedGenericParam(module); iterationCounter++; } } -- cgit v1.2.3