Skip to content

Commit 0af589b

Browse files
authored
Fix loop hoisting logic in redundancy pass. (#5836)
* Fix fast single iteration loop test in redundancy pass. * Fix.
1 parent 941f070 commit 0af589b

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

source/slang/slang-ir-redundancy-removal.cpp

+7-16
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,6 @@ namespace Slang
99
struct RedundancyRemovalContext
1010
{
1111
RefPtr<IRDominatorTree> dom;
12-
bool isSingleIterationLoop(IRLoop* loop)
13-
{
14-
int useCount = 0;
15-
for (auto use = loop->getBreakBlock()->firstUse; use; use = use->nextUse)
16-
{
17-
if (use->getUser() == loop)
18-
continue;
19-
useCount++;
20-
if (useCount > 1)
21-
return false;
22-
}
23-
return true;
24-
}
2512

2613
bool tryHoistInstToOuterMostLoop(IRGlobalValueWithCode* func, IRInst* inst)
2714
{
@@ -31,11 +18,15 @@ struct RedundancyRemovalContext
3118
parentBlock = dom->getImmediateDominator(parentBlock))
3219
{
3320
auto terminatorInst = parentBlock->getTerminator();
34-
if (terminatorInst->getOp() == kIROp_loop &&
35-
!isSingleIterationLoop(as<IRLoop>(terminatorInst)))
21+
if (auto loop = as<IRLoop>(terminatorInst))
3622
{
23+
// If `inst` is outside of the loop region, don't hoist it into the loop.
24+
if (dom->dominates(loop->getBreakBlock(), inst))
25+
continue;
26+
3727
// Consider hoisting the inst into this block.
38-
// This is only possible if all operands of the inst are dominating `parentBlock`.
28+
// This is only possible if all operands of the inst are dominating
29+
// `parentBlock`.
3930
bool canHoist = true;
4031
for (UInt i = 0; i < inst->getOperandCount(); i++)
4132
{

0 commit comments

Comments
 (0)