xshader: lower SAD forms and v_mad_i64_i32 #102

Manually merged
github merged 1 commit from gh-pr/102/head into gh-pr/102/base 2026-11-26 06:31:19 +00:00
Owner

@⁠JohnVonWells on GitHub · 2026-11-24 22:28 UTC · original
xshader/sad-and-mad-i64 → main · +532 −9 · 6 files · 1 commits · merged 2026-11-24 22:31 UTC

Third PR from the synthetic opcode sweep, after #95 and #99.

Instruction Result
v_sad_u8 Σ |a.byte_i − b.byte_i| + c
v_sad_hi_u8 (Σ |a.byte_i − b.byte_i|) << 16 + c
v_sad_u16 Σ |a.half_i − b.half_i| + c
v_sad_u32 |a − b| + c
v_mad_i64_i32 sext(a) · sext(b) + c64

All results wrap modulo 2^32 (SAD) or 2^64 (mad).

Semantics. LLVM's gfx1030 backend selects v_sad_u32 for add(sub(umax(a, b), umin(a, b)), c), and v_mad_i64_i32 v[0:1], null, … for sext(a) * sext(b) + c in i64. The byte and half SADs follow the RDNA2 ISA definitions. LLVM only exposes them as intrinsics and does not fold them.

Lowering

  • The SAD forms go into the ordinary 32-bit forms in hlsl/int32.rs, with the same scope as #95: VGPR sources only, compute shaders only, and clamp, abs, neg and op_sel refused.
  • v_mad_i64_i32 shares the existing v_mad_u64_u32 path and uses tb_mulhi_i32 for the high word. The low product word is the same for both. It accepts only the null carry destination, which is what LLVM emits when the carry is unused; a real carry for the signed form is not modelled.
  • Also fixed: v_mad_u64_u32 with that null carry used to be refused (unsupported multiply-add carry destination). It is now accepted and simply drops the carry.
  • v_msad_u8 and the quad SADs stay refused.

Verification

  • Unit tests decode LLVM gfx1030 words for every form and every refusal.
  • tools/check_sad_mad64_gpu.py runs one LLVM-assembled program with all six forms:
    • Every destination holds a stale value first.
    • Two forms accumulate in place: v_mad_i64_i32 v[18:19], null, v4, v5, v[18:19] and v_sad_u8 v20, v4, v5, v20.
    • Inputs: 48 lanes built from byte-, half- and sign-boundary values, then random values. The receipt records the assembly, so the exact program can be reproduced.
    • The reference is a Python model with hand-computed checks in tests/test_sad_mad64_gpu.py.
  • Result on an AMD Radeon RX 6950 XT (1002:73a5, RADV): 288 inputs, 3,168 results, 9,216 GPU words, zero mismatches. The receipt is evidence/sad-mad64-control.json.
  • The emitter from cb185e0 refuses the program (--baseline).
  • Negative control: I rebuilt the emitter with the unsigned high word for v_mad_i64_i32. All 24 dispatches then failed, only in the two signed high-word columns.
  • Unchanged: all shader-compiler tests and all 210 Python tests pass.

Note on #101: that PR adds flat_inputs/flat_outputs to ShaderExtract, and its Rust CI currently fails on the test literal in hlsl/float64.rs that came in with #99. To avoid adding another literal, the new int32 test reuses that float64 test helper, so there is still only one place to update.

AI-assisted.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY

**@⁠JohnVonWells** on GitHub · 2026-11-24 22:28 UTC · [original](https://github.com/brcly/TuxBox/pull/102) `xshader/sad-and-mad-i64` → `main` · +532 −9 · 6 files · 1 commits · merged 2026-11-24 22:31 UTC Third PR from the synthetic opcode sweep, after #95 and #99. | Instruction | Result | |---|---| | `v_sad_u8` | Σ \|a.byte_i − b.byte_i\| + c | | `v_sad_hi_u8` | (Σ \|a.byte_i − b.byte_i\|) << 16 + c | | `v_sad_u16` | Σ \|a.half_i − b.half_i\| + c | | `v_sad_u32` | \|a − b\| + c | | `v_mad_i64_i32` | sext(a) · sext(b) + c64 | All results wrap modulo 2^32 (SAD) or 2^64 (`mad`). **Semantics.** LLVM's gfx1030 backend selects `v_sad_u32` for `add(sub(umax(a, b), umin(a, b)), c)`, and `v_mad_i64_i32 v[0:1], null, …` for `sext(a) * sext(b) + c` in i64. The byte and half SADs follow the RDNA2 ISA definitions. LLVM only exposes them as intrinsics and does not fold them. **Lowering** - The SAD forms go into the ordinary 32-bit forms in `hlsl/int32.rs`, with the same scope as #95: VGPR sources only, compute shaders only, and clamp, abs, neg and op_sel refused. - `v_mad_i64_i32` shares the existing `v_mad_u64_u32` path and uses `tb_mulhi_i32` for the high word. The low product word is the same for both. It accepts only the `null` carry destination, which is what LLVM emits when the carry is unused; a real carry for the signed form is not modelled. - **Also fixed:** `v_mad_u64_u32` with that `null` carry used to be refused (`unsupported multiply-add carry destination`). It is now accepted and simply drops the carry. - `v_msad_u8` and the quad SADs stay refused. **Verification** - Unit tests decode LLVM gfx1030 words for every form and every refusal. - `tools/check_sad_mad64_gpu.py` runs one LLVM-assembled program with all six forms: - Every destination holds a stale value first. - Two forms accumulate in place: `v_mad_i64_i32 v[18:19], null, v4, v5, v[18:19]` and `v_sad_u8 v20, v4, v5, v20`. - Inputs: 48 lanes built from byte-, half- and sign-boundary values, then random values. The receipt records the assembly, so the exact program can be reproduced. - The reference is a Python model with hand-computed checks in `tests/test_sad_mad64_gpu.py`. - **Result on an AMD Radeon RX 6950 XT** (`1002:73a5`, RADV): 288 inputs, 3,168 results, 9,216 GPU words, zero mismatches. The receipt is `evidence/sad-mad64-control.json`. - The emitter from `cb185e0` refuses the program (`--baseline`). - **Negative control:** I rebuilt the emitter with the unsigned high word for `v_mad_i64_i32`. All 24 dispatches then failed, only in the two signed high-word columns. - Unchanged: all shader-compiler tests and all 210 Python tests pass. **Note on #101:** that PR adds `flat_inputs`/`flat_outputs` to `ShaderExtract`, and its Rust CI currently fails on the test literal in `hlsl/float64.rs` that came in with #99. To avoid adding another literal, the new `int32` test reuses that `float64` test helper, so there is still only one place to update. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY
Add v_sad_u8, v_sad_hi_u8, v_sad_u16 and v_sad_u32 to the ordinary
32-bit integer forms: the sum of absolute differences over bytes,
halves or words plus the 32-bit accumulator. Like the other ordinary
forms they accept VGPR sources only, in compute shaders only, without
clamp or op_sel.

Lower v_mad_i64_i32 next to v_mad_u64_u32: the low product word is
shared and the high word comes from the signed 32x32 product. It
accepts only the null carry destination LLVM emits when the carry is
unused. v_mad_u64_u32 now also accepts that null carry, which was
refused before.

LLVM gfx1030 selects v_sad_u32 for |a - b| + c and v_mad_i64_i32 for
sext(a) * sext(b) + c.

tools/check_sad_mad64_gpu.py runs all six forms on a GPU against a
Python model, with stale destinations and two in-place accumulations.
The int32 test reuses the float64 test helper, so ShaderExtract is
built in one place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:31:17 +00:00
github manually merged commit de5639fa2a into gh-pr/102/base 2026-11-26 06:31:19 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.