fix(shader): release the LDS lock with an exchange and drop fences inside it #6976
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6976"
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 CrimsonMac on 2026-10-17T00:23:03Z.
What
Fixes #6666. On AMD RDNA 2 under Windows, a compute program with 64-bit LDS atomics hangs once its workgroup has more than one wave:
agc_driver_lds_mixed_width_atomicsruns for about 4 s and writes nothing (a TDR on an RX 6400, #5271).The driver's ISA (
VK_KHR_pipeline_executable_properties, Ryzen 5 7600 iGPU, 0x164e, driver 32.0.21042.62) shows why.LockedLdsUpdatereleases the workgroup lock withOpAtomicStore(Release | WorkgroupMemory, Workgroup scope), and the driver compiles that release tos_waitcnt lgkmcnt(0),s_barrier,ds_write_b32. The 32-bit atomics inside the lock add a seconds_barrierthrough theirOpMemoryBarrier(AcquireRelease, Workgroup). The wave holding the lock waits at the barrier, and the other waves never reach it because they loop on the lock. This matches @veosett's capture on an RX 6900 XT in #6670.On the same driver, a release on a read-modify-write atomic does not produce a barrier. The lock is now:
OpAtomicExchangewith the same scope and semantics (Release | LDS memory, Workgroup). The driver compiles it tos_waitcnt lgkmcnt(0)+ds_write_b32, which still waits for every earlier LDS access before freeing the lock.OpMemoryBarrierafter a 32-bit LDS atomic (Atomic32,SharedAtomicUpdate) and at the end ofAtomicUpdateTyped's compare-exchange loop is not emitted between acquire and release. In a program that takes the lock, every LDS atomic runs under it, and the lock's acquire and release use the same scope and storage classes as those fences, so the next holder's acquire already sees everything the previous holder did. Fences outside the lock, and programs without 64-bit LDS atomics, are unchanged.Release forms tried on the RDNA 2 iGPU (
agc_driver_lds_mixed_width_atomics):OpAtomicStoreRelease (main)OpMemoryBarrierRelease + relaxedOpAtomicStoreOpAtomicExchangeReleaseOpAtomicCompareExchangeReleaseOpAtomicExchangeRelease (this PR)OpAtomicCompareExchangeReleaseagc_spirv_lds_atomic_locknow checks that the lock is released by an exchange with Release semantics and that noOpMemoryBarriersits between acquire and release, fords_add_u64,ds_add_u32,ds_inc_u32and a flat atomic into the LDS aperture. Against main's emitter it fails ("a ds_add_u64 is not serialised by one elected lane taking the workgroup lock"); with only theAtomic32fence restored it fails with "ds_add_u32 emits a memory barrier while it holds the workgroup lock".No title is known to hit this. The shader census in #4061 found no 64-bit LDS atomics, so this fixes the hang on RDNA 2 and GCN rather than a game.
#6670 changes the lock's initial store and adds a check to the same test file; the two changes are independent, and whichever merges second needs a small rebase of
SpirvLdsAtomicLock.cpp.Tested
Windows 11 (26100), Ryzen 5 7600, WinLibs GCC 15.2.0 r7, Release,
mainbf2053816plus this commit.agc_driver_lds_mixed_width_atomics, mainlane 0 read 0xdeadbeefdeadbeef--repeat until-fail:20agc_spirv_lds_atomic_lockctest -j 8 --timeout 300agc_driver_layered_volume_vertex_no_gpl, as on main (#5804)agc_driver_f16_fma_denormal_modesandagc_driver_fma_mix_f16_denormal_modes(below)inf * 0(0x7ffffor0xfe00,0x7ffffffffor0xffc00000); the shaders use no LDS, and the f16 one fails the same way on unchanged main (reported on #7034).guest_shader_linkno longer hangs at exit after #6986; the earlierDriver::stop()hangs (#6791) are gone from both full runs.python tools/check_conventions.py --base upstream/mainandgit diff --checkpass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T00:37:09Z.
Some 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/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppImported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-17T11:27:23Z.
RDNA 2 discrete run, since the PR lists only the iGPU. Windows 11 26200, AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, WinLibs GCC 15.2.0 r7, Release,
ANYPS5_REQUIRE_VULKAN=1.main20a84154f:agc_driver_lds_mixed_width_atomicsfails after 2.97 s:lane 0 read 0xdeadbeefdeadbeef, expected 0x00001ffffffff000 after 4096 ds_add_u64 and 4096 ds_add_u32 on one slot(it fails in every full run on this PC).main+ this PR (merges cleanly):agc_driver_lds_mixed_width_atomicsandagc_spirv_lds_atomic_lockpass with--repeat until-fail:20, 0.26 s per run.ctest --parallel 4 --timeout 120with this PR: 1379 tests; the LDS test passes;agc_driver_layered_volume_vertex_no_gplfails as onmain(#4768); four Python-driven tests time out, which happens on this PC only (slow Python startup).AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by CrimsonMac on 2026-10-17T11:36:15Z.
Thank you for the test! It covers the gap in my testing and is valuable information.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T01:43:41Z.
A new report, #7344, gives a second AMD RDNA 2 result:
agc_driver_lds_lock_budgetreturns exactly half the expected sum in its device-backed LDS case on unchanged main. Your branch already reports repeated passes foragc_driver_lds_mixed_width_atomics, so this may share the lock/fence issue, but the report identifies a distinct lock-budget case. Could you or the reporter runagc_driver_lds_lock_budgeton this head? That would tell us whether #6976 covers it or whether #7344 needs a separate fix.Imported from boykopovar/AnyPS5. Originally posted by CrimsonMac on 2026-10-18T01:54:13Z.
I ran it on this head. #6976 does not cover #7344.
Windows 11, AMD Radeon(TM) Graphics iGPU (RDNA 2, 1002:164e), WinLibs GCC 15.2.0 r7, Release,
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,APS5_NO_PIPELINE_JOURNAL=1. This PR merged cleanly intomain0bb238b4a, because the lock-budget case isn't on the PR's base.main0bb238b4a:agc_driver_lds_lock_budgetfails 3/3 in the device-backed case (workgroup=0; device=8193). Lane 0 reads0x00000ffffffff800, expected0x00001ffffffff000, the same value as #7344.main+ this PR:agc_driver_lds_lock_budgetstill fails every run with the same value.agc_driver_lds_mixed_width_atomicsandagc_spirv_lds_atomic_lockpass with--repeat until-fail:20.agc_driver_lds_lock_budgetpasses 20/20, with both the limit and limit-4 cases verified.So #7344 reproduces on a second RDNA 2 device and needs a separate fix. I'll keep #6976 scoped to #6666. No open PR claims #7344 yet (#7351 lists it as an outstanding failure), so I'll take it next, since it reproduces here and I already have the ISA dump setup from #6976.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by tezzgod74 on 2026-10-18T02:28:23Z.
Fixes it on the iGPU from #6666, which is on a different driver build from the one in the description (32.0.21045.5002 vs 32.0.21042.62). Windows 11 Pro 10.0.26300, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
-DANYPS5_ENABLE_SPIRV_TOOLS=ON. A ismain20a84154f, B is a663dfc9a merged into it (clean merge):mainmain+ this PRagc_driver_lds_mixed_width_atomics, Ryzen 9 7950X iGPU (RDNA 2, 0x164e), 5 runs0xdeadbeefdeadbeefagc_*tests (652), iGPUagc_*tests (652), RX 9060 XTNo test fails in B that passes in A on either device. The failures common to both builds are
agc_driver_layered_volume_vertex_no_gpl(both devices) and, on the RX 9060 XT,agc_driver_division_output_modifiers,agc_driver_division_result_modifiers,agc_driver_f64_conversions,agc_driver_f64_transcendentalandagc_driver_quad_pixel_mask.A question on the dropped fences, not a reported failure: the
OpMemoryBarrierafter a 32-bit LDS atomic also kept the lane's later LDS reads from moving above that atomic. With only the lock's acquire and release left, a later read may move into the critical section. For data the lock protects that changes nothing. Is it also fine for a guest pattern that publishes through an LDS atomic and reads the published data outside the lock, or does nothing depend on that?AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by CrimsonMac on 2026-10-18T09:12:49Z.
Thanks for the run, especially on the 21045 driver build.
On the fences: no guest pattern depends on them, because the lock already gives the same ordering.
requirements.ldsLockis decided per shader, so once it's set, every LDS atomic in that shader (32-bit and 64-bit) goes throughLockedLdsUpdate; locked and unlocked atomics never mix on the same LDS. A later read can move above the release, but not above the acquire CAS. In the publish pattern, if the reader's atomic observes the publisher's, the reader's CAS read the lock value written by the publisher's release exchange (or by a later RMW on the lock). So everything the publisher wrote before its atomic happens-before the reader's acquire, and the reader's later reads, inside or outside the lock, see it. The droppedOpMemoryBarrieradded only an acquire after the lock's acquire and a release before the lock's release. A reader that polls a flag with a plainds_readhad no acquire before this change either; that pattern relies ons_barrier, which still emitsOpControlBarrierwith AcqRel on LDS.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T12:22:33Z.
Review: COMMENTED
Apple GPU run, as a non-AMD check that the exchange release and the dropped in-lock fences still give correct results. On Apple, as on other GPUs, every 64-bit LDS atomic goes through the workgroup lock: MoltenVK reports
shaderSharedInt64Atomics = false.Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Head a663dfc9a. Method: macos-port @ b7eb6b6b + ImportWatched + PR diff, not main. The diff applied cleanly.
agc_spirv_lds_atomic_lockfails witha ds_add_u64 is not serialised by one elected lane taking the workgroup lock. That is the message in the description, so the new check runs here.ldstests pass (ctest -R lds,lds_atomics64excluded) on MoltenVK 1.4.2. That includesagc_spirv_lds_atomic_lock,agc_driver_lds_mixed_width_atomics,agc_driver_lds_integer_atomics,agc_driver_lds_atomicsand both pixel LDS tests.--repeat until-fail:20onagc_driver_lds_mixed_width_atomics,agc_driver_lds_integer_atomicsandagc_driver_lds_atomics: 20/20 on MoltenVK 1.4.2 and 20/20 on MoltenVK main + SPIRV-Cross #2704. No hang and no wrong sum. The mixed-width test uses a 256-thread workgroup, which is 8 subgroups of 32 on this GPU, so the multi-wave case behind #6666 runs here.^agcsuite with the PR (SDK driver,-j1 --timeout 30, flip/display/lds_atomics64 excluded): 13 failures out of 433, the same 13 as the port baseline. No new failures.On this port baseline,
agc_driver_lds_mixed_width_atomicsalready passes without the PR, so Apple does not show the RDNA 2 hang. What this run shows is that the new lock form does not break Apple.GPUs tested: Apple M2 Max (MoltenVK 1.4.2 and MoltenVK main + SPIRV-Cross #2704)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by Amoenus on 2026-10-18T14:31:30Z.
@CrimsonMac Independent Windows validation after #7368 merged, on a different AMD iGPU (Ryzen 9 9950X3D, AMD Radeon(TM) Graphics
1002:13c0, driver32.0.21036.18), plus NVIDIA RTX 5080 (10de:2c02, driver610.62), Windows 11 Enterprise 26200, WinLibs GCC 15.2.0 r7 Release.385ed71f2cfa8d67ff466d050d5e88e136d3ec28:agc_driver_lds_mixed_width_atomicsandagc_driver_lds_lock_budgeteach fail once on AMD. The original device-backedlimitreadback passes; the remaininglimit-4case reads0xdeadbeefdeadbeef, expected0x00001ffffffff000, matching the remaining #6666 coverage documented in #7368.bbff86ec6643e2420143ea5e2b04a7d509674a74, based on that main) reproduces both failures. Merging your exact heada663dfc9ab4a302eed28059674293dc46e152fc7, without adapting its code, produces tested integration3b6498d74e. Only your four-file change separates those integration pins.agc_driver_lds_mixed_width_atomics,agc_driver_lds_lock_budget(bothlimitandlimit-4), andagc_driver_lds_beyond_device_limiteach pass 3/3 on both actual GPUs. The unrelated pipeline-header regression also passes 3/3 per GPU: 12 executions per GPU in this focused matrix. Device identity was checked per execution, and the Khronos validation layer actually loaded; zero VUIDs, validation errors or validation warnings.agc_spirv_lds_atomic_lock,agc_lds_lock_budget, and CPU graphics validation each pass 3/3 (9 CPU executions). Matching full Windows relinker/library build and conventions check pass. Each selected test finishes within 30 seconds.This supports the combination of #7368 and #6976 for the remaining AMD cases. The misaligned-address fault printed by the lock-budget fixture is its expected negative control. No Linux result, title unblock, or performance improvement is claimed by this run. Your original authorship and commit history are retained in the fork integration.
AI-assisted: yes (Codex).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T14:39:09Z.
Thanks for the expanded matrix. This adds an independent RDNA 2 discrete GPU, a second AMD iGPU, and an NVIDIA control, and it specifically checks the combined #7368/#6976 path against the two AMD failures. The result supports that tested composition on those Windows drivers; it does not add a Linux or title-run claim.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T16:22:28Z.
Rebased onto current
main(only conflict:SpirvLdsAtomicLock.cpp; kept both #6670's lane-zero init check and this PR's release/fence checks). Checked the exchange release and the fence removal against the lock's acquire/release semantics, the updatedagc_spirv_lds_atomic_lock, and the AMD/NVIDIA/Apple runs in the thread; AGC/AgcDriver ctest 827/827 after the rebase; merged.