shader: lower v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 (saturating) #78

Open
github-bot wants to merge 1 commit from github-bot/gh-78 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:00 UTC · original
lcg007xd-c:shader-cvt-pk-16 → main · +93 −0 · 2 files · 1 commits · open

What is broken

v_cvt_pk_u16_u32 (VOP3 0x36A) and v_cvt_pk_i16_i32 (VOP3 0x36B) decode, but a program using either fails to translate with v_cvt_pk_u16_u32 is not supported (or v_cvt_pk_i16_i32 …). I found them by translating a one-instruction program for every VOP3 opcode the decoder names.

Semantics

  • AMD's RDNA2 ISA (§12.12, opcodes 874 and 875): D.u16_lo = u32_to_u16(S0.u32); D.u16_hi = u32_to_u16(S1.u32), and the same with i32_to_i16.
  • The ISA does not spell out the conversion. Mesa's ACO compiler for AMD does, by how it uses them:
    • It selects these two for NIR's pack_uint_2x16/pack_sint_2x16 (aco_select_nir_alu.cpp). nir_opcodes.py defines those as _mesa_unsigned_to_unsigned(x, 16) / _mesa_signed_to_signed(x, 16), described as "Convert two unsigned integers into a packed unsigned short (clamp is applied)".
    • In nir_op_ldexp, ACO uses v_cvt_pk_i16_i32 … , 0 to "Convert the exponent to 16bit int with saturation" before v_ldexp_f16.
  • So each half saturates: min(x, 0xFFFF) unsigned, clamp(x, −32768, 32767) signed.

The change (emit/lower.rs, in integer)

Each source is clamped to its 16-bit range and masked. Then src0 goes to the low half and src1 to the high half.

Tests

  • emit::lower::tests::saturating_16_bit_packs_translate (every host): both encodings decode to their names, and a program that stores the result translates. Without the lower.rs change it fails: v_cvt_pk_u16_u32: GFX10 code at 0x24: v_cvt_pk_u16_u32 is not supported.
  • valu_tests::v_cvt_pk_u16_u32_and_i16_i32_saturate_each_half (Windows/GPU): four hand-computed cases each. They cover in-range values, 0x10000/0xFFFFFFFF/0x80000000 saturating, and −1, 32768, −32768, i32::MIN, i32::MAX and a small negative for the signed form.
  • CPU run: I ran them on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared all 169 pairs of 13 edge values against Rust's min/clamp. All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.

Not run here: the GPU case, since I have no Windows machine. The saturation comes from Mesa's definition and use, not from a run on RDNA2 hardware.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:00 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/78) `lcg007xd-c:shader-cvt-pk-16` → `main` · +93 −0 · 2 files · 1 commits · open ## What is broken `v_cvt_pk_u16_u32` (VOP3 **0x36A**) and `v_cvt_pk_i16_i32` (VOP3 **0x36B**) decode, but a program using either fails to translate with `v_cvt_pk_u16_u32 is not supported` (or `v_cvt_pk_i16_i32 …`). I found them by translating a one-instruction program for every VOP3 opcode the decoder names. ## Semantics - **AMD's RDNA2 ISA** (§12.12, opcodes 874 and 875): `D.u16_lo = u32_to_u16(S0.u32); D.u16_hi = u32_to_u16(S1.u32)`, and the same with `i32_to_i16`. - **The ISA does not spell out the conversion.** Mesa's ACO compiler for AMD does, by how it uses them: - It selects these two for NIR's `pack_uint_2x16`/`pack_sint_2x16` (`aco_select_nir_alu.cpp`). `nir_opcodes.py` defines those as `_mesa_unsigned_to_unsigned(x, 16)` / `_mesa_signed_to_signed(x, 16)`, described as *"Convert two unsigned integers into a packed unsigned short (clamp is applied)"*. - In `nir_op_ldexp`, ACO uses `v_cvt_pk_i16_i32 … , 0` to *"Convert the exponent to 16bit int with saturation"* before `v_ldexp_f16`. - **So each half saturates:** `min(x, 0xFFFF)` unsigned, `clamp(x, −32768, 32767)` signed. ## The change (`emit/lower.rs`, in `integer`) Each source is clamped to its 16-bit range and masked. Then src0 goes to the low half and src1 to the high half. ## Tests - **`emit::lower::tests::saturating_16_bit_packs_translate`** (every host): both encodings decode to their names, and a program that **stores** the result translates. Without the `lower.rs` change it fails: `v_cvt_pk_u16_u32: GFX10 code at 0x24: v_cvt_pk_u16_u32 is not supported`. - **`valu_tests::v_cvt_pk_u16_u32_and_i16_i32_saturate_each_half`** (Windows/GPU): four hand-computed cases each. They cover in-range values, `0x10000`/`0xFFFFFFFF`/`0x80000000` saturating, and −1, 32768, −32768, `i32::MIN`, `i32::MAX` and a small negative for the signed form. - **CPU run:** I ran them on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared all 169 pairs of 13 edge values against Rust's `min`/`clamp`. All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. **Not run here:** the GPU case, since I have no Windows machine. The saturation comes from Mesa's definition and use, not from a run on RDNA2 hardware. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP3 0x36A and 0x36B decoded but failed to translate ("v_cvt_pk_u16_u32
is not supported"). Each packs two 32-bit integers into 16-bit halves
(src0 low, src1 high; AMD's RDNA2 ISA), saturating each to the 16-bit
range: Mesa's ACO selects them for NIR's pack_uint_2x16/pack_sint_2x16,
defined as "clamp is applied", and uses v_cvt_pk_i16_i32 to saturate an
ldexp exponent to 16 bits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:21 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/78/head:github-bot/gh-78'
git switch 'github-bot/gh-78'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-78'
git switch 'github-bot/gh-78'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-78'
git switch 'github-bot/gh-78'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-78'
git switch 'main'
git merge --squash 'github-bot/gh-78'
git switch 'main'
git merge --ff-only 'github-bot/gh-78'
git switch 'main'
git merge 'github-bot/gh-78'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.