From 06f7ef354cdde4cf8e8797d8853ed2d9c3208b5b Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Fri, 25 Aug 2023 14:53:12 -0400 Subject: Fix various issues with trivial loops (#3149) * Fix issue with trivial loop detection * Fix issue with unreachable blocks in break elimination Add logic to avoid eliminating loops with multi-level breaks. * Incorporate feedback - Use a boolean for multi-level break check - Use dominator trees for region check instead of exhaustive enumeration - Fix potential issue with enumerating parent break blocks. * fix --- source/slang/slang-ir-loop-unroll.cpp | 43 ++--------------------------------- 1 file changed, 2 insertions(+), 41 deletions(-) (limited to 'source/slang/slang-ir-loop-unroll.cpp') diff --git a/source/slang/slang-ir-loop-unroll.cpp b/source/slang/slang-ir-loop-unroll.cpp index c9ac4191b..c4ef1650c 100644 --- a/source/slang/slang-ir-loop-unroll.cpp +++ b/source/slang/slang-ir-loop-unroll.cpp @@ -48,45 +48,6 @@ static bool _eliminateDeadBlocks(List& blocks, IRBlock* unreachableBlo return changed; } -List _collectBlocksInLoop(IRDominatorTree* dom, IRLoop* loopInst) -{ - List loopBlocks; - HashSet loopBlocksSet; - auto addBlock = [&](IRBlock* block) - { - if (loopBlocksSet.add(block)) - loopBlocks.add(block); - }; - auto firstBlock = as(loopInst->block.get()); - auto breakBlock = as(loopInst->breakBlock.get()); - - addBlock(firstBlock); - for (Index i = 0; i < loopBlocks.getCount(); i++) - { - auto block = loopBlocks[i]; - for (auto succ : block->getSuccessors()) - { - if (succ == breakBlock) - continue; - if (!dom->dominates(firstBlock, succ)) - continue; - if (!as(breakBlock->getTerminator())) - { - if (dom->dominates(breakBlock, succ)) - continue; - } - addBlock(succ); - } - } - return loopBlocks; -} - -List collectBlocksInLoop(IRGlobalValueWithCode* func, IRLoop* loopInst) -{ - auto dom = computeDominatorTree(func); - return _collectBlocksInLoop(dom, loopInst); -} - static int _getLoopMaxIterationsToUnroll(IRLoop* loopInst) { static constexpr int kMaxIterationsToAttempt = 4096; @@ -440,7 +401,7 @@ static bool _unrollLoop( firstIterationBreakBlock->removeAndDeallocateAllDecorationsAndChildren(); builder.setInsertInto(firstIterationBreakBlock); - builder.emitBranch(unreachableBlock); + builder.emitUnreachable(); break; } @@ -487,7 +448,7 @@ bool unrollLoopsInFunc( // Remove any continue jumps from the loop. eliminateContinueBlocks(module, loop); - auto blocks = collectBlocksInLoop(func, loop); + auto blocks = collectBlocksInRegion(func, loop); auto loopLoc = loop->sourceLoc; if (!_unrollLoop(module, loop, blocks)) { -- cgit v1.2.3