rdna2spirv: VOP3 clamp of a NaN gives +0 under DX10_CLAMP (SHADER-CLAMP-NAN) #642
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!642
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/642/head"
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?
@aadheel on GitHub · 2026-11-24 10:45 UTC · original
aadheel:fix/vop3-clamp-nan-zero→main· +173 −23 · 11 files · 1 commits · merged 2026-11-24 15:50 UTCClaimed in the coordination issue as: SHADER-CLAMP-NAN
What and why
Gen.clamp_modsapplied the VOP3clampbit as a bareOpExtInst %float %glsl FClamp v %f0 %f1. GLSL.std.450 leavesFClampundefined when an operand is NaN, so any NaN reaching the clamp (a NaN operand,inf - inf,-inf * 0, an all-NaN min/max) gave SPIR-V whose result is up to the driver. The CPU interpreter refuses it (UnsupportedConstruct), andtests/spirv_interp.pypasses the NaN through.The game's MODE has DX10_CLAMP=1. Under it the hardware turns a NaN result into +0. This is already measured on gfx1036 in
docs/shader_validation.md: thev_rcp_iflag_f32run (a NaN result is +0 except with DX10_CLAMP off, 169 programs) and SHADER-186 (v_fmac_f16,v_fma_mixlo_f16,v_fma_mix_f32).docs/design.mdlisted it as "Not modelled".test_alu_vop3_mods_hw.pyleaves out those hardware rows because the translation was undefined, anddesign.mdcounts most of its 6,430 undefined cases as this one.The fix selects +0 for a NaN before
FClamp(OpIsNan+OpSelect), the same pattern the translator already uses for thev_div_fixup_f32,v_cvt_pk_u8_f32andv_cvt_pknorm_*clamps. Every f32/f16 op that appliesclampthroughclamp_mods/clamp_f16gets it: the F1/F2 ALU tables, fma/fmac/mad and the legacy ops, min/max/min3/max3/med3, conversions, mix ops, interp, f16 arithmetic and transcendentals. Non-NaN values are unchanged:FClampsees the same operand as before, and omod still applies before the clamp.Not in this PR:
clamp_mods_f64and the packedv_pk_*clamp still emit a bareFClamp. #475 is rewritingclamp_mods_f64, and the f64 row indesign.mdplus that docstring still say "as for f32 ... left to the driver". Whichever of the two lands second should drop "as for f32". I left those lines alone to avoid a conflict with #475.v_dot2_f32_f16also routes its clamp throughclamp_mods. #410 measured that clamp as a hardware no-op and removes it. The two PRs merge cleanly and their tests pass together.tools/mode_diff.py,div_fixup_diff.pyandf16misc_diff.pystill mark clamp of a NaN as "not judged". Follow-up: they can now judge it as +0.Tested
tests/semantic/test_vop3_clamp_nan.py, 12 tests. Covered: quiet, negative and signalling NaN operands and invalid operations underclamp, for f32 ops, legacy mul/mad, f16 ops writing the low half and the high half (the full word is checked, so the other half must be kept), all-NaNv_med3_f16,v_cvt_f32_f16/v_cvt_f16_f32with and without omod, andv_fma_mix_f32/v_fma_mixlo_f16/v_fma_mixhi_f16. Each case expects +0. Controls without a NaN check that the clamp is unchanged. The test fails 33 subtests onmainand passes with the change.FClampwas refused (test_alu_f32_min_max,test_alu_min3_max3, including the min3/max3 reference model), and the f16 unary harness (f16_unary_cases.py), which now checks clamp of a NaN result instead of skipping it.v_sqrt_f16/v_rsq_f16of a negative now exercise it.tests/test_rcp_iflag_mods.pyandtests/test_f64_conversion.pymatch the SPIR-V text and now expect theOpIsNan/OpSelectbeforeFClamp.python -I scripts/run_semantic_tests.py tests/semantic: 466 files, 6,120 tests, all pass excepttest_int32_to_f64. That one also fails on unmodifiedmainon this machine, because Ubuntu 24.04'sspirv-opt2025.1~rc1 segfaults on it.tests/test_translate.py: 666 passed, 1 failed (ps_sdwa_f16_const.asm), the same failure as onmainhere (Ubuntu's LLVM 18llvm-mc). The other translator unit tests (test_f16misc_diff,test_div_fixup_diff,test_mode_diff,test_rdna2spirv_cli,test_text_write_line_endingsand the rest of the 122tests/test_*.pyfiles that use the translator) pass.spirv-val(Vulkan 1.2).scripts/check_no_game_data.pyon every changed file: clean.git merge-treeagainst the heads of the 82 open PRs that touch these files adds no conflicted file. #475, #410, #270 and #309 also pass their own tests and this one when merged with it.GPUs tested: n/a. CPU interpreter and
spirv-valonly; no GPU fixture was run (nollvm-mcwith the AMDGPU target on this machine).AI-assisted: yes (Claude Code)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.🤖 Generated with Claude Code
@GeorgiyDemo on GitHub · 2026-11-24 11:25 UTC · original
Checked
a9bed902on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), againstmaind42773d07:test_alu_f32_min_max,test_alu_min3_max3,test_alu_vop3_mods_hwandtest_valu_f32_min3_max3_madpass, and the newtests/semantic/test_vop3_clamp_nan.pypasses 12 tests.main's translator the new file errors 33 times withspirv_mini_interp.UnsupportedConstruct: GLSL.std.450 FClamp with these operands (undefined), which is exactly the undefined-on-NaN clamp this PR replaces, and the two modified files error once and twice. So the added cases do distinguish the two revisions.llvm-mc 23.1.6. Synthetic fixtures only, no game data. AI-assisted.
@hamdog0203 on GitHub · 2026-11-24 11:35 UTC · original
Independent code review and Windows 11 + RTX 4060 verification on commit
a9bed902d(compared againstmainc9485def3, no game files, AI-assisted: yes).Environment
26200.9974), NVIDIA GeForce RTX 4060 (driver621.35, subgroup32..32)llvm-mc 23.1.6, Khronosspirv-as/spirv-val,vk_compute_test/ GPU harness built with MSVC14.44.35207Test results on
a9bed902dtests/semantic/test_vop3_clamp_nan.py: 12/12 tests OK (negative control against unpatchedmainfailed with 33 errors:UnsupportedConstruct: GLSL.std.450 FClamp with these operands (undefined))tests/semantic/test_alu_f32_min_max.py(43 OK),test_alu_min3_max3.py(20 OK),test_valu_f32_min3_max3_mad.py(22 OK),test_alu_vop3_mods_hw.py(2 OK),tests/test_f64_conversion.py(1 OK),tests/test_rcp_iflag_mods.py(5 OK), and all 5test_valu_*_f16.pysuites (fract,pack_b32,rcp,rsq,sqrt: all OK).vk_compute_test):1OpIsNanguard onmainto10ona9bed902d; passedspirv-valand returned exact expected bits on all 10/10 slots on RTX 4060 (note: NVIDIA's driver already evaluated unguardedFClamp(NaN, 0, 1)to+0.0, whilespirv_mini_interpconfirms the guard eliminates the undefined SPIR-V cases).scripts/run_gpu_tests.py):257/257fixtures translated and passedspirv-val.ps_f32_mods,ps_outmods_f16,ps_f16_med3_mods,ps_f16_fnan_med3,ps_fnan_med3,ps_fnan_med3_s2,ps_mul_legacy_denorm_mods,ps_vop3_const_mods,ps_fmac_f16_mods, etc. all4096/4096 pixels exact).cs_imgload_tfeandcs_imgload_d16_tfefailed, which also fail identically (0/2 passed,texel(5,3) = 2071) on unmodifiedmainhere.Non-blocking code-review note
Gen.clamp_modscovers scalarf32/f16VOP3 paths cleanly. Two otherFClamp(0, 1)sites still emit unguardedFClampon potential NaN results if targeted separately later:Gen.clamp_mods_f64(tools/rdna2spirv.py:6805-6818) and packedv_pk_*_f16clamp (tools/rdna2spirv.py:9369-9375).@qhukz13 on GitHub · 2026-11-24 11:36 UTC · original · reviewed
Reviewed on head
a9bed902d(Linux, no GPU: CPU interpreter and the unit tests only).Checked:
FClampthe translator emits at this head: besidesclamp_mods, the only unguarded ones are the packedv_pk_*clamp (tools/rdna2spirv.py:9379) andclamp_mods_f64(:6821), both called out as out of scope. The remaining sites (v_cvt_{i,u}16_f16,v_cvt_norm_*,v_cvt_pknorm_*, div_fixup) already select +0 for a NaN first, andclamp_f16goes throughclamp_mods, so the f16 paths are covered.test_vop3_clamp_nan.py,test_alu_f32_min_max.py,test_alu_min3_max3.py,test_alu_vop3_mods_hw.py,test_valu_f32_min3_max3_mad.py, the five users off16_unary_cases.py(test_valu_{rcp,rsq,sqrt,fract}_f16.py,test_valu_pack_b32_f16.py),tests/test_rcp_iflag_mods.py,tests/test_f64_conversion.py.tools/semantic_mutants.py --lines 6595-6603 tests/semantic/test_vop3_clamp_nan.py: the one mutant (OpSelect arms swapped) is caught (2 failures, 33 errors).A suggestion, not a blocker: the new file's expectations are +0 by the DX10_CLAMP rule, with the hardware evidence cited from
docs/shader_validation.md.test_alu_vop3_mods_hw.pysays it left out the measured clamp-of-NaN rows because the translation was undefined; now that it is defined, replaying those gfx1036 rows there (in a follow-up if you prefer) would check the +0 against the measured words rather than against the rule.Looks good to me.