Repository navigation
shader: lower v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 in the direct C++ compute path - #2996
Conversation
3e7de7a to
e08d951
Compare
… compute path The direct compute frontend refused the two saturating 16-bit packs. They now follow gfx2hlsl.py's VOP3 table: v_cvt_pk_u16_u32 packs min(S0, 0xffff) and min(S1, 0xffff), v_cvt_pk_i16_i32 the signed sources clamped to [-32768, 32767], S0 in the low half. A nonzero unused SRC2 field and op_sel are refused. tests/native_cvt_pk16_check.py converts eight operand pairs across both bounds of both forms in a dispatch against a CPU reference; its controls refuse the SRC2 and op_sel forms and check that truncating and unsigned-bound references differ. Refs R0Y00#2995. AI-assisted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
e08d951 to
78ef2da
Compare
KelboLive
left a comment
There was a problem hiding this comment.
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 correctmin(S, 0xFFFF)for unsigned 32-bit values. - Signed saturation: The two-step approach (
select(slt(x, -32768), -32768, x)thenand(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] != 0matches thegfx2hlsl.pyrefusal for non-zero unused SRC2 field. - op_sel check:
i.a & 0x7800catches bits 11-15 of the VOP3 operand register — correct for op_sel field detection.
⚠️ Minor observations
-
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. -
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.
-
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
left a comment
There was a problem hiding this comment.
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.
Change
The direct compute frontend now lowers
v_cvt_pk_u16_u32andv_cvt_pk_i16_i32. Before, it refused them withdirect DXIL lowering for <name>. Refs #2995, part of the integer-ALU coverage in #2935.Following the
gfx2hlsl.pyVOP3 table, each saturates S0 and S1 to 16 bits and packs S0 into the low half:v_cvt_pk_u16_u32v_cvt_pk_i16_i32Refused by name:
gfx2hlsl.py;As
docs/experiments.mdnotes for the Python path, the RDNA 2 ISA text does not defineu32_to_u16/i32_to_i16. The saturation follows Mesa and the existing translators and is not measured on the console.Verification
Commit
3e7de7a4oncpp-migration4ff5e36d, Windows 10.0.26100.9711,devpreset. 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_controlspassed. 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_hardwareskipped (77). It needs shader model 6.6 with wave32, so the readback is not measured here.gfx2hlsl.pytable 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_movliterals 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 isdocs/experiments/native-direct-cvt-pk16.md.Checklist
AI-assisted.
🤖 Generated with Claude Code