Skip to content

rdna2spirv: preserve f16 denormals, NaN payloads, and bit-exact clamp in conversions (SHADER-CVT-F16-DENORM-AND-MODS) - #2419

Merged
DadaCap merged 1 commit into
mainfrom
fix/cvt-f16-denorm-and-clamp
Nov 28, 2026
Merged

DadaCap merged 1 commit into
mainfrom
fix/cvt-f16-denorm-and-clamp

Conversation

@Andretuta

@Andretuta Andretuta commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: SHADER-CVT-F16-DENORM-AND-MODS (#1608 (comment))

Related: #2362 (independent — merges cleanly standalone on main; overlaps 4 lines in tools/rdna2spirv.py dpp_insn src0 neg/abs forwarding, keeping args[first] on rebase so destination-less v_cmpx stays intact)

What and why

  • Route v_cvt_f32_f16 through Gen.f16_in_bits instead of raw UnpackHalf2x16, preserving f16 subnormals, applying neg/abs to the half selected by SDWA or DPP (avoiding 32-bit modify on a 16-bit half), and preserving IEEE=0 NaN sign and payload bits out of driver float arithmetic.
  • Add Gen.clamp_f16_bits (3-instruction exact DX10 clamp on rounded f16 words: negative halves including -0 and NaNs map to +0, positive values cap at 1.0, positive subnormals are preserved bit-exactly) and reuse it in v_cvt_f16_f32, v_div_fixup_f16, v_frexp_mant_f16, and v_ldexp_f16.
  • Audited all 13 remaining UnpackHalf2x16 sites in tools/rdna2spirv.py.
  • Add CPU semantic suite tests/semantic/test_cvt_f16_denorm_mods.py and Vulkan GPU fixture runtime/test/shaders/cs_w32_cvt_f16_mods.asm (wired into runtime/test/shaders/check.py and scripts/run_gpu_tests.py), plus contract update in docs/design.md.

Tested

  • python -I tests/semantic/test_cvt_f16_denorm_mods.py: 13/13 OK (6 fail on unpatched main d1dff7949; validated with both spirv-as and spirv_asm.py + spirv-val --target-env vulkan1.2).
  • Full semantic suite: 501 modules, 6,385 passed, 3 skipped, 0 failed.
  • Vulkan GPU (scripts/run_gpu_tests.py, native and LEONIDA_W64_EMULATE=1): cs_w32_cvt_f16_mods before (d1dff7949): 7FFFFFFF 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001; after: FFC4A000 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001 (word 0 fixes v_cvt_f32_f16 DPP neg on NaN; exact 8/8 words in both modes); cs_w32_new_ops (8/8 exact) and ps_sdwa_f16_const (4,096/4,096 pixels exact) pass in both modes.
  • Corpus validation (aggregate counts only): 100/100 shaders containing both v_cvt_f32_f16 and v_cvt_f16_f32 translate and pass spirv-val (100/100 before and after).
  • python -I tests/test_run_gpu_tests.py (71/71 OK), doc/line-ending tests (25/25 OK), python -I scripts/check_no_game_data.py (exit 0).

GPUs tested: NVIDIA GeForce RTX 3060 12 GB, NVIDIA driver 617.14, Windows 10

AI-assisted: yes (Codex gpt-6.2-sol + Antigravity gemini-4-neon)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

… in conversions (SHADER-CVT-F16-DENORM-AND-MODS)

Route v_cvt_f32_f16 through f16_in_bits so SDWA/DPP half selection, subnormals, neg/abs,
and IEEE=0 NaN sign/payload bits are preserved without raw UnpackHalf2x16 flush risk,
and clamp v_cvt_f16_f32 and v_div_fixup_f16 directly on rounded f16 bits via shared
clamp_f16_bits. Verified on RTX 3060 (driver 617.14, native + --wave64-emulate: fixed
first word 0x7FFFFFFF -> 0xFFC4A000 on cs_w32_cvt_f16_mods), 13/13 new semantic tests,
501-module semantic suite (6,385 passed, 0 failed), and 100/100 real corpus shaders
passing spirv-val.

AI-assisted: Codex gpt-6.2-sol + Antigravity gemini-4-neon.
@ecox982745

Copy link
Copy Markdown

On a Windows 11 RTX 4090 (NVIDIA 616.64), I get 0 wrong output words out of 786,432 at df294dd8a1, down from 75,766 on base d1dff7949b, using the same synthetic pixel shaders and bitwise expectations.

  • Seven 256×256 draws each supplied all 65,536 binary16 encodings at runtime. Plain widening, negation, absolute value, negated absolute value, clamp, negated SDWA WORD_1, and negated DPP horizontal swap all passed. On base, five forms each missed all 2,046 NaN encodings. The negated DPP swap missed every input, including finite numbers. For example, widening and negating the donor half 0x0001 returned 0x7fffffff on base and the expected 0xb3800000 on the PR. Clamped widening passed on both.
  • Five draws tested clamped narrowing over 327,680 binary32 input words, including values around the minimum half subnormal, the normal boundary, and 1.0. Both revisions matched the expected rounded half and preserved the high 16 destination bits. To avoid framebuffer handling of NaNs, each shader split its result into two unsigned 16-bit pieces and exported each as an exactly representable float. The readback also checked which pixel or DPP donor supplied each input. All modules passed Vulkan 1.2 SPIR-V validation.

For the rebase with #2362, I tested its general neg/abs forwarding on top of this PR. Store the shuffled value and its two modifier flags in args[first] throughout: first is 0 for destination-less v_cmpx and 1 for the conversion. Keeping the older branch's literal args[1] produced 57 failing compare subcases locally. Using args[first] passed all 30 tests across test_cvt_f16_denorm_mods, test_dpp_f16_mods, and test_dpp_compare, and the combined translator passed the same 786,432-word GPU check. This was a local integration test of cf622ce9c2 and df294dd8a1.

@Andretuta

Copy link
Copy Markdown
Collaborator Author

Thanks @ecox982745 for the exhaustive RTX 4090 verification across all 65,536 binary16 encodings and 327,680 narrowing inputs (0 / 786,432 wrong words vs 75,766 on main), and for testing the integration with #2362!

Noted on args[first]: this branch already stores the shuffled operand and modifiers into args[first] (preserving first = 0 for destination-less v_cmpx). If #2362 lands first, I'll rebase keeping args[first] throughout so test_dpp_compare and test_dpp_f16_mods stay green; since #2419 merges cleanly standalone on main, I also updated the PR header note from Depends on: to Related: so it doesn't look blocked on #2362's rebase.

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

Approving. Checked on df294dd merged onto current main (d1dff79).

Translator semantic suite: 501 files, only main's known test_fspx_dot4_op_sel_disasm and test_int32_to_f64 failures. tests/test_translate.py: 721 passed, the one failure is main's ps_sdwa_f16_const.asm. GPU fixtures on llvmpipe under Wine: ps_outmods_f16, ps_fmac_f16_mods, ps_interp_mov_mods, ps_interp_f32_mods, the six med3 fixtures, cs_cvt_f32_sat, cs_cvt_pk_u8 and ps_cvt_off all pass. Against main's translator test_cvt_f16_denorm_mods fails 6 times.

Read the diff. clamp_f16_bits is right as an unsigned compare: everything above 0x7C00 (negatives, -0, positive NaNs) goes to +0, +inf caps to 0x3C00, positive subnormals survive, and it keeps clamp off the driver's float path, where an UnpackHalf2x16 could flush them. The DPP change is a real fix: before it, neg on a v_cvt_f32_f16_dpp source went through modify() on the 32-bit word, flipping bit 31 instead of the half's sign. On llvmpipe the fixture shows exactly that: against main, word 0 is 0x7FC4A000, with the PR it is 0xFFC4A000.

One suggestion: cs_w32_cvt_f16_mods is in WAVE32_LANE_DUMPS, so it skips on any device that can't give exactly 32 lanes (llvmpipe here: "can't run this shader at 32 lanes on request"). Its only cross-lane read is the quad_perm inside one quad, so it doesn't need 32. With that one line removed locally it runs at llvmpipe's 8 lanes and all eight words match CVT_F16_MODS_WORDS. Keeping it in WAVE32_DUMPS (wave32 translation) but out of the forced-size list would let it run on more devices. Not a blocker.

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

Verified head df294dd8a (vs parent d1dff7949) on Windows 11 with dual-GPU hardware readback (RTX 5080 616.64 + AMD Radeon gfx1036 32.0.21030.2001, Vulkan SDK 1.4.363.0 Core + SyncVal).

Static & semantic checks:

  • Bitwise f16 NaN payload/sign reconstruction, subnormal widening (mantissa * 2^-24), rounded-half clamp mapping, and DPP modifier forwarding via args[first] all check out.
  • tests/semantic/test_cvt_f16_denorm_mods.py — 13/13 OK.
  • tests/semantic/test_frexp_ldexp_f16_mods.py — 7/7 OK.

Dual-GPU hardware checks (Vulkan SDK 1.4.363.0 Core + SyncVal):

  • Checked-in fixtures (cs_w32_cvt_f16_mods, cs_w32_new_ops, ps_sdwa_f16_const): PASS (exit 0 under Core + SyncVal) on both RTX 5080 and AMD Radeon (cs_w32_cvt_f16_mods exact 8/8 words, ps_sdwa_f16_const 4096/4096 pixels).
  • Directed compute shader covering DPP row_shr:1 across row boundaries (lanes 0 & 16) under bound_ctrl:0 and bound_ctrl:1 with neg on subnormal 0x0001 (0xb3800000 interior, sentinel 0xdeadbeef / 0x80000000 on boundaries) plus op_sel high-destination v_ldexp_f16 and v_frexp_mant_f16 clamp preserving low halves (128 readback words):
    • Parent d1dff7949: 62/128 mismatches on RTX 5080 and 62/128 mismatches on AMD Radeon (0 VUIDs).
    • Head df294dd8a: 0/128 mismatches on RTX 5080 and 0/128 mismatches on AMD Radeon (0 VUIDs).

Minor note (non-blocking): Agree with the earlier review note that cs_w32_cvt_f16_mods only uses a within-quad swap and writes from lane 0, so keeping its wave32 translation registration while removing it from the forced 32-lane list in scripts/run_gpu_tests.py avoids unnecessary skips on 8-lane devices.

No game data used. AI-assisted review (Codex static audit + Gemini dual-GPU verification).

@sodove

sodove commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2419 | head df294dd8a1470b22a8300c951276f2f39b3abfe0 | python3 -m unittest discover -s tests/semantic -p test_cvt_f16_denorm_mods.py -v | red: 13 run, 6 failures | green: 13 passed | Red → green. Compute readback red → green on AMD and RTX

The semantic tests caught half-precision conversion/modifier regressions. The compute shader also returned an incorrect word on both physical devices before the merge.

Captured 12 distinct complete SPIR-V module(s) emitted while rerunning test_cvt_f16_denorm_mods.py on the merged tree. All assembled and passed spirv-val for Vulkan 1.2 with --allow-offset-texture-operand.

cs_w32_cvt_f16_mods assembled and validated on both trees. Its output failed the PR checker on both GPUs on main and matched on both after merging.

Windows Vulkan adapters: AMD Radeon(TM) Graphics (26.11.2) and NVIDIA GeForce RTX 5070 Ti (610.74).

Additional commands

  • python scripts/run_gpu_tests.py --filter cs_w32_cvt_f16_mods --require-pass --assembler spirv-as --build <build> --work <work>

Base 3bd936ecd98995bca3e1da83d5e28d2343001aef was merged into the recorded PR head for the second run. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Native runs used Windows 11, Python 3.12.10. Counts include unittest subtest events where applicable. AI-assisted with OpenAI Codex. Batch claim.

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

VERIFY-2419-VEGA20-RADV: on fixed-wave64 gfx906 / RADV Mesa 26.0.8, the same two synthetic compute inputs gave 83 wrong result words out of 904 on 2d4735682, zero on the merge with df294dd8a. Raw integer readback was compared bit for bit; NaNs and signed zeros were not normalized.

The PR fixture adapted to a native 64-lane workgroup (same encoded instructions) went from 1/8 wrong to 0/8. A 64-lane arithmetic fixture went from 82/896 wrong to 0/896: 18 NaN lanes lost the negated sign on widening, and all 64 lanes were wrong for negation after DPP selection. The subnormal-clamp controls matched on both builds on this driver; they are not part of the hardware Red count. The merge also preserved all 384 checked untouched half-words.

The targeted semantic suite went from 6 failures in 13 tests to 13/13 passed. The modules passed spirv-val; Khronos synchronization validation reported zero errors or hazards on the physical run. No claim of native wave32 execution.

AI-assisted.

@DadaCap
DadaCap merged commit c9f097e into main Nov 28, 2026
7 of 12 checks passed
@DadaCap

DadaCap commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Merged, thank you @Andretuta! This translator PR (rdna2spirv: preserve f16 denormals, NaN payloads, and bit-exact clamp in conversions (SHADER-CVT-F16-DENORM-AND-MODS)) passed its own tests with main merged in during sodove's verification of this batch, so it went in with the batch.

AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.

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