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

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/2986 into cpp-migration 2026-11-27 04:35:45 +00:00
legacy-code-archaeologist commented 2026-11-27 03:22:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T03:22:46Z.


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 #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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5520621022 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2986). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T03:22:46Z. --- ## 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 #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 - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
legacy-code-archaeologist commented 2026-11-27 03:58:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T03:58:37Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7209419303 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2986#issuecomment-7209419303). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T03:58:37Z. --- 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 commented 2026-11-27 04:16:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T04:16:14Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083426325 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2986#pullrequestreview-6083426325). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-27T04:16:14Z. **Review:** COMMENTED --- 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 commented 2026-11-27 04:35:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-27T04:35:42Z.

Review: APPROVED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083534557 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2986#pullrequestreview-6083534557). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-27T04:35:42Z. **Review:** APPROVED --- 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 in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2986
No description provided.