rdna2spirv: the binary64 roundings keep a NaN's bits and round a subnormal without the driver (SHADER-ROUND-F64-SPECIALS) #1047

Merged
milk4sale merged 8 commits from upstream-pr/1047 into main 2026-11-24 23:20:04 +00:00
milk4sale commented 2026-11-24 17:18:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by milk4sale on 2026-11-24T17:18:00Z.


Claimed in the coordination issue as: SHADER-ROUND-F64-SPECIALS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7138652249)

Depends on: #1034, which depends on #1012 (this branch is one commit on top of them; the last commit is the one to review. #1034 adds the fixture that found this and that this makes exact.)

What and why

v_trunc_f64, v_ceil_f64, v_rndne_f64 and v_floor_f64 bitcast their source pair to %double and call the driver's GLSL.std.450 Trunc, Ceil, RoundEven or Floor. For two kinds of source the driver's answer is not RDNA2's (the finding linked above, measured with cs_w32_round_f64):

  • A NaN. Vulkan lets a driver return any NaN for a NaN operand. A signalling NaN came back quiet, sign and payload kept (0x7ff0000000000001 gave 0x7ff8000000000001), on an RTX 4070 Ti (NVIDIA 620.70.06) and on llvmpipe. The hardware returns a NaN unchanged (row in docs/design.md, gfx1036, IEEE=0).
  • A subnormal. The module declares no DenormPreserve for 64 bits, so a driver may read one as zero, and llvmpipe did: v_ceil_f64 of 0x0000000000000001 gave +0 and v_floor_f64 of its negative -0, where the hardware gives 1 and -1. Declaring the mode is not a way out: neither device reports shaderDenormPreserveFloat64, and adding it by hand changed nothing on either.

The translator now selects those two sources with integer ops on the words, after the source modifiers and before the output ones:

  • a NaN's two words pass through;
  • a subnormal gives a zero of its sign, or 1 with its sign where the op rounds that sign away from zero (v_ceil_f64 of a positive one, v_floor_f64 of a negative one);
  • every other source takes the driver's result, as before.

With that, cs_w32_round_f64 drops the two allowances #1034 gave it and compares all 1152 heap words exactly. tests/semantic/test_f64_round.py gets F64RoundSpecialSources, which runs the four ops in the interpreter under a model of a driver that quiets a NaN and reads a subnormal as zero. The row in docs/design.md describes the rule and how it was confirmed, and no longer lists the approximation #1034 recorded.

Tested

At this head, llvm-mc 23.1.4, SPIRV-Tools v2026.4:

  • python -I scripts/run_gpu_tests.py --build runtime/build --filter cs_w32_round_f64 --require-pass: PASS ("1152 words exact") on the RTX 4070 Ti natively and with LEONIDA_W64_EMULATE=1, and on llvmpipe. One commit below, compared exactly, 15 of the 512 results differ on the 4070 Ti and 20 on llvmpipe.
  • --filter f64 on the 4070 Ti: 7/7 (cs_div_fmas_f64, cs_div_fmas_f64_rt, cs_f64_add, cs_w32_cmp_f64, cs_w32_round_f64, ps_f64_mods, ps_f64_round). The module passes spirv-val.
  • Rules broken one at a time, fixture rebuilt, on both devices. Each of these failed on both: a NaN left to the driver (word 784), the NaN and subnormal tests ignoring the low dword, v_ceil_f64 lifting the negative subnormal (word 34), v_floor_f64 lifting the positive one (word 38), a subnormal's result losing its sign (word 40), a NaN's low word dropped (word 784), rndne lowered as floor(x + 0.5) (word 20), trunc or ceil lowered as Floor, the neg or the abs source modifier ignored (words 8 and 26), the two result words swapped (word 8), the fixture without its v_cmpx (word 30). Two are not caught everywhere and are not counted: a subnormal left to the driver fails on llvmpipe and passes on the 4070 Ti, which keeps binary64 denormals, and RoundEven replaced by GLSL Round passes on both, since both send a tie to the even integer.
  • python -m unittest discover -s tests/semantic -p test_f64_round.py: 10 tests. With tools/rdna2spirv.py put back to the commit below, F64RoundSpecialSources fails in 18 subtests (the signalling NaNs for all four ops, ceil and floor of a subnormal) and the other tests pass.
  • Whole translator suite, python -I scripts/run_semantic_tests.py tests/semantic -j 8: 470 files, 6,169 tests, 0 files failed.
  • Corpus: none of the 25,734 disassembly listings in my work folder contains one of the four ops (5 contain any _f64 op), so no game shader's translation changes.
  • tests/test_gpu_round_f64.py (14 tests), tests/test_shader_checks.py (87), tests/test_run_gpu_tests.py (64), tests/test_semantic_translator_use.py, tests/test_docs_design_rows.py, tests/test_docs_links.py, tests/test_docs_repo_paths.py, tests/test_text_write_line_endings.py: OK. python -m compileall -q make_game.py tools tests and scripts/check_no_game_data.py on the changed files: OK.

Not run: the whole GPU suite at this head (the change is confined to the four ops, and every fixture that uses a binary64 op was run); llvm-mc 19; Windows; AMD hardware. The hardware's words are the row's gfx1036 measurement, which this does not repeat: the 4070 Ti and llvmpipe runs show that the translated module now gives those words on two drivers that used to differ.

Merge of #1034's 60c2ed910 (88785d0f3). The red linux-gpu-tools here was #1034's fixture on a runner without AVX-512, where llvmpipe computes Trunc and RoundEven of a double with its float code (48 of the 512 results; details in #1034). #1034 now lets llvmpipe, and only llvmpipe, return what that path gives for v_trunc_f64 and v_rndne_f64. This branch merges that commit; the one conflict was in round_f64_problem, where the judge stays exact for every other word and device, a NaN and a subnormal included, and takes that one allowance. The translator change is as reviewed at 26d010b41. On the merge: the fixture passes on llvmpipe (Mesa 26.2.6) natively and under GALLIUM_OVERRIDE_CPU_CAPS=avx and sse2, and on the RTX 4070 Ti natively and with LEONIDA_W64_EMULATE=1; tests/test_gpu_round_f64.py, tests/test_shader_checks.py, tests/test_run_gpu_tests.py and tests/semantic/test_f64_round.py pass, also on the tree merged with main 4a50cafc7.

GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 620.70.06, Linux (CachyOS, kernel 7.3.3); Mesa llvmpipe 26.2.6 (LLVM 23.1.4), same machine

AI-assisted: yes (Claude Code)

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.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5476950084 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1047). Originally posted by [milk4sale](https://github.com/milk4sale) on 2026-11-24T17:18:00Z. --- Claimed in the coordination issue as: SHADER-ROUND-F64-SPECIALS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7138652249) Depends on: #1034, which depends on #1012 (this branch is one commit on top of them; the last commit is the one to review. #1034 adds the fixture that found this and that this makes exact.) ### What and why `v_trunc_f64`, `v_ceil_f64`, `v_rndne_f64` and `v_floor_f64` bitcast their source pair to `%double` and call the driver's GLSL.std.450 `Trunc`, `Ceil`, `RoundEven` or `Floor`. For two kinds of source the driver's answer is not RDNA2's (the finding linked above, measured with `cs_w32_round_f64`): - **A NaN.** Vulkan lets a driver return any NaN for a NaN operand. A signalling NaN came back quiet, sign and payload kept (`0x7ff0000000000001` gave `0x7ff8000000000001`), on an RTX 4070 Ti (NVIDIA 620.70.06) and on llvmpipe. The hardware returns a NaN unchanged (row in `docs/design.md`, gfx1036, IEEE=0). - **A subnormal.** The module declares no `DenormPreserve` for 64 bits, so a driver may read one as zero, and llvmpipe did: `v_ceil_f64` of `0x0000000000000001` gave +0 and `v_floor_f64` of its negative -0, where the hardware gives 1 and -1. Declaring the mode is not a way out: neither device reports `shaderDenormPreserveFloat64`, and adding it by hand changed nothing on either. The translator now selects those two sources with integer ops on the words, after the source modifiers and before the output ones: - a NaN's two words pass through; - a subnormal gives a zero of its sign, or 1 with its sign where the op rounds that sign away from zero (`v_ceil_f64` of a positive one, `v_floor_f64` of a negative one); - every other source takes the driver's result, as before. With that, `cs_w32_round_f64` drops the two allowances #1034 gave it and compares all 1152 heap words exactly. `tests/semantic/test_f64_round.py` gets `F64RoundSpecialSources`, which runs the four ops in the interpreter under a model of a driver that quiets a NaN and reads a subnormal as zero. The row in `docs/design.md` describes the rule and how it was confirmed, and no longer lists the approximation #1034 recorded. ### Tested At this head, llvm-mc 23.1.4, SPIRV-Tools v2026.4: - `python -I scripts/run_gpu_tests.py --build runtime/build --filter cs_w32_round_f64 --require-pass`: PASS ("1152 words exact") on the RTX 4070 Ti natively and with `LEONIDA_W64_EMULATE=1`, and on llvmpipe. One commit below, compared exactly, 15 of the 512 results differ on the 4070 Ti and 20 on llvmpipe. - `--filter f64` on the 4070 Ti: 7/7 (`cs_div_fmas_f64`, `cs_div_fmas_f64_rt`, `cs_f64_add`, `cs_w32_cmp_f64`, `cs_w32_round_f64`, `ps_f64_mods`, `ps_f64_round`). The module passes `spirv-val`. - Rules broken one at a time, fixture rebuilt, on both devices. Each of these failed on both: a NaN left to the driver (word 784), the NaN and subnormal tests ignoring the low dword, `v_ceil_f64` lifting the negative subnormal (word 34), `v_floor_f64` lifting the positive one (word 38), a subnormal's result losing its sign (word 40), a NaN's low word dropped (word 784), `rndne` lowered as `floor(x + 0.5)` (word 20), `trunc` or `ceil` lowered as `Floor`, the `neg` or the `abs` source modifier ignored (words 8 and 26), the two result words swapped (word 8), the fixture without its `v_cmpx` (word 30). Two are not caught everywhere and are not counted: a subnormal left to the driver fails on llvmpipe and passes on the 4070 Ti, which keeps binary64 denormals, and `RoundEven` replaced by GLSL `Round` passes on both, since both send a tie to the even integer. - `python -m unittest discover -s tests/semantic -p test_f64_round.py`: 10 tests. With `tools/rdna2spirv.py` put back to the commit below, `F64RoundSpecialSources` fails in 18 subtests (the signalling NaNs for all four ops, `ceil` and `floor` of a subnormal) and the other tests pass. - Whole translator suite, `python -I scripts/run_semantic_tests.py tests/semantic -j 8`: 470 files, 6,169 tests, 0 files failed. - Corpus: none of the 25,734 disassembly listings in my work folder contains one of the four ops (5 contain any `_f64` op), so no game shader's translation changes. - `tests/test_gpu_round_f64.py` (14 tests), `tests/test_shader_checks.py` (87), `tests/test_run_gpu_tests.py` (64), `tests/test_semantic_translator_use.py`, `tests/test_docs_design_rows.py`, `tests/test_docs_links.py`, `tests/test_docs_repo_paths.py`, `tests/test_text_write_line_endings.py`: OK. `python -m compileall -q make_game.py tools tests` and `scripts/check_no_game_data.py` on the changed files: OK. Not run: the whole GPU suite at this head (the change is confined to the four ops, and every fixture that uses a binary64 op was run); llvm-mc 19; Windows; AMD hardware. The hardware's words are the row's gfx1036 measurement, which this does not repeat: the 4070 Ti and llvmpipe runs show that the translated module now gives those words on two drivers that used to differ. **Merge of #1034's `60c2ed910` (`88785d0f3`).** The red `linux-gpu-tools` here was #1034's fixture on a runner without AVX-512, where llvmpipe computes `Trunc` and `RoundEven` of a double with its float code (48 of the 512 results; details in #1034). #1034 now lets llvmpipe, and only llvmpipe, return what that path gives for `v_trunc_f64` and `v_rndne_f64`. This branch merges that commit; the one conflict was in `round_f64_problem`, where the judge stays exact for every other word and device, a NaN and a subnormal included, and takes that one allowance. The translator change is as reviewed at `26d010b41`. On the merge: the fixture passes on llvmpipe (Mesa 26.2.6) natively and under `GALLIUM_OVERRIDE_CPU_CAPS=avx` and `sse2`, and on the RTX 4070 Ti natively and with `LEONIDA_W64_EMULATE=1`; `tests/test_gpu_round_f64.py`, `tests/test_shader_checks.py`, `tests/test_run_gpu_tests.py` and `tests/semantic/test_f64_round.py` pass, also on the tree merged with `main` 4a50cafc7. GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 620.70.06, Linux (CachyOS, kernel 7.3.3); Mesa llvmpipe 26.2.6 (LLVM 23.1.4), same machine AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
whoalin1 commented 2026-11-24 17:48:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by whoalin1 on 2026-11-24T17:48:11Z.


Independent hardware verification from a second Windows box (Ryzen 5 7600 RDNA2 iGPU, gfx1035, AMD proprietary driver 24.10.24, Vulkan 1.3.280, 128-byte push constants, LEONIDA_VK_VALIDATION=1, LEONIDA_PC_TAIL_UBO=1; plus NVIDIA RTX 4070 SUPER, driver 610.88):

  • run_gpu_tests.py --filter round_f64 on the AMD iGPU against this PR's SPIR-V: PASS, 1152/1152 words exact (cs_w32_round_f64).
  • The unmodified origin/main translator output on the same AMD iGPU also passes bit-exact — AMD's driver already implements RDNA2 f64 rounding semantics (NaN passthrough, subnormals kept), so the deviation does not reproduce on AMD hardware.
  • On the NVIDIA RTX 4070 SUPER, main's lowering reproduces the reported bug exactly: word 784 (thread 49): trunc of 0x7ff0000000000001 gave 0x7ff8000000000001, expected 0x7ff0000000000001 (sNaN quieted). With this PR's fixed SPIR-V the same check passes on NVIDIA.
  • python -m unittest discover -s tests/semantic -p test_f64_round.py: 10 tests OK (1 skip, optional spirv-tools absent).

So the finding is a driver-conformance gap (NVIDIA quiets sNaNs; llvmpipe also flushes f64 subnormals), not an RDNA2 hardware bug, and this PR removes the driver dependence. AMD RDNA2 + NVIDIA both verified clean. AI-assisted verification.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7139726144 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1047#issuecomment-7139726144). Originally posted by [whoalin1](https://github.com/whoalin1) on 2026-11-24T17:48:11Z. --- Independent hardware verification from a second Windows box (Ryzen 5 7600 RDNA2 iGPU, gfx1035, AMD proprietary driver 24.10.24, Vulkan 1.3.280, 128-byte push constants, LEONIDA_VK_VALIDATION=1, LEONIDA_PC_TAIL_UBO=1; plus NVIDIA RTX 4070 SUPER, driver 610.88): - `run_gpu_tests.py --filter round_f64` on the AMD iGPU against this PR's SPIR-V: **PASS, 1152/1152 words exact** (`cs_w32_round_f64`). - The **unmodified `origin/main` translator output** on the same AMD iGPU also passes bit-exact — AMD's driver already implements RDNA2 f64 rounding semantics (NaN passthrough, subnormals kept), so the deviation does not reproduce on AMD hardware. - On the **NVIDIA RTX 4070 SUPER**, main's lowering reproduces the reported bug exactly: `word 784 (thread 49): trunc of 0x7ff0000000000001 gave 0x7ff8000000000001, expected 0x7ff0000000000001` (sNaN quieted). With this PR's fixed SPIR-V the same check **passes** on NVIDIA. - `python -m unittest discover -s tests/semantic -p test_f64_round.py`: 10 tests OK (1 skip, optional spirv-tools absent). So the finding is a driver-conformance gap (NVIDIA quiets sNaNs; llvmpipe also flushes f64 subnormals), not an RDNA2 hardware bug, and this PR removes the driver dependence. AMD RDNA2 + NVIDIA both verified clean. AI-assisted verification.
iFeyz commented 2026-11-24 19:40:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by iFeyz on 2026-11-24T19:40:40Z.


AMD run (the "Windows; AMD hardware" gap): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, scripts/run_gpu_tests.py --filter _f64 with the test programs of a current main build.

  • Head 26d010b41: 7 of 7 pass, native and with LEONIDA_W64_EMULATE=1. cs_w32_round_f64: 1152 words exact. cs_w32_cmp_f64: 1536 words exact. tests/semantic/test_f64_round.py: 10 tests OK.
  • The AMD driver needs this fix too. I ran #1034's translation (355c8c9c2, no translator change) under this PR's strict round_f64_problem. It fails at word 784: trunc of the signalling NaN 0x7ff0000000000001 gives 0x7ff8000000000001, the same quieting you measured on NVIDIA. Native and wave64 alike. So without this PR the proprietary AMD driver does not give RDNA2's words for a signalling NaN either, and with it every word matches.
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7142434326 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1047#issuecomment-7142434326). Originally posted by [iFeyz](https://github.com/iFeyz) on 2026-11-24T19:40:40Z. --- AMD run (the "Windows; AMD hardware" gap): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, `scripts/run_gpu_tests.py --filter _f64` with the test programs of a current `main` build. - Head `26d010b41`: 7 of 7 pass, native and with `LEONIDA_W64_EMULATE=1`. `cs_w32_round_f64`: **1152 words exact**. `cs_w32_cmp_f64`: 1536 words exact. `tests/semantic/test_f64_round.py`: 10 tests OK. - **The AMD driver needs this fix too.** I ran #1034's translation (`355c8c9c2`, no translator change) under this PR's strict `round_f64_problem`. It fails at word 784: `trunc` of the signalling NaN `0x7ff0000000000001` gives `0x7ff8000000000001`, the same quieting you measured on NVIDIA. Native and wave64 alike. So without this PR the proprietary AMD driver does not give RDNA2's words for a signalling NaN either, and with it every word matches.
aslan2142 commented 2026-11-24 23:20:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-24T23:20:07Z.


Merged, after #1012 and #1034. Checked locally on main 3a73ce11a + 7fb019f22:

  • Claim: valid. SHADER-ROUND-F64-SPECIALS was posted at 17:04 UTC, before this PR, and is unedited.
  • The rule is right: NaN and subnormal sources are detected on the words after neg/abs and before the output modifiers. A subnormal goes to ±0 under trunc and rndne; ceil gives 1 for a positive one and −0 for a negative one; floor gives +0 and −1. That is IEEE 754. Passing a signalling NaN through unchanged follows the gfx1036 measurement in the row, as the PR says.
  • tests/semantic/test_f64_round.py: F64RoundSpecialSources fails in 18 subtests on main's translator and passes with this change.
  • GPU, --filter f64: 7 of 7 on llvmpipe (Mesa 26.3.1) and on an RDNA2 iGPU (RADV). Without the change, llvmpipe fails cs_w32_round_f64 at word 34 (ceil of the smallest subnormal), while RADV already returns the hardware's words.
  • On the merge: the semantic suite passes (475 files, 6,216 tests, 0 files failed); test_gpu_round_f64, test_run_gpu_tests, test_shader_checks pass; the 13 docs and writer tests pass; check_no_game_data.py is clean on the 8 changed files.
  • CI had not finished on this head. The reports here from whoalin1 and iFeyz (an RTX 4070 SUPER and an RX 9070 XT failing without the fix and passing with it) back the runs above.

AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7147283318 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1047#issuecomment-7147283318). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-24T23:20:07Z. --- Merged, after #1012 and #1034. Checked locally on `main` `3a73ce11a` + `7fb019f22`: - **Claim:** valid. `SHADER-ROUND-F64-SPECIALS` was posted at 17:04 UTC, before this PR, and is unedited. - **The rule is right:** NaN and subnormal sources are detected on the words after `neg`/`abs` and before the output modifiers. A subnormal goes to ±0 under `trunc` and `rndne`; `ceil` gives 1 for a positive one and −0 for a negative one; `floor` gives +0 and −1. That is IEEE 754. Passing a signalling NaN through unchanged follows the gfx1036 measurement in the row, as the PR says. - **`tests/semantic/test_f64_round.py`:** `F64RoundSpecialSources` fails in 18 subtests on `main`'s translator and passes with this change. - **GPU, `--filter f64`:** 7 of 7 on llvmpipe (Mesa 26.3.1) and on an RDNA2 iGPU (RADV). Without the change, llvmpipe fails `cs_w32_round_f64` at word 34 (`ceil` of the smallest subnormal), while RADV already returns the hardware's words. - **On the merge:** the semantic suite passes (475 files, 6,216 tests, 0 files failed); `test_gpu_round_f64`, `test_run_gpu_tests`, `test_shader_checks` pass; the 13 docs and writer tests pass; `check_no_game_data.py` is clean on the 8 changed files. - **CI had not finished on this head.** The reports here from whoalin1 and iFeyz (an RTX 4070 SUPER and an RX 9070 XT failing without the fix and passing with it) back the runs above. AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).
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
ProjectLeonida/mandy!1047
No description provided.