Skip to content

startup: lower v_sad_u8, v_sad_u16, and v_sad_u32 in gfx2hlsl - #2450

Merged
MasterDomino merged 2 commits into
R0Y00:mainfrom
triggered02:startup/sad-u8-u16
Nov 25, 2026
Merged

MasterDomino merged 2 commits into
R0Y00:mainfrom
triggered02:startup/sad-u8-u16

Conversation

@triggered02

@triggered02 triggered02 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Lowers unmodified gfx1013 VOP3A v_sad_u8 (0x15a), v_sad_u16 (0x15c), and v_sad_u32 (0x15d) in tools/startup/gfx2hlsl.py.

What changed

  • tools/startup/gfx2hlsl.py:
    • 'v_sad_u8', 'v_sad_u16' and 'v_sad_u32' in VEC_U, emitted as on-demand preamble helpers sad_u8, sad_u16, sad_u32 in Pipeline.common() (tracked via self.pipe.uses_sad on both Pipeline and ComputePipeline).
    • sad_u8(a, b, c) sums uint(abs(int((a >> shift) & 0xffu) - int((b >> shift) & 0xffu))) across the 4 byte lanes and adds c with 32-bit unsigned wraparound.
    • sad_u16(a, b, c) sums uint(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) + c with an unsigned comparison (no signed int overflow).
    • check_encoding refuses the per-source VOP3A abs bits (w0[10:8]) and neg bits (w1[31:29]) on the raw word, so neg on an inline 1 is refused rather than printed as -1 and read as a negative constant; VEC_U/Stage.u refuse the printed |vN|/-vN forms, 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.yml pure-Python job, AGENTS.md pure-Python list) — the latter added in d2cf1136.

Measured checks (d2cf1136, rebased onto origin/main 1a0e506d)

  • 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 + 12 abs/neg rows), 36 encoding-guard rows whose refusal reason is read back from G.decode() (12 per instruction: abs and neg on sources 0/1/2 as a VGPR and as an inline 1), 39 Stage.u refusal forms, and 3 unlowered controls.
  • tests/startup_gfx2hlsl_sad_check.py draw <dxc> <offscreen_check> warp <fxc> (Microsoft Basic Render Driver 1414:008c, driver 10.0.26100.9818): PASS — 18/18 good draws (9 FXC ps_5_1 + 9 DXC ps_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, driver 32.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 _debug1 variants 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.
  • Fail-first mutations on tools/startup/gfx2hlsl.py: the 4 helper/uses_sad mutations are caught, the abs mask narrowed to w0 & 0x100 is caught (v_sad_u8: encoding guard refuses abs src1 VGPR), and the neg mask narrowed to w1 & 0x20000000 is caught (v_sad_u8: accepted binary VOP3A with neg src1 inline 1, i.e. the silent -1 acceptance this guard exists to prevent). The last two survived the pipeline-level refusal control because Stage.u still rejects the printed |v1|.

Not established

  • No console measurement: the draws show that the translated desktop HLSL matches the Python reference for these forms, not that the console executes them.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T17:10:32.967373Z 9377558 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.py exit 0, including binary VOP3A refuses clamp, omod, op_sel, abs, and neg (VGPR & inline).

mutation result
sad_u8 helper: widen the byte lanes to words caught
sad_u16 helper: mask to one half caught
sad_u32: move the addend outside the wrap caught
uses_sad bookkeeping removed caught
the VOP3A abs/neg guard narrowed: word0 & 0x700 → word0 & 0x100 survived

Why the last one is a gap

word0 bits 10:8 are the three per-source ABS bits (the neighbouring v_cmp*_16 guard two lines up says exactly that: VOP3 w0[10:8] ABS). 0x700 covers all three; 0x100 covers only the first source. So with the mutant, ABS on source 1 or source 2 of v_sad_u8/u16/u32 is accepted, which is the case the comment above the guard is trying to prevent — neg on an inline integer printing as -N and 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:

  1. assert the mask itself: assert 'word0 & 0x700' in source_text — crude but it stops the narrowing, in the same spirit as the control: the guard is in the translator once pattern already used elsewhere in this tree;
  2. one refusal case per source slot, encoding ABS on source 0, then 1, then 2 (the fixture already builds VOP3A binaries, so this is three rows);
  3. the same for word1 & 0xe0000000 (NEG, bits 31:29) — narrowing that to 0x20000000 would 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_u8 widening the lanes, sad_u16 masking to one half, and sad_u32 hoisting the addend out of the wrap all fail. The uses_sad bookkeeping also fails when removed, so the helper is not emitted unconditionally.

The unlowered control: removing v_sad_u* from VEC_U restores refusals line 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.

triggered02 added a commit to triggered02/Xbox2Windows that referenced this pull request Nov 25, 2026
The v_sad_u8/u16/u32 check listed the ABS (VOP3A w0[10:8]) and NEG
(w1[31:29]) rows but aggregated them into one boolean, so a mask narrowed
to a single bit (w0[8] or w1[29]) still made G.Pipeline() raise: the
printed |v1| or -1 was caught later by Stage.one(), not by the encoding
guard. Mutation of either mask survived.

Read the refusal back from G.decode()'s refusals list, one row per source
and modifier, for VGPR and inline-integer sources alike, and assert the
exact 'v_sad_* with neg/abs' reason, so narrowing either mask fails.

Register tests/startup_gfx2hlsl_sad_check.py in the windows.yml
pure-Python job and the AGENTS.md pure-Python list.

Refs R0Y00#2450

AI-assisted.
triggered02 added a commit to triggered02/Xbox2Windows that referenced this pull request Nov 25, 2026
The v_sad_u8/u16/u32 check listed the ABS (VOP3A w0[10:8]) and NEG
(w1[31:29]) rows but aggregated them into one boolean, so a mask narrowed
to a single bit (w0[8] or w1[29]) still made G.Pipeline() raise: the
printed |v1| or -1 was caught later by Stage.one(), not by the encoding
guard. Mutation of either mask survived.

Read the refusal back from G.decode()'s refusals list, one row per source
and modifier, for VGPR and inline-integer sources alike, and assert the
exact 'v_sad_* with neg/abs' reason, so narrowing either mask fails.

Register tests/startup_gfx2hlsl_sad_check.py in the windows.yml
pure-Python job and the AGENTS.md pure-Python list.

Refs R0Y00#2450

AI-assisted.
@triggered02

Copy link
Copy Markdown
Contributor Author

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 & 0x700 narrowed to word0 & 0x100, the ABS-on-source-1 word is still refused, just by a different guard: check_encoding passes it, G.decode() prints v12 = v_sad_u8(v0, |v1|, v2), and Stage.u rejects 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 the windows.yml pure-Python job and the AGENTS.md pure-Python list):

  • abs rows for w0[10:8] bits 0/1/2 and neg rows for w1[31:29] bits 29/30/31, each on a VGPR source and again on an inline 1 — 12 rows per instruction, 36 total, each naming its slot and modifier.
  • Each row calls 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_sel stay in the existing G.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 to tools/startup/gfx2hlsl.py:

mutation result
word0 & 0x700 → word0 & 0x100 caught — AssertionError: v_sad_u8: encoding guard refuses abs src1 VGPR
word1 & 0xe0000000 → word1 & 0x20000000 caught — AssertionError: v_sad_u8: accepted binary VOP3A with neg src1 inline 1 (the mutant reads -1 as a negative constant, i.e. the actual bug)
the four helper / uses_sad mutations from your table still caught (unchanged)

Rebased and re-verified on origin/main 1a0e506d (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.py and git diff --check → all pass.
  • draw mode: 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.md no longer claims the _hardware variant 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.

@MasterDomino MasterDomino 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 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:

    • unsigned byte and halfword absolute differences plus S2, with 32-bit wrap;
    • sad_u32 as an unsigned |S0 − S1| + S2, the same as LLVM's umax−umin+c selection and pixel_aot's max − min + c.

    Everything is integer, so the FXC/DXC float relaxations don't apply.

  • Modifiers:

    • ABS and NEG bits are refused on the raw word for every source slot.
    • clamp, omod and op_sel are refused: v_sad_u32 … clamp, assembled with llvm-mc 15.0.7, is refused as v_sad_u32 with ['clamp'].
    • Inline float constants lower as their bit patterns (1.0 becomes 0x3f800000u).
    • v_sad_hi_u8 and v_msad_u8 stay refused.
  • Cache revision: x2w-startup-shader-14 and 14cs are unchanged. I replayed the 863 distinct Pipeline/ComputePipeline inputs built by 69 passing gfx2hlsl checks through the merge base and this head:

    • previously accepted shaders whose text changed: 0 of 315;
    • accepted shaders now refused: none;
    • previously refused shaders now accepted: 4, all 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)

python -B tests/startup_gfx2hlsl_sad_check.py
ctest --preset dev --no-tests=error -j1 -R '^startup_gfx2hlsl_sad_(controls|warp|hardware)$' -V
python -B tests/startup_gfx2hlsl_{handler_arity,operand_count,bit_alu}_check.py
Test Result
startup_gfx2hlsl_sad_controls pass
startup_gfx2hlsl_sad_warp pass: 18 good draws (9 FXC ps_5_1, 9 DXC ps_6_0); the 6 mutated draws read their mutated words; debug_errors=0
startup_gfx2hlsl_sad_hardware (RX 9070 XT) pass: the same 18 + 6, with 0 debug errors or warnings
neighbouring checks pass

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:

Mutation Controls Draw
sad_u8 drops + S2 600-triple MISMATCH expected 000000ff,00000109,fffffc0a,000000df, actual 00000000,…
sad_u32 compares as signed 605-triple MISMATCH expected ffffffff,ffffffff,80000006,ffffffff, actual 00000001,00000001,80000004,00000005
sad_u16 high half sign-extended 600-triple MISMATCH expected 0000ffff,00010009,fffe0004,00000fff, actual 00000001,0000000b,ffff0002,00000fff
encoding guard without v_sad_u32 accepted binary VOP3A with neg src0 inline 1 (the draw has no modifier rows)

All were caught.

To resolve before merge

  1. Rebase: docs/experiments.md conflicts with current main, because both append a section at the end. Keep both. With that resolved locally, the checks above pass 3/3 on a rebuilt offscreen_check. Note also that #2452 adds a check_encoding block at the same spot in gfx2hlsl.py; keeping both blocks works, and both checks pass on main + #2450 + #2452.
  2. Nit, docs/experiments.md:14779: the text says that on the GTX 1660 SUPER "the _debug1 variants skip with exit 77", but this PR registers only _controls, _warp and _hardware. Please drop or correct that clause.

AI-assisted; commands listed were executed locally.

The v_sad_u8/u16/u32 check listed the ABS (VOP3A w0[10:8]) and NEG
(w1[31:29]) rows but aggregated them into one boolean, so a mask narrowed
to a single bit (w0[8] or w1[29]) still made G.Pipeline() raise: the
printed |v1| or -1 was caught later by Stage.one(), not by the encoding
guard. Mutation of either mask survived.

Read the refusal back from G.decode()'s refusals list, one row per source
and modifier, for VGPR and inline-integer sources alike, and assert the
exact 'v_sad_* with neg/abs' reason, so narrowing either mask fails.

Register tests/startup_gfx2hlsl_sad_check.py in the windows.yml
pure-Python job and the AGENTS.md pure-Python list.

Refs R0Y00#2450

AI-assisted.
@triggered02

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (�fba03c9) as @thevoid asked. Head is now �991d931 (was d2cf113); 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.

@MasterDomino MasterDomino 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.

Approved at head a991d931, triggered02's rebase of d2cf1136 onto main. Thanks for the rebase.

Re-verified at a991d931

  • Same change: the PR's changed lines in tools/startup/gfx2hlsl.py, the check, CMakeLists.txt, AGENTS.md and the workflow are identical to d2cf1136'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.
  • Rebuilt and re-ran on the rebased tree: Windows 11 build 26300, offscreen_check built at a991d931.
    ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sad_(controls|warp|hardware)$' -V
    python -B tests/startup_gfx2hlsl_{handler_arity,operand_count,bit_alu}_check.py
    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.md still says the _debug1 variants skip with exit 77 on the GTX 1660 SUPER (now at line 14900), but this PR registers only _controls, _warp and _hardware. It's a one-line wording fix that can go in a later docs touch-up.

Note for #2452

#2452 adds a check_encoding block at the same spot in gfx2hlsl.py, so it will need one more rebase after this lands; keep both blocks.

Merge checks: required CI passed at this head. The /O2 and 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.

@MasterDomino
MasterDomino merged commit 16c2fd3 into R0Y00:main Nov 25, 2026
5 checks passed
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Post-merge audit of the v_sad_* lowering: ten mutations, all caught

Read this as a coverage statement about main at 16c2fd3d, not a review request. I have been mutating the code
behind 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.

python -B tests/startup_gfx2hlsl_sad_check.py
    unlowered control: removing v_sad_u* from VEC_U restores refusals: ok
    exit 0
mutation of tools/startup/gfx2hlsl.py caught
sad_u8 template → sad_u16 caught
sad_u32({0}, {1}, {2}) → third source reused ({0}) so the accumulator is ignored caught
sad_u16 sources swapped ({1}, {0}) caught
pipe.uses_sad.add(base) removed caught
uses_sad recorded only when a set already exists (add → union) caught
the uses_sad block guarded by if False: caught
VEC_U arity 3 → 4 caught
the sad neg/abs refusal deleted entirely caught
the refusal restricted to word0 & 0x700 (word1 bits ignored) caught
the refusal restricted to word1 & 0xe0000000 (word0 bits ignored) caught

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 & 0xe0000000 for one reason — a NEG on an inline integer would otherwise
print as -N and be accepted as a negative constant. Dropping either half, or the whole guard, is invisible unless the
fixture actually assembles the offending encoding, and it does.

Likewise the two uses_sad mutations: recording the name only into an existing set, or not at all, would change the
emitted 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.

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.

3 participants