Skip to content

rdna2spirv: MUBUF loads to LDS, buffer_load_{dword,ubyte,sbyte,ushort,sshort} ... lds (SHADER-MUBUF-LDS-DMA) - #2168

Open
vinceychu wants to merge 1 commit into
ProjectLeonida:mainfrom
vinceychu:shader-mubuf-lds-dma
Open

vinceychu wants to merge 1 commit into
ProjectLeonida:mainfrom
vinceychu:shader-mubuf-lds-dma

Conversation

@vinceychu

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-MUBUF-LDS-DMA (#1608)

What and why

The translator refused the lds modifier on MUBUF loads (raise Unsupported(op + " form")). With it, RDNA2 loads go
straight to LDS ("Memory Buffer Load to LDS", RDNA2 ISA 8.1.9), the way compilers stage data into shared memory.

  • Forms: buffer_load_dword, buffer_load_ubyte, buffer_load_sbyte, buffer_load_ushort, buffer_load_sshort
    with lds, in compute and mesh shaders.
  • The value goes to LDS, not a VGPR, at LDS_BASE + M0[15:0] + inst_offset + TIDinWave*4 (the ISA figure; TIDinWave
    is added on the LDS path whatever the V#'s ADD_TID_ENABLE says). The VDATA field names the M# and is ignored; the
    llvm-mc spelling omits it, so a three-operand listing is accepted too.
  • The memory read is buffer_address's, bounds-checked exactly like the VGPR forms; an out-of-range read supplies 0,
    which is what the lane writes. Sub-dword values are zero- or sign-extended to the stored dword (the byte/short read
    is shared with buffer_load_sub through a new buffer_sub_value helper).
  • The LDS write runs in active lanes only and goes through lds_access, so it is dropped outside the wave's LDS
    allocation (RSRC2 LDS_SIZE for compute), like the ds_* stores.
  • Still refused, rather than guessed: buffer_load_dwordxN ... lds (not in the ISA's lds subset),
    buffer_load_format_* ... lds, tfe with lds, and other stages.
  • design.md: new contract-table row next to "Sub-dword MUBUF loads".

Tested

  • New GPU fixture runtime/test/shaders/cs_buf_lds.asm (registered in check.py and run_gpu_tests.py): 32 lanes
    load a dword, a ubyte and an sbyte into three M0-based LDS regions, s_waitcnt + s_barrier, then read back
    their own and their neighbour's (t^1) slots. run_gpu_tests.py --filter cs_buf_lds: PASS on an RTX 5090
    (NVIDIA 595.99.02, Ubuntu 26.04) and on the Ryzen 9 9950X's Radeon iGPU (RADV, Mesa 26.0.8)
    .
  • New tests/semantic/test_buffer_load_lds.py (10 tests through spirv_mini_interp): per-lane slots, offset: and
    M0[15:0], the three-operand form, u/s byte/short extension, out of range -> 0, inactive lanes write nothing, the
    out-of-allocation drop, the refused forms.
  • python -I scripts/run_semantic_tests.py tests/semantic: 496 files, 6,343 tests, 0 failed.
  • pytest tests/test_translate.py: 714 passed; the 2 failures are cs_dpp_cmp.asm with LLVM 21, failing on main too
    (tests: the pytest helper takes a text-only sample's lines as its listing, as run_gpu_tests.py does (TEST-TRANSLATE-TEXT-ONLY) #1375).
  • python -m compileall, tests/test_ci_registration.py: clean.
  • Not run: the native hardware oracle (tools/hw_oracle) — the only RDNA2 GPU here drives a display.

GPUs tested: NVIDIA GeForce RTX 5090 (595.99.02, Ubuntu 26.04.1); AMD Radeon iGPU of the Ryzen 9 9950X (RADV, Mesa 26.0.8)

AI-assisted: yes (Claude Code, with a Devin draft reviewed against the ISA, rebased and GPU-tested)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RAL8MMXUuNpQAVa1iiyAMP

@real-rusher

Copy link
Copy Markdown

Verified on main a73a57e (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly into main. With this PR: test_buffer_load_lds 10 OK and test_buffer_load_sub 10 OK (unittest discover in tests/semantic). test_buffer_load_lds.py against main's translator: 19 errors, so it fails on main and passes with the change. compileall clean.

Not run: the whole semantic suite and the translated / spirv-val counts over a corpus (no game data here).

Note for review: it conflicts with #2164 (tools/rdna2spirv.py and tests/semantic/test_buffer_load_sub.py), so whichever lands later needs a rebase.

AI-assisted: yes (Claude Code)

@Tauwni

Tauwni commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Tested 462137455 on an Apple M3 (macOS 26.5.1) with the three Vulkan drivers there. I ran the full scripts/run_gpu_tests.py in auto mode, against main a73a57e13, with test programs built from main.

Driver main this PR
MoltenVK 1.4.2 195 pass, 70 skip 196 pass, 70 skip
MoltenVK, LEONIDA_W64_EMULATE=1 229 pass, 36 skip 230 pass, 36 skip
KosmicKrisp (Mesa 26.3.1) 190 pass, 73 skip 191 pass, 73 skip
llvmpipe (Mesa 26.3.1) 177 pass, 90 skip 177 pass, 91 skip
llvmpipe, LEONIDA_W64_EMULATE=1 212 pass, 55 skip 212 pass, 56 skip

The new cs_buf_lds passes on MoltenVK (native and emulated) and on KosmicKrisp, with the same eight words: 1592590336 128 4294967168 1592590337 1592590337 129 4294967169 1592590336. llvmpipe skips it (subgroup 4 is narrower than 32 lanes). The failures are the same on both sides, none new: cs_fma_fused (#1724), cs_imgatomic_ext (#1518) and ps_ssharp (#1774) on MoltenVK; cs_bvh/cs_bvh32, ps_cndmask_sign_mods and ps_valu16_mad_sel (KosmicKrisp's (NAN) constants, #2229) on KosmicKrisp; cs_imgatomic_ext on llvmpipe.

AI-assisted (Claude Code). No game data.

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

macOS arm64 / GPU check of #2168 (SHADER-MUBUF-LDS-DMA). A translator rule plus a GPU fixture; both run here on two drivers.

Tested head 462137455 merged onto main bdbc7e923 as merge cf10651bf (clean, no conflicts).

Environment: Apple M3 Pro, arm64, macOS 27.2.1, Apple clang 21.0.0, CMake 4.5.0, CPython 3.15.2, llvm-mc 23.1.5, SPIRV-Tools v2026.4, MoltenVK 1.4.0, KosmicKrisp = Mesa 26.3.0.

The fixture runs and the controls bite

  • cs_buf_lds on the merge, MoltenVK and KosmicKrisp, identical output on both: 0x5eed0000 0x80 0xffffff80 0x5eed0001 0x5eed0001 0x81 0xffffff81 0x5eed0000 - dword/ubyte/sbyte at M0 64/192/320 in their own lanes, and lane 0 reading lane 1's dword slot and back. PASS 1/1 both times, which is the per-lane address and the M0[15:0] + TIDinWave*4 term both being exercised.
  • Full suite on the merge, MoltenVK: 279 shaders: 278 translated, 278 pass spirv-val, 196 / 3 / 70 of 269, the same three failures as main's baseline on this driver (cs_fma_fused, cs_imgatomic_ext, ps_ssharp). The single added pass over the baseline (195 / 3 / 70 of 268) is the new fixture; ps_ij_computed is still the expected refusal under the f16 fallback in this tree (#2239).
  • Semantic: tests/semantic/test_buffer_load_lds.py 10 tests OK; whole suite 501 files, 6377 tests, 0 files failed (main 500 / 6367, so the new file and its 10 cases, nothing else changed).
  • NC 1 (translator only, fixture and tests kept): FAILED (errors=19) in the new semantic file, GPU FAIL cs_buf_lds missing .spv (translation failed) - the old buffer_load ... lds form refusal.
  • NC 2 (drop the TIDinWave*4 scale, a plausible address bug): semantic file 3 failures; GPU FAIL ... 0x5eed0000 0x80 0xffffff80 0x5eed0004 0x5eed0004 0x84 0xffffff84 0x5eed0000, i.e. the lanes collide and lane 1 reads lane 4's slot.
  • NC 3 (drop the offset:N term from the LDS address only): semantic file 1 failure, so the inst_offset half of the formula is pinned by a test too.

Finding: ms is allowed but gets no LDS bound, unlike cs
buffer_load_lds passes self.lds_declared if self.stage == "cs" else None as the lds_access bound (tools/rdna2spirv.py:2756). For a mesh shader, lds_declared is never set (:18704 is inside the cs block), lds_bound stays None, so lds_access emits the bare OpAccessChain with no range check (:5259-5260). The Workgroup array is sized from ms's own metadata (:18659-18672: PSV0 shared bytes rounded up to a power of two, and only ds_* constant offsets grow it), so a buffer_load_dword ... lds whose M0 points past the declared shared size becomes an out-of-bounds access chain - undefined in SPIR-V - where the console drops the write, which is exactly what the cs path models by bounding to the RSRC2 allocation. M0 is a runtime register, so no constant scan can prove it in range. Either bound the ms write by the array size the translator itself chose (self.lds_dwords, available before translation) or keep lds refused on ms until it is measured; the row says "CS/MS only" and the GPU fixture and all ten semantic cases are compute, so mesh is currently an untested promise. Related docs point: the row quotes LDS_BASE in the address formula but the code uses 0 (:2751-2768); if LDS_BASE is a stage-dependent base and not always 0 (the mesh path is the one to suspect), that should be stated rather than left implicit.

Finding (test gap, not a defect): the out-of-range path is CPU-only
The row's "an out-of-range read supplies 0, which the lane writes to LDS" is tested by tests/semantic/test_buffer_load_lds.py through the interpreter, but cs_buf_lds.asm has no out-of-range lane, and no device can tell us whether RDNA2 writes that 0 to LDS or skips the write. I believe the 0-write reading is right (the MUBUF bounds check zeroes the data before it reaches the LDS path), but it is an assumption like the tfe status one in #2164, and it silently clobbers a neighbour's LDS slot if wrong. A one-lane OOB variant of the fixture (or a tools/hw_oracle probe on gfx1036) would close it.

Nit: for the three-operand listing the translator prepends None only in its local argument list (tools/rdna2spirv.py:2740-2742); ins["args"] still holds [vaddr, srsrc, soffset], so vgpr_dest_registers and read_before_write_vgprs see the vaddr as the destination (vaddr gets marked written and is never counted as a read). No current analysis runs on cs/ms instructions, so nothing is miscompiled today - but if one ever does, this is the same class of operand-model slip as the tfe off-by-one I reported on #2164. Normalising the stored args once, at decode, would make both go away.

Not run: the full corpus (needs a game copy), native RDNA2 hardware, and a mesh-shader draw (no MS fixture and no mesh support in this harness on Metal). The change is translator-only, so no C++ test is affected.

python -I scripts/check_no_game_data.py on all seven changed files: clean.

No game files. AI-assisted.

…,sshort} ... lds (SHADER-MUBUF-LDS-DMA)

The translator refused the lds modifier on buffer loads. RDNA2 ISA 8.1.9
(Memory Buffer Load to LDS): the fetched value goes to LDS, not a VGPR, at
LDS_BASE + M0[15:0] + inst_offset + TIDinWave*4. The read is buffer_address's,
bounds-checked like the VGPR forms (out of range supplies 0), sub-dword values
are zero/sign-extended, and the LDS write runs in active lanes only, dropped
outside the wave's LDS allocation. dwordxN, format, tfe with lds and stages
other than CS/MS stay refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RAL8MMXUuNpQAVa1iiyAMP
@vinceychu
vinceychu force-pushed the shader-mubuf-lds-dma branch from 4621374 to 5e7ab21 Compare November 26, 2026 19:33
@vinceychu

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (it had picked up a conflict in scripts/run_gpu_tests.py with the new cs_dpp_cmpx entry; both kept). After the rebase: cs_buf_lds passes on the RTX 5090 (NVIDIA 595.99.02) and the Radeon iGPU (RADV), same eight words; run_semantic_tests.py tests/semantic 501 files, 6,383 tests, 0 failed. Thanks @Tauwni and @wavehs for the Apple M3 runs.


Generated by Claude Code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants