Skip to content

rdna2spirv, lane_masks: wave64 s_bfm_b64 with literal operands is a known constant (data and mask readers) - #8264

Open
peta008 wants to merge 1 commit into
ItsNotRexed:mainfrom
peta008:fix/wave64-s-bfm-b64-literal-data
Open

peta008 wants to merge 1 commit into
ItsNotRexed:mainfrom
peta008:fix/wave64-s-bfm-b64-literal-data

Conversation

@peta008

@peta008 peta008 commented Nov 23, 2026

Copy link
Copy Markdown

Claim: #7804 (comment) (new item, found by a differential test). AI-assisted (Claude).

Problem

In wave64 code, s_bfm_b64 was always lowered per lane (each invocation gets 0 or ~0 in both halves of the pair). That is right for a lane-mask reader, but a reader that takes the pair as data gets the wrong words, with no Unsupported:

A plain s_mov_b64 s[8:9], 0x00C0000000000000 is already right for both kinds of reader.

Change

  • tools/rdna2spirv.py: wave64 s_bfm_b64 with two literal operands into an SGPR pair is a known constant, so it is moved like s_mov_b64 s[N:N+1], <value> (mov64).
  • tools/lane_masks.py: the same instruction is classified as that immediate copy is (EITHER for 0 / ~0, plain data otherwise).
  • Unchanged: a run-time operand (incl. a constant held in an SGPR), and vcc / exec / null destinations keep the lane-mask lowering; float literals (fimm) are not taken by the new path (same as main).
  • docs/design.md (Wave64 handling row): one sentence on the rule and how it was measured.

Tests

  • New tests/semantic/test_wave64_bfm64_data.py: every invocation stores the words (10 size/offset cases), the S# word folds to OpConstant %uint 12582912, the same constant still works as a lane mask (v_cndmask over 64 lanes, spirv_wave_interp), and lane_masks classifies it as the immediate copy. On main: 18 failures; with this change: OK.
  • Existing tests that encoded the old rule:
    • tests/test_lane_masks.py::test_bfm64_and_half_kinds and tests/test_translate.py::test_wave64_bfm64_and_half_cmp_emit_wave_ops now use a register operand, so they keep covering the per-lane path; the literal case is asserted as data.
    • tests/semantic/test_wave32_bfm64_data.py: the lane_masks unit case now expects data for wave64 literals and a mask for a run-time operand.
    • tests/semantic/test_wave64_subgroup_ops.py: cs_wm_bfm64 on two 32-wide subgroups is now correct (16 8 8 40, the GPU check's value). The old pinned words (16 8 0 4294967295) were measured on an NVIDIA RTX 4070 SUPER with its native 32-lane subgroup and the old lowering; the new ones are measured on AMD with the subgroup size forced to 32 (below). README line updated.
  • Full translator suite (python -I scripts/run_semantic_tests.py tests/semantic, SPIRV-Tools 1.4.363 on PATH): 448 files, 5 960 tests; 3 files fail, the same 3 as on main (test_image_atomics_guard, test_image_cube_store_glc, test_image_store_d16, covered by tests/semantic: three image test files red on main (stale after #4473 and #6805; LLVM_MC on Windows) #7284 / tests: fix image atomics guard and image_store d16 tests after #4473 and #6805 #7608).
  • All pytest-based tests/test_*.py (pinned tests/requirements.txt): identical totals with and without the change (1 743 passed; the same 3 pre-existing failures in test_tex_export / test_user_sgpr_census).
  • python -m compileall -q make_game.py tools tests clean; scripts/check_no_game_data.py on the changed files clean.

Hardware

AMD Radeon RX 7700 XT, driver 26.11.2 (LLPC), Windows 11, vk_compute_test <spv> 1 dump from a RelWithDebInfo MSVC build, hand-written listings translated by this checkout:

shader VKC_SUBGROUP_SIZE main this PR expected
s_bfm_b64 s[8:9], 2, 54, lane 0 stores s8, s9 64 0 0 0 12582912 0 12582912
same 32 0 0 0 12582912 0 12582912
cs_wm_bfm64 64 16 8 8 40 16 8 8 40 16 8 8 40
cs_wm_bfm64 32 16 8 0 4294967295 16 8 8 40 16 8 8 40

Not run: the corpus translated / spirv-val-valid counts (no game files here).

Notes

🤖 Generated with Claude Code

…nown constant

In wave64 code s_bfm_b64 was always lowered per lane (each lane 0 or ~0 in
both halves), so a reader that takes the pair as data got the wrong words:
the S# idiom `s_bfm_b64 s[88:89], 2, 54` (fixed for wave32 in ItsNotRexed#6809) left
word 1 unfolded, and `v_mov_b32 v5, s9` stored 0 or 0xFFFFFFFF instead of
0x00C00000. With two literal operands into an SGPR pair the result is a
constant, so it is now moved like `s_mov_b64 s[N:N+1], <value>`, which
serves a data reader and a mask reader (v_cndmask, EXEC) alike, and
lane_masks classifies it as that immediate copy. A run-time operand, and
vcc/exec/null destinations, keep the lane-mask lowering.

Measured on an AMD Radeon RX 7700 XT (driver 26.11.2, LLPC) with
vk_compute_test: the data read gives `0 0` on main and `0 0x00C00000` with
this change at subgroup size 64 and 32; cs_wm_bfm64 at subgroup 32 gives
16 8 0 4294967295 on main and the correct 16 8 8 40 with this change, so
test_wave64_subgroup_ops pins that.

AI-assisted (Claude).

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

Copy link
Copy Markdown

Independent native Windows GPU check of 5ee3abc4 on an RTX 3080 Ti (driver 621.35, MSVC 19.44, Vulkan SDK 1.4.363.0). The GPU used 32-lane subgroups with wave64 emulation enabled.

The stock cs_wm_bfm64 fixture returned the expected 16 8 8 40. A separate hand-written fixture read the result of s_bfm_b64 as data with width 2 and offset 54. Both words matched: 0 12582912. Both modules passed spirv-val, and direct reruns with Vulkan core and synchronization validation returned the same words with zero diagnostics.

Synthetic inputs only. AI-assisted.

@jacob-rowlands

Copy link
Copy Markdown

Tested 5ee3abc4b merged onto main dde82f27e, Linux (CachyOS), no game files: pass. This adds native wave64 (64-lane hardware) to @thehollowedone's emulated check, with a negative control against main.

A hand-written fixture (below) reads s_bfm_b64 s[8:9], 2, 54 as data and stores both words from lane 0. The control is the same value built by two s_mov_b32. It ran through vk_compute_test <spv> 1 dump:

Run main this PR expected
Ryzen 5 7600X iGPU (RDNA2, RADV, Mesa 26.3.1), native 64 lanes 0 0 0 12582912 0 12582912 0 12582912 0 12582912 0 12582912
RTX 3080 (NVIDIA 620.73.04), LEONIDA_W64_EMULATE=1, 32 lanes 0 0 0 12582912 0 12582912 0 12582912 same
RADV, LEONIDA_W64_EMULATE=1, 32 lanes 0 0 0 12582912 0 12582912 0 12582912 same

On main the high word comes out as lane 0's mask value (0) instead of 0x00C00000, with no Unsupported, as the description says. Both translators accept the fixture and pass spirv-val. The stock cs_wm_bfm64 (the mask reader) gives 16 8 8 40 natively on RADV and emulated on the RTX 3080.

run_gpu_tests.py gives the same results as main on all three runs. The semantic suite has the same 2 files failing as main.

Fixture
; local check for PR 8264 only (not committed): wave64 s_bfm_b64 with literal operands read as data
; s_bfm_b64 s[8:9], 2, 54 = bits 54..55 = 0x00C00000_00000000 -> out0 = s8 = 0, out1 = s9 = 0x00C00000 = 12582912
; control: s10 = 0, s11 = 0x00C00000 by two s_mov_b32 -> out2 = 0, out3 = 12582912
s_mov_b64 exec, -1
s_bfm_b64 s[8:9], 2, 54
s_mov_b32 s10, 0
s_mov_b32 s11, 0x00C00000
v_cmpx_eq_u32_e32 0, v0
v_mov_b32_e32 v5, s8
v_mov_b32_e32 v2, 0
buffer_store_dword v5, v2, s[0:3], 0 offen
v_mov_b32_e32 v6, s9
v_mov_b32_e32 v2, 4
buffer_store_dword v6, v2, s[0:3], 0 offen
v_mov_b32_e32 v5, s10
v_mov_b32_e32 v2, 8
buffer_store_dword v5, v2, s[0:3], 0 offen
v_mov_b32_e32 v6, s11
v_mov_b32_e32 v2, 12
buffer_store_dword v6, v2, s[0:3], 0 offen
s_endpgm

AI-assisted (Claude Code).

@alvaroolt

Copy link
Copy Markdown

Tested 5ee3abc4b on native Windows 11 (build 26300), against an upstream/main control (47428cc22): pass.

GPUs: AMD Radeon iGPU (native wave64, driver 24.10.34, AMD proprietary shader compiler) and NVIDIA GeForce RTX 5080 (driver 621.24, 32-lane subgroups). vk_compute_test from a RelWithDebInfo MSVC build, Vulkan SDK 1.4.363, llvm-mc 23.1.6. The change is Python-only, so the same binary ran both translators' output.

A hand-written fixture (not committed) reads three literal s_bfm_b64 results as data and stores both words of each from lane 0: s[8:9], 2, 54 (high half only), s[10:11], 4, 8 (low half only), and s[12:13], 8, 28 (straddles the two halves). Expected 0 12582912 3840 0 4026531840 15. Both translators accept it and pass spirv-val.

shader run main this PR
data fixture AMD, VKC_SUBGROUP_SIZE=64 0 0 0 0 0 0 0 12582912 3840 0 4026531840 15
data fixture AMD, VKC_SUBGROUP_SIZE=32 0 0 0 0 0 0 0 12582912 3840 0 4026531840 15
data fixture RTX 5080 (32) 0 0 0 0 0 0 0 12582912 3840 0 4026531840 15
cs_wm_bfm64 AMD, 64 16 8 8 40 16 8 8 40
cs_wm_bfm64 AMD, 32 16 8 0 4294967295 16 8 8 40
cs_wm_bfm64 RTX 5080 (32) 16 8 0 4294967295 16 8 8 40

So the straddling case is also all zeros on main (not only the high word), and fixed here. The lane-mask reader matches on every run, and the 32-lane cs_wm_bfm64 values agree with the new pinned 16 8 8 40.

The CPU tests are the same on head and main where they exist: test_wave32_bfm64_data, test_wave64_subgroup_ops, tests/test_lane_masks.py, and tests/test_translate.py (648 passed) all pass. The PR's new test_wave64_bfm64_data.py passes on head and gives FAILED (failures=18) when run against main (negative control).

Not run: the full semantic suite and run_gpu_tests.py. No game files.

AI-assisted: yes (Claude Code); results reviewed by me.

@NiHuShu

NiHuShu commented Nov 23, 2026

Copy link
Copy Markdown

AI-assisted exhaustive literal-operand check on CachyOS x86-64, CPython 3.14.10, LLVM 23.1.5, NVIDIA GeForce RTX 4080, driver 620.70.06 (32-wide, validation on). Compared dde82f27e994337094afc476ded0b1426f6e87e2 and 5ee3abc4b2c6dc0e3be2858f2bd9f9d8687d6071 in wave64 emulation. Used 64 synthetic compute modules, one per literal width. Each tried all 64 offsets with all 64 logical lanes active. In the same program, each lane read the SGPR pair as low/high words and as its v_cndmask predicate. Reference was an independent 64-bit integer bitfield.

python -I tools/rdna2spirv.py <disasm> --out <spirv> --wave64-emulate, then VKC_SUBGROUP_SIZE=32 VKC_OUT_WORDS=12288 VKC_DUMP=<file> vk_compute_test <spv> 1 dump per width. All 64 modules per revision translated and passed spirv-val; all dispatches exited 0, zero skips.

Readback class Parent wrong / checked Head wrong / checked
Low data word 155,312 / 262,144 0 / 262,144
High data word 207,888 / 262,144 0 / 262,144
Per-lane mask bit 0 / 262,144 0 / 262,144

This covers every six-bit width/offset combination, including zero widths, crossing bit 31, and truncation at bit 63. The data-reader failure cleared, and the previously working mask use stayed exact.

@Ekabusto

Copy link
Copy Markdown

Tested 5ee3abc4b on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD driver 26.11.2, Windows 11 10.0.26300:
pass.

  • New tests: test_wave32_bfm64_data.py 6 OK, test_wave64_bfm64_data.py 4 OK, test_wave64_subgroup_ops.py 6 OK.
    With main's tools/rdna2spirv.py and tools/lane_masks.py swapped in, they give 1, 18 and 1 failures.
  • tests/test_lane_masks.py ok. tests/test_translate.py (pytest, with llvm-mc and SPIRV-Tools): 648 passed.
  • Full scripts/run_gpu_tests.py: every fixture has the same result as on main, including the 20 cs_w64x_* /
    ms_w64x_* and cs_wm_bfm64. The only difference was ms_wave_info_s3 (PASS here, FAIL in the main run), which
    is intermittent on this card (ms_wave_info_s3 intermittently fails on RX 7900 XT / RADV NAVI31 (pass rate 100% -> 2.5%, same binaries) #6172).

AI-assisted: yes (Claude ran the build and tests; results checked by me).

@trick-ye

Copy link
Copy Markdown

Tested 5ee3abc4b merged onto main 6dda0630b on AMD Radeon RX 7800 XT (RDNA3), AMD 26.11.2 (Windows driver 32.0.32029.2010), Windows 11 26200: native 64-lane run on AMD's Windows driver: the data read of s_bfm_b64 s[8:9], 2, 54 is wrong on main and right on this head, and the stock mask fixture's output is unchanged.

I used jacob-rowlands's hand-written fixture from the comment above (not committed), built with runtime/test/shaders/build.py and run as VKC_SUBGROUP_SIZE=64 vk_compute_test <spv> 1 dump. The harness prints compute subgroup size: 64.

main 6dda0630b this head expected
data fixture, out[0..3] 0 0 0 12582912 0 12582912 0 12582912 0 12582912 0 12582912
stock cs_wm_bfm64, out[0..3] 16 8 8 40 16 8 8 40 16 8 8 40
  • Each of the four runs was done twice with the same output.
  • Full scripts/run_gpu_tests.py: main 225 of 241 passed, 16 skipped; this head 224 of 241 passed, 16 skipped, 1 failed. The failure is ms_wave_info_s3 (at (0,0): got (-1.0000,-1.0000,-1.0000,-1.0000)), which is intermittent in full runs on main on this card (4 of 6 full runs failed on dde82f27e with the same line; 6 of 6 pass when run alone) and whose .spv is byte-identical in the two trees; no other result differs. Of the generated .spv files only cs_wm_bfm64 and the local fixture differ between the two trees.
  • Not run: the semantic tests.
  • Test programs built from main 6dda0630b (the PR changes no C++). MSVC 14.42 (VS 2022), Vulkan SDK 1.4.363.0, llvm-mc 23.1.6, Python 3.13, LEONIDA_VK_DEVICE=7800. Hand-written fixtures only, no game files.

AI-assisted (Claude Code).

@N4use4tic

Copy link
Copy Markdown

Tested 5ee3abc on Windows 11, Python 3.14.4, no GPU or LLVM: pass.
From tests/semantic: test_wave64_bfm64_data (4 tests), test_wave32_bfm64_data (6) and test_wave64_subgroup_ops (6) OK; tests/test_lane_masks.py ok.
test_image_atomics_guard and test_image_store_d16 are still red, as on main (#7284).
Note: this PR conflicts with #8409 (2 conflicts) and #8448 (1 conflict) in the s_bfm_b64 and lane_masks changes; found with git merge-tree.
Code not reviewed beyond the test run.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 5ee3abc4 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_lane_masks.py -> ok

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

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