Skip to content

xshader: lower masked and quad byte SADs - #286

Merged
brcly merged 1 commit into
brcly:mainfrom
JohnVonWells:xshader/masked-sad
Nov 27, 2026
Merged

brcly merged 1 commit into
brcly:mainfrom
JohnVonWells:xshader/masked-sad

Conversation

@JohnVonWells

Copy link
Copy Markdown
Contributor

Adds the four masked and quad byte SAD forms that the translator refused: v_msad_u8, v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8 and v_mqsad_u32_u8.

v_msad_u8 joins the other SADs in int32.rs. The quad forms get their own module, quad_sad.rs. Window k of the S0 pair is bits [8k+31:8k], each window is compared with S1, and the result goes into the matching accumulator field of S2: 16 bits for the _pk forms, 32 bits for v_mqsad_u32_u8. The translator computes every result word before it writes any, so a destination may overlap S0 or S2. As with the existing SADs, the forms accept plain VGPR operands without clamp, op_sel or negation, in compute shaders only. The register analysis knows about the extra source words and the wider results.

Semantics from native execution

The ISA documentation is unclear on two points. tools/native_rdna2/check_sad.py settles them by running the instructions natively on an RX 6950 XT, through the HSA runtime:

  • The masked forms skip a byte when the S1 byte is zero. A model that masks on S0 fails on most inputs.
  • Without clamp, the 16-bit fields of the _pk forms wrap. A saturating model fails.

MODE does not change any result: MODE 0 and 0x3ff give identical results. 16,384 inputs per kernel, 81,920 comparisons, no mismatches.

GPU control

tools/check_byte_sad_gpu.py runs the translated shaders on a D3D12 GPU against the same model. It uses dynamic VGPR operands and includes destinations that overlap the accumulator and the S0 pair. Result on the RX 6950 XT: 384 inputs and 6,528 results, all correct.

  • Baseline: the previous translator refuses the program.

  • Negative controls: masking on S0 instead of S1 fails 12 of 12 dispatches, and so does a 17-bit field instead of a 16-bit one.

  • Translator checks: byte-sad is now part of tools/check_translator_gpu.py. The sad-mad64 control had v_msad_u8 as a negative control (it must be refused). It now uses v_msad_u8 ... clamp, which stays refused.

python tuxbox.py check on the RX 6950 XT: Lanes, the D3D12 regression controls and all translator checks pass. (On the first run sad-mad64 failed on that old negative control; after the change, sad-mad64 and byte-sad pass.) There is no run on an NVIDIA card yet.

Coverage of single decodable ALU instructions goes from 498 to 502 translated.

Evidence: evidence/native-rdna2-byte-sad.json, evidence/byte-sad-control.json.

AI-assisted.

Add v_msad_u8 to the 32-bit integer forms and v_qsad_pk_u16_u8,
v_mqsad_pk_u16_u8 and v_mqsad_u32_u8 in a new quad_sad module. Window k
of the S0 pair is bits [8k+31:8k]; each window's SAD against S1 goes into
the matching 16- or 32-bit accumulator field of S2. Every result word is
computed before any is written, so a destination may overlap S0 or S2.
Plain VGPR operands without clamp, op_sel or negation, compute only. The
live-in analysis knows the extra source words and wider results.

Native RDNA2 execution (tools/native_rdna2/check_sad.py) settled what
the ISA text leaves open: the masked forms skip a byte whose S1 byte is
zero, the 16-bit fields wrap without clamp, and MODE changes nothing.
81,920 comparisons, no mismatches.

tools/check_byte_sad_gpu.py runs the translated shaders on a D3D12 GPU
against the same model, with dynamic operands and overlapping
destinations: 384 inputs, 6,528 results, zero mismatches. Masking on S0
or a 17-bit field fails it. It joins the translator's GPU checks, and the
sad-mad64 negative control now uses a clamped v_msad_u8, which stays
refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY
@brcly
brcly merged commit 6fe69f4 into brcly:main Nov 27, 2026
2 checks passed
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.

2 participants