fix(shader): fence the LDS lock's critical section on MoltenVK #12188
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12188"
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 Gonic0 on 2026-10-28T02:38:22Z.
What
On MoltenVK, contended 64-bit LDS atomics lose updates.
agc_driver_lds_atomics64,agc_driver_lds_mixed_width_atomicsandagc_driver_lds_lock_budgetfail onmainon an Apple M5 Pro: "lane 0 contended ds_add_rtn_u64 total is 0xc6ef372fe94f82a0, expected 0x8564cc6e78ec4100", and "lane 0 read 0x0000100ffffffff0, expected 0x00001ffffffff000 after 4096 ds_add_u64 and 4096 ds_add_u32 on one slot".LockedLdsUpdatetakes the workgroup lock with an acquireOpAtomicCompareExchangeand releases it with a releaseOpAtomicExchange. Between the two, the 64-bit update is plain loads and stores of the two dwords. In the MoltenVK shader dump, SPIRV-Cross emits both lock atomics asmemory_order_relaxedwith no fence, so nothing orders those loads and stores against the lock:With this change the driver sets the new
SpirvTarget::ldsLockFenceson MoltenVK only (LdsLockFencesRequiredinSubgroupBarrier.hpp). The lock then emits a workgroup-scope acquireOpMemoryBarrierat the start of its critical section and a release one before the exchange. SPIRV-Cross turns each into a threadgroup fence with no execution barrier, inside the elected lane's branch:The two fences are emitted outside the
insideLdsLockwindow, so the existing suppression of fences inside the lock does not remove them. Other drivers emit the same SPIR-V as before: #6976 removed fences from inside the lock because AMD's Windows driver compiles a workgroup-scopeOpMemoryBarrierthere tos_barrier, which hangs the waves that spin on the lock. The flag is part of the cache key,agc_shader_replaytakes--lds-lock-fences, and the new debt entry "LDS lock ordering on MoltenVK" indocs/dev/debt/shader.mdrecords the workaround.A release
OpAtomicStorein place of the exchange also makes the tests pass, but its MSL is still a relaxedatomic_store_explicitwith no fence, so it only works because of how Metal compiles it today.Tests:
CheckLockFencesinSpirvLdsAtomicLock.cpprecompilesds_add_u64alone andds_add_u32together withds_add_u64withldsLockFencesset. In every critical section it requires exactly two workgroup-scope barriers on workgroup memory: an acquire before the first LDS access and a release after the last one. It also checks that the 32-bitOpAtomicIAddstays inside the lock, and that a program with onlyds_add_u32still gets no lock. The existing checks still require no barrier inside the lock when the flag is unset.verifyLdsLockFenceDriversinSubgroupBarrier.cppchecks that onlyVK_DRIVER_ID_MOLTENVKneeds the fences.ShaderDiskCache.cppchecks that the flag changes the cache key.Overlap:
tools/pr_overlap.pyreports line conflicts only where other PRs add a line to the same list: #10547 (CacheKey.hpp), #10652, #11265 and #11483 (theagc_shader_replayflags). None of them changes the LDS lock. #10858 also editsSubgroupBarrier.hppand its test; the hunks are in other functions, andgit merge-treemerges the two branches without conflicts.Tested
Apple M5 Pro, macOS 27.2, Apple clang 21.0.0, MoltenVK from Vulkan SDK 1.4.363, x86-64 Release under Rosetta 2, with
ANYPS5_NO_SHADER_CACHE=1.mainalone does not build libkernel or the driver on macOS. The tree ismaincccdaa2734plus only the open PRs it needs to build: #10552, #11447, #10669, #11554, #11437, #7631, #7804, andException.cppfrom #4630. It also includes #11705: without it, every execution test that submits throughAgcDriver::Submitstops on macOS at "deferred destruction found two different device resolvers", with or without this change. #7631, #7804 and #4630 conflict withmain; I resolved them locally. This commit is on top. Rebased onto3850cc86f5, which changes none of the files touched here.The 26 tests matching
lds|subgroup_barrier|shader_disk_cacheinctest, 3 runs each:agc_driver_lds_atomics64,agc_driver_lds_mixed_width_atomics,agc_driver_lds_lock_budget,agc_driver_lds_subword_store_branch,agc_driver_lds_tree_reductionagc_driver_lds_subword_store_branch,agc_driver_lds_tree_reductionThe two remaining failures are the MoltenVK wave LDS ordering problem that #10858 addresses, and are the same with and without this commit.
Mutations of this commit, each failing the test that covers it:
agc_spirv_lds_atomic_lock: "ds_add_u64 with LDS lock fences emitted 0 memory barriers inside the workgroup lock, expected 2"agc_spirv_lds_atomic_lock: "ds_add_u64 with LDS lock fences accesses LDS outside the fences inside the workgroup lock"agc_shader_disk_cacheLdsLockFencesRequiredfalse for MoltenVKagc_subgroup_barrier: "driver 14: expected LDS lock fences only on MoltenVK"The GPU tests do not tell the two fences apart: in an earlier run with acquire-release semantics on both, each fence alone also passed them. The commit keeps both, because the lock needs an acquire after taking it and a release before giving it back.
In the MoltenVK dumps of the two GPU tests, each of the 5 lock shaders has exactly two of these fences per compare-exchange, and no
threadgroup_barrierbetween them. The 2 lock shaders that the run without this commit also reaches have the same number ofthreadgroup_barriers in both runs.The same 26 tests with
VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1(ctest -V): 21 pass without this commit and 24 with it, the same failures as above. Neither log reports aVUID-error or "was not found but was requested by env var VK_INSTANCE_LAYERS".Not tested on Linux or Windows. There the flag stays unset, and the SPIR-V is the same as on
main.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T03:13:40Z.
Review: APPROVED
I reviewed exact head
2f25ab919feee34f8663c50369b56071478fa5b7. The workaround is scoped to MoltenVK, where the report demonstrates the relaxed Metal lock atomics do not order plain LDS accesses. The added barriers are memory-only (no workgroup execution rendezvous), bracket the elected lane’s locked update, and are enabled only after lock acquisition. The target flag is propagated into emission and the shader cache key; tests cover fence scope/order, the mixed 32/64-bit lock path, no-lock programs, driver selection, and cache separation. The reported M5 Pro A/B runs reproduce the lost updates on main and pass the focused 26-test group on the change across three runs; mutation checks exercise each policy/configuration guard. I verified the current-main merge preview is clean, conventions pass, andgit diff --checkis clean. Hosted policy checks, relinker matrix including ASan, and Windows build pass; Linux full build and specialized platform jobs are skipped by workflow filters. I did not independently reproduce the Apple hardware run, but the detailed controlled evidence and scoped implementation support approval.