shader: lower 32-bit bit counts and scans in the direct compute path #3051

Manually merged
github merged 1 commit from gh-pr/3051/head into gh-pr/3051/base 2026-11-27 11:53:49 +00:00
Owner

@⁠pigalot on GitHub · 2026-11-27 11:37 UTC · original
pigalot:shader/direct-bitscan → main · +292 −0 · 4 files · 1 commits · merged 2026-11-27 11:53 UTC

Fixes #3050 (part of #2935, instruction coverage).

Change

The direct compute frontend (src/gfx1013_direct_compute.inl) used to refuse the 32-bit bit counts and bit scans with direct DXIL lowering for <name>. They now lower to DXIL Countbits (31), FirstbitLo (32), FirstbitHi (33) and FirstbitSHi (34):

Operation Result SCC
s_bcnt0_i32_b32, s_bcnt1_i32_b32 number of zero or one bits of S0 D != 0
s_ff0_i32_b32, s_ff1_i32_b32, v_ffbl_b32 index of the first zero or one bit, counted from bit 0 unchanged
s_flbit_i32_b32, v_ffbh_u32 position of the first one bit, counted from bit 31 unchanged
s_flbit_i32, v_ffbh_i32 position of the first bit that differs from the sign bit, counted from bit 31 unchanged
  • No qualifying bit: every scan returns 0xffffffff.
  • Agreement with existing lowerings: the rules are those of gfx2hlsl.py (op_s_ff1_i32_b32, op_s_flbit_i32_b32 and the s_bcnt* table) and of tools/pixel_aot.py (dpp_ffbh_u32, dpp_ffbh_i32, bcnt1_i32_b32).
  • Why there is no 31 - correction: DXIL FirstbitHi and FirstbitSHi already count from bit 31. dxc -T cs_6_0 compiles HLSL firstbithigh(x) to unaryBits(33, x) followed by sub i32 31, … and a select for -1.
  • Not covered: VOP3 source/output modifiers stay refused with direct ALU operand modifiers, as for the other integer VOP3 forms. The 64-bit scalar forms are left for later.

Verification

main 1c879506 plus this change (44f24d71). Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 5090 (driver 32.0.16.1088), dev preset with the debug layer on.

ctest --preset dev --no-tests=error -R '^native_bitscan_' -V
Check Result
native_bitscan_controls pass: compiled; clamp refused; the wrong-end, unsigned, zero-is-32, SCC-dropped and SCC-written references each differ
native_bitscan_hardware pass: bit counts and scans: 1024 words of two groups and the guards matched
Lowering removed (main source) both fail: native compute +0x40: direct DXIL lowering for v_ffbh_u32
Lowering mutated: FirstbitLo and FirstbitHi exchanged readback fails
Lowering mutated: the scans also write SCC readback fails at word 20 (the SCC after s_bcnt1 then s_ff1)
ctest -R '^native_' -E '_hardware$' -j 8 42 of 43 passed, see below
docs_links_check, bidi_controls_check pass
  • Already failing on main 98007de5 without this change: native_sdwa_f16_ir, native_sdwa_f16_hardware and native_cvt_pk_u8_hardware. native_sdwa_f16_ir fails the same way at 1c879506 with it.
    • The two sdwa_f16 checks report the same value as the expected one, for example (0, 'add', '0xa55a4200', '0xa55a4200'), so their matches() seems to reject an equal word.
    • cvt_pk_u8 differs in the low byte, 0xa5a5a501 against 0xa5a5a500.
  • Not run: this readback has not been run on WARP or on an AMD GPU.

Evidence and follow-up

  • Fixture: tests/native_bitscan_check.py (tests/cmake/native_bitscan.cmake), with notes in docs/experiments/native-direct-bitscan.md. It is original and uses no external inputs.
    • The program is built with the encoding helpers of the other native fixtures (sop1, scalar, vop2, vmove). Every word was checked with tools/disasm_rdna2.py --hex. LLVM was not available with the AMDGPU target here.
    • Inputs, over two groups of 32 lanes:
      • x = (0x80000000 >> g) >> L, shifted arithmetically: negative values down to 0xffffffff in group 0, and single bits down to 0 in group 1.
      • z = x ^ mul_u32_u24(L + 32g, 0x9e3779).
      • Scalar inputs s9 = g * 0xff0f0010 (0 in group 0) and s10 = ~s9.
  • Local start-up capture: I ran compile_dxil --refusals (counts only) on 2,689 compute requests.
    • 12 requests stopped first at these names.
    • With this change, 6 of them compile, so the total goes from 392 to 398.
    • The rest stop later: 2 at s_absdiff_i32, 2 at unbound numeric SGPR, 1 at direct descriptor address outside bound table and 1 at non-contiguous resource descriptor.

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. Only the new experiment note; no README row covers this path.
  • I did not include game files, dumps, distribution links or secrets.

This change was AI-assisted.

**@⁠pigalot** on GitHub · 2026-11-27 11:37 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3051) `pigalot:shader/direct-bitscan` → `main` · +292 −0 · 4 files · 1 commits · merged 2026-11-27 11:53 UTC Fixes #3050 (part of #2935, instruction coverage). ## Change The direct compute frontend (`src/gfx1013_direct_compute.inl`) used to refuse the 32-bit bit counts and bit scans with `direct DXIL lowering for <name>`. They now lower to DXIL `Countbits` (31), `FirstbitLo` (32), `FirstbitHi` (33) and `FirstbitSHi` (34): | Operation | Result | SCC | |---|---|---| | `s_bcnt0_i32_b32`, `s_bcnt1_i32_b32` | number of zero or one bits of S0 | D != 0 | | `s_ff0_i32_b32`, `s_ff1_i32_b32`, `v_ffbl_b32` | index of the first zero or one bit, counted from bit 0 | unchanged | | `s_flbit_i32_b32`, `v_ffbh_u32` | position of the first one bit, counted from bit 31 | unchanged | | `s_flbit_i32`, `v_ffbh_i32` | position of the first bit that differs from the sign bit, counted from bit 31 | unchanged | - **No qualifying bit:** every scan returns 0xffffffff. - **Agreement with existing lowerings:** the rules are those of `gfx2hlsl.py` (`op_s_ff1_i32_b32`, `op_s_flbit_i32_b32` and the `s_bcnt*` table) and of `tools/pixel_aot.py` (`dpp_ffbh_u32`, `dpp_ffbh_i32`, `bcnt1_i32_b32`). - **Why there is no `31 -` correction:** DXIL `FirstbitHi` and `FirstbitSHi` already count from bit 31. `dxc -T cs_6_0` compiles HLSL `firstbithigh(x)` to `unaryBits(33, x)` followed by `sub i32 31, …` and a select for -1. - **Not covered:** VOP3 source/output modifiers stay refused with `direct ALU operand modifiers`, as for the other integer VOP3 forms. The 64-bit scalar forms are left for later. ## Verification `main` `1c879506` plus this change (`44f24d71`). Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 5090 (driver 32.0.16.1088), dev preset with the debug layer on. ```powershell ctest --preset dev --no-tests=error -R '^native_bitscan_' -V ``` | Check | Result | |---|---| | `native_bitscan_controls` | pass: `compiled; clamp refused; the wrong-end, unsigned, zero-is-32, SCC-dropped and SCC-written references each differ` | | `native_bitscan_hardware` | pass: `bit counts and scans: 1024 words of two groups and the guards matched` | | Lowering removed (`main` source) | both fail: `native compute +0x40: direct DXIL lowering for v_ffbh_u32` | | Lowering mutated: `FirstbitLo` and `FirstbitHi` exchanged | readback fails | | Lowering mutated: the scans also write SCC | readback fails at word 20 (the SCC after `s_bcnt1` then `s_ff1`) | | `ctest -R '^native_' -E '_hardware$' -j 8` | 42 of 43 passed, see below | | `docs_links_check`, `bidi_controls_check` | pass | - **Already failing on `main` `98007de5` without this change:** `native_sdwa_f16_ir`, `native_sdwa_f16_hardware` and `native_cvt_pk_u8_hardware`. `native_sdwa_f16_ir` fails the same way at `1c879506` with it. - The two `sdwa_f16` checks report the same value as the expected one, for example `(0, 'add', '0xa55a4200', '0xa55a4200')`, so their `matches()` seems to reject an equal word. - `cvt_pk_u8` differs in the low byte, `0xa5a5a501` against `0xa5a5a500`. - **Not run:** this readback has not been run on WARP or on an AMD GPU. ## Evidence and follow-up - **Fixture:** `tests/native_bitscan_check.py` (`tests/cmake/native_bitscan.cmake`), with notes in `docs/experiments/native-direct-bitscan.md`. It is original and uses no external inputs. - The program is built with the encoding helpers of the other native fixtures (`sop1`, `scalar`, `vop2`, `vmove`). Every word was checked with `tools/disasm_rdna2.py --hex`. LLVM was not available with the AMDGPU target here. - Inputs, over two groups of 32 lanes: - x = (0x80000000 >> g) >> L, shifted arithmetically: negative values down to 0xffffffff in group 0, and single bits down to 0 in group 1. - z = x ^ mul_u32_u24(L + 32g, 0x9e3779). - Scalar inputs s9 = g * 0xff0f0010 (0 in group 0) and s10 = ~s9. - **Local start-up capture:** I ran `compile_dxil --refusals` (counts only) on 2,689 compute requests. - 12 requests stopped first at these names. - With this change, 6 of them compile, so the total goes from 392 to 398. - The rest stop later: 2 at `s_absdiff_i32`, 2 at `unbound numeric SGPR`, 1 at `direct descriptor address outside bound table` and 1 at `non-contiguous resource descriptor`. ## 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. Only the new experiment note; no README row covers this path. - [x] I did not include game files, dumps, distribution links or secrets. This change was AI-assisted.
The direct compute frontend refused s_bcnt0_i32_b32, s_bcnt1_i32_b32,
s_ff0_i32_b32, s_ff1_i32_b32, s_flbit_i32_b32, s_flbit_i32, v_ffbl_b32,
v_ffbh_u32 and v_ffbh_i32. They now lower to DXIL Countbits, FirstbitLo,
FirstbitHi and FirstbitSHi:
- FirstbitHi and FirstbitSHi count from bit 31, as the gfx10 scans do.
- All the scans return ~0 when no bit qualifies.
- Only the bit counts write SCC (D != 0).

tests/native_bitscan_check.py dispatches the scans over negative values,
single bits, 0 and ~0 against a CPU reference. It refuses the clamp
form, and checks that references with the wrong counting direction, an
unsigned signed-scan, a 32-for-zero result, a bit count that leaves SCC
alone and a scan that writes SCC each differ.

Fixes #3050. AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 11:53:47 +00:00
github manually merged commit 44f24d710f into gh-pr/3051/base 2026-11-27 11:53:49 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-27 11:52 UTC · original · approved these changes

Reviewed 44f24d71 with main 298a2034 (integration tree 5c5734ff). I rebuilt and ran ctest --preset <dev|release> --no-tests=error -V -R '^(native_bitscan_(controls|hardware)|native_f16_alu_(ir|hardware)|native_global_srv_heap_replay_(hardware|warp))$'. Each preset gave five passes and one WARP capability skip in the unrelated heap replay. The RTX matched every one of the 1,024 output words and the guards. The control compiled, rejected clamp and distinguished the wrong scan end, unsigned scan, no-match handling and both SCC mutations. Windows 11 26200.9974, SDK DXC 10.0.26100, RTX 4060 Laptop driver 32.0.16.1656, WARP 10.0.26100.9818. The native replay fixture does not enable the D3D12 debug layer. Hosted Windows, Linux Python and gfx1013 checks passed.

**@⁠R0Y00** on GitHub · 2026-11-27 11:52 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3051#pullrequestreview-6087717716) · **approved these changes** Reviewed `44f24d71` with `main` `298a2034` (integration tree `5c5734ff`). I rebuilt and ran `ctest --preset <dev|release> --no-tests=error -V -R '^(native_bitscan_(controls|hardware)|native_f16_alu_(ir|hardware)|native_global_srv_heap_replay_(hardware|warp))$'`. Each preset gave five passes and one WARP capability skip in the unrelated heap replay. The RTX matched every one of the 1,024 output words and the guards. The control compiled, rejected clamp and distinguished the wrong scan end, unsigned scan, no-match handling and both SCC mutations. Windows 11 26200.9974, SDK DXC 10.0.26100, RTX 4060 Laptop driver 32.0.16.1656, WARP 10.0.26100.9818. The native replay fixture does not enable the D3D12 debug layer. Hosted Windows, Linux Python and gfx1013 checks passed.
This discussion has been locked. Commenting is limited to contributors.
No description provided.