fix(shader): keep a v_writelane lane known across computed-index window stores #8881
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8881"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T04:16:25Z.
What
Two titles stop when a shader reloads a spilled resource descriptor with
v_readlane_b32from a window register that the program also uses as an ordinary VGPR:0xffde46b100(@fdfawef324 on #6277):#6277 lets
ReadLaneEliminatorfollow a lane written withv_writelane_b32across computed-index window stores, but only for a window slot that is never used as an ordinary register. @fdfawef324 notes that LLVM can allocate the VGPRs it spills SGPRs into like any other register (LLVM 18 and 19 do; LLVM 17 and earlier, and 20 and later, reserve them, see the review reply), so one register can be an ordinary register early in a program and hold spill lanes later. In RE4's program,v16(window slot 11) is written and read by vector ALU instructions first and holds the spill later. The slot is rejected, the M0-indexed stores between the spill and the reload make the lane unknown, and the descriptor stays a run-time value.This change is @fdfawef324's first suggestion on #6277. In
laneFlow, a computed-index store no longer resets the lane. A known lane can only come from av_writelane_b32chain with no constant-index store of the slot since, so keeping it is wrong only when a relative write reaches the register between thatv_writelane_b32and a laterv_readlane_b32of the lane. An unknown lane stays unknown, and a constant-index store of the slot still ends the spill, becausestoredLanereturns unknown for an ordinary value. The program-wide spill-slot rule (collectLaneSpillSlots) goes away.The assumption is the one #6277 already makes, now for every slot: a relative write never reaches a register between a
v_writelane_b32of one of its lanes and a laterv_readlane_b32of that lane. That holds if the compiler keeps spill registers out of its relatively indexed arrays, which is not verified. It can't be proven per store for these programs either: in RE4's program the index comes from per-lane data and the window runs pastv16(@fdfawef324 above).So the assumption is now checked at run time (after @MarcZX10's review). After the reads are replaced,
ReadLaneEliminatorruns a backward liveness over each window slot and lane whose known value a replaced read used. It inserts aWindowLaneGuardbefore every computed-indexStoreVectorWindowwhere that lane is still live, meaning a replaced read can reach it without a constant-index store of the slot in between. The guard loads the slot's current value. In SPIR-V it records a new fault reason,WindowLaneSpill, in the fault buffer, asUnlinkedCalldoes, when the store's index is the slot, the invocation is the spill's lane, and the stored value differs from the current one. The driver then fails the dispatch or draw witha relative write stored <value> in lane <lane> of vector window slot <slot>, which holds a scalar spilled with v_writelane_b32 that a later v_readlane_b32 was replaced with. The driver reads the fault buffer back for every program with a guard (awindowLaneGuardscount on the compiled shader, also when the fault buffer otherwise only serves image tables; shader cache format 45). A program that breaks the assumption is then reported with that message instead of silently using a stale descriptor. The dispatch itself still runs with the replaced value: the check makes the problem visible, it doesn't correct it. That includes the pure spill slotsmainalready folds, where @THEMANnt's m0 = 1 program silently gets the stale value today.The guard tests the store's own index, value and lane, so a lane that is inactive or out of range (which stores its old value back) doesn't trip it. Neither does a relative write to the register after the spill's last replaced read, because the lane is no longer live there. The debt entry states the assumption gap first and says how a violation surfaces.
One trade-off remains from dropping the per-slot rule. A
v_readlane_b32of a lane in a register that also has ordinary uses is now replaced even if a relative write changes that lane on purpose before the read.mainread such a lane at run time; now the guard reports it. Compilers emitv_writelane_b32/v_readlane_b32pairs for SGPR spills, and I know of no program that relies on this. Limiting the relaxed rule to reads that feed a resource descriptor would remove the case, and can follow if wanted. The guard also can't identify the lane where the host's lane id doesn't cover it (wave64 graphics stages on 32-wide hosts, single-lane stages), so it detects nothing there.@fdfawef324's replay of RE4's request prepares (1,558,931 SPIR-V words) with the rejected slots kept as candidates, which behaves like this change. With a per-live-spill variant and the later fixes, the title reached the brightness calibration and play-data consent screens. @THEMANnt's four captured LEGO requests compile with every constant slot kept as a spill candidate. Those were their local variants, not this patch. I asked @fdfawef324 on #6277 whether they wanted to open it themselves, and have credited them as co-author.
Tested
Windows 11 26200, AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON,ANYPS5_REQUIRE_VULKAN=1.This branch is now one commit (
1a74a6cdd9) onmain2f161d4f33, rebased on 2026-10-21; the only conflict was the shader cache format, now 45. On1a74a6cdd9the six focused tests below pass 3 of 3. The validation-layer run (A:main2f161d4f33, B:1a74a6cdd9, the same six tests) has 0VUID-lines and nowas not found but was requestedon either side (side A also carried #9115's libSceNpTrophy2-only commit, which none of these tests load); B's only fault lines are the two expected m0 = 1 reports. The rest of this section was measured on the previous head c6c9a87e3 (onmainc58c2af63).agc_vector_window: a sampler dword spilled into a window register that has ordinary writes keeps its definition past a relative write, straight-line and around a loop entered at its condition. The same loop without the ordinary write is a baseline, and it also passes onmain. A spill overwritten by an ordinary write before itsv_readlane_b32stays a run-time value.agc_driver_window_lane_spill(GPU): a window slot written as an ordinary register before thev_writelane_b32spill, with av_movreld_b32between the spill and the reload, dispatches and stores each lane's value. The overwritten-spill program is still refused.agc_driver_window_lane_spill(GPU), the runtime check, with @THEMANnt's aliasing program from #6277 (v_movreld_b32 v2, v0betweenv_writelane_b32 v3, s13, 0andv_readlane_b32 s8, v3, 0, here withv_mov_b32 v2, 7before that write so that it always changes the register it writes):v2, not the spill register, so every lane stores 0x1234 and nothing is reported.v3in lane 0, and the dispatch reportsa relative write stored 0 in lane 0 of vector window slot 1, which holds a scalar spilled with v_writelane_b32 that a later v_readlane_b32 was replaced with.v_readlane_b32, with m0 = 1: every lane stores 0x1234 and nothing is reported.stored 40 in lane 40 of vector window slot 1. This runs where the device's subgroup size is at least 64.agc_driver_compute_table_callsdoes, and requires the message in the m0 = 1 cases and its absence in the others.APS5_DUMP_IR=all, each of the threeagc_vector_windowprograms that keep a spill past a relative write has oneWindowLaneGuardright before that store.mainc58c2af63with only the test changes (the first head of this PR, before the runtime check):agc_vector_windowfails its two ordinary-write cases withGetSamplerResource dword 0 is not a valid runtime value; chain: ReadLane(LoadVectorWindow(Immediate), Immediate), LEGO Horizon Adventures' message.agc_driver_window_lane_spillfails withwindow lane spill into a register with ordinary writes past an indexed store: lane 0 stored 3735928559 through the V# read back from a window register, after[gpu] deferred write-back failed: BDA access failed.guardSpilledLanesgiven no replaced reads):agc_driver_window_lane_spillfails withwindow lane spill overwritten by an indexed store with m0 = 1 was not reported.window lane spill overwritten by an indexed store with m0 = 1 after its v_readlane: reported an overwritten spill: [gpu] deferred write-back failed: shader stage 4: a relative write stored 0 in lane 0 of vector window slot 1, ..., so the liveness is what keeps a dead spill register from tripping the check.agc_driver_window_lane_spill,agc_vector_window,agc_driver_relative_indexing,agc_driver_write_lane,agc_driver_read_laneandagc_shader_memorypass 3 of 3.VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1,ctest -V, same RX 6700 XT, onmainc58c2af63and on this change on it (first ecf53eaa1, now c6c9a87e3): the six pass on all three (6 of 6). No log reportswas not found but was requestedor anyVUID-error. WithVK_LOADER_DEBUG=layer, the run ofagc_driver_window_lane_spillat this change showsInsert instance layer "VK_LAYER_KHRONOS_validation"and the device layer. At c6c9a87e3 its only fault lines are the two expected m0 = 1 reports (wave32 lane 0, wave64 lane 40).ctest --parallel 4 --timeout 120at this head (all test executables rebuilt, sinceIrOpcode.hppandBdaAbi.hppchange): 1826 tests, 4 fail. None involves the recompiler's lane or window code:agc_driver_tessellation, #8861 onmainon this card;agc_driver_image_array_window_past_layer_limit, not run, becauseImageArrayWindowPastLayerLimit.cpp'salignas(65536)doesn't build with MinGW (requested alignment '65536' exceeds object file maximum 8192), the same onmain;log_macrosandlog_macros_timestamps, which fail only in this local build because ccache's base directory turns__FILE__into a relative path with backslashes.guardSpilledLanesgiven no replaced reads, the spill test fails 3/3 on both GPUs.mainf894ba69d3(RTX 5070 Ti, 616.92): these six plusagc_driver_scalar_relative_indexingandagc_driver_vector_window_direct_accesspass onmainand with the PR under the validation layer, with noVUID-or missing-layer line; the wave64 cases skip at subgroup size 32. A fresh install of LEGO Horizon Adventures now stops earlier on both builds, so the original stop could not be rechecked.python tools/check_conventions.py --base origin/mainwith this title and body passes.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T04:42:19Z.
Code review location:
core/shader/recompiler/Optimization/src/ReadLaneEliminator.cpp; line 114This now ignores every computed-index
StoreVectorWindowfor every slot, so av_movreld_b32that actually targets this slot leaves the pre-store lane marked known. That can foldv_readlaneto the stalev_writelanevalue and embed a wrong image/sampler descriptor; the “compiler keeps spill slots out of relative arrays” condition is explicitly unverified in the PR. Could the analysis conservatively invalidate lanes for computed stores unless it proves their target range excludes this slot, or add evidence that the compiler guarantees the non-aliasing invariant for all supported wave sizes? The new tests set M0 so the relative write misses the spill slot, so they do not exercise the aliasing case.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T07:15:51Z.
Code review location:
core/shader/recompiler/Optimization/src/ReadLaneEliminator.cpp; line 114You're right that this was unchecked, and the PR depended on it entirely. Neither option fully fits these programs, though. In RE4's mesh program the index comes from per-lane data and the window runs past the spill register (
v16, slot 11), as @fdfawef324 found, so a per-store proof isn't available. Invalidating unless the range excludes the slot would leave both titles stopped. c6c9a87e3 keeps the fold and checks the assumption at run time:ReadLaneEliminatorruns a backward liveness over each window slot and lane whose known value a replaced read used. Where a replaced read can still be reached without a constant-index store of the slot, it puts aWindowLaneGuardbefore the computed-indexStoreVectorWindow.WindowLaneSpill, through the fault buffer (asUnlinkedCalldoes) when the store's index is the slot, the invocation is the spill's lane, and the stored value differs from what the slot holds. The driver then fails the dispatch or draw witha relative write stored <value> in lane <lane> of vector window slot <slot>, which holds a scalar spilled with v_writelane_b32 that a later v_readlane_b32 was replaced with.v_movreld_b32that actually targets the slot is now reported with that message instead of passing silently with a stale descriptor. The dispatch still runs with the replaced value; the check makes the problem visible, it doesn't correct it. This also covers the pure spill slotsmainalready folds today with no check (@THEMANnt's m0 = 1 program gets the stale value onmain). An inactive or out-of-range lane stores the old value back, so it doesn't trip the guard. The liveness is a may-liveness, the same a register allocator uses, so a compiler that never overwrites a spill register while the spill may still be read never trips it.agc_driver_window_lane_spill, in wave32 (lane 0) and in wave64 (lane 40). With m0 = 1 the write reaches the spill register in that lane, and the fault is required. With m0 = 0, and with the same m0 = 1 write placed after thev_readlane_b32, the output must be the spilled value with no fault. As controls, without the guards the m0 = 1 case goes unreported, and with a guard at every computed-index store regardless of liveness, the after-read case reports a false fault. The six focused tests pass 3/3 and under the validation layer (0VUID-). The full suite has 4 failures out of 1826, none in lane or window code: #8861's tessellation test, a test that doesn't build with MinGW onmaineither, and two log tests that fail only in my local build. Details are in the body.The body also states the trade-off this leaves. A data
v_readlane_b32whose lane a relative write changes on purpose is now reported instead of being read at run time. The body also lists the stages where the guard can't see the lane.On the compiler side, upstream LLVM's AMDGPU backend guarantees the invariant only for in-range indices:
insertelementlowered tov_movreld_b32(gfx10, gfx1013 included). Its pseudo carries the whole vector tuple as a tied implicit def and use, and the machine verifier enforces that (SIInstructions.td, SIInstrInfo.cpp). An in-range write therefore stays inside the tuple's registers, which the allocator can't give to anything live at that point, a spill lane included. Wave32 and wave64 differ only in the exec-mask opcodes and the lanes per spill VGPR (AMDGPULaneMaskUtils.h). Upstream never emitsv_movrelsd_b32,v_movrelsd_2_b32orv_swaprel_b32(VOP1Instructions.td).v[base + M0]wherever that lands (SIISelLowering.cpp). LangRef only makes such aninsertelementpoison (LangRef). LLVM's own -O0 test has this PR's exact shape: spill lanes inv31around a waterfallv_movreld_b32intov[0:15](indirect-addressing-si.ll).v16dual use fits only LLVM 18 and 19, which came out after the game. And since LLVM 11 a divergent index is lowered to compare/select, not to a relative write (SIISelLowering.cpp).Whether the PS5's compiler is LLVM, and which version, isn't known, so none of this covers the titles' binaries. That's why the check runs at run time rather than being argued from the compiler. On the console, a write that hits the live lane is read back by the program's own
v_readlane_b32. So the check fires exactly where the console would read the written value and the emulator would otherwise use the stale one.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T07:52:49Z.
Code review location:
core/shader/recompiler/Optimization/src/ReadLaneEliminator.cpp; line 114Thanks for the detailed response and the added aliasing cases. The wave32/lane 0 and wave64/lane 40 tests exercise an actual write into the spill slot, and the before/after-read controls show the guard is limited to a live spill value rather than every computed-index store. This changes the failure mode to an explicit runtime fault when the alias occurs; it does not emulate programs that intentionally read back that overwritten value. Since the PR documents that limitation and the observed path needs a reliable diagnostic, this addresses my concern about silently folding an unchecked alias. The unknown console compiler behavior remains a real boundary, as you note.
Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-21T08:25:50Z.
Native Linux execution check at exact head
c6c9a87e3a5946a75ddff8c2acb4024e297a1c9d:stored 0 in lane 0 of vector window slot 1fault. Its wave64 cases are explicitly skipped because this device's subgroup size is 32.1002:13c0) / RADV Mesa 25.2.8: all six pass in three serial rounds. The expected wave32/lane-0 and wave64/stored 40 in lane 40 of vector window slot 1diagnostics both appear in every round. The non-aliasing and after-read controls pass their output/no-spill-fault assertions.agc_vector_window,agc_shader_memoryandagc_driver_relative_indexingpass each round. Read-lane, write-lane and window-spill execution skip because subgroup size 8 cannot hold wave32. This supplies no lavapipe guard-execution coverage.Negative control: changing only the
guardSpilledLanescall to receive an empty folded-read list, rebuilding and runningagc_driver_window_lane_spill_tests, fails 3/3 on each physical GPU withwindow lane spill overwritten by an indexed store with m0 = 1 was not reported. Restoring the exact source and rebuilding all six executables makes the full focused set pass again on both. Checkout is clean.Ubuntu 24.04.3, GCC 13.3, Release;
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,ctest -V. The six entries wereagc_vector_window,agc_driver_window_lane_spill,agc_driver_relative_indexing,agc_driver_write_lane,agc_driver_read_lane, andagc_shader_memory.No title execution or Khronos validation-layer A/B here (the layer is unavailable). This verifies the documented runtime diagnostic and its controls; it does not establish console compiler invariants or correct execution after a live spill is overwritten.
Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-21T13:03:23Z.
Tested #8881 head
c6c9a87e3, cherry-picked onto mainf894ba69d300(local571063307d85), on Windows 11 / RTX 5070 Ti 16 GB, NVIDIA 616.92, GCC 15.2 Release.The same eight focused tests pass on main and the PR build: shader memory, vector window, read lane, write lane, relative indexing, scalar relative indexing, vector-window direct access and window-lane spill. Both runs used
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationwithctest -V; neither log contains a missing-layer message or aVUID-error. The PR's wave64 spill cases skip on subgroup size 32.I could not complete the requested original four-request replay: those captures were removed during my earlier LEGO cleanup. After redownloading PPSA14632 01.004.000, fresh runs stop earlier. Main and the PR build both log a guest null write at
lego-horizon.exe+0x37dd2b. One PR run reached the Performance/Fidelity selection but was stopped by the memory watchdog before the opening level; a retry after closing unused apps hit the same guest exception with about 11 GiB RAM still free.I retained three fresh request sets (193, 255 and 145). Every request prepares and recompiles on both builds, so none reproduces the original
ReadLane(LoadVectorWindow...)rejection. I therefore cannot yet confirm this clears LEGO's original stop or claim gameplay success.AI-assisted: yes (Codex); builds, tests and captures were run locally.