fix(shader): give fma the default NaN for infinity times a hardware-flushed denormal #18469
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/18469"
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 angrysandhill on 2026-11-11T17:13:31Z.
What
Measured on RDNA2 (gfx1031, RX 6750 XT; the PS5's gfx1013 is not measured): with f32 denormals flushed (FLOAT_MODE f32 denormal field 0, as in the
0xc0most titles use), the GPU flushes a denormal factor before the multiply. So infinity times a denormal is an invalid product, and the fused forms return the default NaN0xffc00000, also when the addend is a NaN.On a host that uses the hardware f32 flush (
DenormFlushToZero 32where the driver offers it, the driver's default on NVIDIA and MoltenVK),v_fma_f32,v_fmac_f32,v_fmamk_f32andv_fmaak_f32returned the NaN addend instead:f32DenormalFlushForleaves the flush of these instructions to the host, so the translation does not flush their factors.invalidProductF32then tested the unflushed factor for an exact zero, missed the invalid product, andnanResultF32picked the addend.The emulated flush already flushes the factors first and gives the default NaN. That path is used by hosts without the hardware flush, with
APS5_NO_HW_F32_FLUSH, in denormal mode 2, with directed rounding, and byv_fma_mix_f32. So the two translations of one instruction disagreed.The change:
f32DenormalFlushFor: returns4instead of0when the host flushes. Its other readers test only the values 1 and 2, so they behave as before.invalidProductF32: with that value set, a factor below the smallest normal counts as zero.No debt entry changes.
shader.mdalready records both halves of the rule: "f32 denormals" says these forms flush their f32 inputs when the field is 0, and "Signaling NaN inputs" says an invalid product gives the default NaN even when src2 is a NaN. Neither describes the hardware-flush translation, so there is no statement to correct.ARCHITECTURE.mdis not touched either: #17588 adds a sentence to the same one-line paragraph about the hardware flush.Related open PRs, none of which changes these lines or this behaviour:
0xc0comparison.nanResultF32and take the invalid-product flag as given.Tested
Setup: Windows 10 Home 22H2 (10.0.19045), AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2.0 r7 UCRT, Release,
BUILD_TESTING=ON. Everything below was run onmainafe2226017and on this branch, which is based on it. This device uses the hardware f32 flush: its start-up line reads[float-controls] f32 denormal flush: hardware (0 of 5472 probe results differ from the emulated flush, 389 ms).RDNA2 measurement (hardware oracle, HIP runtime), compared row by row with the recompiler through
HW_ORACLE_RUNTIME=vulkan.Rows differing from RDNA2 with the f32 denormal field at 0, the same at IEEE 0 and IEEE 1:
mainv_fma_f32,v_fmac_f32v_fmamk_f32with a denormal literalv_fmamk_f32with an infinite literalv_fmaak_f32with a NaN literalv_fma_f32withnegandabs, VOP3v_fmac_f32v_fma_mix_f32with f32 sources,v_mul_f32,v_mul_legacy_f320xffc00000,maingives the addend (quieted with IEEE 1).mainor with this change.mainwithAPS5_NO_HW_F32_FLUSH=1(the emulated flush) matches RDNA2 on all 256 values of the new test's rows in both IEEE modes, so only the hardware-flush translation was wrong.v_fma_f16andv_fmac_f16(with the f16 denormal field at 0 to 3) andv_fma_mix_f32with an f16 source (with f16 denormals kept and flushed). Their NaN-bit differences, which the "Signaling NaN inputs" entry leaves to the driver, are the same onmainand with this change.Tests:
agc_driver_f32_fma_flushed_invalid_product: 32 rows through 8 forms:v_fma_f32,v_fmac_f32,v_fmamk_f32with an infinite and with a denormal literal,v_fmaak_f32with a NaN literal,v_fma_f32withnegandabs, VOP3v_fmac_f32andv_fma_mix_f32. The expected values are RDNA2's.0xc0IEEE 0 and0xc0IEEE 1, with the emulated flush and, whereTarget()uses it, with the hardware flush; each also withfusedExactArithmeticforced. The last line says how many of the four passes ran.mainwith only this test file added it fails:f32 fma flushed invalid product: hardware flush no float mode lane 0 v_fma_f32 v10, v4, v5, v6 is 0x7fc00001, expected 0xffc00000. The three unfused emulated-flush runs come first and pass.mainfrom this branch. Here, withAPS5_NO_HW_F32_FLUSH=1(the recompiler is told this device has no hardware flush),mainwith only this test file added passes:f32 fma flushed invalid product tests passed (2 of 4 passes run; the hardware flush passes need a device that uses the hardware f32 flush). So the test fails onmainonly on a device that uses the hardware flush.^(agc_|shader|vector_), 4 at a time: 1,647 tests: 1,628 passed, 18 skipped, 1 failed.main: the same run onmain(1,646 tests) gives every test the same outcome. The only extra test is the new one, which passes.agc_driver_sample_order_grid_referencefails onmaintoo, with the same message ("the exact path did not render the checkerboard bit for bit like the reference"). It is the odd-row failure that #18229 reports on an RX 6700 XT; #9503 addresses it (not run here).-R "^(agc_driver_(f32_|fma_|mac_|mullit|nan_|div_fmas|f16_fma|f64_fma|float16_fma|vcmp_f32).*|agc_driver_.*(denormal|_nan).*)$"(48 onmain, which lacks the new one), one at a time withctest -V,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1.was not found but was requested by env var VK_INSTANCE_LAYERS, and every test logs the layer insertion.VUID-line on either side.agc_driver_float16_unused_nan_payloadsskips on both sides, because this device has no native f16 conversions with RTE rounding and kept f16 denormals.Not measured: the PS5's gfx1013, and hosts other than this one. Built with GCC 15.2 only:
f32DenormalFlushForis the function #17685 reports GCC 14 miscompiling, and this change was not built with GCC 14.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-12T14:27:03Z.
Review: COMMENTED
Reviewed — no blocking findings
The change makes the hardware-flush translation of the fma forms return the default NaN for an infinite product with a denormal factor, as the emulated flush already does. I found no defect in the three changed files at head
352f72a9f064f97b7441f79d2aff8f2d8eafd2b0.What I ran
Head
352f72a9f064f97b7441f79d2aff8f2d8eafd2b0, basemain. No build and no test run in this pass.Read the run folder (
description.md,commits.md,manifest.json,files.json,diff.patch) and the house sections "Readable reviews", "Standard PR response format" and "Accuracy and evidence". Repository reads at the head, through the reader:Results
f32DenormalFlushForDispatchInstructionstests/execution/*.cppFindings
Blocking
None.
Non-blocking
None.
Sources
f32DenormalFlushForflushF32DenormalandflushTinyProducttests/executionNot verified
main" negative control is author-reported. I did not run it.OperandAccess.cppandVectorInstructions.cppI read the diff and the head excerpts above, not the full texts infiles/.flushesInHardware(VectorInstructions.cpp:27) was not read. The claim thatv_fma_mix_f32stays on the emulated path is not confirmed.nanResultF32was not read. The claim that only denormal-factor × infinity rows change rests on the author's table.shader.md, which the description cites, was not checked. Only "Signaling NaN inputs" was.To get this to a good state
Nothing blocking. Before merge: confirm hosted CI on
352f72a9is green, and, if the new test has not run on the final head, run it on a device that uses the hardware f32 flush.