fix(shader): narrow a goto junction Phi to the arm its branch allows #14636

Merged
zaryabzip merged 13 commits from upstream-pr/14636 into main 2026-11-10 01:51:01 +00:00
zaryabzip commented 2026-11-01 16:42:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-11-01T16:42:32Z.


What

A new pass, GotoPhiRefiner, removes a resource-planning stop that the structurizer's goto-variable junctions cause.

The problem. When the structurizer can't clone a region, it routes control through one junction block and a goto variable. That happens in routeThroughJunction for an entered region, and in routeLoopExits for a loop whose exits share no post-dominator. A value that differs between the routed paths becomes a Phi at the junction, next to the Bool Phi of the goto variable, and a branch on that Bool Phi then picks the next region. Below that branch only the arms whose goto value matches can arrive, but SSA keeps the whole Phi. A descriptor address read there is a Phi of the right pointer and the other paths' values. The SRT walk can't evaluate that, and the shader stops with GetImageResource dword 0 is not a valid runtime value.

What the pass does. It runs just before the SRT walk. If it replaces anything, dead code elimination runs again.

  • Facts. A two-way branch on a Bool Phi, outside a loop header, gives its single-predecessor targets a fact: that Phi is true or false. The fact holds in every block the target dominates. Since the junction dominates the branch, such a block can't be reached from the junction again without passing through the target.
  • Narrowing. Where a non-Phi instruction uses a non-Bool Phi of the same junction, the pass keeps only the arms whose incoming guard is a constant equal to the fact. It follows parallel guard Phis of nested junctions the same way. An arm with a guard that isn't constant is always kept. The operand is replaced only when a single value is left.
  • Soundness. The replacement must dominate the use. No block the narrowing relied on, nor the replacement's own block, may reach the use without passing through the junction. Uses by Phis are left alone.
  • Cost. Results are kept per set of facts, with the blocks they relied on, because the same junction Phis sit under many arms. Without that, the recursion is exponential on large shaders.
  • Rounds. One round follows at most 64 levels of chained junctions and guards. The pass repeats while a round replaces an operand, up to 32 rounds, so a use below a longer chain still narrows. freneza found such a use on PPSA01317, about 20 junctions deep.

The disk cache format moves to 68. A shader.md debt entry records which merges stay unnarrowed.

Where it showed up. In Little Nightmares (PPSA10737, discussion #6275), on main + #7395 + #9749 + #10000, compute shader 0x200c340000 stopped at pc 0x119b8. Its address merged the call site's constant-buffer values (0x444 + 8/0xc) with scratch values from the other linked callees. With this pass the shader prepares and the address is the call site's value. The title then stops in the driver, because the module is 6.2M words; that is #13538's problem. On plain main, this title's shaders stop earlier and still need #9749 and #10000, so this PR unblocks nothing on its own. The new test shows the case directly.

GotoJunctionDescriptor.cpp builds a 64-wide compute program:

  • a loop sets s[16:17] from user data s[4:5] and leaves early on s_cmp_eq_u32 s8, s6;
  • otherwise it overwrites them with an s_buffer_load_dwordx2 and loops;
  • each exit ends the program, so the two have no common post-dominator and routeLoopExits builds a goto-variable junction;
  • the early-exit block loads a T# through s[16:17] and runs image_load.

The test checks that one image is planned and that each of its eight dwords is read through the SRT from a LoadAddressU32 whose address is GetUserData of s4 and s5, not a Phi.

GotoJunctionGuards.cpp builds IR by hand and runs the pass directly. Each guard gets a check that fails when that guard is removed:

  • an arm whose goto guard isn't a constant must stay;
  • a nested junction's value that is recomputed between the junction and the use must not replace the Phi;
  • a use the junction can reach again without passing the goto branch gets no fact.

Two twin checks show the same shapes being narrowed when the guards allow it.

Overlap: git merge-tree against main and every open pull request finds conflicts only on the FormatVersion line of ShaderDiskCache.hpp, with the 19 open PRs that also move the disk-cache format. Following the ladder in #10846, this PR takes 68, the next free number: open PRs hold 51–52, 54, 56–60 and 62–67, with #8939 at 66 and #9457 at 67.

Tested

Windows 11 (WinLibs GCC 15.2.0) and Ubuntu 24.04 on WSL2 (GCC 13.3.0), on main f2d874c1fd, at a commit with the same code and test as the head (only the debt entry's wording changed since).

  • New test agc_goto_junction_descriptor, 3 runs each:
    • On main with only the test, it fails 3 of 3 on both hosts with GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x0000003c(GetAddressResource(Phi(GetUserData(Void), ReadConst(GetSrtResource, Immediate)), Phi(...)), ...).
    • At the head it passes 3 of 3 on both.
  • Guard tests agc_goto_junction_guards: pass 3 of 3 on both hosts. In a mutation check, removing each guard fails only its own check:
    • dropping non-constant-guard arms fails "non-constant guard";
    • skipping the relied-block reachability check fails "nested junction value after the junction";
    • dropping the fact condition fails "junction rerun before the use".
  • Recompiler CPU-side tests: 37 of 37 on both refs and both hosts.
  • agc_* suite, Windows only, RTX 3060 Laptop GPU, NVIDIA 617.42, ANYPS5_NO_SHADER_CACHE=1:
    • main: 1202 passed and 9 skipped, of 1211.
    • Head: 1203 passed and the same 9 skipped, of 1212. The one extra test is the new one.
  • Little Nightmares, offline (agc_shader_replay --prepare):
    • 0x200c340000 prepares, where before it stopped at pc 0x119b8. On this PR's head 447e80bde4 with #7395, #9749 and #13538's opt-in switches (unset) on main 5b1d381d5a, it is 7,046,975 SPIR-V words; at 0eac5c8070 with #9749 on main 4f41f4c375, 7,045,511. An earlier, pre-PR version of the pass gave 6,222,886, which this description used to quote;
    • on that earlier stack, 0x2010fa0000 still prepared, with 479,939 words, and the agc_* suite showed no change;
    • in game on NVIDIA under Windows (RTX 3060 Laptop GPU, 617.42), the 7.05M-word module then fails vkCreateComputePipelines with -13. That is the module-size problem tracked in #13538, not a resource refusal (run).
  • No warnings in the new or changed files. tools/check_conventions.py passes.
  • At the head 38fc45950f, after the stale-fact check was dropped:
    • both new tests pass 3 of 3 on Windows and WSL2, and the 37 CPU-side tests pass on both;
    • GotoPhiRefiner.cpp builds with no warnings;
    • removing the dominance condition fails "junction rerun before the use" and "non-constant guard".
  • Rerun after the main merge, by me at e92fa373fe, which contains main 4977f64052 (comment): on Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), both agc_goto_junction tests pass, ctest -R shader passes 112 of 112 and 104 of 104, and the 37 CPU-side tests pass 37 of 37. The merge keeps FormatVersion 68 over main's 57.
  • PPSA01317 (01.710.000), by freneza, on Linux (Fedora 44) with an RTX 5070, on main 4977f64052 with the GPU pull requests listed in #4697 and limitCallees raised to 256 locally. These are agc_shader_replay --prepare replays of the 29 compute shaders that were refused, not a game run:
    • At e92fa373fe (comment), all 29 still stop with GetImageResource dword 0 is not a valid runtime value. In 28 of them, one use of the pointer Phi is narrowed, but a deeper use at pc 0x19420 is not, because one round stops at MaxRefinementDepth.
    • At 8e5b82d0f2 (comment), those 28 get past the refusal and stop on image resource limit exceeded (ShaderInfo::MaxImages, outside this PR), so none prepares yet. 0x120aed7f00 still stops at pc 0x19f8, which has a different cause. The mean replay time per shader went from 41 s to 51 s, with 7 replays in parallel. agc_goto_junction_guards passes 3 of 3 there.
  • Rerun with the repeated rounds, by me at 8e5b82d0f2 (comment). On Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), both agc_goto_junction tests pass 3 of 3, ctest -R shader passes 112 of 112 and 104 of 104, and the 37 CPU-side tests pass 37 of 37. With e92fa373fe's GotoPhiRefiner.cpp, agc_goto_junction_guards fails only "deep junction chain" (Windows). No warnings in GotoPhiRefiner.cpp or GotoJunctionGuards.cpp.
  • Rerun after the main merge, by me at 1560cfdef0, which merges main 095ff72367 (comment). On Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), each set run once: both agc_goto_junction tests pass, ctest -R shader passes 117 of 117 and 109 of 109, and the 37 CPU-side tests pass 37 of 37. The merge keeps FormatVersion 68 over main's 60. No warnings in GotoPhiRefiner.cpp, GotoJunctionDescriptor.cpp or GotoJunctionGuards.cpp.
  • Not tested: the new test with the shader cache on, since agc_add_default_test turns it off.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3060 Laptop GPU, driver 617.42, Windows 11
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5109398832 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14636). Originally posted by [zaryabzip](https://github.com/zaryabzip) on 2026-11-01T16:42:32Z. --- ### What A new pass, `GotoPhiRefiner`, removes a resource-planning stop that the structurizer's goto-variable junctions cause. **The problem.** When the structurizer can't clone a region, it routes control through one junction block and a goto variable. That happens in `routeThroughJunction` for an entered region, and in `routeLoopExits` for a loop whose exits share no post-dominator. A value that differs between the routed paths becomes a Phi at the junction, next to the Bool Phi of the goto variable, and a branch on that Bool Phi then picks the next region. Below that branch only the arms whose goto value matches can arrive, but SSA keeps the whole Phi. A descriptor address read there is a Phi of the right pointer and the other paths' values. The SRT walk can't evaluate that, and the shader stops with `GetImageResource dword 0 is not a valid runtime value`. **What the pass does.** It runs just before the SRT walk. If it replaces anything, dead code elimination runs again. - **Facts.** A two-way branch on a Bool Phi, outside a loop header, gives its single-predecessor targets a fact: that Phi is true or false. The fact holds in every block the target dominates. Since the junction dominates the branch, such a block can't be reached from the junction again without passing through the target. - **Narrowing.** Where a non-Phi instruction uses a non-Bool Phi of the same junction, the pass keeps only the arms whose incoming guard is a constant equal to the fact. It follows parallel guard Phis of nested junctions the same way. An arm with a guard that isn't constant is always kept. The operand is replaced only when a single value is left. - **Soundness.** The replacement must dominate the use. No block the narrowing relied on, nor the replacement's own block, may reach the use without passing through the junction. Uses by Phis are left alone. - **Cost.** Results are kept per set of facts, with the blocks they relied on, because the same junction Phis sit under many arms. Without that, the recursion is exponential on large shaders. - **Rounds.** One round follows at most 64 levels of chained junctions and guards. The pass repeats while a round replaces an operand, up to 32 rounds, so a use below a longer chain still narrows. freneza found such a use on PPSA01317, about 20 junctions deep. The disk cache format moves to 68. A `shader.md` debt entry records which merges stay unnarrowed. **Where it showed up.** In Little Nightmares (PPSA10737, discussion #6275), on `main` + #7395 + #9749 + #10000, compute shader `0x200c340000` stopped at pc 0x119b8. Its address merged the call site's constant-buffer values (0x444 + 8/0xc) with scratch values from the other linked callees. With this pass the shader prepares and the address is the call site's value. The title then stops in the driver, because the module is 6.2M words; that is #13538's problem. On plain `main`, this title's shaders stop earlier and still need #9749 and #10000, so this PR unblocks nothing on its own. The new test shows the case directly. `GotoJunctionDescriptor.cpp` builds a 64-wide compute program: - a loop sets `s[16:17]` from user data `s[4:5]` and leaves early on `s_cmp_eq_u32 s8, s6`; - otherwise it overwrites them with an `s_buffer_load_dwordx2` and loops; - each exit ends the program, so the two have no common post-dominator and `routeLoopExits` builds a goto-variable junction; - the early-exit block loads a T# through `s[16:17]` and runs `image_load`. The test checks that one image is planned and that each of its eight dwords is read through the SRT from a `LoadAddressU32` whose address is `GetUserData` of `s4` and `s5`, not a Phi. `GotoJunctionGuards.cpp` builds IR by hand and runs the pass directly. Each guard gets a check that fails when that guard is removed: - an arm whose goto guard isn't a constant must stay; - a nested junction's value that is recomputed between the junction and the use must not replace the Phi; - a use the junction can reach again without passing the goto branch gets no fact. Two twin checks show the same shapes being narrowed when the guards allow it. Overlap: `git merge-tree` against `main` and every open pull request finds conflicts only on the `FormatVersion` line of `ShaderDiskCache.hpp`, with the 19 open PRs that also move the disk-cache format. Following the ladder in #10846, this PR takes 68, the next free number: open PRs hold 51–52, 54, 56–60 and 62–67, with #8939 at 66 and #9457 at 67. ### Tested Windows 11 (WinLibs GCC 15.2.0) and Ubuntu 24.04 on WSL2 (GCC 13.3.0), on `main` f2d874c1fd, at a commit with the same code and test as the head (only the debt entry's wording changed since). - **New test** `agc_goto_junction_descriptor`, 3 runs each: - On `main` with only the test, it fails 3 of 3 on both hosts with `GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x0000003c(GetAddressResource(Phi(GetUserData(Void), ReadConst(GetSrtResource, Immediate)), Phi(...)), ...)`. - At the head it passes 3 of 3 on both. - **Guard tests** `agc_goto_junction_guards`: pass 3 of 3 on both hosts. In a mutation check, removing each guard fails only its own check: - dropping non-constant-guard arms fails "non-constant guard"; - skipping the relied-block reachability check fails "nested junction value after the junction"; - dropping the fact condition fails "junction rerun before the use". - **Recompiler CPU-side tests:** 37 of 37 on both refs and both hosts. - **`agc_*` suite**, Windows only, RTX 3060 Laptop GPU, NVIDIA 617.42, `ANYPS5_NO_SHADER_CACHE=1`: - `main`: 1202 passed and 9 skipped, of 1211. - Head: 1203 passed and the same 9 skipped, of 1212. The one extra test is the new one. - **Little Nightmares, offline** (`agc_shader_replay --prepare`): - `0x200c340000` prepares, where before it stopped at pc 0x119b8. On this PR's head `447e80bde4` with #7395, #9749 and #13538's opt-in switches (unset) on `main` `5b1d381d5a`, it is 7,046,975 SPIR-V words; at `0eac5c8070` with #9749 on `main` `4f41f4c375`, 7,045,511. An earlier, pre-PR version of the pass gave 6,222,886, which this description used to quote; - on that earlier stack, `0x2010fa0000` still prepared, with 479,939 words, and the `agc_*` suite showed no change; - in game on NVIDIA under Windows (RTX 3060 Laptop GPU, 617.42), the 7.05M-word module then fails `vkCreateComputePipelines` with `-13`. That is the module-size problem tracked in #13538, not a resource refusal ([run](https://github.com/boykopovar/AnyPS5/issues/13538#issuecomment-6637856508)). - No warnings in the new or changed files. `tools/check_conventions.py` passes. - **At the head 38fc45950f**, after the stale-fact check was dropped: - both new tests pass 3 of 3 on Windows and WSL2, and the 37 CPU-side tests pass on both; - `GotoPhiRefiner.cpp` builds with no warnings; - removing the dominance condition fails "junction rerun before the use" and "non-constant guard". - **Rerun after the `main` merge, by me** at `e92fa373fe`, which contains `main` `4977f64052` ([comment](https://github.com/boykopovar/AnyPS5/pull/14636#issuecomment-6706960289)): on Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), both `agc_goto_junction` tests pass, `ctest -R shader` passes 112 of 112 and 104 of 104, and the 37 CPU-side tests pass 37 of 37. The merge keeps `FormatVersion` 68 over `main`'s 57. - **PPSA01317 (01.710.000), by freneza**, on Linux (Fedora 44) with an RTX 5070, on `main` `4977f64052` with the GPU pull requests listed in #4697 and `limitCallees` raised to 256 locally. These are `agc_shader_replay --prepare` replays of the 29 compute shaders that were refused, not a game run: - At `e92fa373fe` ([comment](https://github.com/boykopovar/AnyPS5/pull/14636#issuecomment-6710416581)), all 29 still stop with `GetImageResource dword 0 is not a valid runtime value`. In 28 of them, one use of the pointer Phi is narrowed, but a deeper use at pc 0x19420 is not, because one round stops at `MaxRefinementDepth`. - At `8e5b82d0f2` ([comment](https://github.com/boykopovar/AnyPS5/pull/14636#issuecomment-6711420567)), those 28 get past the refusal and stop on `image resource limit exceeded` (`ShaderInfo::MaxImages`, outside this PR), so none prepares yet. `0x120aed7f00` still stops at pc 0x19f8, which has a different cause. The mean replay time per shader went from 41 s to 51 s, with 7 replays in parallel. `agc_goto_junction_guards` passes 3 of 3 there. - **Rerun with the repeated rounds, by me** at `8e5b82d0f2` ([comment](https://github.com/boykopovar/AnyPS5/pull/14636#issuecomment-6711154563)). On Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), both `agc_goto_junction` tests pass 3 of 3, `ctest -R shader` passes 112 of 112 and 104 of 104, and the 37 CPU-side tests pass 37 of 37. With `e92fa373fe`'s `GotoPhiRefiner.cpp`, `agc_goto_junction_guards` fails only "deep junction chain" (Windows). No warnings in `GotoPhiRefiner.cpp` or `GotoJunctionGuards.cpp`. - **Rerun after the `main` merge, by me** at `1560cfdef0`, which merges `main` `095ff72367` ([comment](https://github.com/boykopovar/AnyPS5/pull/14636#issuecomment-6726498087)). On Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), each set run once: both `agc_goto_junction` tests pass, `ctest -R shader` passes 117 of 117 and 109 of 109, and the 37 CPU-side tests pass 37 of 37. The merge keeps `FormatVersion` 68 over `main`'s 60. No warnings in `GotoPhiRefiner.cpp`, `GotoJunctionDescriptor.cpp` or `GotoJunctionGuards.cpp`. - Not tested: the new test with the shader cache on, since `agc_add_default_test` turns it off. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> NVIDIA GeForce RTX 3060 Laptop GPU, driver 617.42, Windows 11 - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
MarcZX10 commented 2026-11-02 07:20:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T07:20:40Z.

Review: COMMENTED


Reviewed — no blocking findings from the read; the guards test was built but did not run on Windows

This PR adds GotoPhiRefiner, a shader optimization that narrows a goto junction Phi to the arm its branch allows, wired into PrepareResourceProgram after the first dead-code pass. I read the new pass and its header in full, and the wiring. The Windows CTest run passed the descriptor test. The guards test was compiled and linked on Windows, but it is not in the CTest selection, so it has no hosted pass.

What I ran

  • Head 5ab18d0ce2f9a4e44eb332d809872dcb671a041b; base f2d874c1fdccf781c0fa5d86619bb5dd8894b958. 10 files, +638/-1: Optimization/src/GotoPhiRefiner.cpp (+293, new), Optimization/include/Optimization/GotoPhiRefiner.hpp (+20, new), Recompiler.cpp (+5), ShaderDiskCache.hpp (FormatVersion 50 to 68), the recompiler CMake line, two AGC driver test sources and their CMake fragments, and a debt line.
  • Read the refiner and header in full: the dominator computation, the reachability and the fact collection from single-predecessor conditional targets, the memoized restriction of junction Phis, and the replacement of non-Phi operands. Read the wiring in Recompiler.cpp: when the refiner replaces any operand, dead-code elimination runs again.
  • Test sources: read the function names only. GotoJunctionDescriptor.cpp (103 lines) and GotoJunctionGuards.cpp (197 lines) were not read in full.
  • Hosted, Windows CTest: 2 cases were selected. agc_goto_junction_descriptor (Test #1828) passed, and conventions_env_reference_test_a_name_the_pull_request_does_not_touch_is_not_reported (Test #182) passed. The log shows agc_goto_junction_guards_tests.exe linked, and it is not in the selection.
  • Conventions: two runs on this head. The run started at 20:59:13 was cancelled. A later run started at 20:59:28 completed with success.
  • Read-only checks from the trusted main checkout: diff check, conventions and PR template, and the stack check all passed (no declared dependencies, no errors). ABI audit not applicable.
  • The test-quality helper flagged both new test sources as possibly unregistered. Each CMake fragment names its file by the relative path tests/…cpp, which the helper's literal match did not find. The Windows build compiled both.
  • No MarcZX10 review at this head.

Results

Check Result Note
Diff check passed
Conventions and PR template passed the latest run succeeded; an earlier run on the same head was cancelled
Mergeability and dependencies passed stack check: no declared dependencies, no errors
ABI audit not applicable no export changed
agc_goto_junction_descriptor (hosted, Windows) passed Test #1828
agc_goto_junction_guards_tests (hosted, Windows) built, not run linked, but not in the CTest selection
Refiner logic (read) passed by reading no blocking defect found in the read
Refiner results on the test inputs not verified test bodies not read
Relinkers and relinker-asan (hosted) passed macOS, Ubuntu and Windows relinkers succeeded
library-portability (hosted) passed
Linux build and runtime tests not run Linux is skipped on pull requests
Negative controls not run no local build; no PR-head code runs here
Read-only check output

Precheck was not run: no PR-head code is run here. Diff check: passed. Conventions: passed. Stack check: passed, no declared dependencies, no errors. ABI audit: not applicable. Hosted: gh api repos/boykopovar/AnyPS5/commits/5ab18d0ce2f9a4e44eb332d809872dcb671a041b/check-runs, and the Windows job log.

Negative control

Not run here. A negative control needs a local build and run of the shader tests, and code from this head is not run here.

Findings

Blocking

None from the code read.

Non-blocking

  • Recompiler.cpp runs the refiner for every shader that goes through PrepareResourceProgram, so a defect would reach every shader that uses a goto junction. The only hosted pass is the descriptor test. The guards test (197 lines) was not in the Windows selection, so nothing hosted exercises the junction guards. A run of agc_goto_junction_guards_tests at this head would close that gap.

Sources

Not verified

  • The bodies of GotoJunctionDescriptor.cpp and GotoJunctionGuards.cpp.
  • The refiner's output on real game shaders, and on any input other than the hosted tests.
  • The guards test on any host (it was built on Windows and not run in CTest).

To get this to a good state

  • Run agc_goto_junction_guards_tests at this head in hosted CI. Nothing else blocking.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5733184900 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14636#pullrequestreview-5733184900). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T07:20:40Z. **Review:** COMMENTED --- # Reviewed — no blocking findings from the read; the guards test was built but did not run on Windows This PR adds `GotoPhiRefiner`, a shader optimization that narrows a goto junction Phi to the arm its branch allows, wired into `PrepareResourceProgram` after the first dead-code pass. I read the new pass and its header in full, and the wiring. The Windows CTest run passed the descriptor test. The guards test was compiled and linked on Windows, but it is not in the CTest selection, so it has no hosted pass. ## What I ran - Head `5ab18d0ce2f9a4e44eb332d809872dcb671a041b`; base `f2d874c1fdccf781c0fa5d86619bb5dd8894b958`. 10 files, +638/-1: `Optimization/src/GotoPhiRefiner.cpp` (+293, new), `Optimization/include/Optimization/GotoPhiRefiner.hpp` (+20, new), `Recompiler.cpp` (+5), `ShaderDiskCache.hpp` (`FormatVersion` 50 to 68), the recompiler CMake line, two AGC driver test sources and their CMake fragments, and a debt line. - Read the refiner and header in full: the dominator computation, the reachability and the fact collection from single-predecessor conditional targets, the memoized restriction of junction Phis, and the replacement of non-Phi operands. Read the wiring in `Recompiler.cpp`: when the refiner replaces any operand, dead-code elimination runs again. - Test sources: read the function names only. `GotoJunctionDescriptor.cpp` (103 lines) and `GotoJunctionGuards.cpp` (197 lines) were not read in full. - Hosted, Windows CTest: 2 cases were selected. `agc_goto_junction_descriptor` (Test #1828) passed, and `conventions_env_reference_test_a_name_the_pull_request_does_not_touch_is_not_reported` (Test #182) passed. The log shows `agc_goto_junction_guards_tests.exe` linked, and it is not in the selection. - Conventions: two runs on this head. The run started at 20:59:13 was cancelled. A later run started at 20:59:28 completed with success. - Read-only checks from the trusted main checkout: diff check, conventions and PR template, and the stack check all passed (no declared dependencies, no errors). ABI audit not applicable. - The test-quality helper flagged both new test sources as possibly unregistered. Each CMake fragment names its file by the relative path `tests/…cpp`, which the helper's literal match did not find. The Windows build compiled both. - No MarcZX10 review at this head. ## Results | Check | Result | Note | | --- | --- | --- | | Diff check | passed | | | Conventions and PR template | passed | the latest run succeeded; an earlier run on the same head was cancelled | | Mergeability and dependencies | passed | stack check: no declared dependencies, no errors | | ABI audit | not applicable | no export changed | | `agc_goto_junction_descriptor` (hosted, Windows) | passed | Test #1828 | | `agc_goto_junction_guards_tests` (hosted, Windows) | built, not run | linked, but not in the CTest selection | | Refiner logic (read) | passed by reading | no blocking defect found in the read | | Refiner results on the test inputs | not verified | test bodies not read | | Relinkers and relinker-asan (hosted) | passed | macOS, Ubuntu and Windows relinkers succeeded | | `library-portability` (hosted) | passed | | | Linux build and runtime tests | not run | Linux is skipped on pull requests | | Negative controls | not run | no local build; no PR-head code runs here | <details><summary>Read-only check output</summary> Precheck was not run: no PR-head code is run here. Diff check: passed. Conventions: passed. Stack check: passed, no declared dependencies, no errors. ABI audit: not applicable. Hosted: `gh api repos/boykopovar/AnyPS5/commits/5ab18d0ce2f9a4e44eb332d809872dcb671a041b/check-runs`, and the Windows job log. </details> ## Negative control Not run here. A negative control needs a local build and run of the shader tests, and code from this head is not run here. ## Findings **Blocking** None from the code read. **Non-blocking** - `Recompiler.cpp` runs the refiner for every shader that goes through `PrepareResourceProgram`, so a defect would reach every shader that uses a goto junction. The only hosted pass is the descriptor test. The guards test (197 lines) was not in the Windows selection, so nothing hosted exercises the junction guards. A run of `agc_goto_junction_guards_tests` at this head would close that gap. ## Sources - Refiner: https://github.com/boykopovar/AnyPS5/blob/5ab18d0ce2f9a4e44eb332d809872dcb671a041b/core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp - Header: https://github.com/boykopovar/AnyPS5/blob/5ab18d0ce2f9a4e44eb332d809872dcb671a041b/core/shader/recompiler/Optimization/include/Optimization/GotoPhiRefiner.hpp - Wiring: https://github.com/boykopovar/AnyPS5/blob/5ab18d0ce2f9a4e44eb332d809872dcb671a041b/core/shader/recompiler/Recompiler.cpp - Windows job log: check-run `windows` on 5ab18d0ce2f9a4e44eb332d809872dcb671a041b ## Not verified - The bodies of `GotoJunctionDescriptor.cpp` and `GotoJunctionGuards.cpp`. - The refiner's output on real game shaders, and on any input other than the hosted tests. - The guards test on any host (it was built on Windows and not run in CTest). ## To get this to a good state - Run `agc_goto_junction_guards_tests` at this head in hosted CI. Nothing else blocking.
XairRrR commented 2026-11-03 11:55:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T11:55:29Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The build, the conventions check, both new tests, the negative control and the guard mutations all pass. But a small hand-built IR graph shows that GotoPhiRefiner follows a nested guard Phi at a loop header and replaces a Phi with a value from a later loop iteration, which is a miscompile.

What I ran

Head 78e43aa03f1b7950af802cb55875f4092671b883, main 4f41f4c375f5abb487dbd184fbc150f31994d762, merged with --no-ff as 3be69c42c664ce529c76ffe7f5a0f96a9a36b324. Debian 13 VM (Xeon Gold 6136, AVX2), GCC 14.2.0, Vulkan through lavapipe (Mesa 26.1.6).

git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...78e43aa03f1b7950af802cb55875f4092671b883
python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762
env VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 VK_LOADER_DEBUG=error,warn,layer \
    ctest --test-dir build -V -R '^(agc_driver_native_depth_uniform_planes|agc_goto_junction_descriptor|agc_goto_junction_guards)$'
# negative control: Recompiler.cpp and ShaderDiskCache.hpp from main
ninja -C build agc_goto_junction_descriptor_tests agc_goto_junction_guards_tests
ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$'
# three single-guard mutations of GotoPhiRefiner.cpp, each followed by
ninja -C build agc_goto_junction_guards_tests && build/tests/agc_goto_junction_guards_tests
# the same validation run of agc_driver_native_depth_uniform_planes with the refiner call reverted
# a temporary extra check in GotoJunctionGuards.cpp (below), then everything restored to the merged tree

Results

Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Build (script) ok
Tests reached (script) 2238 ran, 0 new failures, 0 timeouts; 7 known failures that also fail on main
agc_goto_junction_descriptor passed
agc_goto_junction_guards passed (5 checks)
Validation, new VUID on agc_driver_native_depth_uniform_planes not from this PR (see below)
Mutations of the guards each one fails its own check (see below)
Loop-header nested-guard probe wrong replacement (see Blocking)

The script reported VUID-vkCmdDispatch-groupCountX-00386 as new on agc_driver_native_depth_uniform_planes, but that test was not in the older baseline (d1ae628bc2), because it reached main later. I ran the test with the validation layer on the merged tree and again with Recompiler.cpp and ShaderDiskCache.hpp restored from main, which takes the refiner out of the pipeline. Both runs give the same 8 VUID-vkCmdDispatch-groupCountX-00386 lines, so they come from main. The layer loaded in both runs: neither log has a "was not found but was requested" line.

Mutation results (guards test)
Mutation in GotoPhiRefiner.cpp Checks that fail
relied-block reachability check disabled (line 203) "nested junction value after the junction"
fact added at every block, not only where the target dominates "non-constant guard", "junction rerun before the use"
arms whose guard is neither a constant nor a Phi dropped "non-constant guard"

This matches what the author reported in the discussion.

Negative control

I restored core/shader/recompiler/Recompiler.cpp and ShaderDiskCache.hpp from main. That keeps the new pass compiled but no longer called. agc_goto_junction_descriptor then fails with the stop described in the PR:

goto junction descriptor: GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x0000003c(GetAddressResource(Phi(GetUserData(Void), ReadConst(GetSrtResource, Immediate)), Phi(GetUserData(Void), ReadConst(GetSrtResource, Immediate))), Immediate, Immediate, Immediate)

agc_goto_junction_guards calls the pass directly, so this revert does not affect it. Its control is the mutation table above. After all of this, the files were restored to the merged tree, rebuilt and retested (both pass), and git status --porcelain is empty.

Findings

Blocking

1. The nested guard Phi path picks a back-edge value that has been redefined since the inner junction ran. See GotoPhiRefiner.cpp lines 255–259.

When an outer junction's guard arm is a Phi of the same block as the value arm, RestrictArms restricts the inner Phi and returns the arm value it selects. That arm value is what the inner Phi received when the inner block was entered. If the inner block is a loop header and the selected arm comes from the latch, the defining instruction runs again between the inner header and the outer junction. The current value is then a different iteration's value.

The later checks do not catch this:

  • The definition does dominate the use, so the dominance check at line 197 passes.
  • The reachability check at line 203 only asks whether a relied block can reach the use without passing the outer junction. It does not ask whether the value was redefined between the inner and outer junctions.

To probe this, I temporarily added the following check to GotoJunctionGuards.cpp, using the file's own helpers:

entry:  initial = lane + 2                          -> header
header: innerGuard = phi(entry: false, latch: true)
        innerPhi   = phi(entry: initial, latch: counter)  -> body
body:   counter = innerPhi + 1; branch counter < 16 ? exit : latch
latch:  -> header
exit:   -> junction
junction: guard = phi(exit: innerGuard); merged = phi(exit: innerPhi); branch guard ? below : other
below:  ReferenceU32(merged)

Below the true branch, merged is innerPhi: the counter of the previous iteration, which is counter - 1. The pass replaces it with counter:

goto junction guards: loop header inner junction: replaced=1: the previous iteration value was replaced by the current iteration value

The pass runs on every shader in PrepareResourceProgram. It rewrites any non-Bool Phi operand, not only descriptor chains. A miscompile like this gives silently wrong values instead of a stop. I did not check whether the structurizer currently emits this exact shape (a goto guard Phi at a loop header that feeds an outer junction). The pass is written as a general IR rewrite, though, so it has to be sound on valid IR.

Suggested fix: in the nested case, accept a selected value only if its definition block dominates the inner Phi's block (immediates are always fine), or reject any arm whose predecessor is dominated by the inner Phi's block (a back edge). With that dominance condition, the definition cannot run again between the inner junction and the outer one: the inner block dominates the outer junction's predecessor, so the definition can't reach that predecessor without passing the inner block again. Please also add the graph above as a regression check in GotoJunctionGuards.cpp.

Non-blocking

None.

Not verified

  • Whether the shape in the blocking finding comes out of routeThroughJunction or routeLoopExits in practice. I did not run real title shaders or agc_shader_replay.
  • The Little Nightmares (0x200c340000) result in the description, which needs #7395, #9749 and #10000 on top.
  • The full agc_* suite. I ran only the selected tests and the touched tests listed above.
  • Disk cache format 68 being still free at merge time. I did not check the other open PRs.

To get this to a good state

  1. Restrict the nested guard path in RestrictArms so a selected value can't have been redefined after the inner junction, for example with the dominance condition above.
  2. Add the loop-header graph above as a check in GotoJunctionGuards.cpp. It should leave merged in place, and it should fail without the fix.
  3. Rerun the two new tests and the mutation checks.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5751745329 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14636#pullrequestreview-5751745329). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T11:55:29Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The build, the conventions check, both new tests, the negative control and the guard mutations all pass. But a small hand-built IR graph shows that `GotoPhiRefiner` follows a nested guard Phi at a loop header and replaces a Phi with a value from a later loop iteration, which is a miscompile. ## What I ran Head `78e43aa03f1b7950af802cb55875f4092671b883`, `main` `4f41f4c375f5abb487dbd184fbc150f31994d762`, merged with `--no-ff` as `3be69c42c664ce529c76ffe7f5a0f96a9a36b324`. Debian 13 VM (Xeon Gold 6136, AVX2), GCC 14.2.0, Vulkan through lavapipe (Mesa 26.1.6). ``` git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...78e43aa03f1b7950af802cb55875f4092671b883 python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762 env VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 VK_LOADER_DEBUG=error,warn,layer \ ctest --test-dir build -V -R '^(agc_driver_native_depth_uniform_planes|agc_goto_junction_descriptor|agc_goto_junction_guards)$' # negative control: Recompiler.cpp and ShaderDiskCache.hpp from main ninja -C build agc_goto_junction_descriptor_tests agc_goto_junction_guards_tests ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$' # three single-guard mutations of GotoPhiRefiner.cpp, each followed by ninja -C build agc_goto_junction_guards_tests && build/tests/agc_goto_junction_guards_tests # the same validation run of agc_driver_native_depth_uniform_planes with the refiner call reverted # a temporary extra check in GotoJunctionGuards.cpp (below), then everything restored to the merged tree ``` ## Results | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Build (script) | ok | | Tests reached (script) | 2238 ran, 0 new failures, 0 timeouts; 7 known failures that also fail on `main` | | `agc_goto_junction_descriptor` | passed | | `agc_goto_junction_guards` | passed (5 checks) | | Validation, new VUID on `agc_driver_native_depth_uniform_planes` | not from this PR (see below) | | Mutations of the guards | each one fails its own check (see below) | | Loop-header nested-guard probe | **wrong replacement** (see Blocking) | The script reported `VUID-vkCmdDispatch-groupCountX-00386` as new on `agc_driver_native_depth_uniform_planes`, but that test was not in the older baseline (`d1ae628bc2`), because it reached `main` later. I ran the test with the validation layer on the merged tree and again with `Recompiler.cpp` and `ShaderDiskCache.hpp` restored from `main`, which takes the refiner out of the pipeline. Both runs give the same 8 `VUID-vkCmdDispatch-groupCountX-00386` lines, so they come from `main`. The layer loaded in both runs: neither log has a "was not found but was requested" line. <details><summary>Mutation results (guards test)</summary> | Mutation in `GotoPhiRefiner.cpp` | Checks that fail | |---|---| | relied-block reachability check disabled (line 203) | "nested junction value after the junction" | | fact added at every block, not only where the target dominates | "non-constant guard", "junction rerun before the use" | | arms whose guard is neither a constant nor a Phi dropped | "non-constant guard" | This matches what the author reported in the discussion. </details> ## Negative control I restored `core/shader/recompiler/Recompiler.cpp` and `ShaderDiskCache.hpp` from `main`. That keeps the new pass compiled but no longer called. `agc_goto_junction_descriptor` then fails with the stop described in the PR: ``` goto junction descriptor: GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x0000003c(GetAddressResource(Phi(GetUserData(Void), ReadConst(GetSrtResource, Immediate)), Phi(GetUserData(Void), ReadConst(GetSrtResource, Immediate))), Immediate, Immediate, Immediate) ``` `agc_goto_junction_guards` calls the pass directly, so this revert does not affect it. Its control is the mutation table above. After all of this, the files were restored to the merged tree, rebuilt and retested (both pass), and `git status --porcelain` is empty. ## Findings ### Blocking **1. The nested guard Phi path picks a back-edge value that has been redefined since the inner junction ran.** See [`GotoPhiRefiner.cpp` lines 255–259](https://github.com/boykopovar/AnyPS5/blob/78e43aa03f1b7950af802cb55875f4092671b883/core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp#L255-L259). When an outer junction's guard arm is a Phi of the same block as the value arm, `RestrictArms` restricts the inner Phi and returns the arm value it selects. That arm value is what the inner Phi received *when the inner block was entered*. If the inner block is a loop header and the selected arm comes from the latch, the defining instruction runs again between the inner header and the outer junction. The current value is then a different iteration's value. The later checks do not catch this: - The definition does dominate the use, so the dominance check at line 197 passes. - The reachability check at line 203 only asks whether a relied block can reach the use without passing the outer junction. It does not ask whether the value was redefined between the inner and outer junctions. To probe this, I temporarily added the following check to `GotoJunctionGuards.cpp`, using the file's own helpers: ``` entry: initial = lane + 2 -> header header: innerGuard = phi(entry: false, latch: true) innerPhi = phi(entry: initial, latch: counter) -> body body: counter = innerPhi + 1; branch counter < 16 ? exit : latch latch: -> header exit: -> junction junction: guard = phi(exit: innerGuard); merged = phi(exit: innerPhi); branch guard ? below : other below: ReferenceU32(merged) ``` Below the true branch, `merged` is `innerPhi`: the counter of the *previous* iteration, which is `counter - 1`. The pass replaces it with `counter`: ``` goto junction guards: loop header inner junction: replaced=1: the previous iteration value was replaced by the current iteration value ``` The pass runs on every shader in `PrepareResourceProgram`. It rewrites any non-Bool Phi operand, not only descriptor chains. A miscompile like this gives silently wrong values instead of a stop. I did not check whether the structurizer currently emits this exact shape (a goto guard Phi at a loop header that feeds an outer junction). The pass is written as a general IR rewrite, though, so it has to be sound on valid IR. Suggested fix: in the nested case, accept a selected value only if its definition block dominates the inner Phi's block (immediates are always fine), or reject any arm whose predecessor is dominated by the inner Phi's block (a back edge). With that dominance condition, the definition cannot run again between the inner junction and the outer one: the inner block dominates the outer junction's predecessor, so the definition can't reach that predecessor without passing the inner block again. Please also add the graph above as a regression check in `GotoJunctionGuards.cpp`. ### Non-blocking None. ## Not verified - Whether the shape in the blocking finding comes out of `routeThroughJunction` or `routeLoopExits` in practice. I did not run real title shaders or `agc_shader_replay`. - The Little Nightmares (`0x200c340000`) result in the description, which needs #7395, #9749 and #10000 on top. - The full `agc_*` suite. I ran only the selected tests and the touched tests listed above. - Disk cache format 68 being still free at merge time. I did not check the other open PRs. ## To get this to a good state 1. Restrict the nested guard path in `RestrictArms` so a selected value can't have been redefined after the inner junction, for example with the dominance condition above. 2. Add the loop-header graph above as a check in `GotoJunctionGuards.cpp`. It should leave `merged` in place, and it should fail without the fix. 3. Rerun the two new tests and the mutation checks. <!-- anyps5-bots job=3220 head=78e43aa03f1b7950af802cb55875f4092671b883 -->
XairRrR commented 2026-11-03 12:17:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T12:17:28Z.

Review: APPROVED


Re-checked — approved

Commit 0eac5c8070 adds two conditions to the nested-guard branch of RestrictArms, which closes the loop-header miscompile from my last review. The new "loop header inner junction" check fails without the fix and passes with it.

What changed since my last review

  • One new commit, 0eac5c8070. The previous five commits are unchanged, per the range-diff.
  • GotoPhiRefiner.cpp lines 255–266 now requires two things before it narrows through an inner junction:
    • the inner junction dominates the predecessor edge into the outer one;
    • a non-immediate selected value is defined in a block that strictly dominates the inner junction.
  • GotoJunctionGuards.cpp adds CheckLoopHeaderInnerJunction. It is the graph from my last review, and it requires that merged is left in place.
  • In the discussion, the author's reply describes the fix. Their Windows run was announced but not yet posted.

What I ran

Head 0eac5c8070969ea7ffa6b5725678d82be7ee4f1d, main 4f41f4c375f5abb487dbd184fbc150f31994d762, merged tree cedd8e001c67b76b700a8e2cd306b91713d913df.

git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...0eac5c8070969ea7ffa6b5725678d82be7ee4f1d
python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762
git diff 78e43aa03f1b7950af802cb55875f4092671b883 0eac5c8070969ea7ffa6b5725678d82be7ee4f1d
ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$'
# negative control: GotoPhiRefiner.cpp from 78e43aa03f (the previous head), keeping the new test
ninja -C build agc_goto_junction_guards_tests agc_goto_junction_descriptor_tests
ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$'
# mutation A: drop only the "inner junction dominates the predecessor" condition
# mutation B: drop only the "definition strictly dominates the inner junction" condition
ninja -C build agc_goto_junction_guards_tests
build/tests/agc_goto_junction_guards_tests
# restore GotoPhiRefiner.cpp from the merged tree, rebuild, rerun both tests
git status --porcelain
Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Build and selected tests (script) 2238 ran, 0 new failures, 0 timeouts; the 7 known failures also fail on main
agc_goto_junction_descriptor passed
agc_goto_junction_guards (6 checks) passed
Negative control: refiner at the previous head "loop header inner junction" fails
Mutation B: strict-dominance condition removed "loop header inner junction" fails
Mutation A: predecessor-dominance condition removed all checks pass (see below)
Worktree after restoring git status --porcelain is empty; both tests pass again
Negative control output
goto junction guards: loop header inner junction: a junction Phi was narrowed to a loop value redefined after the inner junction it came through

Validation: the script again lists VUID-vkCmdDispatch-groupCountX-00386 as new on agc_driver_native_depth_uniform_planes, marked "(test not run on main)". In my last review I showed that this VUID comes from main: it shows up the same way with the refiner taken out of the pipeline. This push changes only the body of RestrictArms, not the pipeline, so it is not a finding. I did not rerun that comparison at this head.

Previous findings

  1. The nested guard Phi path picks a back-edge value that has been redefined since the inner junction ran: Resolved.
    • The probe graph is now in the test file as CheckLoopHeaderInnerJunction (GotoJunctionGuards.cpp:175). It passes at the head and fails with the refiner from the previous head.
    • The strict-dominance condition is the one that matters: mutation B fails the check.
    • Why the fix is sound: let D be the block that defines the value. D strictly dominates the inner block, so entry reaches D without passing the inner block. Since the inner block dominates the predecessor P of the outer junction, any path from D to P passes the inner block again. So no D runs after the inner junction's last entry and before P, and the value is the same instance the inner Phi received.
    • Refusing D equal to the inner block is right, because a latch arm can name a value defined in the header itself.

New findings

Blocking

None.

Non-blocking

None. One observation: no test pins the condition that the inner junction dominates the predecessor (mutation A passes). On valid SSA it already holds: the outer guard Phi's arm from P is the inner guard Phi, and a Phi operand must dominate its incoming predecessor. The test helper runs ValidateProgram before the pass, so no valid graph can reach this branch with the condition false. It is a cheap defensive check, and it's fine as it is.

Not verified

  • The author's announced Windows run, including whether Little Nightmares 0x200c340000 still prepares with the stricter condition. It was not posted at the time of this review, and I did not run agc_shader_replay or any title shaders.
  • The full agc_* suite; only the tests the script selected and the two new tests ran.
  • The validation comparison for agc_driver_native_depth_uniform_planes at this head (it was checked at the previous head; see above).
  • Whether disk-cache format 68 is still free when this merges.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5752006783 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14636#pullrequestreview-5752006783). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T12:17:28Z. **Review:** APPROVED --- # Re-checked — approved Commit `0eac5c8070` adds two conditions to the nested-guard branch of `RestrictArms`, which closes the loop-header miscompile from my last review. The new "loop header inner junction" check fails without the fix and passes with it. ## What changed since my last review - One new commit, `0eac5c8070`. The previous five commits are unchanged, per the range-diff. - [`GotoPhiRefiner.cpp` lines 255–266](https://github.com/boykopovar/AnyPS5/blob/0eac5c8070969ea7ffa6b5725678d82be7ee4f1d/core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp#L255-L266) now requires two things before it narrows through an inner junction: - the inner junction dominates the predecessor edge into the outer one; - a non-immediate selected value is defined in a block that strictly dominates the inner junction. - `GotoJunctionGuards.cpp` adds `CheckLoopHeaderInnerJunction`. It is the graph from my last review, and it requires that `merged` is left in place. - In the discussion, the author's reply describes the fix. Their Windows run was announced but not yet posted. ## What I ran Head `0eac5c8070969ea7ffa6b5725678d82be7ee4f1d`, `main` `4f41f4c375f5abb487dbd184fbc150f31994d762`, merged tree `cedd8e001c67b76b700a8e2cd306b91713d913df`. ``` git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...0eac5c8070969ea7ffa6b5725678d82be7ee4f1d python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762 git diff 78e43aa03f1b7950af802cb55875f4092671b883 0eac5c8070969ea7ffa6b5725678d82be7ee4f1d ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$' # negative control: GotoPhiRefiner.cpp from 78e43aa03f (the previous head), keeping the new test ninja -C build agc_goto_junction_guards_tests agc_goto_junction_descriptor_tests ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$' # mutation A: drop only the "inner junction dominates the predecessor" condition # mutation B: drop only the "definition strictly dominates the inner junction" condition ninja -C build agc_goto_junction_guards_tests build/tests/agc_goto_junction_guards_tests # restore GotoPhiRefiner.cpp from the merged tree, rebuild, rerun both tests git status --porcelain ``` | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Build and selected tests (script) | 2238 ran, 0 new failures, 0 timeouts; the 7 known failures also fail on `main` | | `agc_goto_junction_descriptor` | passed | | `agc_goto_junction_guards` (6 checks) | passed | | Negative control: refiner at the previous head | "loop header inner junction" fails | | Mutation B: strict-dominance condition removed | "loop header inner junction" fails | | Mutation A: predecessor-dominance condition removed | all checks pass (see below) | | Worktree after restoring | `git status --porcelain` is empty; both tests pass again | <details><summary>Negative control output</summary> ``` goto junction guards: loop header inner junction: a junction Phi was narrowed to a loop value redefined after the inner junction it came through ``` </details> Validation: the script again lists `VUID-vkCmdDispatch-groupCountX-00386` as new on `agc_driver_native_depth_uniform_planes`, marked "(test not run on main)". In my last review I showed that this VUID comes from `main`: it shows up the same way with the refiner taken out of the pipeline. This push changes only the body of `RestrictArms`, not the pipeline, so it is not a finding. I did not rerun that comparison at this head. ## Previous findings 1. **The nested guard Phi path picks a back-edge value that has been redefined since the inner junction ran: Resolved.** - The probe graph is now in the test file as `CheckLoopHeaderInnerJunction` (`GotoJunctionGuards.cpp:175`). It passes at the head and fails with the refiner from the previous head. - The strict-dominance condition is the one that matters: mutation B fails the check. - Why the fix is sound: let D be the block that defines the value. D strictly dominates the inner block, so entry reaches D without passing the inner block. Since the inner block dominates the predecessor P of the outer junction, any path from D to P passes the inner block again. So no D runs after the inner junction's last entry and before P, and the value is the same instance the inner Phi received. - Refusing D equal to the inner block is right, because a latch arm can name a value defined in the header itself. ## New findings ### Blocking None. ### Non-blocking None. One observation: no test pins the condition that the inner junction dominates the predecessor (mutation A passes). On valid SSA it already holds: the outer guard Phi's arm from P is the inner guard Phi, and a Phi operand must dominate its incoming predecessor. The test helper runs `ValidateProgram` before the pass, so no valid graph can reach this branch with the condition false. It is a cheap defensive check, and it's fine as it is. ## Not verified - The author's announced Windows run, including whether Little Nightmares `0x200c340000` still prepares with the stricter condition. It was not posted at the time of this review, and I did not run `agc_shader_replay` or any title shaders. - The full `agc_*` suite; only the tests the script selected and the two new tests ran. - The validation comparison for `agc_driver_native_depth_uniform_planes` at this head (it was checked at the previous head; see above). - Whether disk-cache format 68 is still free when this merges. <!-- anyps5-bots job=3324 head=0eac5c8070969ea7ffa6b5725678d82be7ee4f1d -->
XairRrR commented 2026-11-03 12:48:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T12:48:13Z.

Review: APPROVED


Re-checked — approved

The only change is commit 0a68c34c10. It removes the check that the inner junction dominates the edge into the outer one. My last review called that check defensive and said valid SSA already guarantees it. The strict-dominance condition that fixes the loop-header case is still there and still tested.

What changed since my last review

  • One new commit, 0a68c34c10. The range-diff shows the previous six commits unchanged.
  • In GotoPhiRefiner.cpp lines 255–264, the from/Dominates(block, from) test is gone. The nested-guard branch now only needs the inner block to be indexed. Then the selected value's definition must strictly dominate the inner junction.
  • The author posted Windows and WSL2 results for 0eac5c8070, plus a Little Nightmares replay: 0x200c340000 still prepares, with the same word counts with or without the fix. I accept these as author-reported.

What I ran

Head 0a68c34c101094622d75fed71f63cfa1bad453a2, main 4f41f4c375f5abb487dbd184fbc150f31994d762, merged tree b2744169eb04f8035a4b5c39b63aed0c7249d9bf.

git diff 0eac5c8070969ea7ffa6b5725678d82be7ee4f1d 0a68c34c101094622d75fed71f63cfa1bad453a2
git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...0a68c34c101094622d75fed71f63cfa1bad453a2
python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762
ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$'
# negative control: GotoPhiRefiner.cpp from 78e43aa03f (before the loop-header fix), tests kept
git show 78e43aa03f1b7950af802cb55875f4092671b883:core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp > core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp
ninja -C build -j32 -l32 agc_goto_junction_guards_tests agc_goto_junction_descriptor_tests
ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$'
# restore from the merged tree, rebuild, rerun
git show b2744169eb04f8035a4b5c39b63aed0c7249d9bf:core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp > core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp
ninja -C build -j32 -l32 agc_goto_junction_guards_tests agc_goto_junction_descriptor_tests
ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$'
git status --porcelain
Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Build and selected tests (script) 2222 ran, 0 new failures, 0 timeouts; the 7 known failures also fail on main
Vulkan validation (script) 770 tests, layer loaded in 769, layer missing in none, 0 tests with new VUIDs
agc_goto_junction_descriptor passed
agc_goto_junction_guards passed
Negative control (refiner from 78e43aa03f) "loop header inner junction" fails; descriptor test passes
After restoring both tests pass; git status --porcelain is empty
Negative control output
goto junction guards: loop header inner junction: a junction Phi was narrowed to a loop value redefined after the inner junction it came through
50% tests passed, 1 tests failed out of 2

Previous findings

  • My last review had no blocking or non-blocking findings.

  • Observation: no test pinned the predecessor-dominance condition. No longer applies, because the condition was removed. The author's reasoning holds: IrProgram.cpp lines 783–786 rejects a Phi operand whose definition neither dominates its incoming predecessor nor sits in it. The arm is a Phi of the inner block, so the inner block dominates the predecessor P (or is P). My earlier soundness argument still works for every reachable P:

    • the value's block D strictly dominates the inner block;
    • so any path from D to P passes the inner block again.

    For a predecessor that is unreachable from entry, the old code returned nullptr and the new code may continue. That arm never runs, and Refined still requires the replacement to dominate the use, so this does not change results.

New findings

Blocking

None.

Non-blocking

None.

Not verified

  • The author's Windows and WSL2 runs and the Little Nightmares replay are author-reported; I did not run agc_shader_replay or any title shaders.
  • The full agc_* suite; only the tests the script selected and the two junction tests ran.
  • Whether disk-cache format 68 is still free when this merges.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5752397353 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14636#pullrequestreview-5752397353). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T12:48:13Z. **Review:** APPROVED --- # Re-checked — approved The only change is commit `0a68c34c10`. It removes the check that the inner junction dominates the edge into the outer one. My last review called that check defensive and said valid SSA already guarantees it. The strict-dominance condition that fixes the loop-header case is still there and still tested. ## What changed since my last review - One new commit, `0a68c34c10`. The range-diff shows the previous six commits unchanged. - In [`GotoPhiRefiner.cpp` lines 255–264](https://github.com/boykopovar/AnyPS5/blob/0a68c34c101094622d75fed71f63cfa1bad453a2/core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp#L255-L264), the `from`/`Dominates(block, from)` test is gone. The nested-guard branch now only needs the inner block to be indexed. Then the selected value's definition must strictly dominate the inner junction. - The author posted Windows and WSL2 results for `0eac5c8070`, plus a Little Nightmares replay: `0x200c340000` still prepares, with the same word counts with or without the fix. I accept these as author-reported. ## What I ran Head `0a68c34c101094622d75fed71f63cfa1bad453a2`, `main` `4f41f4c375f5abb487dbd184fbc150f31994d762`, merged tree `b2744169eb04f8035a4b5c39b63aed0c7249d9bf`. ``` git diff 0eac5c8070969ea7ffa6b5725678d82be7ee4f1d 0a68c34c101094622d75fed71f63cfa1bad453a2 git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...0a68c34c101094622d75fed71f63cfa1bad453a2 python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762 ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$' # negative control: GotoPhiRefiner.cpp from 78e43aa03f (before the loop-header fix), tests kept git show 78e43aa03f1b7950af802cb55875f4092671b883:core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp > core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp ninja -C build -j32 -l32 agc_goto_junction_guards_tests agc_goto_junction_descriptor_tests ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$' # restore from the merged tree, rebuild, rerun git show b2744169eb04f8035a4b5c39b63aed0c7249d9bf:core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp > core/shader/recompiler/Optimization/src/GotoPhiRefiner.cpp ninja -C build -j32 -l32 agc_goto_junction_guards_tests agc_goto_junction_descriptor_tests ctest --test-dir build --output-on-failure -R '^agc_goto_junction_(descriptor|guards)$' git status --porcelain ``` | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Build and selected tests (script) | 2222 ran, 0 new failures, 0 timeouts; the 7 known failures also fail on `main` | | Vulkan validation (script) | 770 tests, layer loaded in 769, layer missing in none, 0 tests with new VUIDs | | `agc_goto_junction_descriptor` | passed | | `agc_goto_junction_guards` | passed | | Negative control (refiner from `78e43aa03f`) | "loop header inner junction" fails; descriptor test passes | | After restoring | both tests pass; `git status --porcelain` is empty | <details><summary>Negative control output</summary> ``` goto junction guards: loop header inner junction: a junction Phi was narrowed to a loop value redefined after the inner junction it came through 50% tests passed, 1 tests failed out of 2 ``` </details> ## Previous findings - My last review had no blocking or non-blocking findings. - **Observation: no test pinned the predecessor-dominance condition.** No longer applies, because the condition was removed. The author's reasoning holds: [`IrProgram.cpp` lines 783–786](https://github.com/boykopovar/AnyPS5/blob/0a68c34c101094622d75fed71f63cfa1bad453a2/core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp#L783-L786) rejects a Phi operand whose definition neither dominates its incoming predecessor nor sits in it. The arm is a Phi of the inner block, so the inner block dominates the predecessor P (or is P). My earlier soundness argument still works for every reachable P: - the value's block D strictly dominates the inner block; - so any path from D to P passes the inner block again. For a predecessor that is unreachable from entry, the old code returned `nullptr` and the new code may continue. That arm never runs, and `Refined` still requires the replacement to dominate the use, so this does not change results. ## New findings ### Blocking None. ### Non-blocking None. ## Not verified - The author's Windows and WSL2 runs and the Little Nightmares replay are author-reported; I did not run `agc_shader_replay` or any title shaders. - The full `agc_*` suite; only the tests the script selected and the two junction tests ran. - Whether disk-cache format 68 is still free when this merges. <!-- anyps5-bots job=3336 head=0a68c34c101094622d75fed71f63cfa1bad453a2 -->
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!14636
No description provided.