startup: lower v_sad_u8, v_sad_u16, and v_sad_u32 in gfx2hlsl #2442
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2450"
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 triggered02 on 2026-11-25T17:07:17Z.
Lowers unmodified gfx1013 VOP3A
v_sad_u8(0x15a),v_sad_u16(0x15c), andv_sad_u32(0x15d) intools/startup/gfx2hlsl.py.What changed
tools/startup/gfx2hlsl.py:'v_sad_u8','v_sad_u16'and'v_sad_u32'inVEC_U, emitted as on-demand preamble helperssad_u8,sad_u16,sad_u32inPipeline.common()(tracked viaself.pipe.uses_sadon bothPipelineandComputePipeline).sad_u8(a, b, c)sumsuint(abs(int((a >> shift) & 0xffu) - int((b >> shift) & 0xffu)))across the 4 byte lanes and addscwith 32-bit unsigned wraparound.sad_u16(a, b, c)sumsuint(abs(int(a & 0xffffu) - int(b & 0xffffu))) + uint(abs(int(a >> 16) - int(b >> 16))) + c.sad_u32(a, b, c)computes(a > b ? a - b : b - a) + cwith an unsigned comparison (no signedintoverflow).check_encodingrefuses the per-source VOP3Aabsbits (w0[10:8]) andnegbits (w1[31:29]) on the raw word, sonegon an inline1is refused rather than printed as-1and read as a negative constant;VEC_U/Stage.urefuse the printed|vN|/-vNforms,clamp/omod/op_sel, and wrong operand counts.tests/startup_gfx2hlsl_sad_check.py,CMakeLists.txt,docs/experiments.md, and the CI registrations (.github/workflows/windows.ymlpure-Python job,AGENTS.mdpure-Python list) — the latter added ind2cf1136.Measured checks (
d2cf1136, rebased ontoorigin/main1a0e506d)tests/startup_gfx2hlsl_sad_check.py(pure Python): PASS — on-demand emission (5/5), CPU HLSL evaluation against the reference over 600 triples (v_sad_u8), 600 (v_sad_u16) and 605 (v_sad_u32), mutated-HLSL mismatch controls, 54 synthetic VOP3A binary refusals (18 per instruction: 6 modifier forms + 12abs/negrows), 36 encoding-guard rows whose refusal reason is read back fromG.decode()(12 per instruction:absandnegon sources 0/1/2 as a VGPR and as an inline1), 39Stage.urefusal forms, and 3 unlowered controls.tests/startup_gfx2hlsl_sad_check.py draw <dxc> <offscreen_check> warp <fxc>(Microsoft Basic Render Driver1414:008c, driver10.0.26100.9818): PASS — 18/18 good draws (9 FXCps_5_1+ 9 DXCps_6_0) matched the reference words, and 6/6 mutated draws failed with the exact wrong readback words.tests/startup_gfx2hlsl_sad_check.py draw <dxc> <offscreen_check> hardware <fxc>(NVIDIA GeForce GTX 1660 SUPER, driver32.0.16.1074, Windows build 26200): PASS — the same 18/18 good and 6/6 mutated draws. The debug layer is not installed on that host, so the_debug1variants skip with exit 77.startup_translator_check.py,docs_links_check.py(468 links),bidi_controls_check.py,startup_tools_lint_check.py,git diff --check: all PASS.tools/startup/gfx2hlsl.py: the 4 helper/uses_sadmutations are caught, theabsmask narrowed tow0 & 0x100is caught (v_sad_u8: encoding guard refuses abs src1 VGPR), and thenegmask narrowed tow1 & 0x20000000is caught (v_sad_u8: accepted binary VOP3A with neg src1 inline 1, i.e. the silent-1acceptance this guard exists to prevent). The last two survived the pipeline-level refusal control becauseStage.ustill rejects the printed|v1|.Not established
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T17:07:28Z.
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
9377558ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T17:20:23Z.
Five mutations: four caught, and the survivor is a real gap in the abs/neg guard rather than an equivalent
Baseline on this branch:
tests/startup_gfx2hlsl_sad_check.pyexit 0, includingbinary VOP3A refuses clamp, omod, op_sel, abs, and neg (VGPR & inline).sad_u8helper: widen the byte lanes to wordssad_u16helper: mask to one halfsad_u32: move the addend outside the wrapuses_sadbookkeeping removedword0 & 0x700→word0 & 0x100Why the last one is a gap
word0bits10:8are the three per-source ABS bits (the neighbouringv_cmp*_16guard two lines up says exactly that:VOP3 w0[10:8] ABS).0x700covers all three;0x100covers only the first source. So with the mutant, ABS on source 1 or source 2 ofv_sad_u8/u16/u32is accepted, which is the case the comment above the guard is trying to prevent —negon an inline integer printing as-Nand being accepted as a negative constant.The check does refuse "abs" today (that line passes on the branch), so it is exercising one of the three bits. With three sources and one mask, one case cannot cover all three, and nothing notices when two of the three stop being tested.
Three ways to close it, cheapest first:
assert 'word0 & 0x700' in source_text— crude but it stops the narrowing, in the same spirit as thecontrol: the guard is in the translator oncepattern already used elsewhere in this tree;word1 & 0xe0000000(NEG, bits31:29) — narrowing that to0x20000000would survive for the identical reason.I would take (2) plus (3): it turns the single "abs and neg refused" line into six, and each one names which slot it is.
What is well pinned here
The three helpers are each caught, which is more than I expected for a diff whose helpers are generated as HLSL text:
sad_u8widening the lanes,sad_u16masking to one half, andsad_u32hoisting the addend out of the wrap all fail. Theuses_sadbookkeeping also fails when removed, so the helper is not emitted unconditionally.The
unlowered control: removing v_sad_u* from VEC_U restores refusalsline is the right shape for a table entry — it proves the acceptance comes from the table and not from the table plus something else.AI-assisted; the five mutations were run locally on Windows 11 build 26300.9974.
Imported from R0Y00/Xbox2Windows. Originally posted by triggered02 on 2026-11-25T17:56:38Z.
Thanks — I took (2) plus (3), with one addition that (2)+(3) alone would not have covered.
Why the six rows alone were not enough. With
word0 & 0x700narrowed toword0 & 0x100, the ABS-on-source-1 word is still refused, just by a different guard:check_encodingpasses it,G.decode()printsv12 = v_sad_u8(v0, |v1|, v2), andStage.urejects the|v1|operand. A per-slot "is it refused at all?" boolean therefore still passes on the mutant. I confirmed that on the mutant before touching the test — the pipeline-level control was green for the wrong reason, exactly as you guessed.What the check does now (
tests/startup_gfx2hlsl_sad_check.py, now also registered in thewindows.ymlpure-Python job and theAGENTS.mdpure-Python list):absrows forw0[10:8]bits 0/1/2 andnegrows forw1[31:29]bits 29/30/31, each on a VGPR source and again on an inline1— 12 rows per instruction, 36 total, each naming its slot and modifier.G.decode(word, refusals)and asserts the refusals list is exactly['v_sad_<width> with neg/abs'], so the row fails when the refusal comes from anywhere but the encoding guard.clamp/omod/op_selstay in the existingG.Pipeline()refusal loop: they have no encoding guard and are correctly refused through the printed modifier text.Mutation results on head
d2cf1136, each applied alone totools/startup/gfx2hlsl.py:word0 & 0x700→word0 & 0x100AssertionError: v_sad_u8: encoding guard refuses abs src1 VGPRword1 & 0xe0000000→word1 & 0x20000000AssertionError: v_sad_u8: accepted binary VOP3A with neg src1 inline 1(the mutant reads-1as a negative constant, i.e. the actual bug)uses_sadmutations from your tableRebased and re-verified on
origin/main1a0e506d(the branch was 14 commits behind):tests/startup_gfx2hlsl_sad_check.py→ exit 0;startup_translator_check.py,docs_links_check.py(468 links),bidi_controls_check.py,startup_tools_lint_check.pyandgit diff --check→ all pass.drawmode: 18/18 good draws and 6/6 mutated draws pass on WARP (10.0.26100.9818) and again on the NVIDIA GeForce GTX 1660 SUPER (driver 32.0.16.1074, Windows build 26200).docs/experiments.mdno longer claims the_hardwarevariant was unrun, and it now documents the per-slot pinning and why a narrowed mask survives a pipeline-level control.I did not take (1) (asserting the guard's source text): the reason-pinned rows fail on behaviour, so renaming or reformatting the guard does not churn the fixture.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T19:22:32Z.
Review: COMMENTED
Review of head
d2cf1136. I found no P1 or P2 issues. It needs a rebase, and there's one docs nit; I'll merge once it's rebased and CI is green.What I checked
Semantics against RDNA 2
V_SAD_U8/U16/U32:S2, with 32-bit wrap;sad_u32as an unsigned|S0 − S1| + S2, the same as LLVM's umax−umin+c selection andpixel_aot'smax − min + c.Everything is integer, so the FXC/DXC float relaxations don't apply.
Modifiers:
clamp, omod and op_sel are refused:v_sad_u32 … clamp, assembled with llvm-mc 15.0.7, is refused asv_sad_u32 with ['clamp'].1.0becomes0x3f800000u).v_sad_hi_u8andv_msad_u8stay refused.Cache revision:
x2w-startup-shader-14and14csare unchanged. I replayed the 863 distinctPipeline/ComputePipelineinputs built by 69 passing gfx2hlsl checks through the merge base and this head:v_sad_*.So no bump is needed.
Commands executed (Windows 11 build 26300; AMD Radeon RX 9070 XT 1002:7550, driver 32.0.32029.2010, debug layer enabled; WARP 10.0.26100.9818; DXC/FXC from SDK 10.0.26100)
startup_gfx2hlsl_sad_controlsstartup_gfx2hlsl_sad_warpps_5_1, 9 DXCps_6_0); the 6 mutated draws read their mutated words;debug_errors=0startup_gfx2hlsl_sad_hardware(RX 9070 XT)There were no capability skips. This adds the AMD hardware run the description didn't have.
Negative control
Each mutation is one line of
tools/startup/gfx2hlsl.py, applied alone and reverted. The draws ran on WARP and the RX 9070 XT:sad_u8drops+ S2000000ff,00000109,fffffc0a,000000df, actual00000000,…sad_u32compares as signedffffffff,ffffffff,80000006,ffffffff, actual00000001,00000001,80000004,00000005sad_u16high half sign-extended0000ffff,00010009,fffe0004,00000fff, actual00000001,0000000b,ffff0002,00000fffv_sad_u32accepted binary VOP3A with neg src0 inline 1All were caught.
To resolve before merge
docs/experiments.mdconflicts with currentmain, because both append a section at the end. Keep both. With that resolved locally, the checks above pass 3/3 on a rebuiltoffscreen_check. Note also that #2452 adds acheck_encodingblock at the same spot ingfx2hlsl.py; keeping both blocks works, and both checks pass onmain+ #2450 + #2452.docs/experiments.md:14779: the text says that on the GTX 1660 SUPER "the_debug1variants skip with exit 77", but this PR registers only_controls,_warpand_hardware. Please drop or correct that clause.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T19:34:07Z.
Review: APPROVED
Approved at head
a991d931, triggered02's rebase ofd2cf1136ontomain. Thanks for the rebase.Re-verified at
a991d931tools/startup/gfx2hlsl.py, the check,CMakeLists.txt,AGENTS.mdand the workflow are identical tod2cf1136's, compared as the set of +/- lines. So my review and mutation results above still apply: the semantics match the ISA, there is no cache bump, and all mutants are caught on the RX 9070 XT and WARP.offscreen_checkbuilt ata991d931. 3/3 pass on the AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010) and WARP 10.0.26100.9818, and the neighbouring checks pass. There were no skips.Follow-up (not blocking)
docs/experiments.mdstill says the_debug1variants skip with exit 77 on the GTX 1660 SUPER (now at line 14900), but this PR registers only_controls,_warpand_hardware. It's a one-line wording fix that can go in a later docs touch-up.Note for #2452
#2452 adds a
check_encodingblock at the same spot ingfx2hlsl.py, so it will need one more rebase after this lands; keep both blocks.Merge checks: required CI passed at this head. The
/O2and coverage skips come from explicit workflow conditions. There are no unresolved review threads. I read @ChazY11's mutation report and triggered02's responses in full.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by triggered02 on 2026-11-25T19:29:32Z.
Rebased onto current main (fba03c9) as @TheVoid asked. Head is now 991d931 (was
d2cf1136); the two commits replayed, and the only conflict was the docs/experiments.md append-tail: I kept main's newest section and re-appended this PR's ## Startup translator: v_sad_u8 ... record. No conflict markers anywhere.Verified on 991d931: ests/startup_gfx2hlsl_sad_check.py control mode exit 0; the test file is blob-identical to the pre-rebase head; both encoding guards are present (_cmpx*_16 at gfx2hlsl.py:368, sad* at :374); the windows.yml and AGENTS.md registrations survived; docs_links_check.py 19 files/473 links; git diff --check clean. Diff vs main is the PR's 6 files (+601). PR is MERGEABLE and CI is running on the new head.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T19:35:28Z.
Post-merge audit of the
v_sad_*lowering: ten mutations, all caughtRead this as a coverage statement about
mainat16c2fd3d, not a review request. I have been mutating the codebehind other PRs and did the same to this one now that it is merged, because the sad lowering touches three separate
places that are easy to get partly right.
tools/startup/gfx2hlsl.pysad_u8template →sad_u16sad_u32({0}, {1}, {2})→ third source reused ({0}) so the accumulator is ignoredsad_u16sources swapped ({1}, {0})pipe.uses_sad.add(base)removeduses_sadrecorded only when a set already exists (add→union)uses_sadblock guarded byif False:VEC_Uarity 3 → 4word0 & 0x700(word1 bits ignored)word1 & 0xe0000000(word0 bits ignored)The last three are the ones I most wanted to see caught, because they are three different ways to get the same guard
wrong: line 374 tests
word0 & 0x700 or word1 & 0xe0000000for one reason — a NEG on an inline integer would otherwiseprint as
-Nand be accepted as a negative constant. Dropping either half, or the whole guard, is invisible unless thefixture actually assembles the offending encoding, and it does.
Likewise the two
uses_sadmutations: recording the name only into an existing set, or not at all, would change theemitted HLSL's declarations without changing the instruction text, which is exactly the sort of thing a text-only
assertion misses. It does not.
No survivors, so nothing to ask for. Recording the list here so the next person who touches these three sites knows
which mistakes are already covered.
AI-assisted; pure Python, no build or GPU, Windows 11 build 26300.9974.