shader: lower v_cvt_pknorm_i16_f16 and v_cvt_pknorm_u16_f16 #80

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

@⁠lcg007xd-c on GitHub · 2026-11-25 22:03 UTC · original
lcg007xd-c:shader-pknorm-f16 → main · +90 −3 · 2 files · 1 commits · open

What is broken

v_cvt_pknorm_i16_f16 (VOP3 0x312) and v_cvt_pknorm_u16_f16 (VOP3 0x313) decode, but a program using either fails to translate with v_cvt_pknorm_i16_f16 is not supported (or the u16 name). I found them by translating a one-instruction program for every VOP3 opcode the decoder names. Mesa's ACO selects exactly these two for NIR's pack_snorm_2x16/pack_unorm_2x16 on 16-bit sources (aco_select_nir_alu.cpp), so they appear in normal compiler output for packing two halves into an SNORM16/UNORM16 pair.

Semantics

AMD's RDNA2 ISA (§12.12, opcodes 786 and 787) gives D = {(snorm)S1.f16, (snorm)S0.f16} and the same with unorm: two f16 values converted to normalized 16-bit integers, src0 in the low half.

The change (emit/lower.rs)

  • Reuses the existing conversion. Each half goes through the conversion v_cvt_norm_i16_f16/v_cvt_norm_u16_f16 already use (cvt_norm16): clamped to [−1, 1] or [0, 1], scaled by 32767 or 65535, rounded to nearest even once from the exact product, NaN → 0. No new arithmetic.
  • cvt_norm16_of(inst, src, signed): the conversion now takes which source to read, so each source's op_sel half, abs and neg apply. cvt_norm16 calls it with source 0, so the existing instructions are unchanged.

Tests

  • emit::lower::tests::f16_norm_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_pknorm_i16_f16: GFX10 code at 0x24: v_cvt_pknorm_i16_f16 is not supported.
  • valu_tests::v_cvt_pknorm_i16_and_u16_f16_pack_two_norm_conversions (Windows/GPU): about 140 pairs of f16 patterns per opcode, checked against the existing cvt_norm16_reference. They include ±1, 0.5 (a tie), ±inf, NaN and the smallest denormal. The sources carry junk high halves.
  • CPU run: I ran this on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I used an independent f64 reference over every 331st f16 pattern plus edges, an op_sel case (both sources from the high halves), and neg on src1. 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 f32 forms (v_cvt_pknorm_*_f32, 0x368/0x369) are left for a separate change. Their rounding (an exact product, or Mesa's f32-product definition) needs its own decision.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:03 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/80) `lcg007xd-c:shader-pknorm-f16` → `main` · +90 −3 · 2 files · 1 commits · open ## What is broken `v_cvt_pknorm_i16_f16` (VOP3 **0x312**) and `v_cvt_pknorm_u16_f16` (VOP3 **0x313**) decode, but a program using either fails to translate with `v_cvt_pknorm_i16_f16 is not supported` (or the `u16` name). I found them by translating a one-instruction program for every VOP3 opcode the decoder names. Mesa's ACO selects exactly these two for NIR's `pack_snorm_2x16`/`pack_unorm_2x16` on 16-bit sources (`aco_select_nir_alu.cpp`), so they appear in normal compiler output for packing two halves into an SNORM16/UNORM16 pair. ## Semantics AMD's RDNA2 ISA (§12.12, opcodes 786 and 787) gives `D = {(snorm)S1.f16, (snorm)S0.f16}` and the same with `unorm`: two f16 values converted to normalized 16-bit integers, src0 in the low half. ## The change (`emit/lower.rs`) - **Reuses the existing conversion.** Each half goes through the conversion `v_cvt_norm_i16_f16`/`v_cvt_norm_u16_f16` already use (`cvt_norm16`): clamped to [−1, 1] or [0, 1], scaled by 32767 or 65535, rounded to nearest even once from the exact product, NaN → 0. No new arithmetic. - **`cvt_norm16_of(inst, src, signed)`:** the conversion now takes which source to read, so each source's `op_sel` half, `abs` and `neg` apply. `cvt_norm16` calls it with source 0, so the existing instructions are unchanged. ## Tests - **`emit::lower::tests::f16_norm_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_pknorm_i16_f16: GFX10 code at 0x24: v_cvt_pknorm_i16_f16 is not supported`. - **`valu_tests::v_cvt_pknorm_i16_and_u16_f16_pack_two_norm_conversions`** (Windows/GPU): about 140 pairs of f16 patterns per opcode, checked against the existing `cvt_norm16_reference`. They include ±1, 0.5 (a tie), ±inf, NaN and the smallest denormal. The sources carry junk high halves. - **CPU run:** I ran this on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I used an independent f64 reference over every 331st f16 pattern plus edges, an `op_sel` case (both sources from the high halves), and `neg` on src1. 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 f32 forms (`v_cvt_pknorm_*_f32`, 0x368/0x369) are left for a separate change. Their rounding (an exact product, or Mesa's f32-product definition) needs its own decision. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP3 0x312 and 0x313 decoded but failed to translate
("v_cvt_pknorm_i16_f16 is not supported"). Each packs two f16 values
converted to normalized 16-bit integers (src0 low, src1 high; AMD's
RDNA2 ISA), with the conversion v_cvt_norm_i16_f16/v_cvt_norm_u16_f16
already use here; that helper now takes the source to read, so op_sel,
abs and neg apply per source. Mesa's ACO selects these two for NIR's
pack_snorm_2x16/pack_unorm_2x16 on 16-bit sources.

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:26 +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/80/head:github-bot/gh-80'
git switch 'github-bot/gh-80'

Merge

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