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

Closed
Sangonapou wants to merge 2 commits from upstream-pr/1231 into main
Sangonapou commented 2026-11-24 20:38:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Sangonapou on 2026-11-24T20:38:47Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5479946867 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1231). Originally posted by [Sangonapou](https://github.com/Sangonapou) on 2026-11-24T20:38:47Z. --- Claimed in the coordination issue as: SHADER-F32-SPECIAL-RESULTS ([claim](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143040476)) ### 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)
GeorgiyDemo commented 2026-11-24 21:05:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-24T21:05:38Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7144465886 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1231#issuecomment-7144465886). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-24T21:05:38Z. --- 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.
adrianmjim commented 2026-11-25 06:32:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by adrianmjim on 2026-11-25T06:32:37Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7154899264 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1231#issuecomment-7154899264). Originally posted by [adrianmjim](https://github.com/adrianmjim) on 2026-11-25T06:32:37Z. --- 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`.
Sangonapou commented 2026-11-25 20:06:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Sangonapou on 2026-11-25T20:06:56Z.


@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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7173210912 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1231#issuecomment-7173210912). Originally posted by [Sangonapou](https://github.com/Sangonapou) on 2026-11-25T20:06:56Z. --- @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.
aslan2142 commented 2026-12-01 11:53:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:53:46Z.

Review: CHANGES_REQUESTED


Reviewed against current main: modeling the measured special results of v_rcp/v_rsq/v_sqrt/v_log_f32 in tools/rdna2spirv.py looks well-tested, but this branch conflicts with main and has failing CI. Please rebase onto current main so CI runs cleanly.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130725954 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1231#pullrequestreview-6130725954). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:53:46Z. **Review:** CHANGES_REQUESTED --- Reviewed against current `main`: modeling the measured special results of `v_rcp`/`v_rsq`/`v_sqrt`/`v_log_f32` in `tools/rdna2spirv.py` looks well-tested, but this branch conflicts with `main` and has failing CI. Please rebase onto current `main` so CI runs cleanly. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 17:22:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T17:22:22Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7319193984 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1231#issuecomment-7319193984). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T17:22:22Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

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!1231
No description provided.