Skip to content

fix(rdna2spirv): lower s_bitreplicate_b64_b32 to EXEC and wave64 VCC (SHADER-1289) - #8842

Closed
Cryptoteep wants to merge 1 commit into
ProjectLeonida:mainfrom
Cryptoteep:fix/shader-1289-bitreplicate-dest
Closed

Cryptoteep wants to merge 1 commit into
ProjectLeonida:mainfrom
Cryptoteep:fix/shader-1289-bitreplicate-dest

Conversation

@Cryptoteep

@Cryptoteep Cryptoteep commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Coordination claim
Item: SHADER-1289
Depends on: #8526

What changed

Translate s_bitreplicate_b64_b32 destination writes to EXEC and wave64 VCC in compute shaders:

  • Each lane $L \in [0..63]$ receives bit $L // 2$ from the 32-bit scalar source ((word >> (lane // 2)) & 1).
  • Stores to %active (for EXEC) or %vcc (for VCC), preserving SCC as required by the RDNA2 ISA specification.
  • Updated tools/lane_masks.py to record MASK state on vcc for s_bitreplicate_b64_b32.
  • Updated tools/rdna2spirv.py:16505 to recognize s_bitreplicate_b64_b32 on exec/vcc as a wave64 marker in code_w64.
  • Updated contract in docs/design.md.

Verification

  • Added synthetic per-lane tests in tests/semantic/test_salu_b32_lane_masks.py:
    • test_bitreplicate_writes_exec_and_keeps_scc: verified all 64 lanes and SCC preservation for words 0, 0x80000001, 0x0f0f0f0f, 0x55555555, 0xAAAAAAAA, and 0xFFFFFFFF.
    • test_bitreplicate_writes_wave64_vcc_and_keeps_scc: verified all 64 lanes via v_cndmask_b32 and SCC preservation for the same patterns.
  • All 10 tests in tests/semantic/test_salu_b32_lane_masks.py passed.
  • All 7 tests in tests/semantic/test_salu_quadmask_bitreplicate.py passed.
  • Verified no game data via scripts/check_no_game_data.py.
  • Windows 11, CPython 3.14, CPU-only.

…(SHADER-1289)

Lower s_bitreplicate_b64_b32 destination writes to EXEC and wave64 VCC
in compute shaders, deriving each lane's bit from scalar source bit L // 2.

In compute shaders, each lane L in [0..63] evaluates:
  (s0 >> (lane_id // 2)) & 1
and stores to %active (for EXEC) or %vcc (for VCC), preserving SCC.
Also recognize s_bitreplicate_b64_b32 on EXEC/VCC as a wave64 marker
in code_w64 and mark VCC mask state in lane_masks.py.

Contract: docs/design.md row for s_quadmask / s_bitreplicate.
Verified with synthetic tests in tests/semantic/test_salu_b32_lane_masks.py.
@catminou4

Copy link
Copy Markdown
Contributor

Tested 74859f4da on Apple M1 Max, macOS 27.2.1 arm64, MoltenVK 1.4.2: pass.

  • python -m unittest test_salu_b32_lane_masks → 10/10 OK on ARM64.
  • GPU run_gpu_tests.py --filter new_ops: cs_w32_new_ops (contains s_bitreplicate) PASS — 85 32769 3221225472 2147565568 1073807359 119 153 4660 exact on MoltenVK.

No game files. AI-assisted (Devin).

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

Independent Linux CPU review (74859f4d)

What changed: rdna2spirv lowers s_bitreplicate_b64_b32 to EXEC and wave64 VCC (SHADER-1289).

Checked: focused tests 10 OK.

Looks correct; no game files. Note other open rdna2spirv.py PRs may need rebase order.

@matheusfermiano-source

Copy link
Copy Markdown

Linux Vulkan validation of the new wave64 EXEC and VCC paths at 74859f4d. I dispatched both synthetic programs from the test, with the initial SCC parameterized to check both 0 and 1. The parent rejects both programs with Unsupported: s_bitreplicate_b64_b32 on a lane mask.

GPU Translation path Confirmed compute subgroup Dispatches
AMD Radeon 660M, RADV Mesa 26.3.1 Native wave64 64 24/24
AMD Radeon 660M, RADV Mesa 26.3.1 Wave64 emulation 32 24/24
NVIDIA RTX 3050 Laptop, driver 620.73.04 Wave64 emulation 32 24/24

For each dispatch I compared the entire 128-word output from vk_compute_test: all 64 mask values matched (s4 >> (lane // 2)) & 1, and all 64 copies of SCC retained the initial value. The six input masks were 0, 0x80000001, 0x0f0f0f0f, 0x55555555, 0xaaaaaaaa, and 0xffffffff. I used the test's translate_cs(..., 64, emulate=...), with the initial compare changed to s_cmp_eq_u32 s6, 0 and VKC_SGPRS=<input>,0,<s6>. The harness ran one workgroup with VKC_OUT_WORDS=128, a binary VKC_DUMP, and the required subgroup size above.

Every module assembled as SPIR-V 1.5 and passed spirv-val --target-env vulkan1.2. The two focused Python suites passed all 17 tests.

Synthetic inputs only. AI-assisted (Codex).

@nonverifiedperson

Copy link
Copy Markdown
Contributor

AMD Windows wave64 GPU and silicon check of 74859f4, merged with current main (clean merge): pass.

The test shader is a wave64 compute shader (group 64,1,1, run with --require-wave64). It writes 6 words through s_bitreplicate_b64_b32 into EXEC and into VCC: 0x80000001, 0x0f0f0f0f, 0x55555555, 0xaaaaaaaa, 0x12345678, 0xffffffff. For EXEC each lane marks itself with a v_mov_b32 and EXEC is then restored. For VCC each lane takes v_cndmask_b32_e64 v, 0, 1, vcc. Every lane stores its 12 words, and the masks rebuilt from them are compared with lane L = bit L/2 of the source:

12 masks (6 EXEC + 6 VCC)
gfx1036 silicon (tools/hw_oracle, wave64, game MODE) 12/12 match the replication
this PR, translated, AMD Radeon iGPU, compute subgroup 64 12/12 match
main not translated: build.py reports 0 translated

python -I scripts/run_semantic_tests.py tests/semantic on the merge: 447 files, 5959 tests. 3 files fail: test_image_atomics_guard, test_image_cube_store_glc and test_image_store_d16. These are the same 3 that fail on main, and #7284 fixes them.

This adds the AMD proprietary Windows driver to the RADV check above. I didn't re-check SCC preservation here; the PR's CPU tests and the RADV run cover it.

Windows 10 22H2, AMD Radeon iGPU gfx1036 (driver 32.0.13036.4), Vulkan SDK 1.4.363. The RTX 5070 Ti has no 64-wide subgroup, so it can't run the wave64 path. Synthetic values only, no game files. AI-assisted (Claude Code).

@Ysergic

Ysergic commented Nov 23, 2026 •

Copy link
Copy Markdown

Independent GPU validation of the NEW EXEC/VCC destination paths at 74859f4: pass.

Bazzite 45, RTX 3070 (Ampere, 32-wide physical subgroups), NVIDIA 620.73.04, Python 3.15.2, LLVM 23.1.6, SPIRV-Tools 2026.4. Khronos core/synchronization validation enabled. Synthetic fixtures only.

I assembled two compute fixtures with llvm-mc gfx1013 +wavefrontsize64, one writing EXEC and one writing VCC with s_bitreplicate_b64_b32. Both take the source word and initial SCC selector from runtime user SGPRs. They write a result for all 64 logical lanes and a second 64-word array recording SCC. The EXEC fixture initializes its result before masking and restores EXEC before storing both arrays; the VCC fixture observes the result through v_cndmask.

608 GPU dispatches, 77,824 output words, zero mismatches:

  • 38 source words: zero, all ones, 0x80000001, 0x0f0f0f0f, 0x55555555, 0xaaaaaaaa, and all 32 one-hot basis vectors.
  • Initial SCC 0 and 1 for every word, preserved in every lane.
  • Both destinations, raw and --opt, 144-byte push constants and 128-byte --pc-tail-ubo.
  • Asserted wave64=true and wave64_emulated=true in every emitted index. Every module passed spirv-val --target-env vulkan1.2, and no Vulkan validation errors occurred.

The per-lane reference was (word >> (lane // 2)) & 1, with SCC unchanged, matching AMD's RDNA2 ISA, bit-wise/SOP1 instruction tables.

Negative control: main 867d922 refuses both special-destination fixtures; this is not merely another run of cs_w32_new_ops, which does not cover these destinations.

Full semantic suite: 447 files / 5,959 tests; only test_image_atomics_guard and test_image_store_d16 fail, matching the known main baseline.

Focused semantic checks: test_salu_b32_lane_masks.py 10/10, test_salu_quadmask_bitreplicate.py 7/7. This is translated Vulkan behavior on NVIDIA, not a new native AMD measurement. AI-assisted with Codex.

@TG0000

TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Hardware GPU verification: AMD Radeon RX 6800 (gfx1030), Windows 11, AMD 26.11.2, Vulkan 1.4.363

Head 74859f4dab3a5245f0ef35d50d4803b008629cb5, trial-merged onto origin/main 867d922f0 (clean merge). Vulkan test targets built with clang (llvm-mingw), -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF.

  • python -m compileall -q make_game.py tools tests: OK
  • tests/semantic/test_salu_b32_lane_masks.py: 10 tests, OK
  • Full scripts/run_gpu_tests.py: 223/241 passed, 17 skipped, 1 failed (bare origin/main on the same machine: 224/241 passed, 17 skipped, 0 failed)
  • Related --filter runs: mask 12/12, salu64 1/1, lane 5/6 (1 skipped), all passed
  • The one full-run failure is ms_wave_info_s3 (got -1.0 everywhere), the known intermittent failure on RX 6800 that also shows up on bare origin/main. On this head, --filter ms_wave_info_s3 passed on both reruns (2/2), so the result is not deterministic for this PR. I count it as the flake, not a regression.
    This PR adds no GPU test under tests/semantic/fixtures, so the hardware check covers regressions in the existing GPU suite, not the new lowering itself.

AI-assisted run (Claude Code).

@wavehs

wavehs commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

macOS arm64 verification (Apple M3 Pro) — vcc_lo in wave64 loses the upper half

Tested PR head 74859f4d merged onto main 867d922f0 (merge 74859f4d), no conflicts.

Host: macOS 27.2.1, Apple M3 Pro (arm64), CPython 3.15.2.

Check Result
python3 -I tests/semantic/test_salu_b32_lane_masks.py 10/10 pass
Full translator suite 447 files, 5,959 tests, 77 s — 2 files failed
main baseline 447 files, 5,957 tests, the same 2 files

+2 tests over the baseline (the two new methods), no new failures; the two (test_image_atomics_guard, test_image_store_d16) reproduce on unmodified main here. Negative control: main's translator with the new tests → 2 errors, Unsupported: s_bitreplicate_b64_b32 on a lane mask.

The exec and full-vcc cases are correct: lane L gets S0[L // 2], SCC is untouched, and the wave64 marker in code_w64 and the lane_masks.py MASK marking are both the right places for this.

Found: a vcc_lo destination in wave64 clobbers lanes 32..63

The new branch accepts exec, vcc and vcc_lo, but for all of them it emits the same bare store:

if d["t"] == "exec":
    self.set_exec(bit)
else:
    self.emit(f"OpStore %vcc {bit}")

%vcc holds this invocation's single VCC bit, so a bare store overwrites every lane's bit. That is correct for a full 64-bit vcc destination, but vcc_lo writes only VCC[31:0] — lanes 32..63 must keep whatever VCC_HI held. The translator already has exactly this logic in put_mask() (rdna2spirv.py:5880), which the new code bypasses.

Reproduced through the test file's own helpers:

asm = ["s_mov_b64 vcc, -1",                     # VCC = 1 in all 64 lanes
       "s_bitreplicate_b64_b32 vcc_lo, s4",     # write VCC_LO only
       "v_cndmask_b32 v4, 0, 1, vcc",           # read VCC back as a mask
       ...store v4 per lane...]
spv = translate_cs("w64_bitreplicate_vcc_lo", asm, 64)
run_cs(spv, 64, s4=0)
lanes 0..31 lanes 32..63
expected 0 1 (VCC_HI kept)
got 0 0 ← clobbered

So a shader that writes VCC_LO and later uses VCC as a mask would silently drop every pixel/thread in the upper half. This is precisely the class of bug the PR is trying to fix for exec.

Verified fix. Use the existing half-aware writer instead of the bare store:

if d["t"] == "exec":
    self.set_exec(bit)
else:
    self.put_mask(d, bit, "lo" if self.wave64 else "")

I patched that in and ran both widths: wave64 gives [0]*32 + [1]*32 (upper half preserved), and a wave32 vcc_lo shader still gets S0[L // 2] in every lane (the write is not swallowed). Reverted afterwards; git diff clean.

Why the tests miss it: they cover exec (all 64 lanes) and full vcc (all 64 lanes), but never vcc_lo, which is the one form where the half rule matters. The probe above would make a good third test — it needs no game data and runs in the interpreter.

Two smaller points: vcc_hi is rejected while vcc_lo is accepted, which is asymmetric — if that form can occur, put_mask(d, bit, "hi") handles it symmetrically; if it cannot, rejecting both would be more honest than accepting one. And #8526 touches this exact block (it rejects vcc entirely); whichever lands second will conflict, and the merge should keep this PR's wider handling rather than 8526's stricter check.

No game files. AI-assisted.

@nonverifiedperson

Copy link
Copy Markdown
Contributor

@wavehs, about the vcc_lo finding: I don't think the hardware has that case, so put_mask(d, bit, "lo") would model something it doesn't do.

s_bitreplicate_b64_b32 has a 64-bit SDST, and the operand that names VCC there is the pair. llvm-mc 21 for gfx1030, with both +wavefrontsize64 and +wavefrontsize32, gives:

input result
s_bitreplicate_b64_b32 vcc, s4 [0x04,0x3b,0xea,0xbe]: SDST 0x6a (106, the VCC_LO register number)
s_bitreplicate_b64_b32 vcc_lo, s4 error: invalid operand for instruction
s_bitreplicate_b64_b32 s[6:7], s4 [0x04,0x3b,0x86,0xbe]

So SDST 106 on a 64-bit op is printed vcc and writes VCC_LO and VCC_HI together. No disassembly of a real shader has vcc_lo as this instruction's destination. In my wave64 run above, s_bitreplicate_b64_b32 vcc, s4 on gfx1036 silicon gave lane L = bit L/2 across all 64 lanes, which means it also overwrote VCC_HI. The translated shader matched that on the Radeon.

The bare OpStore %vcc is therefore right for every form that can be encoded. The PR's vcc_lo branch is dead code. The tidiest change would be to drop vcc_lo from the accepted destinations (or raise Unsupported on it) rather than give it half-write behaviour, so a hand-written .asm with that token can't pass on the CPU while llvm-mc rejects it.

AI-assisted (Claude Code). Synthetic values only.

@CreaCloudz

Copy link
Copy Markdown

Tested 74859f4 on Windows 11, CPU-only (no game data, no GPU): the VCC-destination path has a correctness bug, and both the EXEC and VCC paths share a scope gap with #8526.

VCC write stores a raw 0/1 bit instead of the project's mask representation. Every lane-mask value elsewhere in this translator (mask_src, exec_value(), tools/lane_masks.py's own invariant: "it holds 0 or ~0 per lane") is 0 or 0xFFFFFFFF. OpStore %vcc {bit} here stores the bare OpBitwiseAnd result (0 or 1), so a later op that inverts VCC with a bitwise NOT still sees a lane that should now be off as nonzero (truthy).

Repro (tests/semantic/lds_run.py, CPU-only, 64 lanes):

s_bitreplicate_b64_b32 vcc, 0x1
s_not_b64 vcc, vcc

Expected: lanes 0-1 -> 0, lanes 2-63 -> 1 (bit 0 of the source replicates to lanes 0 and 1; NOT flips every lane). Got on this head: all 64 lanes -> 1. #8714's mask = 0 - bit (two's complement) through mask_dst avoids this; the same repro passes on its head.

No self.stage == "cs" guard. The new branch only checks the destination type, not the stage, so a pixel shader with s_bitreplicate_b64_b32 exec, 8 also translates instead of staying Unsupported, even though SHADER-1289 and the measured corpus are compute-only. Confirmed by translating a synthetic ps_*.s with just that instruction against this head: it's accepted. #8526 (EXEC) has the same gap.

AI-assisted review.

@nonverifiedperson

Copy link
Copy Markdown
Contributor

GPU and silicon confirmation of @CreaCloudz's VCC finding. This also corrects my last comment: the vcc_lo point stands, but I called the bare OpStore %vcc {bit} "right for every encodable form", and it isn't. It stores 0/1 where VCC must hold the 0/~0 mask form.

The test is a wave64 compute shader (group 64, --require-wave64). s4 = 1 comes through v_readfirstlane_b32 + s_add_u32, so the translator can't fold it to a constant. Each lane then takes v_cndmask_b32_e64 v, 0, 1, vcc:

sequence gfx1036 silicon (hw_oracle, wave64) 74859f4 + main, translated, Radeon (subgroup 64)
s_bitreplicate_b64_b32 vcc, s4; s_not_b64 vcc, vcc 0xfffffffffffffffc 0xffffffffffffffff ✗
s_bitreplicate_b64_b32 s[8:9], s4; s_not_b64 vcc, s[8:9] 0xfffffffffffffffc 0xfffffffffffffffc

So on AMD Windows hardware, a NOT after the VCC write keeps lanes 0 and 1 on. Through an SGPR pair it's right, because that path already goes through the mask form. My earlier 12/12 VCC run only read VCC straight back with v_cndmask, where 0/1 and 0/~0 look the same, so it couldn't catch this.

Storing 0 - bit (or routing through mask_dst, as #8714 does) gives the mask form. The vcc_lo destination is still one llvm-mc rejects for this 64-bit op.

Windows 10 22H2, AMD Radeon iGPU gfx1036 (driver 32.0.13036.4), Vulkan SDK 1.4.363. Synthetic values only. AI-assisted (Claude Code).

@Chrysana

Copy link
Copy Markdown

Thanks for this! #8714 fixes the same s_bitreplicate_b64_b32 case, and per CreaCloudz's repro above (s_not_b64 vcc, vcc after it), the raw 0/1 VCC store here gives the wrong lanes, while #8714's 0/~0 mask through mask_dst passes. Would you consider closing this in favour of #8714, or folding the EXEC handling into #8526? (PR cleanup, #8919) agent: yes

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.