fix(shader): order buffer and image memory at s_barrier #5117
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5117"
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 nikowolf123 on 2026-10-13T12:12:09Z.
What
Outside vertex-path and hull programs,
s_barrierbecameOpControlBarrier(Workgroup, Workgroup, AcquireRelease | WorkgroupMemory): semantics0x108, or0x148when LDS lives in device memory. A SPIR-V barrier orders only the storage classes its semantics name. The SPIR-V specification, 3.2.24 Memory Semantics, says: "Storage classes not selected are not being constrained."UniformMemory(0x40) coversStorageBufferandPhysicalStorageBuffer, andImageMemory(0x800) covers image contents. The guest's V# buffers, GPU-selected (BDA) buffers, GDS and storage images all live there. So a store that one wave made before the barrier was not ordered against a load that another wave of the work-group made after it. The Vulkan memory model (Memory Model appendix, inter-thread-happens-before) orders accesses through a release or acquire barrier only when the barrier's semantics include their storage classes. Without that, such an exchange is a data race and the load may return the old value.How the guest gets this ordering on the console:
S_BARRIERmakes each wave wait until the other waves of the work-group reach it.S_WAITCNTso that earlier memory operations have completed.buffer_gl0_inv.s_waitcnt lgkmcnt(0) & vmcnt(0) & vscnt(0)for release, and the same followed bybuffer_gl0_invfor acquire.vmcnt(0)/vscnt(0)and thebuffer_gl0_inv. The release keeps its waits in both modes.s_waitcnt,s_waitcnt_vscntandbuffer_gl0_invto nothing (EmitWaitcntandEmitControlNopinSpirvFlowEmitter.cpp). It also does not know the CU or WGP mode. So the translated barrier is the only place left to carry the work-group release and acquire.On RADV this has an effect. In Mesa ACO,
setup_barrierandfinish_barrier_internal(src/amd/compiler/aco_insert_waitcnt.cppat Mesa9b9e14ed5c99) make a workgroup barrier wait only for outstanding accesses in the storage classes named in its semantics, and only in WGP mode. WithWorkgroupMemoryalone, the barrier does not wait for a pending buffer store. RADV compiles a compute program in WGP mode on gfx10 and later only when it uses no LDS (radv_should_use_wgp_modeinsrc/amd/vulkan/radv_shader.c, same commit); a program with LDS runs in CU mode, where ACO drops these waits at a workgroup barrier.Change:
EmitBarrieraddsUniformMemory | ImageMemory. The semantics become0x948(AcquireRelease | UniformMemory | WorkgroupMemory | ImageMemory), still with Workgroup execution and memory scope.OpMemoryBarrierafter atomics.vmcnt(0)/vscnt(0)wait.s_waitcntreaches the IR as an operand-lessWaitcnt, so the barrier cannot tell these cases apart. Those barriers get an ordering the console does not guarantee. That is allowed, but it may cost host waits (below).lgkmcnt. On RADV this concerns only programs without LDS, the ones it runs in WGP mode.Still open, recorded in
docs/dev/debt/shader.mdnext to the vertex-paths_barrierentry:CoherentnorVolatilein the SPIR-V:buffersarray when it is notCoherent;buffer_gl0_invand has no GLC or DLC access. Making those accesses coherent is a separate change with its own cost (#2369 measured whatCoherentcosts on NVIDIA).Test:
agc_driver_barrier_global_memory(tests/execution/BarrierGlobalMemory.cpp).0xdeadbeef.llvm-mc21,-mcpu=gfx1030:tidof groupgmust readg * 512 + 127 - tid, the value that thread127 - tidstored.glc=1loads.Coherentbuffer view.OpControlBarriermust have Workgroup execution and memory scope, and semantics that contain0x948.Tested
Not measured on AMD hardware. Everything below ran on lavapipe, except the last run in the list, on an NVIDIA RTX 5080. Reviewers with a gfx103x GPU, please confirm:
tools/hw-oracle, in work-groups of 128 lanes, wave32 and wave64, the hardware reads127 - tid.agc_driver_barrier_global_memorypasses on RADV. If you can, also run it with the change reverted. The test program declares no workgroup memory, so RADV runs it in WGP mode, where that build may read0xdeadbeef; on lavapipe it does not.Setup:
mainatc8eb1f52d.cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DANYPS5_ENABLE_SPIRV_TOOLS=ON, thencmake --build build -j6.VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.json ANYPS5_REQUIRE_VULKAN=1 GALLIUM_OVERRIDE_CPU_CAPS=avx.ANYPS5_ENABLE_SPIRV_TOOLS=ON, SPIRV-Tools validates every recompiled module for the target's Vulkan environment, including the new barrier semantics.Results:
mainplus the test only (ctest -R agc_driver_barrier_global_memory --output-on-failure): fails withwave32 s_barrier semantics 0x00000108 do not order buffer, workgroup and image memory with acquire-release (0x00000948). It gets there after the 4 runtime rounds have passed.OpControlBarrierfromEmitBarrier. The runtime part then fails on lavapipe withwave32 round 0 group 0 thread 0 read 0xdeadbeef after s_barrier, expected 0x0000007f. So the runtime exchange does depend on the barrier.ctest -R 'agc_shader|agc_driver' -j6 --timeout 120:main: 377 tests, 341 passed, 36 skipped;ctest -j6 --timeout 120on this branch: 1044 tests, 0 failed, 43 skipped.main14036cb0e(the branch now sits on0f2683cb7with the same three-file change, no conflicts). Native Windows 11, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 5080 (driver 616.92): the new test passes 3 of 3 runs, andctest -R 'agc_shader|agc_driver|shader|recompiler'passes 383 of 383.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 CnRJay on 2026-10-14T01:01:37Z.
Review: COMMENTED
Careful write-up, and I agree with the reading of the memory model.
OpControlBarrierwith onlyWorkgroupMemorydoesn't orderStorageBuffer,PhysicalStorageBufferor image accesses between invocations. Sinces_waitcnt/buffer_gl0_invtranslate to nothing, the barrier is the only place left to carry the release/acquire. The ACO detail (a workgroup barrier waits only for the storage classes it names) explains why this shows up on RADV first.On the cost, which the description leaves unmeasured: this applies to every
s_barrierin compute and pixel-path programs, including the many that only exchange through LDS (reductions, prefix sums, tiled blurs), where the guest's owns_waitcntcovers onlylgkmcnt. On those, the barrier now also waits for every outstanding VMEM load and store of the wave, including loads issued early to hide latency, which is a common pattern before an LDS barrier. In a tight LDS loop that can remove most of the memory/ALU overlap the shader was scheduled for. Other vendors may do the same, which is why I would measure it on more than RADV.Two suggestions:
APS5_PROFILE_GPU[gputime]by program) for the top programs before and after, would show whether the cost is real.ResourcePlan/ binding usage). A program whose only stores are LDS stores gains nothing fromUniformMemory | ImageMemoryat its barriers, because there is nothing across invocations for them to order. That keeps the fix exact for programs that exchange through memory and costs nothing for the LDS-only ones.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-14T01:17:30Z.
Intel run for this PR: Intel UHD Graphics 770 (Raptor Lake iGPU of an i9-13900K), driver 32.0.101.6129, Windows 11, MinGW-w64 GCC 15.2.0 Release, device picked with
ANYPS5_GPU=Intel, at head41aa1c926.agc_driver_barrier_global_memorypasses.ctest -R "agc_driver|agc_masked|agc_shader" -j4: no failures out of 380 (352 pass, 28 skipped). These are the same 28 skips asmainhas on this GPU (f64, 64-bit and float atomics, fp16 interpolation and dot, mesh, shader clock and the like, which it does not support), and no new ones.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:33:13Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed