Skip to content

rdna2spirv: fix TwoSum overflow in Gen.f64_add_mode via FastTwoSum - #3074

Open
Wasler wants to merge 1 commit into
ItsNotRexed:mainfrom
Wasler:fix/f64-add-mode-overflow
Open

Wasler wants to merge 1 commit into
ItsNotRexed:mainfrom
Wasler:fix/f64-add-mode-overflow

Conversation

@Wasler

@Wasler Wasler commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #2891 (comment)

What and why

Fixes an intermediate TwoSum overflow in Gen.f64_add_mode (tools/rdna2spirv.py) for v_add_f64 under MODE.FP_ROUND[3:2] = 3 (toward zero), reported by @Jolymmiles in #2891 (comment).

When summing opposite-sign finite operands near ±DBL_MAX with |x| < |y| (e.g. 0x7fdd9bb47daa1333 + 0xffefffffffffffff), the unordered TwoSum first subtraction bb = r - x reconstructs ~y with an intermediate rounding across the overflow boundary and produces ±inf, making the rounding residual e = NaN. Ordered OpFOrdGreaterThan / OpFOrdLessThan checks on e then evaluate false and omit the -1 ULP toward-zero step (0xffe13225c12af666 instead of exact RTZ 0xffe13225c12af665).

  • tools/rdna2spirv.py (Gen.f64_add_mode): extracts the high/low 32-bit magnitude words (axhi = xhi & 0x7FFFFFFF, ayhi = yhi & 0x7FFFFFFF), swaps (x, y) to (a, b) with |a| >= |b| via integer comparison + OpSelect %double, and computes magnitude-ordered FastTwoSum (bb = r - a, e = b - bb, both NoContraction). Whenever r is finite and |a| >= |b|, |r - a| <= max(|r|, |a|) <= DBL_MAX, so intermediate subtraction never overflows, while cutting float64 arithmetic ops from 5 to 2.
  • tests/test_mode_round.py: adds RTZ/RNE regression cases for 0x7fdd9bb47daa1333 + 0xffefffffffffffff in both operand orders, a cross-dword 64-bit borrow case near +DBL_MAX (0xffcffffffffffffe + 0x7fefffffffffffff), negative overflow saturation, half-ulp overflow saturation, ±inf / exact cancellation / -0.0 / NaN preservation, NoContraction decoration checks on both OpFSub %double instructions, and a Fraction oracle sweep including subnormals and tie cases.
  • runtime/test/shaders/ps_f64_round.asm, runtime/test/shaders/check.py, scripts/run_gpu_tests.py: adds a pixel shader fixture testing all four cases above (mrt0: 0xc12af665, 0x3fe7ffff, 0xbfffffff, 0xc12af666) with VK_KHR / shaderFloat64.
  • docs/design.md: updates the existing Known approximations row for v_add_f64 FastTwoSum + RTX 5060 Ti verification.

AI-assisted.

Tested

  • GPU (NVIDIA GeForce RTX 5060 Ti, driver 621.01, shaderFloat64, LLVM_MC 23.1.6, SPIRV-Tools 2026.4):
    • python -I scripts/run_gpu_tests.py --build runtime/build --filter ps_f64: 2 shaders: 2 translated, 2 pass spirv-val; ps_f64_mods (4096/4096 pixels exact), ps_f64_round (4096/4096 pixels exact), 2/2 passed.
  • Translator pytest suite (LLVM_MC 23.1.6, SPIRV-Tools 2026.4 spirv-val):
    • pytest tests/test_mode_round.py: 74 passed in 0.82s
    • All 23 import pytest translator test files (pytest ... -q): 1253 passed, 17 skipped in 69.11s
  • Semantic suite:
    • python -I -m unittest discover -s tests/semantic: Ran 4142 tests in 631.532s — OK
  • Standalone docs, check-runner, and f64 unittest checks (compileall, test_docs_design_rows.py, test_docs_links.py, test_docs_list_items.py, test_docs_repo_paths.py, test_text_write_line_endings.py, test_shader_checks.py, test_run_gpu_tests.py, test_f64_conversion.py, test_f64_output_modifiers.py, test_f64_transcendentals.py, test_mode_round_probe.py): all passed.
  • Game shader corpus was not re-translated locally (no game files on this machine).

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.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent Windows Radeon verification, AI-assisted (Codex). Tested the open head 82cb279597db6484351bbf5c31bee752c8b50329 and its sole parent 162821017ea3c286c8bec860f3ff94f037912061 with the same unmodified synthetic ps_f64_round fixture from the head and a shared vs_ids vertex shader.

64×64 RGBA32F readback R G B A Exact pixels
CPU exact-rational oracle c12af665 3fe7ffff bfffffff c12af666
Parent c12af666 3fe80000 bfffffff c12af666 0/4096
Head c12af665 3fe7ffff bfffffff c12af666 4096/4096

Each draw produced one uniform tuple. The parent returned the nearest-even word in both the opposite-sign reproducer (R) and the cross-dword borrow case (G). Finite-overflow saturation (B) and the return to nearest-even mode (A) matched at both commits.

  • Windows 11 25H2, AMD Radeon(TM) Graphics integrated GPU (1002:13c0), AMD driver 25.10.30.02 (Windows 32.0.21030.2001), Vulkan 1.4.315. Preflight confirmed shaderFloat64, shaderSignedZeroInfNanPreserveFloat32, 256 bytes of push constants for the 144-byte layout, and RGBA32F optimal-tiling color-attachment and transfer-source support. The device reports subgroup sizes 32–64 and permits forcing a size for compute only. Both draws used the driver’s default fragment subgroup size. The modules have no subgroup operations.
  • Both fragment modules declare Shader, Float64, and SignedZeroInfNanPreserve, with SignedZeroInfNanPreserve 32 as the sole float-control mode. Each exports a float4 at location 0 and has no fragment inputs or descriptor bindings. The two fragments and shared vertex module passed spirv-val --target-env vulkan1.2 (SPIRV-Tools 2026.4, SDK 1.4.363.0). Translation used uv CPython 3.12.13 with WSL LLVM 21.1.8 for assembly.
  • Both runs used the same offscreen harness with TEST: synchronize vk_harness GPU writes with host readback #2635’s readback barrier applied in disposable scratch, the singleton AMD ICD, a process-local empty implicit-layer path, and Khronos core and synchronization validation, with a 30-second timeout per draw. The layer confirmed both checks were active and reported no validation errors or sync hazards. Coverage is limited to the four exported fixture words. The modules use default f64 controls, and this draw does not establish f64 NaN or subnormal behavior.

@Rainmaker33

Copy link
Copy Markdown

Tested 82cb279 on NVIDIA GeForce RTX 3090 (Ampere, subgroup 32..32), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300: pass.

The PR's diff applied cleanly to main 1628210. VS 2022, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8; overlay layers off. Harness programs from a CMake build of main.

@yagoviccari1-spec

Copy link
Copy Markdown

Tested 82cb279 on Windows 11 build 26200.9974, NVIDIA RTX 5060 Ti / driver 621.24 (Vulkan 1.4.357, shaderFloat64 available), MSVC 19.44.35207, llvm-mc 21.1.8, SPIRV-Tools v2026.4, CMake 4.5.1 + Ninja 1.13.2. The PR head already contained current origin/main 1628210 ("Already up to date").

  • Full run_gpu_tests.py on the head: 195 shaders translated, 195 pass spirv-val; 144/190 passed, 46 skipped, 0 failed. Device line from the run: NVIDIA GeForce RTX 5060 Ti (subgroup 32..32).
  • The new ps_f64_round test passes: 4096/4096 pixels exact.
  • Scope: this is a functional GPU check of the translator output on this driver. I did not re-derive the TwoSum/overflow reasoning or run a separate f64 addition oracle.

No game files used. Ran with an AI agent on my machine.

@bielfreedom

Copy link
Copy Markdown

Tested 82cb2795 (on its base 16282101) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass.

  • ps_f64_round (now a GPU test): 4096/4096 pixels exact.
  • scripts/run_gpu_tests.py: 195 shaders, 195 translated, 195 pass spirv-val; 176/190 passed, 14 skipped, 0 failures.
  • Full ctest: 119/119, 0 validation errors. tests/semantic: 4142 OK.

Tools: llvm-mc 23.1.6 (conda-forge), SPIRV-Tools from the Vulkan SDK. No game files.

AI-assisted (Claude Code).

@hidrogenone

Copy link
Copy Markdown
Contributor

Tested 82cb2795 on NVIDIA GeForce RTX 5080 (Blackwell), driver 32.0.16.2124, Windows 11, Vulkan SDK 1.4.357.0. llvm-mc LLVM 21.1.8 (WSL Ubuntu).

scripts/run_gpu_tests.py --filter f64_round:

ps_f64_round   4096/4096 pixels exact

Translator: 1 shader translated, spirv-val clean — the Gen.f64_add TwoSum overflow fix is exact on this adapter. NVIDIA Blackwell (RTX 50-series) coverage. AI-assisted.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 82cb279 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.

  • New test passes natively: ps_f64_round (4096/4096 pixels exact).
  • Whole run_gpu_tests.py: 176/190 native, 176/190 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 37/37.
  • tests/test_mode_round.py: 74 passed.

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

@zzEmmazz

Copy link
Copy Markdown
Contributor

Tested 82cb2795, applied alone on main 16282101: pass.
Windows 11 (26200), MSVC 14.50 (VS 2026), Ninja RelWithDebInfo. Two GPUs: Ryzen 7 9700X iGPU (RDNA2, AMD 32.0.21042.62) and RTX 3070 (NVIDIA 610.74), picked with LEONIDA_VK_DEVICE.

  • Tests and fixture only (main's translator): pytest tests/test_mode_round.py 3 failed, 71 passed (the two RTZ cases near +-DBL_MAX return the round-to-nearest word, and the OpFSub count is 4 instead of 2). ps_f64_round fails on both GPUs with the same words: got 0xc12af666 0x3fe80000 0xbfffffff 0xc12af666, want 0xc12af665 0x3fe7ffff 0xbfffffff 0xc12af666.
  • Whole PR: pytest tests/test_mode_round.py 74 passed. run_gpu_tests.py --filter ps_f64: ps_f64_mods and ps_f64_round 4096/4096 pixels exact on both GPUs.
  • Whole GPU suite with the PR: Radeon 175/190 passed, 15 skipped; RTX 3070 143/190 passed, 46 skipped, 1 failed. Test by test the only difference from the baseline is the new ps_f64_round PASS; 195 of 195 shaders translate and pass spirv-val.
  • Baseline on main 16282101, whole scripts/run_gpu_tests.py suite: Radeon 174/189 passed, 15 skipped; RTX 3070 142/189 passed, 46 skipped, 1 failed (ps_sdwa_f16_const, which also fails there on clean main, 3 of 3 runs alone).
  • pytest tests/test_translate.py: 542 passed, 4 skipped. tests/test_shader_checks.py 69 OK, tests/test_run_gpu_tests.py 34 OK, test_f64_conversion.py, test_f64_output_modifiers.py, test_f64_transcendentals.py, test_mode_round_probe.py, test_docs_design_rows.py OK.

Not checked: the complete tests/semantic suite. main moved to f847748c after this run and GitHub now reports the PR as conflicting with it; a rebase was not tested.

@Catskan

Catskan commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 82cb279 on Sapphire RX 9070 XT Nitro+ (AMD 26.11.2 LLPC, driverVersion 2.0.416), Windows 11 26200, MSVC, llvm-mc 23.1.6: pass.

  • scripts/run_gpu_tests.py: 176/190 passed, 14 skipped, 0 failed.
  • pytest tests/test_mode_round.py: 74 passed.
  • Full ctest: 0 failed out of 129, with 7 skipped (game package or optional tools).

AI-assisted run.

@iurysf

iurysf commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested b24f2ce9 (rebased on main f847748c) on RTX 4070 (NVIDIA 621.01, subgroup 32, shaderFloat64), Windows 11 (10.0.26300), llvm-mc 23.1.6, SPIRV-Tools 2026.4: pass. Earlier results on this PR are for 82cb2795.

  • scripts/run_gpu_tests.py --filter ps_f64 --require-pass: 2/2, ps_f64_round 4096/4096 pixels exact. Negative control: with only tools/rdna2spirv.py restored to main, ps_f64_round fails on the GPU (got 0xc12af666 0x3fe80000 0xbfffffff 0xc12af666, want 0xc12af665 0x3fe7ffff 0xbfffffff 0xc12af666) and tests/test_mode_round.py gives 3 failed, 71 passed; with the PR it is 74 passed.
  • Full run_gpu_tests.py --require-pass: 154/200 passed, 46 skipped, 0 failed (main: 153/199; the extra pass is ps_f64_round). pytest of the f64/mode/check-runner tests: 193 passed.
  • Full tests/semantic with llvm-mc: 7322 tests OK (same count as main).

Not tested: AMD/Intel GPUs, Linux/Wine. Tested with AI assistance (Claude); synthetic shaders only, no game data.

dev-ewann pushed a commit to dev-ewann/Leonida that referenced this pull request Nov 22, 2026
# Conflicts:
#	docs/design.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.

10 participants