fix(shader): initialize LDS lock from one invocation #6670
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6670"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T03:37:45Z.
What
LockedLdsUpdateuses a workgroup-memory word as a spinlock. Its entry code currently emits an unguardedOpStore 0to that word, so every invocation in the workgroup writes the same location before the barrier. Those unsynchronized non-atomic writes are a data race. The AMD Windows compiler hang reported in #6666 is consistent with this invalid initialization pattern, though I have not reproduced that hardware-specific hang with this change.Only local invocation 0 now initializes the lock. A workgroup barrier still makes the initialized value visible before any invocation attempts to acquire it. The SPIR-V regression test checks that the lock's zero store is guarded by
LocalInvocationIndex == 0and has a negative control for the prior unguarded form.Related: #6666.
Tested
agc_spirv_lds_atomic_lock_testsagainst the configured Linux build artifacts, and ran it successfully.python3 tools/check_conventions.py --base upstream/main --head HEADandgit diff --checkpass.Checklist
main; no other open PR implements this lock initializationNotImplemented_nid_no_patch); silent stubs are documentedImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T04:34:53Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/tests/SpirvLdsAtomicLock.cppL30, L65, L69, L97Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppImported from boykopovar/AnyPS5. Originally posted by veosett on 2026-10-16T06:13:54Z.
Follow-up investigation of #6666 on the same RX 6900 XT, starting from PR head
4864a748022df4fb673202a0c1f430dac339ed10(main5d9dcc42d2ec5401aebd089028b8d4d7410a77b4). This narrows the failure; it is not a proposed fix.A reduced copy of the existing uniform kernel gives these Windows results:
Requiring a physical 32-lane compute subgroup does not remove the failure. Replacing the initial zero stores with atomic stores does not remove it either. The reduced fixture checks every dispatched lane and unchanged sentinels outside the workgroup's output.
Linux/lavapipe controls at the same PR head pass for 64, 128 and 256 threads with one mixed pair and for 128 threads / guest wave64 with one 64-bit operation.
Driver ISA observation. With a temporary local capture hook using
VK_KHR_pipeline_executable_properties, the 128-thread module containss_barrierwhile the workgroup lock is still held:The corresponding SPIR-V critical section ends in
OpAtomicStorewith Workgroup scope andRelease | WorkgroupMemorysemantics; it has noOpControlBarrierthere. The 64-thread native module omits that internals_barrier. Waiting at a collective barrier while holding a lock is a plausible explanation of this multi-wave stall; this observation is not a Vulkan conformance verdict or proof of one hardware scheduling mechanism.As a diagnostic only, clearing the acquire/release operands of the lock makes the reduced 128-thread 64-bit-only case produce the exact result. The mixed case still stalls: its 32-bit update also emits
OpMemoryBarrierinside the lock. This relaxed experiment is not a safe correction. SPIR-V memory semantics and the Vulkan memory model require preserving the relevant ordering and visibility guarantees.Explicit memory barriers, broader atomic scope and the existing buffer-backed LDS path completed some probes with incorrect per-lane results, so none is being proposed as a fix. The saved diagnostic modules were checked with the project's bundled SPIRV-Tools validator. The ISA-capture and optional device-feature hooks were local diagnostics, not part of the clean PR head; they have been removed from the source after capture.
Environment: Windows 11, RX 6900 XT (RDNA 2, 1002:73bf), AMD proprietary driver 25.10.30.02 / Vulkan 1.4.315, WinLibs GCC 15.2.0 r7, Release, bundled dependencies, SPIRV-Tools ON. Linux: Ubuntu 24.04 WSL2, GCC 13.3, lavapipe/Mesa 25.2.8, LLVM 20.1.2. AnyPS5 shader cache disabled, Vulkan required, selected GPU verified. User driver cache was left intact; an additional changed-generator-word control still stalled.
No game, console or other GPU/driver run. The full CTest suite was not run. No production fix is claimed. Logs, diagnostic source, SPIR-V and ISA captures retained locally.
AI-assisted: yes (Codex). Builds and runs were executed on this machine.
Imported from boykopovar/AnyPS5. Originally posted by tezzgod74 on 2026-10-16T10:02:10Z.
Ran it on the RDNA 2 iGPU from #6666. This PR at
4864a7480(onmain5d9dcc42d), Windows 11 Pro 10.0.26300, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release:agc_spirv_lds_atomic_lockagc_driver_lds_mixed_width_atomics, 3 runslane 0 read 0xdeadbeefdeadbeefSame as
main, so the per-invocation lock store isn't what makes the dispatch run past ~4 s here. Every invocation stores the same zero before the workgroup barrier, so no lane can take the lock until those stores are done. The change still removes a data race, though.This matches @veosett's RX 6900 XT runs above on a second RDNA 2 device, here an APU on the same 21045 driver branch. Their ISA capture (an
s_barrieremitted between the critical section'sds_write_b64and the lock release, only in the 128-thread module) fits the threshold in #6666 exactly.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T13:06:50Z.
Thanks for testing this head on the reported RDNA 2 iGPU and the RDNA 4 control. The 3/3 result confirms this initialization change does not resolve the #6666 stall. I suggest keeping #6670 scoped to the separate SPIR-V lock-initialization correctness issue. The captured
s_barrierwhile the lock is held is the concrete lead for a separate follow-up; a change there needs to preserve the memory-ordering guarantees and be validated on RDNA 2.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-16T15:30:07Z.
Review: COMMENTED
NVIDIA control for the #6666 discussion, plus the new test on a device.
Machine: NVIDIA GeForce RTX 3070 (Ampere), driver 595.99.02, Vulkan 1.4.329, plus lavapipe (Mesa 26.0.3, LLVM 21.1.8). Ubuntu 26.04 (kernel 7.0.0-38), i7-8700, GCC 15.2, Release,
BUILD_TESTING=ON. Head4864a7480merged ontomain0615cd2b8, clean merge (maindid not build at the time, #6784).GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02), lavapipe (Mesa 26.0.3, LLVM 21.1.8), Ubuntu 26.04
Set:
agc_spirv_lds_atomic_lock, everyagc_driver_lds_*test and the threeagc_driver_pixel_lds*tests (15),-j1 --timeout 60.main's emitter):agc_spirv_lds_atomic_lockfails withthe workgroup lock is not initialized by only local invocation zero; the other 14 pass.agc_driver_lds_mixed_width_atomicstakes 0.27 s without the change and 0.26-0.53 s with it; no stall either way, unlike the RDNA 2 runs above.lds_addtid,pixel_lds,pixel_lds_lane_slots).So on NVIDIA the stall does not show with or without this change, which fits it being specific to the RDNA 2
s_barrierplacement rather than to the lock initialization. I did not capture ISA here.Not checked: Windows, AMD, the reduced 128-thread kernel from the comment above.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T15:34:50Z.
Thanks for the RTX 3070 and lavapipe control runs. The output comparison and test mutation result add useful hardware evidence for the LDS-lock initialization fix.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T12:53:09Z.
I checked #7368 against this change after it merged to
mainas385ed71f2. They cover separate cases: #7368 uses native 64-bit buffer atomics for device-backed LDS when the feature is available, while workgroup-memory LDS still usesLockedLdsUpdate. This PR's single-invocation initialization remains an independent SPIR-V correctness fix for the lock path. #7368 also confirms the reported #6666 hardware hang is addressed by the separate #6976 lock-release change; I am not claiming this initialization change fixes that RDNA 2 runtime failure.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T08:46:49Z.
Checked: the lock zero-store is now done by local invocation 0 only, ahead of the existing workgroup barrier;
LocalInvocationIndexis added as an input when missing; the regression test requires the store inside theLocalInvocationIndex == 0branch and the old unguarded form fails it. Third-party runs (RDNA 2/3/4, RTX 3070, lavapipe) show no regression. Merged.