feat(shader): emulate 64-bit global and flat atomics on devices without them #25967

Merged
Gonic0 merged 1 commit from upstream-pr/25967 into main 2026-11-27 18:40:42 +00:00
Gonic0 commented 2026-11-23 01:54:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-11-23T01:54:42Z.


What

On a device without shaderBufferInt64Atomics (MoltenVK on Apple GPUs, some integrated GPUs), any program with a global_atomic_*_x2 or a flat_atomic_*_x2 that reaches the device address table failed to compile with "64-bit buffer atomics need shaderBufferInt64Atomics". The 64-bit buffer atomics already had an emulation for such devices (#18842): the two dwords are read and written with 32-bit atomics under a word of the atomic lock table, chosen by a hash of the guest address of the 8-byte word. This extends that emulation to global and flat atomics on the device address table.

  • bufferAtomicLocks no longer excludes programs with address atomics, so buffer and address atomics of one program share the lock table and the same per-qword key.
  • A 64-bit address atomic takes the lock of its guest qword, reads both dwords with OpAtomicLoad, computes the result with the existing FlatAtomicNext64 (every 64-bit address opcode, including inc/dec and the float min/max/cmpswap forms), and stores both dwords with OpAtomicStore. The module never declares Int64Atomics.
  • A 32-bit address atomic of such a program runs under the lock of its qword too, so the two widths stay ordered within the program, as 32-bit buffer atomics already do.
  • The qword hash moves from SpirvRuntimeBuffer.cpp to EmitQwordAtomicLockPointer so both paths use the same key. Only one table lock is held at a time.
  • Devices with shaderBufferInt64Atomics are unchanged: the address path emits exactly what it did before.

agc_driver_global_atomics64, agc_driver_global_atomics_float, agc_driver_global_atomics_lanes, agc_driver_memory_float_atomic_modes and the 64-bit atomic rows of agc_driver_flat_apertures no longer skip without shaderBufferInt64Atomics. verifyInt64AtomicCapabilities checks that a global_atomic_swap_x2, alone and with a buffer_atomic_inc_x2, declares no Int64Atomics and asks for the lock table without the feature, and uses the native atomic with it. GDS 64-bit atomics are not covered here and still need the feature. The debt entry is updated through a fragment.

The emulation uses the existing LockedDeviceUpdate; #24081 changes it to elect one lane per subgroup after a contended buffer atomic hung an Apple M1. It did not hang on the M5 below, and this pull request does not depend on that change.

Tested

Apple M5 Pro, macOS 27.3, MoltenVK (Vulkan SDK 1.4.363.0, subgroup size 32, shaderBufferInt64Atomics false), x86_64 Release under Rosetta 2, main 4b7d1c3660 plus #17425 and #11049, which main still needs to build on macOS; the branch was then rebased onto 3deea61b30 without conflicts. The same build directory for main and this change. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer (Khronos validation layer 1.4.363); each log shows the loader inserting the layer and none reports it missing.

ctest -R 'atomic|flat_apertures|look_back|agc_shader_memory|bda|agc_driver_global|lds|gds' -V main this pull request
Passed 127 of 128 127 of 128
Failed agc_driver_gds_m0_base ("64-bit GDS atomics need shaderBufferInt64Atomics", #24072) same
VUID- 8, all VUID-VkDeviceCreateInfo-sType-unique identical
  • The 18 tests that no longer skip (global_atomics64, global_atomics_float, global_atomics_lanes, memory_float_atomic_modes, flat_apertures_0 to _13): 18 of 18 in each of 3 runs, no VUID-.
  • Negative control, this pull request's tests on main's code: those 18 tests and agc_shader_memory fail, all with "SPIR-V module emission failed: 64-bit buffer atomics need shaderBufferInt64Atomics".
  • agc_driver_look_back_scan keeps its skip: without it, it fails on this change because it reads the buffer_atomic_swap_x2 words with plain loads, which never take the lock (the limitation already in the debt entry).
  • tools/check_conventions.py --base upstream/main and tools/debt_fold.py --check pass.

Not run on a device with shaderBufferInt64Atomics; there verifyInt64AtomicCapabilities checks the native path. Not tested with a title.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.3
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5445755212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25967). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-11-23T01:54:42Z. --- ### What On a device without `shaderBufferInt64Atomics` (MoltenVK on Apple GPUs, some integrated GPUs), any program with a `global_atomic_*_x2` or a `flat_atomic_*_x2` that reaches the device address table failed to compile with "64-bit buffer atomics need shaderBufferInt64Atomics". The 64-bit buffer atomics already had an emulation for such devices (#18842): the two dwords are read and written with 32-bit atomics under a word of the atomic lock table, chosen by a hash of the guest address of the 8-byte word. This extends that emulation to global and flat atomics on the device address table. - `bufferAtomicLocks` no longer excludes programs with address atomics, so buffer and address atomics of one program share the lock table and the same per-qword key. - A 64-bit address atomic takes the lock of its guest qword, reads both dwords with `OpAtomicLoad`, computes the result with the existing `FlatAtomicNext64` (every 64-bit address opcode, including inc/dec and the float min/max/cmpswap forms), and stores both dwords with `OpAtomicStore`. The module never declares `Int64Atomics`. - A 32-bit address atomic of such a program runs under the lock of its qword too, so the two widths stay ordered within the program, as 32-bit buffer atomics already do. - The qword hash moves from `SpirvRuntimeBuffer.cpp` to `EmitQwordAtomicLockPointer` so both paths use the same key. Only one table lock is held at a time. - Devices with `shaderBufferInt64Atomics` are unchanged: the address path emits exactly what it did before. `agc_driver_global_atomics64`, `agc_driver_global_atomics_float`, `agc_driver_global_atomics_lanes`, `agc_driver_memory_float_atomic_modes` and the 64-bit atomic rows of `agc_driver_flat_apertures` no longer skip without `shaderBufferInt64Atomics`. `verifyInt64AtomicCapabilities` checks that a `global_atomic_swap_x2`, alone and with a `buffer_atomic_inc_x2`, declares no `Int64Atomics` and asks for the lock table without the feature, and uses the native atomic with it. GDS 64-bit atomics are not covered here and still need the feature. The debt entry is updated through a fragment. The emulation uses the existing `LockedDeviceUpdate`; #24081 changes it to elect one lane per subgroup after a contended buffer atomic hung an Apple M1. It did not hang on the M5 below, and this pull request does not depend on that change. ### Tested Apple M5 Pro, macOS 27.3, MoltenVK (Vulkan SDK 1.4.363.0, subgroup size 32, `shaderBufferInt64Atomics` false), x86_64 Release under Rosetta 2, `main` `4b7d1c3660` plus #17425 and #11049, which `main` still needs to build on macOS; the branch was then rebased onto `3deea61b30` without conflicts. The same build directory for `main` and this change. `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer` (Khronos validation layer 1.4.363); each log shows the loader inserting the layer and none reports it missing. | `ctest -R 'atomic\|flat_apertures\|look_back\|agc_shader_memory\|bda\|agc_driver_global\|lds\|gds' -V` | `main` | this pull request | |---|---|---| | Passed | 127 of 128 | 127 of 128 | | Failed | `agc_driver_gds_m0_base` ("64-bit GDS atomics need shaderBufferInt64Atomics", #24072) | same | | `VUID-` | 8, all `VUID-VkDeviceCreateInfo-sType-unique` | identical | - The 18 tests that no longer skip (`global_atomics64`, `global_atomics_float`, `global_atomics_lanes`, `memory_float_atomic_modes`, `flat_apertures_0` to `_13`): 18 of 18 in each of 3 runs, no `VUID-`. - Negative control, this pull request's tests on `main`'s code: those 18 tests and `agc_shader_memory` fail, all with "SPIR-V module emission failed: 64-bit buffer atomics need shaderBufferInt64Atomics". - `agc_driver_look_back_scan` keeps its skip: without it, it fails on this change because it reads the `buffer_atomic_swap_x2` words with plain loads, which never take the lock (the limitation already in the debt entry). - `tools/check_conventions.py --base upstream/main` and `tools/debt_fold.py --check` pass. Not run on a device with `shaderBufferInt64Atomics`; there `verifyInt64AtomicCapabilities` checks the native path. Not tested with a title. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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: none - [x] Unblocks: none - [x] GPUs tested: Apple M5 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.3 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-23 02:09:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:09:44Z.

Review: APPROVED


Reviewed — looks good

Nothing in the inspected changes blocks merge; the device address atomic emulation uses the shared qword lock and preserves the existing native path when 64-bit atomics are supported.

What I ran

Read the prepared diff and targeted source at head 8d53b3e23ee5a8e34c51105216c81caaff5a8b0e. I did not run commands or tests. The author reports device tests and a negative control in description.md; those results were not independently run here.

Results

Check Result Note
Source review passed Read targeted emitter and lock code
Automated checks not run No build or tests run

Findings

Blocking

None verified in the inspected code.

Non-blocking

None.

Not verified

The full ShaderMemory.cpp file was not read; only its changed lines in the diff were inspected. Other changed files with no added lines were not read at the pinned head; their diff hunks were inspected. Runtime behavior and author-reported device test results were not independently verified. The excluded debt fragment was not reviewed.

To get this to a good state

Nothing is required.

Sources

Prepared diff and targeted source files at the pinned head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6015829195 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25967#pullrequestreview-6015829195). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T02:09:44Z. **Review:** APPROVED --- # Reviewed — looks good Nothing in the inspected changes blocks merge; the device address atomic emulation uses the shared qword lock and preserves the existing native path when 64-bit atomics are supported. ## What I ran Read the prepared diff and targeted source at head `8d53b3e23ee5a8e34c51105216c81caaff5a8b0e`. I did not run commands or tests. The author reports device tests and a negative control in `description.md`; those results were not independently run here. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Read targeted emitter and lock code | | Automated checks | not run | No build or tests run | ## Findings ### Blocking None verified in the inspected code. ### Non-blocking None. ## Not verified The full `ShaderMemory.cpp` file was not read; only its changed lines in the diff were inspected. Other changed files with no added lines were not read at the pinned head; their diff hunks were inspected. Runtime behavior and author-reported device test results were not independently verified. The excluded debt fragment was not reviewed. ## To get this to a good state Nothing is required. ## Sources Prepared diff and targeted source files at the pinned head.
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!25967
No description provided.