From ff064b9a0f1316007f46710432f854777a520fdf Mon Sep 17 00:00:00 2001 From: davli-nv Date: Thu, 16 Oct 2025 18:22:39 -0700 Subject: Fix infinite loop in SPIRVLegalizationContext::processWorkList (#8712) When slangc is invoked with -g, a source shader that has static infinite loop can generate IR that have branch to a block that contains a branch to the first block that contains the first branch, resulting in infinite loop. Change SPIRVLegalizationContext::processWorkList to only add branch target to work list via its parent, this avoids the infinite loop above. Also change addToWorkList to stop addUsersToWorkList, users should be added explicitly by logic for specific insts. Add regression test as tests/spirv/infinite-loop.slang Fixes #8669 --- source/slang/slang-ir-spirv-legalize.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'source') diff --git a/source/slang/slang-ir-spirv-legalize.cpp b/source/slang/slang-ir-spirv-legalize.cpp index 965d6dc9b..97649f4ad 100644 --- a/source/slang/slang-ir-spirv-legalize.cpp +++ b/source/slang/slang-ir-spirv-legalize.cpp @@ -184,13 +184,7 @@ struct SPIRVLegalizationContext : public SourceEmitterBase // OrderedHashSet workList; - void addToWorkList(IRInst* inst) - { - if (workList.add(inst)) - { - addUsersToWorkList(inst); - } - } + void addToWorkList(IRInst* inst) { workList.add(inst); } void addUsersToWorkList(IRInst* inst) { @@ -1692,8 +1686,6 @@ struct SPIRVLegalizationContext : public SourceEmitterBase } }; - void processBranch(IRInst* branch) { addToWorkList(branch->getOperand(0)); } - void processPtrLit(IRInst* inst) { IRBuilder builder(inst); @@ -1844,9 +1836,11 @@ struct SPIRVLegalizationContext : public SourceEmitterBase case kIROp_PtrLit: processPtrLit(inst); break; - case kIROp_UnconditionalBranch: - processBranch(inst); - break; + // kIROp_UnconditionalBranch is handled in default case that only + // adds children inst and not target inst to work list. + // Branch target should be added to work list via its parent, + // to avoid cycle when branch target block has branch to the block + // that's parent of this branch inst. case kIROp_SPIRVAsm: processSPIRVAsm(as(inst)); break; -- cgit v1.2.3