Skip to content

Translator: v_qsad_pk_u16_u8 and v_mqsad_pk_u16_u8 - #623

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:valu-qsad
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:valu-qsad

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

tools/rdna2spirv.py raises Unsupported on v_qsad_pk_u16_u8 and v_mqsad_pk_u16_u8, the four-at-once versions of the SADs added in #544. They aren't in the translated corpus, so this is future-proofing. Claimed in #374.

Change

  • tools/rdna2spirv.py follows the RDNA ISA pseudocode. S0 is 64 bits, S1 is 32 bits, and S2 and D are 64 bits holding four u16 fields:
    • D.u16[k] = (S2.u16[k] + Σ over 4 bytes |W_k.b − S1.b|) & 0xFFFF, where W_k = S0[8k+31 : 8k]. The four windows slide over S0 one byte at a time.
    • In the m form, a byte where S1's byte is 0 is skipped, as in v_msad_u8.
    • Everything uses 32-bit ops: each window is (lo >> 8k) | (hi << (32 − 8k)). No Int64 is needed.
    • S0 and S2 can be VGPR or SGPR pairs, or a 64-bit inline constant. clamp raises Unsupported instead of being guessed.
    • v_mqsad_u32_u8 stays unsupported. test_translate.py and test_f16_op_sel.py use it as their example of an unsupported instruction.
  • tests/semantic/test_alu_qsad.py (new, stdlib unittest) checks D against a plain-Python reference:
    • a 6×6 edge grid per op, with accumulators that make the u16 fields wrap
    • 30 seeded cases, including masked bytes
    • an SGPR source pair with an inline accumulator
    • clamp rejected
    • D leaves exp as four 16-bit fields ORed into 0x40000000, so no export channel can be a NaN pattern

Tested

  • python -I -m unittest discover -s tests/semantic: all pass. Without the translator change, the new file has errors (Unsupported).
  • python -m compileall -q tools tests passes. Not run on a GPU, and no game files used.

AI-assisted (Claude Code): it wrote the change and the tests, and ran the checks listed above.

@IAmNotSiob IAmNotSiob left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

went through the window shifts and the masked form, looks right, and refusing clamp instead of guessing is good. tests pass with main (810). AI-assisted review (Claude Code).

@Andretuta

Andretuta commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 14ff174 on NVIDIA GeForce RTX 3060 (Ampere GA106, driver 617.14, Vulkan 1.4.351, subgroup 32..32, Windows 10 Pro build 19045, Python 3.13.14, llvm-mc 23.1.3 / spirv-val v2026.4 via MSYS2 clang64): pass

What was run (no game files; synthetic tests + direct RTX 3060 GPU execution):

  • python -m compileall -q tools tests — ok
  • python -I tests/semantic/test_alu_qsad.py -v — 5/5 ok
  • python -I -m unittest discover -s tests/semantic — 788/788 ok
  • python -I tests/test_shader_checks.py — 25/25 ok
  • python -I tests/test_run_gpu_tests.py — 18/18 ok
  • scripts/run_gpu_tests.py (native): 86/86 translated & spirv-val valid; 57/83 passed, 26 skipped (all 26 expected wave64 skips on 32-wide NVIDIA)
  • LEONIDA_W64_EMULATE=1 scripts/run_gpu_tests.py: 86/86 translated & spirv-val valid; 83/83 passed, 0 skipped, 0 failed
  • Direct synthetic compute shader executing 24 cases (48 dwords) of v_qsad_pk_u16_u8 and v_mqsad_pk_u16_u8 (VGPR pairs, u16 wraparound, masked zero bytes in S1, SGPR S0 pair + 64-bit inline -1/0 accumulator, dst == s0 aliasing, and 16 seeded random vectors) -> llvm-mc -> PR's rdna2spirv.py -> spirv-val -> vk_compute_test.exe on RTX 3060: 1/1 translated & valid, 24/24 cases (48/48 dwords) bit-exact (0 mismatches).

AI-assisted.

@matheuskshn

Copy link
Copy Markdown
Contributor

Tested 14ff174 on Windows 11 / Python 3.12.14. All five tests in test_alu_qsad.py passed through the CPU interpreter. Coverage includes accumulator wraparound, masked comparison bytes, and the SGPR/inline-accumulator path. The four outputs are compared as separate 16-bit fields. AI-assisted validation with synthetic inputs.

Both raised Unsupported. Four byte-SADs packed as u16: D.u16[k] =
(S2.u16[k] + SAD of S0's 32-bit window at byte k against S1) & 0xFFFF, the
windows sliding over the 64-bit S0 one byte at a time (RDNA ISA
pseudocode). The m form skips bytes where S1's byte is 0. clamp is rejected.

tests/semantic/test_alu_qsad.py checks both against a plain-Python reference.
main pins 48557ea again since the ItsNotRexed#666-ItsNotRexed#698 merge round, while
tools/rpf9dump/Program.cs (3816356) already uses ArchiveEntry.ResourceDrawableOffset
from 917e9b9, so the Windows 'check' job fails to compile rpf9dump on every
PR. Same one-line change as ItsNotRexed#713 / ItsNotRexed#732; whichever lands first, this merges
cleanly.
@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

CI note: the Windows check job failed here because main pins third_party/rpf9-explorer at 48557ea again since the #666–#698 merge round, while tools/rpf9dump/Program.cs (3816356) already uses ArchiveEntry.ResourceDrawableOffset from 917e9b9, so rpf9dump does not compile on any PR. I added the same one-line re-pin to 917e9b9 that #713 / #732 make (a separate commit, nothing else in it), so this PR gets a clean run; whichever lands first, the other merges cleanly.

@ItsNotRexed
ItsNotRexed merged commit c81acb6 into ItsNotRexed:main Nov 22, 2026
9 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.

5 participants