fix(shader): keep direct VGPR accesses in SSA when the window has only relative reads #4120
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4120"
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 tibzejoker on 2026-10-11T23:44:11Z.
What
On current main (
bab7b3285) a shader that reads VGPRs relatively but spills a descriptor into a windowed VGPR fails to recompile:Seen in GTA V (PPSA04264), pixel shader 0x25a889400, fatal at draw time.
When a program uses relative VGPR addressing, every register from the lowest relatively addressed one upward goes into the window array, so even direct accesses become LoadVectorWindow / StoreVectorWindow. A sampler dword written with v_writelane_b32 and read back with v_readlane_b32 then has no definition that resource analysis can follow.
When the program has no indexed writes (no v_movreld_b32, v_movrelsd_b32, v_movrelsd_2_b32 or v_swaprel_b32), only the relative reads need the window. With this change, direct reads and writes stay SSA registers, and each direct write is also stored into the window so the relative reads still see it. Programs with indexed writes keep the current path.
Files: ProgramMetadata.hpp (new
vectorWindowIndexedWrites), InstructionTranslator.cpp (the existing window scan sets it), IrBuilderRegisters.cpp (GetVectorReg / SetVectorReg).Test: new case in
agc_vector_window_tests(tests/VectorWindow.cpp), "a sampler dword spilled into a relatively read register keeps its definition through v_readlane". It is a hand-assembled wave32 compute program: v_movrels_b32 v1, v4 (m0 = 2), v_writelane_b32 v4, s8, 0, v_readlane_b32 s8, v4, 0, image_sample_lz with T# s[0:7] and S# s[8:11], then a buffer_store of the result. On main it throws the error above. With this change it recompiles.Related: #4057 also edits relative vector windows (window sizing from implicit inputs) and touches VectorWindow.cpp and InstructionTranslator.cpp. It is a different change, but the two may need a small rebase against each other.
Tested
Windows 11 x64, RTX 4090, WinLibs GCC 15.2.0 (MinGW), Release.
agc_vector_window_tests: fails on main with the GetSamplerResource error above, passes with the change (the two existing cases pass in both).ctest -R "agc_driver|agc_masked|agc_shader|agc_vector" -j8(344 tests) on main and with the change: same results. Failing in both:agc_driver_prepared_shaders_registered_workgroup_sync("expected preparation failure") and 18 "Not Run" entries with no executable on this build. Under -j8,agc_driverandagc_driver_prepared_shaders_*sometimes exit with 0xc0000374 (heap corruption) on both main and the branch; they pass when run alone.HostPolloutside#ifndef _WIN32). It is not part of this PR.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01V1W4jdNSKTV7xwuS6xQPJV
🤖 Generated with Claude Code
https://claude.ai/code/session_01V1W4jdNSKTV7xwuS6xQPJV
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T00:50:02Z.
The mechanism is the right shape for the bug described: when a program has no indexed writes, GetVectorReg/SetVectorReg keep direct accesses as SSA registers instead of routing them through the window, which is what lets resource analysis (GetSamplerResource) follow a direct v_writelane_b32/v_readlane_b32 pair. SetVectorReg still emits StoreVectorWindow for a windowed register before checking the new flag, so a relative read of that slot still sees the direct write — the window and the SSA register stay in sync on the write side; only the direct-read path diverges.
The flag is assigned only inside
if (windowBase < vectorLimit)and defaults to false, but that is inert where it matters:vectorWindowIndexreturnsnullptrwhenevervectorWindowSize == 0, and a program with no relative addressing never lowerswindowBasebelowvectorLimit, so no window is built and neitherGetVectorRegnorSetVectorRegconsults the flag. The flag-set itself is right too: of the opcodes that lower the window base,v_movrelsd_b32/_2_b32andv_swaprel_b32all callwriteRelativeVectorand correctly set the flag true, while the one pure relative read,v_movrels_b32, is the opcode this PR targets and is correctly left out of the set.The "programs with indexed writes keep the old path" branch is already exercised by the existing first case (it contains
v_movreld_b32and asserts the window is still built), and the body reports both existing cases pass on main and on the branch, so the new test adds the missing no-indexed-writes case rather than the only coverage of the window. One gap worth naming: the new case's assertion (functionArrays == 1) doesn't actually distinguish SSA-register routing from window routing — the window variable is allocated andStoreVectorWindowis emitted either way — so the case only proves recompilation doesn't throw, not that the direct read-back value is correct. An execution-level case that reads a directly written register back throughv_movrels_b32and checks the value would close that gap.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-12T00:59:56Z.
Thanks, fair point: the shape assertion only showed that the recompile succeeds. I added an execution test,
agc_driver_vector_window_direct_access_tests(tests/execution/VectorWindowDirectAccess.cpp), that runs on the GPU and checks every lane.The program has only relative reads (v_movrels_b32, no v_movreld/v_movrelsd/v_swaprel), so it takes the new path: window v8..v24, direct accesses as SSA registers, direct writes mirrored into the window. It:
All 14 values per lane are checked exactly, for wave32 and wave64.
Results:
7a84b2226) it also passes. That is expected: main routes every access through the window, and the test checks that the new routing gives the same values, not that main is wrong.wave32 lane 0 movrels_valu is 0x00000000, expected 0x00001001).AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-12T06:53:49Z.
Rebased on main
d06c06565. Two test changes came with it:agc_vector_window_stage_inputs(#4057) required that no register at or above the window base is ever written as an SSA value. This PR does exactly that on purpose for programs without indexed writes: the direct write still goes to the window, and a copy stays in SSA for direct reads. The assertion now allows such a write only whenvectorWindowIndexedWritesis false, and it no longer counts as initialization, so the window store is still required for every stage input. Programs with indexed writes are checked as before.ctest -R "agc|shader": 433/433 on Windows, RTX 4090.On overlap with #4213 (window store-to-load forwarding inside a block): I replayed the GTA V request this PR was made for (compute 0x25a889400) on main + #4213. It still fails with
GetSamplerResource dword 0 is not a valid runtime value; chain: ReadLane(WriteLane(..., LoadVectorWindow(Immediate))). With this PR it recompiles (128830 SPIR-V words). The code changes are independent; the only textual overlap istests/VectorWindow.cpp, which #4213 also conflicts on with current main.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T07:30:24Z.
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.
main)core/libs/prx/libSceAgcDriver/tests/VectorWindow.cppL273Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T17:46:09Z.
The gap I named is closed, and it is a test CI will actually run. Read at
44ac15495(the rebased head) — I read the file, I did not build the head here.tests/execution/VectorWindowDirectAccess.cppchecks 14 named values per lane against a computed expectation, for wave32 (32 threads) and wave64 (64 threads). The message carries the pair it compared (… movrels_valu is 0x00000000, expected 0x00001001), so it is a read-back check rather than a shape check.movrels_rewrittenreads a register relatively after a direct rewrite, so a stale window copy shows up as the pre-rewrite value, andmovrels_of_movrels_resultreads a relative-read result back relatively — the chain resource analysis follows.movrels_valu, the relative read whose mirror was removed.tests/execution/, whichcore/libs/prx/libSceAgcDriver/CMakeLists.txt:983globs withCONFIGURE_DEPENDS, and theforeachat:1069turns each intoadd_testwithSKIP_RETURN_CODE 77and flipsEXCLUDE_FROM_ALLoff. So this is a real ctest entry that CI builds and runs — worth knowing, because an execution test that never registers is invisible.maintoo: it is a routing-equivalence check against the window path, not a Red arm. The Red arm is theVectorWindow.cppcase (theGetSamplerResourcethrow), and that is in the head as well.The
VectorWindow.cppchange reads right too:Require(index < base || !program.Metadata().vectorWindowIndexedWrites, …)withif (index < base) stored[index] = truekeeps the window store required for every stage input, so relaxing the assertion does not weaken #4057's check.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T01:19:54Z.
Rebased onto main and pushed one commit on top (
d9c040739) so the full build passes. The Linux job failed two of the new tests; neither comes from #4150:agc_vector_window: the Linux job builds withANYPS5_ENABLE_SPIRV_TOOLS=ON, and the optimizer replaces a window that is only indexed by constants with scalars, sofunctionArrays == 1was false (same on 44ac15495 alone; it passes with the option off). The sampler case now reads M0 from the thread group id (MoveM0GroupX, as the first case does). Without the fix it still throwsGetSamplerResource dword 0 is not a valid runtime value; chain: ReadLane(LoadVectorWindow(Immediate), Immediate).agc_driver_vector_window_direct_access: llvmpipe has 8-lane subgroups (wave32 lane 8 readlane_other_lane is 0x0000e000, expected 0x00006000). The test now skips below 32 lanes, likeReadLane.cppandWriteLane.cpp.Linux, RTX 4090, SPIRV-Tools on: full ctest 1005 tests, 0 failed, 7 skipped for the device;
agc_driver_relative_ancillaryfrom #4150 passes. A 2D title we test with runs as on main.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-13T01:23:33Z.
Thanks @oneandonlydean. Both changes look right to me.
Checked
d9c040739on Windows 11, RTX 4090, GCC 15.2.0 UCRT, Release:agc_vector_window,agc_driver_vector_window_direct_accessandagc_vector_window_stage_inputspass. The direct-access test runs there, since the subgroup size is 32.AI-assisted (Claude Code).