shader: lower v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 in the direct C++ compute path #2996

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/2996 into cpp-migration 2026-11-27 04:35:54 +00:00
legacy-code-archaeologist commented 2026-11-27 03:44:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T03:44:41Z.


Change

The direct compute frontend now lowers v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32. Before, it refused them with direct DXIL lowering for <name>. Refs #2995, part of the integer-ALU coverage in #2935.

Following the gfx2hlsl.py VOP3 table, each saturates S0 and S1 to 16 bits and packs S0 into the low half:

Instruction Saturation
v_cvt_pk_u16_u32 min(S, 0xffff) of the unsigned values
v_cvt_pk_i16_i32 the signed values clamped to [-32768, 32767]

Refused by name:

  • a nonzero unused SRC2 field, as in gfx2hlsl.py;
  • the op_sel bits;
  • source and output modifiers, as for the other integer VOP3 forms.

As docs/experiments.md notes for the Python path, the RDNA 2 ISA text does not define u32_to_u16/i32_to_i16. The saturation follows Mesa and the existing translators and is not measured on the console.

Verification

Commit 3e7de7a4 on cpp-migration 4ff5e36d, Windows 10.0.26100.9711, dev preset. GPU: NVIDIA Quadro M1000M, driver 32.0.15.7371, debug layer unavailable.

  • ctest --preset dev --no-tests=error -R '^native_cvt_pk16_|^native_direct_container$': 2 passed, 1 skipped.
    • native_cvt_pk16_controls passed. It compiles the program and refuses SRC2 = 3 and an op_sel bit. It also checks that the truncating and unsigned-bound references differ.
    • native_cvt_pk16_hardware skipped (77). It needs shader model 6.6 with wave32, so the readback is not measured here.
  • The CPU reference agrees with the gfx2hlsl.py table expressions, evaluated in Python, for all eight pairs. I checked this once.
  • python -B tests/docs_links_check.py: ok.

Evidence and follow-up

Eight operand pairs are set by v_mov literals and converted by both instructions. Each of 64 lanes stores the 16 results. The pairs include 0, 0xffff, 0x10000, 0xffffffff, -32768, -32769, 32767, 32768, 0x7fffffff and 0x80000000.

The VOP3 words were assembled with LLVM 15.0.7. I checked the whole program with tools/disasm_rdna2.py --hex. The note is docs/experiments/native-direct-cvt-pk16.md.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5520804525 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2996). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T03:44:41Z. --- ## Change The direct compute frontend now lowers `v_cvt_pk_u16_u32` and `v_cvt_pk_i16_i32`. Before, it refused them with `direct DXIL lowering for <name>`. Refs #2995, part of the integer-ALU coverage in #2935. Following the `gfx2hlsl.py` VOP3 table, each saturates S0 and S1 to 16 bits and packs S0 into the low half: | Instruction | Saturation | |---|---| | `v_cvt_pk_u16_u32` | min(S, 0xffff) of the unsigned values | | `v_cvt_pk_i16_i32` | the signed values clamped to [-32768, 32767] | Refused by name: - a nonzero unused SRC2 field, as in `gfx2hlsl.py`; - the op_sel bits; - source and output modifiers, as for the other integer VOP3 forms. As `docs/experiments.md` notes for the Python path, the RDNA 2 ISA text does not define `u32_to_u16`/`i32_to_i16`. The saturation follows Mesa and the existing translators and is not measured on the console. ## Verification Commit `3e7de7a4` on `cpp-migration` `4ff5e36d`, Windows 10.0.26100.9711, `dev` preset. GPU: NVIDIA Quadro M1000M, driver 32.0.15.7371, debug layer unavailable. - `ctest --preset dev --no-tests=error -R '^native_cvt_pk16_|^native_direct_container$'`: 2 passed, 1 skipped. - `native_cvt_pk16_controls` passed. It compiles the program and refuses SRC2 = 3 and an op_sel bit. It also checks that the truncating and unsigned-bound references differ. - **`native_cvt_pk16_hardware` skipped (77).** It needs shader model 6.6 with wave32, so **the readback is not measured here.** - The CPU reference agrees with the `gfx2hlsl.py` table expressions, evaluated in Python, for all eight pairs. I checked this once. - `python -B tests/docs_links_check.py`: ok. ## Evidence and follow-up Eight operand pairs are set by `v_mov` literals and converted by both instructions. Each of 64 lanes stores the 16 results. The pairs include 0, 0xffff, 0x10000, 0xffffffff, -32768, -32769, 32767, 32768, 0x7fffffff and 0x80000000. The VOP3 words were assembled with LLVM 15.0.7. I checked the whole program with `tools/disasm_rdna2.py --hex`. The note is `docs/experiments/native-direct-cvt-pk16.md`. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
legacy-code-archaeologist commented 2026-11-27 03:58:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T03:58:38Z.


Rebased onto cpp-migration e0d83759 after #2990 and #2994 merged; they insert at the same place in the integer-ALU chain, and both blocks are kept. The controls and native_direct_container pass again on the rebased head. The readback still skips here (77).

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7209419530 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2996#issuecomment-7209419530). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T03:58:38Z. --- Rebased onto `cpp-migration` `e0d83759` after #2990 and #2994 merged; they insert at the same place in the integer-ALU chain, and both blocks are kept. The controls and `native_direct_container` pass again on the rebased head. The readback still skips here (77). AI-assisted.
KelboLive commented 2026-11-27 04:15:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T04:15:10Z.

Review: COMMENTED


Clean implementation of the saturating pack instructions — correct semantics for both unsigned and signed forms, and the operand checks (SRC2, op_sel, modifiers) are consistent with gfx2hlsl.py.

Findings

✅ What works well

  • Unsigned saturation: select(ugt(x, "65535"), "65535", x) is the correct min(S, 0xFFFF) for unsigned 32-bit values.
  • Signed saturation: The two-step approach (select(slt(x, -32768), -32768, x) then and(select(sgt(low, 32767), 32767, low), 65535)) correctly clamps to [-32768, 32767]:
    • First step floors at -32768
    • Second step ceil-caps at 32767, then keeps the low 16 bits (which for positive ≤32767 is identity)
    • This handles the edge case where a value >32767 has bit 15 set and would wrap to negative without the and 0xFFFF.
  • Pack ordering: or(narrow(a), shl(narrow(b), 16)) packs S0 low, S1 high — correct per the VOP3 encoding.
  • SRC2 check: i.src[2] != 0 matches the gfx2hlsl.py refusal for non-zero unused SRC2 field.
  • op_sel check: i.a & 0x7800 catches bits 11-15 of the VOP3 operand register — correct for op_sel field detection.

⚠️ Minor observations

  1. Signed saturation simplification: The two-step approach works correctly but could be simplified conceptually to three comparisons (clamped = min(max(x, -32768), 32767)) if the IR builder supports it. The current approach is fine and avoids IR combinatorics — no action needed.

  2. Mesa reference: The PR notes that "the RDNA 2 ISA text does not define u32_to_u16/i32_to_i16" and that saturation follows Mesa. This is honest and useful context. One tiny addition: if the specific Mesa commit or source file could be cited in a comment, it would help maintainers trace the decision if behavior ever needs to change.

  3. Test operand pairs: The 8 pairs are well-chosen to cross both bounds of both forms. The (0xFFFF8000, 0xFFFF7FFF) pair tests the signed negative range. Good coverage.

Recommendation

Ready to land. Clean, well-tested, and consistent with the project's existing patterns.

🏷 None — this is a textbook example of the pattern established by previous PRs in this series.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083420825 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2996#pullrequestreview-6083420825). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-27T04:15:10Z. **Review:** COMMENTED --- Clean implementation of the saturating pack instructions — correct semantics for both unsigned and signed forms, and the operand checks (SRC2, op_sel, modifiers) are consistent with `gfx2hlsl.py`. ## Findings ### ✅ What works well - **Unsigned saturation**: `select(ugt(x, "65535"), "65535", x)` is the correct `min(S, 0xFFFF)` for unsigned 32-bit values. - **Signed saturation**: The two-step approach (`select(slt(x, -32768), -32768, x)` then `and(select(sgt(low, 32767), 32767, low), 65535)`) correctly clamps to [-32768, 32767]: - First step floors at -32768 - Second step ceil-caps at 32767, then keeps the low 16 bits (which for positive ≤32767 is identity) - This handles the edge case where a value >32767 has bit 15 set and would wrap to negative without the `and 0xFFFF`. - **Pack ordering**: `or(narrow(a), shl(narrow(b), 16))` packs S0 low, S1 high — correct per the VOP3 encoding. - **SRC2 check**: `i.src[2] != 0` matches the `gfx2hlsl.py` refusal for non-zero unused SRC2 field. - **op_sel check**: `i.a & 0x7800` catches bits 11-15 of the VOP3 operand register — correct for op_sel field detection. ### ⚠️ Minor observations 1. **Signed saturation simplification**: The two-step approach works correctly but could be simplified conceptually to three comparisons (`clamped = min(max(x, -32768), 32767)`) if the IR builder supports it. The current approach is fine and avoids IR combinatorics — no action needed. 2. **Mesa reference**: The PR notes that "the RDNA 2 ISA text does not define u32_to_u16/i32_to_i16" and that saturation follows Mesa. This is honest and useful context. One tiny addition: if the specific Mesa commit or source file could be cited in a comment, it would help maintainers trace the decision if behavior ever needs to change. 3. **Test operand pairs**: The 8 pairs are well-chosen to cross both bounds of both forms. The `(0xFFFF8000, 0xFFFF7FFF)` pair tests the signed negative range. Good coverage. ### Recommendation Ready to land. Clean, well-tested, and consistent with the project's existing patterns. 🏷 None — this is a textbook example of the pattern established by previous PRs in this series.
MasterDomino commented 2026-11-27 04:35:51 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-27T04:35:51Z.

Review: APPROVED


Review of head 78ef2da5. CI on the head is green. This is the wave32 SM 6.6 readback you asked for. Approving and merging.

Code read:

  • Unsigned: min(S, 0xffff).
  • Signed: a clamp to [-32768, 32767] and the low 16 bits.
  • Packing: S0 goes in the low half.
  • Refused by name: a nonzero SRC2 field and the op_sel bits (0x7800).

Run on AMD: Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), plus WARP, release. This was tested together with #2986, #2998, #3003 and #2975 merged onto cpp-migration e0d83759; all five merge cleanly with each other. ctest --preset release --no-tests=error -R '^(native_.*|startup_compute_dispatch(_.*)?_(warp|hardware)|startup_slot67_indirect_dispatch_warp|startup_compute_translate_e2e_warp|startup_stream_draw.*|startup_native_graphics.*)$' gives 94 tests, 90 passed and 4 skipped (77, WARP has no 32-lane waves). native_cvt_pk16_hardware and native_cvt_pk16_controls passed.

Mutation here: truncating the signed form to its low 16 bits instead of clamping fails native_cvt_pk16_hardware, for example (9, '0xffff0000', '0x7fff0000'). With it restored, it passes.

As you note, the saturation follows Mesa and the existing translators and is not measured on the console. I will close #2995 by hand.

AI-assisted; the commands above were run locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083535338 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2996#pullrequestreview-6083535338). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-27T04:35:51Z. **Review:** APPROVED --- Review of head `78ef2da5`. CI on the head is green. **This is the wave32 SM 6.6 readback you asked for.** Approving and merging. **Code read:** - **Unsigned:** `min(S, 0xffff)`. - **Signed:** a clamp to [-32768, 32767] and the low 16 bits. - **Packing:** S0 goes in the low half. - **Refused by name:** a nonzero SRC2 field and the op_sel bits (`0x7800`). **Run on AMD:** Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), plus WARP, release. This was tested together with #2986, #2998, #3003 and #2975 merged onto `cpp-migration` `e0d83759`; all five merge cleanly with each other. `ctest --preset release --no-tests=error -R '^(native_.*|startup_compute_dispatch(_.*)?_(warp|hardware)|startup_slot67_indirect_dispatch_warp|startup_compute_translate_e2e_warp|startup_stream_draw.*|startup_native_graphics.*)$'` gives 94 tests, 90 passed and 4 skipped (77, WARP has no 32-lane waves). `native_cvt_pk16_hardware` and `native_cvt_pk16_controls` **passed**. **Mutation here:** truncating the signed form to its low 16 bits instead of clamping fails `native_cvt_pk16_hardware`, for example `(9, '0xffff0000', '0x7fff0000')`. With it restored, it passes. As you note, the saturation follows Mesa and the existing translators and is not measured on the console. I will close #2995 by hand. AI-assisted; the commands above were run locally.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2996
No description provided.