perf(shader): try the last resolved range first in BDA lookups #2837

Closed
DotDebian wants to merge 1 commit from upstream-pr/2837 into main
DotDebian commented 2026-10-10 16:21:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-10T16:21:52Z.


What

Every guest access by address calls a BDA lookup that binary-searches the range table: about 11 dependent steps over ~1200 ranges, 34 table loads per access. Since #639 every buffer access goes through it, and a draw's accesses usually stay inside a few ranges.

The emitted lookups (read, probe, write, atomic) now keep the last range a search of the invocation resolved, in four Private variables (guest begin and end, device base, permissions), initialized to an empty range. An access inside that range with the wanted permissions returns base + (address - begin) without touching the table. Anything else (another range, a missing permission, an unmapped, zero-byte or wrapping access) takes the existing search with every check and fault it records today, and a successful search refills the cache. The variables only repeat what the table says, and no shader writes the table, so a hit returns what the search would have returned.

APS5_NO_BDA_LOOKUP_CACHE=1 emits the lookups as before. Being a recompiler APS5_* switch, it is part of the shader disk cache key, and the source hash already separates the old and new emission, so the disk cache format version is unchanged.

Tested

  • Windows 11, Core Ultra 9 285K, RTX 5080, MinGW g++ (WinLibs) Release with -DBUILD_TESTING=ON: ctest 709/709 passed (10 skipped as usual). agc_driver_bda_device runs the BDA execution tests on the GPU.
  • BdaExecution gains two reads after a cached hit: one starting in the cached range and ending in the next range (must not use the cached device address), one starting in the last range and ending past the table (must still fault as unmapped).
  • BDA and global-memory tests (-R "bda|global", 14 tests) also pass with APS5_NO_BDA_LOOKUP_CACHE=1 and no shader disk cache.
  • Astro Bot (PPSA21564), Crash Site, same host: GPU time of the draws 59.5 -> 35 ms per frame. This was measured on my local performance stack based on the astrobot branch (where reads by address mostly hit VRAM snapshots), with an earlier version of this change that also skipped the table header checks on the search path; this version keeps every check. Numbers and context in discussion #357: https://github.com/boykopovar/AnyPS5/discussions/357

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4812685200 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2837). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-10T16:21:52Z. --- ### What Every guest access by address calls a BDA lookup that binary-searches the range table: about 11 dependent steps over ~1200 ranges, 34 table loads per access. Since #639 every buffer access goes through it, and a draw's accesses usually stay inside a few ranges. The emitted lookups (read, probe, write, atomic) now keep the last range a search of the invocation resolved, in four `Private` variables (guest begin and end, device base, permissions), initialized to an empty range. An access inside that range with the wanted permissions returns `base + (address - begin)` without touching the table. Anything else (another range, a missing permission, an unmapped, zero-byte or wrapping access) takes the existing search with every check and fault it records today, and a successful search refills the cache. The variables only repeat what the table says, and no shader writes the table, so a hit returns what the search would have returned. `APS5_NO_BDA_LOOKUP_CACHE=1` emits the lookups as before. Being a recompiler `APS5_*` switch, it is part of the shader disk cache key, and the source hash already separates the old and new emission, so the disk cache format version is unchanged. ### Tested - Windows 11, Core Ultra 9 285K, RTX 5080, MinGW g++ (WinLibs) Release with `-DBUILD_TESTING=ON`: ctest 709/709 passed (10 skipped as usual). `agc_driver_bda_device` runs the BDA execution tests on the GPU. - `BdaExecution` gains two reads after a cached hit: one starting in the cached range and ending in the next range (must not use the cached device address), one starting in the last range and ending past the table (must still fault as unmapped). - BDA and global-memory tests (`-R "bda|global"`, 14 tests) also pass with `APS5_NO_BDA_LOOKUP_CACHE=1` and no shader disk cache. - Astro Bot (PPSA21564), Crash Site, same host: GPU time of the draws 59.5 -> 35 ms per frame. This was measured on my local performance stack based on the `astrobot` branch (where reads by address mostly hit VRAM snapshots), with an earlier version of this change that also skipped the table header checks on the search path; this version keeps every check. Numbers and context in discussion #357: https://github.com/boykopovar/AnyPS5/discussions/357 ### 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 (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-10 22:56:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:56:59Z.

Review: CHANGES_REQUESTED


Thanks, the change looks correct. Checked on the PR merged onto main (Linux, RADV): agc_driver_bda_device, agc_driver_bda_span_reads and the global load/atomic/store tests pass with and without APS5_NO_BDA_LOOKUP_CACHE=1. Removing the accessEnd <= cacheEnd term from the hit (SpirvBdaLookup.cpp:60) makes agc_driver_bda_device fail, so the two new rows do pin the cached-range edges.

One thing is missing:

  • The perf claim is measured on a different version of the change (it still skipped the table header checks) and on a local stack based on the astrobot branch. Please post before/after numbers for this exact head against current main (same title/scene, several runs, median), as perf PRs require in CONTRIBUTING. If the gain disappears once the header checks are kept, say so and we can decide whether the extra Private variables are worth it.

Once the numbers are in this is ready to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481108958 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2837#pullrequestreview-5481108958). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T22:56:59Z. **Review:** CHANGES_REQUESTED --- Thanks, the change looks correct. Checked on the PR merged onto main (Linux, RADV): `agc_driver_bda_device`, `agc_driver_bda_span_reads` and the global load/atomic/store tests pass with and without `APS5_NO_BDA_LOOKUP_CACHE=1`. Removing the `accessEnd <= cacheEnd` term from the hit (`SpirvBdaLookup.cpp:60`) makes `agc_driver_bda_device` fail, so the two new rows do pin the cached-range edges. One thing is missing: - The perf claim is measured on a different version of the change (it still skipped the table header checks) and on a local stack based on the `astrobot` branch. Please post before/after numbers for this exact head against current `main` (same title/scene, several runs, median), as perf PRs require in CONTRIBUTING. If the gain disappears once the header checks are kept, say so and we can decide whether the extra Private variables are worth it. Once the numbers are in this is ready to merge.
Silverash-i8 commented 2026-10-11 02:10:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T02:10:50Z.


Windows run, since the PR has no Intel results: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main d31b57a4, RTX 5060 Laptop and the Arrow Lake iGPU (ANYPS5_GPU=Intel).

  • agc_driver_bda_device (which builds BdaExecution.cpp) and agc_driver_bda_span_reads pass on both GPUs, and nothing else in the agc_* suite changed.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104497048 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2837#issuecomment-6104497048). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T02:10:50Z. --- Windows run, since the PR has no Intel results: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto `main` d31b57a4, RTX 5060 Laptop and the Arrow Lake iGPU (`ANYPS5_GPU=Intel`). - `agc_driver_bda_device` (which builds `BdaExecution.cpp`) and `agc_driver_bda_span_reads` pass on both GPUs, and nothing else in the `agc_*` suite changed. AI-assisted (Claude Code); I ran this on my machine.
github-actions[bot] commented 2026-10-13 08:45:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T08:45:57Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#3655 core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp L4, L27-31, L53, L75-76, L94, L111
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6141809324 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2837#issuecomment-6141809324). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T08:45:57Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3655 | `core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp` [L4](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp#L4), [L27-31](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp#L27-L31), [L53](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp#L53), [L75-76](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp#L75-L76), [L94](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp#L94), [L111](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp#L111) | | |
CnRJay commented 2026-10-14 00:54:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T00:54:06Z.

Review: COMMENTED


Agree it's correct. On the "whether the extra Private variables are worth it" question, two GPU-side effects to look for in the numbers:

  • Divergence. The cache is per invocation, but the hit test is a branch. In a wave where some lanes hit and others miss, the hardware runs both the early return and the full search, so the wave pays the search plus the hit check. Access patterns that are coherent per wave (most constant and vertex data) should gain. Scattered per-lane accesses (indirection tables, BVH or particle data across many ranges) can get slightly slower, and every miss also stores four values back. A scene that uses both is a better test than a single favourable one.
  • Register pressure. The cache keeps three 64-bit values and one 32-bit value live across every lookup call site: about 7 VGPRs for the whole shader, since Private variables become registers after inlining. Small shaders won't notice. For the large compute programs other PRs mention (1M+ SPIR-V words), that can cost a wave of occupancy. The pipeline statistics that LogPipelineStatistics already prints (VGPR count) for a few large shaders, before and after, would show whether that happens.

If the gain on current main turns out to be mostly in draws with coherent access, limiting the cache to graphics stages, or to modules below some register count, would keep it where it helps.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511816970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2837#pullrequestreview-5511816970). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T00:54:06Z. **Review:** COMMENTED --- Agree it's correct. On the "whether the extra Private variables are worth it" question, two GPU-side effects to look for in the numbers: - **Divergence.** The cache is per invocation, but the hit test is a branch. In a wave where some lanes hit and others miss, the hardware runs both the early return and the full search, so the wave pays the search plus the hit check. Access patterns that are coherent per wave (most constant and vertex data) should gain. Scattered per-lane accesses (indirection tables, BVH or particle data across many ranges) can get slightly slower, and every miss also stores four values back. A scene that uses both is a better test than a single favourable one. - **Register pressure.** The cache keeps three 64-bit values and one 32-bit value live across every lookup call site: about 7 VGPRs for the whole shader, since `Private` variables become registers after inlining. Small shaders won't notice. For the large compute programs other PRs mention (1M+ SPIR-V words), that can cost a wave of occupancy. The pipeline statistics that `LogPipelineStatistics` already prints (VGPR count) for a few large shaders, before and after, would show whether that happens. If the gain on current `main` turns out to be mostly in draws with coherent access, limiting the cache to graphics stages, or to modules below some register count, would keep it where it helps.
oneandonlydean commented 2026-10-14 02:27:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:27:50Z.


Closing: adriadam10's request from 2026-10-10 for before/after numbers on this head against current main has had no reply or push. CnRJay's review above lists what those numbers should cover: a scene with both wave-coherent and scattered per-lane lookups, and the register cost of the cached values. Reopen it with them when they're in.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159528551 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2837#issuecomment-6159528551). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:27:50Z. --- Closing: adriadam10's request from 2026-10-10 for before/after numbers on this head against current `main` has had no reply or push. CnRJay's review above lists what those numbers should cover: a scene with both wave-coherent and scattered per-lane lookups, and the register cost of the cached values. Reopen it with them when they're in.

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!2837
No description provided.