fix(shader): take a scratch pool slot with a relaxed compare-exchange #16274

Merged
AlfaRojo merged 3 commits from upstream-pr/16274 into main 2026-11-07 12:40:47 +00:00
AlfaRojo commented 2026-11-06 02:44:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlfaRojo on 2026-11-06T02:44:42Z.


What

Fixes #15566. EmitScratchPoolAcquire took a scratch pool slot with an OpAtomicCompareExchange whose semantics were Acquire | UniformMemory at device scope, inside the scan that only invocation 0 of the workgroup runs. AMD's Windows driver compiles an acquire on a storage buffer to s_barrier (recorded in docs/dev/debt/shader.md for the LDS lock), so on a Radeon 0x13c0 that barrier paired with the OpControlBarrier the other waves of the workgroup had already reached after the scan: those waves read scratch_pool_slot from workgroup memory before invocation 0 stored it, took a garbage slot, and every scratch store and load of theirs went out of the pool (robust access dropped the stores and returned 0). Invocation 0's wave kept the right slot, which is why only lanes 64 and up failed in the test, and why the slot counter never reached 0 and the next dispatch of the program found the lock still held.

The compare-exchange is now relaxed, like the atomic load before it, and the OpControlBarrier after the scan acquires at device scope instead of workgroup scope, so the ordering the acquire gave moves into uniform flow, where every wave executes the same barrier. The release store of the last invocation keeps its release semantics; it stays in a divergent branch at the end of the program, which the test did not find harmful on that driver. The debt entry on slot acquisition keeps the gap (relaxed compare-exchange, device-scope acquire barrier, release store measured only by this test); the measurements behind it are here: the failure was seen with driver 32.0.21043.5001 on a Radeon 1002:13c0 and reported with 32.0.21045.20006 on a 1002:164e, and the slot scan's cost was measured on an RTX 4090 with about 9 slots of 114696 bytes (APS5_SCRATCH_POOL_MIB=1), where a dispatch of 32768 workgroups takes 1308 ms against 395 ms with the default 64 MiB pool.

Overlap: tools/pr_overlap.py could not compare #9508, which conflicts with main and edits the lines around OpArrayLength at the top of EmitScratchPoolAcquire, next to the constant this change renames; nothing else in code. Whichever merges first, the other rebases.

Tested

Windows 11 Pro (10.0.26200), WinLibs GCC 15.2.0 r7 UCRT, Release, on main a7e817522f (rebased 2026-11-05; the main column was measured on b611e634f7, the change rerun on a7e817522f), ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, no validation layer available on this machine.

GPU agc_driver_scratch_pool on main with this change
AMD Radeon Graphics 1002:13c0 (RDNA 2 integrated, Ryzen, driver 32.0.21043.5001, subgroup=64, mesh 0) 0 of 3: scratch pool isolation wave64: round 0 lane 0 dword 0 read 0xdeadbeef, expected 0x00000000 3 of 3 pass
NVIDIA GeForce RTX 4070 (driver 32.0.16.1692, subgroup=32) 3 of 3 pass 3 of 3 pass

How the cause was found, with the test cut down to the wave64 isolation case on one workgroup: the failure does not depend on the slot count, the delay loop, the program's own s_barrier or the lane stride, and the pool read back after the dispatch held the stores of lanes 0-63 only, with the lock word still 1 and the counter still 64. Injecting edited SPIR-V into the test: a constant slot 0 instead of the workgroup-memory read passes; an atomic or Volatile workgroup-memory read still fails; a second OpControlBarrier, or an OpMemoryBarrier before it, passes (that driver compiles both to s_barrier); making the compare-exchange relaxed alone passes 3 of 3, with or without the device-scope barrier.

Also run with the change, RTX 4070 unless noted: agc_driver_lds_tree_reduction (both GPUs), agc_flat_scratch and agc_guarded_memory_loads (both GPUs), agc_recorder_upload_scratch, agc_shader_disk_cache, agc_spirv_branch_targets, agc_spirv_failure_text, agc_spirv_lds_atomic_lock, agc_spirv_module, agc_spirv_narrow_constant_store (all three), agc_spirv_subvector_loop_begin: all pass. Linux, macOS and a title not run; the disk cache rejects entries from another source version, so cached programs are rebuilt.

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 under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #15566
  • Unblocks: none
  • GPUs tested: AMD Radeon Graphics 1002:13c0 (RDNA 2 integrated), driver 32.0.21043.5001, Windows 11; NVIDIA GeForce RTX 4070, driver 32.0.16.1692, Windows 11
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5182047273 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16274). Originally posted by [AlfaRojo](https://github.com/AlfaRojo) on 2026-11-06T02:44:42Z. --- ### What Fixes #15566. `EmitScratchPoolAcquire` took a scratch pool slot with an `OpAtomicCompareExchange` whose semantics were `Acquire | UniformMemory` at device scope, inside the scan that only invocation 0 of the workgroup runs. AMD's Windows driver compiles an acquire on a storage buffer to `s_barrier` (recorded in `docs/dev/debt/shader.md` for the LDS lock), so on a Radeon 0x13c0 that barrier paired with the `OpControlBarrier` the other waves of the workgroup had already reached after the scan: those waves read `scratch_pool_slot` from workgroup memory before invocation 0 stored it, took a garbage slot, and every scratch store and load of theirs went out of the pool (robust access dropped the stores and returned 0). Invocation 0's wave kept the right slot, which is why only lanes 64 and up failed in the test, and why the slot counter never reached 0 and the next dispatch of the program found the lock still held. The compare-exchange is now relaxed, like the atomic load before it, and the `OpControlBarrier` after the scan acquires at device scope instead of workgroup scope, so the ordering the acquire gave moves into uniform flow, where every wave executes the same barrier. The release store of the last invocation keeps its release semantics; it stays in a divergent branch at the end of the program, which the test did not find harmful on that driver. The debt entry on slot acquisition keeps the gap (relaxed compare-exchange, device-scope acquire barrier, release store measured only by this test); the measurements behind it are here: the failure was seen with driver 32.0.21043.5001 on a Radeon `1002:13c0` and reported with 32.0.21045.20006 on a `1002:164e`, and the slot scan's cost was measured on an RTX 4090 with about 9 slots of 114696 bytes (`APS5_SCRATCH_POOL_MIB=1`), where a dispatch of 32768 workgroups takes 1308 ms against 395 ms with the default 64 MiB pool. Overlap: `tools/pr_overlap.py` could not compare #9508, which conflicts with `main` and edits the lines around `OpArrayLength` at the top of `EmitScratchPoolAcquire`, next to the constant this change renames; nothing else in code. Whichever merges first, the other rebases. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro (10.0.26200), WinLibs GCC 15.2.0 r7 UCRT, Release, on `main` `a7e817522f` (rebased 2026-11-05; the `main` column was measured on `b611e634f7`, the change rerun on `a7e817522f`), `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`, no validation layer available on this machine. | GPU | `agc_driver_scratch_pool` on `main` | with this change | |---|---|---| | AMD Radeon Graphics `1002:13c0` (RDNA 2 integrated, Ryzen, driver 32.0.21043.5001, `subgroup=64`, `mesh 0`) | 0 of 3: `scratch pool isolation wave64: round 0 lane 0 dword 0 read 0xdeadbeef, expected 0x00000000` | 3 of 3 pass | | NVIDIA GeForce RTX 4070 (driver 32.0.16.1692, `subgroup=32`) | 3 of 3 pass | 3 of 3 pass | How the cause was found, with the test cut down to the wave64 isolation case on one workgroup: the failure does not depend on the slot count, the delay loop, the program's own `s_barrier` or the lane stride, and the pool read back after the dispatch held the stores of lanes 0-63 only, with the lock word still 1 and the counter still 64. Injecting edited SPIR-V into the test: a constant slot 0 instead of the workgroup-memory read passes; an atomic or `Volatile` workgroup-memory read still fails; a second `OpControlBarrier`, or an `OpMemoryBarrier` before it, passes (that driver compiles both to `s_barrier`); making the compare-exchange relaxed alone passes 3 of 3, with or without the device-scope barrier. Also run with the change, RTX 4070 unless noted: `agc_driver_lds_tree_reduction` (both GPUs), `agc_flat_scratch` and `agc_guarded_memory_loads` (both GPUs), `agc_recorder_upload_scratch`, `agc_shader_disk_cache`, `agc_spirv_branch_targets`, `agc_spirv_failure_text`, `agc_spirv_lds_atomic_lock`, `agc_spirv_module`, `agc_spirv_narrow_constant_store` (all three), `agc_spirv_subvector_loop_begin`: all pass. Linux, macOS and a title not run; the disk cache rejects entries from another source version, so cached programs are rebuilt. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #15566 - [x] Unblocks: none - [x] GPUs tested: AMD Radeon Graphics 1002:13c0 (RDNA 2 integrated), driver 32.0.21043.5001, Windows 11; NVIDIA GeForce RTX 4070, driver 32.0.16.1692, Windows 11 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Superseabas commented 2026-11-06 08:30:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-06T08:30:18Z.

Review: APPROVED


Windows run on the GPU #15566 was opened on, the integrated Radeon of a Ryzen 7 7700X (1002:164e), with the validation layer that the description's machine didn't have. It fixes the failure there and adds nothing new across the AGC tests.

Head 372b65b5e8 merged onto main ed41d86d32 (clean merge, local only). Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Vulkan SDK 1.4.363.0; VK_LOADER_DEBUG=layer shows it inserted for the instance and the device), ANYPS5_GPU selecting the device, confirmed by Selected GPU.

Test GPU main this PR this PR with main's SpirvScratchPool.cpp
agc_driver_scratch_pool Radeon iGPU (1002:164e, subgroup=64), AMD 26.10.2 (32.0.21045.20006) 0 of 5: scratch pool isolation wave64: round 0 lane 0 dword 0 read 0xdeadbeef, expected 0x00000000 5 of 5 0 of 5, same message
RTX 5070 Ti (10de:2c05, subgroup=32), 621.01 5 of 5 5 of 5
agc_driver_lds_tree_reduction both 5 of 5 5 of 5
  • Whole AGC set. ctest -R '^agc' -j 8 on each GPU, 1,388 tests:

    GPU main this PR
    RTX 5070 Ti 1,386 pass 1,386 pass
    Radeon iGPU 1,385 pass 1,386 pass

    agc_driver_scratch_pool on the Radeon is the only difference. The other failures, the same on both trees, are tracked: agc_driver_direct_write_completion (#15564) on both GPUs, agc_driver_quad_pixel_mask (#14341) on the RTX, and agc_driver_sample_order_grid_reference (#9503) on the Radeon.

  • Validation. No VUID- line and no missing-layer line in any run, on either tree.

  • The scratch tests that don't select a GPU (agc_flat_scratch, agc_guarded_memory_loads, agc_recorder_upload_scratch, agc_storage_write_back_scratch, agc_spirv_lds_atomic_lock, scalar_scratch_load) pass on the PR.

Non-blocking. check_conventions.py --base main passes, with one warning: the slot-acquisition debt entry is 1,770 characters, over the 1,000-character debt-length limit (957 before). The measurement details could move to this PR's description and the entry keep the gap: the compare-exchange is relaxed and the post-scan barrier acquires at device scope, because AMD's Windows driver turns a storage-buffer acquire into s_barrier; the release store in the divergent exit branch was measured only with this test.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5797598339 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16274#pullrequestreview-5797598339). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-06T08:30:18Z. **Review:** APPROVED --- Windows run on the GPU #15566 was opened on, the integrated Radeon of a Ryzen 7 7700X (`1002:164e`), with the validation layer that the description's machine didn't have. It fixes the failure there and adds nothing new across the AGC tests. Head `372b65b5e8` merged onto `main` `ed41d86d32` (clean merge, local only). Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release. `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (Vulkan SDK 1.4.363.0; `VK_LOADER_DEBUG=layer` shows it inserted for the instance and the device), `ANYPS5_GPU` selecting the device, confirmed by `Selected GPU`. | Test | GPU | `main` | this PR | this PR with `main`'s `SpirvScratchPool.cpp` | |---|---|---|---|---| | `agc_driver_scratch_pool` | Radeon iGPU (`1002:164e`, `subgroup=64`), AMD 26.10.2 (32.0.21045.20006) | 0 of 5: `scratch pool isolation wave64: round 0 lane 0 dword 0 read 0xdeadbeef, expected 0x00000000` | 5 of 5 | 0 of 5, same message | | | RTX 5070 Ti (`10de:2c05`, `subgroup=32`), 621.01 | 5 of 5 | 5 of 5 | | | `agc_driver_lds_tree_reduction` | both | 5 of 5 | 5 of 5 | | - **Whole AGC set.** `ctest -R '^agc' -j 8` on each GPU, 1,388 tests: | GPU | `main` | this PR | |---|---|---| | RTX 5070 Ti | 1,386 pass | 1,386 pass | | Radeon iGPU | 1,385 pass | 1,386 pass | `agc_driver_scratch_pool` on the Radeon is the only difference. The other failures, the same on both trees, are tracked: `agc_driver_direct_write_completion` (#15564) on both GPUs, `agc_driver_quad_pixel_mask` (#14341) on the RTX, and `agc_driver_sample_order_grid_reference` (#9503) on the Radeon. - **Validation.** No `VUID-` line and no missing-layer line in any run, on either tree. - The scratch tests that don't select a GPU (`agc_flat_scratch`, `agc_guarded_memory_loads`, `agc_recorder_upload_scratch`, `agc_storage_write_back_scratch`, `agc_spirv_lds_atomic_lock`, `scalar_scratch_load`) pass on the PR. **Non-blocking.** `check_conventions.py --base main` passes, with one warning: the slot-acquisition debt entry is 1,770 characters, over the 1,000-character `debt-length` limit (957 before). The measurement details could move to this PR's description and the entry keep the gap: the compare-exchange is relaxed and the post-scan barrier acquires at device scope, because AMD's Windows driver turns a storage-buffer acquire into `s_barrier`; the release store in the divergent exit branch was measured only with this test. AI-assisted: yes (Claude Code)
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!16274
No description provided.