diff options
| author | Yong He <yonghe@outlook.com> | 2024-04-12 14:40:30 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-12 14:40:30 -0700 |
| commit | b937207dccd05f700855e9aeb3f7c375b595b827 (patch) | |
| tree | 19146073437f9f02c7784f974519cce02ca7a6db /source | |
| parent | 565a871ad899c01dc6a1b4bdb1b9d2bc4281758f (diff) | |
Fix IR lowering bug of do-while loops. (#3941)
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang-ir-validate.cpp | 7 | ||||
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 14 |
2 files changed, 16 insertions, 5 deletions
diff --git a/source/slang/slang-ir-validate.cpp b/source/slang/slang-ir-validate.cpp index 2868c9929..a4a2921fe 100644 --- a/source/slang/slang-ir-validate.cpp +++ b/source/slang/slang-ir-validate.cpp @@ -66,6 +66,7 @@ namespace Slang State state = kState_Initial; IRInst* prevChild = nullptr; + bool hasSeenTerminatorInst = false; for(auto child : parent->getDecorationsAndChildren() ) { // We need to check the integrity of the parent/next/prev links of @@ -105,7 +106,11 @@ namespace Slang validate(context, !as<IRTerminatorInst>(child), child, "terminator must be last instruction in a block"); } - + if (as<IRTerminatorInst>(child)) + { + validate(context, !hasSeenTerminatorInst, child, "block must not contain more than one terminator"); + hasSeenTerminatorInst = true; + } prevChild = child; } } diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index 3f776c1a1..855bde6a6 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -5890,6 +5890,12 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor> { auto irCondition = getSimpleVal(context, lowerRValueExpr(context, condExpr)); + + // One thing to be careful here is that lowering irCondition + // may create additional blocks due to short circuiting, so + // the block we are current inserting into is not necessarily + // the same as `testLabel`. + // auto invCondition = builder->emitNot(irCondition->getDataType(), irCondition); // Now we want to `break` if the loop condition is false, @@ -5907,15 +5913,15 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor> // // mergeBlock: // goto breakLabel; - auto mergeBlock = builder->emitBlock(); - builder->emitBranch(loopHead); - - builder->setInsertInto(testLabel); + auto mergeBlock = builder->createBlock(); builder->emitIfElse( invCondition, breakLabel, mergeBlock, mergeBlock); + + insertBlock(mergeBlock); + builder->emitBranch(loopHead); } // Finally we insert the label that a `break` will jump to |
