fix(shader): update per-invocation LDS without atomics #5160
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5160"
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-13T13:12:38Z.
What
Pixel, vertex-path and fetch programs keep LDS in an array in
Functionstorage that belongs to one invocation (functionLdsinSpirvAnalysis.cpp; only compute and mesh programs getWorkgroupmemory). Several LDS operations still used atomics on that array:ds_write_b8,ds_write_b16,ds_write_b8_d16_hiandds_write_b16_d16_hi(StoreSubwordInBounds) merged the byte or half into its dword withAtomicUpdate: anOpAtomicLoad, anOpAtomicCompareExchangeloop withWorkgroupscope and anOpMemoryBarrierwithWorkgroupscope. Only scratch took a plain load and store.Atomic32(ds_add_u32,ds_sub_u32, the integer min/max,ds_and/or/xor_b32,ds_wrxchg_rtn_b32) emitted the nativeOpAtomic*and aWorkgroupbarrier.EmitAtomicUpdate(ds_inc_u32,ds_dec_u32) andSharedAtomicUpdate(ds_rsub_u32,ds_cmpst_b32,ds_cmpst_f32,ds_mskor_b32,ds_min/max_f32,ds_add_f32,ds_wrap_rtn_b32) emitted theAtomicUpdateloop.ds_appendandds_consumeon LDS (AppendConsume) emittedOpAtomicIAddorOpAtomicISub.Vulkan forbids both parts:
Uniform,Workgroup,Image,StorageBuffer,PhysicalStorageBufferorTaskPayloadWorkgroupEXTstorage.Functionis not in the list.Workgroupmemory scope is only for the task, mesh, tessellation control and compute execution models.SPIRV-Tools v2025.4, the submodule this repository builds, checks them in validate_atomics.cpp#L241-L262 and validate_scopes.cpp#L275-L295. With
ANYPS5_ENABLE_SPIRV_TOOLS=ON, as in Linux CI,ValidateAndOptimizeSpirvrejected every such shader:SPIR-V validation before optimization failed: [VUID-StandaloneSpirv-None-04686] AtomicLoad: Vulkan spec only allows storage classes for atomic to be: Uniform, Workgroup, Image, StorageBuffer, PhysicalStorageBuffer or TaskPayloadWorkgroupEXT.With the option off, which is the default, the invalid module went to the driver.The instructions do not need atomics in this model. In the RDNA 2 ISA guide (30 November 2020), section 12.13,
DS_WRITE_B8andDS_WRITE_B16(opcodes 30 and 31, p. 193) storeDATA[7:0]andDATA[15:0], andDS_WRITE_B8_D16_HIandDS_WRITE_B16_D16_HI(160 and 161, p. 204) storeDATA[23:16]andDATA[31:16]. The atomics read one dword, write the new value and return the old one. Here the array belongs to one invocation, so nothing else can touch the dword between the load and the store.InFunctionStorage(state, kind)(SpirvBufferAccess.cpp) is true for scratch, and for LDS that is neither in device memory nor in a compute or mesh program.EmitMemoryElementPointernow picks the storage class with it, so the two cannot disagree.StoreSubwordInBounds,Atomic32,EmitAtomicUpdate,SharedAtomicUpdateandAppendConsumeload the dword, compute the new value and store it, with no atomic and no barrier, asStoreSubwordInBoundsandApertureAtomic32already do for scratch. They return the same old value as before.Atomic32takes the new value fromAtomicNext32, keyed on the SPIR-V atomic opcode.FlatAtomicNext32now calls it for its eleven plain integer operations instead of repeating them.docs/dev/debt/shader.mdgets an entry for the per-invocation model, which had none and which this change keeps. On the console one LDS region serves the whole wave (RDNA 2 ISA section 3.6.6, p. 18, and section 10.4, p. 96), so here a lane does not see another lane's LDS writes, andds_append/ds_consumeonly update the first active lane's copy. The entry also says that none of this is measured on hardware.No title is known to use these instructions in pixel, vertex or fetch programs.
Tested
Ubuntu 26.04 under WSL2, GCC 15.2.0, CMake 4.2.3, Ninja 1.13.2, configured like CI (Release,
-DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON, ccache), lavapipe from Mesa 26.0.8 (8-lane subgroups) withVK_ICD_FILENAMES=<lvp_icd json> ANYPS5_REQUIRE_VULKAN=1 GALLIUM_OVERRIDE_CPU_CAPS=avx, onmainff8fe4dbc:agc_shader_memory, newverifyFunctionLdsUpdates: recompiles pixel and vertex programs, as wave64 and wave32, each with one ofds_write_b8,ds_write_b16,ds_write_b8_d16_hi,ds_write_b16_d16_hi,ds_add_u32,ds_inc_rtn_u32,ds_mskor_b32andds_append. It fails if an atomic takes aFunctionpointer, or if an atomic orOpMemoryBarrierusesWorkgroupmemory scope. Withmain's recompiler sources (git checkout ff8fe4dbc --on the four changed recompiler files) all 32 recompiles throw the VUID 04686 error above (AtomicLoad, orAtomicIAddfords_add_u32andds_append). In a local probe with SPIR-V validation switched off, the scan finds 2 to 6 such instructions in each of the 32 modules, so the test also fails onmainin builds without SPIR-V tools. With the fix all 32 compile and the scan finds none.agc_driver_pixel_lds_read_modify_write(new,tests/execution/PixelLdsReadModifyWrite.cpp): draws a 64x8 target as wave64 and wave32, two passes each. Each pixel uses its own 16-byte LDS slot at(pixel index & 511) * 16and fills it with~pixel. It writes bytes and halves into it with all four sub-dword stores, leaving the other bytes of each dword in place. It then chainsds_add_rtn_u32,ds_inc_rtn_u32andds_mskor_rtn_b32on one dword, and readsds_appendfrom a counter that every lane set to 0x1234. The four dwords and the four returned values are stored and compared, 4096 per draw, with values computed from the ISA descriptions. Withmain's recompiler sources the pixel shader fails to recompile (VUID 04686). With the fix all values match, in 0.2 s. The test needs no subgroup wider than the host's, so it runs on lavapipe, whereagc_driver_pixel_ldsandagc_driver_pixel_lds_lane_slotsskip for lack of a 32-lane subgroup. Storing the unmerged value in the newFunctionbranch ofStoreSubwordInBoundsmakes it fail at pixel 0.src2forms,ds_condxchg32_rtn_b64,ds_append,ds_consume, the four sub-dword stores and five 64-bit LDS atomics, in pixel and vertex programs, wave64 and wave32. Onmain48 of them fail validation with VUID 04686; the 64-bit atomics already used plain loads and stores. With the fix all 53 compile and the scan finds nothing.llvm-mc21.1.8 for gfx1013 and gfx1030. The one difference is the 64 inv_mad_u32_u24: a literal in the test, an inline constant inllvm-mc's output.ctest -R 'agc_shader|agc_driver|shader|recompiler': 384 tests, 348 pass and 36 skip.mainhas 383 of them, with the same results, and lacks only the new test.ctest: 1050 tests, 1007 pass and 43 skip. Per test this is the same asmain(1049 tests, 1006 pass, the same 43 skips) plus the new test.main'stools/check_conventions.py --base origin/mainwith this title and body passes.Windows: not built. Not measured on AMD hardware, neither gfx1013 nor any PC RDNA 2 GPU.
tools/hw-oracleruns compute kernels only, so it cannot run the pixel stage (HW_ORACLE.md). The values the GPU test expects do not depend on the stage, though. A reviewer with a gfx103x GPU can check them with the same instructions on one 16-byte slot per lane. Save this asbody.sin the repository root:Then run this from the same directory. It runs one wave64 of 64 rows and compares every row with the values
PixelLdsReadModifyWrite.cppexpects:body.sassembles with the oracle's template for gfx1030 wave64. The script printed64 rows matchagainst a stand-in forrunthat models the LDS byte by byte, not against hardware.Native Windows 11, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 5080 (driver 616.92), on this branch rebased onto
main0f2683cb7(16f63d8ed):agc_driver_pixel_lds_read_modify_writeandagc_shader_memorypass 3 of 3 runs, andctest -R 'agc_shader|agc_driver|shader|recompiler'passes 383 of 383.Run by @kr4y4 on Windows 11 with the Windows CI configuration (SPIR-V tools off), on an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790), head
16f63d8edmerged intomain3b94f923a(comment):agc_driver_pixel_lds_read_modify_writeandagc_shader_memorypass 3 of 3 runs on both GPUs, and the full suite passes 1087 of 1087 on both.main, the pixel test crashes inside NVIDIA's driver (0xc0000005innvoglv64.dll) and stores wrong values on Intel. Without SPIR-V tools nothing reports VUID 04686 first, so on Windows the invalid module reaches the driver.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 github-actions[bot] on 2026-10-13T14:08:44Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/debt/shader.mdL102Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T16:28:51Z.
First Windows build and hardware run. With the fix both GPUs pass. Without it,
main's pixel shader crashes NVIDIA's driver and gives wrong values on Intel. This build has no SPIR-V tools, so nothing reports VUID 04686 first.Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, Windows CI configure flags (
ANYPS5_ENABLE_SPIRV_TOOLSoff), an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790). The head16f63d8edmerged intomain3b94f923a.agc_driver_pixel_lds_read_modify_write,agc_shader_memoryand the shader tests next to it pass 3 of 3 runs on both GPUs. The full suite passes 1087 of 1087 on both (7 skipped on NVIDIA, 34 on Intel).main:agc_shader_memoryfails on both withfunction LDS: ds_write_b8 in a pixel wave64 program uses an atomic on a Function pointer or a Workgroup memory scope.agc_driver_pixel_lds_read_modify_writecrashes after printingpixel LDS read-modify-write wave64, with0xc0000005atnvoglv64.dll+0x17f5ad(a read of 0x8).pixel 0 ds_write_b8 dword expected 4278255360 got 4294967295. The dword still holds the~pixelfill (0xffffffff, expected 0xff00ff00).Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T16:35:01Z.
Thank you. The NVIDIA crash and the wrong Intel values without the fix are the hardware consequence this PR was missing: with SPIR-V tools off, as in the Windows CI configuration, the invalid module goes straight to the driver. I added your run to the description.
Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-14T07:13:47Z.
Closing this following the request on #4926 to hold off on pull requests that do not name a title needing the change while the queue is this long. The branch stays; if a title stops on this on current
main, I will reopen with the title ID and the log line.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T09:15:50Z.
Reopened, like #4926 and #5028: atomics on per-invocation
FunctionLDS in pixel and vertex programs aren't valid Vulkan, so this is worth having without a title. It still merges cleanly on currentmainand its tests pass here; running the full build now. @adriadam10 heads-up since it's incore/shader/.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T09:43:14Z.
The Linux timeout came from
ds_append/ds_consume:FunctionLdsDwordsgave up on their M0 operand, so the per-pixel LDS array was declared at the full 32 KiB, and llvmpipe on CI needs over 5 s to compile that during the draw. Pushed a fix that sizes it by the counter's offset (with a unit test) and a smaller test footprint; the results were right before too.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T10:09:33Z.
One more: since #4806 retained SPIR-V is a span on
main, so the new test's word check now takes a span; it builds againstmainwith SPIRV-Tools on and the shader tests pass.