Skip to content

rdna2spirv: preserve DST_SEL=1 on out-of-bounds buffer_load_format (SHADER-429) - #2467

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
0xTehuti:task/shader-429
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
0xTehuti:task/shader-429

Conversation

@0xTehuti

Copy link
Copy Markdown
Contributor

Item: SHADER-429, claimed in #2300 (finding by @timelery).

Resolves: SHADER-429

What was wrong

In tools/rdna2spirv.py (buffer_load_format), the in_b (in-bounds) check was evaluated via OpSelect over the final post-swizzle register r:

rb = self.tmp()
self.emit(f"{rb} = OpSelect %uint {in_b} {r} {self.cu(0)}")

As reported and measured by @timelery on RDNA2 (gfx1036), when an access falls out of bounds (or is unaligned), raw data channels read 0 but the V# descriptor's DST_SEL still applies. Therefore, channels configured with DST_SEL=1 must return the constant 1.0f (0x3F800000 or 0x3C00 in d16) or integer 1 (0x0001 in d16). Evaluating in_b after the swizzle erroneously forced all channels to 0.

The change

  • The in-bounds gate in_b is now applied directly to the raw fetched buffer components (comps[0..3]):
    bounded_comps = []
    for comp in comps:
        cb = self.tmp()
        self.emit(f"{cb} = OpSelect %uint {in_b} {comp} {self.cu(0)}")
        bounded_comps.append(cb)
  • Swizzled selections DST_SEL=0 remain 0, while DST_SEL=1 evaluates to dst1 intact both in standard format and when packed via d16.
  • Added regression tests in tests/semantic/test_buffer_load_format.py:
    • test_dst_sel_one_out_of_bounds: verifies FMT_F32, FMT_U32, FMT_UNORM8, FMT_UINT8X4 out-of-bounds loads return 0 for memory channels but preserve 1.0f / 1 for DST_SEL=1.
    • test_dst_sel_one_out_of_bounds_d16: verifies packed d16 destinations preserve 0x3C003C00 (f16) and 0x00010001 (u16).

Verification

  • Semantic Suite (tests/semantic/test_buffer_load_format.py): 34/34 tests PASSED (1.27s).
  • Translator Translation Suite (tests/test_translate.py): PASSED (0 errors).
  • Compileall: python -m compileall -q make_game.py tools tests clean (0 errors).
  • Host Environment: Windows 11 AMD64, MSVC 2022 Community (19.44.35225), Python 3.13, NVIDIA GeForce RTX 2070 SUPER (Driver 595.79, Vulkan 1.4).
  • Clean-room compliance: Pure synthetic vectors, zero game files or archives. AI-assisted.

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent GPU check of e701a6f against its parent 4947579, on Windows 11 build 26300, RTX 5070, driver 621.24.

A hand-written compute probe passed the source V# in live user SGPRs and selected (X, 0, 1, 1). Each dispatch had 2 in-bounds lanes and 30 out-of-bounds lanes in the same wave, using RAW bounds. I tested R32_FLOAT, R32_UINT, R8_UNORM and RGBA8_UINT with both full-width and D16 destinations, checking every returned word and the untouched heap. The two modules for each revision passed Vulkan 1.2 spirv-val.

  • PR: 256/256 lane results matched. In-bounds X data stayed intact. All 240 out-of-bounds lanes returned X=0 while preserving both constant-one channels, including packed 0x3c003c00 for float and 0x00010001 for integer.
  • Parent: all 240 out-of-bounds lanes zeroed the constant-one channels. The 16 in-bounds lane results matched the PR.

All 34 formatted buffer semantic tests passed on the PR. The GPU probe covered descriptor bounds with aligned source offsets. Synthetic inputs only. AI-assisted review.

@UNYTIX

UNYTIX commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Read the patch and ran the tests on Windows (Python 3.15, no GPU claim here — this is the CPU side of the rule).

Fail-before / pass-after, checked: at e701a6f1, python -I -m unittest discover -s tests/semantic -p test_buffer_load_format.py → Ran 34 tests ... OK. With tools/rdna2spirv.py from main (49475796) and the PR's tests kept, exactly the two new tests fail:

FAIL: test_dst_sel_one_out_of_bounds ... AssertionError: Tuples differ: (0, 0, 0, 0) != (0, 0, 1065353216, 1065353216)
FAIL: test_dst_sel_one_out_of_bounds_d16 ... AssertionError: Tuples differ: (0, 0) != (0, 1006648320)

So the new tests do catch the old behaviour, and only the two channels the V# sets to DST_SEL=1 change.

The change itself looks right: gating comps[] instead of the post-swizzle r is exactly the distinction — the fetched channels are 0 out of bounds, the swizzle's constant channels are not. OpSelect on a comp the format never reads is harmless, and dst1 still picks 1 vs 0x3F800000 from is_int_fmt, so d16 packs from r as before.

Three things I would add before this merges:

  1. docs/design.md does not state this rule. The "Typed buffer loads" row says DST_SEL swizzle is applied (0→0, 1→1.0f, 4..7→X..W) but says nothing about out of bounds, so the row still describes the buggy behaviour. Worth a clause like "out of bounds, fetched channels are 0 and DST_SEL=1 is still 1.0f/integer 1, measured on gfx1036 (@timelery)". Watch out: that row appears more than once in the table (three "Typed buffer loads" rows at lines ~245, ~246 and ~248 on main), so the sentence needs to go in the right one(s).
  2. No GPU case for it. The rule comes from a hardware measurement, but the only coverage is the interpreter. cs_tbuffer_store.asm or the new cs_buf_sub.asm (GPU test for the sub-dword MUBUF loads and GFX10_FORMAT_8_UINT #2696) could carry one tbuffer_load_format_x past NUM_RECORDS with a V# DST_SEL of X=1: the expected word is then 1 (or 0x3F800000 for a float format) instead of 0, which is a one-line addition to an existing dump. I would rather see that than trust the interpreter alone.
  3. UNORM OOB DST_SEL=1: the test asserts 0x3F800000 for FMT_UNORM8. If the measurement only covered F32/U32/UINT8X4, say so in the test docstring so the unorm answer is marked as inferred rather than measured.

Nit, not a blocker: the gate now costs four OpSelect per load instead of one even when in_b is a constant true (all-raw swizzle, in range). If the translator has a constant-folding path for OpSelect this disappears; if not, skipping the copies when in_b is cu(1) would keep the old output shape for the common case.

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested e701a6f on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 170/185 passed, 15 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base 4947579 on the same machine, every test has the same result.
  • python -m unittest discover -s tests/semantic: 2569 OK

No game files. AI-assisted.

@UNYTIX

UNYTIX commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Heads-up for the coordinator, separate from the review above: @timelery posted done: SHADER-429 #2722 in #2716 for the same rule (preserve DST_SEL=1 on out-of-bounds MUBUF buffer_load_format_*), so #2722 and this PR look like two implementations of one item. Whichever lands first, the other will need rebasing or closing — worth settling before more review time goes in. The points in my review (docs row, a GPU case for the OOB DST_SEL=1 channel, and marking the UNORM answer as measured or inferred) apply to #2722 as well.

@zneel

zneel commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested e701a6f1 on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM), Python 3.12.10: pass.

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. No game files.

run result
run_gpu_tests.py (whole suite, native wave64) 170/185, 15 skipped, 0 failed (main 49475796: 170/185)
same with LEONIDA_W64_EMULATE=1 170/185, 15 skipped, 0 failed
pytest tests/test_translate.py 526 passed, 2 skipped in 62.88s (0:01:02)
unittest discover -s tests/semantic -p test_buffer_load_format.py 34 OK

This head changes 5 suite modules compared with main 49475796. Four of them pass both native and emulated: cs_oob_tail (the out-of-bounds case) and cs_fmt_1010102/cs_fmt_sint32/cs_fmt_xy_xyz, all exact. The fifth, cs_buf_typed, is run by d3d12_buffer_uav_test, and I didn't run ctest here. None of them loads out of bounds with a non-default DST_SEL, so the exact SHADER-429 case is covered only by the semantic test.

AI-assisted (Claude Code); I checked the results.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested e701a6f on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 139/185 passed, 46 skipped, 0 failed. main 4947579 gives 139/185, 46 skipped, 0 failed. All 190 test shaders translate and pass spirv-val.
  • tests/semantic/test_buffer_load_format.py: ran 34 tests in 1.663s; OK.

AI-assisted (Claude Code).

@lunisahdidouch

Copy link
Copy Markdown

Tested e701a6f on an RX 9070 (RDNA4), AMD 26.11.1 (LLPC), Vulkan 1.4.359, Windows 11: pass

tests/semantic passes including the new test_buffer_load_format.py cases (2569 tests, OK). Full run_gpu_tests.py: 170/185 passed, 15 skipped, nothing failed, same as main 4947579, so no regressions in the buffer load tests on hardware.

AI-assisted

@MiguelMatMar

Copy link
Copy Markdown

Tested e701a6f on RTX 4080 SUPER (Ada, subgroup 32), NVIDIA 621.24 (Vulkan 1.4.357), Windows 11 Pro build 26300; MSVC 14.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 built with scripts/build_llvm_mc.ps1, Python 3.14: pass

  • python -I scripts/run_gpu_tests.py (runtime built at the PR's base 49475796): 139/185 passed, 46 skipped, 0 failed. 190/190 translated, 190 pass spirv-val. Same as the base on this GPU. The skips are the 64-wide-subgroup tests and the ones another harness runs.
  • LEONIDA_W64_EMULATE=1 ... --filter w64: 25/26 passed, 1 skipped, 0 failed (same as the base).
  • tests/semantic/test_buffer_load_format.py: 34 passed.
  • python -m unittest discover -s tests/semantic: OK. tests/test_translate.py with llvm-mc + SPIRV-Tools on PATH: all passed (2 skipped).

@ItsNotRexed
ItsNotRexed merged commit 81b63b8 into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
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.

9 participants