fix(shader): translate the fused f32 fma forms in f32 denormal mode 2 #12253

Merged
Michael-KD merged 1 commit from upstream-pr/12253 into main 2026-10-28 11:32:39 +00:00
Michael-KD commented 2026-10-28 05:06:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Michael-KD on 2026-10-28T05:06:19Z.


What

v_fma_f32, v_fmac_f32, v_fmamk_f32 and v_fmaak_f32 threw f32 denormal mode 2 at pc P is not implemented when a shader's FLOAT_MODE flushed f32 denormal inputs and kept denormal results (denormal field 2). This translates mode 2 at round to nearest even, as measured on RDNA2:

  • Mode 2 flushes denormal inputs to zeros of their sign and rounds the exact a * b + c once, with gradual underflow. That is the host's Fma of the flushed inputs under DenormPreserve 32, so mode 2 throws needs DenormPreserve 32 without it, as v_add_f32 does.
  • Mode 1 keeps throwing. There the hardware keeps the inputs and flushes a result that is tiny after rounding to 24 bits with an unbounded exponent, also when the addend is nonzero. The rounded fused result does not show that: 0x807fffff * 0x3f7fffff + 0x80000001 gives 0x80000000 on hardware, while the fused result is 0x80800000. Implementing it needs the exact residual of the fused operation, so it is recorded in the new debt entry instead.
  • The separately rounded v_mac_f32, v_madmk_f32 and v_madak_f32 share the decoder opcodes and still throw in both modes; RDNA2 cannot run them.

Changes:

  • VectorInstructions.cpp: f32DenormalFlushFor returns the mode 2 flush for the fused forms at round mode 0.
  • F32FmaDenormalMode2 (new execution test): 64 of the measured triples with the hardware's results for the four forms (v_fmamk_f32 with factor 0x3f7fffff, v_fmaak_f32 with addend 0x80000001) in mode 2 at IEEE 0 and 1. It is skipped on a device without DenormPreserve 32.
  • docs/dev/debt/shader.md: a new entry for the four forms in modes 1 and 2.

Measurement, with the hardware oracle:

  • GPU: AMD Radeon integrated graphics of a Ryzen 7 9800X3D (gfx1036, RDNA2), AMD Windows driver 32.0.21030.31, through HIP/comgr; wave32; round to nearest even, DX10_CLAMP 1, f16 denormals kept, FP16_OVFL 0.
  • Kernel: v_fma_f32 v10, v4, v5, v6, v_fmac_f32 v11, v4, v5 on a copy of v6, v_fmamk_f32 v12, v4, 0x3f7fffff, v6 and v_fmaak_f32 v13, v4, v5, 0x80000001.
  • Triples: all 4,096 of 16 values (±0, 0x00000001, 0x807fffff, 0x00400000, 2^-126, 0x80800001, ±1, 1 - 2^-24, 0.5, 2^-64, 0x9fffffff, 2^-63, infinity, a NaN), and 1,500 random triples with factors of random sign in [2^-67, 2^-59) and a signed zero or random denormal addend: 5,596. Each was run at IEEE 0 and 1 in every f32 denormal mode.
  • Mode 2: the exact fused result of the flushed inputs, rounded to nearest even with gradual underflow, matches every v_fma_f32 result whose inputs are finite and whose exact value is nonzero (computed with Python's fractions). Mode 2 differs from mode 0 on 733 v_fma_f32 rows, and mode 0 gives a zero on every one of them.
  • Mode 1: of the 1,137 v_fma_f32 rows where it differs from mode 3, 1,129 are mode 3's result flushed. The other 8 are tiny products with a zero addend, such as 0x00800000 * 0x3f7fffff + 0, which gives 0 where the fused result is 0x00800000. v_fmaak_f32, whose addend is the denormal 0x80000001, has 32 rows like the example above.

The translation, run with all 5,596 triples and the hardware's mode 2 results in place of the test's 64 (a local variant of the new test, not committed), gives 0 mismatches for the four forms at IEEE 0 and 1 on the Radeon iGPU. The RTX 5070 Ti offers no DenormPreserve 32, so mode 2 is refused there and the new test runs no mode.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release.

ctest --test-dir build -R '^agc_driver_(f32_fma_denormal_mode2|f32_denormal_flush|division|nan_quieting|f32_add_mul_denormal_modes)$' -V with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Vulkan SDK 1.4.363.0, insertion confirmed with VK_LOADER_DEBUG=layer) and ANYPS5_GPU set to each device. The main arm is the merge base's VectorInstructions.cpp with the new test added:

Test main, RTX 5070 Ti main, Radeon iGPU this PR, RTX 5070 Ti this PR, Radeon iGPU
agc_driver_f32_fma_denormal_mode2 Failed Failed Passed (0 of 2 modes, no DenormPreserve 32) Passed (2 of 2 modes)
agc_driver_division Passed Passed Passed Passed
agc_driver_f32_add_mul_denormal_modes Passed Passed Passed Passed
agc_driver_f32_denormal_flush Passed Passed Passed Passed
agc_driver_nan_quieting Passed Passed Passed Passed

On main the new test fails on both devices with FLOAT_MODE 0x000000e0: ShaderRecompiler::Recompile: f32 denormal mode 2 at pc 16 is not implemented. No log contains was not found but was requested by env var VK_INSTANCE_LAYERS, no log has a VUID- line, and each log names the selected GPU.

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: NVIDIA GeForce RTX 5070 Ti, driver 617.42, Windows 11 Pro 26200; AMD Radeon Graphics (Ryzen 7 9800X3D, gfx1036), Windows driver 32.0.21030.31 (Vulkan driver 2.0.353), Windows 11 Pro 26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5045508454 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12253). Originally posted by [Michael-KD](https://github.com/Michael-KD) on 2026-10-28T05:06:19Z. --- ### What `v_fma_f32`, `v_fmac_f32`, `v_fmamk_f32` and `v_fmaak_f32` threw `f32 denormal mode 2 at pc P is not implemented` when a shader's `FLOAT_MODE` flushed f32 denormal inputs and kept denormal results (denormal field 2). This translates mode 2 at round to nearest even, as measured on RDNA2: - Mode 2 flushes denormal inputs to zeros of their sign and rounds the exact `a * b + c` once, with gradual underflow. That is the host's `Fma` of the flushed inputs under `DenormPreserve 32`, so mode 2 throws `needs DenormPreserve 32` without it, as `v_add_f32` does. - Mode 1 keeps throwing. There the hardware keeps the inputs and flushes a result that is tiny after rounding to 24 bits with an unbounded exponent, also when the addend is nonzero. The rounded fused result does not show that: `0x807fffff * 0x3f7fffff + 0x80000001` gives `0x80000000` on hardware, while the fused result is `0x80800000`. Implementing it needs the exact residual of the fused operation, so it is recorded in the new debt entry instead. - The separately rounded `v_mac_f32`, `v_madmk_f32` and `v_madak_f32` share the decoder opcodes and still throw in both modes; RDNA2 cannot run them. Changes: - `VectorInstructions.cpp`: `f32DenormalFlushFor` returns the mode 2 flush for the fused forms at round mode 0. - `F32FmaDenormalMode2` (new execution test): 64 of the measured triples with the hardware's results for the four forms (`v_fmamk_f32` with factor `0x3f7fffff`, `v_fmaak_f32` with addend `0x80000001`) in mode 2 at IEEE 0 and 1. It is skipped on a device without `DenormPreserve 32`. - `docs/dev/debt/shader.md`: a new entry for the four forms in modes 1 and 2. Measurement, with the [hardware oracle](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/HW_ORACLE.md): - GPU: AMD Radeon integrated graphics of a Ryzen 7 9800X3D (gfx1036, RDNA2), AMD Windows driver 32.0.21030.31, through HIP/comgr; wave32; round to nearest even, DX10_CLAMP 1, f16 denormals kept, FP16_OVFL 0. - Kernel: `v_fma_f32 v10, v4, v5, v6`, `v_fmac_f32 v11, v4, v5` on a copy of v6, `v_fmamk_f32 v12, v4, 0x3f7fffff, v6` and `v_fmaak_f32 v13, v4, v5, 0x80000001`. - Triples: all 4,096 of 16 values (±0, `0x00000001`, `0x807fffff`, `0x00400000`, 2^-126, `0x80800001`, ±1, 1 - 2^-24, 0.5, 2^-64, `0x9fffffff`, 2^-63, infinity, a NaN), and 1,500 random triples with factors of random sign in [2^-67, 2^-59) and a signed zero or random denormal addend: 5,596. Each was run at IEEE 0 and 1 in every f32 denormal mode. - Mode 2: the exact fused result of the flushed inputs, rounded to nearest even with gradual underflow, matches every `v_fma_f32` result whose inputs are finite and whose exact value is nonzero (computed with Python's `fractions`). Mode 2 differs from mode 0 on 733 `v_fma_f32` rows, and mode 0 gives a zero on every one of them. - Mode 1: of the 1,137 `v_fma_f32` rows where it differs from mode 3, 1,129 are mode 3's result flushed. The other 8 are tiny products with a zero addend, such as `0x00800000 * 0x3f7fffff + 0`, which gives 0 where the fused result is `0x00800000`. `v_fmaak_f32`, whose addend is the denormal `0x80000001`, has 32 rows like the example above. The translation, run with all 5,596 triples and the hardware's mode 2 results in place of the test's 64 (a local variant of the new test, not committed), gives 0 mismatches for the four forms at IEEE 0 and 1 on the Radeon iGPU. The RTX 5070 Ti offers no `DenormPreserve 32`, so mode 2 is refused there and the new test runs no mode. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. `ctest --test-dir build -R '^agc_driver_(f32_fma_denormal_mode2|f32_denormal_flush|division|nan_quieting|f32_add_mul_denormal_modes)$' -V` with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (Vulkan SDK 1.4.363.0, insertion confirmed with `VK_LOADER_DEBUG=layer`) and `ANYPS5_GPU` set to each device. The `main` arm is the merge base's `VectorInstructions.cpp` with the new test added: | Test | `main`, RTX 5070 Ti | `main`, Radeon iGPU | this PR, RTX 5070 Ti | this PR, Radeon iGPU | |---|---|---|---|---| | `agc_driver_f32_fma_denormal_mode2` | Failed | Failed | Passed (0 of 2 modes, no `DenormPreserve 32`) | Passed (2 of 2 modes) | | `agc_driver_division` | Passed | Passed | Passed | Passed | | `agc_driver_f32_add_mul_denormal_modes` | Passed | Passed | Passed | Passed | | `agc_driver_f32_denormal_flush` | Passed | Passed | Passed | Passed | | `agc_driver_nan_quieting` | Passed | Passed | Passed | Passed | On `main` the new test fails on both devices with `FLOAT_MODE 0x000000e0: ShaderRecompiler::Recompile: f32 denormal mode 2 at pc 16 is not implemented`. No log contains `was not found but was requested by env var VK_INSTANCE_LAYERS`, no log has a `VUID-` line, and each log names the selected GPU. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: NVIDIA GeForce RTX 5070 Ti, driver 617.42, Windows 11 Pro 26200; AMD Radeon Graphics (Ryzen 7 9800X3D, gfx1036), Windows driver 32.0.21030.31 (Vulkan driver 2.0.353), Windows 11 Pro 26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-28 08:59:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T08:59:27Z.

Review: APPROVED


The mode-2 fused FMA path is constrained to round-to-nearest-even and explicitly requires DenormPreserve 32; the separately rounded forms retain their existing path. The implementation is backed by RDNA2 oracle results and execution vectors for all four fused forms, while the debt entry records mode 1 and unsupported separately rounded forms. The reported same-GPU validation-layer runs and current Windows/relinker CI pass. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5680082586 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12253#pullrequestreview-5680082586). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T08:59:27Z. **Review:** APPROVED --- The mode-2 fused FMA path is constrained to round-to-nearest-even and explicitly requires `DenormPreserve 32`; the separately rounded forms retain their existing path. The implementation is backed by RDNA2 oracle results and execution vectors for all four fused forms, while the debt entry records mode 1 and unsupported separately rounded forms. The reported same-GPU validation-layer runs and current Windows/relinker CI pass. No blocking issue found.
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!12253
No description provided.