fix(shader): LDS 32-bit atomics take the workgroup lock of the program's 64-bit LDS atomics #2675
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2675"
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 husseinkad on 2026-10-10T10:41:36Z.
What
LDS has no native 64-bit atomics, so
ds_*_u64read-modify-writes the two dwords under a workgroup lock. That lock only ordered 64-bit atomics against each other: a 32-bit LDS atomic on one of the same dwords could land between the two halves of a 64-bit update and be lost. #2297 measured the hardware (gfx1036): 128 lanes ofds_add_u32plus 128 lanes ofds_add_u64on one slot sum exactly, so the two widths are serialised.In a program that contains a 64-bit LDS atomic (
requirements.ldsLock), every 32-bit LDS atomic now goes through the same lock (EmitLdsAtomic32;LockedLdsUpdatetakes the result type and zero value). A program without a 64-bit LDS atomic emits the same SPIR-V as before.Tests:
agc_spirv_lds_atomic_lock(new, no GPU needed, 0.3 s): recompiles compute programs and counts the lock opcodes.ds_add_u64alone has one lock; with ads_add_u32(ords_inc_u32) there are two, and the native 32-bitOpAtomicIAddsits between a lock acquire and release; a program with only 32-bit LDS atomics has no lock. Deterministic, so it also guards CI's lavapipe, where the race itself cannot occur.agc_driver_lds_mixed_width_atomics(new execution test): 256 threads x 16 iterations ofds_add_u64+ds_add_u32on one slot must sum exactly, plus a divergent case (64-bit add under exec0x55555555, 32-bit add under0xaaaaaaaa) that checks the lock loop terminates with inactive lanes.Fixes #2297
Tested
Windows 11, WinLibs GCC 15.2.0 r7, Release, NVIDIA RTX 4050 (Vulkan):
ctest -R "lds_|ds_|lds": 24 of 24 pass (72 s total, each test a few seconds).agc_spirv_lds_atomic_lockfails ("elect 1, lock acquire 1, lock release 1, expected 2 each") and the execution test fails on this GPU (3 of 3 runs for the first version of the test; lane 0 read0x00001d9ffffff000, expected0x00001ffffffff000).tools/check_conventions.py --base origin/mainandtools/pr_overlap.py --branchpass;tools/check_test_registration.pyreports no unregistered executable.ds_add_u32againstds_add_u64was measured on hardware; the other 32-bit LDS atomics taking the lock is extrapolated and recorded in TechnicalDebt together with the unmeasured throughput cost, the non-atomic halves ofds_condxchg32_rtn_b64, and the plain LDS reads and writes that stay outside the lock.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T10:41:55Z.
Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T19:48:31Z.
Thank you @oneandonlydean for reopening this. The description above predates the last two commits, so here is where the branch stands (head
7775d6c7, four commits onmain):ANYPS5_ENABLE_SPIRV_TOOLS=ON): with the change, the issue's kernel loses 0 of 128 increments in 20 of 20 runs on RADV (Ryzen 7950X3D iGPU, wave32 and wave64) and on an RTX 3080; onmainit lost 16 to 96 in every run.2e0fd5db): the first version of the execution test used a loop that failed SPIR-V validation withANYPS5_ENABLE_SPIRV_TOOLS=ON(the CI Linux setting), because SPIRV-Tools'BlockMergePassturns a valid module with a nested loop into an invalid one. That is independent of this change; the kernel is unrolled now and passes with validation on.7775d6c7): a single-pass kernel in the issue's shape (even lanesds_add_u64, odd lanesds_add_u32at +4).agc_spirv_lds_atomic_lockfails on every host when the change is reverted, so CI's lavapipe covers it. A per-lane-load variant of the execution case is the open follow-up.ctest -R "lds_|ds_|agc_spirv_lds"passes 23 of 23 in the default build andlds_mixed|agc_spirv_ldspasses with SPIRV-Tools on.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T20:05:46Z.
@husseinkad The per-lane-load variant of the execution case is in husseinkad/AnyPS5#1 (ab238a4b, on 7775d6c7), from my #2297 measurements, if you want to take it into this branch.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T20:09:29Z.
Thank you @SleglJan: I read the diff, ran it here (RTX 4050, Windows:
lds_mixed|agc_spirv_ldspass with SPIRV-Tools on) and merged your per-lane-operand case into the branch with its authorship intact; the head is now2f71d997(five commits). With your measurements the execution test now fails on a revert on both RADV and the RTX 3080, so the earlier note about RADV no longer applies; the deterministic structural test remains the check that works on every host, including lavapipe in CI.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T21:26:32Z.
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/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cppL1232-1233Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:37:11Z.
Review: CHANGES_REQUESTED
Thanks, the fix is right on the hardware. I measured it on gfx1035 with 256 lanes (8 waves) × 16 iterations on one LDS slot:
ds_add_u64againstds_add/sub/inc/dec_u32on the high dword,ds_add_u32on the low dword, andds_xor_b64againstds_xor_b32on both dwords all come out exact in every run. So extending the lock fromds_add_u32to the other 32-bit atomics holds.It needs a rebase before it can land:
main.6a1301f32(FLAT addresses in the shared aperture reach LDS) addedApertureAtomic64. That function forward-declares and calls the two-argumentLockedLdsUpdate(state, update)(SpirvMemoryInstructions.cpp, the declaration beforeApertureAtomic64and itsreturn kind == ResourceKind::Lds ? LockedLdsUpdate(state, update) : update();). With this PR merged, GCC stops with "LockedLdsUpdate(...)is used but never defined". Pass the type and zero as inSharedAtomic64, and drop the stale declaration.ApertureAtomic32handles 32-bitflat_atomic_*whose address lands in the LDS aperture, and itskind == ResourceKind::Ldsbranch still updates without the lock. So aflat_atomic_addracing ads_add_u64(or a 64-bit FLAT atomic, which does take the lock) on the same dwords can still lose updates. Wrap that branch likeEmitLdsAtomic32does whenstate.requirements.ldsLockis set. Then add a case toagc_spirv_lds_atomic_lock, and mention the FLAT path in the TechnicalDebt line.LockedLdsUpdatetoo, and the two branches conflict. Whichever lands first, the other rebases on it.Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T22:39:33Z.
Thank you @adriadam10 for measuring this on gfx1035 and for the three points. I'm working on them now: rebasing onto current
main, updatingApertureAtomic64's call toLockedLdsUpdateto take the type and zero likeSharedAtomic64and dropping the stale forward declaration, locking the LDS branch ofApertureAtomic32whenldsLockis set (with a new case inagc_spirv_lds_atomic_lockand a mention of the FLAT path in the TechnicalDebt entry), and keeping the diff againstLockedLdsUpdatesmall so that #3015 and this PR rebase on each other easily. I'll post here when the rebased branch is pushed, with the build and test results.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T23:00:38Z.
@adriadam10 all three points are addressed; the branch is rebased onto
main91b134131(force-pushed with a lease, six commits, nowc9e1fa0ce; it still merges cleanly with the latermain):ApertureAtomic64now callsLockedLdsUpdate(state, TypeU64(state), ConstantU64(state, 0u), update)asSharedAtomic64does, and the stale forward declaration is gone. I folded this into the first fix commit so every commit builds; before it, the rebased branch failed withLockedLdsUpdate(...) is used but never defined.ApertureAtomic32takes the lock whenstate.requirements.ldsLockis set; programs without a 64-bit LDS atomic emit the same SPIR-V as before.agc_spirv_lds_atomic_lockgains a program withds_add_u64plus a 32-bitflat_atomic_add(expects two elects) and a flat-only program (expects none); it fails with only that lock reverted (elect 1, expected 2). The TechnicalDebt entry names the 32-bit FLAT forms and says they were not measured against a 64-bit atomic.servedResultphi inLockedLdsUpdate;git merge-treereports one conflict. I kept my part to the signature (typeandzeroinstead ofTypeU64/ConstantU64in three places), so whichever lands second only has to usetypein the phis (and in #3015'sattemptedResultphi if it lands second). I did not touch #3015's branch.Windows 11, WinLibs GCC 15.2.0 r7:
ctest -R "lds_|ds_|agc_spirv_lds|flat_aper"passes 24 of 24 (includingagc_driver_flat_apertures), and withANYPS5_ENABLE_SPIRV_TOOLS=ONlds_mixed|agc_spirv_lds|flat_aperpasses 3 of 3; conventions pass. Nothing new was measured on hardware.One request, and please just say no if it doesn't fit your process: I have a number of other small, tested fixes on my fork that the intake check closed automatically (each links its branch from an issue, #2988 and later). If you would like to look at any of them, reopening is welcome, and I understand the intake limit exists for a reason.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:15:41Z.
Review: APPROVED
Checked the rebased head: builds on current
main,agc_spirv_lds_atomic_lockandagc_driver_lds_mixed_width_atomicspass, and both fail with the source change reverted. The 32-bit flat-aperture LDS path now takes the lock as well, and the TechnicalDebt entry matches. Confirmed on hardware by @SleglJan (RADV and RTX 3080). Merging once the full Build for this head is green.Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T23:18:45Z.
Thank you @adriadam10 for the careful review, for measuring it on gfx1035, and for the merge, and thanks to @SleglJan for the RADV and RTX 3080 runs and the per-lane test case, and to @oneandonlydean for reopening it.
AI-assisted: yes.