Repository navigation
shader: keep s_bfe_u32 wide fields in gfx2hlsl - #2114
Conversation
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.
Five mutations, four caught — the fifth (
|
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
left a comment
There was a problem hiding this comment.
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.- It agrees with the reference helper
s_bfe_u32intools/pixel_aot.py(width 0 returns 0,off + w >= 32returnssrc >> 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 ? 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.
|
Thanks for running the mutations and for checking the pair list. It is your second case: under the test evaluator the Reproduction from the repository root at 77a204d ( 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()) |
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.