Skip to content

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

Closed
nikowolf123 wants to merge 1 commit into
boykopovar:mainfrom
nikowolf123:fix/shader-runtime-s-buffer-load-vsharp
Closed

nikowolf123 wants to merge 1 commit into
boykopovar:mainfrom
nikowolf123:fix/shader-runtime-s-buffer-load-vsharp

Conversation

@nikowolf123

@nikowolf123 nikowolf123 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

s_buffer_load_dword* through a V# that the recompiler reads at run time handled that V# the way vector buffer accesses do. This is the gpuDescriptor path: a V# dword comes from a value the SRT walker cannot evaluate when it captures resources, for example v_readfirstlane_b32 of a loaded VGPR. It went wrong in two ways, both in descriptor() in SpirvRuntimeBuffer.cpp (line numbers on main 0f2683c):

  • TYPE. Line 72 marks the V# invalid when word 3 bits 31:30 (TYPE) are not 0. Line 85 then records an InvalidDescriptor BDA fault, which also stops the invocation when BDA faults stop invocations, and line 88 makes the load return 0. After the dispatch the driver reports BDA access failed: ... reason=8.
  • Base alignment. Line 70 builds the 48-bit base from dword 0 and dword 1 bits 15:0 and keeps bits 1:0. Line 597 adds the dword-aligned offset to it. So a base 1 to 3 bytes past a dword boundary read 4 bytes starting at that unaligned address.

Sources:

  • ISA: "RDNA 2" Instruction Set Architecture Reference Guide, 30 November 2020 (https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf, sha256 22aec03f2706dba2e148ffde1f3a25d5cdd42c71a96f95240aeb48d995d44795).
    • Section 7.1, Table 27, field SBASE (page 51): BUFFER instructions read only base, stride and num_records from the resource constant.
    • Section 7.2.1, "Reads using Buffer Constant" (page 52): the buffer constant's other fields are ignored. The two low bits of V#.base and of the final address are ignored, which forces dword alignment. The pseudocode builds the access from m_base, m_stride and m_num_records only and never reads word 3.
  • Hardware measurements already on main, both on RDNA2 (gfx1035):
  • The capture-time evaluator (SrtEvaluator.cpp:132-149) already follows both rules: it reads word 3 but never checks TYPE, and it reads at ((base & ~3) + byteOffset) & ~3. The bound path drops the two low bits (baseMisalignment returns 0 for a scalar buffer, SpirvRuntimeBuffer.cpp:145), but it still rejects a nonzero TYPE when the resource is materialized (ResourceMaterializer.cpp:590).
  • Mesa ACO at mesa/mesa@65b4a55: smem_buffer_load_callback in src/amd/compiler/instruction_selection/aco_select_nir_intrinsics.cpp:487-534 passes the V# to s_buffer_load_* unchanged and does not state either rule. I found no LLVM or Mesa text that states them, so the ISA guide is the source.

What changed:

  • descriptor(): for a ResourceKind::ScalarBuffer access, the base takes dword 0 with bits 1:0 cleared.
  • The TYPE check and its fault are not emitted for scalar buffer loads. invalid is ConstantBool(state, false), as in the formatted path of the same file.
  • valid is still base != 0. A base of 1 to 3 (with zero high bits) is now the same V# as base 0 and reads 0 like a null base. Before, it read from guest address 1 to 3 plus the offset.
  • Vector, formatted and atomic accesses through a runtime V# keep the TYPE check.
  • The pseudocode line addr = (m_base + offset) & ~0x3 taken alone would differ when the low bits of the base and the offset carry. This change keeps (base & ~3) + (offset & ~3), which matches the section's text and the gfx1035 measurement.
  • docs/dev/debt/shader.md: a new entry follows "V# bases off a DWORD boundary". It says the TYPE part is not measured, and that vector accesses through such a V# still read 0 for a nonzero TYPE.

#5049 applies the same word 3 rule on the bound path (ResourceMaterializer.cpp). This PR changes a different file, and git merge-tree merges the two cleanly. #5049 reports that the captured PPSA26358 scene shader 0x12c2298400 stopped on the bound path's buffer descriptor uses an unsupported type. I have no capture that reaches this runtime path, so no title is claimed here.

Tested

Built and run on main c827e11; the later main commits up to 0f2683c change only CI workflows, README.md and tools/progress*.py. Linux (WSL2 Ubuntu), GCC, Release, configured like CI (-DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON, ccache). Tests ran on lavapipe (llvmpipe, LLVM 21.1.8, 256 bits) with VK_ICD_FILENAMES=<lvp_icd>.json ANYPS5_REQUIRE_VULKAN=1 GALLIUM_OVERRIDE_CPU_CAPS=avx.

New test ScalarBufferGpuDescriptor.cpp (agc_driver_scalar_buffer_gpu_descriptor):

  • The shader loads a V# with buffer_load_dwordx4 and moves it to SGPRs with four v_readfirstlane_b32, so the load takes the gpuDescriptor path. It then runs s_buffer_load_dword at offset 0 and s_buffer_load_dwordx2 at offset 4. It runs in wave32 and wave64 with 64 threads.
  • Nine V#s are tested: base +0 to +3 with TYPE 0; TYPE 1, 2 and 3; word 3 all ones; and base +3 with TYPE 2.
  • Each V# points into its own 64-byte region of a GuestAllocations block, so a stale V# would also fail. Every thread's three dwords must equal the dwords at the region's aligned start.
  • One compiled shader serves all nine V#s per wave size. With lavapipe's shader cache disabled (MESA_SHADER_CACHE_DISABLE=true) the test takes 3.3 to 3.7 s.
  • It uses only the dword and dwordx2 forms because each BDA dword read is slow to compile on lavapipe: a shader with 15 such reads took 16 to 25 s to compile cold, depending on the machine's load. The x4, x8 and x16 forms are translated into the same per-dword reads.

Results for the new test with each version of SpirvRuntimeBuffer.cpp (on main c827e11):

  • main's file: fails with "wave32, base +1 (word 3 0x31016fac): thread 0 s_buffer_load_dword is 0xefc69d74, expected 0xc69d744b".
  • Only the TYPE change: fails the same way.
  • Only the base mask: fails with "type 1 (word 3 0x71016fac): thread 0 s_buffer_load_dword is 0xdeadbeef, expected 0x9e754c23", after "BDA access failed: ... reason=8".
  • The full change: passes.

Other runs:

  • ctest -j4 -R 'agc_shader|agc_driver|shader|recompiler': 384 tests, 348 passed, 36 skipped, 0 failed.
  • Full ctest -j4: 1053 tests, 1010 passed, 43 skipped, 0 failed.
  • Unmodified main c827e11 in the same tree: 383 tests, 347 passed, 36 skipped in the filtered run; 1052 tests, 1009 passed, 43 skipped in the full run. Per-test statuses are identical apart from the new test.
  • git merge-tree against every open PR that touches SpirvRuntimeBuffer.cpp or docs/dev/debt/shader.md (29 PRs, perf(recompiler): select guest memory loads instead of branching around them #4048 among them): this branch adds no conflict that the PR does not already have with main.
  • tools/check_conventions.py --base origin/main (main's checker) and git diff --check: clean.
  • Not built on Windows.

This was not measured on AMD hardware. A gfx103x reviewer can check both rules with the hardware oracle, from the repository root:

cat > body.s <<'EOF'
  v_readfirstlane_b32 s12, v4
  v_readfirstlane_b32 s13, v5
  s_add_u32 s8, s4, 0x200
  s_addc_u32 s9, s5, 0
  s_add_u32 s8, s8, s12
  s_addc_u32 s9, s9, 0
  s_and_b32 s9, s9, 0xffff
  s_movk_i32 s10, 0x40
  s_mov_b32 s11, s13
  s_buffer_load_dword s14, s[8:11], 0x0
  s_buffer_load_dwordx4 s[16:19], s[8:11], 0x4
  s_waitcnt lgkmcnt(0)
  v_mov_b32 v10, s14
  v_mov_b32 v11, s16
  v_mov_b32 v12, s17
  v_mov_b32 v13, s18
  v_mov_b32 v14, s19
EOF
python3 - <<'EOF'
import sys
sys.path.insert(0, "tools/hw-oracle")
from hw_oracle import run
body = open("body.s").read()
for offset in (0, 1, 2, 3):
    for word3 in (0x31016fac, 0x71016fac, 0x91b00f2e, 0xc0000000, 0xffffffff):
        out = run(body, [(offset, word3, 0, 0)], bytes(range(1, 65)), ieee=0, dx10_clamp=1,
                  denorm32=0, denorm16=3, round32=0, round16=0, fp16_overflow=0)[0]
        print(offset, f"{word3:08x}", " ".join(f"{v:08x}" for v in out[:5]))
EOF

How the kernel works:

  • It runs one row in wave32, padded to 32 rows, so the 64 extra bytes (1 to 64) start at s[4:5] + 0x200.
  • It builds a V# whose base is that address plus the row's offset, with stride 0, num_records 64 and the row's word 3.
  • It loads a dword at 0 and a dwordx4 at 4.

Per the ISA, all 20 lines end in 04030201 08070605 0c0b0a09 100f0e0d 14131211. If base bits 1:0 were used, the offset 1 to 3 lines would be shifted by that many bytes. If SMEM used TYPE, the lines with a nonzero TYPE (71016fac, 91b00f2e, c0000000, ffffffff) would differ from the 31016fac lines.

Native Windows 11, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 5080 (driver 616.92), on this branch rebased onto main 0f2683c (86fdb52): agc_driver_scalar_buffer_gpu_descriptor passes 3 of 3 runs, and ctest -R 'agc_shader|agc_driver|shader|recompiler' passes 383 of 383.

Run by @kr4y4 on Windows 11 on an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790), head 86fdb52 merged into main 3b94f92 (comment): agc_driver_scalar_buffer_gpu_descriptor passes 3 of 3 runs on both GPUs and the full suite 1087 of 1087; with SpirvRuntimeBuffer.cpp put back to main the test fails on both with the same message as on lavapipe.

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
  • Unblocks: none
  • GPUs tested: lavapipe (Mesa 26.0.8, llvmpipe) on WSL2 Ubuntu 26.04; NVIDIA GeForce RTX 5080, driver 616.92, Windows 11
  • AI-assisted: yes. Written with Claude Code; I reviewed every line and ran the tests above.

🤖 Generated with Claude Code

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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added shader core/shader docs agc libSceAgc* labels Oct 13, 2026
@kr4y4

kr4y4 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

First Windows build and hardware run. It passes on both GPUs, and without the fix it fails with the same message as on lavapipe.

Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790). Both have 32-lane subgroups. The head 86fdb52 merged into main 3b94f92.

  • agc_driver_scalar_buffer_gpu_descriptor passes 3 of 3 runs on both GPUs. The full suite passes 1087 of 1087 on both (7 skipped on NVIDIA, 34 on Intel).
  • With SpirvRuntimeBuffer.cpp put back to main, the test fails on both GPUs with wave32, base +1 (word 3 0x31016fac): thread 0 s_buffer_load_dword is 0xefc69d74, expected 0xc69d744b.

Written with AI assistance.

@nikowolf123

Copy link
Copy Markdown
Contributor Author

Thank you for the NVIDIA and Intel runs; I added them to the description.

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

Copy link
Copy Markdown
Contributor Author

Closing this following the request on #4926 to hold off on pull requests that do not name a title needing the change while the queue is this long. The branch stays; if a title stops on this on current main, I will reopen with the title ID and the log line.

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

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants