Recompiler: translate v_qsad_pk_u16_u8 and v_mqsad_{pk_u16,u32}_u8 #909

Closed
github-bot wants to merge 1 commit from gh-pr/909/head into gh-pr/909/base
Owner

@⁠bigiello on GitHub · 2026-11-24 08:03 UTC · original
feat/recomp-qsad → main · +226 −1 · 2 files · 1 commits · closed without merging

Draft pending scripts/smoke.ps1 regression blocks on a retail title (no local game files available; see Not tested).

Adds RDNA2 VOP3 translation for v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8, and v_mqsad_u32_u8. Previously the _pk_u16_u8 instructions routed into valu16 due to _u16 in their names, and 64/128-bit VALU operands were rejected.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs:
    • pk_sad_u16 helper in PRELUDE reusing sad_u8, msad_u8, and sat_u32.
    • Em::valu_qsad handling 64-bit SRC0, 32-bit SRC1, and 64-bit (_pk_u16_u8) / 128-bit VGPR (v_mqsad_u32_u8) accumulators via temporaries, enforcing VGPR bounds and clamping per element when CLAMP is set.
  • crates/xcl/src/gpu/recomp/tests.rs:
    • qsad_and_mqsad_operand_forms_and_validation (unit translation + DXC compile + bounds/non-VGPR SRC2 rejection).
    • gpu_qsad_and_mqsad (D3D12/DXIL hardware test across wave32 and wave64 covering sliding windows, zero-byte masking, CLAMP wrap vs saturation, SGPR/VCC/inline sources, overlapped registers, and EXEC masking).
    • isa_coverage: pass non-zero SRC2 VGPR operand for V_MQSAD_U32_U8 (OPR_SRC_VGPR).

Tested

  • Hardware: Windows 11 x64, AMD Ryzen 5 5600, 32 GB RAM, NVIDIA GeForce RTX 4070 SUPER (11999 MiB VRAM, D3D12 SM 6.6, 32-lane waves), Windows SDK DXC 1.8.2502.11 (10.0.26100.0\x64).
  • .\scripts\test.ps1 (at commit 8747cb1): exit 0 — 717 passed, 0 failed, 41 ignored across the workspace (xcl --lib: 676 passed, 0 failed, 21 ignored; 2 GPU tests skipped due to no wave64: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware; integration binaries: console_dlls_e2e 13 passed, console_shaders 3 passed / 4 ignored, disasm_e2e 1 passed, e2e 3 passed, gpu_compute_e2e 2 passed, gpu_pso_e2e 7 passed, heap_probe 0 passed / 4 ignored, pls_quota 1 passed, predication_e2e 1 passed, rpf_e2e 2 passed, sparse_reinit 1 passed, texture_states 1 passed, xg_oracle 6 passed / 12 ignored).
  • cargo test -p xcl --lib -- qsad --nocapture: 2 passed, 0 failed.
  • cargo test -p xcl --lib -- isa_coverage --ignored --nocapture: 1 passed (RDNA2 instructions (spec + console): 1157, translated: 1045 (90.3%), VOP3 290 / 298).
  • cargo clippy --workspace --all-targets: 0 errors, 0 new warnings in touched lines.
  • rustfmt --check crates\xcl\src\gpu\recomp\emit.rs crates\xcl\src\gpu\recomp\tests.rs: 0 diffs in changed lines (cargo fmt --check overall exits 1 due to pre-existing formatting in untouched files).

Not tested

  • Native 64-lane hardware waves (NVIDIA RTX 4070 SUPER only exposes 32-lane waves, so v_qsad* ran in wave32 and split-wave64).
  • Retail game shader packs and scripts/smoke.ps1 -Game <folder> (XCL_SAMPLE_* unset; no retail game files available locally).

AI use

  • Tool: Antigravity model: Neon. Assisted with implementing Em::valu_qsad, pk_sad_u16, unit/GPU tests, running the suite, and drafting this PR description. All diffs and test outputs verified locally.
**@⁠bigiello** on GitHub · 2026-11-24 08:03 UTC · [original](https://github.com/xcl-dev/xcl/pull/909) `feat/recomp-qsad` → `main` · +226 −1 · 2 files · 1 commits · closed without merging Draft pending `scripts/smoke.ps1` regression blocks on a retail title (no local game files available; see **Not tested**). Adds RDNA2 VOP3 translation for `v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8`, and `v_mqsad_u32_u8`. Previously the `_pk_u16_u8` instructions routed into `valu16` due to `_u16` in their names, and 64/128-bit VALU operands were rejected. **What is in it** - `crates/xcl/src/gpu/recomp/emit.rs`: - `pk_sad_u16` helper in `PRELUDE` reusing `sad_u8`, `msad_u8`, and `sat_u32`. - `Em::valu_qsad` handling 64-bit `SRC0`, 32-bit `SRC1`, and 64-bit (`_pk_u16_u8`) / 128-bit VGPR (`v_mqsad_u32_u8`) accumulators via temporaries, enforcing VGPR bounds and clamping per element when `CLAMP` is set. - `crates/xcl/src/gpu/recomp/tests.rs`: - `qsad_and_mqsad_operand_forms_and_validation` (unit translation + DXC compile + bounds/non-VGPR `SRC2` rejection). - `gpu_qsad_and_mqsad` (D3D12/DXIL hardware test across wave32 and wave64 covering sliding windows, zero-byte masking, `CLAMP` wrap vs saturation, SGPR/VCC/inline sources, overlapped registers, and `EXEC` masking). - `isa_coverage`: pass non-zero `SRC2` VGPR operand for `V_MQSAD_U32_U8` (`OPR_SRC_VGPR`). **Tested** - Hardware: Windows 11 x64, AMD Ryzen 5 5600, 32 GB RAM, NVIDIA GeForce RTX 4070 SUPER (11999 MiB VRAM, D3D12 SM 6.6, 32-lane waves), Windows SDK DXC 1.8.2502.11 (`10.0.26100.0\x64`). - `.\scripts\test.ps1` (at commit `8747cb1`): exit 0 — 717 passed, 0 failed, 41 ignored across the workspace (`xcl --lib`: 676 passed, 0 failed, 21 ignored; 2 GPU tests skipped due to `no wave64`: `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`; integration binaries: `console_dlls_e2e` 13 passed, `console_shaders` 3 passed / 4 ignored, `disasm_e2e` 1 passed, `e2e` 3 passed, `gpu_compute_e2e` 2 passed, `gpu_pso_e2e` 7 passed, `heap_probe` 0 passed / 4 ignored, `pls_quota` 1 passed, `predication_e2e` 1 passed, `rpf_e2e` 2 passed, `sparse_reinit` 1 passed, `texture_states` 1 passed, `xg_oracle` 6 passed / 12 ignored). - `cargo test -p xcl --lib -- qsad --nocapture`: 2 passed, 0 failed. - `cargo test -p xcl --lib -- isa_coverage --ignored --nocapture`: 1 passed (`RDNA2 instructions (spec + console): 1157, translated: 1045 (90.3%)`, `VOP3 290 / 298`). - `cargo clippy --workspace --all-targets`: 0 errors, 0 new warnings in touched lines. - `rustfmt --check crates\xcl\src\gpu\recomp\emit.rs crates\xcl\src\gpu\recomp\tests.rs`: 0 diffs in changed lines (`cargo fmt --check` overall exits 1 due to pre-existing formatting in untouched files). **Not tested** - Native 64-lane hardware waves (NVIDIA RTX 4070 SUPER only exposes 32-lane waves, so `v_qsad*` ran in wave32 and split-wave64). - Retail game shader packs and `scripts/smoke.ps1 -Game <folder>` (`XCL_SAMPLE_*` unset; no retail game files available locally). **AI use** - Tool: Antigravity model: Neon. Assisted with implementing `Em::valu_qsad`, `pk_sad_u16`, unit/GPU tests, running the suite, and drafting this PR description. All diffs and test outputs verified locally.
Add translation for the three remaining quad-byte SAD VOP3 instructions:
v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8, and v_mqsad_u32_u8. Previously
the packed u16 forms routed into the 16-bit VALU path because of '_u16'
in their mnemonics and v_mqsad_u32_u8 failed on 64/128-bit operands.

- Add Em::valu_qsad in emit.rs before the 16-bit VALU dispatch, reusing
  sad_u8, msad_u8, and sat_u32 plus a pk_sad_u16 prelude helper.
- Capture 64-bit SRC0 and 32-bit SRC1 in temporaries before writing
  destinations so overlapping VDST/SRC registers and EXEC masking stay
  exact across wave32 and wave64, and reject out-of-range VGPR spans or
  non-VGPR SRC2 on v_mqsad_u32_u8.
- Add translation/validation and D3D12 GPU tests covering window
  shifts, zero-byte masking, CLAMP wrap vs saturation, SGPR/VCC/inline
  sources, overlapped registers, and EXEC masks (ISA coverage VOP3
  287/298 -> 290/298, overall 1042 -> 1045 / 1157).

Assisted-by: Antigravity (exact model name not exposed to agent)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:36:37 +00:00
github-bot closed this pull request 2026-11-26 04:36:37 +00:00
Author
Owner

@⁠dcastell1 on GitHub · 2026-11-24 08:16 UTC · original

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on clean main@beaa245 (713 passed, 0 failed, 41 ignored on this box):

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
715 passed; 0 failed; 41 ignored   (exit 0)

Two more passing tests than main, so v_qsad_pk_u16_u8 and v_mqsad_{pk_u16,u32}_u8 translate and their fixtures run green here; nothing else changes verdict.

Tree restored afterwards (md5 and a full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.

**@⁠dcastell1** on GitHub · 2026-11-24 08:16 UTC · [original](https://github.com/xcl-dev/xcl/pull/909#issuecomment-7125944562) Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on clean `main@beaa245` (713 passed, 0 failed, 41 ignored on this box): ``` patch -p1 < this PR # clean, no fuzz cargo test --workspace -- --test-threads=4 715 passed; 0 failed; 41 ignored (exit 0) ``` Two more passing tests than main, so `v_qsad_pk_u16_u8` and `v_mqsad_{pk_u16,u32}_u8` translate and their fixtures run green here; nothing else changes verdict. Tree restored afterwards (md5 and a full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.
Author
Owner

@⁠SlopTart on GitHub · 2026-11-24 09:34 UTC · original

Linux / AMD RDNA3 datapoint.

Merged state: head 4ed088d onto main 09940bb, clean merge.

Full suite, cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 711 passed, 7 failed, 41 ignored; failures are the known Wine-lane set from my #42 rows. qsad pack recompile tests green on RDNA3 wave64.

Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. My Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

**@⁠SlopTart** on GitHub · 2026-11-24 09:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/909#issuecomment-7127605245) Linux / AMD RDNA3 datapoint. **Merged state:** head `4ed088d` onto main `09940bb`, clean merge. **Full suite, `cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4`:** **711 passed, 7 failed, 41 ignored**; failures are the known Wine-lane set from my #42 rows. qsad pack recompile tests green on RDNA3 wave64. **Not tested:** retail titles (none here), Windows, RDNA4. Counts only, no game data. **Machine:** RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross `x86_64-pc-windows-gnu`, `XCL_NETWORK=0`, release profile, `--test-threads=4`. My Linux rows: #42 / #718. AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.
Author
Owner

@⁠4usphs on GitHub · 2026-11-24 09:39 UTC · original

Measured on RDNA2 hardware: the three quad-SAD instructions behave as this translation models them, including the zero-byte mask on S1 and CLAMP.

Method. The Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, RDNA2), Windows 11 Pro 26200, display driver 32.0.21045.29003, AMD OpenCL 3652.0 (PAL,LC). Each instruction runs as inline RDNA2 assembly in an OpenCL C kernel (the #518 / #772 method), VOP3 with a 64-bit S0, a 32-bit S1 and a 64-bit S2 (128-bit for v_mqsad_u32_u8, uint4 operands), with and without clamp. The comparison model is a Python port of valu_qsad with sad_u8 / msad_u8 / pk_sad_u16 / sat_u32: 4 windows of S0 at byte offsets 0..3, S1's zero bytes skipped for mqsad, per-element wrap or saturation.

Inputs. This PR's GPU-test operands (S0 = 0xFF605000_40302010, S1 = 0x40002015, the three accumulators, and -1 / 0x00FF0001), plus 2 000 random cases: S1 bytes zero 30 % of the time, and accumulators near 2^16 / 2^32 for the wrap and clamp edges. Each set runs 3 times (wave32, wave64, wave32) with identical output.

Instruction no clamp clamp
v_qsad_pk_u16_u8 2004 / 2004 2004 / 2004
v_mqsad_pk_u16_u8 2004 / 2004 2004 / 2004
v_mqsad_u32_u8 2004 / 2004 2004 / 2004

On the hardware, the cases in gpu_qsad_and_mqsad give exactly the values the test asserts. These are v8:v9 = 0x009D0036, 0x00BF004E; v12:v13 = 0x009C0033, 0x01BB002B; the clamp forms 0x009CFFFF, 0x01BBFFFF and 0x005CFFFF, 0x016BFFFF; v16:v17 = 0x005D0006, 0x006F004E; v20:v21 = 0x010000FF, 0x01020101; and v_mqsad_u32_u8 = 1, 0xFFFFFFFB, 74, 112 wrapped and 0xFFFFFFFF, 0xFFFFFFFB, 0xFFFFFFFF, 112 clamped. So the expected values are the console generation's, not only the ISA text's.

For comparison, an RX 9070 XT (gfx1201, RDNA4, OpenCL 3686.0) gives the same results in every case.

Not tested: the console itself (gfx1036 is a desktop RDNA2 APU of the same generation), RDNA1, overlapping VDST / source ranges and EXEC masking on the hardware (the compiler picks the registers in this method), this PR's D3D12 tests on this GPU, smoke or a title.

AI use: Claude Code (Claude Opus 5.5) wrote the OpenCL probes and the port of the model, ran them on my PC and wrote this comment. I have not reviewed the code.

**@⁠4usphs** on GitHub · 2026-11-24 09:39 UTC · [original](https://github.com/xcl-dev/xcl/pull/909#issuecomment-7127705476) Measured on RDNA2 hardware: the three quad-SAD instructions behave as this translation models them, including the zero-byte mask on S1 and `CLAMP`. **Method.** The Radeon iGPU of a Ryzen 7 9800X3D (`gfx1036`, RDNA2), Windows 11 Pro 26200, display driver 32.0.21045.29003, AMD OpenCL `3652.0 (PAL,LC)`. Each instruction runs as inline RDNA2 assembly in an OpenCL C kernel (the #518 / #772 method), VOP3 with a 64-bit S0, a 32-bit S1 and a 64-bit S2 (128-bit for `v_mqsad_u32_u8`, `uint4` operands), with and without `clamp`. The comparison model is a Python port of `valu_qsad` with `sad_u8` / `msad_u8` / `pk_sad_u16` / `sat_u32`: 4 windows of S0 at byte offsets 0..3, S1's zero bytes skipped for `mqsad`, per-element wrap or saturation. **Inputs.** This PR's GPU-test operands (`S0 = 0xFF605000_40302010`, `S1 = 0x40002015`, the three accumulators, and `-1` / `0x00FF0001`), plus 2 000 random cases: S1 bytes zero 30 % of the time, and accumulators near 2^16 / 2^32 for the wrap and clamp edges. Each set runs 3 times (wave32, wave64, wave32) with identical output. | Instruction | no clamp | clamp | |---|---:|---:| | `v_qsad_pk_u16_u8` | 2004 / 2004 | 2004 / 2004 | | `v_mqsad_pk_u16_u8` | 2004 / 2004 | 2004 / 2004 | | `v_mqsad_u32_u8` | 2004 / 2004 | 2004 / 2004 | On the hardware, the cases in `gpu_qsad_and_mqsad` give exactly the values the test asserts. These are `v8:v9` = `0x009D0036, 0x00BF004E`; `v12:v13` = `0x009C0033, 0x01BB002B`; the clamp forms `0x009CFFFF, 0x01BBFFFF` and `0x005CFFFF, 0x016BFFFF`; `v16:v17` = `0x005D0006, 0x006F004E`; `v20:v21` = `0x010000FF, 0x01020101`; and `v_mqsad_u32_u8` = `1, 0xFFFFFFFB, 74, 112` wrapped and `0xFFFFFFFF, 0xFFFFFFFB, 0xFFFFFFFF, 112` clamped. So the expected values are the console generation's, not only the ISA text's. For comparison, an RX 9070 XT (`gfx1201`, RDNA4, OpenCL `3686.0`) gives the same results in every case. **Not tested:** the console itself (`gfx1036` is a desktop RDNA2 APU of the same generation), RDNA1, overlapping VDST / source ranges and EXEC masking on the hardware (the compiler picks the registers in this method), this PR's D3D12 tests on this GPU, smoke or a title. **AI use:** Claude Code (Claude Opus 5.5) wrote the OpenCL probes and the port of the model, ran them on my PC and wrote this comment. I have not reviewed the code.
Author
Owner

@⁠NWProgramming00 on GitHub · 2026-11-25 14:58 UTC · original

Continued in #2334. It keeps this PR's commit unchanged, with its authorship, and adds the direct DXIL lowering, a check of both backends against RDNA2 hardware, and the regression run.

**@⁠NWProgramming00** on GitHub · 2026-11-25 14:58 UTC · [original](https://github.com/xcl-dev/xcl/pull/909#issuecomment-7166096760) Continued in #2334. It keeps this PR's commit unchanged, with its authorship, and adds the direct DXIL lowering, a check of both backends against RDNA2 hardware, and the regression run.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.