Repository navigation
recompiler: fold spilled lane reads across lane-compaction writes, guarded at run time (#6191, layer 1) - #27207
humanityBG wants to merge 1 commit into
Conversation
…arded at run time Gran Turismo 7 (PPSA01317) compute shader 0x120aed7f00 (boykopovar#6191) indexes a constant buffer with ReadLane(WriteLane(...), 29) and ReadLane(..., 28): SGPR spills written once at immediate lanes and read back later. ReadLaneEliminator's unique-value fallback gave up on them because the same vector is the target of a lane-compaction loop (v_writelane v, v_readlane(w, s_lane), s_count: a running count as the lane), and because the vector's Phi/Select web has about 6,200 nodes, above the 4,096 visit cap. - collectLaneValues steps over a write that matches the compaction shape exactly (value = v_readlane at a non-immediate lane, lane = a loop-carried phi stepped by immediates); any other non-immediate lane write still fails the walk. - A fold that stepped over such writes rests on the count never reaching the spilled lane while the spill is live, which the IR cannot prove. The fold keeps one real read of that lane next to it and a new ReadLaneGuard records a ReadLaneFold fault if the two differ, like WindowLaneGuard does for spilled window lanes. The guard counts as a lane guard for the driver's fault checks. - The visit cap grows to 65,536 only in a program that has a lane-compaction write; everything else keeps 4,096. - APS5_TRACE_READLANE=1 prints one line per program with the counts. With it, 23 lane reads of the GT7 request fold (20 across compaction writes) and the index becomes a plain ReadConst expression; the shader then refuses one level further up (the constant buffer's V# is a Phi web through a subroutine on the loop back edge), so this does not yet pass GT7. Tests: shader_readlane_eliminator_tests builds the spill-plus-compaction IR by hand; four of its seven cases fail on the unchanged eliminator and all pass with the change. agc_shader_replay --prepare on the dumped request: 0.5 s, same refusal one level up. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — comments
The eliminator and guard wiring are supported by the inspected source, but the new SPIR-V guard emission has no fixture that reaches it, so I cannot verify the emitted fault behavior.
What I ran
Inspected the prepared diff and changed lines at head b80c020ab77ab63e8494d349570ddf2ecb4aaf76 using the review reader. No build, tests, or console run was performed.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed-line inspection | passed | All 11 changed files read |
| Build and tests | not run | No execution environment available |
| SPIR-V guard execution | not run | No fixture reaches guard emission |
Findings
Blocking
None.
Non-blocking
- Add an emitter-level fixture that generates a
ReadLaneGuardand checks the emitted fault behavior. The author notes no current fixture reaches SPIR-V emission of this guard (core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp:1540).
Not verified
Build and test results were not independently verified. Runtime behavior of the emitted guard was not verified. Read all changed files listed in manifest.json; no files were excluded by rule.
To get this to a good state
Nothing is required. Optional: add an emitter-level fixture for ReadLaneGuard.
Superseabas
left a comment
There was a problem hiding this comment.
Windows check of head b80c020ab7 on an NVIDIA and an AMD GPU. The change works and its test guards it, but check_conventions fails on the code comments, so the conventions job will block it.
Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release full build, run from PowerShell. Merged onto main fd653794a1 (clean merge), built together with three other open PRs that touch other files, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. GPUs, each picked with ANYPS5_GPU: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (1002:164e, AMD driver 32.0.21045.29003).
shader_readlane_eliminator,shader_ssa_pass_reads,agc_shader_memory*,agc_driver_pointer*,agc_driver_prepared_shaders*andagc_shader_srt*:main33 of 33, and with this PR 34 of 34 in 2 of 2 runs, on both GPUs. NoVUID-lines.- Red check: with the guard never inserted (
if (values.compactionWrites != 0u)→if (false)),shader_readlane_eliminatorfails witha spill read across a lane-compaction loop folds and is guarded: the fold through compaction writes was not counted.
Conventions: python tools/check_conventions.py --base upstream/main reports 40 errors, all [comment]. The // comments in ReadLaneEliminator.hpp, ReadLaneEliminator.cpp, Recompiler.cpp and SpirvFlowEmitter.cpp are new code comments, which CONTRIBUTING.md and AGENTS.md allow only for technical debt (with a matching docs/dev/debt/changes/ fragment). Moving the explanations into the commit message and a debt fragment for the unproven part (that the compaction count never reaches a live spill lane, now checked at run time) would clear them.
The code: the fold steps over a WriteLane only in the exact compaction shape (ReadLane value at a non-immediate lane, count a phi stepped by immediates). A folded read keeps a real ReadLane of the same lane next to it, and ReadLaneGuard records a ReadLaneFold fault when they differ, which BdaResources.cpp turns into a clear error. The 65,536 visit cap applies only to programs with a compaction write. As MarcZX10 notes, nothing executes the emitted guard: my red check covers only the IR insertion, so a fixture that runs a fold whose lane the compaction does reach, and checks the ReadLaneFold fault, would cover EmitReadLaneGuard too.
AI-assisted: yes (Claude Code)
|
Thanks both for the reviews, and @Superseabas for the two-GPU run and the red check. While fixing the conventions findings I re-checked the PR against its own base and found that it is no longer needed for the shader it was written for. What I measured.
The analysis behind the PR was done on So on current For the record, what the review round found (local commit, not pushed, available if a shader that needs the fold turns up):
AI-assisted: yes (Claude Code). The final review of the diff was done with Claude Fable 5.2. |
Layer 1 of #6191 (Gran Turismo 7, compute shader
0x120aed7f00, the garage stop), as asked in the maintainer's reply: theReadLane(WriteLane, Immediate)key thatReadLaneEliminatorcould not fold. Layers 2 (the V# Phi web through the subroutine on the loop back edge) and 3 (the waterfall-keyed pointer) are not in this PR; GT7 still stops in the garage with it.What the shader does
The spill vector holds SGPR spills at immediate lanes (lanes 0 to 16 and 25 to 35 in this shader) and is also the target of a lane-compaction loop:
v_writelane v, v_readlane(w, s_lane), s_count, with a running count as the lane, unrolled. The read of the spilled key sits after a subroutine call.collectLaneValuesfailed at the first write with a non-immediate lane, and the vector's Phi/Select web has about 6,200 nodes, above the 4,096 visit cap.Change
collectLaneValuessteps over a write that matches the compaction shape exactly: the value is aReadLaneat a non-immediate lane, and the lane is a loop-carried phi stepped by immediates (isLaneCompactionWrite). Every other non-immediate lane write still fails the walk, as before.ReadLaneGuardrecords aReadLaneFoldfault if the two differ, the wayWindowLaneGuardcovers spilled window lanes. The driver reports it aslane N of a vector held X where a v_readlane_b32 across a lane-compaction loop was replaced with the spilled scalar Y. The guard counts as a lane guard forprepareAddressBindings' fault checks. So a wrong fold cannot change a title's result in silence; it faults with the lane and both values.hasLaneCompactionWrite, one linear scan, computed on first need); every other program keeps 4,096.ReadLaneEliminationStats::compactionFolds.APS5_TRACE_READLANE=1prints one line per program with the counts (debug aid next toAPS5_DUMP_IR).Shaders that do not hit the fold
A program without a lane-compaction write runs the unchanged walk with the unchanged cap, so its IR is byte-identical to before; the only new code it can reach is the
hasLaneCompactionWritescan, which runs only when the fallback path is reached at all. Evidence on the title that is on this PC (no second title here):APS5_DUMP_SHADERS=1andAPS5_TRACE_READLANE=1, onmain33876d3 + feat(agc): blend into 11_11_10 float color targets in the pixel shader #9020 + feat(agc): render layered draws into a depth view of several slices #7256 plus this change. The trace covered 2,821 programs through the eliminator; exactly one,0x120aed7f00, folded across lane-compaction writes. The run dumped 67 requests (65 success-path requests with memory, plus the failing shader's prepare and failed pair);agc_shader_replay --spvon each of the 65, on the base tree and on the tree with this change: 65 of 65 byte-identical SPIR-V, none withcompactionFolds > 0.ctest -R "shader|readlane|ReadLane|srt|Srt|agc_driver_pointer|prepared"): 146 of 146 pass on this branch (currentmaindc95673 plus this change), 123 of 123 on the base tree plus this change.Tests
shader_readlane_eliminator_tests(new, likeshader_ssa_pass_reads_tests) builds the spill-plus-compaction IR by hand (entry spill at lane 29, loop with two unrolled compaction writes at a phi count, read after the loop) and checks:ReadLaneGuardright before it that reads the same lane of the same vector and compares against the scalar;ReadLanevalue, or at a lane that is not a loop count, still stops the walk;On the unchanged eliminator cases 1, 2, 3 and 7 fail; with the change all seven pass.
Cost of the cap
The cap only grows in programs with a lane-compaction write, so no other program pays. In GT7's shader the fallback walk visits about 6,250 nodes per read for 23 reads;
agc_shader_replay --prepareon the dumped request takes 0.6 s wall end to end with the change (1.3 s on the base tree, which refuses later in the SRT walk).GT7 with layer 1 alone
With these libs on
main33876d3 + #9020 + #7256, GT7 reaches the garage as before and stops at the same point, the car model load. The refusal moved from theReadLane(WriteLane)key to the V# of thepc=0xac8constant-buffer read (a Phi web, layer 2), as the offline replay predicted:The shader does not reach SPIR-V emission, so the
ReadLaneGuarddid not run on the GPU in this title yet.Not in this PR
ReadLaneGuardyet, because the only shader that folds through compaction writes (GT7's) still refuses in the SRT walk before emission. The emitter mirrorsEmitWindowLaneGuard. A follow-up test on the emitter is welcome if the reviewers want one before merge.Windows 11, RTX 2080 Ti, NVIDIA 617.42, WinLibs GCC 15.2.0.
AI-assisted: yes (Claude Code).
🤖 Generated with Claude Code