shader: keep s_bfe_u32 wide fields in gfx2hlsl #2106

Merged
Makto94 merged 1 commit from upstream-pr/2114 into main 2026-11-25 02:21:05 +00:00
Makto94 commented 2026-11-25 00:43:51 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-25T00:43:51Z.


Problem

s_bfe_u32 reads the width from S1[22:16]. The startup translator built the mask as (1u << (w & 127)) - 1, but HLSL reduces a shift count to 5 bits. At width 32 the mask was 0 instead of all ones, so the result was 0 where the ISA gives S0 >> offset. At widths 33 to 127 the mask was truncated.

DXC 1.9.2602.17 (Windows SDK 10.0.26100) shows it in the IR of the old formula for a cs_6_0 shader that loads both operands from a buffer: %9 = and i32 %8, 31 then %10 = shl i32 1, %9. FXC cs_5_0 emits ishl, which also reads only the low 5 bits of the count.

Change

tools/startup/gfx2hlsl.py: the s_bfe_u32 mask is all ones when the width is 32 or more, and every shift count stays in 0..31. Width 0 still gives 0. SCC is still the nonzero test of the result. The reference is the s_bfe_u32 helper in tools/pixel_aot.py.

tests/startup_gfx2hlsl_sop2_complement_check.py gains s_bfe_u32 cases: widths 0 to 127, offsets 0, 1, 4, 8, 16, 24 and 31, ignored control bits set and clear, and SGPR, in-place, m0, inline and literal forms.

Verification

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sop2_complement$' -V

Result: 1/1 passed.

Result
Before (main) MISMATCH on source 0xffffffff, control 0x200000 (width 32)
After pass

Negative controls, each must fail the check: the old formula (the width taken mod 32 by the shift), a 6-bit width, a dropped field mask, a dropped SCC write.

Pure-Python list from AGENTS.md: all pass on this branch.

Evidence level

  • Author-tested: the commands above. The old and new expressions compile with DXC cs_6_0 and FXC cs_5_0 without warnings.

Not proven

  • No draw on a device uses s_bfe_u32 through gfx2hlsl, so no draw checks this change.

Environment

Windows 11 build 26300, based on main at 246a875d. llvm-mc is not available locally: the binary and decode modes did not run here and are left to CI.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6594477449 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2114). Originally posted by [Makto94](https://github.com/Makto94) on 2026-11-25T00:43:51Z. --- ## Problem `s_bfe_u32` reads the width from S1[22:16]. The startup translator built the mask as `(1u << (w & 127)) - 1`, but HLSL reduces a shift count to 5 bits. At width 32 the mask was 0 instead of all ones, so the result was 0 where the ISA gives `S0 >> offset`. At widths 33 to 127 the mask was truncated. DXC 1.9.2602.17 (Windows SDK 10.0.26100) shows it in the IR of the old formula for a `cs_6_0` shader that loads both operands from a buffer: `%9 = and i32 %8, 31` then `%10 = shl i32 1, %9`. FXC `cs_5_0` emits `ishl`, which also reads only the low 5 bits of the count. ## Change `tools/startup/gfx2hlsl.py`: the `s_bfe_u32` mask is all ones when the width is 32 or more, and every shift count stays in 0..31. Width 0 still gives 0. SCC is still the nonzero test of the result. The reference is the `s_bfe_u32` helper in `tools/pixel_aot.py`. `tests/startup_gfx2hlsl_sop2_complement_check.py` gains `s_bfe_u32` cases: widths 0 to 127, offsets 0, 1, 4, 8, 16, 24 and 31, ignored control bits set and clear, and SGPR, in-place, `m0`, inline and literal forms. ## Verification ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sop2_complement$' -V ``` Result: 1/1 passed. | | Result | |---|---| | Before (`main`) | MISMATCH on source `0xffffffff`, control `0x200000` (width 32) | | After | pass | Negative controls, each must fail the check: the old formula (the width taken mod 32 by the shift), a 6-bit width, a dropped field mask, a dropped SCC write. Pure-Python list from `AGENTS.md`: all pass on this branch. ## Evidence level - Author-tested: the commands above. The old and new expressions compile with DXC `cs_6_0` and FXC `cs_5_0` without warnings. ## Not proven - No draw on a device uses `s_bfe_u32` through gfx2hlsl, so no draw checks this change. ## Environment Windows 11 build 26300, based on `main` at `246a875d`. `llvm-mc` is not available locally: the `binary` and `decode` modes did not run here and are left to CI.
ChazY11 commented 2026-11-25 01:02:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:46:07Z.


Five mutations, four caught — the fifth (& 31 → & 63 on the offset) survived, and I cannot reconcile that, so here is the measurement rather than a claim

Baseline on this branch:

s_bfe_u32 negative control: no field mask fails: … ok
PASS        exit 0
mutation of the new s_bfe_u32 row result
width threshold >= 32 → > 32 (width 32 falls to the shift path) caught
the all-ones mask written 0x7fffffff caught
threshold >= 32 → >= 33 caught
the row reverted to the old 1u << (width & 127) form caught
offset ({1} & 31) → ({1} & 63) survived

The width handling is solidly pinned — including the two off-by-one thresholds, which is where this kind of fix usually slips.

Why the offset survivor puzzles me

The offset field is S1[4:0], so & 31 is the ISA's width and & 63 is wrong for any control word with bit 5 set. A control word that distinguishes them:

ctrl = 0x00000020    offset & 31 = 0    offset & 63 = 32

and your control set does contain such words — I checked the pair list:

pairs with (ctrl & 0x1f) != (ctrl & 0x3f):  src=0xffffffff ctrl=0xff80ffe1
                                            src=0xffffffff ctrl=0xff80ffe4
                                            src=0xffffffff ctrl=0xff80ffe8
                                            src=0xffffffff ctrl=0xff80fff0

So either those four pairs are only fed to the wide check rather than to bfe_first_mismatch's default BFE_PAIRS, or the evaluator that runs the emitted text normalises a shift of 33 the way the reference expects. I could not tell which inside the time I had, and I would rather say that than repeat a mistake I made earlier today on another PR (where I called a survivor a gap and the mutant was equivalent).

If it is the first, the fix is a pair in BFE_PAIRS with ctrl = 0x00000020 and a source whose bit 0 differs from bit 32 — then the mutant dies. If it is the second, ignore me.

One thing worth keeping

The docstring's ISA citation (offset = S1[4:0], width = S1[11:4]) is the part that makes the row reviewable without the instruction guide open, and the >= 32 ? 0xffffffff : ... shape says why the shift had to change rather than just changing it. That is the same shape I used for class16 in #2092 and it is much easier to defend than a plausible-looking min(width, 32).

AI-assisted; the five mutations and the pair-list check were run locally on Windows 11 build 26300.9974.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148855122 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2114#issuecomment-7148855122). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:46:07Z. --- ## Five mutations, four caught — the fifth (`& 31` → `& 63` on the offset) survived, and I cannot reconcile that, so here is the measurement rather than a claim Baseline on this branch: ```text s_bfe_u32 negative control: no field mask fails: … ok PASS exit 0 ``` | mutation of the new `s_bfe_u32` row | result | |---|---| | width threshold `>= 32` → `> 32` (width 32 falls to the shift path) | **caught** | | the all-ones mask written `0x7fffffff` | **caught** | | threshold `>= 32` → `>= 33` | **caught** | | the row reverted to the old `1u << (width & 127)` form | **caught** | | offset `({1} & 31)` → `({1} & 63)` | survived | The width handling is solidly pinned — including the two off-by-one thresholds, which is where this kind of fix usually slips. ## Why the offset survivor puzzles me The offset field is `S1[4:0]`, so `& 31` is the ISA's width and `& 63` is wrong for any control word with bit 5 set. A control word that distinguishes them: ```text ctrl = 0x00000020 offset & 31 = 0 offset & 63 = 32 ``` and your control set **does** contain such words — I checked the pair list: ```text pairs with (ctrl & 0x1f) != (ctrl & 0x3f): src=0xffffffff ctrl=0xff80ffe1 src=0xffffffff ctrl=0xff80ffe4 src=0xffffffff ctrl=0xff80ffe8 src=0xffffffff ctrl=0xff80fff0 ``` So either those four pairs are only fed to the *wide* check rather than to `bfe_first_mismatch`'s default `BFE_PAIRS`, or the evaluator that runs the emitted text normalises a shift of 33 the way the reference expects. I could not tell which inside the time I had, and I would rather say that than repeat a mistake I made earlier today on another PR (where I called a survivor a gap and the mutant was equivalent). If it is the first, the fix is a pair in `BFE_PAIRS` with `ctrl = 0x00000020` and a source whose bit 0 differs from bit 32 — then the mutant dies. If it is the second, ignore me. ## One thing worth keeping The docstring's ISA citation (`offset = S1[4:0], width = S1[11:4]`) is the part that makes the row reviewable without the instruction guide open, and the `>= 32 ? 0xffffffff : ...` shape says *why* the shift had to change rather than just changing it. That is the same shape I used for `class16` in #2092 and it is much easier to defend than a plausible-looking `min(width, 32)`. AI-assisted; the five mutations and the pair-list check were run locally on Windows 11 build 26300.9974.
tedin7 commented 2026-11-25 02:01:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:55:59Z.

Review: COMMENTED


Review of head 9d013a48d6d62817f08bfec2638ac09ab533268f.

Result: no P1, no P2, no nits.

What I checked:

  • tools/startup/gfx2hlsl.py:1963: the new s_bfe_u32 expression matches the ISA. Width is S1[22:16] (7 bits). For width >= 32 the mask is all ones, so the result is S0 >> offset. Width 0 gives 0. Every shift count is masked to 0..31, so HLSL's 5-bit shift behavior cannot bite. SCC is still the nonzero test.
  • It agrees with the reference helper s_bfe_u32 in tools/pixel_aot.py (width 0 returns 0, off + w >= 32 returns src >> off).
  • Title is neutral, no clean-room concerns, docs entry matches the change. CI checks are green: Linux Python checks, Windows build and test, gfx1013 fixtures.

Commands run locally on the PR head:

  • python3 tests/startup_gfx2hlsl_sop2_complement_check.py -> PASS (includes the four negative controls).
  • python3 tests/docs_links_check.py -> 18 files, 395 links OK.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean.
  • Mutation: changed >= 32 ? 0xffffffffu to >= 33 ? 0xffffffffu in gfx2hlsl.py. The check FAILED on both "1764 (source, control) pairs" and "wide fields (width 32 and above)". The mutation was reverted.

Not run: a Windows WARP readback. The change is one scalar expression, covered by the CPU evaluation and the Windows CI job. A GPU run was not needed for this review.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6050454431 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2114#pullrequestreview-6050454431). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:55:59Z. **Review:** COMMENTED --- Review of head `9d013a48d6d62817f08bfec2638ac09ab533268f`. **Result: no P1, no P2, no nits.** What I checked: - `tools/startup/gfx2hlsl.py:1963`: the new `s_bfe_u32` expression matches the ISA. Width is S1[22:16] (7 bits). For width >= 32 the mask is all ones, so the result is `S0 >> offset`. Width 0 gives 0. Every shift count is masked to 0..31, so HLSL's 5-bit shift behavior cannot bite. SCC is still the nonzero test. - It agrees with the reference helper `s_bfe_u32` in `tools/pixel_aot.py` (width 0 returns 0, `off + w >= 32` returns `src >> off`). - Title is neutral, no clean-room concerns, docs entry matches the change. CI checks are green: Linux Python checks, Windows build and test, gfx1013 fixtures. Commands run locally on the PR head: - `python3 tests/startup_gfx2hlsl_sop2_complement_check.py` -> PASS (includes the four negative controls). - `python3 tests/docs_links_check.py` -> 18 files, 395 links OK. - `python3 tests/bidi_controls_check.py` -> no bidirectional controls. - `git diff --check origin/main...HEAD` -> clean. - Mutation: changed `>= 32 ? 0xffffffffu` to `>= 33 ? 0xffffffffu` in `gfx2hlsl.py`. The check FAILED on both "1764 (source, control) pairs" and "wide fields (width 32 and above)". The mutation was reverted. Not run: a Windows WARP readback. The change is one scalar expression, covered by the CPU evaluation and the Windows CI job. A GPU run was not needed for this review. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 03:00:57 +00:00
Makto94 commented 2026-11-25 10:05:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-25T09:43:15Z.


Thanks for running the mutations and for checking the pair list.

It is your second case: under the test evaluator the & 63 survivor is an equivalent mutant, not a gap. In the s_bfe_u32 row on main (77a204d6, tools/startup/gfx2hlsl.py:3479) the offset appears only as a shift count, {0} >> ({1} & 31). The test evaluator reduces shift counts to 5 bits (tests/startup_gfx2hlsl_bit_alu_check.py:254, count = b & 31). Your four controls, and every other control with bit 5 set, do reach bfe_first_mismatch through BFE_PAIRS. With bit 5 set, & 63 gives 32 or more, and the evaluator reduces it modulo 32 to the same count as & 31. For every x, (x & 63) & 31 == x & 31, so no input can tell the two apart.

Reproduction from the repository root at 77a204d6 (python -B mut.py):

import sys; sys.path.insert(0, 'tests')
import startup_gfx2hlsl_sop2_complement_check as T
row = T.G.SCA[T.BFE]
T.G.SCA[T.BFE] = (row[0].replace('({1} & 31)', '({1} & 63)', 1), row[1])
print('mutant text:', T.G.SCA[T.BFE][0])
print('first mismatch:', T.bfe_first_mismatch())
mutant text: ({0} >> ({1} & 63)) & ((({1} >> 16) & 127) >= 32 ? 0xffffffffu : ((1u << (({1} >> 16) & 31)) - 1))
first mismatch: None
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7158710346 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2114#issuecomment-7158710346). Originally posted by [Makto94](https://github.com/Makto94) on 2026-11-25T09:43:15Z. --- Thanks for running the mutations and for checking the pair list. It is your second case: under the test evaluator the `& 63` survivor is an equivalent mutant, not a gap. In the `s_bfe_u32` row on main (77a204d6, `tools/startup/gfx2hlsl.py:3479`) the offset appears only as a shift count, `{0} >> ({1} & 31)`. The test evaluator reduces shift counts to 5 bits (`tests/startup_gfx2hlsl_bit_alu_check.py:254`, `count = b & 31`). Your four controls, and every other control with bit 5 set, do reach `bfe_first_mismatch` through `BFE_PAIRS`. With bit 5 set, `& 63` gives 32 or more, and the evaluator reduces it modulo 32 to the same count as `& 31`. For every x, `(x & 63) & 31 == x & 31`, so no input can tell the two apart. Reproduction from the repository root at 77a204d6 (`python -B mut.py`): ```python import sys; sys.path.insert(0, 'tests') import startup_gfx2hlsl_sop2_complement_check as T row = T.G.SCA[T.BFE] T.G.SCA[T.BFE] = (row[0].replace('({1} & 31)', '({1} & 63)', 1), row[1]) print('mutant text:', T.G.SCA[T.BFE][0]) print('first mismatch:', T.bfe_first_mismatch()) ``` ```text mutant text: ({0} >> ({1} & 63)) & ((({1} >> 16) & 127) >= 32 ? 0xffffffffu : ((1u << (({1} >> 16) & 31)) - 1)) first mismatch: None ```
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!2106
No description provided.