fix(shader): elect one lane for locked 64-bit updates #24081

Merged
Romannachwievor merged 1 commit from upstream-pr/24081 into main 2026-11-27 12:37:10 +00:00
Romannachwievor commented 2026-11-20 05:38:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Romannachwievor on 2026-11-20T05:38:59Z.


What

On a device without native 64-bit buffer or image atomics, the recompiler emulates them with a device-scope spin lock per address (LockedDeviceUpdate). Every pending lane of a subgroup ran the lock's compare-exchange at the same time and looped until it was served. On Apple GPUs through MoltenVK, a contended dispatch then never finishes: the batch is still running after 5 s, macOS resets the GPU, and the device is lost (agc_driver_buffer_atomics_or_swap64, 32 groups of 64 threads on one lock).

Now one lane per host subgroup is elected each iteration to try the lock, with the same election and merge structure LockedLdsUpdate already uses. The other pending lanes wait at the merge and try again on a later iteration. A lane that gets the lock runs the update and releases the lock in the same iteration.

Cost: lanes of one subgroup that update different addresses (different locks) now take turns, one per iteration, where before they could each take their own lock in the same iteration. This only affects devices that take the emulated path. I have not measured it.

Tested

macOS 26.5, Apple M1, x86_64 Release under Rosetta 2, MoltenVK from LunarG SDK 1.4.363.0. One tree and build directory (this branch on main 365fdeddbe). The base was built from the same tree with the parent version of SpirvEmitterHelpers.hpp, then the change was rebuilt. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, ctest --test-dir build -R '^<test>$' -V, 10 serial runs per test and side:

test base this change
agc_driver_buffer_atomics_or_swap64 5 of 10 fail with recorded batch 22 still running on the GPU after 5 s, then Lost VkDevice ... (victim of GPU error/recovery) and vkDeviceWaitIdle: Vulkan result -4. 7 of 10 runs logged the batch as still running after 5 s. Runs took 0.8 to 69.9 s 10 of 10 pass, no batch still running after 5 s, 0.7 to 6.5 s
agc_driver_image_atomics64 10 of 10 pass, 0.8 to 2.4 s 10 of 10 pass, 0.7 to 1.1 s

All 40 logs show Insert instance layer "VK_LAYER_KHRONOS_validation", none shows was not found but was requested by env var VK_INSTANCE_LAYERS, and none contains a VUID- message.

Not tested: a device with native 64-bit atomics (this path is not used there), and the cost for uncontended lanes described above.

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 M1 (8-core GPU), MoltenVK from LunarG SDK 1.4.363.0, macOS 26.5
  • AI-assisted: yes (Claude Code)

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5408404896 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24081). Originally posted by [Romannachwievor](https://github.com/Romannachwievor) on 2026-11-20T05:38:59Z. --- ### What On a device without native 64-bit buffer or image atomics, the recompiler emulates them with a device-scope spin lock per address (`LockedDeviceUpdate`). Every pending lane of a subgroup ran the lock's compare-exchange at the same time and looped until it was served. On Apple GPUs through MoltenVK, a contended dispatch then never finishes: the batch is still running after 5 s, macOS resets the GPU, and the device is lost (`agc_driver_buffer_atomics_or_swap64`, 32 groups of 64 threads on one lock). Now one lane per host subgroup is elected each iteration to try the lock, with the same election and merge structure `LockedLdsUpdate` already uses. The other pending lanes wait at the merge and try again on a later iteration. A lane that gets the lock runs the update and releases the lock in the same iteration. Cost: lanes of one subgroup that update different addresses (different locks) now take turns, one per iteration, where before they could each take their own lock in the same iteration. This only affects devices that take the emulated path. I have not measured it. ### Tested macOS 26.5, Apple M1, x86_64 Release under Rosetta 2, MoltenVK from LunarG SDK 1.4.363.0. One tree and build directory (this branch on `main` `365fdeddbe`). The base was built from the same tree with the parent version of `SpirvEmitterHelpers.hpp`, then the change was rebuilt. `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`, `ctest --test-dir build -R '^<test>$' -V`, 10 serial runs per test and side: | test | base | this change | |---|---|---| | `agc_driver_buffer_atomics_or_swap64` | 5 of 10 fail with `recorded batch 22 still running on the GPU after 5 s`, then `Lost VkDevice ... (victim of GPU error/recovery)` and `vkDeviceWaitIdle: Vulkan result -4`. 7 of 10 runs logged the batch as still running after 5 s. Runs took 0.8 to 69.9 s | 10 of 10 pass, no batch still running after 5 s, 0.7 to 6.5 s | | `agc_driver_image_atomics64` | 10 of 10 pass, 0.8 to 2.4 s | 10 of 10 pass, 0.7 to 1.1 s | All 40 logs show `Insert instance layer "VK_LAYER_KHRONOS_validation"`, none shows `was not found but was requested by env var VK_INSTANCE_LAYERS`, and none contains a `VUID-` message. Not tested: a device with native 64-bit atomics (this path is not used there), and the cost for uncontended lanes described above. ### 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 M1 (8-core GPU), MoltenVK from LunarG SDK 1.4.363.0, macOS 26.5 - [x] AI-assisted: yes (Claude Code) AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-11-20 05:42:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T05:42:19Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the elected lane serializes attempts while the subgroup ballot keeps waiting lanes active until each completes.

What I ran

Reviewed head 61b45000a4bbd084cb6873a047c5cdb53b215978. No commands or tests were run; the author-reported results are not independently verified. Read description.md, commits.md, manifest.json, diff.patch, and the full changed file core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp. No changed files were excluded.

Results

Check Result Note
Code review passed Changed control flow and helper reviewed
Build and tests not run Results in the description are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported MoltenVK runs, recompiler build, and git diff --check were not run during this review. No other changed files were listed in the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5988075740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24081#pullrequestreview-5988075740). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T05:42:19Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the elected lane serializes attempts while the subgroup ballot keeps waiting lanes active until each completes. ## What I ran Reviewed head `61b45000a4bbd084cb6873a047c5cdb53b215978`. No commands or tests were run; the author-reported results are not independently verified. Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, and the full changed file `core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp`. No changed files were excluded. ## Results | Check | Result | Note | |---|---|---| | Code review | passed | Changed control flow and helper reviewed | | Build and tests | not run | Results in the description are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported MoltenVK runs, recompiler build, and `git diff --check` were not run during this review. No other changed files were listed in the manifest. ## To get this to a good state Nothing is required.
Jazeupoi commented 2026-11-20 11:36:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Jazeupoi on 2026-11-20T11:36:19Z.

Review: COMMENTED


Ran it on a second Mac: Apple M4, macOS 27.0.1, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), x86_64 Release build under Rosetta 2. Head 61b45000a4 applied onto main e825fa1492 with #17425 and #11049 (needed for the macOS build); clean apply. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, each run being ctest -R '^<test>$' -j1.

vulkaninfo reports shaderBufferInt64Atomics = false and shaderSharedInt64Atomics = false on this GPU, so these tests take the emulated path, as on the M1.

Test main this PR
agc_driver_buffer_atomics_or_swap64 passes 10 of 10, about 0.25 s each passes 10 of 10, about 0.27 s each
agc_driver_image_atomics64 passes 10 of 10 passes 10 of 10

So the M4 does not hang on main. The contended dispatch finishes here without the election, which suggests the M1 hang depends on how its GPU schedules the spinning lanes. The change causes no regression here. The other 12 tests matching agc_driver_.*atomic.*64 pass, or skip for lack of GDS, on both trees, 2 runs each.

I ran these without the validation layer.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5992048323 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24081#pullrequestreview-5992048323). Originally posted by [Jazeupoi](https://github.com/Jazeupoi) on 2026-11-20T11:36:19Z. **Review:** COMMENTED --- Ran it on a second Mac: Apple M4, macOS 27.0.1, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), x86_64 Release build under Rosetta 2. Head `61b45000a4` applied onto `main` `e825fa1492` with #17425 and #11049 (needed for the macOS build); clean apply. `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`, each run being `ctest -R '^<test>$' -j1`. `vulkaninfo` reports `shaderBufferInt64Atomics = false` and `shaderSharedInt64Atomics = false` on this GPU, so these tests take the emulated path, as on the M1. | Test | `main` | this PR | |---|---|---| | `agc_driver_buffer_atomics_or_swap64` | passes 10 of 10, about 0.25 s each | passes 10 of 10, about 0.27 s each | | `agc_driver_image_atomics64` | passes 10 of 10 | passes 10 of 10 | So the M4 does not hang on `main`. The contended dispatch finishes here without the election, which suggests the M1 hang depends on how its GPU schedules the spinning lanes. The change causes no regression here. The other 12 tests matching `agc_driver_.*atomic.*64` pass, or skip for lack of GDS, on both trees, 2 runs each. I ran these without the validation layer. 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!24081
No description provided.