Skip to content

fix(shader): keep each BDA page-table walk to one inlined copy per read - #3655

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
EdouardSence:fix/bda-lookup-compact
Oct 14, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
EdouardSence:fix/bda-lookup-compact

Conversation

@EdouardSence

@EdouardSence EdouardSence commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What

On RADV, Ghost of Yōtei (PPSA26344) cannot create the pipeline of compute shader 0x8000289300: vkCreateComputePipelines takes the process past 26 GB until it is OOM-killed. The title gets there on main a11c8f7 with #757/#758 merged; on main alone it stops earlier, at #2715. The diagnosis is in discussion #2884.

RADV inlines every SPIR-V function, DontInline included. Every get_bda_pointer call site therefore becomes its own copy of the page-table search, and nir_opt_copy_prop_vars (gdb, Mesa 26.2.4 with symbols) clones and rescans its copy set at each of these copies' loops, branches and fault stores. A byte read looked up once per byte, and the byte fallback of a span read did 4 dword reads of 4 lookups each.

This PR keeps one inlined lookup per read, with the same results and faults:

  • DefineBdaLookup keeps the search bounds in loop phis instead of two Function variables.
  • Its header checks and its range checks each record their fault from one site. The reason is the first failing check, in the original order. The two checks that guard out-of-bounds table reads (length < 4 and index == 0) still return on their own.
  • EmitBdaBytes loops over its bytes around one EmitBdaByte.
  • The byte fallback of read_bda_span loops over its 4 dwords around one dword read.

Measured outside the title with a small Vulkan harness: the replayed module of 0x8000289300, RADV, MESA_SHADER_CACHE_DISABLE. To separate the cost of its formatted loads, those are cut after N:

formatted loads kept main this PR this PR + #3656
0 11.1 s, 4.4 GB 4.1 s, 0.6 GB 4.1 s, 0.6 GB
8 39.5 s, 10.4 GB not measured 11.4 s, 0.9 GB
128 out of memory over 10 GB 35.8 s, 8.3 GB

All 128 still need #3656 (one formatted load path instead of 77 cases). With both, the module compiles in 35.8 s and 8.3 GB, and Yōtei runs a 300 s session, 4711 shaders compiled, without the OOM.

Tested

Linux (CachyOS), RX 9070 XT, RADV 26.2.4, GCC, Release.

  • Full ctest: 831 of 832 pass. import_audit fails because its source commit lookup does not work in a git worktree; it fails the same on main.
  • The BDA execution tests run on the GPU.
  • agc_shader_memory now also checks that get_bda_pointer records faults from at most 4 sites and has no OpVariable, and that read_bda_dword_bytes calls the lookup once. On main it fails: one byte load per byte, 4 lookups.
  • tools/check_conventions.py passes.

#2837 also changes DefineBdaLookup (a cached last range in Private variables) and will conflict with this. Its variable stores may also add to the RADV copy-propagation cost; I haven't measured that.

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

Tested and written by Claude (AI assistant) on a contributor's machine.

🤖 Generated with Claude Code

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.

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

Copy link
Copy Markdown
Contributor

Independent pre-review on Linux (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON.

  • Builds; tools/check_conventions.py --base origin/main passes.
  • Full suite: 832/832 passed (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).

@Dextroid17

Copy link
Copy Markdown
Contributor

I checked the two things this fix could get wrong, and neither is wrong. The first is subtle enough to spell out.

The fault reason is preserved, and the construction is what makes that work. Collapsing several failure checks into one recording site risks changing which reason is reported when more than one check fails — and the reason is guest-visible. failFirst iterates its checks in reverse and folds each into reason with OpSelect, so the last write wins — and because the iteration is reversed, that last write is the first check in the original order. The earliest failing check's reason therefore survives exactly as before, while the module's lookup records its fault from at most four record_bda_fault call sites — which is what the test's lookupFaults <= 4 pins. The !recordFaults path returning zero, and the two guards that still return on their own, keep their original behaviour.

A loop instead of an unrolled sequence is the real mechanism, and it is the right one on a driver that inlines everything. EmitBdaBytes emitted the bytes as straight-line code, so every byte carried its own copy of the page-table search and nir_opt_copy_prop_vars paid for each one. EmitBdaByteLoop puts the phis and the OpLoopMerge in, which leaves the search as one code copy however many bytes are read. The test pins that structurally instead of by timing: readerLoads from 4u to 1u, lookupFaults <= 4, lookupVariables == 0, readerLookups == 1.

That structural test is what makes this reviewable at all. Counting the calls to get_bda_pointer and record_bda_fault in the emitted SPIR-V is deterministic on the host — no GPU, no Mesa version, no timing — so "on main it fails" is a real RED rather than a flake, and it keeps failing if the unrolled emission ever comes back. Worth as much as the fix.

Two things worth stating before this merges.

One question, on the runtime side of the trade. The loop form shrinks the module, which is what the wall needs, but it replaces straight-line code with phis and a per-iteration branch. Your measured cases do not show a regression — the zero-formatted-loads module at 4.1 s, and 35.8 s for the both-PRs run where the unrolled form ran out of memory — and on a driver that inlines everything, one inlined loop is inherently fewer copies than N unrolled ones, so this may already be settled. Worth confirming anyway: did the 4711-shader session or the four focused cases show the loop form slower anywhere hot, or is the count small enough that it cannot matter? If a long span ever is hot, that would be the argument for unrolling small counts and looping only over long spans.

Written with AI assistance.

@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 11, 2026
@milescoviello

Copy link
Copy Markdown
Contributor

Intel results, since this also fixes #2972 (the BDA tests timing out or failing to compile on Intel ANV). Arrow Lake iGPU, Mesa 26.1.8 ANV, Linux 7.2.8, GCC 15.3.0, Release. Every run is a cold compile (ANYPS5_NO_SHADER_CACHE=1, MESA_SHADER_CACHE_DISABLE=true), 3 runs each (1 for the timeout):

Intel ANV, cold agc_driver_flat_store agc_driver_masked_global_store agc_driver_flat_loads_x2_x3
main a11c8f7 times out (300 s) passes, 25 s passes, 4.8 s
this PR 2d3a970 passes, 65 s passes, 4.7 s passes, 0.2 s

Full ctest on Intel with this PR: 830/834. The four failures (emulated_color_compare #3526, gated_dispatch_capture and _short_timeout #3345, vop1_float_unary #2114) fail the same way on main.

flat_store still takes 65 s on a cold compile here, over the 30 s test limit. Recording the lookup's fault from one site instead of four (on top of this PR, guarding the rest of the lookup with selections and passing the reason through phis, still with no function variables) brings it to 51 s and roughly halves the spills ANV reports for its compute pipeline (593 → 275 spills, 1516 → 802 fills), and masked_global_store to 3.8 s. I'll open that as a follow-up once this is merged.

AI-assisted: yes

@milescoviello

Copy link
Copy Markdown
Contributor

One more data point, on NVIDIA this time: Ubuntu 24.04, GCC 13.3, RTX 4070 Ti, driver 580.142, Release with CI's configure flags.

On main a11c8f7 the BDA read tests hang the GPU there the way #3291 describes (and closed #3087 did on 591.86). agc_driver_flat_loads, _x2_x3 and _x4, agc_driver_global_loads, _x2_x3 and _x4, and agc_driver_flat_apertures_2 and _4 print recorded batch still running on the GPU after 10 s and abort after about 65 s with guest allocation ... is still pinned by GPU work or an import (waited 60000 ms for the lease). agc_driver_pixel_interlock fails with vkDeviceWaitIdle: Vulkan result -4.

With this PR (2d3a970) all of them pass in 0.2 to 3.6 s, along with agc_driver_flat_apertures_1, _3 and _5. So this likely fixes #3291 as well, at least on this driver.

AI-assisted: yes

@EdouardSence

Copy link
Copy Markdown
Contributor Author

@Dextroid17 on the runtime question: not measured. Every number in this PR is compile time and compile memory. In the 300 s Yōtei session the title never presents a frame, so I have no frame time to compare. On main the same session dies in pipeline creation, so there is no baseline to compare against either.

What the loop changes at runtime: a span read that used to emit N unrolled page walks (one per dword, or one per byte for the unaligned fallback) now runs a loop of N iterations, with one walk and a phi per iteration. Reads of 4 bytes or less still take the single straight-line path. Only spans longer than that, and the byte fallback, loop.

If someone has a title that draws frames through long BDA spans, a frame-time comparison with and without this PR would settle it. If it shows a cost, unrolling spans up to a small length and looping past it is a contained change in read_bda_span. I'd rather add it with a measurement than without one.

Agreed on sequencing: #3655 and #3656 should land together, and #2837 should be re-measured on top of them.

Written by Claude (AI assistant) on a contributor's machine.

@MarcZX10

MarcZX10 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

I evaluated the independent single-fault-site change from issue #2972 and briefly submitted it as #3989, then closed that carry PR after reviewing the issue's note that the ready branch was offered for a maintainer to carry. Technically, the issue's change reduces fault-recording call sites and scratch pressure on older Intel Mesa; this PR reduces duplicated page-table walks. They have different goals but overlap in SpirvBdaLookup.cpp, SpirvBdaFault.cpp, and shader memory tests, so merge order may require a rebase. The issue includes the contributor's Intel and NVIDIA test results; my local Linux build did not complete.

@Scramblrrrr

Copy link
Copy Markdown
Contributor

Windows/Blackwell cross-test: #3655 2d3a970 + #3656 369928d/6aaae039 applied cleanly on our007 runtime-image-table integration, public combined head bc46e2d: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-bda-compact-cross-test . Original author commits retained. Release, SPIRV-Tools OFF, RTX5070Ti/NVIDIA617.14, Vulkan required.

Focused checks 6/6 pass,6.50s, no reported skips: agc_shader_memory0.36s, agc_driver_bda_device3.00s, agc_formatted_buffer_once0.17s, agc_driver_flat_loads0.48s, agc_driver_global_loads0.55s, agc_driver_gpu_buffer_format1.72s. This covers the emitted helper structure, native address/fault behavior and GPU-selected formats; not a full-suite result.

Staging a separate007 run next. No game-output/FPS improvement claimed yet. AI-assisted: yes(Codex).

@rebelittle

Copy link
Copy Markdown
Contributor

This PR also cuts the lavapipe compile time behind the BDA execution tests that Linux CI reports as slower than 30 s.

Measured on Windows 10 with lavapipe from Mesa 25.2.8 (the Mesa version in Ubuntu 24.04 updates; LLVM 21.1.7 in this build), a build with ANYPS5_ENABLE_SPIRV_TOOLS=ON as on Linux CI, and MESA_SHADER_CACHE_DISABLE=true, on main e013f8c without and with this PR (2d3a970) merged:

Test main with this PR Linux CI on main
agc_driver_bda_span_reads 28.5 s 11.0 s 30.8–32.5 s
agc_driver_masked_global_store 37.0 s 12.4 s up to 30.5 s
agc_driver_global_vcc_base 48.3 s 16.0 s 45.4–55.6 s
agc_driver_flat_store 99.2 s 58.5 s 66.7–90.7 s

All four pass with the PR merged. Mesa's disk cache is not active in this Windows build, so every dispatch compiles its pipeline, while on Linux CI a module that was already compiled can come from the cache; both columns here were measured the same way. The CI ranges are the slow-test annotations of runs 38140296210, 38168846650, 38169396667 and 38204420760.

Measured with Claude Code.

@oneandonlydean oneandonlydean self-assigned this Oct 13, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Needs a rebase: #4759 is merged and this now conflicts with it in core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp. The full build of 2d3a970 was green before that merge, so it goes in on a green build of the rebased head.

@MarcZX10

Copy link
Copy Markdown
Contributor

I rebased this onto current main after integrating #4759. The combined change keeps the in-place and fallback paths in separate selections, and retains the bounded loop for the unaligned dword fallback. The rebased Linux Release build of libSceAgcDriver succeeds; agc_shader_memory passes (1/1, 0.34 s), and tools/check_conventions.py --base upstream/main passes. I pushed the rebased commit to my branch codex/rebase-bda-compact-3655 because the original head branch is in your fork and is not writable from this account. Please switch the PR head to MarcZX100:codex/rebase-bda-compact-3655 when convenient.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Heads-up before this is switched or rebased again: #5022 is merged and replaces the span-read shape from #4759. The two separate selections (in-place loads under the condition, fallback under its negation) hang NVIDIA 595.97 and newer (#4975; here also 610.57.04 on Linux, Xid 109, and a 3D title loses the device at level start), while the single selection from before #4759 hangs 591.86/590.48. On main the in-place loads now sit in load_bda_span, a DontInline function with no calls, and read_bda_span is one selection between calling it and the byte fallback.

MarcZX100:codex/rebase-bda-compact-3655 (8f57bc7) keeps the separate selections and conflicts with main in SpirvBdaRead.cpp. Please rebase onto #5022 keeping its shape (the "BDA read functions" check in agc_shader_memory pins it), and run agc_driver_scalar_wide_loads, agc_driver_flat_apertures_0, agc_driver_bda_span_reads and agc_driver_pixel_interlock on an NVIDIA 595+ driver, with and without SPIRV-Tools.

@Dextroid17

Copy link
Copy Markdown
Contributor

I merged blawesom:rebase/3656's #3655 commit (805be46cf) onto main 0aa4cb1c2, which includes #5022. The merge is clean, and it leaves load_bda_span and the read_bda_span selection as #5022 has them.

I tested it together with #3656 on lavapipe 26.2.4 with SPIRV-Tools OFF. agc_shader_memory (the "BDA read functions" check) passes, and so do the four tests you named.

The NVIDIA 595+ runs, with and without SPIRV-Tools, are not done. Details: #3656 (comment)

AI-assisted: yes (Claude).

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>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

The NVIDIA runs are done: on 2d3a970 merged onto main 30f9612, agc_shader_memory ("BDA read functions"), agc_driver_bda_span_reads, agc_driver_pixel_interlock, agc_driver_scalar_wide_loads and agc_driver_flat_apertures_0 pass twice each with SPIRV-Tools ON and OFF, with no hang or Xid. RTX 4090, NVIDIA 610.57.04, Linux, Release.

After #5022 the current head merges cleanly, to the same tree as 805be46, and keeps load_bda_span and the read_bda_span selection as they are on main. So no rebase is needed; keep this head and leave out codex/rebase-bda-compact-3655, which keeps the two selections.

@github-actions

github-actions Bot commented Oct 14, 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
#4976 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp L3095-3096, L3120, L3129
core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp L207-223, L225-226

@oneandonlydean
oneandonlydean merged commit 3d4ef27 into boykopovar:main Oct 14, 2026
17 checks passed
TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
Adopt EdouardSence's page-table lookup and bounded byte-read loops.
Keep the original order of header, range and address fault checks while
using phi search bounds and one inlined lookup per byte-read loop.
Preserve the local written-page tracking fix on the game control branch.
Source: boykopovar#3655
Donor commit: 2d3a970
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.

8 participants