Skip to content

recompiler: fold spilled lane reads across lane-compaction writes, guarded at run time (#6191, layer 1) - #27207

Closed
humanityBG wants to merge 1 commit into
boykopovar:mainfrom
humanityBG:fix/readlane-compaction-spills
Closed

humanityBG wants to merge 1 commit into
boykopovar:mainfrom
humanityBG:fix/readlane-compaction-spills

Conversation

@humanityBG

Copy link
Copy Markdown

Layer 1 of #6191 (Gran Turismo 7, compute shader 0x120aed7f00, the garage stop), as asked in the maintainer's reply: the ReadLane(WriteLane, Immediate) key that ReadLaneEliminator could 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. collectLaneValues failed 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

  • collectLaneValues steps over a write that matches the compaction shape exactly: the value is a ReadLane at 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.
  • The part the IR cannot prove is that the count never reaches the spilled lane while the spill is live. Instead of assuming it, a fold that stepped over compaction writes keeps one real read of that lane next to the fold and a new ReadLaneGuard records a ReadLaneFold fault if the two differ, the way WindowLaneGuard covers spilled window lanes. The driver reports it as lane 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 for prepareAddressBindings' fault checks. So a wrong fold cannot change a title's result in silence; it faults with the lane and both values.
  • The visit cap grows to 65,536 only in a program that has a lane-compaction write (hasLaneCompactionWrite, one linear scan, computed on first need); every other program keeps 4,096.
  • Stats: ReadLaneEliminationStats::compactionFolds. APS5_TRACE_READLANE=1 prints one line per program with the counts (debug aid next to APS5_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 hasLaneCompactionWrite scan, which runs only when the fallback path is reached at all. Evidence on the title that is on this PC (no second title here):

  • One GT7 run from launch to the garage stop (11 min) with APS5_DUMP_SHADERS=1 and APS5_TRACE_READLANE=1, on main 33876d3 + 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 --spv on each of the 65, on the base tree and on the tree with this change: 65 of 65 byte-identical SPIR-V, none with compactionFolds > 0.
  • The recompiler/shader ctest set (ctest -R "shader|readlane|ReadLane|srt|Srt|agc_driver_pointer|prepared"): 146 of 146 pass on this branch (current main dc95673 plus this change), 123 of 123 on the base tree plus this change.

Tests

shader_readlane_eliminator_tests (new, like shader_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:

  1. the read folds to the spilled scalar, with exactly one ReadLaneGuard right before it that reads the same lane of the same vector and compares against the scalar;
  2. a second read of the same lane folds with its own guard;
  3. a read of another spilled lane folds to its own scalar;
  4. a spill without compaction folds through the phi path as before, with no guard;
  5. a variable-lane write of a non-ReadLane value, or at a lane that is not a loop count, still stops the walk;
  6. compaction into a vector nothing spilled to at that lane is left alone;
  7. a select web of 4,200 nodes does not fold in a program without lane compaction, folds in one with it (no guard: the compaction write is not in the read's web), and a web of 4,000 folds in both.

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 --prepare on 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 main 33876d3 + #9020 + #7256, GT7 reaches the garage as before and stops at the same point, the car model load. The refusal moved from the ReadLane(WriteLane) key to the V# of the pc=0xac8 constant-buffer read (a Phi web, layer 2), as the offline replay predicted:

[readlane] 0x120aed7f00 rewrote 23 reads, 20 of them across lane-compaction writes (each with a ReadLaneGuard)
[videoout] presentation failed: compute shader 0x120aed7f00 (shader_120aed7f00_failed.req): shader 0x120aed7f00 stage 0 (shader_120aed7f00.prepare.req): ShaderRecompiler::Recompile: GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x000019c4(GetAddressResource(LoadAddressU32@pc=0x000019a0(GetAddressResource(ReadConstBuffer@pc=0x00000ac8(GetBufferResource(Phi(Phi(Phi, Phi), Phi(Phi, Phi)), ...), ShiftLeftLogical32(IAdd32(ReadConst(GetSrtResource, Immediate), IMul32(ReadConst, IAdd32)), Immediate)), ...

The shader does not reach SPIR-V emission, so the ReadLaneGuard did not run on the GPU in this title yet.

Not in this PR

Windows 11, RTX 2080 Ti, NVIDIA 617.42, WinLibs GCC 15.2.0.
AI-assisted: yes (Claude Code).

🤖 Generated with Claude Code

…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 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ReadLaneGuard and 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 Superseabas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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* and agc_shader_srt*: main 33 of 33, and with this PR 34 of 34 in 2 of 2 runs, on both GPUs. No VUID- lines.
  • Red check: with the guard never inserted (if (values.compactionWrites != 0u) → if (false)), shader_readlane_eliminator fails with a 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)

@humanityBG

Copy link
Copy Markdown
Author

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. agc_shader_replay --prepare on the dumped Gran Turismo 7 request (0x120aed7f00), on main dc95673, Windows 11, RTX 2080 Ti (617.42):

Eliminator Lane reads rewritten Across lane-compaction writes Refusal
unmodified main 23 0 V# Phi web at pc=0xac8 (layer 2 of #6191)
this PR 23 0 same, output byte-identical

The analysis behind the PR was done on main 33876d3 + #9020 + #7256, where the read of the spilled key was not folded (20 of the 23 folds went across compaction writes there). main has since changed so that the existing paths fold all 23 reads. I ported the change to current main without repeating the replay on the unmodified tree first. That was my mistake. The 65 other dumped requests of the same run also show no fold across compaction writes on dc95673.

So on current main no shader I can test takes the new path, and the PR would add a heuristic, a guard and a technical-debt entry for nothing I can demonstrate. I am closing it. For #6191 this means layer 1 is already done on main; the garage stop is the layer 2 refusal.

For the record, what the review round found (local commit, not pushed, available if a shader that needs the fold turns up):

  • check_conventions: 38 code comments, a non-conventional commit subject and a missing docs/dev/ENVIRONMENT.md row for APS5_TRACE_READLANE. All fixed; the check passes.
  • The fixture both of you asked for exists: tests/execution/ReadLaneFold.cpp runs a wave32 compute shader that spills to lane 5, compacts N lanes in a loop and reads lane 5 back. N = 4 and 5: every lane stores the spilled value, no fault. N = 8: the dispatch reports lane 5 of a vector held 0x5 where a v_readlane_b32 across a lane-compaction loop was replaced with the spilled scalar 0x1234. Passes with VK_LAYER_KHRONOS_validation (1.4.363) with no VUID- lines; fails when EmitReadLaneGuard records nothing and when the eliminator inserts no guard.
  • A defect in the PR as opened: with the visit cap raised to 65,536 the recursive collectLaneValues overflows the stack on a chain between 12,000 and 30,000 values deep (unmodified main ends that walk at 4,096). Fixed locally with an explicit stack in the same visiting order, with a 60,000-deep test case; SPIR-V of the 65 dumped requests is byte-identical between the two walks.
  • A limit of the guard design: ReadLaneGuard compares against a real v_readlane_b32, so it inherits that read's host limits (a lane whose host invocation is inactive, one-lane waves). A guard at the compaction write that tests the uniform count against the spilled lane would not need a cross-lane read.

AI-assisted: yes (Claude Code). The final review of the diff was done with Claude Fable 5.2.

@humanityBG humanityBG closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants