fix(recompiler): read a coherent dwordx2 as one 64-bit atomic load #919
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/919"
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 MichonGoddijn231849 on 2026-10-06T17:05:10Z.
What
A glc
buffer_load_dwordx2is one 64-bit access on the GPU, so it never sees half of abuffer_atomic_swap_x2. The recompiler emitted it as two dword loads, which can: the first can read the flag from before the swap and the second the value from after it.GTA V gives its BVH nodes their record indices with a single-pass decoupled look-back prefix sum:
{flag, value}pair for its block withbuffer_atomic_swap_x2;dwordx2loads.A torn read counts earlier blocks twice. Two nodes then share records, the BVH is corrupt and the ray tracing refit hangs. It only happens in scans of more than 64 blocks (the largest BVHs), and only when the timing lines up.
The change:
OpAtomicLoadon that view.OpUConvertand a shift. It does not useOpBitcastto auvec2, which crashes NVIDIA's shader compiler.tests/execution/LookBackScanruns a decoupled look-back written for the test (43 instructions): one aggregate per block, the block index from an atomic counter,{flag, value}published withbuffer_atomic_swap_x2and read back with glcbuffer_load_dwordx2. Seven look-backs from given blocks and 50 whole scans of 4096 blocks are checked against a CPU prefix sum.Tested
Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080, MinGW-w64 GCC 15.2.0.
ctest -R '^agc_': all pass exceptagc_driver_scalar_sop1, which also fails on currentmain(on this NVIDIA driver; it passes withAPS5_SPIRV_OPT=full) and is unrelated to this change.agc_driver_look_back_scanpasses (5 of 5 runs). Without the recompiler change it fails in the first whole scan in 3 of 3 runs (for examplewhole scan 0: block 18's prefix is 11651, expected 7478).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🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T08:40:17Z.
Review: CHANGES_REQUESTED
Thanks. Checked on our side: build + full ctest pass, and
agc_driver_look_back_scanfails without the recompiler change. On gfx1035 hardware, with 16 waves runningbuffer_atomic_swap_x2and 16 waves reading the same pair (1.5M reads per form),buffer_load_dwordx2never tore with glc+dlc, with glc alone or without glc. Twobuffer_load_dwordreads tore 128 times. So the fix is correct. Two things before merging:tests/execution/LookBackScan.cpp:17-80embeds the game's compiled shader verbatim: 492 dwords, including itssl00header and build metadata (barefoot). We can't ship a title's binary code in the repo, and nothing on main does that. Please replace it with a small hand-written kernel that does the same thing (publish{flag, value}withbuffer_atomic_swap_x2, look back with glcbuffer_load_dwordx2), assembled like the other execution tests. Keep the whole-scan check against the CPU prefix sum and the failure without the fix.SpirvMemoryInstructions.cpp:609: the 64-bit load is only used for coherent loads in programs that already have the u64 view. On hardware, an aligneddwordx2is a single access without glc too (measured above). Two cases can still tear: non-glc loads, and a reader program with no 64-bit atomics while another shader writes the pair withswap_x2. Either cover them, or add a line todocs/dev/TechnicalDebt.mdsaying that only those loads are atomic (criterion: known gaps are recorded). Don't put that line at the end of a section.Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-07T08:57:44Z.
Thanks for measuring it on gfx1035, that settles the hardware side. Both done in abcaf896:
LookBackScannow runs a decoupled look-back written for the test, 43 instructions with no header: one aggregate per block, the block index from an atomic counter,{flag, value}published withbuffer_atomic_swap_x2and read back with glcbuffer_load_dwordx2, one wave per block. Seven look-backs from given blocks and 50 whole scans of 4096 blocks are checked against the CPU prefix sum. Without the recompiler change the first whole scan fails in 3 of 3 runs (whole scan 0: block 18's prefix is 11651, expected 7478); with it, 5 of 5 pass.docs/dev/TechnicalDebt.mdrecords the two cases that can still tear, after the 64-bit LDS atomics entry: loads without glc, and loads in a program without 64-bit buffer atomics while another shader writes the pair withswap_x2.The description is updated to match. Full
agc_ctest passes apart fromagc_driver_scalar_sop1(#928).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T09:10:52Z.
Review: APPROVED
Thanks, both points are addressed. Checked: build + full ctest on RADV,
agc_driver_look_back_scanfails without the recompiler change, and the TechnicalDebt line matches what we measured on gfx1035.I pushed two mechanical commits to your branch:
main(adjacency conflict indocs/dev/TechnicalDebt.md, both lines kept).LookBackScan.cpp(CONTRIBUTING: no code comments; the other execution tests have none).Merging once CI is green.