rdna2spirv: model the measured special results of v_rcp/v_rsq/v_sqrt/v_log_f32 (SHADER-F32-SPECIAL-RESULTS) #1231

Open
github-bot wants to merge 2 commits from github-bot/gh-1231 into main AGit
Owner

@⁠Sangonapou on GitHub · 2026-11-24 20:38 UTC · original
Sangonapou:f32-special-results → main · +155 −62 · 9 files · 2 commits · open

Claimed in the coordination issue as: SHADER-F32-SPECIAL-RESULTS (claim)

What and why

docs/design.md ("Denormal flush and special results") lists RDNA2's measured special results as not modelled: v_rcp_f32/v_rcp_iflag_f32(±0) = ±inf, v_rsq_f32(-0) = -inf, v_sqrt_f32(-0) = -0, v_log_f32(±0) = -inf, and v_sqrt/v_rsq/v_log of a negative value = 0xffc00000. The translator emitted OpFDiv %f1 x and GLSL.std.450 Sqrt/InverseSqrt/Log2, which SPIR-V leaves undefined for exactly those inputs, so the result was whatever the driver returned. These ops occur 1.17M times in 76% of the corpus (counts in that row).

Gen.f32_special_results (used by the F1 path and v_rcp_iflag_f32): a special operand reaches the op as 1.0 and the hardware word is selected afterwards, the same technique the f16 forms already use. Every other operand, NaNs included, reaches the op unchanged, so its result is as before. v_rsq_f32(+0) = +inf (IEEE). Denormal handling is unchanged (separate question). The design.md row and the "Still open" list are updated.

Tested

  • GPU readback (tools/vk_fuzz_run, one wave32 lane, opaque operands), 22 cases (16 special inputs across the five ops plus 6 regular ones): base 15/22, this branch 22/22 match the RDNA2 words. On the base, NVIDIA returns 0x7fffffff for every sqrt/rsq/log of a negative value where RDNA2 gives 0xffc00000; ±0 cases already matched there by luck of the driver.
  • tools/f32_fuzz.py --programs 500 --seed 20261124: base 463 match / 0 mismatch / 17 undefined in SPIR-V / 20 skipped → this branch 480 match / 0 mismatch / 0 undefined / 20 skipped.
  • python -I scripts/run_semantic_tests.py tests/semantic: 470 files, 6169 tests; the only failures were the five tests that asserted the old undefined behaviour (test_alu_rcp, test_valu_f32_transcendentals, test_alu_rcp_f32, test_valu_rcp_fdiv, test_valu_trig_f32), now updated to the hardware words (plus a regular-input check); all pass.
  • python -m compileall, scripts/check_no_game_data.py on every changed file: clean.

GPUs tested: NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32), driver 621.24, Vulkan 1.4.357, Windows 11 Pro build 26300
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

**@⁠Sangonapou** on GitHub · 2026-11-24 20:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1231) `Sangonapou:f32-special-results` → `main` · +155 −62 · 9 files · 2 commits · open Claimed in the coordination issue as: SHADER-F32-SPECIAL-RESULTS ([claim](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14)) ### What and why `docs/design.md` ("Denormal flush and special results") lists RDNA2's measured special results as **not modelled**: `v_rcp_f32`/`v_rcp_iflag_f32(±0)` = ±inf, `v_rsq_f32(-0)` = -inf, `v_sqrt_f32(-0)` = -0, `v_log_f32(±0)` = -inf, and `v_sqrt`/`v_rsq`/`v_log` of a negative value = `0xffc00000`. The translator emitted `OpFDiv %f1 x` and `GLSL.std.450 Sqrt`/`InverseSqrt`/`Log2`, which SPIR-V leaves undefined for exactly those inputs, so the result was whatever the driver returned. These ops occur 1.17M times in 76% of the corpus (counts in that row). `Gen.f32_special_results` (used by the `F1` path and `v_rcp_iflag_f32`): a special operand reaches the op as 1.0 and the hardware word is selected afterwards, the same technique the f16 forms already use. Every other operand, NaNs included, reaches the op unchanged, so its result is as before. `v_rsq_f32(+0)` = +inf (IEEE). Denormal handling is unchanged (separate question). The design.md row and the "Still open" list are updated. ### Tested - **GPU readback** (`tools/vk_fuzz_run`, one wave32 lane, opaque operands), 22 cases (16 special inputs across the five ops plus 6 regular ones): **base 15/22, this branch 22/22** match the RDNA2 words. On the base, NVIDIA returns `0x7fffffff` for every sqrt/rsq/log of a negative value where RDNA2 gives `0xffc00000`; ±0 cases already matched there by luck of the driver. - `tools/f32_fuzz.py --programs 500 --seed 20261124`: base 463 match / 0 mismatch / **17 undefined in SPIR-V** / 20 skipped → this branch 480 match / 0 mismatch / **0 undefined** / 20 skipped. - `python -I scripts/run_semantic_tests.py tests/semantic`: 470 files, 6169 tests; the only failures were the five tests that asserted the old undefined behaviour (`test_alu_rcp`, `test_valu_f32_transcendentals`, `test_alu_rcp_f32`, `test_valu_rcp_fdiv`, `test_valu_trig_f32`), now updated to the hardware words (plus a regular-input check); all pass. - `python -m compileall`, `scripts/check_no_game_data.py` on every changed file: clean. GPUs tested: NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32), driver 621.24, Vulkan 1.4.357, Windows 11 Pro build 26300 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)
docs/design.md listed these as not modelled: v_rcp_f32/v_rcp_iflag_f32(+-0) = +-inf, v_rsq_f32(-0) = -inf
(and +0 -> +inf), v_sqrt_f32(-0) = -0, v_log_f32(+-0) = -inf, and sqrt/rsq/log of a negative value give
0xffc00000 on RDNA2. The emitted SPIR-V left those inputs undefined (OpFDiv by zero, Sqrt/InverseSqrt/Log2
of a negative or zero). Gen.f32_special_results feeds such an operand to the op as 1.0 and selects the
hardware word afterwards; every other operand, NaNs included, is unchanged.

Tests that asserted the old undefined behaviour now check the hardware words; design.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tests/test_f32_fuzz.py used v_rcp_f32(+0) as its example of SPIR-V the translator leaves undefined; it is
modelled now (+inf), so the test asserts it matches, and checks the "undefined" class through an
interpreter error that reports undefined SPIR-V. tests/test_rcp_iflag_mods.py traces the modifier chain
around OpFDiv; it now steps over the special-operand select before it and the special-result select
after it. Both found by @adrianmjim's review (they live outside tests/semantic).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:43:48 +00:00
Author
Owner

@⁠GeorgiyDemo on GitHub · 2026-11-24 21:05 UTC · original

Checked 3cb866aa on Apple M5 Pro, macOS 26.7.3 (25G342), Python 3.15.2, against main 66d4f5aa3:

  • At this head the five touched semantic files all pass: test_alu_rcp, test_alu_rcp_f32, test_valu_f32_transcendentals, test_valu_rcp_fdiv, test_valu_trig_f32.
  • Against main's translator the same files fail (6, 1, 11, 12 and 2 errors respectively), so the measured special results are covered.

No GPU leg: runtime/test/shaders has no v_rcp/v_rsq/v_sqrt/v_log fixture, so the semantic interpreter is the only oracle here. AI-assisted; synthetic fixtures only, no game data.

**@⁠GeorgiyDemo** on GitHub · 2026-11-24 21:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1231#issuecomment-7144465886) Checked `3cb866aa` on Apple M5 Pro, macOS 26.7.3 (25G342), Python 3.15.2, against `main` `66d4f5aa3`: - At this head the five touched semantic files all pass: `test_alu_rcp`, `test_alu_rcp_f32`, `test_valu_f32_transcendentals`, `test_valu_rcp_fdiv`, `test_valu_trig_f32`. - Against `main`'s translator the same files fail (6, 1, 11, 12 and 2 errors respectively), so the measured special results are covered. No GPU leg: `runtime/test/shaders` has no `v_rcp`/`v_rsq`/`v_sqrt`/`v_log` fixture, so the semantic interpreter is the only oracle here. AI-assisted; synthetic fixtures only, no game data.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 06:32 UTC · original

Tested 3cb866aaa applied onto main 66d4f5aa3 on AMD. Setup: AMD Radeon RX 7900 XT (RDNA3, gfx1100, subgroup 32..64), AMD proprietary driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5, test programs from a main 66d4f5aa3 build. No game files.

1. tests/test_f32_fuzz.py fails with this PR. It is outside tests/semantic, so run_semantic_tests.py does not run it. It passes on main, both at 66d4f5aa3 and at today's 42537e7b6. With this PR it fails on both; at 42537e7b6 I merged it with the docs/design.md conflict resolved to main's side, and the translator and tests merged cleanly:

FAIL: test_undefined_spirv_is_its_own_class (__main__.HarnessTests.test_undefined_spirv_is_its_own_class)
  File "tests/test_f32_fuzz.py", line 87, in test_undefined_spirv_is_its_own_class
    self.assertEqual(status, "undefined")
AssertionError: 'match' != 'undefined'

That test uses v_rcp_f32(+0) as its example of undefined SPIR-V ("hardware +inf, OpFDiv by zero"). This PR now models that case, so it matches. The test needs an example that is still undefined after the PR, and v_rcp_f32(+0) could move to a match assertion.

2. GPU readback, the leg missing above. One lane, opaque operand, through tools/vk_fuzz_run.run_programs. I used 29 cases: the 15 special inputs of the new design.md rows, 7 regular inputs, and 7 denormal inputs, which RDNA2 reads as a zero of their sign. Results against the RDNA2 words:

wave32 wave64
main 66d4f5aa3 29/29 29/29
this PR 29/29 29/29

On AMD's driver, main already returns RDNA's words for every case. That includes 0xffc00000 for sqrt, rsq and log of a negative value, -inf for rcp(-0), and the denormal inputs. So the change makes no difference on this card, and its effect is on drivers like NVIDIA's, as your base 15/22 shows. On AMD it causes no regression: the whole GPU suite gives 236/252 passed, 16 skipped, 0 failed, the same per fixture as main.

The script is a reconstruction, since the body gives no command. It runs v_mov_b32 v1, <bits> and then <op> v8, v1 with words=1.

**@⁠adrianmjim** on GitHub · 2026-11-25 06:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1231#issuecomment-7154899264) Tested 3cb866aaa applied onto main 66d4f5aa3 on AMD. Setup: AMD Radeon RX 7900 XT (RDNA3, gfx1100, subgroup 32..64), AMD proprietary driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5, test programs from a `main` `66d4f5aa3` build. No game files. **1. `tests/test_f32_fuzz.py` fails with this PR.** It is outside `tests/semantic`, so `run_semantic_tests.py` does not run it. It passes on `main`, both at `66d4f5aa3` and at today's `42537e7b6`. With this PR it fails on both; at `42537e7b6` I merged it with the `docs/design.md` conflict resolved to `main`'s side, and the translator and tests merged cleanly: ``` FAIL: test_undefined_spirv_is_its_own_class (__main__.HarnessTests.test_undefined_spirv_is_its_own_class) File "tests/test_f32_fuzz.py", line 87, in test_undefined_spirv_is_its_own_class self.assertEqual(status, "undefined") AssertionError: 'match' != 'undefined' ``` That test uses `v_rcp_f32(+0)` as its example of undefined SPIR-V ("hardware +inf, OpFDiv by zero"). This PR now models that case, so it matches. The test needs an example that is still undefined after the PR, and `v_rcp_f32(+0)` could move to a `match` assertion. **2. GPU readback**, the leg missing above. One lane, opaque operand, through `tools/vk_fuzz_run.run_programs`. I used 29 cases: the 15 special inputs of the new `design.md` rows, 7 regular inputs, and 7 denormal inputs, which RDNA2 reads as a zero of their sign. Results against the RDNA2 words: | | wave32 | wave64 | |---|---|---| | main `66d4f5aa3` | **29/29** | 29/29 | | this PR | 29/29 | 29/29 | On AMD's driver, `main` already returns RDNA's words for every case. That includes `0xffc00000` for sqrt, rsq and log of a negative value, `-inf` for `rcp(-0)`, and the denormal inputs. So the change makes no difference on this card, and its effect is on drivers like NVIDIA's, as your base 15/22 shows. On AMD it causes no regression: the whole GPU suite gives 236/252 passed, 16 skipped, 0 failed, the same per fixture as `main`. The script is a reconstruction, since the body gives no command. It runs `v_mov_b32 v1, <bits>` and then `<op> v8, v1` with `words=1`.
Author
Owner

@⁠Sangonapou on GitHub · 2026-11-25 20:06 UTC · original

@⁠adrianmjim thank you, both points were right, and the AMD readback is a very useful second half: it confirms the change is a no-op on AMD (29/29 before and after) and only moves NVIDIA-style drivers onto the RDNA2 words.

Fixed in bd96a747, on top of a rebase onto main e18b59a5:

  1. tests/test_f32_fuzz.py: v_rcp_f32(+0) is now asserted as a match. No op this fuzzer generates is left undefined for these operands any more (I checked every op with 1-3 operands over ±0, ±inf, NaN, ±1, 2), so the undefined class is checked by making run_fragment raise an "undefined in SPIR-V" error. 29 tests OK.
  2. Conflict: only docs/design.md; I took the version on main and re-applied the two edits on top.

Your point also made me run everything outside tests/semantic, which found one more: tests/test_rcp_iflag_mods.py traces the modifier chain around OpFDiv and now steps over the two selects (5 OK). On this branch: run_semantic_tests.py tests/semantic 491 files, 6313 tests, 0 failed; every tests/test_*.py that uses the translator passes except test_runtime_paths.py, which fails the same way on main (tools/conformance/** missing from the runtime.yml path filter), unrelated.

@⁠GeorgiyDemo thanks for the CPU check. AI-assisted.

**@⁠Sangonapou** on GitHub · 2026-11-25 20:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1231#issuecomment-7173210912) @⁠adrianmjim thank you, both points were right, and the AMD readback is a very useful second half: it confirms the change is a no-op on AMD (29/29 before and after) and only moves NVIDIA-style drivers onto the RDNA2 words. Fixed in `bd96a747`, on top of a rebase onto `main` `e18b59a5`: 1. **`tests/test_f32_fuzz.py`**: `v_rcp_f32(+0)` is now asserted as a `match`. No op this fuzzer generates is left undefined for these operands any more (I checked every op with 1-3 operands over ±0, ±inf, NaN, ±1, 2), so the `undefined` class is checked by making `run_fragment` raise an "undefined in SPIR-V" error. 29 tests OK. 2. **Conflict**: only `docs/design.md`; I took the version on `main` and re-applied the two edits on top. Your point also made me run everything outside `tests/semantic`, which found one more: `tests/test_rcp_iflag_mods.py` traces the modifier chain around `OpFDiv` and now steps over the two selects (5 OK). On this branch: `run_semantic_tests.py tests/semantic` 491 files, 6313 tests, 0 failed; every `tests/test_*.py` that uses the translator passes except `test_runtime_paths.py`, which fails the same way on `main` (`tools/conformance/**` missing from the `runtime.yml` path filter), unrelated. @⁠GeorgiyDemo thanks for the CPU check. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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