From 0af589bf2ddf383eb8e6014e8e9da3309284ce0f Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 11 Dec 2024 14:18:41 -0800 Subject: [PATCH] Fix loop hoisting logic in redundancy pass. (#5836) * Fix fast single iteration loop test in redundancy pass. * Fix. --- source/slang/slang-ir-redundancy-removal.cpp | 23 ++++++-------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/source/slang/slang-ir-redundancy-removal.cpp b/source/slang/slang-ir-redundancy-removal.cpp index b885a89469..0d9b910c4f 100644 --- a/source/slang/slang-ir-redundancy-removal.cpp +++ b/source/slang/slang-ir-redundancy-removal.cpp @@ -9,19 +9,6 @@ namespace Slang struct RedundancyRemovalContext { RefPtr dom; - bool isSingleIterationLoop(IRLoop* loop) - { - int useCount = 0; - for (auto use = loop->getBreakBlock()->firstUse; use; use = use->nextUse) - { - if (use->getUser() == loop) - continue; - useCount++; - if (useCount > 1) - return false; - } - return true; - } bool tryHoistInstToOuterMostLoop(IRGlobalValueWithCode* func, IRInst* inst) { @@ -31,11 +18,15 @@ struct RedundancyRemovalContext parentBlock = dom->getImmediateDominator(parentBlock)) { auto terminatorInst = parentBlock->getTerminator(); - if (terminatorInst->getOp() == kIROp_loop && - !isSingleIterationLoop(as(terminatorInst))) + if (auto loop = as(terminatorInst)) { + // If `inst` is outside of the loop region, don't hoist it into the loop. + if (dom->dominates(loop->getBreakBlock(), inst)) + continue; + // Consider hoisting the inst into this block. - // This is only possible if all operands of the inst are dominating `parentBlock`. + // This is only possible if all operands of the inst are dominating + // `parentBlock`. bool canHoist = true; for (UInt i = 0; i < inst->getOperandCount(); i++) {