Repository navigation
fix(shader): choose scalar f16 NaN results on the bits as RDNA2 does - #23904
adiologydev wants to merge 2 commits into
Conversation
v_add/sub/subrev/mul/fma/fmac/fmamk/fmaak_f16 returned the driver's NaN for a NaN source (0x7fff on NVIDIA), losing payload, sign and the IEEE quiet bit. RDNA2 returns the first NaN source after modifiers (the subtrahend negated), quieted only with IEEE_MODE set; an inf * 0 product in the fma forms gives 0xfe00 even with a NaN src2, as does an invalid operation without NaN sources. selectF16Nan applies this rule in the native and emulated scalar paths. When the startup probe keeps the device's NaNs, the native add, sub, subrev and mul forms pass the first NaN source as both operands, as the packed forms do. Measured on gfx1036 (Ryzen 9 7950X iGPU) through the hardware oracle; agc_driver_float16_nan_sources pins 23 rows in eight float modes.
The selected program now returns RDNA2's NaN, so a device whose scalar NaN payload differs must not pass. The probe compared only the fma forms and accepted any NaN for a NaN, and skipped the add, sub, subrev and mul results it already computed, although float16HostNans now applies to them. Also correct which forms and modes the debt entry says were measured, and describe the probe and the f16 NaN selection exactly.
|
Results from three AMD generations on Linux RADV (Mesa 26.2.6), plus lavapipe on the same Mesa. One thing to flag: the new test fails on lavapipe. Tested head
On the three AMD GPUs, main fails at the same first case and the PR passes: So RADV takes the device branch on all three generations, the same branch as your Radeon iGPU on Windows. With your bit-exact probe, 0 of 294912 results differ from the selected NaN. Main's probe compares fewer results (270336), and it also reports 0. On lavapipe, main fails at a
Other focused tests ( AI-assisted: yes (Claude Code) |
|
Thank you for your contribution! Authors with no pull requests merged into |
What
Scalar f16 arithmetic (
v_add_f16,v_sub_f16,v_subrev_f16,v_mul_f16,v_fma_f16,v_fmac_f16,v_fmamk_f16,v_fmaak_f16) returned the driver's NaN when a source was a NaN. On NVIDIA that is always0x7fff, so the NaN payload, its sign and the IEEE quiet bit were lost, in both IEEE modes. The packed f16 path already chooses its NaN on the bits; the scalar paths did not, except for the default NaN of an invalid fma.RDNA2 (gfx1036) gives, for all these forms: the first NaN source in operand order after its modifiers (
v_sub_f16negates src1,v_subrev_f16src0, and src0 is still taken first), quieted only with IEEE=1; an inf * 0 product gives0xfe00even when src2 is a NaN; with no NaN source an invalid operation gives0xfe00.Change:
selectF16Nan(FloatInstructions.cpp) replaces a NaN result with the NaN that rule selects. The native paths call it beforeclampF16Bits;writeF16calls it afterapplyF16ResultModifiers, where a NaN thatDX10_CLAMPturns into 0 is no longer a NaN and is kept.float16Native(native add/sub/subrev/mul) uses it whenfloat16HostNansis not set. When it is set, the second source is replaced by the first NaN source, as the packed path does, so a driver that returns the other NaN cannot change the result.float16NativeFmauses it in place of the invalid-only fix.float16Binary,float16TernarythroughwriteF16) uses it too;writeF16takes the f16 source bits instead of an invalid flag.ProbeFloat16HostNanscompares all 16 scalar results of its program bit for bit. It compared only the fma forms and accepted any NaN for a NaN, which no longer holds now that the selected program returns RDNA2's NaN; the add, sub, subrev and mul forms it already ran were not compared, thoughfloat16HostNansnow applies to them.Found with a differential sweep: every VALU instruction run on gfx1036 through the hardware oracle and through the recompiler on an RTX 5090 (
HW_ORACLE_RUNTIME=vulkan), 1760 operand rows each, at IEEE=1/denormals kept and IEEE=0/DX10_CLAMP=1/f32 denormals flushed. Sweep summary for the six VOP2/VOP3 forms (rows that differ from gfx1036, of 1760, per mode):v_add_f16v_sub_f16v_subrev_f16v_mul_f16v_fma_f16v_fmac_f16All 7260 NaN results of these rows on gfx1036 follow the rule above.
Overlap (
tools/pr_overlap.py --branch):IEEE_MODEset and keeps the driver's payload. This PR replaces that result with the selected NaN, which is already quiet withIEEE_MODEset, so the line fix(shader): quiet NaN results of native scalar f16 add, sub and mul with IEEE_MODE #20099 adds would have no effect after this PR.writeDirectedF16); this PR covers the nearest-even paths. The conflict is thefloat16Nativecall infloat16Binary; after both merge,writeDirectedF16can useselectF16Nan.writeF16from before the conversion to after it; after both merge, the NaN selection must come before that clamp so a NaN underDX10_CLAMPstill gives 0.APS5_NAN_SPECULATION) keys its f16 NaN note on theinvalidargument ofwriteF16, which becomesnanSources.ARCHITECTURE.mdparagraph changed here.mainand could not be compared by the tool; their changes in the same files are in other lines (header lines 30-34 and 417-423,FloatInstructions.cppline 673). The other 21 PRs it could not compare change otherARCHITECTURE.mdsections.Tested
Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON.agc_driver_float16_nan_sources: 23 rows of the eight forms plus VOP3 neg/abs/clamp forms, measured on gfx1036 (Ryzen 9 7950X iGPU, AMD Windows driver 32.0.21042.62, HIP) at IEEE 0/1, DX10_CLAMP 0/1, f16 denormals kept and flushed, on the device target and on a target without native f16. Fails onmainon both GPUs (RTX 5090:v_add_f16 ... 0x7e00 ... is 0xa5c37fff, expected 0xa5c37e00; Radeon iGPU through Vulkan:v_add_f16 ... 0xfe03, 0x7e04 ... is 0xa5c37e04, expected 0xa5c3fe03), passes with this PR on both.f16 NaN results with IEEE_MODE clear: selected, 1504 of 2048 probe results differ). The Radeon iGPU runs the device branch: with the exact probe, 0 of 294912 packed and scalar results differ. With the scalar operand replacement disabled, the probe found 122 of 36864 results that differ and kept the selection; the previous fma-only comparison would not have run those forms.f16|float16|fp16|half|fma|sdwa|vop2|vop3|nan|dot|interpolation|pknorm|ldexp|modifier: 216 pass on both GPUs with this PR;agc_driver_native_fmask_2/8were not built (Not Run), as onmain.float16_nan_sources|float16_native|f16_fma_invalid_nan|packed_float16_native|float16_nan_probe|float16_unused_nan_payloads|float16_modifiers|float16_ternary_clamp|f16_fma_denormal_modes|f16_denormal_flush|float16_misc|float16_rounding|float16_fma_rounding|float16_result_modifiers) onmainand this PR withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, Vulkan SDK 1.4.363.0 layer. Each of the 4 logs has 14Insert instance layer "VK_LAYER_KHRONOS_validation"lines, nowas not found but was requested by env var VK_INSTANCE_LAYERS, and 0VUID-lines.main: 13/14 pass on each GPU (the new test fails); this PR: 14/14.agc_driver_float16_nativeon the Radeon iGPU: 14468 NaN results whose payload differs between the native and the f32 path onmain, 0 with this PR.Checklist
main; no other open PR implements the same functions (fix(shader): quiet NaN results of native scalar f16 add, sub and mul with IEEE_MODE #20099 overlaps on theIEEE_MODEquiet bit offloat16Native; other textual overlaps listed in What)NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)