feat(shader): emulate 64-bit image and buffer atomics on devices without them #18842

Merged
atinseau merged 5 commits from upstream-pr/18842 into main 2026-11-16 15:56:00 +00:00
atinseau commented 2026-11-12 12:10:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by atinseau on 2026-11-12T12:10:20Z.


What

On a device without VK_EXT_shader_image_atomic_int64 or shaderBufferInt64Atomics, such as MoltenVK, a 64-bit image_atomic_* or buffer_atomic_*_x2 fails SPIR-V emission, and the tests that cover them skip. Reanimal (PPSA23372) stopped there at its first present.

Each such atomic now takes one word of a device-wide table of 2^20 lock words (4 MiB), hashed from the texel coordinates or from the guest address of its 8-byte word. It reads and writes its two dwords and releases the word in the same loop iteration; the loop ends once no lane of the host subgroup waits.

  • Images go through an R32G32_UINT view declared Coherent; the placeholder image of an R64_UINT T# uses the same format on such a device.
  • Buffers use 32-bit atomic loads and stores, and every 32-bit buffer atomic of such a program takes the lock word of its 8-byte word, so a 32-bit and a 64-bit atomic on the same dword exclude each other.
  • A swap ORs its value with the old texel ANDed with the held lock word minus one, which is zero: on an Apple M3 Max, a texture write that does not depend on the preceding read of the same texel by the same thread landed before that read.
  • The driver allocates the table, zero-filled, only when the device lacks image or buffer 64-bit atomics, and gives its address to the pipelines that declare it through two specialization constants. A device with native support allocates nothing and emits the same SPIR-V as before.
  • The table words are supplied with the pipeline (like the workgroup offsets), so the prepared values of a mesh layer window do not record them, and a program that takes no lock (an unused-result bitwise 64-bit buffer atomic that #11510 splits into two 32-bit atomics) does not ask for them.

agc_driver_image_atomics64, agc_driver_buffer_atomics_or_swap64 and agc_driver_buffer_atomics_inc_dec64 run on MoltenVK instead of skipping, with new contention cases of 2,048 threads each, run with 32- and 64-lane waves: image_atomic_add and image_atomic_umax on one texel, buffer_atomic_swap_x2 on one qword (the returned values and the final value must be a permutation of the initial and the swapped values) and buffer_atomic_or mixed with buffer_atomic_or_x2 on one qword.

Tested

macOS 27.0.1, Apple M3 Max (36 GB), MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0, Release build with -DBUILD_TESTING=ON, x86_64 under Rosetta 2. main does not run its libraries on macOS, so both sides are main 3b7f31fa7 with the macOS port (the squash of #8658, which its author upstreams in parts) and local macOS build fixes for tests of main that Apple clang refuses; the change side adds this PR's commits.

  • The three atomic tests above pass on the change and skip on main (MoltenVK has neither feature).
  • Focused AGC, shader recompiler and VideoOut set, 811 tests, on both sides with VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1 (ctest --test-dir build-macos -R '<811 test names>' -V): on the change 707 pass, 68 skip, 35 fail and 1 times out, against 704, 71, 35 and 1 on main: the three tests above go from skipped to passed and no other test changes status. The failures are the ones main has on macOS (MoltenVK limits and macOS-only gaps of the tests, including the nine agc_driver_gds_integer_atomics64* tests, which this PR does not touch). Both runs report the same 77 VUID- errors test by test, and was not found but was requested by env var VK_INSTANCE_LAYERS appears only in the 12 lines that print the failure regex of the agc_driver_image_table_packed* tests. The logs show the loader inserting the layer (Insert instance layer) in 490 tests.
  • python3 tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py could not compare the open pull requests that conflict with main and share VulkanDevice.cpp, Texture.cpp, Pipeline.cpp, Recompiler.cpp or the SPIR-V emitters. The open pull requests that mention 64-bit atomics either skip their tests without shaderBufferInt64Atomics (#16934, #18655) or implement image_atomic_inc and image_atomic_dec natively (#16952); none emulates the atomics on a device that lacks them.

Not tested: Linux and Windows, and any device that has the 64-bit atomics natively (the emulation is not emitted there).

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: none
  • Unblocks: none
  • GPUs tested: Apple M3 Max, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.0.1
  • AI-assisted: yes

Context

Found while bringing up Reanimal (PPSA23372) on MoltenVK.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5277132022 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18842). Originally posted by [atinseau](https://github.com/atinseau) on 2026-11-12T12:10:20Z. --- ### What On a device without `VK_EXT_shader_image_atomic_int64` or `shaderBufferInt64Atomics`, such as MoltenVK, a 64-bit `image_atomic_*` or `buffer_atomic_*_x2` fails SPIR-V emission, and the tests that cover them skip. Reanimal (PPSA23372) stopped there at its first present. Each such atomic now takes one word of a device-wide table of 2^20 lock words (4 MiB), hashed from the texel coordinates or from the guest address of its 8-byte word. It reads and writes its two dwords and releases the word in the same loop iteration; the loop ends once no lane of the host subgroup waits. - Images go through an `R32G32_UINT` view declared `Coherent`; the placeholder image of an `R64_UINT` T# uses the same format on such a device. - Buffers use 32-bit atomic loads and stores, and every 32-bit buffer atomic of such a program takes the lock word of its 8-byte word, so a 32-bit and a 64-bit atomic on the same dword exclude each other. - A swap ORs its value with the old texel ANDed with the held lock word minus one, which is zero: on an Apple M3 Max, a texture write that does not depend on the preceding read of the same texel by the same thread landed before that read. - The driver allocates the table, zero-filled, only when the device lacks image or buffer 64-bit atomics, and gives its address to the pipelines that declare it through two specialization constants. A device with native support allocates nothing and emits the same SPIR-V as before. - The table words are supplied with the pipeline (like the workgroup offsets), so the prepared values of a mesh layer window do not record them, and a program that takes no lock (an unused-result bitwise 64-bit buffer atomic that #11510 splits into two 32-bit atomics) does not ask for them. `agc_driver_image_atomics64`, `agc_driver_buffer_atomics_or_swap64` and `agc_driver_buffer_atomics_inc_dec64` run on MoltenVK instead of skipping, with new contention cases of 2,048 threads each, run with 32- and 64-lane waves: `image_atomic_add` and `image_atomic_umax` on one texel, `buffer_atomic_swap_x2` on one qword (the returned values and the final value must be a permutation of the initial and the swapped values) and `buffer_atomic_or` mixed with `buffer_atomic_or_x2` on one qword. ### Tested macOS 27.0.1, Apple M3 Max (36 GB), MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0, Release build with `-DBUILD_TESTING=ON`, x86_64 under Rosetta 2. `main` does not run its libraries on macOS, so both sides are `main` `3b7f31fa7` with the macOS port (the squash of #8658, which its author upstreams in parts) and local macOS build fixes for tests of `main` that Apple clang refuses; the change side adds this PR's commits. - The three atomic tests above pass on the change and skip on `main` (MoltenVK has neither feature). - Focused AGC, shader recompiler and VideoOut set, 811 tests, on both sides with `VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1` (`ctest --test-dir build-macos -R '<811 test names>' -V`): on the change 707 pass, 68 skip, 35 fail and 1 times out, against 704, 71, 35 and 1 on `main`: the three tests above go from skipped to passed and no other test changes status. The failures are the ones `main` has on macOS (MoltenVK limits and macOS-only gaps of the tests, including the nine `agc_driver_gds_integer_atomics64*` tests, which this PR does not touch). Both runs report the same 77 `VUID-` errors test by test, and `was not found but was requested by env var VK_INSTANCE_LAYERS` appears only in the 12 lines that print the failure regex of the `agc_driver_image_table_packed*` tests. The logs show the loader inserting the layer (`Insert instance layer`) in 490 tests. - `python3 tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py` could not compare the open pull requests that conflict with `main` and share `VulkanDevice.cpp`, `Texture.cpp`, `Pipeline.cpp`, `Recompiler.cpp` or the SPIR-V emitters. The open pull requests that mention 64-bit atomics either skip their tests without `shaderBufferInt64Atomics` (#16934, #18655) or implement `image_atomic_inc` and `image_atomic_dec` natively (#16952); none emulates the atomics on a device that lacks them. Not tested: Linux and Windows, and any device that has the 64-bit atomics natively (the emulation is not emitted there). ### 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: none - [x] Unblocks: none - [x] GPUs tested: Apple M3 Max, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.0.1 - [x] AI-assisted: yes ### Context Found while bringing up Reanimal (PPSA23372) on MoltenVK.
Ethan-Hainline commented 2026-11-12 22:03:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ethan-Hainline on 2026-11-12T22:03:52Z.

Review: COMMENTED


I traced the two riskiest paths on c441deb085; both check out, and I have one question about the loop exit.

Lock table publication is correct. VulkanDevice.cpp:2681 creates the table when either 64-bit feature is missing, and VulkanDevice.cpp:4873 publishes DeviceAddress() into Context::atomicLockTable while assembling the context. The reset at :1212 is in the teardown that also resets pipelineCache, so it cannot leave a cached pipeline holding a freed address.

An unserved lane cannot return 0. In LockedDeviceUpdate (SpirvEmitterHelpers.hpp:311), a lane that loses the CAS reaches attemptMerge with served=false, so doneNext=false and waiting=true; the ballot keeps again true for that subgroup, and the lane only leaves after served=true, where resultNext is the old value. HostSubgroupSingleLane uses the lane's own predicate, which is the same rule.

Question: the ballot is a subgroup op that can be inside divergent control flow. Atomics are emitted wherever the guest instruction is, so a conditional if (valid) atomicAdd puts the loop, and OpGroupNonUniformBallot, inside a selection. SPIR-V requires group ops in uniform control flow, and on MoltenVK this lowers to simd_ballot, which expects the whole simdgroup. The loop does not need a collective exit: a lane can leave when its own doneNext is true. Was the ballot chosen to keep the wave converged, or only for the "no lane waits" property? If it is not required, exiting on doneNext would remove the uniformity requirement; if it is, a comment saying why the atomic site is uniform would help.

This is a question, not a blocker; the native path and the three hardware reports look right.

AI-assisted review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5882720225 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18842#pullrequestreview-5882720225). Originally posted by [Ethan-Hainline](https://github.com/Ethan-Hainline) on 2026-11-12T22:03:52Z. **Review:** COMMENTED --- I traced the two riskiest paths on `c441deb085`; both check out, and I have one question about the loop exit. **Lock table publication is correct.** `VulkanDevice.cpp:2681` creates the table when either 64-bit feature is missing, and `VulkanDevice.cpp:4873` publishes `DeviceAddress()` into `Context::atomicLockTable` while assembling the context. The reset at `:1212` is in the teardown that also resets `pipelineCache`, so it cannot leave a cached pipeline holding a freed address. **An unserved lane cannot return 0.** In `LockedDeviceUpdate` (`SpirvEmitterHelpers.hpp:311`), a lane that loses the CAS reaches `attemptMerge` with `served=false`, so `doneNext=false` and `waiting=true`; the ballot keeps `again` true for that subgroup, and the lane only leaves after `served=true`, where `resultNext` is the old value. `HostSubgroupSingleLane` uses the lane's own predicate, which is the same rule. **Question: the ballot is a subgroup op that can be inside divergent control flow.** Atomics are emitted wherever the guest instruction is, so a conditional `if (valid) atomicAdd` puts the loop, and `OpGroupNonUniformBallot`, inside a selection. SPIR-V requires group ops in uniform control flow, and on MoltenVK this lowers to `simd_ballot`, which expects the whole simdgroup. The loop does not need a collective exit: a lane can leave when its own `doneNext` is true. Was the ballot chosen to keep the wave converged, or only for the "no lane waits" property? If it is not required, exiting on `doneNext` would remove the uniformity requirement; if it is, a comment saying why the atomic site is uniform would help. This is a question, not a blocker; the native path and the three hardware reports look right. AI-assisted review.
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!18842
No description provided.