fix(shader): give fma the default NaN for infinity times a hardware-flushed denormal #18469

Merged
angrysandhill merged 1 commit from upstream-pr/18469 into main 2026-11-16 06:00:44 +00:00
angrysandhill commented 2026-11-11 17:13:31 +00:00 (Migrated from github.com)

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 0xc0 most 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 NaN 0xffc00000, also when the addend is a NaN.

On a host that uses the hardware f32 flush (DenormFlushToZero 32 where the driver offers it, the driver's default on NVIDIA and MoltenVK), v_fma_f32, v_fmac_f32, v_fmamk_f32 and v_fmaak_f32 returned the NaN addend instead:

  • f32DenormalFlushFor leaves the flush of these instructions to the host, so the translation does not flush their factors.
  • invalidProductF32 then tested the unflushed factor for an exact zero, missed the invalid product, and nanResultF32 picked 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 by v_fma_mix_f32. So the two translations of one instruction disagreed.

The change:

  • f32DenormalFlushFor: returns 4 instead of 0 when 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.md already 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.md is 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:

  • #18285 lists this difference as the four rows it leaves in its 0xc0 comparison.
  • #17588 makes the start-up flush probe compare NaN bits. The probe has no row with an infinite factor and a NaN addend, so its outcome is the same with and without this change.
  • #4287 and #17733 restructure nanResultF32 and 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 on main afe2226017 and 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: 288 per instruction. One factor is ±the smallest or ±the largest denormal, ±0, the smallest normal or 1; the other is ±infinity or 1; the addend is a quiet NaN, a signalling NaN, a negative NaN with a payload, 1, +0 or −infinity. Both factor orders. The literal forms take one factor or the addend from the literal.
  • Modes: IEEE 0 and 1, each with the f32 denormal field at 0, 1, 2 and 3. Wave32, round to nearest even, DX10_CLAMP 1, FP16_OVFL 0, f16 denormals kept.

Rows differing from RDNA2 with the f32 denormal field at 0, the same at IEEE 0 and IEEE 1:

main this PR
v_fma_f32, v_fmac_f32 48 of 288 each 0
v_fmamk_f32 with a denormal literal 48 of 288 0
v_fmamk_f32 with an infinite literal 36 of 288 0
v_fmaak_f32 with a NaN literal 96 of 288 0
v_fma_f32 with neg and abs, VOP3 v_fmac_f32 48 of 288 each 0
v_fma_mix_f32 with f32 sources, v_mul_f32, v_mul_legacy_f32 0 0
  • Every differing row is a denormal factor times ±infinity with a NaN addend: RDNA2 gives 0xffc00000, main gives the addend (quieted with IEEE 1).
  • With the f32 denormal field at 1, 2 or 3, no row of the table differs on main or with this change.
  • main with APS5_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.
  • The same sweeps include v_fma_f16 and v_fmac_f16 (with the f16 denormal field at 0 to 3) and v_fma_mix_f32 with 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 on main and with this change.

Tests:

  • New agc_driver_f32_fma_flushed_invalid_product: 32 rows through 8 forms: v_fma_f32, v_fmac_f32, v_fmamk_f32 with an infinite and with a denormal literal, v_fmaak_f32 with a NaN literal, v_fma_f32 with neg and abs, VOP3 v_fmac_f32 and v_fma_mix_f32. The expected values are RDNA2's.
    • Runs: no float mode, 0xc0 IEEE 0 and 0xc0 IEEE 1, with the emulated flush and, where Target() uses it, with the hardware flush; each also with fusedExactArithmetic forced. The last line says how many of the four passes ran.
    • On main with 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.
    • With this change it passes.
    • On a device without the hardware flush, such as CI's lavapipe, only the emulated passes run. This change does not alter their translation, so there the test does not tell main from this branch. Here, with APS5_NO_HW_F32_FLUSH=1 (the recompiler is told this device has no hardware flush), main with 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 on main only on a device that uses the hardware flush.
    • Not run on lavapipe or any other host.
  • Full ^(agc_|shader|vector_), 4 at a time: 1,647 tests: 1,628 passed, 18 skipped, 1 failed.
    • Against main: the same run on main (1,646 tests) gives every test the same outcome. The only extra test is the new one, which passes.
    • The failure: agc_driver_sample_order_grid_reference fails on main too, 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).
  • Validation layer (1.4.367): the 49 tests matching -R "^(agc_driver_(f32_|fma_|mac_|mullit|nan_|div_fmas|f16_fma|f64_fma|float16_fma|vcmp_f32).*|agc_driver_.*(denormal|_nan).*)$" (48 on main, which lacks the new one), one at a time with ctest -V, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1.
    • Layer: no log has was not found but was requested by env var VK_INSTANCE_LAYERS, and every test logs the layer insertion.
    • Result: no VUID- line on either side.
    • Skipped: agc_driver_float16_unused_nan_payloads skips 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: f32DenormalFlushFor is the function #17685 reports GCC 14 miscompiling, and this change was not built with GCC 14.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • 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: AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5263684270 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18469). Originally posted by [angrysandhill](https://github.com/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 `0xc0` most 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 NaN `0xffc00000`, also when the addend is a NaN. On a host that uses the hardware f32 flush (`DenormFlushToZero 32` where the driver offers it, the driver's default on NVIDIA and MoltenVK), `v_fma_f32`, `v_fmac_f32`, `v_fmamk_f32` and `v_fmaak_f32` returned the NaN addend instead: - `f32DenormalFlushFor` leaves the flush of these instructions to the host, so the translation does not flush their factors. - `invalidProductF32` then tested the unflushed factor for an exact zero, missed the invalid product, and `nanResultF32` picked 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 by `v_fma_mix_f32`. So the two translations of one instruction disagreed. The change: - **`f32DenormalFlushFor`:** returns `4` instead of `0` when 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.md` already 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.md` is 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: - #18285 lists this difference as the four rows it leaves in its `0xc0` comparison. - #17588 makes the start-up flush probe compare NaN bits. The probe has no row with an infinite factor and a NaN addend, so its outcome is the same with and without this change. - #4287 and #17733 restructure `nanResultF32` and 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 on `main` `afe2226017` and 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/HW_ORACLE.md), HIP runtime), compared row by row with the recompiler through `HW_ORACLE_RUNTIME=vulkan`. - **Rows:** 288 per instruction. One factor is ±the smallest or ±the largest denormal, ±0, the smallest normal or 1; the other is ±infinity or 1; the addend is a quiet NaN, a signalling NaN, a negative NaN with a payload, 1, +0 or −infinity. Both factor orders. The literal forms take one factor or the addend from the literal. - **Modes:** IEEE 0 and 1, each with the f32 denormal field at 0, 1, 2 and 3. Wave32, round to nearest even, DX10_CLAMP 1, FP16_OVFL 0, f16 denormals kept. Rows differing from RDNA2 with the f32 denormal field at 0, the same at IEEE 0 and IEEE 1: | | `main` | this PR | |---|---|---| | `v_fma_f32`, `v_fmac_f32` | 48 of 288 each | 0 | | `v_fmamk_f32` with a denormal literal | 48 of 288 | 0 | | `v_fmamk_f32` with an infinite literal | 36 of 288 | 0 | | `v_fmaak_f32` with a NaN literal | 96 of 288 | 0 | | `v_fma_f32` with `neg` and `abs`, VOP3 `v_fmac_f32` | 48 of 288 each | 0 | | `v_fma_mix_f32` with f32 sources, `v_mul_f32`, `v_mul_legacy_f32` | 0 | 0 | - Every differing row is a denormal factor times ±infinity with a NaN addend: RDNA2 gives `0xffc00000`, `main` gives the addend (quieted with IEEE 1). - With the f32 denormal field at 1, 2 or 3, no row of the table differs on `main` or with this change. - `main` with `APS5_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. - The same sweeps include `v_fma_f16` and `v_fmac_f16` (with the f16 denormal field at 0 to 3) and `v_fma_mix_f32` with 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 on `main` and with this change. **Tests:** - **New `agc_driver_f32_fma_flushed_invalid_product`:** 32 rows through 8 forms: `v_fma_f32`, `v_fmac_f32`, `v_fmamk_f32` with an infinite and with a denormal literal, `v_fmaak_f32` with a NaN literal, `v_fma_f32` with `neg` and `abs`, VOP3 `v_fmac_f32` and `v_fma_mix_f32`. The expected values are RDNA2's. - **Runs:** no float mode, `0xc0` IEEE 0 and `0xc0` IEEE 1, with the emulated flush and, where `Target()` uses it, with the hardware flush; each also with `fusedExactArithmetic` forced. The last line says how many of the four passes ran. - **On `main` with 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. - **With this change** it passes. - **On a device without the hardware flush**, such as CI's lavapipe, only the emulated passes run. This change does not alter their translation, so there the test does not tell `main` from this branch. Here, with `APS5_NO_HW_F32_FLUSH=1` (the recompiler is told this device has no hardware flush), `main` with 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 on `main` only on a device that uses the hardware flush. - **Not run** on lavapipe or any other host. - **Full `^(agc_|shader|vector_)`, 4 at a time:** 1,647 tests: 1,628 passed, 18 skipped, 1 failed. - **Against `main`:** the same run on `main` (1,646 tests) gives every test the same outcome. The only extra test is the new one, which passes. - **The failure:** `agc_driver_sample_order_grid_reference` fails on `main` too, 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). - **Validation layer** (1.4.367): the 49 tests matching `-R "^(agc_driver_(f32_|fma_|mac_|mullit|nan_|div_fmas|f16_fma|f64_fma|float16_fma|vcmp_f32).*|agc_driver_.*(denormal|_nan).*)$"` (48 on `main`, which lacks the new one), one at a time with `ctest -V`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`. - **Layer:** no log has `was not found but was requested by env var VK_INSTANCE_LAYERS`, and every test logs the layer insertion. - **Result:** no `VUID-` line on either side. - **Skipped:** `agc_driver_float16_unused_nan_payloads` skips 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: `f32DenormalFlushFor` is the function #17685 reports GCC 14 miscompiling, and this change was not built with GCC 14. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-12 14:27:03 +00:00 (Migrated from github.com)

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, base main. 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:

repo_read.py search f32DenormalFlush
repo_read.py search invalidProductF32
repo_read.py search flushF32Denormal
repo_read.py search flushesInHardware
repo_read.py search GLOB core/libs/prx/libSceAgcDriver
repo_read.py search "Signaling NaN inputs"
repo_read.py cat core/shader/recompiler/Translation/src/VectorInstructions.cpp 40 120
repo_read.py cat core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp 140 170

Results

Check Result Note
Diff read, 3 of 3 files passed the new test file is read in full through the diff
Readers of the flush value passed only bits 1 and 2 are tested; 4 has neither
Callers of f32DenormalFlushFor passed one, in DispatchInstructions
Test picked up by the build passed CMake globs tests/execution/*.cpp
Build and test run not run no build in this review
Hosted CI for the head not run not checked

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

  • The expected values in the new test and the RDNA2 table are the author's measurements (RX 6750 XT, driver 32.0.21045.16010). I did not reproduce them. Nobody here has measured the PS5's gfx1013.
  • The "fails on main" negative control is author-reported. I did not run it.
  • For OperandAccess.cpp and VectorInstructions.cpp I read the diff and the head excerpts above, not the full texts in files/.
  • flushesInHardware (VectorInstructions.cpp:27) was not read. The claim that v_fma_mix_f32 stays on the emulated path is not confirmed.
  • nanResultF32 was not read. The claim that only denormal-factor × infinity rows change rests on the author's table.
  • The "f32 denormals" entry in shader.md, which the description cites, was not checked. Only "Signaling NaN inputs" was.
  • Hosted checks and mergeability were not checked.

To get this to a good state

Nothing blocking. Before merge: confirm hosted CI on 352f72a9 is green, and, if the new test has not run on the final head, run it on a device that uses the hardware f32 flush.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5876112723 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18469#pullrequestreview-5876112723). Originally posted by [MarcZX10](https://github.com/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`, base `main`. 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: ```sh repo_read.py search f32DenormalFlush repo_read.py search invalidProductF32 repo_read.py search flushF32Denormal repo_read.py search flushesInHardware repo_read.py search GLOB core/libs/prx/libSceAgcDriver repo_read.py search "Signaling NaN inputs" repo_read.py cat core/shader/recompiler/Translation/src/VectorInstructions.cpp 40 120 repo_read.py cat core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp 140 170 ``` ## Results | Check | Result | Note | |---|---|---| | Diff read, 3 of 3 files | passed | the new test file is read in full through the diff | | Readers of the flush value | passed | only bits 1 and 2 are tested; 4 has neither | | Callers of `f32DenormalFlushFor` | passed | one, in `DispatchInstructions` | | Test picked up by the build | passed | CMake globs `tests/execution/*.cpp` | | Build and test run | not run | no build in this review | | Hosted CI for the head | not run | not checked | ## Findings ### Blocking None. ### Non-blocking None. ## Sources - [`f32DenormalFlushFor`](https://github.com/boykopovar/AnyPS5/blob/352f72a9f064f97b7441f79d2aff8f2d8eafd2b0/core/shader/recompiler/Translation/src/VectorInstructions.cpp#L45) - [`flushF32Denormal` and `flushTinyProduct`](https://github.com/boykopovar/AnyPS5/blob/352f72a9f064f97b7441f79d2aff8f2d8eafd2b0/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp#L149-L160) - [Debt entry "Signaling NaN inputs"](https://github.com/boykopovar/AnyPS5/blob/352f72a9f064f97b7441f79d2aff8f2d8eafd2b0/docs/dev/debt/shader.md#L158) - [CMake glob of `tests/execution`](https://github.com/boykopovar/AnyPS5/blob/352f72a9f064f97b7441f79d2aff8f2d8eafd2b0/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L691) ## Not verified - The expected values in the new test and the RDNA2 table are the author's measurements (RX 6750 XT, driver 32.0.21045.16010). I did not reproduce them. Nobody here has measured the PS5's gfx1013. - The "fails on `main`" negative control is author-reported. I did not run it. - For `OperandAccess.cpp` and `VectorInstructions.cpp` I read the diff and the head excerpts above, not the full texts in `files/`. - `flushesInHardware` (`VectorInstructions.cpp:27`) was not read. The claim that `v_fma_mix_f32` stays on the emulated path is not confirmed. - `nanResultF32` was not read. The claim that only denormal-factor × infinity rows change rests on the author's table. - The "f32 denormals" entry in `shader.md`, which the description cites, was not checked. Only "Signaling NaN inputs" was. - Hosted checks and mergeability were not checked. ## To get this to a good state Nothing blocking. Before merge: confirm hosted CI on `352f72a9` is green, and, if the new test has not run on the final head, run it on a device that uses the hardware f32 flush.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!18469
No description provided.