fix(recompiler): read a coherent dwordx2 as one 64-bit atomic load #919

Merged
MichonGoddijn231849 merged 3 commits from upstream-pr/919 into main 2026-10-07 09:21:01 +00:00
MichonGoddijn231849 commented 2026-10-06 17:05:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-06T17:05:10Z.


What

A glc buffer_load_dwordx2 is one 64-bit access on the GPU, so it never sees half of a buffer_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:

  • each workgroup publishes a {flag, value} pair for its block with buffer_atomic_swap_x2;
  • it then reads the previous 64 blocks' pairs with glc dwordx2 loads.

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:

  • In a program that has the u64 storage-buffer view (it uses 64-bit buffer atomics), a coherent raw two-dword buffer load that is 8-byte aligned and in bounds is read with one device-scope OpAtomicLoad on that view.
  • The 64-bit result is split with OpUConvert and a shift. It does not use OpBitcast to a uvec2, which crashes NVIDIA's shader compiler.
  • All other loads (not coherent, not 8-byte aligned, out of bounds, formatted, or in programs without 64-bit buffer atomics) keep the per-dword path.

tests/execution/LookBackScan runs a decoupled look-back written for the test (43 instructions): one aggregate per block, the block index from an atomic counter, {flag, value} published with buffer_atomic_swap_x2 and read back with glc buffer_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 except agc_driver_scalar_sop1, which also fails on current main (on this NVIDIA driver; it passes with APS5_SPIRV_OPT=full) and is unrelated to this change.
  • agc_driver_look_back_scan passes (5 of 5 runs). Without the recompiler change it fails in the first whole scan in 3 of 3 runs (for example whole scan 0: block 18's prefix is 11651, expected 7478).
  • GTA V (PPSA04264), Fidelity mode with ray tracing: a BVH checker found 0 bad BVHs over 7.6 minutes. Before this change the BVHs were corrupt and the ray tracing refit hung.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4762096463 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/919). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-06T17:05:10Z. --- ### What A glc `buffer_load_dwordx2` is one 64-bit access on the GPU, so it never sees half of a `buffer_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: - each workgroup publishes a `{flag, value}` pair for its block with `buffer_atomic_swap_x2`; - it then reads the previous 64 blocks' pairs with glc `dwordx2` loads. 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: - In a program that has the u64 storage-buffer view (it uses 64-bit buffer atomics), a coherent raw two-dword buffer load that is 8-byte aligned and in bounds is read with one device-scope `OpAtomicLoad` on that view. - The 64-bit result is split with `OpUConvert` and a shift. It does not use `OpBitcast` to a `uvec2`, which crashes NVIDIA's shader compiler. - All other loads (not coherent, not 8-byte aligned, out of bounds, formatted, or in programs without 64-bit buffer atomics) keep the per-dword path. `tests/execution/LookBackScan` runs a decoupled look-back written for the test (43 instructions): one aggregate per block, the block index from an atomic counter, `{flag, value}` published with `buffer_atomic_swap_x2` and read back with glc `buffer_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 except `agc_driver_scalar_sop1`, which also fails on current `main` (on this NVIDIA driver; it passes with `APS5_SPIRV_OPT=full`) and is unrelated to this change. - `agc_driver_look_back_scan` passes (5 of 5 runs). Without the recompiler change it fails in the first whole scan in 3 of 3 runs (for example `whole scan 0: block 18's prefix is 11651, expected 7478`). - GTA V (PPSA04264), Fidelity mode with ray tracing: a BVH checker found 0 bad BVHs over 7.6 minutes. Before this change the BVHs were corrupt and the ray tracing refit hung. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-07 08:40:17 +00:00 (Migrated from github.com)

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_scan fails without the recompiler change. On gfx1035 hardware, with 16 waves running buffer_atomic_swap_x2 and 16 waves reading the same pair (1.5M reads per form), buffer_load_dwordx2 never tore with glc+dlc, with glc alone or without glc. Two buffer_load_dword reads tore 128 times. So the fix is correct. Two things before merging:

  • tests/execution/LookBackScan.cpp:17-80 embeds the game's compiled shader verbatim: 492 dwords, including its sl00 header 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} with buffer_atomic_swap_x2, look back with glc buffer_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 aligned dwordx2 is 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 with swap_x2. Either cover them, or add a line to docs/dev/TechnicalDebt.md saying that only those loads are atomic (criterion: known gaps are recorded). Don't put that line at the end of a section.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5439766805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/919#pullrequestreview-5439766805). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T08:40:17Z. **Review:** CHANGES_REQUESTED --- Thanks. Checked on our side: build + full ctest pass, and `agc_driver_look_back_scan` fails without the recompiler change. On gfx1035 hardware, with 16 waves running `buffer_atomic_swap_x2` and 16 waves reading the same pair (1.5M reads per form), `buffer_load_dwordx2` never tore with glc+dlc, with glc alone or **without glc**. Two `buffer_load_dword` reads tore 128 times. So the fix is correct. Two things before merging: - `tests/execution/LookBackScan.cpp:17-80` embeds the game's compiled shader verbatim: 492 dwords, including its `sl00` header 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}` with `buffer_atomic_swap_x2`, look back with glc `buffer_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 aligned `dwordx2` is 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 with `swap_x2`. Either cover them, or add a line to `docs/dev/TechnicalDebt.md` saying that only those loads are atomic (criterion: known gaps are recorded). Don't put that line at the end of a section.
MichonGoddijn231849 commented 2026-10-07 08:57:44 +00:00 (Migrated from github.com)

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:

  • LookBackScan now 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 with buffer_atomic_swap_x2 and read back with glc buffer_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.md records 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 with swap_x2.

The description is updated to match. Full agc_ ctest passes apart from agc_driver_scalar_sop1 (#928).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6034543422 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/919#issuecomment-6034543422). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-07T08:57:44Z. --- Thanks for measuring it on gfx1035, that settles the hardware side. Both done in abcaf896: - `LookBackScan` now 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 with `buffer_atomic_swap_x2` and read back with glc `buffer_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.md` records 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 with `swap_x2`. The description is updated to match. Full `agc_` ctest passes apart from `agc_driver_scalar_sop1` (#928).
adriadam10 commented 2026-10-07 09:10:52 +00:00 (Migrated from github.com)

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_scan fails without the recompiler change, and the TechnicalDebt line matches what we measured on gfx1035.

I pushed two mechanical commits to your branch:

  • Rebased on main (adjacency conflict in docs/dev/TechnicalDebt.md, both lines kept).
  • Dropped the comments in LookBackScan.cpp (CONTRIBUTING: no code comments; the other execution tests have none).

Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5440119503 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/919#pullrequestreview-5440119503). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T09:10:52Z. **Review:** APPROVED --- Thanks, both points are addressed. Checked: build + full ctest on RADV, `agc_driver_look_back_scan` fails without the recompiler change, and the TechnicalDebt line matches what we measured on gfx1035. I pushed two mechanical commits to your branch: - Rebased on `main` (adjacency conflict in `docs/dev/TechnicalDebt.md`, both lines kept). - Dropped the comments in `LookBackScan.cpp` (CONTRIBUTING: no code comments; the other execution tests have none). Merging once CI is green.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!919
No description provided.