Skip to content

Translator: --fma exact, a single-rounded f32 FMA without VK_KHR_shader_fma (SHADER-497) - #3004

Open
AmazingMoaaz wants to merge 2 commits into
ItsNotRexed:mainfrom
AmazingMoaaz:shader-497-exact-fma
Open

AmazingMoaaz wants to merge 2 commits into
ItsNotRexed:mainfrom
AmazingMoaaz:shader-497-exact-fma

Conversation

@AmazingMoaaz

Copy link
Copy Markdown
Contributor

SHADER-497 (claimed in #2716 (comment); finding in #2516 (comment)).

What and why

The Intel UHD Graphics driver (Xe-LP, Tiger Lake-H, Windows 101.7088) splits GLSL.std.450 Fma into two roundings and has no VK_KHR_shader_fma. So the default --fma glsl isn't exact there, and --fma khr can't be used. On main, check.py's cs_fma_fused fails on that device: words 0, 2, 3 and 5 (v_fma_f32, v_fmaak_f32, v_fmac_f32) come out split. #2211's cs_w32_fma_mix fails word 1 the same way.

This adds --fma exact (Gen.fma_exact), a single-rounded f32 multiply-add built from NoContraction f32 operations and needing no extension. It follows Boldo and Melquiond, "Emulation of a FMA and correctly-rounded sums: proved algorithms using rounding to odd" (IEEE Transactions on Computers 57(4), 2008):

  • Dekker's exact product p + pe = x * y. The factors are split into 12-bit halves by masking their low 12 mantissa bits, so the split can't overflow.
  • A TwoSum th + tl = z + p.
  • v = tl + pe, rounded to odd as fma_f16_odd already does.
  • One final rounding th + v, or th when v is zero, so a -0 sum stays -0.

The construction needs every intermediate in the normal range. So operands are first scaled by powers of two, which is exact, and the result is scaled back:

  • By 2^-64 when the product or the addend is at least 2^126, which covers a product that overflows while the addend brings the sum back.
  • By 2^96 when both are tiny.
  • A factor below 2^-100 trades 2^64 with one above 2^-20, so the split halves stay normal on a driver that flushes f32 denormals.

Infinite or NaN operands, and a finite product beyond even the scaled range (the result is ±inf), take the plain Fma.

Every emit_fma caller gets it: v_fma_f32, v_fmac_f32, v_fmaak_f32, v_fmamk_f32, and v_fma_mix* including fma_mix_odd's product error. It costs about 110 instructions per multiply-add, so it's opt-in: the default stays --fma glsl. build.py and run_gpu_tests.py accept LEONIDA_FMA=exact. docs/design.md records the rule and how it was confirmed, and docs/testing.md no longer says no tested driver splits Fma.

Not exact, and documented: a result in the denormal range (it's scaled back with a second rounding), and denormal operands, which follow the driver's denormal mode like every f32 operation here (the translator emits no DenormPreserve).

Tested

  • tests/semantic/test_fma_exact.py (new, 9 tests). It runs the emitted instructions in the interpreter with its Fma split into two roundings, as on the Intel driver, so only the emulation can be exact. It compares 700 random triples (plain, cancelling, near-tie, tiny, huge, overflow-cancel, wide-exponent) and 96 edge triples with the exact rational reference (si.fma_f32), plus signed zeros, inf/NaN operands, every FMA form including v_fma_mix_f32, and spirv-val. Negative control: the same harness gives --fma glsl the split words the Intel UHD gave (0x00000000, 0x3a000000).
  • A Python model of the same steps matched the exact reference in 399,989 of 399,989 triples with normal operands and a normal or zero result, with f32 denormals both kept and flushed (the scaling and factor balancing are what make the flushed case exact).
  • GPU, check.py with LEONIDA_FMA=exact at this branch (rebased on main ec8ec4c), on Windows 11 Pro build 26300:
    • Intel UHD Graphics (Xe-LP, driver 101.7088): cs_fma_fused ok; 124 of 167 pass, 43 skipped, 0 failed (with glsl on the same build: 123 of 167, cs_fma_fused the only failure).
    • NVIDIA RTX 3050 Laptop GPU (driver 617.42): cs_fma_fused ok; 132 of 167 pass, 35 skipped, 0 failed.
    • Tests: GPU test for v_fma_mix_f32, v_fma_mixlo_f16 and v_fma_mixhi_f16 #2211's cs_w32_fma_mix (its tree, with this translator): passes with exact on both GPUs; with glsl the Intel UHD fails word 1 (0x3a000000).
  • python -m unittest discover -s tests/semantic: 4146 tests OK. python -m compileall -q make_game.py tools tests, python -I make_game.py --help: ok.

Built with scripts\build_runtime.ps1 (MSVC 19.51), llvm-mc 23.1.6, Vulkan SDK 1.4.363.

AI-assisted.

Copilot AI balanced review requested due to automatic review settings November 22, 2026 19:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Doffi4

Doffi4 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Independent Windows 11 (26200) check of abc45822 on Radeon 780M (26.11.2) and RTX 5060 Laptop (621.01). Ran the SGPR-fed cs_fma_fused fixture through the Vulkan compute harness and compared the binary readback against all eight expected words. Both GPUs matched all eight for exact, glsl, and khr. The shader modules passed spirv-val; ps_fma_literal and ps_fmac_f16_mods also matched all 4,096 pixels in all six GPU/mode runs. The nine test_fma_exact.py cases passed, including the simulated split-FMA negative control. Neither of these GPU drivers exhibited the Xe-LP split-FMA behavior. Synthetic inputs only. AI-assisted testing and reporting.

@Etoile-Bleu

Copy link
Copy Markdown
Contributor

Tested abc4582 on NVIDIA GeForce RTX 5070 Ti, driver 621.24, Windows 11 (10.0.26200), llvm-mc 21.1.8, Vulkan SDK 1.4.363.0: pass.

  • python -m unittest discover -s tests/semantic: 4146 tests OK; tests/semantic/test_fma_exact.py 9/9.
  • cs_fma_fused (build.py + check.py) is ok with LEONIDA_FMA=glsl, khr and exact. python -I scripts/run_gpu_tests.py with LEONIDA_FMA=exact: 143/189 passed, 0 failed, 46 skipped; no FMAKHR in the index. This card has a correct Fma, so this checks that the emulation does not break a device that does not need it, not the Intel case (already covered above).
  • Independent fuzz of the emitted v_fma_f32 through the interpreter with its Fma replaced by a two-rounding multiply-add (as on the Intel driver) and the exact rational result as reference, with families that test_fma_exact.py does not use: 14,039 triples, namely cancellation to within 4 ulp of -a*b with exponents up to +-60 (2,500), near-tie sums built from the product's rounding error (1,500), few-bit mantissas (1,500), operands up to 2^63 and addends up to 2^127 (1,500), a tiny factor against a huge one (1,500), a product that overflows f32 while c brings the sum back to a finite result (31), and the 8 sign/order variants of 0xBE800001 * 0x3E000001 + 0x3E400000 (= 0x3E1FFFFF; it needs the round-to-odd step: rounding the product first gives the tie 0x3E200000). All match bit for bit except 6 triples (of 1,000 built to land there plus 1,500 tiny-product ones), and in all 6 the exact result is below 2^-126, which the PR documents as not exact. 0 mismatches with a normal result. The same triples with --fma glsl on that two-rounding Fma give 4,053 mismatches, so the harness does see the problem.

No game files used. Run with AI assistance.

@crastinator

Copy link
Copy Markdown
Contributor

Tested abc4582 merged into main 16282101 (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • Whole run_gpu_tests.py: 175/189 native, 175/189 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 37/37.
  • cs_fma_fused on RADV, built with LEONIDA_FMA=glsl|khr|exact and run with VKC_SGPRS=0x3f800800,0x3f800001,0x3f7ffffe,0xbf800002. The expected words are 33800000 a8800000 3a000400 33800000 33800000 33800000 28800000 00007351:
  • The whole run_gpu_tests.py with LEONIDA_FMA=exact: 175/189, the same as the default.
  • tests/semantic/test_fma_exact.py: 9 passed, 801 subtests passed.

No game files. AI-assisted (Claude Code).

AmazingMoaaz added a commit to AmazingMoaaz/Leonida that referenced this pull request Nov 22, 2026
@AmazingMoaaz
AmazingMoaaz requested an adaptive review from Copilot November 22, 2026 19:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

AmazingMoaaz added a commit to AmazingMoaaz/Leonida that referenced this pull request Nov 22, 2026

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on the PR head: tests/semantic/test_fma_exact.py -> 9 tests OK; full semantic 4,146 passed / 48,105 subtests / 0 failed. The construction is the expected 12-bit Dekker split with Knuth TwoSum, round-to-odd and a final th+v, with every OpFMul/FAdd/FSub in no_contraction() so a driver cannot re-fuse it. An independent Fraction/integer-RNE fuzz over 14,738 triples gives 0 mismatches (625 denormal-range results excluded as documented); signed-zero, inf/NaN and spirv-val checks pass. --fma exact emits no OpFmaKHR/FMAKHR; khr is the only path that gates the extension, and both build/run tooling pass exact through. No game data or absolute paths. Approve.

Nit: the help/design text says "about 110" instructions per FMA; the emitted path is 131.

@AmazingMoaaz

Copy link
Copy Markdown
Contributor Author

@Kam471, thanks for the review. On the count: compared with --fma glsl, one v_fma_f32 adds 107 instructions (116 counting its operand loads and stores), plus 31 NoContraction decorations and 13 constants. So "about 110" left the decorations and constants out. The --fma help and the docs/design.md row now give all three (latest commit).

@AmazingMoaaz

Copy link
Copy Markdown
Contributor Author

Re-checked after rebasing onto main f847748 (head 71c02e9), on Windows 11 Pro build 26300:

  • check.py with LEONIDA_FMA=exact: Intel UHD (Xe-LP, 101.7088) 131 of 174 pass, 43 skipped, 0 failed; RTX 3050 Laptop (617.42) 139 of 174 pass, 35 skipped, 0 failed. With glsl on the same Intel build, cs_fma_fused is the only failure (130 of 174).
  • python -m unittest discover -s tests/semantic: 7331 tests OK (1 expected failure).

@AmazingMoaaz

Copy link
Copy Markdown
Contributor Author

Rebased onto main 4c52ce2 (head 3f0c142) after it conflicted. Two changes beyond the rebase, both from what landed in the meantime:

  • main now accepts either the fused or the split word in cs_fma_fused under --fma glsl, and is strict only under khr. Under --fma exact it would have accepted split words too, which would hide a broken emulation. So judge_fma_fused (check.py) and run_gpu_tests.py are now strict under LEONIDA_FMA=exact as well, labelled --fma exact; khr's messages are unchanged. tests/test_shader_checks.py gets test_exact_is_as_strict_as_khr: the RADV split words pass under glsl and fail under exact, both through judge_fma_fused and through fma_fused_test with LEONIDA_FMA=exact.
  • docs/testing.md now keeps main's RADV paragraph (RX 6800, 5 of 7 split) and adds the Intel UHD / exact text after it. My earlier RADV note duplicated it with crastinator's 4-of-8 count, so I dropped that commit.

tests/test_shader_checks.py 76 OK, tests/test_run_gpu_tests.py 40 OK, test_fma_exact / test_fma_khr / test_valu_fma_mix OK. The GPU re-check on this head is running.

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
# Conflicts:
#	docs/design.md
#	docs/testing.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants