Skip to content

shader: lower v_alignbit_b32, v_alignbyte_b32 and v_bfm_b32 in the direct C++ compute path - #2986

Merged
MasterDomino merged 1 commit into
R0Y00:cpp-migrationfrom
legacy-code-archaeologist:shader/direct-alignbit-bfm
Nov 27, 2026
Merged

MasterDomino merged 1 commit into
R0Y00:cpp-migrationfrom
legacy-code-archaeologist:shader/direct-alignbit-bfm

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Change

The direct compute frontend now lowers v_alignbit_b32, v_alignbyte_b32 and v_bfm_b32. Before, it refused them with direct DXIL lowering for <name>. Refs #2985, part of the integer-ALU coverage in #2935.

The semantics are the expressions of the gfx2hlsl.py VOP3 table:

Instruction Result
v_alignbit_b32 bits [k + 31 : k] of {S0, S1}, k = S2[4:0]; S1 when k = 0
v_alignbyte_b32 the same with k = 8 * S2[1:0]
v_bfm_b32 ((1 << S0[4:0]) - 1) << S1[4:0], the rule s_bfm_b32 already uses here

Shift 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.inl through gfx1013-direct-cs@0. On 05438d65 it saw 79 names refused; these three have a gfx2hlsl.py lowering and no issue or PR.

Verification

Commit 7464be92 on cpp-migration 4ff5e36d, Windows 10.0.26100.9711, dev preset. 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_controls passed. It compiles the program and checks that the zero-shift, exchanged-source and unmasked references each differ from the expected words.
    • native_align_bfm_hardware skipped (77). native_store_replay needs 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_b32 takes 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

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

🤖 Generated with Claude Code

…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>
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Rebased onto cpp-migration e0d83759 after #2990 and #2994 merged; they insert at the same place in the integer-ALU chain, and both blocks are kept. The controls and native_direct_container pass again on the rebased head. The readback still skips here (77).

AI-assisted.

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

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. The join operation (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]. The select(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) << S1 is the BFM encoding: S0[4:0] width mask shifted by S1[4:0] start. Matches s_bfm_b32 exactly as noted.
  • shift() reuse: Both instructions use the existing shift() helper which masks shift amounts to 5 bits, so 32-k stays 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=False produces low | high instead of just low), S0/S1 exchanged, and field unmasked (uses 63-bit field) — all independently differ from expected output.

⚠️ Minor observations

  1. v_bfm_b32 mask on S0: The shift("shl", "1", a) where a is operand32(i, i.src[0]) — if i.src[0] is an inline float (VGPR 240–248), it would be cast to an integer and shift by a large amount. However, the shift() helper masks the shift to 5 bits, so this is safe. If this instruction should also refuse inline floats (like v_mad_u64_u32 does), consider adding that check explicitly — though it may not be needed since bfm with large widths produces full masks.

  2. alignbyte byte alignment: The shl(and(val(2), "3"), "3") correctly implements 8 * S2[1:0]. The & 3 keeps only bits 0–1, then << 3 multiplies by 8 (one byte). This matches the AMDISA spec where S2[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 MasterDomino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: k is S2[4:0] or 8 * S2[1:0]. The joined value is (S1 >> k) | (S0 << (32 - k)) through the masking shift() helper, and k = 0 selects S1, so the 32-bit shift arm never matters.
  • v_bfm_b32: ((1 << S0[4:0]) - 1) << S1[4:0], the s_bfm_b32 rule.

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.

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.

3 participants