Repository navigation
rdna2spirv: VOP3 clamp of a NaN gives +0 under DX10_CLAMP (SHADER-CLAMP-NAN) - #642
Conversation
Gen.clamp_mods applied the VOP3 clamp bit as a bare GLSL.std.450 FClamp, which is undefined for a NaN operand. The game's MODE has DX10_CLAMP=1, under which the hardware turns a NaN result into +0 (measured on gfx1036: the v_rcp_iflag_f32 run and SHADER-186 for v_fmac_f16 / v_fma_mix*). Select +0 for a NaN before FClamp, as the div_fixup, cvt_pk_u8 and cvt_pknorm clamps already do. This covers every f32/f16 op that applies clamp through clamp_mods/clamp_f16. - tests/semantic/test_vop3_clamp_nan.py: NaN operands (quiet, negative, signalling) and invalid operations (inf-inf, -inf*0) under clamp give +0 for f32, f16 (low and high half), conversions, legacy and mix ops; controls without a NaN are unchanged. It fails 33 subtests on the old translator. - The two negative controls that expected the undefined FClamp (test_alu_f32_min_max, test_alu_min3_max3) now expect +0, and the f16 unary harness checks clamp of a NaN result instead of skipping. - test_rcp_iflag_mods / test_f64_conversion match the new SPIR-V. - docs/design.md and docs/shader_validation.md no longer list the f32/f16 clamp of a NaN as unmodelled. Still a bare FClamp: the f64 clamp (open PR ProjectLeonida#475) and the packed v_pk_* clamp. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Checked
llvm-mc 23.1.6. Synthetic fixtures only, no game data. AI-assisted. |
|
Independent code review and Windows 11 + RTX 4060 verification on commit Environment
Test results on
|
qhukz13
left a comment
There was a problem hiding this comment.
Reviewed on head a9bed90 (Linux, no GPU: CPU interpreter and the unit tests only).
Checked:
- Every
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. - Pass:
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.py says 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.
Claimed 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. SHADER-F64-OMODS: the binary64 VALU ops take clamp and nothing else from their omod #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 SHADER-F64-OMODS: the binary64 VALU ops take clamp and nothing else from their omod #475.v_dot2_f32_f16also routes its clamp throughclamp_mods. rdna2spirv: treat VOP3P v_dot2_f32_f16 clamp as hardware no-op (SHADER-DOT2-F32-F16-CLAMP) #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. SHADER-F64-OMODS: the binary64 VALU ops take clamp and nothing else from their omod #475, rdna2spirv: treat VOP3P v_dot2_f32_f16 clamp as hardware no-op (SHADER-DOT2-F32-F16-CLAMP) #410, test: follow full-EXEC f64 output word stores #270 and rdna2spirv: intra-block %vgpr/%sgpr value forwarding (resubmission of #4518) #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