fix(recompiler): merge a loop selection whose arm leaves the iteration where its arms rejoin #2941

Merged
CnRJay merged 1 commit from upstream-pr/2941 into main 2026-10-10 23:35:32 +00:00
CnRJay commented 2026-10-10 18:40:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-10T18:40:37Z.


What

PPSA28416 (STAR WARS: Galactic Racer, UE5) stops on main with:

compute shader 0x...: ShaderRecompiler::Recompile: CFG shared merge splitting exceeded budget: originalBlocks=161 currentBlocks=805 splitBudget=644

Four of its compute shaders fail this way (135, 161, 162 and 272 blocks), each with exactly five times its original block count at the throw. A trace of the 161-block shader shows the same selection taking every split. Its header H sits inside a loop, and both arms rejoin at an empty block J that leads to a block X. X is also the merge of the enclosing selection, whose header branches to X directly, so H does not dominate X. Each arm can also leave the iteration through its own block that reaches the loop's continue. Those paths go round the loop and reach X through the enclosing header. So X stays the arms' nearest common post-dominator, findSelectionMerge keeps returning it, and splitSharedMergeBlock inserts one more empty block between J and X on every pass until the budget throws.

findSelectionMerge now handles that case within the iteration. The case: inside a loop, both arms reach the global merge within the iteration, the header does not dominate it, and an arm can reach the loop's continue or merge block without passing it. The merge is then the nearest block that post-dominates both arms over the paths that reach the global merge without leaving the iteration, accepted only when the header dominates it and it lies in the loop construct. Leaving through the continue or the loop's merge from inside a selection is a structured continue or break. In every other case the merge choice is unchanged, and when no such block exists the existing fallback runs.

This differs from #2471, where both arms reach the same loop exit and the merge chosen is that exit. Here the shared block is inside the loop body, and the arms' escapes go to the continue. #2471 alone does not fix these shaders.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release, RTX 4080.

  • agc_shader_control_flow gains a 12-block fixture with this shape. On main it fails with CFG shared merge splitting exceeded budget: originalBlocks=12 currentBlocks=60 splitBudget=48. With the change, the existing harness structures it without cloning or route variables and compares every reachable original and rewritten branch and guest instruction range, for 32- and 64-lane subgroups.
  • agc_structurizer, agc_spirv_* and agc_driver_scalar_program_flow pass. Full suite: 712/712 pass (5 VideoOut tests skipped, as on main).
  • PPSA28416: the four shaders recompile, and the title runs on until a separate host-visible allocation limit. The captured requests replay through agc_shader_replay past structurization.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4813562075 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2941). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-10T18:40:37Z. --- ### What PPSA28416 (STAR WARS: Galactic Racer, UE5) stops on `main` with: ``` compute shader 0x...: ShaderRecompiler::Recompile: CFG shared merge splitting exceeded budget: originalBlocks=161 currentBlocks=805 splitBudget=644 ``` Four of its compute shaders fail this way (135, 161, 162 and 272 blocks), each with exactly five times its original block count at the throw. A trace of the 161-block shader shows the same selection taking every split. Its header H sits inside a loop, and both arms rejoin at an empty block J that leads to a block X. X is also the merge of the enclosing selection, whose header branches to X directly, so H does not dominate X. Each arm can also leave the iteration through its own block that reaches the loop's continue. Those paths go round the loop and reach X through the enclosing header. So X stays the arms' nearest common post-dominator, `findSelectionMerge` keeps returning it, and `splitSharedMergeBlock` inserts one more empty block between J and X on every pass until the budget throws. `findSelectionMerge` now handles that case within the iteration. The case: inside a loop, both arms reach the global merge within the iteration, the header does not dominate it, and an arm can reach the loop's continue or merge block without passing it. The merge is then the nearest block that post-dominates both arms over the paths that reach the global merge without leaving the iteration, accepted only when the header dominates it and it lies in the loop construct. Leaving through the continue or the loop's merge from inside a selection is a structured continue or break. In every other case the merge choice is unchanged, and when no such block exists the existing fallback runs. This differs from #2471, where both arms reach the same loop exit and the merge chosen is that exit. Here the shared block is inside the loop body, and the arms' escapes go to the continue. #2471 alone does not fix these shaders. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, RTX 4080. - `agc_shader_control_flow` gains a 12-block fixture with this shape. On `main` it fails with `CFG shared merge splitting exceeded budget: originalBlocks=12 currentBlocks=60 splitBudget=48`. With the change, the existing harness structures it without cloning or route variables and compares every reachable original and rewritten branch and guest instruction range, for 32- and 64-lane subgroups. - `agc_structurizer`, `agc_spirv_*` and `agc_driver_scalar_program_flow` pass. Full suite: 712/712 pass (5 VideoOut tests skipped, as on `main`). - PPSA28416: the four shaders recompile, and the title runs on until a separate host-visible allocation limit. The captured requests replay through `agc_shader_replay` past structurization. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-10 23:35:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:35:29Z.


Checked on current main: agc_shader_control_flow (new 12-block fixture, wave32/64), agc_structurizer, agc_structurizer_intersection_off, agc_spirv_module and agc_driver_scalar_program_flow pass. The new fixture fails without the Structurizer.cpp change (negative check). The merge choice is unchanged outside the case described, and the new one requires the header to dominate the merge inside the loop construct. Merging.

Note for #2471: it conflicts with this one in Structurizer.cpp; it will need a rebase on top of this change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103368528 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2941#issuecomment-6103368528). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:35:29Z. --- Checked on current main: `agc_shader_control_flow` (new 12-block fixture, wave32/64), `agc_structurizer`, `agc_structurizer_intersection_off`, `agc_spirv_module` and `agc_driver_scalar_program_flow` pass. The new fixture fails without the `Structurizer.cpp` change (negative check). The merge choice is unchanged outside the case described, and the new one requires the header to dominate the merge inside the loop construct. Merging. Note for #2471: it conflicts with this one in `Structurizer.cpp`; it will need a rebase on top of this change.
AlxcBTW commented 2026-10-11 07:49:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-11T07:49:27Z.


@CnRJay a follow-up on #2941. Since it landed, the structurizer emits invalid output for some graphs: a conditional in a loop gets no merge and neither breaks nor continues its loop. On the cfgfuzz corpus (30,000 graphs, seed 3), #2941 structurizes 2,032 graphs at 0 words per block that were refused before, and 1,919 of them are invalid. Smallest case: {{1}, {2, 3}, {4, 5}, {}, {3, 6}, {7, 3}, {1}, {1}} ("block 6 branches without a merge"). Before #2941 it was refused.

iterationSelectionMerge stops its backward walk only at the loop's continue and merge blocks. So the arms' nearest common post-dominator can be a block on the exit path in front of the loop merge, such as the one merge splitting inserts there, and the arms then leave the loop body without a merge.

Fix: AlxcBTW/AnyPS5#40, 8 lines plus a regression test. Blocks outside the loop body end the analysis, so the merge is a block of the loop body, or there is none and the selection keeps its pre-#2941 merge.

  • The 1,919 / 1,919 / 1,664 invalid outputs at 0 / 16 / 1000 words are refused again.
  • 110 / 110 / 240 of the new valid structurizations stay.
  • No graph that is valid on main becomes invalid.
  • Corpus time is unchanged.
  • Full fork CI passes.

The table and the remaining older invalid classes are in the PR description. @oneandonlydean, it's carry-ready if you want it.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106838161 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2941#issuecomment-6106838161). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-11T07:49:27Z. --- @CnRJay a follow-up on #2941. Since it landed, the structurizer emits invalid output for some graphs: a conditional in a loop gets no merge and neither breaks nor continues its loop. On the cfgfuzz corpus (30,000 graphs, seed 3), #2941 structurizes 2,032 graphs at 0 words per block that were refused before, and 1,919 of them are invalid. Smallest case: `{{1}, {2, 3}, {4, 5}, {}, {3, 6}, {7, 3}, {1}, {1}}` ("block 6 branches without a merge"). Before #2941 it was refused. `iterationSelectionMerge` stops its backward walk only at the loop's continue and merge blocks. So the arms' nearest common post-dominator can be a block on the exit path in front of the loop merge, such as the one merge splitting inserts there, and the arms then leave the loop body without a merge. Fix: AlxcBTW/AnyPS5#40, 8 lines plus a regression test. Blocks outside the loop body end the analysis, so the merge is a block of the loop body, or there is none and the selection keeps its pre-#2941 merge. - The 1,919 / 1,919 / 1,664 invalid outputs at 0 / 16 / 1000 words are refused again. - 110 / 110 / 240 of the new valid structurizations stay. - No graph that is valid on `main` becomes invalid. - Corpus time is unchanged. - Full fork CI passes. The table and the remaining older invalid classes are in the PR description. @oneandonlydean, it's carry-ready if you want it. AI-assisted: yes (Claude Code)
Sign in to join this conversation.
No reviewers
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!2941
No description provided.