fix(shader): widen LDS atomic fences to device scope on AMD's proprietary driver #8489
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8489"
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 wachi35676 on 2026-10-20T13:41:29Z.
Builds on #8134, which has merged; this branch is now a single commit on
main.What
The recompiler puts a workgroup-scope
OpMemoryBarrier(Workgroup, AcquireRelease | WorkgroupMemory)after three kinds of LDS update:ds_write_b8/ds_write_b16);AMD's proprietary Windows driver compiles that memory barrier to a real
s_barrier. I read the driver's code objects out of its pipeline cache to see this; capturingVK_KHR_pipeline_executable_propertiesinternal representations fails with -13 for this pipeline.These updates usually sit in branches that only some waves of a workgroup take. Those waves then execute extra
s_barriers, which pair with the other waves' real barriers, so the other waves read LDS before it is written.An
OpMemoryBarrieris not an execution barrier, and SPIR-V allows it in non-uniform control flow. The SPIR-V is valid, so this is a driver bug. On the same driver, a device-scopeOpMemoryBarriercompiles tobuffer_gl0_inv+buffer_gl1_invwith nos_barrier.PS5LM Plus (PPSA99581, llama.cpp's Vulkan backend) drifted into different garbage on every run on that driver with flash attention on. Its flash-attention shader stores two halfwords to LDS inside the KV loop, in a branch that waves 0-1 of its four take. Results from replaying that dispatch with identical inputs:
s_barrierin the driver's code (guest has 21)The guest's 21
s_barriers map to 21OpControlBarriers, which the driver compiles to ones_barriereach, none moved or duplicated. The 2 extra come from the two halfword stores.Changes:
WorkgroupScopeFenceUsable(VkDriverId)inSubgroupBarrier.hppis false only forVK_DRIVER_ID_AMD_PROPRIETARY. NVIDIA, RADV, AMDVLK, Intel, llvmpipe and MoltenVK keep the workgroup scope. AMDVLK is excluded only because it was not tested.SpirvTarget::deviceScopeLdsMemoryBarriers, which is part of the shader cache key.LdsMemoryBarrierScope) is device scope, with the same semantics. Device scope includes the workgroup, so it orders everything the old fence did. Modules use the GLSL450 memory model, and device-scope fences are already emitted for buffer atomics.SharedAtomicUpdate(ds_min/max_f32,ds_cmpst_f32and the other read-modify-write LDS operations) emitted a second fence identical to the oneAtomicUpdatealready emits. The duplicate is dropped, so each update has one fence on every driver.agc_shader_replay --device-scope-lds-memory-barriersreplays a request the way that driver compiles it. Like--wave-lds-memory-barriers, the flag is not serialized.docs/dev/debt/shader.md.The fences inside the LDS lock path are untouched, because that path emits none. On that driver a
ds_add_u64program (the lock path) had nos_barrierbesides its uniform lock-initialisation barrier, and an LDS exchange after it in a branch passed.Not covered:
Tested
Windows 11, WinLibs GCC 15.2, full Release build of 98f4019e6 (on #8134 473497cbf, main
3aa35dc01; the branch also merges cleanly with main44e3389c8).ctest -R "agc|shader|spirv|recomp"ran withANYPS5_REQUIRE_VULKAN=1and-j4; failures were rerun alone. The baseline is #8134's head 473497cbf.agc_driver_msaa_dcc_clear_4andagc_driver_pass_input_hazard. The baseline has the same 2 of 826.agc_trimmed_import,agc_driver_msaa_dcc_clear_4,agc_driver_draw_cache_reuse_offandagc_driver_stencil_replay_no_gplalso fail on the baseline.agc_driver_layered_volume_vertex_no_gplfails with a device fault reading0xbeefdeadc000, a fault the debt file already lists for this driver. The family's failures depend on test order: on the baseline the same fault hitsagc_driver_layered_volume_drawandagc_driver_layered_volume_vertexinstead (baseline: 8 of 826 failed).Linux (WSL Ubuntu 24.04, GCC 13.3, llvmpipe): full build with
-DBUILD_TESTING=ON, the same ctest set, 817 tests. The same 17 tests fail as on #8134; they need watched guest memory or llvmpipe features.agc_subgroup_barrierandagc_shader_disk_cachepass, andagc_driver_lds_subword_store_branchskips (no 32-lane subgroups).New and changed tests:
agc_driver_lds_subword_store_branchruns four wave32 waves. Waves 0-1 store a halfword to LDS in a branch and then spin for 4096 iterations. After that, every thread writes an LDS slot, passes ones_barrierand reads the slot of threadtid ^ 64. The test runs 16 rounds.round 0: thread 64 (wave 2) read 868449278, expected 65536.agc_subgroup_barrieralso checks the new quirk, and thatds_write_b16,ds_add_u32andds_max_f32each emit exactly one fence of the expected scope with the flag off and on. It fails without the change.agc_shader_disk_cachechecks that the new flag changes the cache key.PS5LM Plus v0.8.4 was run with
qwen2.5-0.5b-instruct-q4_k_m.gguf,-fa on -ngl 999, the prompt "The capital of France is" and temperature 0, on the AMD iGPU with a warmed shader cache per configuration. Flash attention also needs #7410: without it, NVIDIA too answers garbage with-fa on. So these runs use a local integration branch, this PR (#8134 + this commit) merged with #7410 (a80f17d7c). The "before" rows are the same build with the new flag forced off." Paris. It is not a city and not to\",__ F 12G"" Paris. It is the largest city in Europe and the second largest in the world"" Paris. It is in France, in France and or in France, in France"The correct text matches NVIDIA and the CPU. The first cold q8_0 run of the final commit hit the guest's own 10 s fence timeout while shaders compiled. The previous revision of this commit, with the duplicate fence still in place, gave the same correct text on 5 q8_0 and 4 f16 runs.
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
https://claude.ai/code/session_01BM2kfDbr6MydxsfTckZJDR
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T23:07:00Z.
Independent Windows run of this PR on an RDNA 2 card (data only).
Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, AMD Radeon RX 6400 (AMD driver 32.0.21033.3005; the log shows
Selected GPU name=AMD Radeon RX 6400 vendor=0x1002 device=0x743f subgroup=64, picked withANYPS5_GPU="RX 6400"), WinLibs GCC 15.2.0 (MinGW-w64 UCRT), Release. PR headc1528a6e18; merge-tree againstmainis clean. I builtagc_driver_lds_subword_store_branch_tests,agc_driver_lds_tree_reduction_testsandagc_subgroup_barrier_tests.Results (3 runs each):
agc_driver_lds_subword_store_branch,agc_driver_lds_tree_reductionandagc_subgroup_barrierpass 3/3.WorkgroupScopeFenceUsableinSubgroupBarrier.hpptoreturn true;, so AMD's proprietary driver keeps the workgroup-scope fence again, then rebuilt.agc_driver_lds_subword_store_branchfails 3/3 on this card withthread 64 (wave 2) read <stale value>, expected 65536(the stale value was 1135280128 in two runs and 1065353216 in one);agc_subgroup_barrierfails 3/3 (driver 1: expected workgroup-scope fences to be unusable, a check of the quirk itself);agc_driver_lds_tree_reductionpasses 3/3 in the baseline too, so it does not distinguish the two states on this card. I restored the PR's header afterwards.So the stale LDS read the PR describes reproduces on an RX 6400 without the device-scope fence and is gone with it. I did not inspect the driver's code objects, so I can not confirm the
s_barrierexplanation, and my driver version differs from the one in the PR text.Not run: PS5LM Plus or any title, the replay comparison against NVIDIA, other GPUs and platforms, the full repository CTest.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-21T12:14:45Z.
Rebased onto
main3e91c8edfnow that #8134 has merged, so this is a single commit:f1a210313. The conflicts were inCacheKey.hpp,Recompiler.hppandSpirvEmitter.cpp, wheremainaddedfloat32DenormPreservebeside this PR'sdeviceScopeLdsMemoryBarriers; both are kept. The code is otherwise unchanged.Windows 11, WinLibs GCC 15.2.0,
agc|shader|spirv|recomp|subgroupsubset:agc_driver_image_array_window_past_layer_limit, which doesn't compile with MinGW onmain(#8310 fixes it);agc_driver_sample_order*tests ("the slopes did not leave depth both at and below the clear value"), which this PR doesn't touch on NVIDIA. They fail the same way in amain+ #7410 build.ANYPS5_GPU=AMD): the same four, plusagc_driver_tessellation. That one is the merged hull failure on this driver that I reported on #6679; it also fails without this PR.agc_driver_lds_subword_store_branch,agc_driver_lds_tree_reductionandagc_subgroup_barrierpass.Thanks @xXPBSXx for the RX 6400 run.
AI-assisted: yes (Claude Code); results above were run on this machine.