Repository navigation
shader: lower v_alignbit_b32, v_alignbyte_b32 and v_bfm_b32 in the direct C++ compute path - #2986
Conversation
7464be9 to
3a485dd
Compare
…rect C++ compute path
The direct compute frontend refused these three VOP3 bit operations.
They now follow the gfx2hlsl.py VOP3 table: v_alignbit_b32 returns
bits [k + 31 : k] of {S0, S1} with k = S2[4:0], v_alignbyte_b32 the
same with k = 8 * S2[1:0], both returning S1 for k = 0, and v_bfm_b32
is ((1 << S0[4:0]) - 1) << S1[4:0] like s_bfm_b32. The zero case is a
select, so the masked shift in the other arm stays in range.
tests/native_align_bfm_check.py dispatches every shift 0 to 31 and
every byte count, repeated with bit 5 set, against a CPU reference,
and its controls check that the zero-shift, exchanged-source and
unmasked references differ.
Refs R0Y00#2985. AI-assisted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3a485dd to
a21837d
Compare
KelboLive
left a comment
There was a problem hiding this comment.
Good, systematic additions — alignbits, alignbyte and bfm all lowered faithfully against the gfx2hlsl.py VOP3 table expressions.
Findings
✅ What works well
- alignbits/alignbyte: The k computation is correct —
and(val(2), "31")masks to 5 bits for alignbits,shl(and(val(2), "3"), "3")masks to 3 bits then shifts by 8 for alignbyte. Thejoinoperation (or(lshr({S0,S1}, k), shl(S0, sub(32, k)))) is the classic rotate-within-64 pattern, correct for extracting bit range[k+31:k]. Theselect(eq(k, "0"), b, joined)handles the degenerate k=0 case (which would produce shift-by-32 = undefined). - bfm:
shift("shl", sub(shift("shl", "1", a), "1"), b)—((1 << S0) - 1) << S1is the BFM encoding:S0[4:0]width mask shifted byS1[4:0]start. Matchess_bfm_b32exactly as noted. - shift() reuse: Both instructions use the existing
shift()helper which masks shift amounts to 5 bits, so32-kstays in [1, 32] range regardless of input. - Test fixture: 2 groups × 32 lanes with S2 ranging 0–63 covers every shift amount 0–31 and byte count 0–3 twice (group 0: bits 0–4, group 1: bits 0–4 with bit 5 set → ignored). Excellent coverage.
- Mutant references: Zero-shift not special-cased (
zero_special=Falseproduceslow | highinstead of justlow), S0/S1 exchanged, and field unmasked (uses 63-bit field) — all independently differ from expected output.
⚠️ Minor observations
-
v_bfm_b32mask on S0: Theshift("shl", "1", a)whereaisoperand32(i, i.src[0])— ifi.src[0]is an inline float (VGPR 240–248), it would be cast to an integer and shift by a large amount. However, theshift()helper masks the shift to 5 bits, so this is safe. If this instruction should also refuse inline floats (likev_mad_u64_u32does), consider adding that check explicitly — though it may not be needed sincebfmwith large widths produces full masks. -
alignbyte byte alignment: The
shl(and(val(2), "3"), "3")correctly implements8 * S2[1:0]. The& 3keeps only bits 0–1, then<< 3multiplies by 8 (one byte). This matches the AMDISA spec whereS2[1:0]is the byte index.
Recommendation
Ready to land. Clean implementation with good test coverage.
🏷 This PR found the instructions via the "local probe" mentioned in the docs — consider whether other VOP3 names with known gfx2hlsl.py lowering that aren't yet implemented could be found the same way. The author noted 79 names were refused on the current head.
MasterDomino
left a comment
There was a problem hiding this comment.
Review of head a21837d1. CI on the head is green. This is the wave32 SM 6.6 readback you asked for. Approving and merging.
Code read:
v_alignbit_b32/v_alignbyte_b32:kisS2[4:0]or8 * S2[1:0]. The joined value is(S1 >> k) | (S0 << (32 - k))through the maskingshift()helper, andk = 0selects S1, so the 32-bit shift arm never matters.v_bfm_b32:((1 << S0[4:0]) - 1) << S1[4:0], thes_bfm_b32rule.
Run on AMD: Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), plus WARP, release. This was tested together with #2996, #2998, #3003 and #2975 merged onto cpp-migration e0d83759; all five merge cleanly with each other. ctest --preset release --no-tests=error -R '^(native_.*|startup_compute_dispatch(_.*)?_(warp|hardware)|startup_slot67_indirect_dispatch_warp|startup_compute_translate_e2e_warp|startup_stream_draw.*|startup_native_graphics.*)$' gives 94 tests, 90 passed and 4 skipped (77, WARP has no 32-lane waves). native_align_bfm_hardware and native_align_bfm_controls passed.
Mutation here: returning S0 instead of S1 for k = 0 fails native_align_bfm_hardware ((8, '0x89abcdef', '0x1234567') and the other zero-shift lanes). With it restored, it passes.
I will close #2985 by hand.
AI-assisted; the commands above were run locally.
Change
The direct compute frontend now lowers
v_alignbit_b32,v_alignbyte_b32andv_bfm_b32. Before, it refused them withdirect DXIL lowering for <name>. Refs #2985, part of the integer-ALU coverage in #2935.The semantics are the expressions of the
gfx2hlsl.pyVOP3 table:v_alignbit_b32v_alignbyte_b32v_bfm_b32s_bfm_b32already uses hereShift amounts are masked to five bits, as
shift()does. The zero case is a select, so the shift by 32 - k in the other arm stays in range.I found the three with a local probe that compiles one instruction per VOP3 name of
gfx1013_isa_tables.inlthroughgfx1013-direct-cs@0. On05438d65it saw 79 names refused; these three have agfx2hlsl.pylowering and no issue or PR.Verification
Commit
7464be92oncpp-migration4ff5e36d, Windows 10.0.26100.9711,devpreset. NVIDIA Quadro M1000M, driver 32.0.15.7371, debug layer unavailable.ctest --preset dev --no-tests=error -R '^native_align_bfm_|^native_shift64_controls$|^native_direct_container$': 3 passed, 1 skipped.native_align_bfm_controlspassed. It compiles the program and checks that the zero-shift, exchanged-source and unmasked references each differ from the expected words.native_align_bfm_hardwareskipped (77).native_store_replayneeds shader model 6.6 with wave32. The readback is not measured here; a run on an SM 6.6 wave32 adapter would be welcome, as for shader: lower root-pointer global_load_dword in the direct C++ compute path #2966.python -B tests/docs_links_check.py: ok.Evidence and follow-up
The fixture words were assembled with LLVM 15.0.7 (
clang -cc1as -triple amdgcn -target-cpu gfx1013 -filetype asm -show-encoding).The dispatch has two groups of 32 lanes. Lane L of group g uses S0 = 0x89abcdef, S1 = 0x01234567 and S2 = L + 32g, so every shift from 0 to 31 and every byte count appears. Group 1 repeats them with bit 5 set, which the field must ignore.
v_bfm_b32takes S0 = S2 and S1 = S2 ^ 0x15. The 192 results and the guard words are compared with a CPU reference.The note is
docs/experiments/native-direct-align-bfm.md.Checklist
AI-assisted.
🤖 Generated with Claude Code