Skip to content

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

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
EdouardSence:fix/generic-gpu-formatted-load
Oct 14, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
EdouardSence:fix/generic-gpu-formatted-load

Conversation

@EdouardSence

Copy link
Copy Markdown
Contributor

What

Follow-up to #3655, for the same stop: Ghost of Yōtei (PPSA26344) compute shader 0x8000289300 exhausts memory in vkCreateComputePipelines on RADV, on main a11c8f7 with #757/#758 merged. The diagnosis is in discussion #2884.

#2349 moved a GPU-selected formatted access into one shared DontInline function per shape, which is enough on NVIDIA. RADV inlines every function, so each of the module's 128 buffer_load_format call sites still gets a copy of all 77 format cases: 31k instructions for load_formatted_gpu_buffer_x4.

For loads, formatSwitch with a GPU-selected V# no longer emits the 77 cases:

  • A Private constant table holds, per IrBufferFormat, the component count, the packed flag, the type, the width, byte offset and bit offset of each component (3 words per format, built from GetFormatInfo).
  • One path then runs formatted()'s steps for 4 components, with componentCount and the widths read at run time:
    • selectors and required, as there;
    • address()'s bounds, through a new byteAddress that takes the offset and size as values;
    • the overflow fault and stop, only for components the format has;
    • one dword read, or one byte read of the component's width (EmitBdaReadBytes, a loop over the bytes, from fix(shader): keep each BDA page-table walk to one inlined copy per read #3655);
    • the bitfield extract;
    • the existing NormalizeFormatComponent / EmitD16FormatComponent of each (type, width) pair a format uses at that component, picked with OpSelect;
    • the swizzle and constant outputs.
  • A format of 0 or above 77 records InvalidDescriptor and stops, as the switch's default did.
  • Stores and loads through a prepared V# keep the switch. A prepared V#'s format is a specialization constant, so the switch folds there.

The conversions are the same functions with the same type and width, so a load returns what the matching switch case returned.

Results for 0x8000289300, on top of #3655:

  • Module: 928,775 → 701,211 SPIR-V words. load_formatted_gpu_buffer_x4: 31k → 3.9k instructions.
  • RADV pipeline: out of memory above 26 GB → 35.8 s and 8.3 GB peak RSS. Measured outside the title with a Vulkan harness, MESA_SHADER_CACHE_DISABLE.
  • Yōtei: a 300 s run (4711 shaders, 53,900 dispatches) no longer gets OOM-killed. Without this PR and with fix(shader): keep each BDA page-table walk to one inlined copy per read #3655, the module still goes past 10 GB.

Tested

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

  • 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 buffer format execution tests run on the GPU.
  • agc_formatted_buffer_once now requires a GPU-selected formatted load to stay under 40,000 SPIR-V words. On main it is 112,835 and the test fails; here it is 16,854.
  • tools/check_conventions.py passes.

#2837 also adds SpirvModule::DefineInitializedGlobalVariable, with the same signature, so one of the two will need a rebase.

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: fix(shader): keep each BDA page-table walk to one inlined copy per read #3655
  • AI-assisted: yes

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

🤖 Generated with Claude Code

@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 table approach reads equivalent to formatted() (selectors, required, bounds, address-overflow fault, sign extension and the per-(type, width) conversions all match). The size test fails without the change, as intended. One thing blocks this:

  • No test pins the values of the new path. agc_driver_buffer_format (tests/execution/BufferFormat.cpp) only uses prepared V#s, where the format is a specialization constant and the switch folds, and FormattedBufferOnce.cpp only counts SPIR-V words. A wrong table entry (a swapped bit offset, a mis-tagged type) would pass everything. Please add an execution test that loads through a GPU-selected descriptor (V# not resolvable at registration) and compares against the expected values for every format the switch handled (at least one per type/width pair: UNORM/SNORM/UINT/SINT/USCALED/SSCALED/FLOAT 8/16/32, the packed ones, 1-4 components, d16, selectors 0/1/4+, a component past the buffer end, format 0 and 78).

Also: this contains #3655's commit; it can only land after #3655, so please keep the Depends on and rebase once it merges.

@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).

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

Copy link
Copy Markdown
Contributor

The equivalence claim here is structural, and I checked the old shape rather than take it on trust.

On main the 77 cases are not 77 hand-written conversions. The switch (SpirvRuntimeBuffer.cpp:359-377, formatSwitch at :351) builds an OpSwitch over the GPU-selected format in which every case is the same call:

formatted(context, instruction, resource,
          GetFormatInfo(static_cast<IrBufferFormat>(index + 1u)), components, store, data);

One formatted() with a different GetFormatInfo constant per case (:371). The new table is built from GetFormatInfo as well (formatTable). So the generic path is the same function carrying the same per-format data, selected at run time instead of by an OpSwitch — which is what makes "a load returns what the matching switch case returned" checkable rather than hopeful, and it is the line worth quoting for whoever merges this.

The contract the switch's default carried is preserved, verified on main. For a GPU-selected descriptor the invalid default records InvalidDescriptor (:379, inside the if (memory.gpuDescriptor) guard), and the post-merge unsupported test — format 0, or above the count of 77 — stops the invocation (:388-389). Both are guest-visible on a bad V#, so that contract is the part that had to survive, and the new path keeps both under the same conditions.

The scoping is what keeps the risk bounded, and it is worth stating in the body. Only the GPU-selected-V# load path goes generic (if (!store && memory.gpuDescriptor)). Stores and loads through a prepared V# keep the switch, where a specialization-constant format lets it fold, and a GPU-selected load whose format is a compile-time immediate is handled earlier still (:342) and never reaches the switch at all. The change is confined to the path that actually blew up.

The word budget is the right shape of guard. agc_formatted_buffer_once requiring under 40,000 SPIR-V words — main at 112,835, this at 16,854 — is a deterministic RED whose failure mode is exactly the regression that matters, and nothing else in the suite would notice the module growing back.

One gap I can name from the tree. That same test is the only one that reaches the GPU-selected formatted-load path, and it is compile-only — it counts emitted words and never creates a device, so it can show the module shrank but it cannot show a load still returns the right value. The execution tests that do dispatch formats (tests/execution/BufferFormat.cpp, and BufferLoadStore.cpp's CheckRuntimeDescriptors, whose cases use formats 5/11/18/20 — one- and two-component ones) take the prepared-descriptor shape, which this PR leaves on the switch; their descriptors come from host-filled buffers, so the tracker validates them instead of treating them as GPU-selected. The generic path is therefore covered for size and not covered for behaviour — and the run-time component count is precisely what the switch never had. A GPU-selected dispatch that runs a one- and a two-component format is the test I would add.

Two things worth writing down before merging.

Not verified here: I have no RADV device, so the module-size and pipeline figures are yours. What I could check is the emitter's shape, the preserved fault paths, the table's provenance, and this tree's test plumbing. The import_audit worktree failure matches what this tree does on main, so taking your note at face value it is not something this PR introduces.

Written with AI assistance.

@EdouardSence

Copy link
Copy Markdown
Contributor Author

@adriadam10 thanks. Added agc_driver_gpu_buffer_format in 6aaae03 (tests/execution/GpuBufferFormat.cpp).

The shader loads the V#'s NUM_RECORDS and word3 from a buffer and moves them into SGPRs with v_readfirstlane, so the descriptor is GPU-selected and every load goes through the table. One compile, then one dispatch per case. Two lanes each read 16 bytes (signed and unsigned extremes, f16 ±1/±inf, f32 NaN and denormal).

  • buffer_load_format_x, _xy, _xyz, _xyzw and buffer_load_format_d16_xyzw, for every format 1-77 with identity selectors.
  • Selectors 0, 1 and 4-7 (mixed and reversed) on 8_8_8_8_UNORM, 8_8_8_8_UINT, 32_32_FLOAT and 32_FLOAT.
  • NUM_RECORDS = 22 on 32_32_32_32_FLOAT, 16_16_16_16_FLOAT, 32_32_FLOAT and 8_8_8_8_UNORM, so lane 1 has components past the end (x alone stays in bounds, xyzw does not).
  • Formats 0 and 78: the invocation stops before its stores (fault reason 8 in the log).

The expected values were captured from the switch at 2d3a970 (#3655 only). They are identical, bit for bit, to the table's output. Corrupting one bit offset in formatTable makes the test fail on format 30 (0x3f701e04, expected 0x3f603c08). I also checked a few values against a hand computation: 8_8_8_8_UNORM 1/255 → 0x3b808081, 127/255 → 0x3efefeff, 16_SNORM -255/32767 → 0xbbff01fe.

Linux, RX 9070 XT, Mesa 26.2.4 RADV: the new test passes, and check_conventions.py --base origin/main passes. Still Depends on #3655. I will rebase once it merges.

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

@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).

@SleglJan

Copy link
Copy Markdown
Contributor

NVIDIA cross-test of #3655 and #3656 (2d3a970, 369928d, 6aaae03) on 007 First Light's compute shaders, run offline.

Setup. RTX 3080, NVIDIA 615.71.09, CachyOS, GCC 16.2.1, Release. The three commits cherry-picked without conflicts onto a local, unpublished 007 stack: main 62f8758 + 82 commits, not current main.

Method.

  • The 151 compute requests were dumped during one 007 level load.
  • I replayed them with agc_shader_replay --spv, built separately at each head.
  • I timed pipeline creation in a standalone Vulkan program: vkCreateShaderModule + vkCreateComputePipelines, __GL_SHADER_DISK_CACHE=0.

Results.

  • None of the 151 modules has a GPU-selected formatted load, so 369928d changes none of them.
  • 2d3a970 shrinks the 17 modules that read through get_bda_pointer, by 828 to 1,491 words each, all of it in the BDA helpers. The other 134 modules are byte-identical. Across all 151: 42,246,335 → 42,225,713 words (−0.05 %).
  • Pipeline creation time doesn't change measurably. The 17 changed modules take 4.20 s before and 4.12 s after. Each module ran once, six of them twice (means used). The eight largest modules, 1.49M to 4.83M words, are byte-identical at both heads, and their totals show the noise: 494 s before, 497 s after.
  • Tests. As a Linux/Ampere complement to Scramblrrrr's Windows/Blackwell run: 28/28 tests matching agc_shader|recompiler|spirv|bda|format pass, including agc_driver_gpu_buffer_format. Without the PRs, 27/27 pass.

Neutral for 007 on NVIDIA.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@blawesom

Copy link
Copy Markdown
Contributor

Rebase handoff + one open finding — rebased onto current main, authorship unchanged. One test-file conflict resolved (kept this PR's 40000-word bound check and main's extended vertex-specialization summary line together).

The finding: after the rebase, agc_formatted_buffer_once fails deterministically — and it's a semantic conflict with main's newer work, not a mechanical rebase issue:

formatted buffer once: a formatted load from a known descriptor kept every format's decoder
(26501 words, a GPU-selected descriptor needs 16854)

The failing check is main's (from 56d5a4985, "read the formatted buffer helper's V# words as specialization constants when the format is known"): it expects the known-descriptor path to stay under half the GPU-selected path's size. This PR's format-table decode makes the GPU-selected path much smaller (16854 words), so the ratio inverts — the known path (26501) is now the larger one. Main's specialization work and this PR's decode change need to meet: either the known-format path should specialize on top of this PR's table decode, or main's ratio check needs recalibrating for the new baseline. I did not adjust the check, since which side should give is a design call.

Everything else: builds, conventions pass, full suite 953/954 (only this test).

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/3656
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is ed64f3a6e.

@Dextroid17

Copy link
Copy Markdown
Contributor

The check is a relative bound, so the calibration is the side that should give — the decode does not need to. On main, core/libs/prx/libSceAgcDriver/tests/FormattedBufferOnce.cpp:98 is:

Check(one.words < gpuOne.words / 2u, "formatted buffer once: a formatted load from a known descriptor kept every format's decoder (… words, a GPU-selected descriptor needs …)");

one is the known-descriptor load and gpuOne the GPU-selected one. The message says what the assertion means: gpuOne is the stand-in for a path that still contains every format's decoder, and half of it is a proxy for "this path did not keep all of them". Nothing in the file pins the known path's absolute size — line 97 does the same thing on the vertex pair with a different margin (1.5x) — so the GPU-selected path is the yardstick, not a subject of the test.

That is why your finding inverts rather than fails: once the table decode takes the GPU-selected path to 16854 words, the yardstick shrinks with it and the ratio stops measuring what its message claims. The resolution that keeps the check's meaning is to compare the known path against something that still keeps every decoder (or against an absolute bound), not to hold the decode back — the decode is the point of the PR. Which side writes that recalibration is @EdouardSence's call with @adriadam10, but it should not be settled by weakening the thing under measurement.

Written with AI assistance.

@Dextroid17

Copy link
Copy Markdown
Contributor

lavapipe measurement: with #3655 + #3656, pipeline creation for one Ghost of Yōtei compute shader (0x8000289300) goes from killed at an 8 GB cap to a 1.9 GB peak.

What was tested. blawesom's rebase: blawesom:rebase/3656 @ ed64f3a6e. It is based on 33ae282e4, which predates #4759 and #5022, and carries #3655 as 805be46cf. I merged it locally onto main 0aa4cb1c2. The merge is clean, but no pushed commit matches the tree I tested.

#5022 is preserved. load_bda_span and the selection in read_bda_span are exactly as #5022 left them; load_bda_span is still a DontInline function that makes no calls. In SpirvBdaRead.cpp the diff changes only the fallbacks: the per-byte read and read_bda_span's per-dword fallback both become loops. It also adds EmitBdaReadBytes for the format-table path.

Setup. lavapipe 26.2.4, CachyOS, GCC 16.2.1, Release build, SPIRV-Tools OFF, clean environment with VK_ICD_FILENAMES set to lavapipe. This is not the CI configuration: CI uses Ubuntu's mesa-vulkan-drivers with SPIRV-Tools ON.

Method. I built agc_shader_replay --spv at both heads and replayed shader_8000289300.req, captured from a GoY run. A small standalone program then runs vkCreateShaderModule + vkCreateComputePipelines once, under systemd-run --user --scope -p MemoryMax=8G -p MemorySwapMax=0.

                         main 0aa4cb1c2              + #3655/#3656 (ed64f3a6e)
SPIR-V words             1,035,274                   807,714
pipeline creation        killed (exit 137) at 8 GB   32.9 s, peak 1,911 MB
                         3.4 GB at 5 s, 6.0 GB at 10 s

Why memory drops more than module size. Module size isn't what drives memory. Mesa's vk_spirv_to_nir always runs nir_inline_functions (src/vulkan/runtime/vk_nir.c, mesa-26.2.4). nir_functions.c keeps functions only for MESA_SHADER_KERNEL with driver functions. So on lavapipe every helper is copied into each call site, and DontInline doesn't survive. Static sizes after full inlining, counted on the two replayed modules:

                                 main          this PR
load_formatted_gpu_buffer_x4     1,663,866     8,119     (called 112x)
load_formatted_gpu_buffer_x1     1,656,741     6,803     (called 16x)
read_bda_span_stop                  12,680       761
entry point, fully inlined     213,311,343  1,207,088    (177x smaller)

One call is already too many on main. I kept N calls to the helpers and replaced the rest with OpUndef; each module passes spirv-val. Results on main's own module:

  • No calls: 515 MB, 17 s.
  • One _x4 call: 6.2 GB, 50 s. One _x1 call gives the same.
  • Two calls: killed at 8 GB.

So cutting call sites wouldn't have been enough. The helper body itself had to shrink, and that's what this PR does.

Focused tests at the merged head, on lavapipe only: 11/11 pass.

  • This PR's tests: agc_formatted_buffer_once, agc_driver_gpu_buffer_format, agc_driver_buffer_format, agc_shader_memory (including the "BDA read functions" check) and agc_driver_bda_device.
  • The four tests named on fix(shader): keep each BDA page-table walk to one inlined copy per read #3655: agc_driver_scalar_wide_loads, agc_driver_flat_apertures (the whole binary, which includes aperture 0), agc_driver_bda_span_reads and agc_driver_pixel_interlock.
  • Also agc_driver_flat_loads and agc_driver_global_loads.

agc_driver_gpu_buffer_format, the test the review asked for, now passes on RADV, on both NVIDIA cross-tests and on lavapipe.

The agc_formatted_buffer_once failure from the rebase handoff doesn't reproduce once the branch is merged onto 0aa4cb1c2. The known-descriptor load is 1,947 words (2,349 for 3 loads), down from blawesom's 26,501. The GPU-selected load is 16,398 (17,254 for 3 loads). That is well inside the /2 bound, so the recalibration I suggested earlier isn't needed. Something on main since 33ae282e4 shrank that path; I haven't bisected it.

Not covered: NVIDIA 595+ (with and without SPIRV-Tools, as asked on #3655), RADV, a SPIRV-Tools ON build, and a full GoY game run. This is one shader from one title, with focused tests rather than the full suite.

What would unblock this:

  1. fix(shader): keep each BDA page-table walk to one inlined copy per read #3655 needs a head on top of fix(shader): load a BDA span in place from its own function #5022, plus NVIDIA 595+ runs.
  2. Then this PR's head (6aaae039, conflicting) needs updating, for example by moving it to rebase/3656 and merging main.
  3. Then a re-review.

AI-assisted: yes (Claude).

@Dextroid17

Copy link
Copy Markdown
Contributor

Follow-up: a full Ghost of Yōtei run on lavapipe with these PRs is no longer killed at the memory cap.

Earlier runs (without these PRs). The game was killed (exit 137) under a 22 GB cap. In an instrumented rerun, the last pipeline creation that started and never finished was a 4.1 MB compute shader. Replaying 0x8000289300's captured request on that run's tree gives a byte-identical module, so it is the shader from my comment above.

This run (with these PRs merged in). Same title, launch scope and 22 GB cap, with blawesom:rebase/3656 (ed64f3a6e) merged in. It ran to the 900 s timeout and was not killed. It compiled 1,661 shaders. Peak sampled RSS of the game process (every 5 s) was 15,103 MB. Over the last 7 minutes it moved up and down between 12,818 and 15,103 MB.

Caveats.

  • The tree is my local run branch: an older base, 566 commits behind main, plus local run commits. It is not main.
  • The two runs are not a controlled A/B. The earlier runs used a warm shader cache and an unoptimised build with extra counters; this one is a Release build that compiled cold.
  • The new log does not name the pipeline, so "gets past this shader" is inferred from the run reaching the timeout, not observed.
  • The scope allowed up to 8 GB of swap, and swap use was not recorded.
  • Near the end of the log one lavapipe GPU batch reported "still running on the GPU after 35 s", and on teardown after the timeout the process aborted (double free or corruption (out)). No run without these PRs got this far, so I can't attribute either yet; I'm looking at them separately.

lavapipe 26.2.4, SPIRV-Tools OFF. Details of the shader-level measurement are in my comment above.

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 oneandonlydean self-assigned this Oct 14, 2026
EdouardSence and others added 2 commits October 15, 2026 10:32
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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

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

Copy link
Copy Markdown
Collaborator

Rebased onto main now that #3655 is in; tests pass.

@oneandonlydean
oneandonlydean force-pushed the fix/generic-gpu-formatted-load branch from 6aaae03 to f1c786d Compare October 14, 2026 22:15
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 14, 2026
@oneandonlydean
oneandonlydean dismissed adriadam10’s stale review October 14, 2026 23:14

Met: f1c786d pins the GPU-selected formatted load values for every buffer format.

@oneandonlydean
oneandonlydean merged commit 6c854ae into boykopovar:main Oct 14, 2026
8 checks passed
@EdouardSence

Copy link
Copy Markdown
Contributor Author

Thanks @oneandonlydean for the rebase and merge, and @Dextroid17 / @blawesom for the lavapipe numbers and the conflict report.

For the record, the recalibration Dextroid17 described was not needed on the final rebase. I rebased the same two commits onto a050f856f locally, and main's unchanged FormattedBufferOnce check passes there: 1947 words for 1 load, 2349 for 3; GPU-selected 16398 and 17254, so one.words < gpuOne.words / 2u holds (1947 < 8199). The 26501 in blawesom's report came from a rebase on an older base (33ae282e4, see Dextroid17's comment).

On that tree, 1172/1188 ctest pass on an RX 9070 XT (RADV, Mesa 26.2.4); 14 are skipped. The two failures, agc_host_import_sparse and agc_driver_tessellation (a segfault inside RADV during LinkPipelineFromLibraries), fail the same way on a050f856f without this PR.

Tested and written by Claude (AI assistant) on a contributor's machine; Gemini ran the build and full ctest. AI-assisted: yes.

TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
Adopt EdouardSence's compact table for GPU-selected formatted loads.
Retain prepared-descriptor loads and stores on their existing format
switch, with the local direct-buffer byte-write path preserved.
Keep descriptor, bounds, address-overflow and BDA fault checks intact.
Source: boykopovar#3656
Donor commit: 369928d
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.

7 participants