perf(shader): load from buffers without Coherent when the load has no GLC or DLC #2369
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2369"
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 karanvora2599 on 2026-10-09T16:27:47Z.
What
A program with a single GLC or DLC buffer load or store (
28b09d4d) decorates the whole guestbuffersdescriptor arrayCoherent. Every other buffer access in the program is then coherent as well, including the loads the guest made without GLC or DLC. On NVIDIA the SM's L1 cache is not coherent between SMs, and the coherent loads lose it: in a compute program that re-reads a 4 KiB window, one GLC store at the end makes it about 2.7 times slower (Tested). GLC/DLC buffer accesses come fromgloballycoherentbuffers and volatile accesses, e.g. decoupled look-back scans (the radix sort behind28b09d4d) and GPU-driven work queues, and these programs usually read a lot of other buffer data with plain loads.Why the loads without GLC or DLC don't need to be coherent
RDNA does not keep them coherent either. A vector load without GLC or DLC can hit the per-CU L0 and the per-shader-array L1 caches, and neither is coherent between work-groups. The LLVM AMDGPU memory model for GFX10 (https://llvm.org/docs/AMDGPUUsage.html, "Memory Model GFX10-GFX11") describes it:
buffer_gl0_inv"is also required for coherence between wavefronts executing in different work-groups", and in WGP mode for wavefronts of the same work-group.buffer/global_load glc=1 dlc=1,s_waitcnt vmcnt(0), thenbuffer_gl1_inv; buffer_gl0_inv, which "Ensures that following loads will not see stale global data".slc=1only.So guest code that needs another work-group's data either reads it with GLC/DLC, or invalidates the caches and then reads it with plain loads. Only the second case needs the plain loads to be coherent on the host, and it is visible in the program as a cache invalidation.
Change
buffer_gl0_inv,buffer_gl1_inv,s_gl1_invands_dcache_inv. They still emit nothing.buffers_coherent, on the same binding asbuffers, decoratedCoherent. All buffer arrays of the program are decoratedAliased, asbuffersandbuffers_u64already are for 64-bit atomics.buffers. GLC/DLC loads, every store and the 32-bit atomics go throughbuffers_coherent, so the program's writes stay coherent as before.buffers_u64is unchanged and staysCoherent.buffersarray isCoherent.buffers, not coherentbuffersCoherentfor every accessbuffers; GLC/DLC loads, stores, atomics:buffers_coherent(Coherent,Aliased)buffersCoherentPlain loads in programs without GLC or DLC accesses were already incoherent; this treats plain loads in programs with GLC/DLC accesses the same way. The
Volatilememory operand on GLC/DLC accesses and the BDA path (GPU-selected V#s,SpirvBdaRead.cpp) are unchanged.Which GPUs gain
The gain comes from plain loads staying in L1. It was measured on Ada (RTX 4060) only. The CUDA C++ Programming Guide says that on compute capability 5.x and 6.x (Maxwell, Pascal) global loads of data that is not read-only are not cached in L1 by default, so those GPUs may gain less. Other vendors were not measured.
Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
ANYPS5_ENABLE_SPIRV_TOOLS=ON,mainatd70b8998. GPU tests on an NVIDIA GeForce RTX 4060 Laptop GPU (591.86, subgroup 32) and on lavapipe (Mesa 26.2.4 for Windows,GALLIUM_OVERRIDE_CPU_CAPS=avx).New check in
agc_shader_memory(no GPU needed): four wave32 compute programs that load from one guest buffer and store to another, inspected in the emitted SPIR-V:buffersarray, notCoherent, used by the load and the store;bufferswithoutCoherentandbuffers_coherentwith it, bothAliased; the load goes throughbuffersand the store throughbuffers_coherent;buffers_coherent;buffer_gl0_inv: onebuffersarray,Coherent, as onmain.Negative checks, each fails the new check:
coherent buffers: the two buffer arrays are not decorated Aliased(buffersis then unused and no longer in the module);coherent buffers: a program that invalidates the vector caches does not keep every buffer access coherent;coherent buffers: a GLC store has no coherent buffer array.Every shader test (
agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program, 234 tests):agc_driver_storage_cache_budget). This includesagc_driver_look_back_scan, a decoupled look-back scan across 4096 work-groups with GLC pair loads, atomics and plain loads and no cache invalidation, which now takes the new lowering (checked in its SPIR-V: plain loads onbuffers, the GLC loads and stores onbuffers_coherent).look_back_scanamong them); all 26 pass on the RTX 4060.Full ctest after
cmake --build buildand--target libs, serial,--timeout 120, RTX 4060: 505 of 511 pass, 2 skipped (agc_driver_storage_cache_budget,guest_video_out_wqhd_detection_no_vulkan), 4 fail. All four are relinker tests, whose build contains no recompiler sources:windows_import_modules(Windows Error Reporting keeps its GUI fixture open, #2227), andtls_function_coverage,windows_guiandwindows_icon, which fail to open an executable they just wrote to the temporary directory because Windows Defender removes it (Defender logged the detections during the run).Throughput on the RTX 4060, not committed: a wave32 compute program whose 32 lanes read 512 dwords each from a 4 KiB window of one buffer (64 batches of 8
buffer_load_dword, L1-resident after the first pass), sum them and write the result with onebuffer_store_dwordx4; 8192 work-groups, timed per dispatch including submit and wait, best of 8 dispatches. All variants run in one process, in a rotated order each round, because the laptop's clocks change by up to 4x over a few seconds and separate runs could not be compared. Abuffer_gl0_invat the start selects the previous lowering for the same program (the instruction itself emits nothing), so the last column is whatmaindoes for this program:buffer_gl0_invbuffer_gl0_inv(main)The previous lowering is the slowest variant in every round, 1.7 to 4.8 times the GLC store with this change (2.7 times in the best rounds: 3.09 ms against 1.14 ms); with the change, the GLC program runs about as fast as the program without GLC. All variants produce the same output (same checksum). An earlier in-process run with a temporary switch that dropped the
Volatilememory operand from the GLC store showed no measurable difference, with either lowering; the difference is theCoherentdecoration on the loads.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 repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:44:15Z.
Linux run: Linux 7.2 (x86_64), GCC 16.2.1, Release,
-DBUILD_TESTING=ON, #2369 (d220f48e) tested on currentmain(0fdda7be).tests/agc_shader_memory_tests: passes cleanly (Shader memory capture, strict validation and deterministic replay passed).tools/check_conventions.py: passes cleanly.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:17:19Z.
Checked on Linux (merged onto current main): build +
libSceAgc/libSceAgcDriverctest green (294 tests,agc_shader_memorypasses), and the new check fails with the non-test changes reverted. Thanks @Kikoarl for the Linux run.This changes memory behaviour, so a design call before merging:
buffer_gl0_inv/buffer_gl1_inv/s_gl1_inv/s_dcache_invstop being coherent. That follows the LLVM GFX10 memory model, but nothing here shows that a real title never reads shared data with plain loads after a GLC flag load without invalidating (the HW oracle can't measure cross-work-group coherence either).@oneandonlydean could you run
agc_driver_look_back_scanand a title on the RTX 4090 with and without this change? @boykopovar: are you ok with relaxing plain-load coherence in these programs?Before it can land:
docs/dev/TechnicalDebt.md: add a line saying that loads without GLC/DLC are not coherent in programs with GLC/DLC accesses and no cache invalidation, and that this follows the LLVM AMDGPU GFX10 memory model, not a measurement (not at the end of its section).Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T10:41:31Z.
Thanks for checking it. I merged current
main(1c75f4d2) into the branch, without conflicts, and added thedocs/dev/TechnicalDebt.mdline (5a16ed15). It sits under "Functional", right after the rawbuffer_load_dwordx2entry rather than at the end of the section:Retested at
2069fd12(Windows 11, MinGW-w64 GCC 15.2.0, Release):agc_shader_memorypasses on the RTX 4060 and on lavapipe 25.2.8 (the version CI installs);agc_driver_look_back_scanpasses on the RTX 4060 and is skipped on lavapipe.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 319 tests): the only failures are onesmainhas too.agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures_2,agc_driver_flat_apertures_4andagc_driver_pixel_interlock.agc_driver_image_plane_address_on_line_implicit_lod, and an intermittent segfault inagc_driver_depth_fast_clear_submit. Since its fill kernel stores through a buffer, I ran it 12 times each way: it crashed in 5 runs with this branch and in 7 withmain's recompiler, and never on lavapipe 26.2.4.The design question for @boykopovar and the RTX 4090 run from @oneandonlydean are still open; I'll follow whatever you decide.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:47:43Z.
Checked merged onto current main: libSceAgc/libSceAgcDriver build + ctest green (402 tests),
agc_driver_look_back_scanpasses with the new lowering, andagc_shader_memoryfails with the non-test changes reverted. Thanks @Kikoarl for the Linux run.The design question is settled on our side: plain loads in these programs follow the GFX10 memory model (acquire = GLC+DLC load plus
buffer_gl0_inv/buffer_gl1_inv), programs that invalidate keep the old lowering, and the TechnicalDebt line records that this is not measured on hardware. Merging once CI is green.