Skip to content

fix(shader): choose scalar f16 NaN results on the bits as RDNA2 does - #23904

Closed
adiologydev wants to merge 2 commits into
boykopovar:mainfrom
adiologydev:fix/shader-scalar-f16-nan-sources
Closed

adiologydev wants to merge 2 commits into
boykopovar:mainfrom
adiologydev:fix/shader-scalar-f16-nan-sources

Conversation

@adiologydev

Copy link
Copy Markdown

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 always 0x7fff, 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_f16 negates src1, v_subrev_f16 src0, and src0 is still taken first), quieted only with IEEE=1; an inf * 0 product gives 0xfe00 even when src2 is a NaN; with no NaN source an invalid operation gives 0xfe00.

Change:

  • New selectF16Nan (FloatInstructions.cpp) replaces a NaN result with the NaN that rule selects. The native paths call it before clampF16Bits; writeF16 calls it after applyF16ResultModifiers, where a NaN that DX10_CLAMP turns into 0 is no longer a NaN and is kept.
  • float16Native (native add/sub/subrev/mul) uses it when float16HostNans is 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.
  • float16NativeFma uses it in place of the invalid-only fix.
  • The f32 lowering (float16Binary, float16Ternary through writeF16) uses it too; writeF16 takes the f16 source bits instead of an invalid flag.
  • ProbeFloat16HostNans compares 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, though float16HostNans now 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):

Instruction main this PR
v_add_f16 531 0
v_sub_f16 381 0
v_subrev_f16 347 0
v_mul_f16 537 0
v_fma_f16 681 0
v_fmac_f16 529 0

All 7260 NaN results of these rows on gfx1036 follow the rule above.

Overlap (tools/pr_overlap.py --branch):

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON.

  • New 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 on main on 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.
  • The RTX 5090 runs the selection branch (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.
  • 218 AGC tests matching 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/8 were not built (Not Run), as on main.
  • Validation: 14 focused tests (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) on main and this PR with ANYPS5_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 14 Insert instance layer "VK_LAYER_KHRONOS_validation" lines, no was not found but was requested by env var VK_INSTANCE_LAYERS, and 0 VUID- lines. main: 13/14 pass on each GPU (the new test fails); this PR: 14/14.
  • agc_driver_float16_native on the Radeon iGPU: 14468 NaN results whose payload differs between the native and the f32 path on main, 0 with this PR.

Checklist

  • Based on current 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 the IEEE_MODE quiet bit of float16Native; other textual overlaps listed in What)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 5090, driver 621.14 (Vulkan 1.4.357), Windows 11 Pro 26200; AMD Radeon Graphics (Ryzen 9 7950X iGPU, RDNA2 gfx1036), AMD Windows driver 32.0.21042.62, Windows 11 Pro 26200
  • AI-assisted: yes

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.
@github-actions github-actions Bot added shader core/shader docs agc libSceAgc* labels Nov 19, 2026
@tablefrogpillow

Copy link
Copy Markdown
Contributor

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 26f94dee1a. For the main arm I used the same tree and restored only the four non-test source files (Float16NanProbe.cpp, TranslationContext.hpp, FloatInstructions.cpp, SubwordPacking.cpp) from the merge-base 428b801f7e, so both arms build the new Float16NanSources.cpp. Env: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, Khronos validation 1.4.363. NAVI31, RAPHAEL and lavapipe ran on one box; for Vega I pulled the same build's test binaries and .prx libraries to a second box with the same Mesa 26.2.6 and layer 1.4.363.

agc_driver_float16_nan_sources

GPU probe (f16 NaN results with IEEE_MODE clear), PR main PR
RX 7900 GRE (RADV NAVI31, gfx1100, RDNA3) host, 0 of 294912 differ fail pass
Ryzen 7800X3D iGPU (RADV RAPHAEL, gfx1036, RDNA2) host, 0 of 294912 differ fail pass
RX Vega 56 (RADV VEGA10, gfx900, GCN5, Wave64), 3 runs each arm host, 0 of 294912 differ fail 3/3 pass 3/3
lavapipe (llvmpipe, LLVM 21.1.8) no native f16 fail fail (3/3)

On the three AMD GPUs, main fails at the same first case and the PR passes:

v_subrev_f16 v12, v4, v5 with f16 denormals kept, IEEE mode off on the device target of 0x0000fe03, 0x00007e04, 0x00003c00 is 0xa5c37e04, expected 0xa5c37e03

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 v_subrev_f16 case (0x7e00, 0x3c00 gives 0x7e00, expected 0xfe00). With the PR that case passes, but the test then fails deterministically at an fma with a NaN addend:

v_fma_f16 v14, v4, v5, v6 with f16 denormals flushed, IEEE mode off, DX10 clamp on the device target of 0x00003c00, 0x00003c00, 0x00007e02 is 0xa5c33c00, expected 0xa5c37e02

1.0 * 1.0 + NaN comes back as 1.0. On lavapipe the "device target" is the path without native f16 (the device has no native f16 with RTE and kept denormals). I haven't found the cause. I can't tell whether this is the lowering or llvmpipe's handling of that NaN, or whether main's lowering would give the same result here, because main stops at the v_subrev_f16 case first.

Other focused tests (f16_fma_invalid_nan, f16_min_max_nan, float16_nan_probe, float16_native, packed_float16_native, float16_nan_probe_spirv; float16_unused_nan_payloads skips on all four because they have no native f16 conversions with RTE and kept f16 denormals) give the same result on both arms on NAVI31 and RAPHAEL: they pass, or skip on lavapipe. NAVI31, RAPHAEL and lavapipe ran once per arm. On Vega I ran only float16_nan_sources and float16_nan_probe; the probe test passes on both arms. Every log shows Insert instance layer "VK_LAYER_KHRONOS_validation", none shows was not found, and there are 0 VUID- lines in either arm.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Nov 19, 2026
@github-actions

Copy link
Copy Markdown

Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

@github-actions github-actions Bot closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants