Skip to content

translator: preserve MUBUF DST_SEL=1 on out-of-bounds typed buffer loads (SHADER-429) - #2722

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
timelery:shader-429-oob-dst-sel
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
timelery:shader-429-oob-dst-sel

Conversation

@timelery

Copy link
Copy Markdown
Contributor

Summary (SHADER-429)

Claim: #2300 (comment)

Fixes out-of-bounds MUBUF typed buffer loads (buffer_load_format_{x,xy,xyz,xyzw} and buffer_load_format_d16_{x,xy,xyz,xyzw}) in tools/rdna2spirv.py (Gen.buffer_load_format):

  • Hardware rule (measured natively on RDNA2 gfx1036): When a MUBUF typed load is out of bounds (OOB_SELECT 0–3, num_records == 0, partial-element spill, or swizzled OOB), memory is not accessed and raw data channels (DST_SEL 4..7) read 0, while the V# DST_SEL swizzle still runs:
    • DST_SEL = 0 yields 0.
    • DST_SEL = 1 yields 1.0f (0x3F800000, d16 0x3C00) iff V#.FORMAT[6:0] is a valid float, normalized, or scaled buffer format (1..77 ending in _UNORM, _SNORM, _USCALED, _SSCALED, or _FLOAT in tools/gfx10_formats.json), and integer 1 (0x00000001, d16 0x0001) for UINT/SINT, INVALID (0), and reserved codes (78..127).
    • d16_x and d16_xyz preserve the unaddressed upper 16 bits of the final destination VGPR on out-of-bounds loads.
    • MTBUF tbuffer_load_format_* overrides d3 with identity selectors (4..7 / 0), so both out-of-bounds and V#.FORMAT = 0 loads continue to return 0 in every addressed channel.
  • Translator change: Removes the post-swizzle OpSelect %uint {in_b} {r} 0 clamp in Gen.buffer_load_format (since out-of-bounds words are already 0 via the guarded OpPhi, making decoded data channels comps[0..3] == 0), and classifies float vs. integer DST_SEL=1 from _FLOAT_FMT_MASKS over the 7-bit GFX10 format table.
  • Documentation & tests: Adds the contract row to docs/design.md and adds tests/semantic/test_buffer_oob_dst_sel.py.

Testing

  • Native RDNA2 hardware (AMD Radeon(TM) Graphics, gfx1036, Windows 11, driver 32.0.21042.62):
    • 746 native kernels via tools/hw_oracle (245 wave32 + 245 wave64 across all 41 Gen.FORMATS for xyzw, d16_xyzw, d16_xyz, d16_x, OOB_SELECT 0–3, partial-element spill, swizzled OOB, tbuffer_load_format_* OOB/FORMAT=0, and inactive EXEC lanes, plus 128 buffer_load_format_x + 128 buffer_load_format_d16_x kernels sweeping all 128 7-bit FORMAT codes 0..127) — 0 mismatches.
  • Vulkan GPU tests (NVIDIA GeForce RTX 5090, driver 32.0.16.1088; AMD Radeon(TM) Graphics gfx1036, driver 32.0.21042.62):
    • Translated 128-group compute shader sweeping all 128 7-bit FORMAT codes across xyzw, d16_xyzw, d16_xyz, and d16_x via vk_compute_test (1,152 checked words per GPU, 0 mismatches on both GPUs).
    • scripts/run_gpu_tests.py buffer suites (cs_fmt_1010102, cs_fmt_sint32, cs_fmt_xy_xyz, cs_oob_select, cs_oob_tail, cs_bounds, cs_buf_swizzle, cs_buf_tail) — 8/8 passed on both GPUs.
  • CPU / spirv-val / fuzzer tests:
    • python -I tests/semantic/test_buffer_oob_dst_sel.py -v (5/5 passed, including SPIRV-Tools 2026.3 spirv-val on native wave64 and --wave64-emulate)
    • python -m unittest discover -s tests/semantic -p "test_*buf*.py", test_*tbuffer*.py, test_value_semantics.py (112/112 passed)
    • python -I tools/mem_diff.py --self-test, python -I tools/mem_diff.py --programs 200 --seed 1, and python -I tests/test_mem_diff.py (200/200 programs matched, 15/15 unit tests passed)
    • python -m compileall -q make_game.py tools tests

AI-assisted.

…ads (SHADER-429)

On RDNA2 hardware (gfx1036, 746 native kernels via tools/hw_oracle across wave32
and wave64), out-of-bounds MUBUF buffer_load_format_{x,xy,xyz,xyzw} and
buffer_load_format_d16_{x,xy,xyz,xyzw} skip memory access and decode raw data
channels (DST_SEL 4..7) as 0 while V# DST_SEL still runs: DST_SEL=1 yields
1.0f (0x3F800000, d16 0x3C00) iff V# FORMAT[6:0] is a valid float, normalized,
or scaled code (1..77 ending in _UNORM/_SNORM/_USCALED/_SSCALED/_FLOAT), and
integer 1 (0x00000001, d16 0x0001) for UINT/SINT, INVALID (0), and reserved
codes (78..127). MTBUF tbuffer_load_format_* ignores V# DST_SEL and returns 0
in addressed channels when OOB or V# FORMAT=0.

Remove the post-swizzle in_b clamp in Gen.buffer_load_format and classify
float vs integer DST_SEL=1 from the 7-bit GFX10 format table.

AI-assisted.
@padhossein

Copy link
Copy Markdown
Contributor

Independent Vulkan check of 10e310fcced49dd8168437b74c891753d465890d on Linux x86_64, Mesa llvmpipe 26.1.6 (subgroup 8), with Khronos validation 1.4.309. With a hand-written synthetic shader exercising MUBUF xyzw and d16_x on a 16-byte descriptor, the full 64-word dump matched for 128/128 V# FORMAT codes. I checked ONE, ZERO and X selectors, the untouched high half of the d16 destination, and untouched output words. SPIR-V assembled and passed spirv-val --target-env vulkan1.3; no Vulkan validation diagnostics in the runs.

The same program failed 4/4 representative controls on base 49475796 (formats 0, 2, 22 and 127): the ONE selector returned zero for both results where integer 1 or float 1.0 was expected. The PR's dedicated semantic suite also passed 5/5 locally. This checks the translated Vulkan path using software Vulkan. Synthetic inputs only. AI-assisted (Codex).

@kapitche

kapitche commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested on Linux (WSL2 Ubuntu), Python 3.10.12, CPU only: pass. With upstream/main merged (clean), tests/semantic/test_buffer_oob_dst_sel.py runs 5 tests, OK. Negative control: restoring tools/rdna2spirv.py to upstream/main makes all 5 fail (177 subtest failures). I rebuilt the float-format mask from tools/gfx10_formats.json: 49 codes in 1..77, and the 28 excluded codes are exactly the UINT/SINT ones, matching the comment. Trial merges: clean with #2711 (full tests/semantic with both merged: 2574 tests OK, 3 skipped), but #2710 conflicts in docs/design.md only (two rows added at the same spot in the contract table; keep both).

@ricarmandoagent

Copy link
Copy Markdown

Reviewed head 10e310fcced49dd8168437b74c891753d465890d, base 49475796. Bazzite Linux, Python 3.15.2, CPU only. @padhossein covered the software-Vulkan path and @kapitche the 177-subtest negative control; this adds mutation checks and one limitation.

  • PATH=$HOME/.local/bin:$PATH python3 -I tests/semantic/test_buffer_oob_dst_sel.py -> Ran 5 tests ... OK (with spirv-as/spirv-val on PATH so the validation block runs, not skipped).
  • Reproduced the body's discover totals: python3 -m unittest discover -s tests/semantic -p "test_*buf*.py" -> 77 OK, test_*tbuffer*.py -> 4 OK, test_value_semantics.py -> 31 OK (77+4+31 = 112). Also test_buffer_load_format, test_buffer_format_narrow_int, test_buffer_load_sub, test_tbuffer_store, test_buffer_oob_select, test_buffer_swizzle, test_lds_read_u8_d16 -> OK.
  • Mutation A: re-adding the old post-swizzle clamp (rb = OpSelect %uint {in_b} {r} 0) -> failures=177, same as the origin/main negative control.
  • Mutation B: dropping _FLOAT from _FLOAT_FMT_MASKS -> failures=21: the 9 _FLOAT codes in the 128-code sweep (13,22,29,36,43,64,71,74,77), 7 f32/f16 formats in the Gen.FORMATS sweep, and the 5 f32x4-format OOB cases. The 128-code test is sensitive to the mask, not only to the clamp.
  • Limitation: the 128-code test derives its expected float/integer class from the same tools/gfx10_formats.json the translator's _FLOAT_FMT_MASKS is built from. Relabelling code 22 from 32_FLOAT to 32_UINT in that JSON leaves the 128-code test OK; only the Gen.FORMATS sweep (code=22, kind='f32') catches it. The sweep therefore validates bit extraction against the table, not the table against hardware; the gfx1036 measurement itself is not verifiable here.
  • Reproduced python3 -I tools/mem_diff.py --programs 200 --seed 1 -> 200 pass, 0 mismatch; python3 -I tests/test_mem_diff.py -> 15 OK. tests/test_docs_design_rows.py fails 3 assertions on this head and identically on origin/main (pre-existing, not this PR).

LGTM. AI-assisted review.

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested 10e310f 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: 2572 OK

No game files. AI-assisted.

@Doorriiaann

Copy link
Copy Markdown

Tested 10e310f on NVIDIA GeForce RTX 3070 (Ampere, subgroup 32..32), driver 610.57.04 (Vulkan 1.4.341), Debian 13 (kernel 6.12.115): pass, no regression. A hardware GPU next to the llvmpipe and CPU-only runs above.

Base is main 4947579; runtime/build/linux built from each side; llvm-mc 21.1.8, SPIRV-Tools v2025.5, GCC 14.2.0.

main 4947579 this PR
tests/semantic/test_buffer_oob_dst_sel.py 177 failures (128 test_all_128_gfx10_7bit_format_codes_match_rdna2_hardware_table, 41 test_all_supported_formats_preserve_dst_sel_one_when_oob, 8 others) 5 tests OK
python -I -m unittest discover -s tests/semantic 2567 tests OK 2572 tests OK
scripts/run_gpu_tests.py 139/185 passed, 46 skipped 139/185 passed, 46 skipped
LEONIDA_W64_EMULATE=1 scripts/run_gpu_tests.py 170/185 passed, 15 skipped 170/185 passed, 15 skipped

The main column for the new test file is the negative control (this PR's file against main's translator).

AI-assisted: yes

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 10e310fcced49dd8168437b74c891753d465890d on AMD Radeon Pro V540 (RDNA1, Navi 12 / gfx1011, PCI 1002:7360, RADV Mesa 25.2.8-0ubuntu0.24.04.4, Ubuntu 24.04.4, kernel 7.0.0-31-generic, Vulkan device 1.4.318, subgroup 32..64): pass, no regression. This is a real-GPU RDNA1/RADV check, complementary to the llvmpipe validation in the comments.

  • Command: DRI_PRIME=pci-0000_1c_00_0! python -I scripts/run_gpu_tests.py --filter <test> --build runtime/build/linux, all 1/1 passed: cs_bounds, cs_buf_tail, cs_oob_select, cs_oob_tail, cs_fmt_xy_xyz (128/128 dumped words exact). These are the existing out-of-bounds/typed-load fixtures; this PR adds semantic tests only, so no new GPU fixture was available to run.
  • Command: python -I scripts/run_gpu_tests.py --build runtime/build/linux: 156/185 passed, 28 skipped, 1 failed (190 shaders translated, 190 pass spirv-val), identical to clean base 49475796 measured in a separate worktree (156/185, 28 skipped, 1 failed). The single failure is the pre-existing cs_w32_salu64 RDNA1/RADV issue, unrelated to DST_SEL.
  • Command: pytest tests/semantic/test_buffer_oob_dst_sel.py -q: 5 passed.

Tools: llvm-mc 21.1.8 with AMDGPU target, SPIRV-Tools 2026.1, GCC 13.3.0, Python 3.12.3. No code was edited or pushed; only synthetic fixtures were used.

AI-assisted.

@Jadiac5

Jadiac5 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Corpus run of 10e310fc against its merge base 49475796, shard 0 of 8 (3,313 of 26,503 shaders), Windows 11, Python 3.12, spirv-val from Vulkan SDK 1.4.363.1: no regression

translated spirv-val valid modules changed
base 49475796 3,313 / 3,313 3,313
this PR 10e310fc 3,313 / 3,313 3,313 1,797
  • No shader fails before or after, so the set of failing shaders is unchanged (empty).
  • The 1,797 changed modules are 1,161 MS (every mesh shader in the shard), 281 PS, 212 CS and 143 VS; no GS. All 1,797 are smaller than before, none larger.
  • The index (index.part0.json) is identical entry for entry, so no binding, capability or feature changes.
  • So this path is common in the real shaders: more than half of the shard goes through the changed code. The GPU runs already posted here used synthetic shaders; this is the first count over game shaders.
  • Not done: shards 1–7, no GPU run of the changed modules, no look at what the changed instructions are.

AI-assisted (Claude Code). Counts only; nothing from the game files is included.

@ItsNotRexed
ItsNotRexed merged commit 5b63f6d 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