Skip to content

shader: keep s_bfe_u32 wide fields in gfx2hlsl - #2114

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
Makto94:shader/gfx2hlsl-s-bfe-u32-wide-width
Nov 25, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
Makto94:shader/gfx2hlsl-s-bfe-u32-wide-width

Conversation

@Makto94

@Makto94 Makto94 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

s_bfe_u32 takes the width from S1[22:16]. HLSL reduces a shift count to 5 bits, so the old mask (1u << (width & 127)) - 1 read 0 at width 32 and truncated at widths 33 to 127. The mask is now all ones for widths of 32 or more. The sop2_complement check covers the forms against an ISA reference, with negative controls.
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 merged commit 1277cae into R0Y00:main Nov 25, 2026
5 checks passed
@Makto94

Makto94 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

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 (77a204d, 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 77a204d (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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants