fix(shader): flush v_fma_f16 family denormals by the f16 denormal field #7034

Merged
thuanquang merged 1 commit from upstream-pr/7034 into main 2026-10-17 07:52:18 +00:00
thuanquang commented 2026-10-17 06:44:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-17T06:44:13Z.


What

v_fma_f16, v_fmac_f16, v_fmamk_f16 and v_fmaak_f16 ignored the FLOAT_MODE f16 denormal field; the technical debt entry for the scalar f16 ops listed "ternary arithmetic" as keeping the previous lowering. On the GPU, at f16 round to nearest even:

  • with bit 6 of FLOAT_MODE clear (f16 denormal modes 0 and 2) every denormal input is flushed to a zero of its sign, so v_fma_f16 0, 0, 0x8001 is 0x0000 where main writes 0x8001;
  • with bit 7 clear (modes 0 and 1) the result is flushed to a zero of its sign when the exact product-sum, rounded to 11 significant bits with an unbounded exponent, is below 2^-14, the rule v_mul_f16 already follows. So v_fma_f16 0, 0, 0x0001 is 0x0000 where main writes 0x0001, while 0x0400 * 0x0400 + 0x8400 (exactly -2^-14 + 2^-28) rounds to 0x8400 and is kept. Detecting tininess before rounding instead flushes it to 0x8000 and disagrees with the GPU on 10 (mode 0) and 103 (mode 1) of the 4580 rows without NaN operands.

float16Ternary now flushes the inputs of the f16 FMA path with flushF16InputValue and its round-to-odd f32 result against 2^-14 - 2^-26 (0x387ff000) in the modes that flush them, gated like float16Binary on f16 round to nearest even. The magnitude test moves from a lambda in float16Binary to flushTinyF16Value, which both paths and flushF16InputValue use. Comparing the round-to-odd value is the same as comparing the exact one, since 0x387ff000 has an even significand. v_fma_mixlo/mixhi_f16, directed rounding and shaders without a known float mode are translated as before.

Measured

The same instructions were run directly on the GPU with the tools/hw-oracle template, launched through the driver's HIP runtime, and through the recompiler on Vulkan, at IEEE 0 and 1, over 5075 operand triples: the 4000 from 20 halves around the denormal range (±0, ±0x0001, ±0x03ff, 0x0200, ±0x0400, 0x0401, ±0.5, ±1, 0x7bff, inf, 0x3bff, 0x3c01, 0x1fff, 0x2001), 75 whose product-sum lies just below 2^-14, and 1000 random ones:

f16 denormal mode main vs GPU (v_fma_f16 and v_fmac_f16 each) this change vs GPU
0 2167 / 5075 0 / 5075
1 1461 / 5075 0 / 5075
2 2049 / 5075 0 / 5075
3 0 / 5075 0 / 5075

With this change every mode also matches, at IEEE 0 and 1 and FP16_OVFL 0 and 1, over the first 4075 triples for v_fmamk_f16 with literals 0x8400 and 0x3bff, v_fmaak_f16 with 0x8400, v_fma_f16 -v4, |v5|, -v6, op_sel:[1,1,1,0] high-half sources, op_sel:[0,0,0,1] into a register holding another value, clamp, and VOP3 v_fmac_f16_e64 (0 mismatches each). VOP3 v_fmac_f16_e64 with a source modifier or clamp throws "VOP3 source modifiers are not implemented" in the recompiler; this PR does not touch that path.

Tested

Windows 11 26200, AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, ANYPS5_REQUIRE_VULKAN=1:

  • New agc_driver_f16_fma_denormal_modes: v_fma_f16 and VOP2 v_fmac_f16 over 64 of those triples (inputs and results at the denormal boundary, the triples just below 2^-14, infinities and invalid products), in f16 denormal modes 0 to 3 at IEEE 0 and 1 and without a known float mode, against the GPU's results for each mode. On main it fails: v_fma_f16 f16 denormal mode 0, IEEE mode on of 0x3bff, 0x0400, 0x0000 is 0x0400, expected 0x0000. With this change it passes.
  • Full ctest --parallel 4 --timeout 120 on main 6a32550fd with this change: 1368 tests, 2 fail, both failing on main on this GPU too: agc_driver_lds_mixed_width_atomics (#5271) and agc_driver_layered_volume_vertex_no_gpl (#4768). agc_driver_float16_fma_rounding, agc_driver_f16_denormal_flush and the other f16 tests pass.
  • python tools/check_conventions.py --base origin/main with this title and body passes.
  • Not tested: the PS5's gfx1013, directed f16 rounding, other AMD drivers, NVIDIA, Intel, and titles.

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
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4898014184 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7034). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-17T06:44:13Z. --- ### What `v_fma_f16`, `v_fmac_f16`, `v_fmamk_f16` and `v_fmaak_f16` ignored the `FLOAT_MODE` f16 denormal field; the technical debt entry for the scalar f16 ops listed "ternary arithmetic" as keeping the previous lowering. On the GPU, at f16 round to nearest even: - with bit 6 of `FLOAT_MODE` clear (f16 denormal modes 0 and 2) every denormal input is flushed to a zero of its sign, so `v_fma_f16 0, 0, 0x8001` is `0x0000` where `main` writes `0x8001`; - with bit 7 clear (modes 0 and 1) the result is flushed to a zero of its sign when the exact product-sum, rounded to 11 significant bits with an unbounded exponent, is below 2^-14, the rule `v_mul_f16` already follows. So `v_fma_f16 0, 0, 0x0001` is `0x0000` where `main` writes `0x0001`, while `0x0400 * 0x0400 + 0x8400` (exactly -2^-14 + 2^-28) rounds to `0x8400` and is kept. Detecting tininess before rounding instead flushes it to `0x8000` and disagrees with the GPU on 10 (mode 0) and 103 (mode 1) of the 4580 rows without NaN operands. `float16Ternary` now flushes the inputs of the f16 FMA path with `flushF16InputValue` and its round-to-odd f32 result against 2^-14 - 2^-26 (`0x387ff000`) in the modes that flush them, gated like `float16Binary` on f16 round to nearest even. The magnitude test moves from a lambda in `float16Binary` to `flushTinyF16Value`, which both paths and `flushF16InputValue` use. Comparing the round-to-odd value is the same as comparing the exact one, since `0x387ff000` has an even significand. `v_fma_mixlo/mixhi_f16`, directed rounding and shaders without a known float mode are translated as before. ### Measured The same instructions were run directly on the GPU with the `tools/hw-oracle` template, launched through the driver's HIP runtime, and through the recompiler on Vulkan, at IEEE 0 and 1, over 5075 operand triples: the 4000 from 20 halves around the denormal range (±0, ±0x0001, ±0x03ff, 0x0200, ±0x0400, 0x0401, ±0.5, ±1, 0x7bff, inf, 0x3bff, 0x3c01, 0x1fff, 0x2001), 75 whose product-sum lies just below 2^-14, and 1000 random ones: | f16 denormal mode | `main` vs GPU (`v_fma_f16` and `v_fmac_f16` each) | this change vs GPU | |---|---|---| | 0 | 2167 / 5075 | 0 / 5075 | | 1 | 1461 / 5075 | 0 / 5075 | | 2 | 2049 / 5075 | 0 / 5075 | | 3 | 0 / 5075 | 0 / 5075 | With this change every mode also matches, at IEEE 0 and 1 and `FP16_OVFL` 0 and 1, over the first 4075 triples for `v_fmamk_f16` with literals `0x8400` and `0x3bff`, `v_fmaak_f16` with `0x8400`, `v_fma_f16 -v4, |v5|, -v6`, `op_sel:[1,1,1,0]` high-half sources, `op_sel:[0,0,0,1]` into a register holding another value, `clamp`, and VOP3 `v_fmac_f16_e64` (0 mismatches each). VOP3 `v_fmac_f16_e64` with a source modifier or `clamp` throws "VOP3 source modifiers are not implemented" in the recompiler; this PR does not touch that path. ### Tested Windows 11 26200, AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, `ANYPS5_REQUIRE_VULKAN=1`: - New `agc_driver_f16_fma_denormal_modes`: `v_fma_f16` and VOP2 `v_fmac_f16` over 64 of those triples (inputs and results at the denormal boundary, the triples just below 2^-14, infinities and invalid products), in f16 denormal modes 0 to 3 at IEEE 0 and 1 and without a known float mode, against the GPU's results for each mode. On `main` it fails: `v_fma_f16 f16 denormal mode 0, IEEE mode on of 0x3bff, 0x0400, 0x0000 is 0x0400, expected 0x0000`. With this change it passes. - Full `ctest --parallel 4 --timeout 120` on `main` `6a32550fd` with this change: 1368 tests, 2 fail, both failing on `main` on this GPU too: `agc_driver_lds_mixed_width_atomics` (#5271) and `agc_driver_layered_volume_vertex_no_gpl` (#4768). `agc_driver_float16_fma_rounding`, `agc_driver_f16_denormal_flush` and the other f16 tests pass. - `python tools/check_conventions.py --base origin/main` with this title and body passes. - Not tested: the PS5's gfx1013, directed f16 rounding, other AMD drivers, NVIDIA, Intel, and titles. ### 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] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-17 06:56:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T06:56:47Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 5782 docs/dev/debt/shader.md L95-96

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.

Pull request Shared files not compared
PR 4072 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
core/shader/recompiler/Translation/src/FloatInstructions.cpp
PR 5021 core/shader/recompiler/Translation/src/FloatInstructions.cpp
docs/dev/debt/shader.md
PR 5371 docs/dev/debt/shader.md
PR 5426 docs/dev/debt/shader.md
PR 5500 docs/dev/debt/shader.md
PR 6222 docs/dev/debt/shader.md
PR 6235 docs/dev/debt/shader.md
PR 6412 docs/dev/debt/shader.md
PR 6503 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6230934621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7034#issuecomment-6230934621). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T06:56:47Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 5782](https://www.github.com/boykopovar/AnyPS5/pull/5782) | `docs/dev/debt/shader.md` [L95-96](https://github.com/boykopovar/AnyPS5/blob/3b10c7ef0915d50a8bc442b8c0980aa7ec4d00be/docs/dev/debt/shader.md#L95-L96) | | | 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. | Pull request | Shared files not compared | |---|---| | [PR 4072](https://www.github.com/boykopovar/AnyPS5/pull/4072) | `core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp`<br>`core/shader/recompiler/Translation/src/FloatInstructions.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/shader/recompiler/Translation/src/FloatInstructions.cpp`<br>`docs/dev/debt/shader.md` | | [PR 5371](https://www.github.com/boykopovar/AnyPS5/pull/5371) | `docs/dev/debt/shader.md` | | [PR 5426](https://www.github.com/boykopovar/AnyPS5/pull/5426) | `docs/dev/debt/shader.md` | | [PR 5500](https://www.github.com/boykopovar/AnyPS5/pull/5500) | `docs/dev/debt/shader.md` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `docs/dev/debt/shader.md` | | [PR 6235](https://www.github.com/boykopovar/AnyPS5/pull/6235) | `docs/dev/debt/shader.md` | | [PR 6412](https://www.github.com/boykopovar/AnyPS5/pull/6412) | `docs/dev/debt/shader.md` | | [PR 6503](https://www.github.com/boykopovar/AnyPS5/pull/6503) | `core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp` |
LeandroLP23 commented 2026-10-17 08:08:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-17T08:08:59Z.


Another AMD driver, since the description lists other AMD drivers as untested.

Windows 11 (26300), Radeon 860M (RDNA 3.5, 1002:1114), driver 32.0.13058.8001, WinLibs GCC 15.2.0, Release, ANYPS5_REQUIRE_VULKAN=1. Tree: main 17cfb1f4c with #7018, #7034, #7036, #7038, #7040, #7041 and #7042 merged together (only docs/dev/debt/shader.md conflicted, for #7040); "main" is the same tree with the six recompiler files reverted to main and the tests kept.

  • agc_driver_f16_fma_denormal_modes: fails on main, passes 3 of 3 with this change.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6231564583 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7034#issuecomment-6231564583). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-17T08:08:59Z. --- Another AMD driver, since the description lists other AMD drivers as untested. Windows 11 (26300), Radeon 860M (RDNA 3.5, `1002:1114`), driver 32.0.13058.8001, WinLibs GCC 15.2.0, Release, `ANYPS5_REQUIRE_VULKAN=1`. Tree: `main` `17cfb1f4c` with #7018, #7034, #7036, #7038, #7040, #7041 and #7042 merged together (only `docs/dev/debt/shader.md` conflicted, for #7040); "`main`" is the same tree with the six recompiler files reverted to `main` and the tests kept. - `agc_driver_f16_fma_denormal_modes`: fails on `main`, passes 3 of 3 with this change. AI-assisted: yes (Claude Code).
Amoenus commented 2026-10-17 08:31:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Amoenus on 2026-10-17T08:31:59Z.


Confirmed a cross-vendor test failure on unchanged main adb0e7a8cb, without our integration patches:

  • Windows 11 Enterprise 26200, WinLibs GCC15.2r7, Release; ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1.
  • Actual RTX5080 (10de:2c02), driver32.0.16.1062: agc_driver_f16_fma_denormal_modes fails 3/3 fresh processes, each under0.4s, at the same first assertion:
AGC graphics: v_fma_f16 without a known float mode of 0x7c00, 0x0000, 0x0000 is 0x7fff, expected 0xfe00
  • Actual Radeon(TM) Graphics (1002:13c0), driver32.0.21036.18, same build: 3/3 pass, each under0.5s. Device identity is checked in each process log.

Built agc_driver_f16_fma_denormal_modes_tests on the clean detached main, then ran ctest -V --no-tests=error --timeout 30 -R '^agc_driver_f16_fma_denormal_modes$' with ANYPS5_GPU set to each adapter. Both observed bit patterns are NaNs, but this alone does not settle the required payload/sign semantics or justify weakening the assertion. This reports the test's cross-vendor failure; it does not establish a denormal-semantics regression or a cause of our title's separate DCC/Vulkan stops. No source changes or proposed NaN fix in this report. AI-assisted: yes (Codex); author responsible.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6231777019 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7034#issuecomment-6231777019). Originally posted by [Amoenus](https://github.com/Amoenus) on 2026-10-17T08:31:59Z. --- <!-- codex-f16-fma-nvidia-control-v1 --> Confirmed a cross-vendor test failure on **unchanged main `adb0e7a8cb`**, without our integration patches: - Windows 11 Enterprise 26200, WinLibs GCC15.2r7, Release; `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`. - Actual RTX5080 (`10de:2c02`), driver32.0.16.1062: `agc_driver_f16_fma_denormal_modes` fails **3/3 fresh processes**, each under0.4s, at the same first assertion: ```text AGC graphics: v_fma_f16 without a known float mode of 0x7c00, 0x0000, 0x0000 is 0x7fff, expected 0xfe00 ``` - Actual Radeon(TM) Graphics (`1002:13c0`), driver32.0.21036.18, same build: **3/3 pass**, each under0.5s. Device identity is checked in each process log. Built `agc_driver_f16_fma_denormal_modes_tests` on the clean detached main, then ran `ctest -V --no-tests=error --timeout 30 -R '^agc_driver_f16_fma_denormal_modes$'` with `ANYPS5_GPU` set to each adapter. Both observed bit patterns are NaNs, but this alone does not settle the required payload/sign semantics or justify weakening the assertion. This reports the test's cross-vendor failure; it does not establish a denormal-semantics regression or a cause of our title's separate DCC/Vulkan stops. No source changes or proposed NaN fix in this report. AI-assisted: yes (Codex); author responsible. <!-- /codex-f16-fma-nvidia-control-v1 -->
screamm commented 2026-10-17 11:42:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T11:42:17Z.


Second NVIDIA data point, with the bit pattern explained: RTX 4070 (driver 596.21, Windows 11 Pro 26220, WinLibs GCC 15.2) on plain main ca82d7d88 fails 3 of 3 with the same message, and so does agc_driver_fma_mix_f16_denormal_modes from #7040 (is 0x7fffffff, expected 0xffc00000). Both are inf * 0 cases: the table holds the AMD default NaN (sign set), NVIDIA returns its canonical NaN (sign clear). Details in #7100.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233472318 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7034#issuecomment-6233472318). Originally posted by [screamm](https://github.com/screamm) on 2026-10-17T11:42:17Z. --- Second NVIDIA data point, with the bit pattern explained: RTX 4070 (driver 596.21, Windows 11 Pro 26220, WinLibs GCC 15.2) on plain `main` ca82d7d88 fails 3 of 3 with the same message, and so does `agc_driver_fma_mix_f16_denormal_modes` from #7040 (`is 0x7fffffff, expected 0xffc00000`). Both are inf * 0 cases: the table holds the AMD default NaN (sign set), NVIDIA returns its canonical NaN (sign clear). Details in #7100. Written with Claude Code assistance (validation by a subagent, reviewed in this session).
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!7034
No description provided.