fix(shader): serialize locked LDS updates with a wave-uniform loop exit #833

Merged
LeandroLP23 merged 1 commit from upstream-pr/833 into main 2026-10-07 07:57:08 +00:00
LeandroLP23 commented 2026-10-06 08:16:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-06T08:16:41Z.


Closes #826.

What

64-bit LDS atomics in compute shaders go through the LDS lock (LockedLdsUpdate). Each lane left the lock loop as soon as its compare-exchange acquired the lock, so the exit was divergent and the driver's compiler was free to move the critical section and the release past the loop exit. On AMD the lock holder then waited outside the loop for the lanes still spinning on its lock.

Now each iteration elects one of the lanes not yet served (OpGroupNonUniformElect); that lane alone spins on the lock, runs the update and releases it, and the loop exits once a ballot of the unserved lanes is empty. The exit is uniform across the subgroup, so the holder cannot be separated from the loop. The lock stays for exclusion between subgroups of a workgroup. ldsLock now also requests subgroupBallot for the elect and ballot capabilities.

Investigation

  • Every run of agc_driver_lds_atomics64 on unmodified main logged a LiveKernelEvent 141 (VIDEO_ENGINE_TIMEOUT_DETECTED) in Windows Error Reporting: the wait took ~5 s, none of the 4096 output words was written, and vkDeviceWaitIdle still returned success.
  • Running the update without the lock: no hang (2 ms wait) and every per-lane result correct; only the contended checks failed.
  • Electing one lane per attempt while keeping the per-lane exit still hung, which fits the critical section being moved out of the loop rather than intra-wave contention alone.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release with BUILD_TESTING=ON, AMD Radeon 860M (RDNA 3.5), driver 32.0.13058.8001. No title or homebrew.

  • agc_driver_lds_atomics64: passes in 0.34 s, contended serial-order checks included, and no LiveKernelEvent is logged.
  • Full ctest: 356/358 passed; the 2 failures are #825 and #827 (fixed by #829), which also fail on unmodified main.
  • NVIDIA, tested by @fisherjoey (comment): Fedora 44, RTX 5070 Ti, driver 595.91.07. On main the test hangs there too (NVRM: Xid 109 CTX SWITCH TIMEOUT, then a segfault, every run); with this PR it passes in about 0.3 s, 3 of 3 runs, and the full ctest passes.
  • Not tested on Intel.

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 (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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4756053805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/833). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-06T08:16:41Z. --- Closes #826. ### What 64-bit LDS atomics in compute shaders go through the LDS lock (`LockedLdsUpdate`). Each lane left the lock loop as soon as its compare-exchange acquired the lock, so the exit was divergent and the driver's compiler was free to move the critical section and the release past the loop exit. On AMD the lock holder then waited outside the loop for the lanes still spinning on its lock. Now each iteration elects one of the lanes not yet served (`OpGroupNonUniformElect`); that lane alone spins on the lock, runs the update and releases it, and the loop exits once a ballot of the unserved lanes is empty. The exit is uniform across the subgroup, so the holder cannot be separated from the loop. The lock stays for exclusion between subgroups of a workgroup. `ldsLock` now also requests `subgroupBallot` for the elect and ballot capabilities. ### Investigation - Every run of `agc_driver_lds_atomics64` on unmodified `main` logged a `LiveKernelEvent 141` (VIDEO_ENGINE_TIMEOUT_DETECTED) in Windows Error Reporting: the wait took ~5 s, none of the 4096 output words was written, and `vkDeviceWaitIdle` still returned success. - Running the update without the lock: no hang (2 ms wait) and every per-lane result correct; only the contended checks failed. - Electing one lane per attempt while keeping the per-lane exit still hung, which fits the critical section being moved out of the loop rather than intra-wave contention alone. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release with `BUILD_TESTING=ON`, AMD Radeon 860M (RDNA 3.5), driver 32.0.13058.8001. No title or homebrew. - `agc_driver_lds_atomics64`: passes in 0.34 s, contended serial-order checks included, and no LiveKernelEvent is logged. - Full `ctest`: 356/358 passed; the 2 failures are #825 and #827 (fixed by #829), which also fail on unmodified `main`. - NVIDIA, tested by @fisherjoey ([comment](https://github.com/boykopovar/AnyPS5/pull/833#issuecomment-6024461730)): Fedora 44, RTX 5070 Ti, driver 595.91.07. On `main` the test hangs there too (`NVRM: Xid 109 CTX SWITCH TIMEOUT`, then a segfault, every run); with this PR it passes in about 0.3 s, 3 of 3 runs, and the full `ctest` passes. - Not tested on Intel. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fisherjoey commented 2026-10-06 20:04:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-06T20:04:58Z.


Another data point for #826: the same test hangs the GPU on NVIDIA.

  • Fedora 44 (Nobara), kernel 7.2, GCC 16.2.1, RTX 5070 Ti, driver 595.91.07
  • main at 5895ea72: agc_driver_lds_atomics64 prints [gpu] recorded batch still running on the GPU after 5 s, the kernel logs NVRM: Xid 109 CTX SWITCH TIMEOUT for the test process, and the test segfaults. It happens on every run.
  • main + this PR (e302d550): passes in about 0.3 s, 3 of 3 runs, no Xid. Full ctest (359 tests, with #928 also merged) passes.

This supports the forward-progress explanation in #826. The lock spin never finishes on NVIDIA either, and the driver kills the context instead of returning stale output.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6024461730 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/833#issuecomment-6024461730). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-06T20:04:58Z. --- Another data point for #826: the same test hangs the GPU on NVIDIA. - Fedora 44 (Nobara), kernel 7.2, GCC 16.2.1, RTX 5070 Ti, driver 595.91.07 - `main` at 5895ea72: `agc_driver_lds_atomics64` prints `[gpu] recorded batch still running on the GPU after 5 s`, the kernel logs `NVRM: Xid 109 CTX SWITCH TIMEOUT` for the test process, and the test segfaults. It happens on every run. - `main` + this PR (e302d550): passes in about 0.3 s, 3 of 3 runs, no Xid. Full ctest (359 tests, with #928 also merged) passes. This supports the forward-progress explanation in #826. The lock spin never finishes on NVIDIA either, and the driver kills the context instead of returning stale output.
adriadam10 commented 2026-10-07 07:57:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T07:57:05Z.


Checked on RADV, local lavapipe and CI's lavapipe (agc_driver_lds_atomics64 green, full ctest 385/385 merged onto main). On hardware (gfx1035, HW oracle), contended ds_add_rtn_u64/ds_max_rtn_u64 on one address serialize in lane order, which is exactly what electing the lowest active lane gives. The hang doesn't reproduce on RADV, so the fix itself rests on your Windows AMD run and @fisherjoey's NVIDIA one. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6033585113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/833#issuecomment-6033585113). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T07:57:05Z. --- Checked on RADV, local lavapipe and CI's lavapipe (`agc_driver_lds_atomics64` green, full ctest 385/385 merged onto main). On hardware (gfx1035, HW oracle), contended `ds_add_rtn_u64`/`ds_max_rtn_u64` on one address serialize in lane order, which is exactly what electing the lowest active lane gives. The hang doesn't reproduce on RADV, so the fix itself rests on your Windows AMD run and @fisherjoey's NVIDIA one. Merging.
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!833
No description provided.