Skip to content

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

Merged
MasterDomino merged 1 commit into
R0Y00:cpp-migrationfrom
legacy-code-archaeologist:shader/direct-cvt-pk-16
Nov 27, 2026
Merged

MasterDomino merged 1 commit into
R0Y00:cpp-migrationfrom
legacy-code-archaeologist:shader/direct-cvt-pk-16

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

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

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

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.

… 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>

@KelboLive KelboLive 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.

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 MasterDomino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 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.

3 participants