fix(shader): give a selection a merge when one arm returns through nested branches #6405

Merged
fdfawef324 merged 1 commit from upstream-pr/6405 into main 2026-10-19 00:07:55 +00:00
fdfawef324 commented 2026-10-15 14:12:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-15T14:12:36Z.


What

mergeBesideReturns (ControlFlow/src/Structurizer.cpp) finds the merge of a selection whose arms do not meet. It computes post-dominators with the edges into blocks that only lead to a return left out. On main a block counted as one that only leads to a return when a straight run of single-predecessor blocks starts at it and ends in a return (hasLinearPathToTerminal, linearTail). An arm whose return path branches again, such as an early return inside a nested if/else, still counted as a block of its own. The arms then had no common post-dominator, and the structurizer gave the whole program to route variables ("conditional block 0 has no structured merge").

  • A block now only leads to a return when it has one predecessor, the header dominates it and every successor only leads to a return (a block with no successor returns). The walk uses an explicit stack and keeps one result per block; a block on a cycle does not count.

Why it matters: in the route-variable form every original block becomes a guarded region, and the register phis at the region joins also carry values from paths that never reach a later use. In Resident Evil 4 (PPSA07411) pixel program 0x20762add00, the T# of an image_load became Phi(0, load through Phi(user data, compare mask)), and resource analysis refused it. The title stopped there, in sceAgcCreateInterpolantMapping_0100 -> Driver::ResolveShaderAbi on a game thread:

terminate called after throwing an instance of 'std::runtime_error'
  what():  shader 0x20762add00 stage 5 (shader_20762add00.req): ShaderRecompiler::Recompile: GetImageResource dword 0 is not a valid runtime value; chain: Phi(Immediate, LoadAddressU32@pc=0x00000214(GetAddressResource(Phi(GetUserData(Void), CompositeExtractU32x4(Ballot(LogicalAnd(IEqual32(GetBuiltin, Immediate), ULessThan32(Immediate, SelectU32))), Immediate)), Phi(GetUserData(Void), ...

Structured, the T# is the load through the user-data pointer, and the program prepares.

The title gets to this program only after other stops: main stops it at Value::setTypeMismatchHandler (#6126) and then at its NGG geometry states (#5884). The run above was a private build of main 8cd937e29 with #6126 and local changes that get it past the geometry states. The request the driver saved for the program (APS5_DUMP_SHADERS=1) shows the refusal on current main without the title, see Tested.

Overlap: #6025 (dominator sets as bitsets) also changes Structurizer.cpp; git merge-tree merges the two cleanly. No open pull request changes mergeBesideReturns.

Tested

Windows Server 2025 (10.0.26100), AMD Ryzen 9 5900X, NVIDIA GeForce RTX 3060 (driver 591.86), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON.

  • agc_structurizer_tests: a new graph (14 blocks; both arms of the header reach a return through nested branches and share one block) must keep the same executions as the original graph and must not route through a goto variable. On main (a5e2a80c6, with only the test added) it fails: an arm whose returns branch once a tail shared across arms is cloned: block 0 routes through a goto variable instead of a structured selection. With the change it passes.
  • agc_shader_replay --prepare of the request the driver saved for the title's program:
    • main a5e2a80c6: resource analysis failed: GetImageResource dword 0 is not a valid runtime value; chain: Phi(Immediate, LoadAddressU32@pc=0x00000214(GetAddressResource(Phi(GetUserData(Void), ...
    • with the change (on 0e4ea13a0): prepared: 81214 SPIR-V words.
  • ctest -R "agc|shader|video_out" -j4 on the RTX 3060, on this branch (main 0e4ea13a0): 621 of 622 pass, 14 skip. The failure is agc_driver_store_over_untracked (store over untracked: the first result was not stored), #6347; it fails on main here without the change too (5 of 5 runs at a5e2a80c6). agc_structurizer, its _intersection_off, _iteration_selection_off and _forced_fallback modes, agc_shader_control_flow and its two modes pass, and so does control_flow_graph. Before a rebase, on main 71bd2876e with the change: 617 of 618, the same failure.
  • tools/check_conventions.py from main with this title and description: passes.
  • Resident Evil 4 (PPSA07411), with this change on a private build (main 8cd937e29 + #6126 + #5884 + a read-lane change like #6277 + #5907 and a follow-up to it): the title no longer stops on this program. It reached the brightness calibration screen (1,485 s run) and the "About the Settings for Sending Play Data" screen (2,096 s run); both runs ended on the harness's timed dump, with no driver failure in the log.

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 under Silent stubs in their library's technical debt file
  • 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
  • Unblocks: PPSA07411 (no Game bring-up discussion yet; also needs #6126 and #5884 to get here)
  • GPUs tested: NVIDIA GeForce RTX 3060, driver 591.86, Windows Server 2025
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4874660673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6405). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-15T14:12:36Z. --- ### What `mergeBesideReturns` (`ControlFlow/src/Structurizer.cpp`) finds the merge of a selection whose arms do not meet. It computes post-dominators with the edges into blocks that only lead to a return left out. On `main` a block counted as one that only leads to a return when a straight run of single-predecessor blocks starts at it and ends in a return (`hasLinearPathToTerminal`, `linearTail`). An arm whose return path branches again, such as an early return inside a nested if/else, still counted as a block of its own. The arms then had no common post-dominator, and the structurizer gave the whole program to route variables ("conditional block 0 has no structured merge"). - A block now only leads to a return when it has one predecessor, the header dominates it and every successor only leads to a return (a block with no successor returns). The walk uses an explicit stack and keeps one result per block; a block on a cycle does not count. Why it matters: in the route-variable form every original block becomes a guarded region, and the register phis at the region joins also carry values from paths that never reach a later use. In Resident Evil 4 (PPSA07411) pixel program `0x20762add00`, the T# of an `image_load` became `Phi(0, load through Phi(user data, compare mask))`, and resource analysis refused it. The title stopped there, in `sceAgcCreateInterpolantMapping_0100` -> `Driver::ResolveShaderAbi` on a game thread: ``` terminate called after throwing an instance of 'std::runtime_error' what(): shader 0x20762add00 stage 5 (shader_20762add00.req): ShaderRecompiler::Recompile: GetImageResource dword 0 is not a valid runtime value; chain: Phi(Immediate, LoadAddressU32@pc=0x00000214(GetAddressResource(Phi(GetUserData(Void), CompositeExtractU32x4(Ballot(LogicalAnd(IEqual32(GetBuiltin, Immediate), ULessThan32(Immediate, SelectU32))), Immediate)), Phi(GetUserData(Void), ... ``` Structured, the T# is the load through the user-data pointer, and the program prepares. The title gets to this program only after other stops: `main` stops it at `Value::setTypeMismatchHandler` (#6126) and then at its NGG geometry states (#5884). The run above was a private build of `main` 8cd937e29 with #6126 and local changes that get it past the geometry states. The request the driver saved for the program (`APS5_DUMP_SHADERS=1`) shows the refusal on current `main` without the title, see Tested. Overlap: #6025 (dominator sets as bitsets) also changes `Structurizer.cpp`; `git merge-tree` merges the two cleanly. No open pull request changes `mergeBesideReturns`. ### Tested Windows Server 2025 (10.0.26100), AMD Ryzen 9 5900X, NVIDIA GeForce RTX 3060 (driver 591.86), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`. - `agc_structurizer_tests`: a new graph (14 blocks; both arms of the header reach a return through nested branches and share one block) must keep the same executions as the original graph and must not route through a goto variable. On `main` (a5e2a80c6, with only the test added) it fails: `an arm whose returns branch once a tail shared across arms is cloned: block 0 routes through a goto variable instead of a structured selection`. With the change it passes. - `agc_shader_replay --prepare` of the request the driver saved for the title's program: - `main` a5e2a80c6: `resource analysis failed: GetImageResource dword 0 is not a valid runtime value; chain: Phi(Immediate, LoadAddressU32@pc=0x00000214(GetAddressResource(Phi(GetUserData(Void), ...` - with the change (on 0e4ea13a0): `prepared: 81214 SPIR-V words`. - `ctest -R "agc|shader|video_out" -j4` on the RTX 3060, on this branch (`main` 0e4ea13a0): 621 of 622 pass, 14 skip. The failure is `agc_driver_store_over_untracked` (`store over untracked: the first result was not stored`), #6347; it fails on `main` here without the change too (5 of 5 runs at a5e2a80c6). `agc_structurizer`, its `_intersection_off`, `_iteration_selection_off` and `_forced_fallback` modes, `agc_shader_control_flow` and its two modes pass, and so does `control_flow_graph`. Before a rebase, on `main` 71bd2876e with the change: 617 of 618, the same failure. - `tools/check_conventions.py` from `main` with this title and description: passes. - Resident Evil 4 (PPSA07411), with this change on a private build (`main` 8cd937e29 + #6126 + #5884 + a read-lane change like #6277 + #5907 and a follow-up to it): the title no longer stops on this program. It reached the brightness calibration screen (1,485 s run) and the "About the Settings for Sending Play Data" screen (2,096 s run); both runs ended on the harness's timed dump, with no driver failure in the log. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Unblocks: PPSA07411 (no Game bring-up discussion yet; also needs #6126 and #5884 to get here) - [x] GPUs tested: NVIDIA GeForce RTX 3060, driver 591.86, Windows Server 2025 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-15 15:04:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T15:04:39Z.


I ran the test case this PR adds, on Linux.

Setup: head a2f497a05 merged into main at ff23657f3 (no conflicts; the PR's base is 25 commits behind), Linux, default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment (env -i with only HOME, PATH and VK_ICD_FILENAMES set). ctest -R '^agc_structurizer$', 3 runs each.

  • with this PR: 3 of 3 runs pass.
  • with core/shader/recompiler/ControlFlow/src/Structurizer.cpp put back to main's version (the PR's test changes kept): 3 of 3 runs fail, at "an arm whose returns branch once a tail shared across arms is cloned: block 0 routes through a goto variable instead of a structured selection".

The same-executions check on that graph passes on main too, so main gives the same paths, only through a goto variable. I only ran the structurizer test, not a title.
I did not check Windows or macOS.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6195270677 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6405#issuecomment-6195270677). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-15T15:04:39Z. --- I ran the test case this PR adds, on Linux. Setup: head a2f497a05 merged into main at ff23657f3 (no conflicts; the PR's base is 25 commits behind), Linux, default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment (`env -i` with only HOME, PATH and VK_ICD_FILENAMES set). `ctest -R '^agc_structurizer$'`, 3 runs each. - with this PR: 3 of 3 runs pass. - with core/shader/recompiler/ControlFlow/src/Structurizer.cpp put back to main's version (the PR's test changes kept): 3 of 3 runs fail, at "an arm whose returns branch once a tail shared across arms is cloned: block 0 routes through a goto variable instead of a structured selection". The same-executions check on that graph passes on main too, so main gives the same paths, only through a goto variable. I only ran the structurizer test, not a title. I did not check Windows or macOS. AI-assisted: yes
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!6405
No description provided.