fix(shader): give f16 fma invalid operations the default NaN #7074
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7074"
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 boykopovar/AnyPS5. Originally posted by CrimsonMac on 2026-10-17T10:23:26Z.
What
On NVIDIA, an invalid operation in
v_fma_f16and its family, or inv_fma_mix_f32with f16 sources, returns NVIDIA's own NaN (0x7fff/0x7fffffff), not the default NaN. AMD's driver returns the default NaN (0xfe00/0xffc00000), and so does lavapipe on x86.agc_driver_f16_fma_denormal_modes(#7034) andagc_driver_fma_mix_f16_denormal_modes(#7040) originally failed on NVIDIA because of it:#7093 has since made both tests accept any NaN, so this PR adds its own test,
agc_driver_f16_fma_invalid_nan, which requires the exact bits.v_fma_f16,v_fmac_f16,v_fmamk_f16,v_fmaak_f16andv_fma_mix_f32with f16 sources left all of their NaN bits to the driver (docs/dev/debt/shader.md, "Signaling NaN inputs"). The f32-source forms already pick their NaN on the bits throughnanResultF32, where an invalid product gives0xffc00000. The f16-source forms can't reuse it, because NVIDIA's f16 to f32 conversion loses the payload of an f16 NaN source. An invalid operation has no payload to keep, so this PR handles only that case:inf * 0, orinf - infin the add) becomes the default NaN.v_fma_f16family:0xfe00, selected on the f16 bits after the conversion and clamp. A DX10 clamp still turns it into 0.v_fma_mix_f32with an f16 source:0xffc00000.inf * 0product with a NaN src2 also gives the default NaN, as in the f32 forms:0xfe00in the f16 fma forms and0xffc00000inv_fma_mix_f32. @thuanquang measured this natively on gfx1031 through HIP in all four modes (comment). The RDNA 2 iGPU's Vulkan driver instead returns src2 quieted in the f16 fma forms, so the test requires the default NaN there too.invalidOperationF32holds the condition.writeF16takes it as an optional argument, and only the f16 FMA family passes it.Follow-up, not in this PR: scalar f16 add, sub and mul have the same gap for
inf - infandinf * 0. Their tests accept any NaN, and their hardware bits are not measured here.Tested
Windows 11 (26100), Ryzen 5 7600, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON, this branch at3b9c285ea(based on673f7406c; the earlier runs below were onmain47a266cf9merged withbe204648d).agc_driver_f16_fma_invalid_nancoversv_fma_f16,v_fmac_f16and bothv_fma_mix_f32forms. It runs 10 rows each, in 5 float modes (no known mode, denormals flushed or kept, IEEE off and on):core/shader/recompilerreverted to main0x7fff,0x7fffffff)inf * 0+ NaN src2 row (0x7e00)5bb403d84's src2 carve-out0x7fff)0x7e00)*_nantests, this PR,--repeat until-fail:5,ANYPS5_NO_SHADER_CACHE=1ctest -R '^agc_' -j8(645 tests),3b9c285eaagc_driver_multisample_fill_host_onlyfailure under-j8(passes 10/10 alone)ctest -j 8(1399 tests),be204648dguest_sce_net(host DNS timeout, also on main) anddynamic_segment/windows_icon(antivirus in TEMP; both pass with TEMP on an excluded folder)agc_driver_lds_mixed_width_atomics(#6666, fixed by #6976) andagc_driver_layered_volume_vertex_no_gpl(#5804), both failing on mainctest -R '^agc_'passes 662/662 (comment).python tools/check_conventions.py --base upstream/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_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)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T10:31:29Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/shader/recompiler/Translation/include/Translation/TranslationContext.hppcore/shader/recompiler/Translation/src/FloatInstructions.cppcore/shader/recompiler/Translation/src/FloatInstructions.cppdocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mdcore/shader/recompiler/Translation/src/FloatInstructions.cppdocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mdImported from boykopovar/AnyPS5. Originally posted by marco-scheffler on 2026-10-17T14:18:31Z.
Linux NVIDIA data point: Ubuntu 24.04.4, RTX 4000 SFF Ada Generation, driver 595.99.02, GCC 13.3.0, Release. This PR at
b8190ee12merged ontomain47a266cf9, which already has #7093's NaN-tolerant checks. To see the bits this PR produces, I also ran both tests with their strict versions from before #7093 (32f28a462^1).ANYPS5_NO_SHADER_CACHE=1, 5 runs each.main's tests, RTX0x7fff, expected0xfe00;0x7fffffff, expected0xffc00000With this PR
ctest -R '^agc_'passes 662/662 on the RTX.Imported from boykopovar/AnyPS5. Originally posted by CrimsonMac on 2026-10-17T14:49:30Z.
Thanks for the RTX 4000 Ada / Linux run, and for checking against the strict tests from before #7093. Since #7093 made those checks accept any NaN, nothing in the suite would catch a regression here, so I've added
agc_driver_f16_fma_invalid_nan(be204648d). It requires0xfe00/0xffc00000for invalid operations inv_fma_f16,v_fmac_f16and bothv_fma_mix_f32forms, in 5 float modes. Withcore/shader/recompilerreverted to main it fails on every row on the RTX 4070 SUPER.Writing it turned up one case the first commit got wrong. Without this PR, AMD's driver on the RDNA 2 iGPU returns src2 for
inf * 0plus a NaN src2 in the f16 fma forms (0x7e00, and0x7d00quieted to0x7f00), whilev_fma_mix_f32gives0xffc00000for the same sources. The first commit forced0xfe00there.5bb403d84now leaves that case to the driver in the f16 fma forms and keeps it inv_fma_mix_f32; the debt entry records it. The body has the updated runs.Imported from boykopovar/AnyPS5. Originally posted by marco-scheffler on 2026-10-18T09:33:47Z.
Re-ran
be204648donmaincb1d6e5bd(RTX 4000 SFF Ada, 595.99.02, Linux;ANYPS5_NO_SHADER_CACHE=1):agc_driver_f16_fma_invalid_nanand both denormal-mode tests pass 5/5 on the RTX and on lavapipe, andctest -R '^agc_'passes 709/709 on the RTX. Withcore/shader/recompilertaken frommain, the new test fails 3/3 on both:v_fma_f16 … is 0x7fff, expected 0xfe00on the RTX andv_fma_mix_f32 of three f16 sources with denormals kept, IEEE mode off of 0x7e007c00, 0x00008000, 0x80000000 is 0x7fc00000, expected 0xffc00000on lavapipe, so CI's lavapipe job would catch a regression too.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T12:22:31Z.
Review: COMMENTED
Apple GPU data point for
agc_driver_f16_fma_invalid_nan: Apple's driver gives a third NaN encoding here, so the test is red on Apple without the fix, not only on NVIDIA.Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Head be204648d. Method: macos-port @ b7eb6b6b + ImportWatched + PR diff, not main. One context conflict in
FloatInstructions.cpp: the port predates the f16 input/tiny-result flush aroundfmaF16RoundedToOdd, so I kept the port's line and added this PR'sinvalidOperationF32(...)argument to itswriteF16call. The other four files applied cleanly.v_fma_f16 without a known float mode of 0x00007c00, 0x00000000, 0x00000000 is 0x00007e00, expected 0x0000fe00So for
inf * 0inv_fma_f16, Apple returns the positive quiet NaN0x7e00, while NVIDIA returns0x7fffand AMD returns0xfe00. The PR selects0xfe00on the bits, so the result no longer depends on the vendor. The test stops at the first row that fails, so this is the only raw Apple value I have.Also with the PR:
agc_driver_f16_min_max_nanpasses on both drivers.agc_driver_float16_fma_roundingfails on the SDK driver and passes with #2704, the same as the port baseline (signed-zero issue, not this PR).Full
^agcsuite with the PR (SDK driver,-j1 --timeout 30, flip/display/lds_atomics64 excluded): 13 failures out of 434, the same 13 as the port baseline. No new failures.GPUs tested: Apple M2 Max (MoltenVK 1.4.2 and MoltenVK main + SPIRV-Cross #2704)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-18T19:52:16Z.
Native hardware data for the case the debt line leaves open ("the native
v_fma_f16is not measured"). RX 6700 XT (gfx1031, RDNA 2), the instructions run directly through HIP withtools/hw-oracle/template.s(assembled by the AMD Windows driver's comgr, driver 32.0.21045.5002), in all four modes: IEEE 0 and 1, f16/f32 denormals kept and flushed.The PR's own rows and encodings (the
FmaCodeandMixCodewords ofF16FmaInvalidNan.cppwith its 8FmaRowsand 8MixRows): every result is what the test expects,0x0000fe00fromv_fma_f16andv_fmac_f16,0xffc00000from bothv_fma_mix_f32forms, in all four modes.inf * 0 with a NaN src2, the case 5bb403d84 leaves to the driver: the hardware gives the default NaN here too, not src2. Products
inf*0,0*inf,-inf*-0,inf*-0,-inf*0and-0*-inf, with src20x7e00,0x7d00,0xfe00,0xfd00,0x7c01,0x7fff,0x7e55and0x7d55:v_fma_f16,v_fmac_f160x0000fe00v_fmamk_f16(K0x0000and0x7c00)0x0000fe00v_fmaak_f16(K0x7e00,0x7d00,0xfe00)0x0000fe00v_fma_mix_f32, three f16 sources0xffc00000inf * 0 with a non-NaN src2 and inf - inf (
inf*1 + -infand similar) give0x0000fe00in every f16 form, and0xffc00000inv_fma_mix_f32.NaN sources without an invalid product, including a NaN factor times zero: the first NaN in operand order, quieted only with IEEE=1, in 384 of 384 rows (
v_fma_f16,v_fmac_f16andv_fma_mix_f32, 32 rows each in each of the four modes).So on this GPU the f16 fma forms follow the f32 rule in the debt entry: an invalid product gives the default NaN even when src2 is a NaN. The
0x7e00/0x7f00from the RDNA 2 iGPU came through the Vulkan driver, so it reflects how that driver compiles f16 fma, not whatv_fma_f16does. By this data, b8190ee12's form (the default NaN for an inf * 0 product whatever src2 is) matches the hardware. A row likeinf * 0 + 0x7e00inFmaRowswould cover the case.This is desktop RDNA 2, not the PS5's gfx1013. I have not run the PR's test through the Vulkan driver on this GPU.
Imported from boykopovar/AnyPS5. Originally posted by CrimsonMac on 2026-10-18T20:14:53Z.
Thanks, that settles it. Since the iGPU's
0x7e00/0x7f00comes from the Vulkan driver's compilation and not fromv_fma_f16itself,5bb403d84was wrong.3b9c285eagoes back tob8190ee12's form: in every f16 fma form, aninf * 0product gives0xfe00whatever src2 is. The debt entry now cites your gfx1031 measurement and notes the iGPU driver's Vulkan result separately.FmaRowsgetsinf * 0 + 0x7e00and-0 * -inf + 0x7d00, andMixRowsgets-inf * -0 + 0x7e00andinf * 0 + 0x7fff. With5bb403d84's line put back, the test now fails on both of my GPUs:v_fma_f16 … of 0x00007c00, 0x00000000, 0x00007e00 is 0x00007fffon the RTX 4070 SUPER and… is 0x00007e00on the RDNA 2 iGPU, both expecting0x0000fe00. With the commit, the new test and the f16 fma, fma_mix,float16_fmaandf16_min_maxtests pass 5/5 on both.ctest -R '^agc_'(645 tests) passes on the RTX, apart from one-j8failure ofagc_driver_multisample_fill_host_onlythat passes 10/10 on its own. On the iGPU it fails only #5804 and #6666.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-19T06:49:17Z.
The updated test on the AMD Windows driver whose
0x7e00started this: RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200, WinLibs GCC 15.2.0 r7, Release.main7f596df48with this PR's head3b9c285eamerged (clean merge). Each run is a fresh renamed copy of the test executable, withANYPS5_NO_SHADER_CACHE=1.agc_driver_f16_fma_invalid_nanpasses 3 of 3.FloatInstructions.cppset back to 5bb403d84's form (the test unchanged), it fails 3 of 3 on the new row:v_fma_f16 without a known float mode of 0x00007c00, 0x00000000, 0x00007e00 is 0x00007e00, expected 0x0000fe00. So on this driver the new rows catch the case, and the forced default NaN is what makes them pass.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T17:57:52Z.
Checked the diff against the description, the new
agc_driver_f16_fma_invalid_nan(requires exact bits0xfe00/0xffc00000; fails without the change per the NVIDIA, Linux and AMD runs in the thread) and the debt entry (cites the gfx1031 measurement). Rebased onto currentmain(onlydocs/dev/debt/shader.mdconflicted, next to thev_fma_mixlo_f16line; diff otherwise unchanged); merged.