shader: lower compute v_mbcnt_lo/hi_u32_b32 with a uniform mask #1775

Merged
henriazz10 merged 1 commit from upstream-pr/1782 into main 2026-11-24 18:48:33 +00:00
henriazz10 commented 2026-11-24 18:34:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by henriazz10 on 2026-11-24T18:34:43Z.


Change

Refs #1543. This is the v_mbcnt_lo_u32_b32 / v_mbcnt_hi_u32_b32 slice claimed in https://github.com/R0Y00/Xbox2Windows/issues/1543#issuecomment-7135171733. It is separate from the v_or_b32_dpp slice and touches only tools/compute_store_aot.py, its test and the docs.

  • v_mbcnt_lo_u32_b32 vD, mask, base lowers to countbits(mask & ((1u << (gindex & 31u)) - 1u)) + base. gindex is SV_GroupIndex, the flat local thread index. gfx10 numbers the lanes of a wave by that index modulo the wave size, so the result does not depend on the desktop wave width and uses no wave intrinsic. exec is not read, matching the instruction.
  • v_mbcnt_hi_u32_b32 vD, mask, base lowers to base: a wave32 lane has no bit in the high half of the mask.
  • A group of at most 32 threads is one wave of either size, so it needs no statement. A larger group needs the caller's statement wave_size=32 (--wave-size 32, a new optional lower() argument and CLI option). Without it the program could be wave64, and it is refused with Unverified lane count operand or wave size. wave_size=64 is refused.
  • The mask must be uniform: an inline constant, a literal, or an SGPR that holds a known word. A VGPR mask, a scalar still loading, an unknown scalar and an unknown base are refused. Each refusal has a control in mbcnt_controls().
  • The semantics match the existing per-lane evaluation of the same opcodes in tools/mesh_entry_aot.py for lanes 0–31.

s_bcnt1_i32_b32 ... exec_lo, v_readlane, v_writelane and ds_bpermute are not part of this PR. The other parts of #1543 remain open.

Verification

Commit 65660899, rebased onto 67319a17. Windows 11 build 26300, VS 2019 Build Tools 16.11 (MSVC 14.29), DXC 1.8.2502.11, Python 3.12, debug layer enabled. offscreen_check was built from an earlier main (0aa6522c); this PR does not change it. The rebase resolved conflicts with the rsrc2, image_float_domain and v_cvt_pk_u8_f32 work in lower()'s signature, its CLI and docs/experiments.md. The controls, both draws and the LLVM 15 run below were repeated on 65660899.

python -B tests/compute_store_check.py draw-mbcnt <dxc> <offscreen_check> warp
python -B tests/compute_store_check.py draw-mbcnt <dxc> <offscreen_check> hardware
Backend Adapter / driver Result
WARP Microsoft Basic Render Driver 10.0.26100.9818 exit 0, compared_words=320 mismatches=0, debug_errors=0
hardware NVIDIA GeForce RTX 5080, 32.0.16.1692 exit 0, compared_words=320 mismatches=0, debug_errors=0

No capability skips.

The LLVM 15 round trip ran separately from the GPU runs, in Ubuntu 22.04 (llvm-15 15.0.7 from apt, as in the Linux CI job):

python3 -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1   # exit 0

mbcnt_binary() assembles the fixture, disassembles it and requires the same v_mbcnt lines and the same translation.

Before the rebase, on 4c83e0cc over 81df4051, there were no regressions on WARP with the same toolchain: draw, draw-guard-read, draw-f32, draw-lds-private and draw-mubuf-format-store all exit 0. The new CTest entries are compute_store_aot_mbcnt_warp and compute_store_aot_mbcnt_hardware. I ran the script directly with the commands above, not through CTest.

Evidence and follow-up

The fixture is source-built and hand-written in LLVM 15 spelling. It runs one group of 64 threads, two wave32 waves, and each thread stores three words: mbcnt_lo(-1, 0), mbcnt_lo(0x5a5a5a5a, 7) and mbcnt_hi(-1, lane). A Python model gives the expected words by counting bits below flat % 32. All 320 words are compared.

  • Reference controls. Each must fail at the first word where it differs:
    • the flat index used as the lane: word 96, the first thread of the second wave;
    • the mask inverted: word 4;
    • the base dropped: word 1;
    • a changed sentinel past the group: word 192.
  • Recompiled-HLSL controls. Each must fail on the device:
    • a lane that saturates at 31;
    • bits counted above the lane instead of below;
    • the base dropped;
    • mbcnt_hi adding 32.
  • One finding while writing the controls: a lane taken modulo 64 is not a usable mutation. HLSL masks a 32-bit shift count to five bits, so 1u << (gindex & 63u) equals the modulo-32 form on both adapters. docs/experiments.md records this next to the new section.

Assumption: the wave32 statement belongs to the caller, as for the existing ds_*_addtid path. Nothing in the container proves the wave size. Wave64 programs (lane = flat % 64, with mbcnt_hi counting the high half) are left for a follow-up.

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 (docs/experiments.md).
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code, Claude Opus 5.5). The measurements above come from runs on the machine described in Verification.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6588770193 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1782). Originally posted by [henriazz10](https://github.com/henriazz10) on 2026-11-24T18:34:43Z. --- ## Change Refs #1543. This is the `v_mbcnt_lo_u32_b32` / `v_mbcnt_hi_u32_b32` slice claimed in https://github.com/R0Y00/Xbox2Windows/issues/1543#issuecomment-7135171733. It is separate from the `v_or_b32_dpp` slice and touches only `tools/compute_store_aot.py`, its test and the docs. - `v_mbcnt_lo_u32_b32 vD, mask, base` lowers to `countbits(mask & ((1u << (gindex & 31u)) - 1u)) + base`. `gindex` is `SV_GroupIndex`, the flat local thread index. gfx10 numbers the lanes of a wave by that index modulo the wave size, so the result does not depend on the desktop wave width and uses no wave intrinsic. `exec` is not read, matching the instruction. - `v_mbcnt_hi_u32_b32 vD, mask, base` lowers to `base`: a wave32 lane has no bit in the high half of the mask. - A group of at most 32 threads is one wave of either size, so it needs no statement. A larger group needs the caller's statement `wave_size=32` (`--wave-size 32`, a new optional `lower()` argument and CLI option). Without it the program could be wave64, and it is refused with `Unverified lane count operand or wave size`. `wave_size=64` is refused. - The mask must be uniform: an inline constant, a literal, or an SGPR that holds a known word. A VGPR mask, a scalar still loading, an unknown scalar and an unknown base are refused. Each refusal has a control in `mbcnt_controls()`. - The semantics match the existing per-lane evaluation of the same opcodes in `tools/mesh_entry_aot.py` for lanes 0–31. `s_bcnt1_i32_b32 ... exec_lo`, `v_readlane`, `v_writelane` and `ds_bpermute` are not part of this PR. The other parts of #1543 remain open. ## Verification Commit `65660899`, rebased onto `67319a17`. Windows 11 build 26300, VS 2019 Build Tools 16.11 (MSVC 14.29), DXC 1.8.2502.11, Python 3.12, debug layer enabled. `offscreen_check` was built from an earlier `main` (`0aa6522c`); this PR does not change it. The rebase resolved conflicts with the `rsrc2`, `image_float_domain` and `v_cvt_pk_u8_f32` work in `lower()`'s signature, its CLI and `docs/experiments.md`. The controls, both draws and the LLVM 15 run below were repeated on `65660899`. ```powershell python -B tests/compute_store_check.py draw-mbcnt <dxc> <offscreen_check> warp python -B tests/compute_store_check.py draw-mbcnt <dxc> <offscreen_check> hardware ``` | Backend | Adapter / driver | Result | |---|---|---| | WARP | Microsoft Basic Render Driver 10.0.26100.9818 | exit 0, `compared_words=320 mismatches=0`, `debug_errors=0` | | hardware | NVIDIA GeForce RTX 5080, 32.0.16.1692 | exit 0, `compared_words=320 mismatches=0`, `debug_errors=0` | No capability skips. The LLVM 15 round trip ran separately from the GPU runs, in Ubuntu 22.04 (`llvm-15` 15.0.7 from apt, as in the Linux CI job): ```sh python3 -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 # exit 0 ``` `mbcnt_binary()` assembles the fixture, disassembles it and requires the same `v_mbcnt` lines and the same translation. Before the rebase, on `4c83e0cc` over `81df4051`, there were no regressions on WARP with the same toolchain: `draw`, `draw-guard-read`, `draw-f32`, `draw-lds-private` and `draw-mubuf-format-store` all exit 0. The new CTest entries are `compute_store_aot_mbcnt_warp` and `compute_store_aot_mbcnt_hardware`. I ran the script directly with the commands above, not through CTest. ## Evidence and follow-up The fixture is source-built and hand-written in LLVM 15 spelling. It runs one group of 64 threads, two wave32 waves, and each thread stores three words: `mbcnt_lo(-1, 0)`, `mbcnt_lo(0x5a5a5a5a, 7)` and `mbcnt_hi(-1, lane)`. A Python model gives the expected words by counting bits below `flat % 32`. All 320 words are compared. - **Reference controls.** Each must fail at the first word where it differs: - the flat index used as the lane: word 96, the first thread of the second wave; - the mask inverted: word 4; - the base dropped: word 1; - a changed sentinel past the group: word 192. - **Recompiled-HLSL controls.** Each must fail on the device: - a lane that saturates at 31; - bits counted above the lane instead of below; - the base dropped; - `mbcnt_hi` adding 32. - **One finding while writing the controls:** a lane taken modulo 64 is not a usable mutation. HLSL masks a 32-bit shift count to five bits, so `1u << (gindex & 63u)` equals the modulo-32 form on both adapters. `docs/experiments.md` records this next to the new section. Assumption: the wave32 statement belongs to the caller, as for the existing `ds_*_addtid` path. Nothing in the container proves the wave size. Wave64 programs (`lane = flat % 64`, with `mbcnt_hi` counting the high half) are left for a follow-up. ## 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 (`docs/experiments.md`). - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code, Claude Opus 5.5). The measurements above come from runs on the machine described in Verification. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 19:01:29 +00:00
tedin7 commented 2026-11-24 19:01:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T18:48:08Z.

Review: COMMENTED


Verified commit 65660899028ae93b524102cb353278afce1ed94e — no defects found.

Checks run locally (Linux)

  • git diff --check origin/main...HEAD — passed
  • python -B tests/docs_links_check.py — passed (17 Markdown files, 366 links)
  • python -B tests/bidi_controls_check.py — passed (1303 tracked files)
  • python -B tests/compute_store_check.py binary llvm-mc-19 llvm-objcopy-19 /usr/lib/llvm-19/lib/libLLVM-19.so — passed (includes mbcnt_controls() and mbcnt_binary() assemble/disassemble round-trip)
  • python -B tests/rdna2_tools_check.py — passed (148 tests, 18 skipped)
  • python -B tests/form_coverage_check.py — passed

Mutation controls (reverted cleanly after each)

  1. Disabled wave_size != 32 check in tools/compute_store_aot.py -> mbcnt_controls() failed (AssertionError: Expected rejection: Unverified lane count operand or wave size).
  2. Dropped + {hlsl_word(base)} in v_mbcnt_lo_u32_b32 lowering -> mbcnt_controls() failed on exact HLSL output check.
  3. Changed v_mbcnt_hi_u32_b32 lowering to add 32u instead of passing through base -> mbcnt_controls() failed on exact HLSL output check.

Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP)

  • Debug (--preset dev): ctest --preset dev -R '^(compute_store_aot_mbcnt_warp|compute_store_aot_warp|compute_store_aot_guard_read_warp|compute_store_aot_f32_warp)$' --parallel 6 --no-tests=error --output-on-failure — 4/4 passed (compute_store_aot_mbcnt_warp 4.43s)
  • Release /O2 (--preset release): ctest --preset release -R '^(compute_store_aot_mbcnt_warp|compute_store_aot_warp|compute_store_aot_guard_read_warp|compute_store_aot_f32_warp)$' --parallel 6 --no-tests=error --output-on-failure — 4/4 passed (compute_store_aot_mbcnt_warp 4.49s)

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6045315832 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1782#pullrequestreview-6045315832). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T18:48:08Z. **Review:** COMMENTED --- Verified commit `65660899028ae93b524102cb353278afce1ed94e` — no defects found. ### Checks run locally (Linux) - `git diff --check origin/main...HEAD` — passed - `python -B tests/docs_links_check.py` — passed (17 Markdown files, 366 links) - `python -B tests/bidi_controls_check.py` — passed (1303 tracked files) - `python -B tests/compute_store_check.py binary llvm-mc-19 llvm-objcopy-19 /usr/lib/llvm-19/lib/libLLVM-19.so` — passed (includes `mbcnt_controls()` and `mbcnt_binary()` assemble/disassemble round-trip) - `python -B tests/rdna2_tools_check.py` — passed (148 tests, 18 skipped) - `python -B tests/form_coverage_check.py` — passed ### Mutation controls (reverted cleanly after each) 1. Disabled `wave_size != 32` check in `tools/compute_store_aot.py` -> `mbcnt_controls()` failed (`AssertionError: Expected rejection: Unverified lane count operand or wave size`). 2. Dropped `+ {hlsl_word(base)}` in `v_mbcnt_lo_u32_b32` lowering -> `mbcnt_controls()` failed on exact HLSL output check. 3. Changed `v_mbcnt_hi_u32_b32` lowering to add `32u` instead of passing through `base` -> `mbcnt_controls()` failed on exact HLSL output check. ### Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) - **Debug (`--preset dev`)**: `ctest --preset dev -R '^(compute_store_aot_mbcnt_warp|compute_store_aot_warp|compute_store_aot_guard_read_warp|compute_store_aot_f32_warp)$' --parallel 6 --no-tests=error --output-on-failure` — **4/4 passed** (`compute_store_aot_mbcnt_warp` `4.43s`) - **Release `/O2` (`--preset release`)**: `ctest --preset release -R '^(compute_store_aot_mbcnt_warp|compute_store_aot_warp|compute_store_aot_guard_read_warp|compute_store_aot_f32_warp)$' --parallel 6 --no-tests=error --output-on-failure` — **4/4 passed** (`compute_store_aot_mbcnt_warp` `4.49s`) AI-assisted; commands listed were executed 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!1775
No description provided.