rdna2spirv: preserve f16 denormals, NaN payloads, and bit-exact clamp in conversions (SHADER-CVT-F16-DENORM-AND-MODS) #2419
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2419"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-27T03:17:03Z.
Claimed in the coordination issue as: SHADER-CVT-F16-DENORM-AND-MODS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7208856827)
Related: #2362 (independent — merges cleanly standalone on
main; overlaps 4 lines intools/rdna2spirv.pydpp_insnsrc0neg/absforwarding, keepingargs[first]on rebase so destination-lessv_cmpxstays intact)What and why
v_cvt_f32_f16throughGen.f16_in_bitsinstead of rawUnpackHalf2x16, preserving f16 subnormals, applyingneg/absto the half selected by SDWA or DPP (avoiding 32-bitmodifyon a 16-bit half), and preservingIEEE=0NaN sign and payload bits out of driver float arithmetic.Gen.clamp_f16_bits(3-instruction exact DX10 clamp on rounded f16 words: negative halves including-0and NaNs map to+0, positive values cap at1.0, positive subnormals are preserved bit-exactly) and reuse it inv_cvt_f16_f32,v_div_fixup_f16,v_frexp_mant_f16, andv_ldexp_f16.UnpackHalf2x16sites intools/rdna2spirv.py.tests/semantic/test_cvt_f16_denorm_mods.pyand Vulkan GPU fixtureruntime/test/shaders/cs_w32_cvt_f16_mods.asm(wired intoruntime/test/shaders/check.pyandscripts/run_gpu_tests.py), plus contract update indocs/design.md.Tested
python -I tests/semantic/test_cvt_f16_denorm_mods.py: 13/13 OK (6 fail on unpatchedmaind1dff7949; validated with bothspirv-asandspirv_asm.py+spirv-val --target-env vulkan1.2).scripts/run_gpu_tests.py, native andLEONIDA_W64_EMULATE=1):cs_w32_cvt_f16_modsbefore (d1dff7949):7FFFFFFF 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001; after:FFC4A000 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001(word 0 fixesv_cvt_f32_f16DPPnegon NaN; exact 8/8 words in both modes);cs_w32_new_ops(8/8 exact) andps_sdwa_f16_const(4,096/4,096 pixels exact) pass in both modes.v_cvt_f32_f16andv_cvt_f16_f32translate and passspirv-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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-27T03:48:13Z.
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 based1dff7949b, using the same synthetic pixel shaders and bitwise expectations.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 half0x0001returned0x7fffffffon base and the expected0xb3800000on the PR. Clamped widening passed on both.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:firstis 0 for destination-lessv_cmpxand 1 for the conversion. Keeping the older branch's literalargs[1]produced 57 failing compare subcases locally. Usingargs[first]passed all 30 tests acrosstest_cvt_f16_denorm_mods,test_dpp_f16_mods, andtest_dpp_compare, and the combined translator passed the same 786,432-word GPU check. This was a local integration test ofcf622ce9c2anddf294dd8a1.Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-27T04:05:45Z.
Thanks @ecox982745 for the exhaustive RTX 4090 verification across all 65,536 binary16 encodings and 327,680 narrowing inputs (
0 / 786,432wrong words vs75,766onmain), and for testing the integration with #2362!Noted on
args[first]: this branch already stores the shuffled operand and modifiers intoargs[first](preservingfirst = 0for destination-lessv_cmpx). If #2362 lands first, I'll rebase keepingargs[first]throughout sotest_dpp_compareandtest_dpp_f16_modsstay green; since #2419 merges cleanly standalone onmain, I also updated the PR header note fromDepends on:toRelated:so it doesn't look blocked on #2362's rebase.Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T05:47:11Z.
Review: APPROVED
Approving. Checked on
df294dd8amerged onto current main (d1dff7949).Translator semantic suite: 501 files, only main's known
test_fspx_dot4_op_sel_disasmandtest_int32_to_f64failures.tests/test_translate.py: 721 passed, the one failure is main'sps_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_u8andps_cvt_offall pass. Against main's translatortest_cvt_f16_denorm_modsfails 6 times.Read the diff.
clamp_f16_bitsis 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,negon av_cvt_f32_f16_dppsource went throughmodify()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_modsis inWAVE32_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 matchCVT_F16_MODS_WORDS. Keeping it inWAVE32_DUMPS(wave32 translation) but out of the forced-size list would let it run on more devices. Not a blocker.Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T03:20:53Z.
Review: APPROVED
Verified head
df294dd8a(vs parentd1dff7949) on Windows 11 with dual-GPU hardware readback (RTX 5080616.64+ AMD Radeongfx103632.0.21030.2001, Vulkan SDK1.4.363.0Core + SyncVal).Static & semantic checks:
mantissa * 2^-24), rounded-half clamp mapping, and DPP modifier forwarding viaargs[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.0Core + SyncVal):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_modsexact 8/8 words,ps_sdwa_f16_const4096/4096 pixels).row_shr:1across row boundaries (lanes 0 & 16) underbound_ctrl:0andbound_ctrl:1withnegon subnormal0x0001(0xb3800000interior, sentinel0xdeadbeef/0x80000000on boundaries) plusop_selhigh-destinationv_ldexp_f16andv_frexp_mant_f16clamp preserving low halves (128 readback words):d1dff7949: 62/128 mismatches on RTX 5080 and 62/128 mismatches on AMD Radeon (0 VUIDs).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_modsonly 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 inscripts/run_gpu_tests.pyavoids unnecessary skips on 8-lane devices.No game data used. AI-assisted review (Codex static audit + Gemini dual-GPU verification).
Imported from ubiquitarianism/leonida. Originally posted by sodove on 2026-11-28T10:45:17Z.
#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 RTXThe 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
3bd936ecd98995bca3e1da83d5e28d2343001aefwas 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.Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:12:44Z.
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 on2d4735682, zero on the merge withdf294dd8a. 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.
Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-28T11:54:03Z.
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.