perf(shader): cache BDA ranges in span reads #14260

Open
kiansaeidnia wants to merge 1 commit from upstream-pr/14260 into main
kiansaeidnia commented 2026-11-01 05:05:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kiansaeidnia on 2026-11-01T05:05:31Z.


What

Draft: not ready for review yet. The change and the results below are real; the open items are listed at the end of this section.

Address-based (BDA) span reads walk the BDA table (probe_bda_pointer) on every access. A program that runs as one wave pays every walk on its critical path, because nothing else is resident to hide the latency. In Ghost of Yotei's opening cinematic, the single-wave program 0x807b90a100 spends 94 % of its time in a triangle loop of six runtime-format buffer loads. Shader-clock instrumentation of that loop shows the table walk is about a third of each load (~2,950 of ~8,400 cycles); the data load itself is ~220 cycles.

Each invocation now keeps the last four ranges its span reads found, as (begin, bound, device base) entries.

  • A read inside one of them translates directly.
  • A miss walks the table with the existing range-returning lookup (bda_anchor_range, with the same checks as the probe) and replaces the oldest entry with the range it finds.
  • A kept range ends at the next range's start, which is the first address the walk would resolve elsewhere, so a hit resolves exactly as the walk would.
  • An unmapped span still returns 0 and takes the same fault path as before.

The entries are separate variables indexed only by constants. A first version indexed 16 slots by pc; its dynamically indexed array added 512 bytes of local memory per thread on NVIDIA, and that showed up as 270 MB more VRAM in the frame replay. The four-entry version adds 176 bytes and about 40 MB.

APS5_NO_BDA_RANGE_CACHE=1 walks the table for every read, as before.

Still to do before this is ready:

  • A main-vs-PR summary from the same machine and scene. Ghost of Yotei only runs on the Celegans12 yotei/best fork, so the in-game numbers below come from that fork, not from main. I still need a title or homebrew that runs on main and exercises address-based span reads.
  • The replay and in-game tables were measured on the first (pc-indexed) version, or alongside a separate ballot change. They need repeating with exactly this commit.
  • #7392 edits the same span-read function (BdaSpanReadResultType and the stop handling) for a different purpose, masking writes after faults. Whichever lands second needs a rebase.

Overlap: the pr-overlap bot could not compare 54 open PRs that conflict with main, so I reviewed them by hand.

  • 40 share only docs/dev/ENVIRONMENT.md (rows in the variable table).
  • 14 also touch SpirvEmitterState.hpp, where this PR adds one field (bdaRangeCache). Their additions there are unrelated: fault masking (#6751, #7392), device image tables and samplers (#7642, #10193, #13928, #13929), the mesh fallback (#9356, #9387), spanned-wave subgroups (#8004), stencil export (#10351), MoltenVK image atomics (#10652), f16/f64 modes (#11351, #11483), and the ARM64 AOT consolidation (#5021).
  • None of them caches BDA ranges. Only #7392 also edits SpirvBdaRead.cpp (see above).

Tested

Windows 11 Pro 10.0.26200, RTX 3080 Ti, NVIDIA 616.92.

  • New agc_driver_bda_range_cache test. Eight loads over five guest blocks: A, A again (a hit), then B, C and D (misses), E (evicts A), A (evicts B) and B. It runs on wave32, wave64 and split wave64, with and without glc, and also passes with APS5_NO_BDA_RANGE_CACHE=1.

  • Focused tests on this branch rebased onto main 6fe120d11b, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (ctest -R "bda_range_cache|bda_span_reads|bda_anchor_reads|bda_device|global_loads|gpu_buffer_format|buffer_format|descriptor_web_faults|buffer_load_store" -V):

    • main: 15/15 passed.
    • This PR: 16/16 passed (the same 15 plus the new test).
    • Neither log has a VUID- line or a missing-layer message.
  • spirv-val --target-env vulkan1.3 passes for the five largest captured Ghost of Yotei programs (on the earlier base).

  • Frame replay (agc_frame_replay, exact mode) of a captured cinematic frame, alternating runs with the cache off and on. These runs used the first, pc-indexed version; the four-entry version gives 0x807b90a100 122.5 ms in the same replay. Output buffers are byte-identical in every run.

    Cache off Cache on
    0x807b90a100 174.7, 172.1 ms 120.7, 120.8 ms (−30 %)
    0x801edd2700 162, 176 ms 114, 120 ms (−30 %)
    All timed GPU work 678, 629 ms 576, 591 ms

    Shader-clock cycles of 0x807b90a100 and 0x807b925400 drop 34 % with the first version. With four entries, 1 of 42,846 reads walks the table.

  • In game, Ghost of Yotei (PPSA26344), on the yotei/best fork's runtime (see above). The change build also carried a spanned-ballot change that measured −1 to −6 % on these programs. Warm shader cache, tools/trace_digest.py of aps5-summary.jsonl:

    Without With
    Logo screens, presents per 10 s (p50) 70 (143 ms) 78 (129 ms)
    Opening cinematic, presents per 10 s 3.2–4.4 (5 runs) 4.95
    GPU busy per present in the cinematic 1,251–1,685 ms 959 ms
    0x807b90a100 / 0x807b925400 / 0x801edd2700 per run 155 / 129 / 20 ms 87 / 73 / 11 ms

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3080 Ti, driver 616.92, Windows 11 Pro 10.0.26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5104569152 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14260). Originally posted by [kiansaeidnia](https://github.com/kiansaeidnia) on 2026-11-01T05:05:31Z. --- ### What **Draft: not ready for review yet.** The change and the results below are real; the open items are listed at the end of this section. Address-based (BDA) span reads walk the BDA table (`probe_bda_pointer`) on every access. A program that runs as one wave pays every walk on its critical path, because nothing else is resident to hide the latency. In Ghost of Yotei's opening cinematic, the single-wave program 0x807b90a100 spends 94 % of its time in a triangle loop of six runtime-format buffer loads. Shader-clock instrumentation of that loop shows the table walk is about a third of each load (~2,950 of ~8,400 cycles); the data load itself is ~220 cycles. Each invocation now keeps the last four ranges its span reads found, as `(begin, bound, device base)` entries. - A read inside one of them translates directly. - A miss walks the table with the existing range-returning lookup (`bda_anchor_range`, with the same checks as the probe) and replaces the oldest entry with the range it finds. - A kept range ends at the next range's start, which is the first address the walk would resolve elsewhere, so a hit resolves exactly as the walk would. - An unmapped span still returns 0 and takes the same fault path as before. The entries are separate variables indexed only by constants. A first version indexed 16 slots by pc; its dynamically indexed array added 512 bytes of local memory per thread on NVIDIA, and that showed up as 270 MB more VRAM in the frame replay. The four-entry version adds 176 bytes and about 40 MB. `APS5_NO_BDA_RANGE_CACHE=1` walks the table for every read, as before. Still to do before this is ready: - A `main`-vs-PR summary from the same machine and scene. Ghost of Yotei only runs on the Celegans12 `yotei/best` fork, so the in-game numbers below come from that fork, not from `main`. I still need a title or homebrew that runs on `main` and exercises address-based span reads. - The replay and in-game tables were measured on the first (pc-indexed) version, or alongside a separate ballot change. They need repeating with exactly this commit. - #7392 edits the same span-read function (`BdaSpanReadResultType` and the stop handling) for a different purpose, masking writes after faults. Whichever lands second needs a rebase. Overlap: the pr-overlap bot could not compare 54 open PRs that conflict with `main`, so I reviewed them by hand. - 40 share only `docs/dev/ENVIRONMENT.md` (rows in the variable table). - 14 also touch `SpirvEmitterState.hpp`, where this PR adds one field (`bdaRangeCache`). Their additions there are unrelated: fault masking (#6751, #7392), device image tables and samplers (#7642, #10193, #13928, #13929), the mesh fallback (#9356, #9387), spanned-wave subgroups (#8004), stencil export (#10351), MoltenVK image atomics (#10652), f16/f64 modes (#11351, #11483), and the ARM64 AOT consolidation (#5021). - None of them caches BDA ranges. Only #7392 also edits `SpirvBdaRead.cpp` (see above). ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro 10.0.26200, RTX 3080 Ti, NVIDIA 616.92. - New `agc_driver_bda_range_cache` test. Eight loads over five guest blocks: A, A again (a hit), then B, C and D (misses), E (evicts A), A (evicts B) and B. It runs on wave32, wave64 and split wave64, with and without `glc`, and also passes with `APS5_NO_BDA_RANGE_CACHE=1`. - Focused tests on this branch rebased onto `main` 6fe120d11b, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (`ctest -R "bda_range_cache|bda_span_reads|bda_anchor_reads|bda_device|global_loads|gpu_buffer_format|buffer_format|descriptor_web_faults|buffer_load_store" -V`): - `main`: 15/15 passed. - This PR: 16/16 passed (the same 15 plus the new test). - Neither log has a `VUID-` line or a missing-layer message. - `spirv-val --target-env vulkan1.3` passes for the five largest captured Ghost of Yotei programs (on the earlier base). - Frame replay (`agc_frame_replay`, exact mode) of a captured cinematic frame, alternating runs with the cache off and on. These runs used the first, pc-indexed version; the four-entry version gives 0x807b90a100 122.5 ms in the same replay. Output buffers are byte-identical in every run. | | Cache off | Cache on | |---|---|---| | 0x807b90a100 | 174.7, 172.1 ms | 120.7, 120.8 ms (−30 %) | | 0x801edd2700 | 162, 176 ms | 114, 120 ms (−30 %) | | All timed GPU work | 678, 629 ms | 576, 591 ms | Shader-clock cycles of 0x807b90a100 and 0x807b925400 drop 34 % with the first version. With four entries, 1 of 42,846 reads walks the table. - In game, Ghost of Yotei (PPSA26344), on the `yotei/best` fork's runtime (see above). The change build also carried a spanned-ballot change that measured −1 to −6 % on these programs. Warm shader cache, `tools/trace_digest.py` of `aps5-summary.jsonl`: | | Without | With | |---|---|---| | Logo screens, presents per 10 s (p50) | 70 (143 ms) | 78 (129 ms) | | Opening cinematic, presents per 10 s | 3.2–4.4 (5 runs) | 4.95 | | GPU busy per present in the cinematic | 1,251–1,685 ms | 959 ms | | 0x807b90a100 / 0x807b925400 / 0x801edd2700 per run | 155 / 129 / 20 ms | 87 / 73 / 11 ms | ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3080 Ti, driver 616.92, Windows 11 Pro 10.0.26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
CnRJay commented 2026-11-01 19:42:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:42:48Z.

Review: COMMENTED


Marked draft, so this is early feedback. The idea is sound for a single-wave loop of dependent loads, where the table walk is on the critical path. The cost lands on every shader with address-based span reads.

1. Local memory per thread is added to every program with a span read. Four (begin, bound, base) entries are 176 bytes per invocation, and the description's own first attempt showed what that costs: 270 MB more VRAM in the frame replay, and about 40 MB for the four-entry version. On NVIDIA that is also register or local-memory pressure for every wave, which lowers occupancy for programs that have many waves resident and do not need the cache. The win quoted is for a program that runs as one wave. Please apply the cache only where it can pay: a span read inside a loop, or a program whose launch is a single wave, and keep the table walk for the rest. Report the register or local-memory change for the largest captured programs from the replay.

2. The hit rate is not shown. Eight loads over five blocks test correctness. What decides the value is how often a real shader's reads hit one of four ranges. Please add hit and miss counters under the existing APS5_NO_BDA_RANGE_CACHE switch and report the ratio for the Yotei program and for two more titles. A 4-entry cache with oldest-replacement thrashes on a loop that cycles through five buffers.

3. Misses cost more than before. Each miss is a walk plus a replace, where before it was a walk. For a program with a low hit rate this is a regression. Show the replay time with the cache on and off for a program with scattered reads.

4. The numbers are on a different tree. The in-game table comes from the Celegans12 fork and the first version of the change, as the open items say. For the evidence to be usable, rerun it on exactly this commit, and on main for at least one title that runs there.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729898214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14260#pullrequestreview-5729898214). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:42:48Z. **Review:** COMMENTED --- Marked draft, so this is early feedback. The idea is sound for a single-wave loop of dependent loads, where the table walk is on the critical path. The cost lands on every shader with address-based span reads. **1. Local memory per thread is added to every program with a span read.** Four `(begin, bound, base)` entries are 176 bytes per invocation, and the description's own first attempt showed what that costs: 270 MB more VRAM in the frame replay, and about 40 MB for the four-entry version. On NVIDIA that is also register or local-memory pressure for every wave, which lowers occupancy for programs that have many waves resident and do not need the cache. The win quoted is for a program that runs as one wave. Please apply the cache only where it can pay: a span read inside a loop, or a program whose launch is a single wave, and keep the table walk for the rest. Report the register or local-memory change for the largest captured programs from the replay. **2. The hit rate is not shown.** Eight loads over five blocks test correctness. What decides the value is how often a real shader's reads hit one of four ranges. Please add hit and miss counters under the existing `APS5_NO_BDA_RANGE_CACHE` switch and report the ratio for the Yotei program and for two more titles. A 4-entry cache with oldest-replacement thrashes on a loop that cycles through five buffers. **3. Misses cost more than before.** Each miss is a walk plus a replace, where before it was a walk. For a program with a low hit rate this is a regression. Show the replay time with the cache on and off for a program with scattered reads. **4. The numbers are on a different tree.** The in-game table comes from the Celegans12 fork and the first version of the change, as the open items say. For the evidence to be usable, rerun it on exactly this commit, and on `main` for at least one title that runs there.
MarcZX10 commented 2026-11-19 19:34:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T19:34:53Z.

Review: COMMENTED


Reviewed — comments

The review covered the changed implementation, its new execution test, and the documentation at head 8630fa906d675132ad2741f997c275edb36434f9; performance results and focused test runs are author-reported, and I did not run checks.

What I ran

No commands or tests were run. I read review/description.md, review/commits.md, review/manifest.json, and review/diff.patch, plus the full text of all four changed files through the pinned reader. The supplied reader was repo_read.py.

Results

Check Result Note
Source review passed Four changed files read
Tests not run Results in description are author-reported
Performance evidence not verified Reported captures predate this exact implementation

Findings

Blocking

None.

Non-blocking

  • core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp:393 caches the environment-variable decision in a function-local static. Changing APS5_NO_BDA_RANGE_CACHE after the first shader compilation will not change behavior for later compilations in that process. This may be fine if runtime switches are intentionally process-start settings, but the new toggle's expected lifetime is not specified in the change description.
  • The description says the performance tables and replay were measured with earlier code or alongside another change, and says a same-machine main comparison and exact-commit captures remain outstanding. Those results therefore do not establish the performance or memory impact of this exact head.

Not verified

I did not execute tests or reproduce performance captures. The test and source files were read; no changed files were excluded according to manifest.json. review/files.json was not available through the reader, so its exclusion list could not be independently checked. I did not verify behavior on physical console hardware or other host platforms.

To get this to a good state

Nothing is required. Optional: clarify whether the environment switch is process-start-only, and provide exact-head performance captures and a matched main comparison when available.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5981971873 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14260#pullrequestreview-5981971873). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T19:34:53Z. **Review:** COMMENTED --- # Reviewed — comments The review covered the changed implementation, its new execution test, and the documentation at head `8630fa906d675132ad2741f997c275edb36434f9`; performance results and focused test runs are author-reported, and I did not run checks. ## What I ran No commands or tests were run. I read `review/description.md`, `review/commits.md`, `review/manifest.json`, and `review/diff.patch`, plus the full text of all four changed files through the pinned reader. The supplied reader was `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Four changed files read | | Tests | not run | Results in description are author-reported | | Performance evidence | not verified | Reported captures predate this exact implementation | ## Findings ### Blocking None. ### Non-blocking - `core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp:393` caches the environment-variable decision in a function-local `static`. Changing `APS5_NO_BDA_RANGE_CACHE` after the first shader compilation will not change behavior for later compilations in that process. This may be fine if runtime switches are intentionally process-start settings, but the new toggle's expected lifetime is not specified in the change description. - The description says the performance tables and replay were measured with earlier code or alongside another change, and says a same-machine `main` comparison and exact-commit captures remain outstanding. Those results therefore do not establish the performance or memory impact of this exact head. ## Not verified I did not execute tests or reproduce performance captures. The test and source files were read; no changed files were excluded according to `manifest.json`. `review/files.json` was not available through the reader, so its exclusion list could not be independently checked. I did not verify behavior on physical console hardware or other host platforms. ## To get this to a good state Nothing is required. Optional: clarify whether the environment switch is process-start-only, and provide exact-head performance captures and a matched `main` comparison when available.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/14260:upstream-pr/14260'
git switch 'upstream-pr/14260'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/14260'
git switch 'upstream-pr/14260'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/14260'
git switch 'upstream-pr/14260'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/14260'
git switch 'main'
git merge --squash 'upstream-pr/14260'
git switch 'main'
git merge --ff-only 'upstream-pr/14260'
git switch 'main'
git merge 'upstream-pr/14260'
git push origin 'main'
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!14260
No description provided.