Skip to content

rdna2spirv: a multi-dword MUBUF access builds each bounds check once - #4477

Open
mrp1no wants to merge 2 commits into
ItsNotRexed:mainfrom
mrp1no:shader-buffer-bounds-reuse
Open

mrp1no wants to merge 2 commits into
ItsNotRexed:mainfrom
mrp1no:shader-buffer-bounds-reuse

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

New SHADER item. Claim: #4421 (comment)

buffer_address(parts=True) computes dword 0's bounds check and returns it as in_b. The raw multi-dword loads (buffer_load_dwordx2/x3/x4) and stores (buffer_store_dwordx*) ignore it, and call parts(0) instead, which builds the same check a second time. With a run-time V#, that is about 10 dead instructions per access.

Inside each check, end_ok also adds offset + end once for mode 0 and once more for mode 3 when the two share the offset (soffset 0).

Commits

  1. rdna2spirv: parts(0) returns in_b, and end_ok keeps one ulong sum per (offset, end).

    • in_b is computed in buffer_address, before any of the caller's branches, so it dominates every use.
    • The checks are the same expressions, just built once.

    With a run-time V#:

    Instruction Lines before Lines after
    buffer_load_dword 90 80
    buffer_load_dwordx4 168 155
    buffer_store_dwordx4 155 142
  2. tests/semantic/test_buffer_bounds_shared.py translates three run-time-V# accesses and checks that no OpIAdd %ulong with the same operands appears twice:

    • buffer_load_dwordx4;
    • buffer_load_dwordx2 with soffset and an offset;
    • buffer_store_dwordx4.

    It fails on all three with main's translator, where the same offset + 4 sum appears up to 4 times in the x4 load.

Tested

  • Corpus sample: shard 0/40 of my own 1.0.2.1 corpus. 663 of 663 shaders translate and pass spirv-val. Total SPIR-V goes from 500.1 to 496.4 MiB (−0.75%); 473 modules are smaller, by up to 5.5%.
  • Buffer, bounds and swizzle tests: all 17 files pass (test_buffer_oob_select, test_buffer_swizzle, test_vector_mem_mubuf, test_buffer_store_rdna2, test_tbuffer_*, …). They check the OOB results per mode, so they back the claim that nothing changes in behaviour.
  • Full semantic suite (7,456 tests): the only failures are the ones main already has. Those are test_loop_ballot_barrier, test_cmp_class_f64, and test_mutations' legacy anchor (tests/semantic: re-anchor the v_mul_legacy_f32 mutation after the flush change #3883).

Independent of #4345, #4391 and @SweatierKey's %active reuse item. Counts only; no data from the corpus. AI-assisted (Claude Code).

🤖 Generated with Claude Code

mrp1no and others added 2 commits November 23, 2026 02:29
buffer_address(parts=True) computed dword 0's bounds check as in_b, and
the raw multi-dword loads and stores ignored it and built the same check
again through parts(0): about 10 dead instructions per access with a
run-time V#. Inside each check, end_ok also added offset + end once for
mode 0 and again for mode 3 when they share the offset (soffset 0).

parts(0) now returns in_b, computed in buffer_address before any of the
caller's branches, so it dominates every use; end_ok keeps one ulong sum
per (offset, end). The checks are the same expressions, built once:
buffer_load_dword 90 -> 80 lines, buffer_load_dwordx4 168 -> 155,
buffer_store_dwordx4 155 -> 142 with a run-time V#.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_buffer_bounds_shared.py translates a run-time-V# buffer_load_dwordx4,
a buffer_load_dwordx2 with soffset and an offset, and a
buffer_store_dwordx4, and checks that no OpIAdd %ulong with the same
operands appears twice. With main's translator it fails on all three (the
same offset + 4 sum appears up to 4 times in the x4 load).

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

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

Ran this at cacbac1c on Windows 11: tests/semantic/test_buffer_bounds_shared.py -> 1 test, OK; compileall and git diff --check clean.

A multi-dword MUBUF access now builds each bounds check once, and the test verifies no sum is emitted twice. No blocking issue.

@leoleahy

Copy link
Copy Markdown
Contributor

Tested cacbac1c on Windows 11 (Python 3.14.8, CPU only, no game files): pass.

  • At HEAD (cacbac1c): python tests/semantic/test_buffer_bounds_shared.py -> 1 test (3 subtests: load x4, load x2 with soffset, store x4), OK.
  • Negative control (base 03b09a08 tools/rdna2spirv.py + HEAD test): 3 subtest failures (duplicate OpIAdd %ulong bounds sums emitted on all 3 cases: 4x/2x on load x4 and store x4, 2x on load x2 with soffset).
  • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_buffer_bounds_shared.py and git diff --check (03b09a08..cacbac1c): clean.
  • Code/dominance review: checked both buffer_address(..., parts=True) callers (buffer_load and buffer_store). Both evaluate raw_parts(i) / per(i) in the unconditional merge chain before each dword's OpSelectionMerge/OpBranchConditional, in_b = check(0) is emitted before any branch, and check_end() uses straight-line OpSelect with no internal branches — so both reusing in_b for k == 0 and caching (x, end_bytes) in ends preserve SSA dominance. No issues found.

AI-assisted.

@Atomic258

Copy link
Copy Markdown

Tested cacbac1c on Linux (Ubuntu), CPython 3.10.12, CPU only, synthetic .asm fixture, no game files: pass.

  • python -I tests/semantic/test_buffer_bounds_shared.py: 1/1 OK.
  • Negative control: on main the test fails — the same offset+end bounds check is emitted up to 4 times in one multi-dword MUBUF store (store x4: {'%t63 %cl4': 4, ...}), which is exactly the duplication this removes.
  • git diff --check clean; compileall clean. spirv-as/spirv-val absent here, so binary validation didn't run.

AI-assisted (Devin).

@theswiss24

Copy link
Copy Markdown

Tested cacbac1c (contains main 03b09a08) on NVIDIA GeForce RTX 2060 SUPER (Turing), driver 580.178.04, Ubuntu 24.04.5, llvm-mc 20.1.2, test programs built from main 03b09a08 (this PR changes no C++): pass.

LEONIDA_W64_EMULATE=1 python3 -I scripts/run_gpu_tests.py --build <main build>: 195/210 passed, 15 skipped, 0 failed, the same result on every test as main 03b09a08.

No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

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

The change reuses exactly the dword-0 check already emitted by buffer_address, and the local cache shares only identical offset/end sums while preserving each limit comparison. The other dwords keep separate bounds predicates in the modes that require them. The semantic test covers load x4, load x2 with soffset, and store x4; I found no regression in the diff.

@mrbean56

Copy link
Copy Markdown

Tested cacbac1c81 on Linux (Debian 13), Python 3.13.5: pass, with a mutation that proves the guard.

  • pytest tests/semantic/test_buffer_bounds_shared.py -q: 1 passed, 3 subtests.
  • Non-vacuous: each of the three accesses emits 9 OpIAdd %ulong and all 9 operand pairs are distinct, so the "no pair built twice" assertion is looking at a populated table.
  • Mutation: disabling the ends cache in buffer_address (end = None) makes the test fail on load x4 and store x4 subtests, so a regression that rebuilds the shared check would be caught. The load x2 with soffset row still passes under that mutation, because there soffset is non-zero and the two modes do not share an offset id — worth knowing, since those rows are the ones the shared-check optimisation does not cover.
  • The test deliberately does not restate OOB semantics (test_buffer_oob_select.py and test_vector_mem_mubuf.py own those), which is the right split.

No GPU, no game data. AI-assisted.

@amarovndres

Copy link
Copy Markdown

Tested cacbac1c on AMD Radeon RX 9060 XT (RDNA4), AMD 26.10.1 (32.0.32015.7029, LLPC, Vulkan 1.4.359), Windows 11 Pro (build 26200): pass. As far as I can see, the first AMD GPU run on this PR.

  • GPU: valu_fuzz.py --family mem --programs 1000 --seed 1 --wave both (llvm-mc 23.1.6 from conda-forge, vk_compute_test built with llvm-mingw, the same binary for both sides): main 03b09a08 2000/2000 match, this PR 2000/2000 match, 0 skips, 0 harness errors.
  • The whole tests/semantic suite, with every module it translates also run through the translator's own assemble_spirv (spirv-as + spirv-val, vulkan1.2), since sharing in_b and the offset + end sums is only valid if each value dominates its later uses: this PR 7,457 tests and 7,324 modules, 0 invalid; main 7,456 tests and 7,321 modules, 0 invalid. The new test_buffer_bounds_shared passes. Nine tests fail or error on both sides alike (test_loop_ballot_barrier wave64-emulate cases, test_mutations for v_mul_legacy_f32, test_cmp_class_f64 rejected forms), so nothing new from this PR; I haven't checked whether those nine are specific to my setup.

AI-assisted (Claude Code). Synthetic inputs only, no game files.

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.

8 participants