Skip to content

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

Closed
DotDebian wants to merge 1 commit into
boykopovar:mainfrom
DotDebian:feat/bda-lookup-last-range-cache
Closed

DotDebian wants to merge 1 commit into
boykopovar:mainfrom
DotDebian:feat/bda-lookup-last-range-cache

Conversation

@DotDebian

Copy link
Copy Markdown
Contributor

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 Astro Bot (PPSA21564) #357: Astro Bot (PPSA21564) #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

Every access by guest address calls a lookup that binary-searches the
BDA range table: about 11 dependent steps over ~1200 ranges, 34 table
loads per access. Since boykopovar#639 every buffer access goes through it.

The lookups (read, probe, write and atomic) now keep the last range a
search of the invocation resolved in four private variables: guest
begin and end, device base and permissions. An access inside that range
with the wanted permissions returns base + (address - begin) at once.
Anything else (another range, a missing permission, an unmapped,
zero-byte or wrapping access) takes the full search with every check
and fault it records today, and a successful search refills the cache.
The variables only hold what the table says, and no shader writes the
table, so the result is the search's own answer.

Astro Bot (PPSA21564), Crash Site, Windows 11, Core Ultra 9 285K,
RTX 5080: GPU time of the draws 59.5 -> 35 ms per frame.

APS5_NO_BDA_LOOKUP_CACHE=1 emits the lookups without the cache. The
switch is part of the shader disk cache key.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Windows run, since the PR has no Intel results: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main d31b57a, 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

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

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

RINNECODER added a commit to RINNECODER/AnyPS5 that referenced this pull request Oct 13, 2026
* fix(shader): keep each BDA page-table walk to one inlined copy per read

RADV inlines every SPIR-V function, DontInline included, so every
get_bda_pointer call site becomes its own copy of the page-table search,
and NIR's copy propagation of variables grows with each copy. The lookup
now keeps its search bounds in phis and records its header and range
faults from one call site each, picking the first failing check; a byte
read and the byte fallback of a span read loop over one lookup instead of
unrolling one per byte or dword.

(cherry picked from commit 2d3a970)

Ported from upstream PR boykopovar#3655

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): decode a GPU-selected formatted load from a format table

A formatted load whose V# is selected on the GPU emitted all 77 format
cases in its shared function, and drivers that inline every function,
like RADV, copied all of them into each call site. The load now reads the
format's component count, widths, byte and bit offsets and type from a
constant table and runs one path for its four components, with the
existing conversion of each (type, width) pair selected at run time.
Stores and loads through a prepared V# keep the format switch.

(cherry picked from commit 369928d)

Ported from upstream PR boykopovar#3656

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(shader): pin GPU-selected formatted loads for every buffer format

agc_driver_gpu_buffer_format loads the V#'s records and word3 from memory
and moves them into SGPRs with v_readfirstlane, so the recompiler sees a
GPU-selected descriptor and the load goes through the format table. It
checks buffer_load_format x/xy/xyz/xyzw and d16_xyzw on two lanes for
formats 1-77, selectors 0/1/4-7 on 1-, 2- and 4-component formats, loads
with a component past NUM_RECORDS, and that formats 0 and 78 stop the
invocation before its stores.

The expected values were captured on the format switch (2d3a970) and
are identical with the table.

(cherry picked from commit 6aaae03)

Ported from upstream PR boykopovar#3656

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Every access by guest address calls a lookup that binary-searches the
BDA range table: about 11 dependent steps over ~1200 ranges, 34 table
loads per access. Since boykopovar#639 every buffer access goes through it.

The lookups (read, probe, write and atomic) now keep the last range a
search of the invocation resolved in four private variables: guest
begin and end, device base and permissions. An access inside that range
with the wanted permissions returns base + (address - begin) at once.
Anything else (another range, a missing permission, an unmapped,
zero-byte or wrapping access) takes the full search with every check
and fault it records today, and a successful search refills the cache.
The variables only hold what the table says, and no shader writes the
table, so the result is the search's own answer.

On one title's busiest scene (Windows, discrete GPU), GPU time of the draws 59.5 -> 35 ms per frame.

APS5_NO_BDA_LOOKUP_CACHE=1 emits the lookups without the cache. The
switch is part of the shader disk cache key.

(cherry picked from commit 443583a)

Ported from upstream PR boykopovar#2837

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): read adjacent BDA dword loads of one block through one span read

A scalar load of several dwords (s_load_dwordx8) becomes one
LoadAddressU32 per dword, and each took its own span read: a probe of
the page table and a fallback that looks every byte up. Loads in one
block with equal address operands and offsets within 16 bytes of the
first now share its span read. Instructions that write memory end a
group; the address operands are compared by content, since each load
carries its own GetAddressResource and SelectU32 values.

A title's main-menu pixel shader goes from 136 span reads
to 34. AMD's Windows compiler inlines the read helpers at every call,
and did not finish that shader before.

(cherry picked from commit 4577633)

Ported from upstream PR boykopovar#4931

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): read GPU-selected V#s of s_buffer_load as SMEM does

An s_buffer_load_dword* whose V# the recompiler reads at run time (a
GPU-selected V#, for example one built with v_readfirstlane) went
through the vector buffer descriptor checks. A nonzero TYPE in word 3
made it return 0 and record an invalid-descriptor fault, and the base
was used with its two low bits, so a base off a dword boundary read
shifted bytes.

SMEM uses only the base, stride and num_records of the V#, and the two
low bits of the base and of the final address are ignored (AMD RDNA2
ISA guide, 30 November 2020, section 7.1 Table 27 SBASE and section
7.2.1 "Reads using Buffer Constant"). The capture-time evaluator
already follows both rules, and the bound path already drops the two
low bits. For scalar buffer loads the runtime path now masks the base
and skips the TYPE check; vector, formatted and atomic accesses keep
it.

agc_driver_scalar_buffer_gpu_descriptor runs s_buffer_load_dword and
s_buffer_load_dwordx2 through such a V# with the base 0 to 3 bytes off
a dword and TYPE 0 to 3, in wave32 and wave64.

(cherry picked from commit 86fdb52)

Ported from upstream PR boykopovar#5161

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(recompiler): read the buffer stride and presence at run time in compute modules

(cherry picked from commit 573ba3b)

Ported from upstream PR boykopovar#5353

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: EdouardSence <103503538+EdouardSence@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: DotDebian <quentin.waguet@hotmail.fr>
Co-authored-by: poonasor <rickypoon89@icloud.com>
Co-authored-by: Niko <nikobilger2000@gmail.com>
Co-authored-by: Alex Collini <pipodi93@gmail.com>

@CnRJay CnRJay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

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.

@atk3001

atk3001 commented Oct 31, 2026

Copy link
Copy Markdown

Numbers for a scene with scattered per-lane lookups, as asked above. Result: no measurable gain there.

Setup: Windows 10, RTX 5080 (driver 616.64), MinGW Release. The head ported onto main 0f30281. SpirvBdaLookup.cpp conflicted with #7571's single-exit lookup, so the hit test leaves through leave(), and the cache is not used for bda_anchor_range, whose range bound the cache does not hold. Header checks are kept. A/B in the same build with and without APS5_NO_BDA_LOOKUP_CACHE=1.

Tests: ANYPS5_REQUIRE_VULKAN=1 ctest -R "bda|global|flat|formatted_buffer|buffer_format" passes 49/49, and 43/43 with the switch set.

Scene: GTA V (PPSA04263), prologue, about 3 FPS. 89 % of GPU time is one compute dispatch per frame, program 0x25b254a00 (wave64, 8x8x1). Its main loop chases pointers per lane: each global_load address comes from the previous load. So nearly every lookup is per lane and dependent.

[gputime] per dispatch of 0x25b254a00 (APS5_PROFILE_GPU=1, 10 s intervals once the scene settled; runs are short because a frame takes 300 ms):

run per-interval mean ms/dispatch median
cache 1 139, 157, 166, 150, 156 156
cache 2 142, 155, 162, 135, 147 147
APS5_NO_BDA_LOOKUP_CACHE=1 1 136, 162, 169 162

The difference stays inside the spread between intervals (135–169 ms), and frame times did not change. That is two runs with the cache and one without.

Why: A Nsight Graphics GPU trace of the same scene (on main, per-line PC sampling on the SPIR-V) splits the dispatch's samples as follows:

Function Share of samples
probe_bda_pointer + get_bda_pointer, the search 16 %
load_bda_span, the data load 28 %
main, mostly waiting on read_bda_span_stop results 46 %

Long scoreboard accounts for 77 % of stalls, and L2 hits 97 % of requests. So even a perfect cache would remove at most about 16 % here. Successive reads in the chain touch different ranges, so one cached range per invocation probably keeps getting replaced. This is the scattered case CnRJay described.

Register cost (CnRJay's second point): offline compile of 0x25b254a00 on the RTX 5080, from APS5_PIPELINE_STATS:

SPIR-V words Registers Local memory Binary
without cache 216 616 255 1488 B 574 848 B
with cache 217 172 255 1520 B 577 408 B

The program is already at the 255 limit, so the cached values go to local memory (+32 B per thread) rather than costing occupancy.

I can push the ported branch if a reopen with these numbers is wanted.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants