Repository navigation
fix(shader): narrow a goto junction Phi to the arm its branch allows - #14636
Conversation
When the structurizer routes several regions through one junction, a value that differs between them becomes a Phi at the junction, and the goto variable that picks the next region becomes a Bool Phi beside it. Below a branch on that Bool Phi only the arms whose goto value matches the branch side can arrive, but the resource tracker still sees every arm. A descriptor address that is the call site's value on the taken side then merges with other callees' scratch values and cannot be resolved (Little Nightmares' 0x200c340000, discussion boykopovar#6275). GotoPhiRefiner runs before the SRT walk. For each operand below such a branch it keeps the arms whose guard is a constant equal to the branch side, following parallel guard Phis of nested junctions, and replaces the operand when one value is left. The fact must hold on every path to the use: the use is dominated by the branch's single-predecessor target and is not reachable from the junction around it. The value must dominate the use, and no block it relies on may reach the use without passing the junction. Results are kept per fact set, since the same junction Phis sit under many arms. The disk cache format moves to 66.
…ves to that exit's pointer A loop whose two exits end the program separately has no common post-dominator, so its exits are routed through a goto-variable junction. The early exit leaves with s[16:17] set from user data s[4:5]; the latch exit leaves after a constant-buffer load overwrites them. The early-exit block loads a T# through s[16:17] and reads an image, so its address is the junction Phi unless the branch on the goto variable narrows it. On main this stops with "GetImageResource dword 0 is not a valid runtime value"; the test checks that all eight T# dwords are read through s[4:5].
|
Thanks for the detailed write-up. I went through Since the pass runs on every shader and rewrites any narrowable operand (not only descriptor chains), could you add negative tests that pin those three guards? Right now removing any one of them still passes
They can be IR-level checks like the existing test (planned resource count and operand shape). Also please check that disk-cache format 66 is still free when this lands, since another PR taking the same number would merge without a conflict. Reviewed at dc12552. |
|
@oneandonlydean Thanks for the review. I'll add all three as negative tests:
Each test leaves its operand a Phi. Each will also be checked to fail when only its own guard is removed, so they pin the guards rather than just passing. I'll post the mutation results with the push. The disk-cache format: 66 is still free today, since open PRs hold 51 to 65. I'll check again when this is about to land. |
… tests Each check builds IR by hand and runs GotoPhiRefiner directly. A junction Phi arm whose goto guard is not a constant must stay. A latch arm that is a nested junction value 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. The constant-guard and entry-value twins show the same shapes are narrowed when the guards allow it.
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
… dominance implies A fact needs the branch target to dominate the use. The junction dominates the branch, so entry reaches the junction without passing the target; a path from the junction to the use that avoided the target would then avoid it from entry too. The use is never stale where the target dominates it, so the reachability walk per fact is dropped. The rerun check of agc_goto_junction_guards now pins the dominance condition.
|
@oneandonlydean Added in 10c6526 ( Mutation check. Each guard was removed locally, rebuilt and run on Windows with GCC 15.2:
Why
38fc459 drops the check and its reachability walk per fact. With it gone, the rerun check pins the dominance condition: without that condition, the fact reaches the use through the other branch. The positive test and the four other checks are unchanged. |
|
Confirmed at 38fc459 on Windows (GCC 15.2) and WSL2 (GCC 13.3):
|
boykopovar#8939 now takes 66 and boykopovar#9457 67, and the same number in two PRs would merge without a conflict.
|
@oneandonlydean You were right to ask about the format. #8939 moved to 66 on Oct 31, and #9457 is at 67. Two PRs with the same number would have merged without a conflict, so 5ab18d0 moves this one to 68. I read the |
✅ NVIDIA GPU test: passedPR head
Unstable on main (3, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (4, not counted against this PR)Validated but not compared (2, not in the main baseline's validation pass)Validation IDs already present on main (5 IDs in 484 tests)Skipped (46)ReproduceAutomated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
MarcZX10
left a comment
There was a problem hiding this comment.
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; basef2d874c1fdccf781c0fa5d86619bb5dd8894b958. 10 files, +638/-1:Optimization/src/GotoPhiRefiner.cpp(+293, new),Optimization/include/Optimization/GotoPhiRefiner.hpp(+20, new),Recompiler.cpp(+5),ShaderDiskCache.hpp(FormatVersion50 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) andGotoJunctionGuards.cpp(197 lines) were not read in full. - Hosted, Windows CTest: 2 cases were selected.
agc_goto_junction_descriptor(Test #1828) passed, andconventions_env_reference_test_a_name_the_pull_request_does_not_touch_is_not_reported(Test #182) passed. The log showsagc_goto_junction_guards_tests.exelinked, 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.cppruns the refiner for every shader that goes throughPrepareResourceProgram, 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 ofagc_goto_junction_guards_testsat 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
windowson 5ab18d0
Not verified
- The bodies of
GotoJunctionDescriptor.cppandGotoJunctionGuards.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_testsat this head in hosted CI. Nothing else blocking.
|
@MarcZX10 Both junction tests at the exact head 5ab18d0, merged into
Why the hosted run skipped the guards test: on pull requests the Windows job runs the tests that |
…shader-goto-junction-phis-main # Conflicts: # core/shader/recompiler/ShaderDiskCache.hpp
|
Merged current AI-assisted: yes (Claude Code) |
|
Rerun at Each set run once:
I ran no control this time, because the earlier runs covered it. AI-assisted: yes (Claude Code) |
|
Tested on PPSA01317 (01.710.000), Linux, RTX 5070, on a local branch of The same 29 compute shaders stay refused with The chain of The pointer read at pc 0x191a0 is still a Phi of the record word and of two values loaded at pc 0x2920 and 0x311c. The same arms are in the chain without this change, which stops at pc 0x3554 instead. The printed chain cuts off below those two loads, so I can't tell what they read. I have not looked at why this read is not narrowed; pc 0x19420 may be in a linked table callee, which I have not confirmed. |
|
Thanks @freneza, this is useful. When 28 chains move from pc 0x3554–0x35b0 to 0x19420–0x1947c, the pass has narrowed a junction Phi in each of those shaders. The walk used to stop at that merge, and it now gets past it to the next pointer read. The pass only narrows when three conditions hold (the
The Phi at pc 0x191a0 can miss any of these, and the chain alone doesn't show which:
If you can attach the capture of I'll add your run to Tested as a partial result. On PPSA01317 with that stack, 28 of the 29 refused chains end later and none resolves. The PR doesn't claim to unblock PPSA01317. AI-assisted: yes (Claude Code) |
|
One correction before the capture: comparing the full chains of A capture holds title code and guest memory, so I'd rather not attach it publicly. I'll run the replay locally and report the junction block, the facts at the use and the refusal reason. |
|
@freneza agreed, and thanks for the correction. I overstated it in my last comment. The pass narrows per use. The use at 0x353c was narrowed, so the walk moves on. At 0x19420 the same Phi tree is read again unnarrowed, so there is no fact there, or
Logging the fact count at the use's block and which of these returns AI-assisted: yes (Claude Code) |
|
Thanks. I replayed the capture locally on It's the recursion limit. The use at 0x191a0 has 27 facts, including The loop-header exclusion isn't involved, and I changed no guard. On that request:
I'd prefer the repeated pass: it keeps the recursion depth bounded, and every replacement still goes through the same Limits: this is |
…es nothing One round stops at MaxRefinementDepth, so a use below a long chain of goto junctions was replaced only by a Phi part way down the chain, and the SRT walk still met a merge. freneza found this on PPSA01317, where a use about 20 junctions deep kept the full Phi tree. Run the pass again, up to 32 rounds, while a round replaces an operand; each round starts with fresh memos and every replacement goes through the same checks.
|
@freneza thanks, that settles it. I took your repeated-pass variant:
If you rerun AI-assisted: yes (Claude Code) |
|
Rerun at
AI-assisted: yes (Claude Code) |
|
Thanks. I replayed all 29 captures on my stack with The mean replay time per shader went from 41 s to 51 s, with 7 replays in parallel, so that's only a relative figure. These are replays and that one test on my stack, not a game run, and not the PR alone. |
|
@freneza thanks for running all 29. I've added both of your runs to Tested, including the replay time, 41 s to 51 s per shader with 7 in parallel. That cost is the extra rounds. Each round recomputes dominators and facts for the whole program; only the memos have to start fresh. If the time matters, the dominators and facts could be computed once and shared across rounds, since the pass only replaces operands and never changes the CFG. I'd keep that for a follow-up unless a reviewer wants it here.
AI-assisted: yes (Claude Code) |
|
@freneza, the #10489 raises the limit to 128, and its refusal prints the count, as On #9969 (ce65f49) plus this PR's 8e5b82d, #10489 (74407aa) conflicts in two places:
|
|
@zaryabzip thanks for adding the runs. The chain of Here the pointer comes from a constant-buffer read at pc 0xac8. Each of that buffer's four V# dwords is a nested Phi, and the offset is built from @EagleBearerYS thanks. With |
|
@freneza thanks.
Both need their own work, so I'll leave it out of this PR. On module size, thanks for the numbers in #13538. That's where it belongs. AI-assisted: yes (Claude Code) |
|
@XairRrR your approval is on an earlier head. Since then,
freneza found the case on PPSA01317: a use about 20 junctions deep that one round left unnarrowed at the recursion limit. The rerun on Windows and WSL2 is in Tested, with the old pass as a control. If you have time, a quick look at that commit would help: it is 7 lines in AI-assisted: yes (Claude Code) |
…shader-goto-junction-phis-main # Conflicts: # core/shader/recompiler/ShaderDiskCache.hpp
|
Rerun after the main merge. This is the Windows and WSL2 run of
AI-assisted: yes (Claude Code) |
|
Re-read the changes since the approved head: |
Takes main's FormatVersion 68 from #14636, which is above this branch's 63; the next commit gives this branch its own number.
…at, 79 `main` moved to 68 with #14636, above this PR's 63, and 69 to 78 are held by open PRs, so this PR takes 79 and a cache written by another branch's recompiler is not read as this one's.
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
routeThroughJunctionfor an entered region, and inrouteLoopExitsfor 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 withGetImageResource 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.
The disk cache format moves to 68. A
shader.mddebt entry records which merges stay unnarrowed.Where it showed up. In Little Nightmares (PPSA10737, discussion #6275), on
main+ #7395 + #9749 + #10000, compute shader0x200c340000stopped 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 plainmain, 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.cppbuilds a 64-wide compute program:s[16:17]from user datas[4:5]and leaves early ons_cmp_eq_u32 s8, s6;s_buffer_load_dwordx2and loops;routeLoopExitsbuilds a goto-variable junction;s[16:17]and runsimage_load.The test checks that one image is planned and that each of its eight dwords is read through the SRT from a
LoadAddressU32whose address isGetUserDataofs4ands5, not a Phi.GotoJunctionGuards.cppbuilds IR by hand and runs the pass directly. Each guard gets a check that fails when that guard is removed:Two twin checks show the same shapes being narrowed when the guards allow it.
Overlap:
git merge-treeagainstmainand every open pull request finds conflicts only on theFormatVersionline ofShaderDiskCache.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
mainf2d874c, at a commit with the same code and test as the head (only the debt entry's wording changed since).agc_goto_junction_descriptor, 3 runs each:mainwith only the test, it fails 3 of 3 on both hosts withGetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x0000003c(GetAddressResource(Phi(GetUserData(Void), ReadConst(GetSrtResource, Immediate)), Phi(...)), ...).agc_goto_junction_guards: pass 3 of 3 on both hosts. In a mutation check, removing each guard fails only its own check:agc_*suite, Windows only, RTX 3060 Laptop GPU, NVIDIA 617.42,ANYPS5_NO_SHADER_CACHE=1:main: 1202 passed and 9 skipped, of 1211.agc_shader_replay --prepare):0x200c340000prepares, where before it stopped at pc 0x119b8. On this PR's head447e80bde4with feat(shader): read samplers through nested and pointer-keyed descriptor tables #7395, fix(shader): read a chased pointer's high word at its own offset #9749 and Ratchet & Clank: Rift Apart (PPSA01474): the compute shader whose s_swappc_b64 calls a computed target becomes a 2.5M-word SPIR-V module that RADV cannot compile (nir_opt_copy_prop_vars grows past 16 GB; Mesa 25.2.8 and 26.2.6) #13538's opt-in switches (unset) onmain5b1d381d5a, it is 7,046,975 SPIR-V words; at0eac5c8070with fix(shader): read a chased pointer's high word at its own offset #9749 onmain4f41f4c375, 7,045,511. An earlier, pre-PR version of the pass gave 6,222,886, which this description used to quote;0x2010fa0000still prepared, with 479,939 words, and theagc_*suite showed no change;vkCreateComputePipelineswith-13. That is the module-size problem tracked in Ratchet & Clank: Rift Apart (PPSA01474): the compute shader whose s_swappc_b64 calls a computed target becomes a 2.5M-word SPIR-V module that RADV cannot compile (nir_opt_copy_prop_vars grows past 16 GB; Mesa 25.2.8 and 26.2.6) #13538, not a resource refusal (run).tools/check_conventions.pypasses.GotoPhiRefiner.cppbuilds with no warnings;mainmerge, by me ate92fa373fe, which containsmain4977f64052(comment): on Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), bothagc_goto_junctiontests pass,ctest -R shaderpasses 112 of 112 and 104 of 104, and the 37 CPU-side tests pass 37 of 37. The merge keepsFormatVersion68 overmain's 57.main4977f64052with the GPU pull requests listed in Gran Turismo 7 (PPSA01317) #4697 andlimitCalleesraised to 256 locally. These areagc_shader_replay --preparereplays of the 29 compute shaders that were refused, not a game run:e92fa373fe(comment), all 29 still stop withGetImageResource 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 atMaxRefinementDepth.8e5b82d0f2(comment), those 28 get past the refusal and stop onimage resource limit exceeded(ShaderInfo::MaxImages, outside this PR), so none prepares yet.0x120aed7f00still 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_guardspasses 3 of 3 there.8e5b82d0f2(comment). On Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), bothagc_goto_junctiontests pass 3 of 3,ctest -R shaderpasses 112 of 112 and 104 of 104, and the 37 CPU-side tests pass 37 of 37. Withe92fa373fe'sGotoPhiRefiner.cpp,agc_goto_junction_guardsfails only "deep junction chain" (Windows). No warnings inGotoPhiRefiner.cpporGotoJunctionGuards.cpp.mainmerge, by me at1560cfdef0, which mergesmain095ff72367(comment). On Windows (WinLibs GCC 15.2.0 r7, Release) and on WSL2 (GCC 13.3.0, Release), each set run once: bothagc_goto_junctiontests pass,ctest -R shaderpasses 117 of 117 and 109 of 109, and the 37 CPU-side tests pass 37 of 37. The merge keepsFormatVersion68 overmain's 60. No warnings inGotoPhiRefiner.cpp,GotoJunctionDescriptor.cpporGotoJunctionGuards.cpp.agc_add_default_testturns it off.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)