shader: keep s_bfe_u32 wide fields in gfx2hlsl #2106
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2114"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-25T00:43:51Z.
Problem
s_bfe_u32reads 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 givesS0 >> 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_0shader that loads both operands from a buffer:%9 = and i32 %8, 31then%10 = shl i32 1, %9. FXCcs_5_0emitsishl, which also reads only the low 5 bits of the count.Change
tools/startup/gfx2hlsl.py: thes_bfe_u32mask 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 thes_bfe_u32helper intools/pixel_aot.py.tests/startup_gfx2hlsl_sop2_complement_check.pygainss_bfe_u32cases: 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
Result: 1/1 passed.
main)0xffffffff, control0x200000(width 32)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
cs_6_0and FXCcs_5_0without warnings.Not proven
s_bfe_u32through gfx2hlsl, so no draw checks this change.Environment
Windows 11 build 26300, based on
mainat246a875d.llvm-mcis not available locally: thebinaryanddecodemodes did not run here and are left to CI.Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:46:07Z.
Five mutations, four caught — the fifth (
& 31→& 63on the offset) survived, and I cannot reconcile that, so here is the measurement rather than a claimBaseline on this branch:
s_bfe_u32row>= 32→> 32(width 32 falls to the shift path)0x7fffffff>= 32→>= 331u << (width & 127)form({1} & 31)→({1} & 63)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& 31is the ISA's width and& 63is wrong for any control word with bit 5 set. A control word that distinguishes them:and your control set does contain such words — I checked the pair list:
So either those four pairs are only fed to the wide check rather than to
bfe_first_mismatch's defaultBFE_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_PAIRSwithctrl = 0x00000020and 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 forclass16in #2092 and it is much easier to defend than a plausible-lookingmin(width, 32).AI-assisted; the five mutations and the pair-list check were run locally on Windows 11 build 26300.9974.
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 news_bfe_u32expression matches the ISA. Width is S1[22:16] (7 bits). For width >= 32 the mask is all ones, so the result isS0 >> 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.s_bfe_u32intools/pixel_aot.py(width 0 returns 0,off + w >= 32returnssrc >> off).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.>= 32 ? 0xffffffffuto>= 33 ? 0xffffffffuingfx2hlsl.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.
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
& 63survivor is an equivalent mutant, not a gap. In thes_bfe_u32row 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 reachbfe_first_mismatchthroughBFE_PAIRS. With bit 5 set,& 63gives 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):