rdna2spirv: v_mul/fma/min/max_f64 and the f64 <-> f32/int conversions… #1374

Open
github-bot wants to merge 3 commits from github-bot/gh-1374 into main AGit
Owner

@⁠vsking2000 on GitHub · 2026-11-24 22:49 UTC · original
vsking2000:shader-f64-valu-ops → main · +425 −44 · 7 files · 3 commits · open

… (SHADER-F64-VALU-OPS)

The translator refused seven binary64 VALU ops by name. Gen.f64_valu now lowers them:

  • v_mul_f64: OpFMul; v_fma_f64: one rounding through Gen.emit_fma64
  • v_min_f64/v_max_f64: NMin/NMax with S0 selected when S1 is a NaN, the rule measured for the f32/f16 forms (one NaN gives the other operand, two give S0)
  • v_cvt_f64_f32: exact OpFConvert; an f32 denormal source reads as a signed zero under the game's MODE, as the measured v_floor/ceil/fract_f32 do
  • v_cvt_i32_f64/v_cvt_u32_f64: truncate, saturate (infinities included), NaN -> 0, per the RDNA2 ISA, as the f32 forms do

Source abs/neg apply; f64 results take clamp/omod through clamp_mods_f64 as v_add_f64 does; integer results take no output modifier. v_mul/v_fma_f64 are refused under a directed MODE.FP_ROUND, which only v_add_f64 models.

shader_ir flags their hex literal sources as f64 literals (bits 63:32); spirv_mini_interp gets OpFConvert; docs/design.md gets a contract row. tests/semantic/test_valu_f64_ops.py is new, and test_valu_cvt_int_f64.py, which asserted the integer conversions are refused until implemented, now runs them through the translator. CPU interpreter and spirv-val only; not GPU-checked.

AI-assisted.

Claude-Session: https://claude.ai/code/session_01Y7fbZYVY7EtLknWBZUfSLf

Claimed in the coordination issue as:

Depends on:

Fixes:

What and why

Tested

GPUs tested:

AI-assisted:

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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.
**@⁠vsking2000** on GitHub · 2026-11-24 22:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1374) `vsking2000:shader-f64-valu-ops` → `main` · +425 −44 · 7 files · 3 commits · open … (SHADER-F64-VALU-OPS) The translator refused seven binary64 VALU ops by name. Gen.f64_valu now lowers them: - v_mul_f64: OpFMul; v_fma_f64: one rounding through Gen.emit_fma64 - v_min_f64/v_max_f64: NMin/NMax with S0 selected when S1 is a NaN, the rule measured for the f32/f16 forms (one NaN gives the other operand, two give S0) - v_cvt_f64_f32: exact OpFConvert; an f32 denormal source reads as a signed zero under the game's MODE, as the measured v_floor/ceil/fract_f32 do - v_cvt_i32_f64/v_cvt_u32_f64: truncate, saturate (infinities included), NaN -> 0, per the RDNA2 ISA, as the f32 forms do Source abs/neg apply; f64 results take clamp/omod through clamp_mods_f64 as v_add_f64 does; integer results take no output modifier. v_mul/v_fma_f64 are refused under a directed MODE.FP_ROUND, which only v_add_f64 models. shader_ir flags their hex literal sources as f64 literals (bits 63:32); spirv_mini_interp gets OpFConvert; docs/design.md gets a contract row. tests/semantic/test_valu_f64_ops.py is new, and test_valu_cvt_int_f64.py, which asserted the integer conversions are refused until implemented, now runs them through the translator. CPU interpreter and spirv-val only; not GPU-checked. AI-assisted. Claude-Session: https://claude.ai/code/session_01Y7fbZYVY7EtLknWBZUfSLf Claimed in the coordination issue as: <!-- the item ID from the open "Coordinating who works on what" issue, for example TEST-17, or a link to your claim --> Depends on: <!-- #N for every pull request this one builds on or conflicts with (the coordination issue's Commands section, AGENTS.md); delete the line if it builds on none and conflicts with none --> Fixes: <!-- #N for each issue this closes (GitHub closes it on merge); delete the line if it closes none --> ### What and why <!-- What this changes and the problem it solves. --> ### Tested <!-- What you ran and on which GPU and driver (CONTRIBUTING.md). For the shader translator: how many shaders translate and pass spirv-val, before and after. --> GPUs tested: <!-- vendor, model, driver and OS of each GPU the GPU tests ran on, or n/a --> AI-assisted: <!-- yes or no (AGENTS.md asks a coding agent to say so) --> ### Checklist - [ ] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [ ] One topic; follow-up work goes in a new pull request. - [ ] 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.
The translator refused seven binary64 VALU ops by name. Gen.f64_valu now
lowers them:

- v_mul_f64: OpFMul; v_fma_f64: one rounding through Gen.emit_fma64
- v_min_f64/v_max_f64: NMin/NMax with S0 selected when S1 is a NaN, the rule
  measured for the f32/f16 forms (one NaN gives the other operand, two give S0)
- v_cvt_f64_f32: exact OpFConvert; an f32 denormal source reads as a signed
  zero under the game's MODE, as the measured v_floor/ceil/fract_f32 do
- v_cvt_i32_f64/v_cvt_u32_f64: truncate, saturate (infinities included),
  NaN -> 0, per the RDNA2 ISA, as the f32 forms do

Source abs/neg apply; f64 results take clamp/omod through clamp_mods_f64 as
v_add_f64 does; integer results take no output modifier. v_mul/v_fma_f64 are
refused under a directed MODE.FP_ROUND, which only v_add_f64 models.

shader_ir flags their hex literal sources as f64 literals (bits 63:32);
spirv_mini_interp gets OpFConvert; docs/design.md gets a contract row.
tests/semantic/test_valu_f64_ops.py is new, and test_valu_cvt_int_f64.py,
which asserted the integer conversions are refused until implemented, now
runs them through the translator. CPU interpreter and spirv-val only; not
GPU-checked.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Y7fbZYVY7EtLknWBZUfSLf
v_mul/fma/min/max_f64 and v_cvt_f64_f32 passed mul:2/mul:4/div:2 through
clamp_mods_f64. An f64 result ignores the omod while f64 denormals are kept
(the game's MODE): the per-result-precision gate documented in
tools/f64_omod_probe.py, measured on gfx1036 for v_cvt_f64_i32/u32,
v_frexp_mant_f64 and v_div_fixup_f64, and already applied by the
v_cvt_f64_i32 path. Gen.f64_valu now accepts the omod syntactically and hands
dst_f64 only the clamp. It is not measured for these five ops; the design.md
row says so. The duplicate-modifier and integer-result refusals are unchanged.

v_min_f64/v_max_f64 apply their source abs/neg as sign-bit moves in the high
word (src_f64 raw_sign), so a signalling NaN is not quieted before the compare,
as src_f_sign_mods does for the f32/f16 forms.

docs/design.md: the v_cvt_f64_f32 denormal flush is stated as by analogy with
the f32 rule and not measured; the integer conversions cite the ISA section and
opcodes like the f32 row and say the f64 wording is not GPU-checked.
tools/f64_diff.py --list-ops drops the six now-translated ops from its
"Excluded" line.

tests/semantic/test_valu_f64_ops.py (21 tests): the fma div:2 and max mul:4
clamp expectations that encoded the wrong rule are corrected; no-scaling cases
for the omods on each f64-result op, alone and with clamp; signed-zero,
signalling-NaN and NaN-S0 sign-modifier min/max cases; SGPR-pair and inline
integer sources; literal expected values in place of Python-computed ones.
CPU interpreter and spirv-val only; not GPU-checked.

AI-assisted.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Y7fbZYVY7EtLknWBZUfSLf
main 364ab4ea rewrote the v_trunc/ceil/rndne/floor_f64 row of docs/design.md
next to the row this branch adds; both are kept.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Y7fbZYVY7EtLknWBZUfSLf
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:49:16 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-24 23:16 UTC · original

On an RTX 4090 (Windows, driver 616.64), all 31 compute readbacks with runtime operands matched the expected bits on 2e6fc3c6. I found one precision difference when the FMA operands are constants.

I used the cancellation case from test_one_rounding, with a = 1 + 2^-30 and c = -(1 + 2^-29). The exact result of v_fma_f64(a, a, c) is 2^-60 (binary64 bits 0x3c30000000000000). I ran two hand-written compute shaders and read the low and high output words directly from the Vulkan buffer.

Operand source Default GLSL Fma --fma khr
Loaded from the buffer at dispatch 2^-60 2^-60
Same bits set with v_mov_b32 in the shader +0.0 2^-60

This reproduces the caveat already documented in emit_fma64 for this newly supported opcode. I'd qualify the new design-table row's fused-result claim for the default mode and include this constant-input case alongside the runtime case in GPU coverage.

The seven synthetic modules covered multiply, FMA, min, max, f32 widening and both saturating integer conversions. The parent d4de3a7d refused all seven by opcode. The PR head translated and validated all seven. The 31 runtime cases included a binary64 subnormal product, two NaNs with distinct payloads, signed flushing of f32 subnormals and saturation at integer limits. I built vk_compute_test from the PR head. Separately, the two focused CPU suites passed 23/23. These results cover translated Vulkan compute on one NVIDIA device. AI-assisted verification.

**@⁠ecox982745** on GitHub · 2026-11-24 23:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1374#issuecomment-7147204581) On an RTX 4090 (Windows, driver 616.64), all **31 compute readbacks with runtime operands** matched the expected bits on `2e6fc3c6`. I found one precision difference when the FMA operands are constants. I used the cancellation case from `test_one_rounding`, with `a = 1 + 2^-30` and `c = -(1 + 2^-29)`. The exact result of `v_fma_f64(a, a, c)` is `2^-60` (binary64 bits `0x3c30000000000000`). I ran two hand-written compute shaders and read the low and high output words directly from the Vulkan buffer. | Operand source | Default GLSL Fma | `--fma khr` | | --- | --- | --- | | Loaded from the buffer at dispatch | `2^-60` | `2^-60` | | Same bits set with `v_mov_b32` in the shader | `+0.0` | `2^-60` | This reproduces the caveat already documented in `emit_fma64` for this newly supported opcode. I'd qualify the new design-table row's fused-result claim for the default mode and include this constant-input case alongside the runtime case in GPU coverage. The seven synthetic modules covered multiply, FMA, min, max, f32 widening and both saturating integer conversions. The parent `d4de3a7d` refused all seven by opcode. The PR head translated and validated all seven. The 31 runtime cases included a binary64 subnormal product, two NaNs with distinct payloads, signed flushing of f32 subnormals and saturation at integer limits. I built `vk_compute_test` from the PR head. Separately, the two focused CPU suites passed **23/23**. These results cover translated Vulkan compute on one NVIDIA device. AI-assisted verification.
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-24 23:45 UTC · original

I tested the output-modifier and NaN changes in a44423b8 against its parent 2e6fc3c6 on the RTX 4090 (Windows 11, NVIDIA 616.64). The same 28 synthetic compute shaders passed SPIR-V validation on each revision. I loaded the operands from a buffer at dispatch and compared the raw 64-bit results with the new CPU tests' expectations.

GPU readback Parent Updated head
Output modifiers on multiply, FMA, min, max and f32-to-f64 conversion 5/25 25/25
Min/max with signalling NaN in source 0, quiet NaN in source 1, and neg/abs/neg-abs on source 0 0/6 6/6
Min/max of opposite signed zeros, both operand orders 4/4 4/4

For all five arithmetic/conversion ops, an unscaled result of 0.25 stayed 0.25 under mul:2, mul:4, div:2, and clamp mul:4 on the updated head. A separate above-one input still clamped to 1 on both revisions. For min(-sNaN, qNaN), the parent returned 0x7ff8000000000123; the updated head returned 0xfff0000000000123, preserving the signalling bit pattern and applying the sign change.

The two focused CPU suites also passed, 29/29 on the updated head. These measurements verify the translated Vulkan output on this NVIDIA driver. Native RDNA2 results for these five instructions with output modifiers remain open.

**@⁠ecox982745** on GitHub · 2026-11-24 23:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1374#issuecomment-7147763894) I tested the output-modifier and NaN changes in `a44423b8` against its parent `2e6fc3c6` on the RTX 4090 (Windows 11, NVIDIA 616.64). The same 28 synthetic compute shaders passed SPIR-V validation on each revision. I loaded the operands from a buffer at dispatch and compared the raw 64-bit results with the new CPU tests' expectations. | GPU readback | Parent | Updated head | | --- | ---: | ---: | | Output modifiers on multiply, FMA, min, max and f32-to-f64 conversion | 5/25 | 25/25 | | Min/max with signalling NaN in source 0, quiet NaN in source 1, and neg/abs/neg-abs on source 0 | 0/6 | 6/6 | | Min/max of opposite signed zeros, both operand orders | 4/4 | 4/4 | For all five arithmetic/conversion ops, an unscaled result of 0.25 stayed 0.25 under `mul:2`, `mul:4`, `div:2`, and `clamp mul:4` on the updated head. A separate above-one input still clamped to 1 on both revisions. For `min(-sNaN, qNaN)`, the parent returned `0x7ff8000000000123`; the updated head returned `0xfff0000000000123`, preserving the signalling bit pattern and applying the sign change. The two focused CPU suites also passed, 29/29 on the updated head. These measurements verify the translated Vulkan output on this NVIDIA driver. Native RDNA2 results for these five instructions with output modifiers remain open.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 06:18 UTC · original · approved these changes

Approving. Merged into current main with no conflicts; the PR's tests pass and the full translator suite gives 488 files / 6,306 tests with only test_int32_to_f64 failing, the same baseline failure as main here (Linux, Python 3.13). I read the diff and the change matches the ISA behaviour it claims, with tests for the new path and its refusals.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 06:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1374#pullrequestreview-6052737830) · **approved these changes** Approving. Merged into current main with no conflicts; the PR's tests pass and the full translator suite gives 488 files / 6,306 tests with only `test_int32_to_f64` failing, the same baseline failure as main here (Linux, Python 3.13). I read the diff and the change matches the ISA behaviour it claims, with tests for the new path and its refusals. AI-assisted (Claude).
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-26 01:19 UTC · original

I checked 1b0442fc on Linux (Python 3.14), CPU only. I did not use a GPU.

Merge state. The branch is 792 commits behind main and now conflicts with it in docs/design.md. #1477 also
conflicts with it, in two lines:

  • the parse_line f64 list in tools/shader_ir.py;
  • the --list-ops "Excluded" line in tools/f64_diff.py.

Whichever of the two lands second has to drop the other's opcodes from that line. While you are on that line,
v_ldexp_f64 is still listed as excluded, but main translates it.

Tests. I merged the branch into main (88479ff6) and took the PR's side of docs/design.md. These pass:
test_valu_f64_ops, test_valu_cvt_int_f64, test_valu_add_f64 and tests/test_semantic_translator_use.py.

Edge cases. I translated 14 hand-written listings and ran them through spirv_interp. They give the words in
the row:

Case Result
min(qNaN_a, qNaN_b) qNaN_a with its payload (S0)
max(sNaN, 1.0) and max(1.0, sNaN) 1.0
min(-0, +0) and min(+0, -0) -0 in the interpreter. The row says the driver decides this.
Subnormal product 2^-1000 × 2^-60 exact, 0x4000
v_cvt_i32_f64(-2^31 - 0.5) 0x80000000
v_cvt_u32_f64(-0.9) and v_cvt_u32_f64(NaN) 0
A literal source of v_fma_f64 the high word, with a zero low word
An SGPR-pair source handled
-|x| on min handled

One gap. A clamp of a NaN result still goes through the bare FClamp in clamp_mods_f64. GLSL.std.450 leaves
that result undefined. With DX10_CLAMP set, the game's MODE, the hardware gives +0. The f32 and f16 clamps select +0
for a NaN since 2026-11-24 ("Float min/max/med3 with NaN"), and that row lists the f64 clamp as still bare. That gap
was already on main through v_add_f64. This PR gives it new paths:

  • v_max_f64 v[4:5], qNaN, qNaN clamp returns the NaN in the interpreter;
  • v_mul_f64 inf, 0 clamp returns 0xfff8000000000000.

A NaN select before the FClamp in clamp_mods_f64 would cover every f64 op at once. A sentence in the new row
saying the f64 clamp of a NaN is left to the driver would also do. #1477's v_fract_f64 ... clamp of an infinity
hits the same line.

Doc nit. The new row still says v_fma_f64 is "one rounding" in the default mode. ecox982745's RTX 4090 readback
above, which the emit_fma64 docstring also records, shows NVIDIA rounding twice when all FMA operands are constants.
Saying that in the row, or pointing to --fma khr, would keep the row from overclaiming.

For scale: docs/isa_coverage.md puts the f64 transcendentals and v_div_* among the groups the 1.0.2.1 corpus does
not need. As far as I can tell, these ops are not on the game's path either, so none of the above blocks merging.

AI-assisted (Claude).

**@⁠qhukz13** on GitHub · 2026-11-26 01:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1374#issuecomment-7179666670) I checked `1b0442fc` on Linux (Python 3.14), CPU only. I did not use a GPU. **Merge state.** The branch is 792 commits behind main and now conflicts with it in `docs/design.md`. #1477 also conflicts with it, in two lines: - the `parse_line` f64 list in `tools/shader_ir.py`; - the `--list-ops` "Excluded" line in `tools/f64_diff.py`. Whichever of the two lands second has to drop the other's opcodes from that line. While you are on that line, `v_ldexp_f64` is still listed as excluded, but main translates it. **Tests.** I merged the branch into main (`88479ff6`) and took the PR's side of `docs/design.md`. These pass: `test_valu_f64_ops`, `test_valu_cvt_int_f64`, `test_valu_add_f64` and `tests/test_semantic_translator_use.py`. **Edge cases.** I translated 14 hand-written listings and ran them through `spirv_interp`. They give the words in the row: | Case | Result | |---|---| | `min(qNaN_a, qNaN_b)` | `qNaN_a` with its payload (S0) | | `max(sNaN, 1.0)` and `max(1.0, sNaN)` | 1.0 | | `min(-0, +0)` and `min(+0, -0)` | -0 in the interpreter. The row says the driver decides this. | | Subnormal product 2^-1000 × 2^-60 | exact, `0x4000` | | `v_cvt_i32_f64(-2^31 - 0.5)` | `0x80000000` | | `v_cvt_u32_f64(-0.9)` and `v_cvt_u32_f64(NaN)` | 0 | | A literal source of `v_fma_f64` | the high word, with a zero low word | | An SGPR-pair source | handled | | `-\|x\|` on min | handled | **One gap.** A `clamp` of a NaN result still goes through the bare `FClamp` in `clamp_mods_f64`. GLSL.std.450 leaves that result undefined. With DX10_CLAMP set, the game's MODE, the hardware gives +0. The f32 and f16 clamps select +0 for a NaN since 2026-11-24 ("Float min/max/med3 with NaN"), and that row lists the f64 clamp as still bare. That gap was already on main through `v_add_f64`. This PR gives it new paths: - `v_max_f64 v[4:5], qNaN, qNaN clamp` returns the NaN in the interpreter; - `v_mul_f64 inf, 0 clamp` returns `0xfff8000000000000`. A NaN select before the `FClamp` in `clamp_mods_f64` would cover every f64 op at once. A sentence in the new row saying the f64 clamp of a NaN is left to the driver would also do. #1477's `v_fract_f64 ... clamp` of an infinity hits the same line. **Doc nit.** The new row still says `v_fma_f64` is "one rounding" in the default mode. ecox982745's RTX 4090 readback above, which the `emit_fma64` docstring also records, shows NVIDIA rounding twice when all FMA operands are constants. Saying that in the row, or pointing to `--fma khr`, would keep the row from overclaiming. For scale: `docs/isa_coverage.md` puts the f64 transcendentals and `v_div_*` among the groups the 1.0.2.1 corpus does not need. As far as I can tell, these ops are not on the game's path either, so none of the above blocks merging. AI-assisted (Claude).
This pull request has changes conflicting with the target branch.
  • docs/design.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1374/head:github-bot/gh-1374'
git switch 'github-bot/gh-1374'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1374'
git switch 'github-bot/gh-1374'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1374'
git switch 'github-bot/gh-1374'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1374'
git switch 'main'
git merge --squash 'github-bot/gh-1374'
git switch 'main'
git merge --ff-only 'github-bot/gh-1374'
git switch 'main'
git merge 'github-bot/gh-1374'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.