perf(shader): load bound buffers without branches under robustBufferAccess #2440

Closed
karanvora2599 wants to merge 1 commit from upstream-pr/2440 into main
karanvora2599 commented 2026-10-09 20:24:40 +00:00 (Migrated from github.com)

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, and s_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's robustBufferAccess, 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 4060 buffer_load_dword drops from 24 to 9.5 instructions, s_buffer_load_dword from 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, so EmitRuntimeBufferLoad, readBuffer and EmitRuntimeScalarBufferLoad (SpirvBackend/src/SpirvRuntimeBuffer.cpp) computed the three conditions and only issued the load inside EmitValueOrDefaultIfCondition / EmitValueOrZeroIfCondition branches. 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 with robustBufferAccess. VulkanDevice sets 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.
  • With it, for a buffer bound directly (not GPU-selected through BDA), not a formatted load and not the coherent 64-bit read:
    • EmitRuntimeBufferLoad computes the descriptor and each dword's address and guest bounds as before, issues readBuffer, and selects exec && inBounds ? value : 0 per dword;
    • readBuffer loads the element (and, for a misaligned base, the next one) without the host-length branch, and selects 0 when the element is past OpArrayLength;
    • EmitRuntimeScalarBufferLoad selects 0 outside the scalar V#'s range the same way.
  • Without the feature, and for stores, atomics, formatted loads, the BDA path and the coherent 64-bit read, nothing changes.

Why the results don't change

  • Every condition the branches tested is still computed, per dword, and selects the same 0 the branch's default gave. The only difference is that the load itself runs for lanes and dwords whose value is then discarded.
  • With 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.
  • An inactive lane gets 0, as it did from the branch's default.
  • RDNA zeroes out-of-bounds dwords of a wider load individually (a buffer_load_dwordx4 that 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. robustBufferAccess is 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, main at 8b393b6e. 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 report robustBufferAccess, so both run the new path.

Correctness

  • New check in agc_shader_memory (no GPU needed): a wave32 compute program with four buffer_load_dword and a store, recompiled with robustBufferAccess off and on. With it, the module has at least four fewer selection merges and still all its loads.
  • New agc_driver_buffer_structured_bounds: a structured V# (stride 16, 4 records, OOB_SELECT 0, the index-and-offset check) read with idxen loads 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 with main's lowering too, which checks the expectations.
  • Negative checks:
    • the new path without the guest bounds (only exec): agc_driver_buffer_structured_bounds fails on both devices with buffer 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;
    • the new path never taken: agc_shader_memory fails on both devices with robust buffer loads: buffer_load_dword still branches with robustBufferAccess.
  • The buffer, flat and global load tests (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.
  • Every shader test (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.

main this PR registers
buffer_load_dword 24 9.5 21 → 64
buffer_load_ubyte / ushort 25 11 21 → 65
buffer_load_dwordx4 (per instruction) 95.5 38.5 23 → 79
buffer_load_format_xyzw 24 17.5 21 → 20
s_buffer_load_dword (per dword, also for x4 and x16) 16.5 10 17 → 55
buffer_store_dword, atomics, LDS (unchanged) same same same

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_dword spread over 128 KiB (the address moves 4 KiB per batch), each batch summed, 16384 work-groups of 32; and the same with 32 batches of s_buffer_load_dwordx16. Each is timed per dispatch against a chain of 1024 v_fma_f32 dispatched 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.

time per loaded dword, relative to one v_fma_f32 (median, range) main this PR
buffer_load_dword 0.919 (0.854-1.106) 0.468 (0.423-0.714)
s_buffer_load_dwordx16 0.685 (0.602-0.759) 0.468 (0.430-0.532)

The measurement programs aren't part of this PR; I can attach them if useful.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4805483763 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2440). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-09T20:24:40Z. --- ### What A guest buffer load through a directly bound buffer (`buffer_load_dword*`, `buffer_load_ubyte/ushort`, and `s_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's `robustBufferAccess`, 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 4060 `buffer_load_dword` drops from 24 to 9.5 instructions, `s_buffer_load_dword` from 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, so `EmitRuntimeBufferLoad`, `readBuffer` and `EmitRuntimeScalarBufferLoad` (`SpirvBackend/src/SpirvRuntimeBuffer.cpp`) computed the three conditions and only issued the load inside `EmitValueOrDefaultIfCondition` / `EmitValueOrZeroIfCondition` branches. 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 with `robustBufferAccess`. `VulkanDevice` sets 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. - With it, for a buffer bound directly (not GPU-selected through BDA), not a formatted load and not the coherent 64-bit read: - `EmitRuntimeBufferLoad` computes the descriptor and each dword's address and guest bounds as before, issues `readBuffer`, and selects `exec && inBounds ? value : 0` per dword; - `readBuffer` loads the element (and, for a misaligned base, the next one) without the host-length branch, and selects 0 when the element is past `OpArrayLength`; - `EmitRuntimeScalarBufferLoad` selects 0 outside the scalar V#'s range the same way. - Without the feature, and for stores, atomics, formatted loads, the BDA path and the coherent 64-bit read, nothing changes. #### Why the results don't change - Every condition the branches tested is still computed, per dword, and selects the same 0 the branch's default gave. The only difference is that the load itself runs for lanes and dwords whose value is then discarded. - With `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. - An inactive lane gets 0, as it did from the branch's default. - RDNA zeroes out-of-bounds dwords of a wider load individually (a `buffer_load_dwordx4` that 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. `robustBufferAccess` is 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`, `main` at 8b393b6e. 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 report `robustBufferAccess`, so both run the new path. **Correctness** - New check in `agc_shader_memory` (no GPU needed): a wave32 compute program with four `buffer_load_dword` and a store, recompiled with `robustBufferAccess` off and on. With it, the module has at least four fewer selection merges and still all its loads. - New `agc_driver_buffer_structured_bounds`: a structured V# (stride 16, 4 records, `OOB_SELECT` 0, the index-and-offset check) read with `idxen` loads 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 with `main`'s lowering too, which checks the expectations. - Negative checks: - the new path without the guest bounds (only `exec`): `agc_driver_buffer_structured_bounds` fails on both devices with `buffer 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; - the new path never taken: `agc_shader_memory` fails on both devices with `robust buffer loads: buffer_load_dword still branches with robustBufferAccess`. - The buffer, flat and global load tests (`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. - Every shader test (`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. | | `main` | this PR | registers | |---|---|---|---| | `buffer_load_dword` | 24 | 9.5 | 21 → 64 | | `buffer_load_ubyte` / `ushort` | 25 | 11 | 21 → 65 | | `buffer_load_dwordx4` (per instruction) | 95.5 | 38.5 | 23 → 79 | | `buffer_load_format_xyzw` | 24 | 17.5 | 21 → 20 | | `s_buffer_load_dword` (per dword, also for x4 and x16) | 16.5 | 10 | 17 → 55 | | `buffer_store_dword`, atomics, LDS (unchanged) | same | same | same | 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_dword` spread over 128 KiB (the address moves 4 KiB per batch), each batch summed, 16384 work-groups of 32; and the same with 32 batches of `s_buffer_load_dwordx16`. Each is timed per dispatch against a chain of 1024 `v_fma_f32` dispatched 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. | time per loaded dword, relative to one `v_fma_f32` (median, range) | `main` | this PR | |---|---|---| | `buffer_load_dword` | 0.919 (0.854-1.106) | 0.468 (0.423-0.714) | | `s_buffer_load_dwordx16` | 0.685 (0.602-0.759) | 0.468 (0.430-0.532) | The measurement programs aren't part of this PR; I can attach them if useful. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-09 20:49:10 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089013351 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2440#issuecomment-6089013351). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!2440
No description provided.