Commit 2188a47
committed
For a for-loop with a switch containing a `continue`, the IR has this shape:
loop(target=%body, break=%after, continue=%incr)
%body:
switch(x, break=%post_switch, ...)
case 0: unconditionalBranch(%incr) <- 'continue'
case 1: unconditionalBranch(%post_switch) <- 'break'
%post_switch:
unconditionalBranch(%incr) <- normal post-switch flow
%incr: i++; unconditionalBranch(%body) <- back-edge
%after: ... <- loop break
The `continue` inside the switch branches to %incr (the loop's continueBlock).
Because continueBlock (%incr) != targetBlock (%body) in a for-loop, this is a
branch that exits the switch region to reach an exit block of the enclosing loop
region -- a multi-level branch that must be transformed for valid SPIRV.
The bug: populateExitBlocks() stored continueBlock but did not add it to
region->exitBlocks, so mapExitBlockToRegion never mapped %incr to the loop
region. gatherInfo() therefore never flagged the branch from case 0 to %incr as
a multi-level branch, needsContinueElimination stayed false, and the raw IR
branch reached the SPIRV emitter -- producing unstructured control flow.
The fix: add continueBlock to region->exitBlocks whenever continueBlock !=
targetBlock (i.e. for for-loops). This makes %incr visible in
mapExitBlockToRegion, gatherInfo() detects the multi-level branch, and
eliminateContinueBlocksInFunc() is called to wrap the loop body in an inner
breakable region. After that transformation the `continue` becomes a break from
the inner region -- a valid SPIRV structured exit to its merge block -- and the
outer loop handles the actual iteration.
Also fix a stack leak: after processing a loop region, pop continueBlock from
the global exitBlocks stack unconditionally. For for-loops it is already in
info.exitBlocks and removed by the existing loop; for while-loops (where
continueBlock == targetBlock and is not in info.exitBlocks) it was pushed to
the global stack but never popped, which could affect sibling constructs.
Fixes #9585, #10198.
1 parent 99e2a4f commit 2188a47
File tree
2 files changed
+95
-2
lines changed- source/slang
- tests/bugs
2 files changed
+95
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
| 75 | + | |
75 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
76 | 101 | | |
77 | 102 | | |
78 | 103 | | |
| |||
177 | 202 | | |
178 | 203 | | |
179 | 204 | | |
180 | | - | |
| 205 | + | |
181 | 206 | | |
182 | 207 | | |
| 208 | + | |
| 209 | + | |
183 | 210 | | |
184 | 211 | | |
185 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
0 commit comments