Skip to content

rdna2spirv: --fma khr covers the f64 FMAs (v_div_fmas_f64) with OpFmaKHR on doubles - #6635

Merged
shayderrr merged 2 commits into
ProjectLeonida:mainfrom
DeSunShine:f64-fma-khr
Nov 23, 2026
Merged

shayderrr merged 2 commits into
ProjectLeonida:mainfrom
DeSunShine:f64-fma-khr

Conversation

@DeSunShine

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new SHADER item, #6414 (comment)

Depends on: none. Touches the same v_div_fmas_f64 lines as #4840 and #3791 (one-line emit → emit_fma64 swaps), whichever lands first.

What and why

GLSL.std.450 Fma on doubles rounds twice on NVIDIA. On an RTX 4090 (driver 621.24), the new cs_div_fmas_f64 test gives the split 0 instead of 2^-60. The test is v_div_fmas_f64 with VCC = 0, i.e. a plain f64 FMA, computing fma(a, a, c) with a = 1 + 2^-30 and c = -(1 + 2^-29). The f32 cs_fma_fused fuses 8/8 on the same card. --fma khr only covered f32, so v_div_fmas_f64 was off in the last bits there with no option to fix it. The pre-scaled FMA and the exact product error Fma(x, y, -p) that its scaled path relies on were also double-rounded.

  • Gen.emit_fma64: OpFmaKHR %double under --fma khr, GLSL Fma otherwise (--fma exact has no f64 form and keeps Fma). v_div_fmas_f64's three multiply-adds use it.
  • Index: shaderFmaFloat64 for an f64 OpFmaKHR, and shaderFmaFloat32 now only for an f32 one. Before, FMAKHR always mapped to Float32. vk_pipeline_check enables and checks both. The test harnesses already enable whatever VK_KHR_shader_fma reports.
  • GPU test runtime/test/shaders/cs_div_fmas_f64.asm (+ run_gpu_tests.py). It is strict where the index lists shaderFmaFloat64 and accepts the split result under the default, as cs_fma_fused does.
  • docs/design.md: the floating-point row no longer says "no f64 fused multiply-add in the translator yet", and records the measurement.
  • Default --fma glsl output is unchanged.

Tested

NVIDIA GeForce RTX 4090 (driver 621.24, Windows 11 Pro 26200, VS Build Tools 18 + Ninja, Vulkan SDK 1.4.363, llvm-mc 23.1.6), runtime built from this branch:

  • run_gpu_tests.py --filter cs_div_fmas_f64: default → PASS with the split 0x0 0x0 (the driver splits); LEONIDA_FMA=khr → PASS 0x0 0x3c300000 (fused). cs_fma_fused under khr is still 8/8.
  • tests/semantic/test_fma_khr_f64.py (new: emission, index features for f64-only / f32-only / both, spirv-val), plus test_fma_khr.py, test_alu_div_fmas.py, test_run_gpu_tests.py, test_docs_repo_paths.py, test_design_contract_rows.py: 79 passed. regen_fixtures.py --check is clean.
  • Not run on the shader corpus. Only --fma khr output changes, and only for shaders with v_div_fmas_f64.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

…KHR on doubles

GLSL.std.450 Fma on doubles rounds twice on NVIDIA (RTX 4090, 621.24, Windows):
the new cs_div_fmas_f64 GPU test, fma(a, a, c) with a = 1 + 2^-30 and
c = -(1 + 2^-29), gives the split 0 instead of 2^-60 under the default
--fma glsl, and the fused 2^-60 with --fma khr, which now emits OpFmaKHR on
doubles for v_div_fmas_f64's three multiply-adds. The index lists
shaderFmaFloat64 for them (and shaderFmaFloat32 only for an f32 FMA);
vk_pipeline_check checks both. Default output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mortify7612

Copy link
Copy Markdown

Tested be9c4ed7 merged onto main 1cc6e3c1 (clean) on AMD Radeon RX 9060 XT (RDNA4, GFX1200), RADV Mesa 25.0.7 (no VK_KHR_shader_fma in this Mesa), Debian 12 Linux, llvm-mc 23.1.6, vk_pipeline_check rebuilt at the PR: pass, plus one gap below.

  • Default --fma glsl, run_gpu_tests.py --filter cs_: 104/108 passed, 4 skipped, none failed. cs_div_fmas_f64 gives 0x0 0x3c300000, the fused 2^-60: AMD fuses GLSL Fma on doubles, unlike the RTX 4090. cs_fma_fused is 8/8 fused.
  • LEONIDA_FMA=khr: 101/108 passed, 7 skipped, none failed. cs_div_fmas_f64, cs_fma_fused and cs_w32_new_ops skip cleanly because the device lacks the extension.
  • tests/semantic/test_fma_khr_f64.py: OK.

Gap: the index now lists shaderFmaFloat64 for cs_div_fmas_f64, but the harnesses' gate, runtime/test/vk_shader_fma.h (untouched here), still reads only shaderFmaFloat32. accepts() lets any FMAKHR module through once float32 is set, and its message always names shaderFmaFloat32 (that is what printed for the f64 module above). On a device with shaderFmaFloat32 but not shaderFmaFloat64, the f64 test would reach pipeline creation instead of skipping with 77. Recording features.shaderFmaFloat64 too, and requiring it when the module also declares Float64 (capability 10), would close it. AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

Confirmed the f64 cancellation result on Ampere with the native Linux Vulkan driver. On be9c4ed75, I built the compute harness from the PR and ran cs_div_fmas_f64 separately under LEONIDA_FMA=glsl and khr. Both modules passed spirv-val and executed on an RTX 3080 Ti (CachyOS x86-64, NVIDIA 620.73.04).

Mode GPU result Indexed FMA feature
glsl +0 (split) none
khr 2⁻⁶⁰ (single-rounded) shaderFmaFloat64 only

I also searched all 26,503 disassembled Xbox 1.0.2.1 shader listings for the affected v_div_fmas_f64 mnemonic, including encoding suffixes: zero listings matched. The numerical comparison above is from the synthetic cancellation fixture. AI-assisted.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent AI-assisted CPU check at be9c4ed7. I reproduced the existing shared-helper gap with mocked FMA feature bits, and found a counterexample to using the Float64 capability as a proxy.

A synthetic listing with v_fma_f32 v2, v10, v10, -v11 and v_add_f64 v[0:1], 1.0, 1.0 translated and passed Vulkan 1.2 SPIR-V validation. It declares Float64 for OpFAdd %double, while its only OpFmaKHR is %float. The index correctly requires ordinary shaderFloat64 and shaderFmaFloat32. On a mock device with those features and no shaderFmaFloat64, the Float64-capability proxy refuses the valid module. A width check needs the result type of each OpFmaKHR or reliable width metadata.

The checker's index-based width decisions matched the mocked feature matrix, but the FMA checks currently run after vkCreateShaderModule. The pre-module check is where an unsupported FMA module could be skipped before submission. I verified call order in source without submitting unsupported modules to a driver.

All 88 synthetic outputs validated, and 11/11 default-mode modules and index entries matched base, including mixed-width instruction orders. The focused suite passed 79 at head and 74 at base. Windows, uv CPython 3.12.13, SDK 1.4.363.0; no GPU for this check.

@Mortify7612

Copy link
Copy Markdown

@whattheflop right, thanks. Using Float64 as a proxy is too coarse: your v_fma_f32 + v_add_f64 counterexample would be refused wrongly. The width has to come from each OpFmaKHR's result type (or the index's shaderFmaFloat64). The gap itself stands: vk_shader_fma.h only knows shaderFmaFloat32. AI-assisted (Claude Code).

…shaderFmaFloat64)

Review of ProjectLeonida#6635: the harnesses' gate knew only shaderFmaFloat32, so an f64
OpFmaKHR module would reach pipeline creation on a device with Float32 but
not Float64 FMA instead of exiting 77. accepts() now reads each OpFmaKHR's
result type (OpTypeFloat / OpTypeVector width), so a module that declares
Float64 for other doubles but has only f32 OpFmaKHRs still needs only
shaderFmaFloat32. ShaderFma records shaderFmaFloat64 and supported() is
either width. vk_pipeline_check checks both FMA features before
vkCreateShaderModule. New ctest vk_shader_fma (no GPU).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DeSunShine

Copy link
Copy Markdown
Contributor Author

Thanks @Mortify7612, @whattheflop, @Kev-Ball. Pushed 6b0e03e87, which fixes the gate gap:

  • runtime/test/vk_shader_fma.h: accepts() now reads each OpFmaKHR's result type. It takes the width of OpTypeFloat, or the component width for OpTypeVector, and requires shaderFmaFloat32 and/or shaderFmaFloat64 for that width. It does not use Float64 as a proxy, so @whattheflop's v_fma_f32 + v_add_f64 module needs only shaderFmaFloat32. The refusal names the missing feature. ShaderFma records float64, and supported() / link_after() work with either width.
  • vk_pipeline_check: both FMA feature checks moved into check_missing_module_feature, so the skip happens before vkCreateShaderModule.
  • New ctest vk_shader_fma (runtime/test/vk_shader_fma_test.cpp, no Vulkan instance): 13 checks on hand-built modules, including your counterexample, mixed widths, vector results, and FMAKHR without an OpFmaKHR.

RTX 4090 (621.24), runtime rebuilt at 6b0e03e87: ctest -R vk_shader_fma passes. Under LEONIDA_FMA=glsl and khr, cs_div_fmas_f64 gives split / fused as before, and cs_fma_fused (8/8) and cs_w32_new_ops pass. The focused CPU suite gives 121 passed. AI-assisted (Claude Code).

@Mortify7612

Copy link
Copy Markdown

Retested at 6b0e03e8 merged onto main 1cc6e3c1 (clean), same RX 9060 XT / RADV Mesa 25.0.7 / Debian 12, full runtime rebuild (clang 23, 0 errors): pass, and the gate gap is closed.

  • ctest -R "vk_shader_fma|vk_pipeline_check": 2/2 (the new vk_shader_fma included).
  • Default, --filter cs_: 104/108 passed, 4 skipped; cs_div_fmas_f64 0x0 0x3c300000, cs_fma_fused 8/8 fused.
  • LEONIDA_FMA=khr: 101/108 passed, 7 skipped. The skip now names the right feature for each module: cs_div_fmas_f64 needs shaderFmaFloat64, while cs_fma_fused and cs_w32_new_ops need shaderFmaFloat32.

AI-assisted (Claude Code).

@SlopTart

Copy link
Copy Markdown

RADV data point at head 6b0e03e87 - RX 7900 XT (RADV NAVI31), Mesa 26.2.5, Arch/Omarchy, full runtime rebuild, llvm-mc 23.1.3:

  • ctest -R 'vk_shader_fma|vk_pipeline_check': 2/2 (the width-aware accepts() unit checks).
  • Extension state, checked by direct vkEnumerateDeviceExtensionProperties on this device: 246 device extensions, no VK_KHR_shader_fma (and no fma-named ext at all). Same situation Mortify7612 hit on Mesa 25.0.7, so it is not a Mesa-version gap: RADV has no indexed-feature exposure up to 26.2.5, and a true "driver reports shaderFmaFloat32/64 = true" row is not obtainable on Mesa today.
  • Still worth one empirical note: with LEONIDA_FMA=khr the translated modules really do contain OpFmaKHR (verified by disassembly: 8 ops in cs_fma_fused, 4 in cs_div_fmas_f64), and on this RADV they pipeline-create and execute fine: cs_fma_fused 8/8 fused exact, cs_div_fmas_f64 0x0 0x3c300000 (matches Kev-Ball's Ampere and Mortify7612's glsl-mode values). Default --fma glsl lane: both pass too.
  • Read of it: RADV is lenient about OpFmaKHR modules without the extension, so on Mesa the new gate's strictness is only exercised by its unit tests, not by pipeline creation. Not an argument against the gate - spec-wise an ungated module is UB - just flagging that RADV won't be the platform that catches gate regressions; NVIDIA (Kev-Ball's row) and any driver that ships the extension will.

No game files - synthetic compute fixtures, bare hardware. Posted by Hermes (agent) on my machine (RX 7900 XT, Arch; also the boxb/GTX 1070 reporter).

@jordi-vancuijlenborg-vinci

Copy link
Copy Markdown

Tested 6b0e03e8 on Windows 11 build 26200 with an NVIDIA GeForce RTX 3080 (driver 621.01, Vulkan 1.4.357, subgroup size 32). Built the Vulkan test tools from the same revision with LLVM-MinGW 23.1.6.

cs_div_fmas_f64 returned 0x00000000 0x00000000 with LEONIDA_FMA=glsl and 0x00000000 0x3c300000 with LEONIDA_FMA=khr. The KHR result matches the expected single-rounded value of 2⁻⁶⁰. Both modules passed spirv-val, and vk_pipeline_check compiled the FP64 KHR pipeline. The FP32 control cs_fma_fused also passed with 8/8 exact words in KHR mode. Synthetic fixtures only. AI-assisted.

@SlopTart

Copy link
Copy Markdown

GTX 1070 row (second machine: boxb, Pascal GP104, driver 550.163.01, Debian 13, head 6b0e03e8 built fresh), plus one tooling-floor finding:

  • --fma glsl (default): cs_div_fmas_f64 passes here, same as on our RX 7900 XT/Mesa 26.2.5 — so @jordi-vancuijlenborg-vinci's 0x00000000 0x00000000 glsl result on the RTX 3080 looks like an NVIDIA-Windows-specific glsl lowering difference, not a fixture problem.
  • LEONIDA_FMA=khr: could not execute on this machine — not a driver issue but a validator floor: SPIRV-Tools 2025.1 (Debian 13) rejects the translated module with Invalid capability 'FMAKHR' at validation, so the test dies before pipeline creation. With our SPIRV-Tools 2026.4 the same modules validate fine (RADV row: 7105267958). Whatever gate semantics rdna2spirv: --fma khr covers the f64 FMAs (v_div_fmas_f64) with OpFmaKHR on doubles #6635 settles, note that CI runners or contributors on ~1-year-old SPIRV-Tools cannot even reach the KHR path — worth a skip-with-message instead of a hard failure in the test when spirv-val doesn't know the capability.
  • Pascal's 550 driver never got to create the KHR pipeline here, so no claim about its acceptance behavior — just the tooling observation.

No game files — synthetic fixtures from the PR, bare hardware. Posted by Hermes (agent) from the second machine (boxb, GTX 1070; the RX 7900 XT row is above).

@WilzoZiza

Copy link
Copy Markdown

Tested 6b0e03e on AMD Radeon RX 7600 (RDNA3, Navi 33), AMD proprietary 26.11.2 (LLPC), Windows driver 32.0.32029.2010, Vulkan 1.4.359, subgroup 32..64, Windows 11 Pro 26300: pass. Test programs built from the PR head with MSVC 19.44 (VS Build Tools 17.14), llvm-mc 23.1.6 (conda-forge), Vulkan SDK 1.4.363 / SPIRV-Tools v2026.4.

python -I scripts/run_gpu_tests.py --filter cs_, 110 shaders translated and spirv-val-valid in both runs:

Mode run_gpu_tests.py cs_div_fmas_f64 cs_fma_fused
default (--fma glsl) 104/108 passed, 4 skipped, 0 failed PASS 0x0 0x3c300000 PASS 8/8 fused exact (GLSL Fma)
LEONIDA_FMA=khr 104/108 passed, 4 skipped, 0 failed PASS 0x0 0x3c300000 PASS 8/8 fused exact (OpFmaKHR)

AMD's Windows driver exposes the FP64 FMA here, so nothing skips under khr (unlike the RADV 25.0.7 row), and it fuses GLSL Fma on doubles under the default too (the fused 2^-60, as on RADV, unlike the RTX 3080 split). The 4 skips are the cases run by d3d12_buffer_uav_test / d3d12_m4_compute_test. Not run: vk_shader_fma_test, tests/semantic.

AI-assisted: run and written up with Claude (Claude Opus 5.5, Anthropic). Synthetic fixtures only, no game files.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

  • Extends OpFmaKHR support to float64: vk_shader_fma.h gains spirv_fma_widths() deriving f32/f64 width from the instruction's result type; vk_pipeline_check.cpp gates on shaderFmaFloat64, checks the device feature before module creation, and a new vk_shader_fma_test CTest verifies the width gate without a GPU.
  • Adds a cs_div_fmas_f64.asm single-rounding fixture and updates docs/design.md FMA notes.

Why merge

The translator can now emit OpFmaKHR on doubles, and the harness correctly detects and skips devices lacking the matching feature, avoiding spurious failures. New CPU-side coverage checks the width logic.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

  • Extends OpFmaKHR support to float64: vk_shader_fma.h gains spirv_fma_widths() deriving f32/f64 width from the instruction's result type; vk_pipeline_check.cpp gates on shaderFmaFloat64, checks the device feature before module creation, and a new vk_shader_fma_test CTest verifies the width gate without a GPU.
  • Adds a cs_div_fmas_f64.asm single-rounding fixture and updates docs/design.md FMA notes.

Why merge

The translator can now emit OpFmaKHR on doubles, and the harness correctly detects and skips devices lacking the matching feature, avoiding spurious failures. New CPU-side coverage checks the width logic.

Verdict: Approve

@shayderrr
shayderrr merged commit f6a00fc into ProjectLeonida:main Nov 23, 2026
iurysf added a commit to iurysf/Leonida that referenced this pull request Nov 23, 2026
…ar, accept either zero sign

Review follow-ups on ItsNotRexed#4536 (KubickNubick, DeSunShine, whattheflop, tedin7).

- KubickNubick's RTX 4080 mismatch (`v_div_fmas_f64 v[8:9], s[4:5], 3.25, s[4:5]`, VCC clear,
  last bit) and DeSunShine's `fma(a, a, c)` giving 0 on an RTX 4090 are the same driver
  behaviour, reproduced on an RTX 4070: an all-constant GLSL Fma comes back split (product
  rounded to binary64, then the sum) while the same Fma on runtime operands is fused and equals
  C's fma and the Fraction reference (the reference is right). A GPU result equal to the split
  value (scaled the same way with VCC set) is now a counted skip with a vendor-neutral
  reason; `--fma khr` makes it a mismatch, for a translator that emits OpFmaKHR (ItsNotRexed#6635).
  `--oracle hw` always judges.
- Two directed cancellation programs (fma(a, a, c), a = 1 + 2^-30, c = -(1 + 2^-29), with VCC
  clear and set) run with every v_div_fmas_f64 sweep, and the generator emits VCC clear about
  half of the time (parity of an operand word, so the random stream of other programs is
  unchanged), so the split is seen on every run.
- whattheflop: `1 * (-0) + (-0)` is -0 in IEEE and +0 here, and the RDNA2 notes do not specify
  the sign; a zero of the other sign is a counted skip in the translator and gpu oracles, judged
  by hw.
- tedin7: the fixup/fmas rules are the measured gfx1036 ones and equal those of the closed
  ItsNotRexed#6947 (0 mismatches on an RX 6700 XT); a test pins the values.

tests/test_f64_diff.py: 45 tests, seven mutations of the new logic each fail one.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
AimLogin added a commit to AimLogin/Leonida that referenced this pull request Nov 23, 2026
Conflicts, each resolved keeping both sides:
- runtime/shims/d3d12_vulkan_device.cpp, create_graphics(): main's
  mesh-shader PSOs (ItsNotRexed#2359: `create_graphics(desc, ms, as)` and the
  mesh-limit check) plus the per-stage code-base slots. An MS stage
  sits in stages[0] and reads dwords 34-35 like a VS, a PS reads
  38-39, and push_bytes is the max over all stages, MS included.
- tools/rdna2spirv.py, the index entry: main's FMA features (ItsNotRexed#6635)
  plus `push_constant_bytes = 4 * USER_SGPRS + 4 * tail_dwords(g)`,
  so a code-base PS still declares 160 bytes.
- docs/design.md and docs/d3d12_vulkan.md: merged word by word. The
  one main edit not kept is ItsNotRexed#5288's `**ASSUMPTION.**` marker. It sat
  inside the old "two code bases in one block" paragraph, which this
  PR replaces with the implemented text, so the assumption is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

8 participants