shader: recognise whole wave32 reductions in gfx2hlsl pixel stages (Part of #2125) #2827
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2835"
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 roansystemlabs on 2026-11-26T12:43:34Z.
Change
Part of #2125, item D, first slice (wave32), as claimed in https://github.com/R0Y00/Xbox2Windows/issues/2125#issuecomment-7153462376. The chain is the one in denn0x's census (https://github.com/R0Y00/Xbox2Windows/issues/2125#issuecomment-7153771111) and the fill and the three exact shapes are the ones in Miffedrandir's census (https://github.com/R0Y00/Xbox2Windows/issues/2125#issuecomment-7189349802): 574 wave32 chains, all one of three shapes.
tools/startup/gfx2hlsl.pyrecognises a whole wave32 reduction chain in a straight-line pixel stage instead of its instructions (the mirror steps read lanes outside the quad, which the one-lane model cannot prove). Each chain is:s_or_saveexec_b32 sS, -1;v_cndmask_b32_e64 vT, <identity>, vX, sS, identity0forv_or_b32,-1forv_min_u32,0forv_max_u32;OP_dpp vT, vT, vTwithquad_perm:[1,0,3,2],quad_perm:[2,3,0,1],row_half_mirror,row_mirror(row_mask:0xf bank_mask:0xf,fi:1or not,bound_ctrlor not: with every lane on, they decide nothing);v_permlanex16_b32 vP, vT, 0, 0, thenOP vT, vP, vT(either source order);s_mov_b32 exec_lo, sS, thenv_readlane_b32 sD, vT, 31.The OR and MAX shapes read lane 31 right after the restore. The MIN shape reads it later: restore, one
v_cndmask_b32_sdwa, the next chain'ss_or_saveexec_b32 sS2, -1, then the MIN's readlane, which the MAX chain carries before its fill. Readlane ignores EXEC, so this is the MIN result as long as nothing in between rewrites the MIN'svTand the readlane does not overwritesS2; both are refused, as is a later readlane that no MAX chain carries. Thev_cndmask_b32_sdwakeeps its own handler, under the restored EXEC.Each chain lowers to one
WaveActiveBitOr,WaveActiveMinorWaveActiveMaxofvX, written tovTat the restore and copied tosDat the readlane, so withvTshared by the MIN and the MAX the MIN readlane still reads the MIN.Contract: the console packs pixels into waves in its own way, so a correct program cannot depend on which pixels share a wave; the lowering reduces over the active lanes of the desktop wave. Miffedrandir's reading of the programs (the results feed a scalar loop over the OR's bits, and each lane gates its work on its own bit) fits that. The chain is accepted only while EXEC is still the entry mask, not widened by
s_wqm, so helper lanes hold the identity on the console and are left out ofWaveActive*on the desktop (measured below).Every other shape that starts like a chain is refused with its reason: wave64, another fill (another constant, another saved mask, a copy, none), a wrong identity, missing or reordered steps, another DPP control, partial masks, other or mixed operations, other permlane selects or
op_sel, a readlane lane other than 31 or before the restore, EXEC not restored fromsS, another instruction in place of thev_cndmask_b32_sdwa,vTrewritten before the later readlane, a missing later readlane, one that overwrites the MAX's saved EXEC, a later readlane in an OR or MAX chain, a chain other than MAX carrying it, ans_or_saveexec_b32after it that starts no chain, a branch target inside, branches, a vertex stage,s_wqm, andvPread after the restore before a VALU rewrites it. Eight refusals are conservative (the lane model shows they would still reduce): reordered steps, other permlane selects,op_sel, lane 30, the readlane before the restore, another instruction in place of thev_cndmask_b32_sdwa, a later readlane in a MAX chain, an OR chain carrying it. A MIN chain read right after its restore is also accepted (the OR shape with another operation). Wave64 (40 chains in Miffedrandir's capture) is the next slice.tests/offscreen_check.cppgains--startup-pixel-dump, the--startup-pixel-wordsdraw with the readback written to a file and gated on wave operations: the reference depends on the desktop packing, which is only known after the draw. The tests are registered intests/cmake/startup_gfx2hlsl_wave_reduce.cmake; the controls and thebinarymode are listed for the Linux jobs intests/checks/; the note isdocs/experiments/startup-gfx2hlsl-wave-reduce.md.Verification
Head
e1a52c13(d92978e3plus a test-only commit, see below) on main02075500. Windows 11 Home 26200, i7-10700, MSVC 19.44.35230, Ninja, DXC 1.9.2602.17 (Windows SDK 10.0.26100.0), Python 3.12.10, debug layer on, otherwise idle machine.On
e1a52c13:The
binaryline was run the way thepixel-binaryjob runs it:merge_checksfromtools/run_checks_parallel.pyovertests/checks/pixel-binary, thenrun_checks, withllvm-mc-15onPATH(llvm-mc 15.0.7, the conda-forgellvm-toolsWindows build; the job uses the Ubuntullvm-15package): 3 of 3 checks passed.On
d92978e3(the translator is unchanged since):ctest --preset dev --no-tests=error -R '^startup_', 1130 tests: 1121 passed, 9 skipped, 0 failed.e1a52c13fixes the failingCheck machine-code inputs with LLVM 15step:-show-encodingprints a branch to a label with its offset still a fixup ([A,A,0x88,0xbf]), so the three programs with a branch were compared against unresolved words. They are now assembled with-filetype=objand compared with the.textsection; the other programs keep the-show-encodingpath. The earlier local run had used clang 15 with llvm-objdump 15 in place ofllvm-mc-15, which resolves the branch and hid this. The same commit adds a fill constant of 5 for each operation to the refusals.main has since moved to
22aa7bf9; the branch merges cleanly, and on the merged tree the 109 controls and the 53binaryprograms pass.Each draw covers 482 pixels of a 32 x 32 target with a slanted triangle (30 partly covered quads). Every covered pixel's results are compared with the operation over the inputs of the covered pixels of its desktop wave (grouped by
WaveReadLaneFirstof the pixel id); the interleaved draw also checks thev_cndmask_b32_sdwaresult per pixel. Planted errors in the translated text must fail, and do on both adapters (14 per adapter): another operation, signed for unsigned, the lane's own value and the first lane's value for each operation (11, OR has no signed form); for the interleaved pair, the MIN readlane lowered after the MAX, MIN and MAX swapped, and thev_cndmask_b32_sdwadropped.startup_gfx2hlsl_wave_reduce_controls: 109 checks. Fail-first one1a52c13: for each fault,tools/andtests/were copied to a scratch directory, one line of the copiedtools/startup/gfx2hlsl.pywas replaced, and the copiedtests/startup_gfx2hlsl_wave_reduce_check.pywas run against it. 16 planted translator faults (hook removed, empty fill table, fill with any constant, fill that also accepts 5, fill under any mask, identity check removed,finot stripped, later readlane for any operation,vTrewrite check removed, saved-EXEC overwrite check removed, carrier check removed, uncarried readlane accepted, thev_cndmask_b32_sdwaswallowed, thevPscan started after the later readlane, branch guard removed, MAX lowered as MIN) each turn it red.Evidence and follow-up
The fixtures are original, written for this test in LLVM 15 spelling from the shapes in the two censuses (no captured programs) and assembled from source (the
binarymode checks every word); MIT, like the repository. The semantics come from the RDNA 2 ISA (DPP16 controls andFI,V_PERMLANEX16_B32,V_READLANE_B32, SDWA selects) and are checked by a 32-lane model in the test, written from those descriptions and not from the lowering.Not run:
e1a52c13(they run on push): locally the controls ran withtools/run_checks_parallel.py --from-dir tests/checks/python-linux, and thebinarymode as described above with the Windows build of llvm-mc 15.0.7.Checklist
docs/experiments/startup-gfx2hlsl-wave-reduce.md,docs/startup-runtime.md).AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T12:50:08Z.
Review: COMMENTED
Review of head
d92978e3(wave32 reductions in gfx2hlsl pixel stages). Verdict: not mergeable yet, one test-script bug, the lowering itself looks sound.Blocking: the new check fails in CI ("Check machine-code inputs with LLVM 15").
Three programs report MISMATCH in
check_binary: "branch target inside the chain", "branch target on the later readlane of the v_min_u32 chain", "a program with branches". Only the first word differs, e.g. built0a0a88bfvs recorded0f0088bf.Cause:
assemble()(imported fromtests/startup_flat_param_check.py:188) reads the bytes from the-show-encodingtext. For a branch to a label, llvm-mc prints the offset bytes as the placeholder[A,A,0x88,0xbf](a fixup), andint('A', 16)turns that into0x0a0a. The label placement incheck_binaryis right (the label sits before thev_max_u32_e32, offset 64); the branch word is simply never resolved. It reproduces locally with llvm-mc 19 as well, so it is not an LLVM 15 quirk.Fix: for the programs that contain a label, assemble to an object (
-filetype=obj) and take.text(llvm-objcopy-O binary --only-section=.text), which resolves the branch offset; or patch the 16-bit offset in the first word from the label distance. Programs without a branch can keep the current path.What I checked (all passed on this head)
python3 tests/startup_gfx2hlsl_wave_reduce_check.py(controls): PASS.python3 tests/docs_links_check.py,python3 tests/bidi_controls_check.py,git diff --check origin/main...HEAD: clean.v_min_u32mapped toWaveActiveMaxinWAVE_REDUCE: the controls fail, as they should.wave_reduce_cndmask_fill: the controls fail, as they should.'5') inwave_reduce_cndmask_fill: the controls still pass. The refusal list has no case with a fill constant other than 0 / -1; a "wrong identity" fixture for each operation would pin that line.startup_gfx2hlsl_wave_reduce_warp/hardware) reads back a real D3D12 draw and compares per desktop wave with a CPU reference, which is the right kind of guard for this lowering. I did not run it in this pass; the Windows WARP receipt should come after the fix above.s_not_b32 execfill instead of thev_cndmask_b32_e64fill) is not the census spelling, so it falls to the refusal path. That is the intended behavior; just noting that the recognised form is narrow.No other open or merged PR covers this slice that I could find.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T13:33:11Z.
Review: COMMENTED
Review of head
e1a52c13(follow-up to the earlier review ond92978e3).Checked on Linux, against origin/main:
.textsection is compared, so branch targets are resolved the same way the real object has them.tests/startup_gfx2hlsl_wave_reduce_check.pypasses (PASS, self-test passed).wave_reduce_cndmask_fillaccept5; the check then fails. Restored afterwards.tests/docs_links_check.py,tests/bidi_controls_check.pyandgit diff --check origin/main...HEADare clean.Real GPU run not available (no hardware GPU host at the moment); WARP results will follow in the Win2025 receipt.
No blocking findings.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-26T13:28:56Z.
@tedin7 thank you for the careful review and for pinning down the cause.
Pushed
e1a52c13(test only, the translator is unchanged):-filetype=objand compared with the.textsection, as you suggested; the others keep the-show-encodingpath. I had run thebinarymode with clang 15 and llvm-objdump 15 standing in forllvm-mc-15, which resolves the branch, so I missed it. With llvm-mc 15.0.7 the old head fails on exactly those three programs, and the new one passes all 53 when run the way thepixel-binaryjob runs it (merge_checksovertests/checks/pixel-binary, thenrun_checks).5turns the controls red (3 checks).The WARP and RTX 3070 draw tests pass on
e1a52c13(dev and release, twice each); the numbers are in the updated description. Agreed that the recognised form is narrow on purpose: an LLVM-styles_not_b32 execfill stays refused.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-26T13:33:10Z.
Independent AMD (RDNA 4) draw result on
e1a52c13: the threestartup_gfx2hlsl_wave_reduceentries pass, the new fill-constant refusal is pinned by a one-line mutation, and the desktop wave is 64 lanes on this adapter. Not a code change; measurements only.Environment
10.0.26200.9974(ver); MSVC19.44.35230(BuildTools14.44.35207), presetdev.1.9.2602.17(Windows SDK10.0.26100.0), the binary theadd_testline passes.1002:7550, driver32.0.32029.2010; WARP10.0.26100.9818for thewarpentry.cleangate incheck_drawhas nothing to inspect.git rev-parse HEAD=e1a52c135b32d413eeecc43bbbb920d3d6437427andgit status --porcelain --untracked-files=noempty just before the build; onlyoffscreen_check(plus its dependencies) was built. Merge-base withorigin/main(da782506) is02075500.Commands
Observed
The draw entries print one facts line per reference draw;
v_min_u32,v_max_u32andmin then max, interleavedprint the same counts asv_or_b32on both backends:No line in any log contains
MISMATCH. Each backend draws 18 times: 4 reference draws plus 14 mutants (3 forv_or_b32, 4 each forv_min_u32andv_max_u32, 3 for the interleaved pair). All 28 mutant lines (14 per backend) report a value difference, for example on hardwareThe repeated run holds 12
backend=hardwarelines and 42 mutant lines — three passes of four reference draws and fourteen mutants — with the samecovered/waves/lane_count/partial_waves/partial_quadsin each.This head against
d92978e3tools/startup/gfx2hlsl.pyis byte-identical between the two heads (sha256 5374b13aa098509ec3a5891bbb006481, 679 265 bytes), and so istests/startup_image_3d_check.py; only the check fixture differs._hardwareentry prints 136 lines ond92978e3and 139 on this head, anddiffbetween them is exactly the three new linesrefuses a fill with another constant (5 for v_or_b32|v_min_u32|v_max_u32), by name: ok. So the draw words measured here are the same ones the previous head produced.The new refusal is pinned, not just added
Widening
wave_reduce_cndmask_fillto accept5(tools/startup/gfx2hlsl.py:9420:ops[1] not in ('0', '-1')becomes('0', '-1', '5')), a local edit restored afterwards —git status --porcelain --untracked-files=noempty and the sameHEADafter the restore — turnsstartup_gfx2hlsl_wave_reduce_controlsred:After the restore the same entry passes. The
binarymode was not run (nollvm-mcon this machine), so the branch-assembly change is not covered by this receipt.What the hardware entry adds over WARP
warpentry packs 4-lane waves (135 waves for 482 covered pixels), so each wave is a single pixel quad. The hardware entry packs 64-lane waves: 11 waves for the same 482 pixels, 8 of them partial, so 3 waves hold exactly 64 covered pixels with 64 distinct lane indices (:652) — at least 16 quads each. Those are the only reductions in this fixture that cross a quad boundary at the pixel level.the lane's own valueandthe first lane's valuemutants are detected on both backends, so the value read back is the emittedWaveActive*and not the lane's own input.-Gisto the fixture's own compiler invocation (tests/startup_image_3d_check.py:145; the flag the repo builder passes attools/startup/gfx2hlsl.py:11044) as a local edit, restored afterwards, gives the same 139 output lines for_hardware(diffempty).Notes on the guard
Stage.WAVE_REDUCE_FILLSwith(Stage.__dict__['wave_reduce_cndmask_fill'],)(tests/startup_gfx2hlsl_wave_reduce_check.py:496-498), andWAVE_REDUCE_FILLSis built as exactly that tuple (tools/startup/gfx2hlsl.py:9428). It pins which form sits in the table, not what the form accepts — which is why the mutation above has to bite through the refusal fixtures at:417-421rather than through this check.WaveReadLaneFirst(pid)in the appended channel,:580and:647); a one-pixel-per-wave partition would make each pixel its own reference. What rules that out in this run is thethe lane's own valuemutant being detected, not the oracle itself.Limits
debug_layer=unavailablemakescleanvacuous.-T ps_6_0and no-Gis; the-Gisrun above shows the output does not change, but these entries still do not run the builder's compile profile.mrt0is replaced byWaveGetLaneIndex() | WaveGetLaneCount() << 8 | WaveReadLaneFirst(pid) << 16, so the stage's ownwoutput is not compared.errorsis non-empty, anderrorsalso carries the structural checks (:652on the lane indices,:678on partly covered quads and the covered count). Every mutant line here names a value difference, but the guard does not require one.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T13:36:50Z.
Win2025 WARP receipt for head
e1a52c13(first auto-discovered run did not include this PR's tests; this run targets them by name).Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
e1a52c135b32d413eeecc43bbbb920d3d6437427:--preset dev):cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim offscreen_check -- -j 2):4.11s(configure0.00s)ctest --preset dev -R 'startup_gfx2hlsl_wave_reduce' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (7.66s:startup_gfx2hlsl_wave_reduce_controls2.90s,startup_gfx2hlsl_wave_reduce_warp5.03s,startup_gfx2hlsl_wave_reduce_hardware4.57s)/O2(--preset release):cmake --build --preset release --target d3d12_xs_device_obj d3d12_xs_shim offscreen_check -- -j 2):7.67s(configure0.00s)ctest --preset release -R 'startup_gfx2hlsl_wave_reduce' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (7.38s:startup_gfx2hlsl_wave_reduce_controls2.89s,startup_gfx2hlsl_wave_reduce_warp4.38s,startup_gfx2hlsl_wave_reduce_hardware4.31s)tests/startup_gfx2hlsl_wave_reduce_check.pyok (2.7s)sccache: 25/27 hits; wall28.4stested:
e1a52c13via fetch (pull/2835/head, no patch)Real-GPU run not available (RunPod stopped).
AI-assisted; commands listed were executed locally.