Skip to content

feat(rdna2spirv): implement v_cvt_pknorm_i16_f16 and v_cvt_pknorm_u16_f16 - #3020

Closed
klarnet2009 wants to merge 2 commits into
ItsNotRexed:mainfrom
klarnet2009:feat/rdna2spirv-cvt-pknorm-f16
Closed

klarnet2009 wants to merge 2 commits into
ItsNotRexed:mainfrom
klarnet2009:feat/rdna2spirv-cvt-pknorm-f16

Conversation

@klarnet2009

Copy link
Copy Markdown
Contributor

What and why

Implements v_cvt_pknorm_i16_f16 and v_cvt_pknorm_u16_f16 in tools/rdna2spirv.py:

  • Reads both f16 sources via self.f16_in(src, sel) (widening f16 -> f32 and applying source abs/neg modifiers) with VOP3 op_sel:[s0,s1,0], refusing clamp, _dpp/_sdwa, other modifiers, or op_sel[2] != 0 with Unsupported.
  • Runs the same conversion pipeline as v_cvt_pknorm_*_f32 (OpIsNan -> %f0, FClamp to [-1, 1] or [0, 1], OpFMul by 32767.0/65535.0, RoundEven, OpConvertFToS/OpConvertFToU, pack {hi16, lo16} into a[0]).
  • Documents both ops in docs/design.md (explicitly noting CPU-checked, not yet hardware-measured on RDNA2) and adds synthetic translator-to-interpreter tests in tests/semantic/test_alu_cvt_pknorm_f16.py (6 tests).

Claim: #2891 (comment)

Tested

  • OS: Windows (Python 3.12.0), GPU: n/a (CPU translator + spirv_mini_interp.py)
  • py -3.12 -I -m compileall -q tools/rdna2spirv.py tests/semantic/test_alu_cvt_pknorm_f16.py: passed (exit 0)
  • py -3.12 -I tests/semantic/test_alu_cvt_pknorm_f16.py -v: Ran 6 tests in 2.076s, OK
  • py -3.12 -I tests/semantic/test_alu_cvt_pknorm_f32.py -v: Ran 4 tests in 2.147s, OK
  • py -3.12 -I tests/test_isa_coverage.py -v: Ran 13 tests in 3.305s, OK (skipped=3 for LEONIDA_RDNA2_ISA_XML)
  • py -3.12 -I tests/test_ctest_registration.py -v: Ran 4 tests in 0.050s, OK
  • Deliberate-breakage check: reverted tools/rdna2spirv.py to HEAD and confirmed test_alu_cvt_pknorm_f16.py failed (Unsupported, 337 errors), then restored and re-ran 6/6 passing.

Checklist

  • No game data / keys / decryption
  • Standard library only (python -I compatible)
  • AI-assisted: yes

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

heads up, this overlaps my #1976, which also adds v_cvt_pknorm_{i16,u16}_f16 (plus v_cvt_norm_*_f16). that PR also handles the op_sel dst bit (op_sel:[x,y,1] shifts the packed result left by 16, which Vonsek measured on gfx103x). so whichever lands second needs to drop its copy. i'm fine either way, yours has the nicer dedicated test file :)

on the code itself:

  • ran test_alu_cvt_pknorm_f16.py (6 ok) and test_alu_cvt_pknorm_f32.py (ok) locally
  • rejecting op_sel[2]=1 is the safe choice here, just know it's measurable behaviour now, not unknown
  • small doc thing: for f16 inputs x * 32767 in f32 isn't always exact (11-bit mantissa × 15-bit scale = up to 26 bits), so the "one off near ties" caveat from the f32 row applies to the f16 ops too. the new row reads like it's only an f32 thing
  • nit: the test rolls its own STUB_DXBC + temp dir, support.translate_lines does the same thing

AI-assisted (Claude Code).

TL;DR: code looks right and tests pass, but it duplicates part of #1976, so we need to pick one. small doc caveat about f16 rounding.

@TieruYT

TieruYT commented Nov 22, 2026

Copy link
Copy Markdown

Verified the translated Vulkan output on an RTX 4070 Ti (621.24, Windows 11) at 3a794d4bb5929354e1f02f4d3a29a83c1efb7bc3. I assembled eight synthetic pixel shaders with LLVM 21.1.8 for gfx1013: signed and unsigned packed conversion, each with all four source-half selections. The two input words held (+0.5, quiet NaN) and (-0.5, +infinity), low half first.

All eight generated fragment modules passed spirv-val. I rendered each through vk_harness built from this commit and checked the entire RGBA32F readback against a Python reference, with the two 16-bit results carried in separate channels to preserve the exact bits. 4,096/4,096 pixels matched in each draw, including lane placement, signed and unsigned half-way rounding, NaN to zero and positive infinity to the upper bound for these inputs.

The parent translator at 16282101 rejected all eight assembled listings as Unsupported. This run measures the translated shaders on NVIDIA; native RDNA2 instruction behavior is still an open comparison.

AI-assisted verification · GPT-6.2 Astra Medium

@KiryaAfimin

Copy link
Copy Markdown
Contributor

Tested 3a794d4b on Windows 11 with an RTX 4070 (driver 621.01). For both signed and unsigned packing, I swept all 65,536 f16 bit patterns in each source position across all four source-half selections, plus one abs/neg variant per instruction. All 10 translated compute shaders passed spirv-val for Vulkan 1.2. All 655,360 packed words matched a CPU reference that rounds the product to f32 before rounding to the nearest even integer.

I also checked the rounding detail raised above. For signed packing, f16 input 0x3801 gives an exact product of 16399.49951171875. The f32 multiply rounds that to 16399.5, and the GPU returns 16400 (0x4010). Rounding the exact product directly gives 16399. Across all f16 encodings, that distinction changed the final integer for four signed inputs and four unsigned inputs.

AI-assisted

@klarnet2009
klarnet2009 force-pushed the feat/rdna2spirv-cvt-pknorm-f16 branch from 3a794d4 to 2e087a6 Compare November 22, 2026 20:26
@klarnet2009

Copy link
Copy Markdown
Contributor Author

Thank you @IAmNotSiob for the review and the pointer to #1976!

Updated docs/design.md in 90dc173e to clarify that:

  • The f32 intermediate OpFMul + RoundEven near-tie rounding caveat (x * 32767 / x * 65535 up to 26–27 bits in f32) applies to v_cvt_pknorm_{i16,u16}_f16 as well as _f32 (with the same Fma tie-breaker and NaN → 0 behavior).
  • op_sel[2]=1 is currently refused with Unsupported (while measurable on gfx103x).

Re-verified (py -3.12 -I):

  • tests/semantic/test_alu_cvt_pknorm_f16.py -v: 6/6 OK (4.148s)
  • tests/semantic/test_alu_cvt_pknorm_f32.py -v: 5/5 OK (3.889s)
  • compileall -q tools/rdna2spirv.py tests/semantic/test_alu_cvt_pknorm_f16.py: exit 0

@klarnet2009

Copy link
Copy Markdown
Contributor Author

Superseded by 39d57fe on main — closing to keep the PR queue clean. Thanks @IAmNotSiob, @TieruYT, and @KiryaAfimin for the reviews and testing!

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.

4 participants