Structurizer accepts a loop whose selection shares a tail block with a nested selection; the SPIR-V then fails validation #2762

Closed
opened 2026-10-10 13:15:34 +00:00 by orishamir · 5 comments
orishamir commented 2026-10-10 13:15:34 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-10T13:15:34Z.


Summary

On main a29ef592 Structurizer::Structurize succeeds on a loop whose first selection shares a tail block with a nested selection, but the output has a conditional branch without a selection merge whose arms are not the loop's merge or continue block. Recompile then fails in SPIRV-Tools:

SPIR-V validation before optimization failed:
level=2 word=165: block <ID> 26 branches to the selection construct, but not to the selection header <ID> 24

requireStructuredBranches in tests/Structurizer.cpp already rejects this output. The structurizer's final verification and verifyStructured in tests/ControlFlow.cpp accept it.

Minimal reproduction (graph shape only, no shader code)

Using makeGraph from core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp, added to main() before the diamonds block:

auto sharedTailInLoop = makeGraph({{1}, {2, 3}, {6}, {2, 4}, {5, 6}, {1}, {}});
Structurizer{}.Structurize(sharedTailInLoop);
requireStructuredBranches(sharedTailInLoop, "a loop whose selection shares a tail block with a nested selection");
cmake --build build --target agc_structurizer_tests && ./build/tests/agc_structurizer_tests
a loop whose selection shares a tail block with a nested selection: block 4 branches to 3/5 without a merge, and neither is its loop's merge or continue

Block 1 is the loop header: it branches to the nested selection 3 or to the shared tail 2. Block 3 branches to the same tail 2 or to 4. Block 4 branches to the return 6 or to the latch 5. The tail 2 goes to the return 6.

As a program, through Recompile (wave64 compute, the request of recompile() in tests/ControlFlow.cpp; GraphBuilder builds exactly the graph above from these words):

const std::vector<std::uint32_t> loopSharedTail{
    0x060202f2u, 0x060202f4u, 0x7d880084u, 0xbf870002u, 0x060202f6u, 0xbf820009u, 0x060202ffu, 0x3f820000u, 0x7d880085u, 0xbf87fffau,
    0x060202ffu, 0x3f840000u, 0x7d880087u, 0xbf870001u, 0xbf82fff2u, 0x060202ffu, 0x3f850000u, 0xe0700000u, 0x80010100u, 0xbf810000u};
verifyGraph("loop with a shared tail", loopSharedTail);    // passes
recompile("loop with a shared tail", loopSharedTail, 64u); // throws the validation error above

What happens

Structurized output for the graph above (ids 1 and 7 are new):

block 1  LOOP merge=5 continue=7
block 2  CondBranch(true=3 false=4)  merge=4
block 3  -> 5                                   shared tail
block 4  CondBranch(true=3 false=6)  no merge   <- the problem
block 5  Return
block 6  CondBranch(true=7 false=5)  no merge   break/continue conditional, valid without a merge
block 7  -> 1

In the final merge assignment (Structurizer.cpp ~1294-1310 at a29ef592) a conditional is exempt from needing a selection merge when isInnermostLoopControlConditional (line 456) is true, which it is for block 4 because exactly one of its targets is outside loop->blocks (if (trueInBody != falseInBody) return true;), without requiring that target to be the loop's merge or continue block. exitTailSelectionMerge (line 483) then gives it no merge because the tail, block 3, is not dominated by block 4: it is shared with selection 2. SPIR-V only allows a merge-less OpBranchConditional as a direct break or continue, and block 3 is the true arm of selection 2 whose merge is block 4, so the merge of selection 2 branches back into its own construct, which is what the validator reports.

Notes

  • Two fix directions: (1) apply the requireStructuredBranches rule in the structurizer's final verification so this fails as an unsupported CFG throw instead of an invalid module, and keep the graph above as the regression; (2) clone the two-instruction shared tail for the edge 4 -> 3, as shared tails are cloned outside loops (Split::Clone programs in tests/ControlFlow.cpp) and as privatizeSharedReturns clones small return blocks since #2201, so that block 4's leaving arm can be routed to the loop merge.
  • Found with a random structured-program fuzzer (if/else, while and do-while loops, break, continue, return, shared returns; harness and exact commands: https://github.com/orishamir/anyps5-cfgfuzz). Of 30,000 programs on main, 225 fail the requireStructuredBranches rule; 203 of those run through Recompile gave 199 SPIR-V validation failures, 2 valid modules and 2 structurizer refusals. #2471 widens the same class on that corpus (numbers in a comment there).
  • Earlier fixes of the same validator messages: #275, #413. Related: #2642 (budget failures, a different class).
  • Environment: Ubuntu (WSL2), GCC 15.2.0, Release, ANYPS5_ENABLE_SPIRV_TOOLS=ON; the shader words were assembled with LLVM 21 llvm-mc --triple=amdgcn --mcpu=gfx1013 --mattr=+wavefrontsize64.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5791564740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2762). Originally posted by [orishamir](https://github.com/orishamir) on 2026-10-10T13:15:34Z. --- ### Summary On `main` a29ef592 `Structurizer::Structurize` succeeds on a loop whose first selection shares a tail block with a nested selection, but the output has a conditional branch without a selection merge whose arms are not the loop's merge or continue block. `Recompile` then fails in SPIRV-Tools: ``` SPIR-V validation before optimization failed: level=2 word=165: block <ID> 26 branches to the selection construct, but not to the selection header <ID> 24 ``` `requireStructuredBranches` in `tests/Structurizer.cpp` already rejects this output. The structurizer's final verification and `verifyStructured` in `tests/ControlFlow.cpp` accept it. ### Minimal reproduction (graph shape only, no shader code) Using `makeGraph` from `core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp`, added to `main()` before the `diamonds` block: ```cpp auto sharedTailInLoop = makeGraph({{1}, {2, 3}, {6}, {2, 4}, {5, 6}, {1}, {}}); Structurizer{}.Structurize(sharedTailInLoop); requireStructuredBranches(sharedTailInLoop, "a loop whose selection shares a tail block with a nested selection"); ``` ``` cmake --build build --target agc_structurizer_tests && ./build/tests/agc_structurizer_tests a loop whose selection shares a tail block with a nested selection: block 4 branches to 3/5 without a merge, and neither is its loop's merge or continue ``` Block 1 is the loop header: it branches to the nested selection 3 or to the shared tail 2. Block 3 branches to the same tail 2 or to 4. Block 4 branches to the return 6 or to the latch 5. The tail 2 goes to the return 6. As a program, through `Recompile` (wave64 compute, the request of `recompile()` in `tests/ControlFlow.cpp`; `GraphBuilder` builds exactly the graph above from these words): ```cpp const std::vector<std::uint32_t> loopSharedTail{ 0x060202f2u, 0x060202f4u, 0x7d880084u, 0xbf870002u, 0x060202f6u, 0xbf820009u, 0x060202ffu, 0x3f820000u, 0x7d880085u, 0xbf87fffau, 0x060202ffu, 0x3f840000u, 0x7d880087u, 0xbf870001u, 0xbf82fff2u, 0x060202ffu, 0x3f850000u, 0xe0700000u, 0x80010100u, 0xbf810000u}; verifyGraph("loop with a shared tail", loopSharedTail); // passes recompile("loop with a shared tail", loopSharedTail, 64u); // throws the validation error above ``` ### What happens Structurized output for the graph above (ids 1 and 7 are new): ``` block 1 LOOP merge=5 continue=7 block 2 CondBranch(true=3 false=4) merge=4 block 3 -> 5 shared tail block 4 CondBranch(true=3 false=6) no merge <- the problem block 5 Return block 6 CondBranch(true=7 false=5) no merge break/continue conditional, valid without a merge block 7 -> 1 ``` In the final merge assignment (`Structurizer.cpp` ~1294-1310 at a29ef592) a conditional is exempt from needing a selection merge when `isInnermostLoopControlConditional` (line 456) is true, which it is for block 4 because exactly one of its targets is outside `loop->blocks` (`if (trueInBody != falseInBody) return true;`), without requiring that target to be the loop's merge or continue block. `exitTailSelectionMerge` (line 483) then gives it no merge because the tail, block 3, is not dominated by block 4: it is shared with selection 2. SPIR-V only allows a merge-less `OpBranchConditional` as a direct break or continue, and block 3 is the true arm of selection 2 whose merge is block 4, so the merge of selection 2 branches back into its own construct, which is what the validator reports. ### Notes - Two fix directions: (1) apply the `requireStructuredBranches` rule in the structurizer's final verification so this fails as an `unsupported CFG` throw instead of an invalid module, and keep the graph above as the regression; (2) clone the two-instruction shared tail for the edge 4 -> 3, as shared tails are cloned outside loops (`Split::Clone` programs in `tests/ControlFlow.cpp`) and as `privatizeSharedReturns` clones small return blocks since #2201, so that block 4's leaving arm can be routed to the loop merge. - Found with a random structured-program fuzzer (if/else, while and do-while loops, break, continue, return, shared returns; harness and exact commands: https://github.com/orishamir/anyps5-cfgfuzz). Of 30,000 programs on `main`, 225 fail the `requireStructuredBranches` rule; 203 of those run through `Recompile` gave 199 SPIR-V validation failures, 2 valid modules and 2 structurizer refusals. #2471 widens the same class on that corpus (numbers in a comment there). - Earlier fixes of the same validator messages: #275, #413. Related: #2642 (budget failures, a different class). - Environment: Ubuntu (WSL2), GCC 15.2.0, Release, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`; the shader words were assembled with LLVM 21 `llvm-mc --triple=amdgcn --mcpu=gfx1013 --mattr=+wavefrontsize64`. AI-assisted: yes (Claude Code).
MarcZX10 commented 2026-10-10 13:53:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T13:53:13Z.


I revised the candidate after checking it against the open #2471: the first version rejected one of that PR's valid shared-loop cases. The current check accepts exits to merges of enclosing structured constructs, and rejects the #2762 graph because its merge-less conditional has no valid enclosing exit.

The updated branch is fix/structurizer-shared-loop-tail-2762, now at d70036e0, based on current upstream main (8b812318).

Validation Result
Current main: build agc_structurizer_tests and agc_shader_control_flow_tests Passed
Current main: CTests agc_structurizer and agc_shader_control_flow 2/2 passed
Current #2471 head overlaid with this guard: the same two CTests 2/2 passed, including #2471's shared-loop control-flow fixture
Conventions and diff check Passed

The #2762 graph is now rejected during structurization before SPIR-V emission. The candidate is pushed to my fork; the intake workflow still closes new PRs below its merged-PR threshold, so I have not opened an upstream PR. AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6098197096 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2762#issuecomment-6098197096). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T13:53:13Z. --- I revised the candidate after checking it against the open [#2471](https://github.com/boykopovar/AnyPS5/pull/2471): the first version rejected one of that PR's valid shared-loop cases. The current check accepts exits to merges of enclosing structured constructs, and rejects the #2762 graph because its merge-less conditional has no valid enclosing exit. The updated branch is [fix/structurizer-shared-loop-tail-2762](https://github.com/MarcZX100/AnyPS5/tree/fix/structurizer-shared-loop-tail-2762), now at `d70036e0`, based on current upstream `main` (`8b812318`). | Validation | Result | | --- | --- | | Current `main`: build `agc_structurizer_tests` and `agc_shader_control_flow_tests` | Passed | | Current `main`: CTests `agc_structurizer` and `agc_shader_control_flow` | 2/2 passed | | Current #2471 head overlaid with this guard: the same two CTests | 2/2 passed, including #2471's shared-loop control-flow fixture | | Conventions and diff check | Passed | The #2762 graph is now rejected during structurization before SPIR-V emission. The candidate is pushed to my fork; the intake workflow still closes new PRs below its merged-PR threshold, so I have not opened an upstream PR. AI-assisted: yes.
orishamir commented 2026-10-10 14:02:19 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-10T14:02:19Z.


Measured d70036e0's Structurizer.cpp (ControlFlow sources are unchanged between a29ef592 and 8b812318) with the fuzzer from the issue: 30,000 random structured programs at 0/16/1,000 words per block and with one 1,000-word block, plus 903 of them as real compute shaders through Recompile with SPIRV-Tools validation. Harness: https://github.com/orishamir/anyps5-cfgfuzz.

Candidate alone, against main

  • The guard fires only on graphs that requireStructuredBranches flags (222 of 225 at 16 words, 227 of 230 at 1,000); it changes no other outcome in 30,000 graphs, and rejects none of the 283 sampled shaders that produce valid SPIR-V on main.

  • Of 203 sampled shaders that produce invalid SPIR-V on main, the guard stops 196 before emission. The 3 it misses fail a different validator rule, block exits the loop headed by X, but not via a structured exit. Minimal, as GraphBuilder builds it (structurizes on main and on the candidate, passes requireStructuredBranches and verifyStructured):

    makeGraph({{1}, {2}, {3, 5}, {1, 4}, {2}, {6, 7}, {1}, {}})
    

    After structurization block 5 is a conditional inside the inner loop (header 3, merge 7, continue 6) that branches to 6 or to 10, the outer loop's continue block. One arm is the inner loop's continue, so both checks accept it; the other arm leaves the inner loop without passing its merge, which SPIR-V rejects. The rule the validator applies is per target: every target of a merge-less conditional must stay inside the innermost construct or be that construct's own merge or continue block.

Candidate merged with #2471 (clean three-way merge)

  • Of 200 sampled shaders that #2471 moves from the budget error to accepted output, 104 produce invalid SPIR-V with #2471; the guard stops 98 of them, and #2471's own reproducer from my comment there now throws in the structurizer. (On 2 of 30,000 graphs the guard's throw also triggers the retry path, which then succeeds or hits the budget instead; no graph that structurized before is lost.) The 6 it misses all fail block exits the selection headed by X, but not via a structured exit. Minimal, GraphBuilder form (budget on main, invalid SPIR-V with #2471, still invalid with #2471 + this guard):

    makeGraph({{1}, {1, 2}, {3, 6}, {4, 5}, {8}, {1}, {7, 8}, {3}, {}})
    

    After structurization block 3 is a selection with merge 5 and block 4 inside it is a selection with merge 6; block 4 branches to 6 or straight to 5. The jump to the enclosing selection's merge is what the relaxed check accepts ("exits to merges of enclosing structured constructs"); the validator only accepts the innermost construct's merge, or a loop break/continue. Restricting the exemption to the innermost construct would close this one; the loop case above needs the per-target rule.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6098280507 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2762#issuecomment-6098280507). Originally posted by [orishamir](https://github.com/orishamir) on 2026-10-10T14:02:19Z. --- Measured `d70036e0`'s `Structurizer.cpp` (ControlFlow sources are unchanged between a29ef592 and 8b812318) with the fuzzer from the issue: 30,000 random structured programs at 0/16/1,000 words per block and with one 1,000-word block, plus 903 of them as real compute shaders through `Recompile` with SPIRV-Tools validation. Harness: https://github.com/orishamir/anyps5-cfgfuzz. **Candidate alone, against `main`** - The guard fires only on graphs that `requireStructuredBranches` flags (222 of 225 at 16 words, 227 of 230 at 1,000); it changes no other outcome in 30,000 graphs, and rejects none of the 283 sampled shaders that produce valid SPIR-V on `main`. - Of 203 sampled shaders that produce invalid SPIR-V on `main`, the guard stops 196 before emission. The 3 it misses fail a different validator rule, `block exits the loop headed by X, but not via a structured exit`. Minimal, as `GraphBuilder` builds it (structurizes on `main` and on the candidate, passes `requireStructuredBranches` and `verifyStructured`): ```cpp makeGraph({{1}, {2}, {3, 5}, {1, 4}, {2}, {6, 7}, {1}, {}}) ``` After structurization block 5 is a conditional inside the inner loop (header 3, merge 7, continue 6) that branches to 6 or to 10, the outer loop's continue block. One arm is the inner loop's continue, so both checks accept it; the other arm leaves the inner loop without passing its merge, which SPIR-V rejects. The rule the validator applies is per target: every target of a merge-less conditional must stay inside the innermost construct or be that construct's own merge or continue block. **Candidate merged with #2471** (clean three-way merge) - Of 200 sampled shaders that #2471 moves from the budget error to accepted output, 104 produce invalid SPIR-V with #2471; the guard stops 98 of them, and #2471's own reproducer from my comment there now throws in the structurizer. (On 2 of 30,000 graphs the guard's throw also triggers the retry path, which then succeeds or hits the budget instead; no graph that structurized before is lost.) The 6 it misses all fail `block exits the selection headed by X, but not via a structured exit`. Minimal, `GraphBuilder` form (budget on `main`, invalid SPIR-V with #2471, still invalid with #2471 + this guard): ```cpp makeGraph({{1}, {1, 2}, {3, 6}, {4, 5}, {8}, {1}, {7, 8}, {3}, {}}) ``` After structurization block 3 is a selection with merge 5 and block 4 inside it is a selection with merge 6; block 4 branches to 6 or straight to 5. The jump to the enclosing selection's merge is what the relaxed check accepts ("exits to merges of enclosing structured constructs"); the validator only accepts the innermost construct's merge, or a loop break/continue. Restricting the exemption to the innermost construct would close this one; the loop case above needs the per-target rule. AI-assisted: yes (Claude Code).
BharatYadav007 commented 2026-10-12 04:12:45 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by BharatYadav007 on 2026-10-12T04:12:45Z.


Retested the original seven-block graph from the issue on main at d146ec1b6 (Windows, MinGW-w64 GCC 15.2.0, Release). I added the exact makeGraph({{1}, {2, 3}, {6}, {2, 4}, {5, 6}, {1}, {}}) reproducer to agc_structurizer_tests and ran it through Structurizer::Structurize followed by requireStructuredBranches. The test passes; CTest agc_structurizer completed in 0.07 s.

The original graph failure no longer reproduces on this revision. This check does not establish that all graphs from the wider fuzz corpus are fixed, and I have not rerun that corpus or the original RDNA word stream through SPIR-V validation.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117611799 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2762#issuecomment-6117611799). Originally posted by [BharatYadav007](https://github.com/BharatYadav007) on 2026-10-12T04:12:45Z. --- Retested the original seven-block graph from the issue on `main` at `d146ec1b6` (Windows, MinGW-w64 GCC 15.2.0, Release). I added the exact `makeGraph({{1}, {2, 3}, {6}, {2, 4}, {5, 6}, {1}, {}})` reproducer to `agc_structurizer_tests` and ran it through `Structurizer::Structurize` followed by `requireStructuredBranches`. The test passes; CTest `agc_structurizer` completed in 0.07 s. The original graph failure no longer reproduces on this revision. This check does not establish that all graphs from the wider fuzz corpus are fixed, and I have not rerun that corpus or the original RDNA word stream through SPIR-V validation.
oneandonlydean commented 2026-10-12 06:18:18 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T06:18:18Z.


The seven-block graph structures on main since #2471 (retest above). Does your fuzz corpus still produce a case on current main? If not, this can close.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6118872694 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2762#issuecomment-6118872694). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T06:18:18Z. --- The seven-block graph structures on main since #2471 (retest above). Does your fuzz corpus still produce a case on current main? If not, this can close.
oneandonlydean commented 2026-10-14 00:09:02 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T00:09:02Z.


Closing: the seven-block graph structures on main since #2471 (retest above). If the fuzz corpus still finds a failing graph on current main, comment here with it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157874170 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2762#issuecomment-6157874170). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T00:09:02Z. --- Closing: the seven-block graph structures on main since #2471 (retest above). If the fuzz corpus still finds a failing graph on current main, comment here with it.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#2762
No description provided.