Skip to content

rdna2spirv: fix v_fma_mix* f16 literals and replace remaining PackHalf2x16 sites - #711

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
eIBart0:fix-f16-mix-interp-rte
Nov 22, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
eIBart0:fix-f16-mix-interp-rte

Conversation

@eIBart0

@eIBart0 eIBart0 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Rebased onto origin/main (762893f, after #641 merged f16_bits for v_interp_p2_f16 and v_fma_mix*_f16 output packing). This PR fixes binary16 inline literal decoding in v_fma_mix* and replaces the remaining PackHalf2x16 sites in tools/rdna2spirv.py so zero PackHalf2x16 instructions remain emitted:

  1. v_fma_mix_f32, v_fma_mixlo_f16, and v_fma_mixhi_f16 source decoding (op_sel_hi=1):
    • Extracted shared fma_mix_srcs(a, mods) using self.f16_in(src, sb[i]) when hb[i] == 1 and self.src_f(src) otherwise. Previously, self.src_u(...) encoded fimm inline float constants (such as 1.5, -2.0) as 32-bit float bit patterns instead of binary16 via self.src_h(...).
    • Simplified v_fma_mixlo_f16 / v_fma_mixhi_f16 output packing to self.f16_out(a[0], r, hi=(op == "v_fma_mixhi_f16")), reusing f16_bits(r) while preserving the unwritten half of the destination VGPR.
  2. Remaining PackHalf2x16 sites (encode_format, buffer_load_format d16, and image_sample_c_lz d16):
    • encode_format() (kind == "f16"): replaced PackHalf2x16 with self.f16_bits(self.tof(fcomps[c])).
    • buffer_load_format() (d16 path): replaced PackHalf2x16 with self.f16_pack2(self.tof(lo), self.tof(hi)).
    • image_sample (kind == "_c_lz" with d16): replaced PackHalf2x16 with self.f16_bits(d).

Commits

  1. rdna2spirv: fix v_fma_mix* f16 literals and replace remaining PackHalf2x16 sites
  2. tests/test_translate.py: test v_fma_mix* fimm literals and remaining f16_bits pack sites

Tests & Verification

Added 16 synthetic test cases at the end of tests/test_translate.py (translated via rdna2spirv.translate, validated with spirv-val --target-env vulkan1.2, and executed via spirv_mini_interp):

  • test_fma_mix_decodes_fimm_as_binary16_and_preserves_other_half: verifies 1.5 and -2.0 fimm operands on v_fma_mixlo_f16, v_fma_mixhi_f16, and v_fma_mix_f32 (op_sel_hi:[1,0,0] / [1,1,0]), destination half preservation, spirv-val, and no PackHalf2x16.
  • test_fma_mix_f16_rte_edge_cases_and_half_preservation (12 cases): verifies ties-to-even, subnormals and subnormal ties, +-Inf overflow, -0.0, and quiet NaN on both v_fma_mixlo_f16 and v_fma_mixhi_f16 with sentinel half preservation.
  • test_encode_format_f16_uses_f16_bits_rte (xy format 29 and x format 13): verifies RTE tie-to-even stores, spirv-val, and no PackHalf2x16.
  • test_remaining_d16_paths_avoid_packhalf2x16: verifies buffer_load_format_d16_xy and image_sample_c_lz d16 emit no PackHalf2x16 and pass spirv-val.

Observed locally against wt_f16mix (Windows, Python 3.13, spirv-as/spirv-val Vulkan SDK 1.4.341.1, straight-line synthetic listings without llvm-mc):

AI-assisted

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested f93afcad40fb2545e14c7d92615c104896a1b209 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

I translated a synthetic fragment listing with binary16-selected float literals and ran the resulting Vulkan shader on the RTX 5090. Both shader modules passed spirv-val. Every pixel of the 64×64 raw RGBA32F readback matched the same four expected words:

  • mixlo: 1.5 × 2 + 0.25 = 3.25, packed as 0xbeef4280 with the high sentinel intact.
  • mixhi: −2 × 1.5 + 1 = −2, packed as 0xc000cafe with the low sentinel intact.
  • mix_f32: −2 × 1.5 + 0.25 and 2 × 2 + 0.25 returned 0xc0300000 and 0x40880000.

This was a direct translator-listing test (4,096/4,096 pixels exact). It covers the three mixed-FMA forms and destination-half preservation. Buffer and image D16 conversion paths were outside this draw.

AI-assisted verification.

@kapitche

Copy link
Copy Markdown
Contributor

Tested f93afca on Windows 11, Python 3.12 (conda-forge), llvm-mc 23.1.6 (AMDGPU), spirv-tools from conda-forge, CPU only: pass

@gabrielsteglich

Copy link
Copy Markdown
Contributor

Independent RDNA2 round-trip and GPU test — RTX 3090

Tested c53e06bd121d74511cfc7a7fabed58a08efb6684 against base 762893fd6056c08c174a66479aed3a51739eeaa3 on Windows 11 Pro (build 26200), NVIDIA GeForce RTX 3090 (driver 621.01, Vulkan 1.4.357), Python 3.13.7, native AMDGPU llvm-mc 23.1.5, and Vulkan SDK 1.4.363.0. I used the same MSVC 19.44 vk_compute_test binary for both revisions. The runtime sources are identical.

tests/test_translate.py passed 229/229 on the PR head.

I assembled and disassembled two hand-written gfx1013 compute shaders with llvm-mc before translating them with each revision. All four SPIR-V outputs passed spirv-val and were run on the RTX 3090.

GPU check Base 762893f PR c53e06b
Mixed FMA with f16-selected literals 32/128 words matched 128/128 matched
Typed f16x2 store and buffer_load_format_d16_xy 2/2 matched 2/2 matched, with no PackHalf2x16 emitted

The FMA probe used 32 lanes and checked both destination-half writes and two f32 results. For each lane the base returned beef4280 3c00cafe 3e800000 3e800000. The PR returned the expected beef4280 c000cafe c0300000 40880000. In the assembled input, 1.5 becomes the binary16 literal 0x3e00, while -2.0 and 2.0 remain inline constants.

Both D16 buffer results were 0x3c023c00 on each revision, confirming the round-to-nearest-even midpoint cases. Image D16 coverage was limited to translation and SPIR-V validation in the test suite.

AI-assisted verification using synthetic shaders only. No game files were used.

@Pierre-Kookis

Copy link
Copy Markdown

GPU check of #711 on AMD and NVIDIA. The GPU runs are at f93afca. The new head c53e06b only changes tests/test_translate.py, and pytest tests/test_translate.py there gives 229 passed. The AMD Windows driver is the one where PackHalf2x16 rounding showed up as 1 ULP low (see #575).

Setup: Windows 11. AMD Radeon Graphics (Ryzen 7 7800X3D iGPU, RDNA2), driver 32.0.21043.5001, subgroup 32..64. RTX 4070 Ti SUPER, driver 610.88, subgroup 32. Test programs from main 67204bf (llvm-mingw clang), llvm-mc 23.1.6.

run_gpu_tests.py valu_fuzz.py --family f16, seed 1
AMD native 86/91, 5 skipped (mesh) 100/100 (--wave both)
NVIDIA, no emulation 64/91, 27 skipped (wave64) 50/50 (--wave 32)
NVIDIA LEONIDA_W64_EMULATE=1 91/91 100/100 (--emulate)

No failures, the same counts as main.

Code read (a second model, read-only): no emitted PackHalf2x16 is left, only mentions in comments. Rounding, special values and the preserved register halves look right. One smaller remark: the d16 tests around tests/test_translate.py:970 and :983 check that PackHalf2x16 is gone, but they don't assert output values, so a wrong rounding or swapped packed halves would still pass them.

(AI-assisted run, counts only.)

@dougmb

dougmb commented Nov 22, 2026

Copy link
Copy Markdown

Tested c53e06b (merged with main 67204bf, no conflicts) on NVIDIA GeForce RTX 3060, driver 620.73.04, Linux (llvm-mc 23.1.5): pass.

  • scripts/run_gpu_tests.py: 64/91 passed, 27 skipped, 0 failed, the same as main.
  • tests/semantic: OK.
  • Not run: tests/test_translate.py, because pytest is not installed on this machine.

AI-assisted (Claude Code).

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.

7 participants