shader: lower compute s_bfe_u32 and s_bfe_i32 #1948

Merged
henriazz10 merged 1 commit from upstream-pr/1955 into main 2026-11-25 00:36:09 +00:00
henriazz10 commented 2026-11-24 22:07:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by henriazz10 on 2026-11-24T22:07:06Z.


Change

Refs #721. This is the s_bfe_u32 / s_bfe_i32 slice claimed in https://github.com/R0Y00/Xbox2Windows/issues/721#issuecomment-7143412663. It touches only tools/compute_store_aot.py, tests/compute_store_check.py, CMakeLists.txt and docs/experiments.md.

  • Lowers s_bfe_u32 sD, S0, S1 and s_bfe_i32 sD, S0, S1 (SOP2) on known 32-bit scalar words (alu_word, hlsl_word, ALU_EXPRESSION_LIMIT), with offset = S1[4:0] and width = S1[22:16] per the RDNA2 ISA (0 when width == 0, (S0 << (32 - offset - width)) >> (32 - width) when offset + width <= 32, and S0 >> offset when offset + width > 32; logical for s_bfe_u32, arithmetic for s_bfe_i32). SCC is unread.
  • Emits explicit HLSL helpers (s_bfe_u32 / s_bfe_i32) only when referenced in the lowered body so width == 0 returns 0u instead of shifting by 32 and all shift counts stay in 0..31.
  • Refused, each with its own control:
    • unknown or pending scalar source (s30, s12, s23 before s_waitcnt lgkmcnt(0));
    • destination past s105 (s106) or pending load destination (s23);
    • literal wider than 32 bits (0x1ffffffff);
    • 64-bit forms (s_bfe_u64, s_bfe_i64);
    • vector/float/special-register sources (v0, 1.0, exec_lo) and unsupported suffixes/modifiers (_e64, clamp, -s4, abs(s4));
    • SCC consumers (s_cbranch_scc0 0), use as a store address or lane-guard compare operand, and expressions exceeding ALU_EXPRESSION_LIMIT.

Verification

Commit 314bad99, rebased onto 32595645. The draws, controls() and the LLVM 15 round trip were re-run on 314bad99. Windows 11, DXC 10.0.26100.0, Python 3.12 (pe-env), debug layer enabled. offscreen_check was built from main; this PR does not change it.

python -B tests/compute_store_check.py draw-bfe <dxc> <offscreen_check> warp
python -B tests/compute_store_check.py draw-bfe <dxc> <offscreen_check> hardware
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
git diff --check HEAD~1
Backend Adapter / driver Result
WARP Microsoft Basic Render Driver 10.0.26100.9818 exit 0, groups=1 compared_words=320 mismatches=0, debug_errors=0 debug_warnings=0
hardware NVIDIA GeForce RTX 5080, 32.0.16.1692 exit 0, groups=1 compared_words=320 mismatches=0, debug_errors=0 debug_warnings=0

Doc/repo checks:

  • tests/docs_links_check.py: exit 0 (Checked 18 Markdown files and 376 links.).
  • tests/bidi_controls_check.py: exit 0 (Checked 1330 tracked source, script and build/CI files: no bidirectional controls.).
  • git diff --check HEAD~1: exit 0.

LLVM 15 round trip in Docker:

docker run --rm -v "C:\Proyectos\Xbox2Windows\wt-bfe:/src" x2w-llvm15 bash -c 'cp -r /src /w && cd /w && python3 -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 /usr/lib/x86_64-linux-gnu/libLLVM-15.so.1; echo binary_exit=$?'

binary_exit=0. bfe_binary() decodes the assembled BFE fixture back to the same s_bfe_u32 / s_bfe_i32 lines and requires the same translation.

Negative control. With tools/compute_store_aot.py from origin/main, bfe_controls() fails with ValueError: Unsupported compute instruction: s_bfe_u32 s16, s4, 0x80008.

Evidence

The BFE fixture is source-built in LLVM 15 spelling for one 64-thread group (wave_size=32, 4 words per thread, 256 words written and words 256..319 left at SENTINEL). It covers mid-word extraction with MSB 1 (0x8000f5a3, offset 8, width 8) under both s_bfe_u32 and s_bfe_i32, width == 0 with ignored bits set in S1[31:23] and S1[15:5] (0xff80ffe8, 0xff80ffe0), offset + width == 32 (0x100010), offset + width > 32 (0x140014), negative inline constant -1 with offset = 0, width = 32 (0x200000), and chained s_bfe output. All 320 words are compared against an independent Python model (bfe_model).

  • Translation controls (bfe_controls). Checks exact s_bfe_u32 / s_bfe_i32 helper emission (and per-helper omission when unused), the exact Store4 expression, and all rejection cases listed above.
  • Reference controls. Each fails at its first differing word:
    • s_bfe_i32 treated as unsigned: word 1;
    • width == 0 not returning 0: word 3;
    • offset + width > 32 not sign-extending: word 2;
    • sentinel past group: word 256.
  • Recompiled-HLSL controls. Each fails on the device:
    • width == 0u returning src instead of 0u;
    • s_bfe_i32 using unsigned right shift ((src >> offset)).

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. Gemini 4 Hydrogen (Antigravity CLI) wrote the change and ran the checks; Claude Code reviewed it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6592363155 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1955). Originally posted by [henriazz10](https://github.com/henriazz10) on 2026-11-24T22:07:06Z. --- ## Change Refs #721. This is the `s_bfe_u32` / `s_bfe_i32` slice claimed in https://github.com/R0Y00/Xbox2Windows/issues/721#issuecomment-7143412663. It touches only `tools/compute_store_aot.py`, `tests/compute_store_check.py`, `CMakeLists.txt` and `docs/experiments.md`. - Lowers `s_bfe_u32 sD, S0, S1` and `s_bfe_i32 sD, S0, S1` (SOP2) on known 32-bit scalar words (`alu_word`, `hlsl_word`, `ALU_EXPRESSION_LIMIT`), with `offset = S1[4:0]` and `width = S1[22:16]` per the RDNA2 ISA (`0` when `width == 0`, `(S0 << (32 - offset - width)) >> (32 - width)` when `offset + width <= 32`, and `S0 >> offset` when `offset + width > 32`; logical for `s_bfe_u32`, arithmetic for `s_bfe_i32`). `SCC` is unread. - Emits explicit HLSL helpers (`s_bfe_u32` / `s_bfe_i32`) only when referenced in the lowered body so `width == 0` returns `0u` instead of shifting by `32` and all shift counts stay in `0..31`. - Refused, each with its own control: - unknown or pending scalar source (`s30`, `s12`, `s23` before `s_waitcnt lgkmcnt(0)`); - destination past `s105` (`s106`) or pending load destination (`s23`); - literal wider than 32 bits (`0x1ffffffff`); - 64-bit forms (`s_bfe_u64`, `s_bfe_i64`); - vector/float/special-register sources (`v0`, `1.0`, `exec_lo`) and unsupported suffixes/modifiers (`_e64`, `clamp`, `-s4`, `abs(s4)`); - `SCC` consumers (`s_cbranch_scc0 0`), use as a store address or lane-guard compare operand, and expressions exceeding `ALU_EXPRESSION_LIMIT`. ## Verification Commit `314bad99`, rebased onto `32595645`. The draws, `controls()` and the LLVM 15 round trip were re-run on `314bad99`. Windows 11, DXC 10.0.26100.0, Python 3.12 (`pe-env`), debug layer enabled. `offscreen_check` was built from `main`; this PR does not change it. ```powershell python -B tests/compute_store_check.py draw-bfe <dxc> <offscreen_check> warp python -B tests/compute_store_check.py draw-bfe <dxc> <offscreen_check> hardware python -B tests/docs_links_check.py python -B tests/bidi_controls_check.py git diff --check HEAD~1 ``` | Backend | Adapter / driver | Result | |---|---|---| | WARP | Microsoft Basic Render Driver 10.0.26100.9818 | exit 0, `groups=1 compared_words=320 mismatches=0`, `debug_errors=0 debug_warnings=0` | | hardware | NVIDIA GeForce RTX 5080, 32.0.16.1692 | exit 0, `groups=1 compared_words=320 mismatches=0`, `debug_errors=0 debug_warnings=0` | Doc/repo checks: - `tests/docs_links_check.py`: exit 0 (`Checked 18 Markdown files and 376 links.`). - `tests/bidi_controls_check.py`: exit 0 (`Checked 1330 tracked source, script and build/CI files: no bidirectional controls.`). - `git diff --check HEAD~1`: exit 0. LLVM 15 round trip in Docker: ```sh docker run --rm -v "C:\Proyectos\Xbox2Windows\wt-bfe:/src" x2w-llvm15 bash -c 'cp -r /src /w && cd /w && python3 -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 /usr/lib/x86_64-linux-gnu/libLLVM-15.so.1; echo binary_exit=$?' ``` `binary_exit=0`. `bfe_binary()` decodes the assembled `BFE` fixture back to the same `s_bfe_u32` / `s_bfe_i32` lines and requires the same translation. **Negative control.** With `tools/compute_store_aot.py` from `origin/main`, `bfe_controls()` fails with `ValueError: Unsupported compute instruction: s_bfe_u32 s16, s4, 0x80008`. ## Evidence The `BFE` fixture is source-built in LLVM 15 spelling for one 64-thread group (`wave_size=32`, 4 words per thread, 256 words written and words `256..319` left at `SENTINEL`). It covers mid-word extraction with MSB 1 (`0x8000f5a3`, offset 8, width 8) under both `s_bfe_u32` and `s_bfe_i32`, `width == 0` with ignored bits set in `S1[31:23]` and `S1[15:5]` (`0xff80ffe8`, `0xff80ffe0`), `offset + width == 32` (`0x100010`), `offset + width > 32` (`0x140014`), negative inline constant `-1` with `offset = 0, width = 32` (`0x200000`), and chained `s_bfe` output. All 320 words are compared against an independent Python model (`bfe_model`). - **Translation controls (`bfe_controls`).** Checks exact `s_bfe_u32` / `s_bfe_i32` helper emission (and per-helper omission when unused), the exact `Store4` expression, and all rejection cases listed above. - **Reference controls.** Each fails at its first differing word: - `s_bfe_i32` treated as unsigned: word 1; - `width == 0` not returning `0`: word 3; - `offset + width > 32` not sign-extending: word 2; - sentinel past group: word 256. - **Recompiled-HLSL controls.** Each fails on the device: - `width == 0u` returning `src` instead of `0u`; - `s_bfe_i32` using unsigned right shift (`(src >> offset)`). ## 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. Gemini 4 Hydrogen (Antigravity CLI) wrote the change and ran the checks; Claude Code reviewed it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
tedin7 commented 2026-11-25 00:04:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T23:57:50Z.

Review: COMMENTED


Review of head 742306417b7d149c9b384a5903967c4b0151a0a7.

Diff read in full (CMakeLists.txt, docs/experiments.md, tests/compute_store_check.py, tools/compute_store_aot.py). CMakeLists.txt only registers compute_store_aot_bfe_<adapter>. The semantics follow the RDNA 2 ISA pseudocode (width 0 returns 0, offset+width > 32 shifts only, shift counts stay in 0..31). Spec-derived and independent of any other source; no clean-room concern.

P1: 0. P2: 0.

Commands executed (Linux):

  • git merge-tree --write-tree --name-only origin/main HEAD: clean, no conflicts
  • python3 tests/docs_links_check.py: PASS (18 files, 391 links)
  • python3 tests/bidi_controls_check.py: PASS (1407 files)
  • git diff --check origin/main...HEAD: PASS
  • python3 -B tests/compute_store_check.py <mode>: controls() incl. new bfe_controls() PASS
  • gh pr checks 1955: Linux checks, gfx1013 fixtures, Windows build and test all pass

Windows Server 2025 (x2w_pipe.py win2025 1955): Debug (--preset dev) 72/72 pass; Release /O2 (--preset release) 72/72 pass; includes compute_store_aot_bfe_warp.

Mutations on the code under test (reverted with git checkout -- .):

  • "s_bfe_i32" lowering mapped to s_bfe_u32(...): KILLED by bfe_controls() (helper and exact-HLSL assertion).
  • width == 0u returning src, and offset + width <= 32u changed to <: not exercised by the Linux controls (they check emission and signature only); these are covered by the on-device draw-bfe comparison, which I did not re-run with the mutation.
  • Width mask 127u changed to 63u: survives. No fixture sets S1[22]. Minor, not blocking.

Note (not blocking): the Linux controls assert only the helper signature lines, so helper-body regressions are caught on the device run only.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049393861 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1955#pullrequestreview-6049393861). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T23:57:50Z. **Review:** COMMENTED --- Review of head `742306417b7d149c9b384a5903967c4b0151a0a7`. Diff read in full (CMakeLists.txt, docs/experiments.md, tests/compute_store_check.py, tools/compute_store_aot.py). CMakeLists.txt only registers `compute_store_aot_bfe_<adapter>`. The semantics follow the RDNA 2 ISA pseudocode (width 0 returns 0, offset+width > 32 shifts only, shift counts stay in 0..31). Spec-derived and independent of any other source; no clean-room concern. P1: 0. P2: 0. Commands executed (Linux): - `git merge-tree --write-tree --name-only origin/main HEAD`: clean, no conflicts - `python3 tests/docs_links_check.py`: PASS (18 files, 391 links) - `python3 tests/bidi_controls_check.py`: PASS (1407 files) - `git diff --check origin/main...HEAD`: PASS - `python3 -B tests/compute_store_check.py <mode>`: `controls()` incl. new `bfe_controls()` PASS - `gh pr checks 1955`: Linux checks, gfx1013 fixtures, Windows build and test all pass Windows Server 2025 (`x2w_pipe.py win2025 1955`): Debug (`--preset dev`) 72/72 pass; Release /O2 (`--preset release`) 72/72 pass; includes `compute_store_aot_bfe_warp`. Mutations on the code under test (reverted with `git checkout -- .`): - `"s_bfe_i32"` lowering mapped to `s_bfe_u32(...)`: KILLED by `bfe_controls()` (helper and exact-HLSL assertion). - `width == 0u` returning `src`, and `offset + width <= 32u` changed to `<`: not exercised by the Linux controls (they check emission and signature only); these are covered by the on-device `draw-bfe` comparison, which I did not re-run with the mutation. - Width mask `127u` changed to `63u`: survives. No fixture sets `S1[22]`. Minor, not blocking. Note (not blocking): the Linux controls assert only the helper signature lines, so helper-body regressions are caught on the device run only. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 01:00:54 +00:00
tedin7 commented 2026-11-25 01:00:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:35:17Z.

Review: COMMENTED


Review of head 314bad99f5 (s_bfe_u32 / s_bfe_i32 lowering).

Result: 0 P1, 0 P2. Clean-room: the change is original code, a spec-derived model and a source-built fixture. CMakeLists.txt only registers compute_store_aot_bfe_${adapter}.

Semantics read: width 0 returns 0, offset + width <= 32 uses shift-left then shift-right (logical for u32, arithmetic for i32), and the overflow case uses src >> offset. Every shift count stays in 0..31. Helpers are emitted only when referenced. The Python model is independent of the HLSL helper.

Commands executed locally:

  • git merge-tree --write-tree --name-only origin/main HEAD: no conflicts. PR is MERGEABLE.
  • gh pr checks 1955: Linux, Windows and gfx1013 fixtures pass.
  • python3 -B tests/compute_store_check.py <mode>: controls() including bfe_controls() pass.
  • python3 tests/docs_links_check.py: PASS (18 files, 394 links).
  • python3 tests/bidi_controls_check.py: PASS.
  • git diff --check origin/main...HEAD: clean.
  • Win2025 x2w_pipe.py win2025 1955 --wait 900: Debug (--preset dev) passed, Release /O2 (--preset release) passed. compute_store_aot_bfe_warp ran and passed.

Mutations (all killed by bfe_controls()):

  1. The s_bfe_i32 table entry changed to call s_bfe_u32: exact-HLSL assertion fails.
  2. The s_bfe_i32 helper emission suppressed, so the s_bfe_i32 helper was missing: the per-helper emission assertion fails.
    The device-side mutations (width 0 returns src, unsigned shift in i32) are part of bfe_draw and passed on Win2025.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049719035 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1955#pullrequestreview-6049719035). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:35:17Z. **Review:** COMMENTED --- Review of head 314bad99f5943c2b568e860cb46ac4b9665fcf86 (s_bfe_u32 / s_bfe_i32 lowering). Result: 0 P1, 0 P2. Clean-room: the change is original code, a spec-derived model and a source-built fixture. CMakeLists.txt only registers `compute_store_aot_bfe_${adapter}`. Semantics read: width 0 returns 0, `offset + width <= 32` uses shift-left then shift-right (logical for u32, arithmetic for i32), and the overflow case uses `src >> offset`. Every shift count stays in 0..31. Helpers are emitted only when referenced. The Python model is independent of the HLSL helper. Commands executed locally: - `git merge-tree --write-tree --name-only origin/main HEAD`: no conflicts. PR is MERGEABLE. - `gh pr checks 1955`: Linux, Windows and gfx1013 fixtures pass. - `python3 -B tests/compute_store_check.py <mode>`: controls() including bfe_controls() pass. - `python3 tests/docs_links_check.py`: PASS (18 files, 394 links). - `python3 tests/bidi_controls_check.py`: PASS. - `git diff --check origin/main...HEAD`: clean. - Win2025 `x2w_pipe.py win2025 1955 --wait 900`: Debug (`--preset dev`) passed, Release /O2 (`--preset release`) passed. `compute_store_aot_bfe_warp` ran and passed. Mutations (all killed by `bfe_controls()`): 1. The `s_bfe_i32` table entry changed to call `s_bfe_u32`: exact-HLSL assertion fails. 2. The `s_bfe_i32` helper emission suppressed, so the `s_bfe_i32` helper was missing: the per-helper emission assertion fails. The device-side mutations (width 0 returns src, unsigned shift in i32) are part of `bfe_draw` and passed on Win2025. 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!1948
No description provided.