perf(shader): load bound buffers without branches under robustBufferAccess #2440
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2440"
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-09T20:24:40Z.
What
A guest buffer load through a directly bound buffer (
buffer_load_dword*,buffer_load_ubyte/ushort, ands_buffer_load_dword*from a bound buffer) was emitted as up to three nested branches per dword: one for the lane's exec bit, one for the guest V#'s bounds and one for the host buffer's length (OpArrayLength). On devices with Vulkan'srobustBufferAccess, which AnyPS5 already enables whenever the device has it, this emits the load unconditionally and selects 0 for an inactive or out-of-bounds lane instead. Results are identical. On an RTX 4060buffer_load_dworddrops from 24 to 9.5 instructions,s_buffer_load_dwordfrom 16.5 to 10 per dword, and a load-heavy kernel runs about twice as fast.Background
RDNA bounds-checks every buffer access in hardware: a load from an inactive lane does nothing, and a load outside the V#'s range (per
num_records, stride, swizzle and out-of-bounds mode) returns 0, per dword. SPIR-V has no such guarantee, soEmitRuntimeBufferLoad,readBufferandEmitRuntimeScalarBufferLoad(SpirvBackend/src/SpirvRuntimeBuffer.cpp) computed the three conditions and only issued the load insideEmitValueOrDefaultIfCondition/EmitValueOrZeroIfConditionbranches. Each branch is a selection with a merge, which costs instructions of its own on NVIDIA; without them, the compiler also keeps more loads in flight at once (in the test kernel below the register count rises and the throughput doubles).After
SpirvTarget::robustBufferAccess(new) says that the device was created withrobustBufferAccess.VulkanDevicesets it from the enabled feature; it is part of the shader cache key, of the request serialization (version 16) and of the shader device profile check.EmitRuntimeBufferLoadcomputes the descriptor and each dword's address and guest bounds as before, issuesreadBuffer, and selectsexec && inBounds ? value : 0per dword;readBufferloads the element (and, for a misaligned base, the next one) without the host-length branch, and selects 0 when the element is pastOpArrayLength;EmitRuntimeScalarBufferLoadselects 0 outside the scalar V#'s range the same way.Why the results don't change
robustBufferAccess, a load through a storage buffer descriptor outside the bound range can't fault: it returns some value from the buffer or 0 (Vulkan specification, "Robust Buffer Access"). Those are exactly the cases whose value the select discards. The descriptor array index is still the constant resource index, so the descriptor itself is always valid.buffer_load_dwordx4that straddles the end returns its in-range dwords). Each dword keeps its own condition, so that stays the same.Where it matters
Buffer and constant-buffer loads are among the most frequent instructions in title shaders: every structured or byte-address buffer read, and every constant read from a bound buffer.
robustBufferAccessis supported by NVIDIA, AMD and Intel drivers and by lavapipe, so the new path is what CI runs as well. Only an Ada GPU (sm_89) was measured.Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
ANYPS5_ENABLE_SPIRV_TOOLS=ON,mainat8b393b6e. GPU tests on an NVIDIA GeForce RTX 4060 Laptop GPU (driver 591.86, subgroup 32) and on lavapipe (Mesa 26.2.4 for Windows,GALLIUM_OVERRIDE_CPU_CAPS=avx); both reportrobustBufferAccess, so both run the new path.Correctness
agc_shader_memory(no GPU needed): a wave32 compute program with fourbuffer_load_dwordand a store, recompiled withrobustBufferAccessoff and on. With it, the module has at least four fewer selection merges and still all its loads.agc_driver_buffer_structured_bounds: a structured V# (stride 16, 4 records,OOB_SELECT0, the index-and-offset check) read withidxenloads at offsets 0 to 28 for indices 0 to 7. Offsets from 16 up and indices from 4 up must return 0 although memory is bound behind them; the rest returns the element's dword. It passes on both devices, and withmain's lowering too, which checks the expectations.exec):agc_driver_buffer_structured_boundsfails on both devices withbuffer structured bounds: lane 0 index 0 offset 16 is 0x00001004, expected 0x00000000. The existing buffer tests did not catch this (presumably because in their buffers the host length check covers the same range), which is why the new test is part of this PR;agc_shader_memoryfails on both devices withrobust buffer loads: buffer_load_dword still branches with robustBufferAccess.agc_driver_buffer_*,agc_driver_flat_load*,agc_driver_global_load*,agc_driver_tbuffer*and the related shader tests, 26 tests) pass on both devices.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program, 235 tests before the new execution test was added): all pass on the RTX 4060 (1 skipped,agc_driver_storage_cache_budget); on lavapipe 209 pass and 26 are skipped for missing device features.Instructions per op (RTX 4060, not committed)
Method: for each instruction, a wave32 compute program with 16 instances of it (independent addresses, results summed) and the same program without them, recompiled and created with
APS5_PIPELINE_STATS=1; the difference in "Binary Size" / 16 bytes per instruction, divided by 16. Register counts are of the whole test program.mainbuffer_load_dwordbuffer_load_ubyte/ushortbuffer_load_dwordx4(per instruction)buffer_load_format_xyzws_buffer_load_dword(per dword, also for x4 and x16)buffer_store_dword, atomics, LDS (unchanged)The register count rises because, without the branches, the compiler issues all 16 loads of the test before using them. Whether that costs occupancy depends on the shader; in the load-heavy kernel below it doesn't.
Throughput (RTX 4060, not committed)
Method: 32 batches of 16 independent
buffer_load_dwordspread over 128 KiB (the address moves 4 KiB per batch), each batch summed, 16384 work-groups of 32; and the same with 32 batches ofs_buffer_load_dwordx16. Each is timed per dispatch against a chain of 1024v_fma_f32dispatched right before or after it in the same process (the laptop's clocks move too much to compare separate runs), 12 rounds per run, 2 runs per build, builds alternating.v_fma_f32(median, range)mainbuffer_load_dwords_buffer_load_dwordx16The measurement programs aren't part of this PR; I can attach them if useful.
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 github-actions[bot] on 2026-10-09T20:49:10Z.
Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
Pull request closed