spirv_interp: handle GLSL IEEE edge domains, FMA sign/overflow, and OpFNegate NaNs #2266

Merged
leonida/mandy
kirigayakazima merged 1 commit from fix/spirv-interp-ieee-safety into main 2026-11-28 15:07:13 +00:00

Pull request successfully merged and closed

kirigayakazima commented 2026-11-26 11:50:04 +00:00

Claimed in the coordination issue as: SPIRV-INTERP-IEEE-DOMAIN-OVERFLOW-FMA-AND-BITCAST-SIGN (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191180874)

What and why

tests/spirv_interp.py previously raised Python exceptions or lost IEEE-754 sign/payload bits on valid edge-case inputs:

  • Sqrt, InverseSqrt, Log2, Sin, Cos, Fract, and Exp2 called raw math.* / 2.0 ** x functions directly, raising ValueError or OverflowError on negative/zero/infinite/huge operands instead of returning IEEE-754 NaN, signed infinity, 0.0, or -0.0.
  • Fma converted Fraction sums unconditionally via float(...), losing -0.0 sign when -0.0 * c + (-0.0) summed to zero and raising OverflowError on overflowing binary64 results.
  • OpFNegate ran unary -v, stripping _F32 bit-pattern payloads (including signalling NaN bits) and failing on tuple vector operands.

This adds IEEE-754 domain/overflow/-0.0-safe evaluation in glsl(), preserves _F32 bit patterns and vector/f64 NaN signs in OpFNegate, and adds regression tests in tests/test_spirv_interp_signs.py.

Tested

  • python -I tests/test_spirv_interp_signs.py -v (11 passed)
  • python -I tests/test_spirv_interp_bits.py -v (5 passed)
  • python -I tests/test_spirv_interp_f64_glsl.py -v (6 passed)
  • Detached upstream/main baseline negative control: failed with the new tests before the fix
  • python -I tests/test_ci_registration.py, python -I tests/test_text_write_line_endings.py, python -I scripts/check_no_game_data.py, python -I -m compileall -q, and git diff --cached --check

GPUs tested: n/a

AI-assisted: yes

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.
Claimed in the coordination issue as: SPIRV-INTERP-IEEE-DOMAIN-OVERFLOW-FMA-AND-BITCAST-SIGN (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191180874) ### What and why `tests/spirv_interp.py` previously raised Python exceptions or lost IEEE-754 sign/payload bits on valid edge-case inputs: - `Sqrt`, `InverseSqrt`, `Log2`, `Sin`, `Cos`, `Fract`, and `Exp2` called raw `math.*` / `2.0 ** x` functions directly, raising `ValueError` or `OverflowError` on negative/zero/infinite/huge operands instead of returning IEEE-754 `NaN`, signed infinity, `0.0`, or `-0.0`. - `Fma` converted `Fraction` sums unconditionally via `float(...)`, losing `-0.0` sign when `-0.0 * c + (-0.0)` summed to zero and raising `OverflowError` on overflowing binary64 results. - `OpFNegate` ran unary `-v`, stripping `_F32` bit-pattern payloads (including signalling NaN bits) and failing on tuple vector operands. This adds IEEE-754 domain/overflow/`-0.0`-safe evaluation in `glsl()`, preserves `_F32` bit patterns and vector/f64 NaN signs in `OpFNegate`, and adds regression tests in `tests/test_spirv_interp_signs.py`. ### Tested - `python -I tests/test_spirv_interp_signs.py -v` (11 passed) - `python -I tests/test_spirv_interp_bits.py -v` (5 passed) - `python -I tests/test_spirv_interp_f64_glsl.py -v` (6 passed) - Detached `upstream/main` baseline negative control: failed with the new tests before the fix - `python -I tests/test_ci_registration.py`, `python -I tests/test_text_write_line_endings.py`, `python -I scripts/check_no_game_data.py`, `python -I -m compileall -q`, and `git diff --cached --check` GPUs tested: n/a AI-assisted: yes ### 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. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
real-rusher commented 2026-11-26 12:23:30 +00:00

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_interp_signs.py: 11 tests OK. The PR's test file against main's interpreter: 1 failure and 2 errors, so the new tests fail on main and pass with the change. compileall clean.

AI-assisted: yes (Claude Code)

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_interp_signs.py: 11 tests OK. The PR's test file against main's interpreter: 1 failure and 2 errors, so the new tests fail on main and pass with the change. compileall clean. AI-assisted: yes (Claude Code)
wavehs reviewed 2026-11-26 19:45:28 +00:00
wavehs left a comment
No description provided.
**macOS arm64 check of #2266** (spirv_interp IEEE edge domains). A test-oracle change only; the new tests run here and the negative control confirms they bite. Tested head `d1b14075a` merged onto `main` `bdbc7e923` as merge `e575c7a77` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, CPython 3.15.2. **On the merge** - `python -I tests/test_spirv_interp_signs.py`: 11 tests OK. `test_spirv_interp_bits.py`, `test_spirv_interp_f64_glsl.py` and `test_ci_registration.py` also OK, so the new file is selected by CI's standard-library loop (`tests/test_ci_registration.py` passes). - The whole semantic suite on the merge: `500 files, 6367 tests, 0 files failed` - unchanged, as expected for a tests/ helper. - Every standalone user of `tests/spirv_interp.py` that can run without pytest passes: `test_rcp_iflag_mods.py`, `test_spirv_interp_bits.py`, `test_spirv_interp_f64_glsl.py`, `test_spirv_interp_signs.py`. `test_f16_hw_words.py`, `test_f16_op_sel_values.py`, `test_mode_round.py`, `test_u16_high_half.py` and `test_values.py` import pytest, which this interpreter does not have; they cannot be run here. **Negative control (new tests kept, `tests/spirv_interp.py` reverted to `main`)** - `FAILED (failures=1, errors=2)` in `test_spirv_interp_signs.py` - exactly the three tests that encode the new behaviour (`test_fma_negative_zero_and_f64_overflow`, `test_glsl_domain_and_overflow_ieee`, `test_op_fnegate_preserves_f32_nan_bits_and_vectors`); the file's other eight cases cover pre-existing interpreter behaviour and stay green. - `test_spirv_interp_bits.py` stays OK on the old implementation, which is right: it tests the bitcast/NaN-payload mechanism, not these edge domains. **Assessment** - The FMA zero-sign rule matches IEEE: a zero exact sum is `-0.0` only when the product and the addend are both negative zero, `+0.0` otherwise (`tests/spirv_interp.py:284-296`). The overflow path now returns the signed infinity instead of raising `OverflowError`, which is what previously made a test that hit it die with a traceback rather than compare values. - `_fneg` flipping the sign bit of a `_F32` (and of the binary64 pattern for a plain NaN) is the correct `OpFNegate` model: negating a NaN flips the sign bit and keeps the payload, where Python's unary `-` had dropped the `_F32.bits` it carries for exactly this reason. Vector recursion is right too. - `_ieee_exp2` under/overflow (Python raises `OverflowError` above binary64, but the result is pre-rounded to f32 by the caller) and `_ieee_log2` at `±0`/negative look right. **Nit: the "undefined on hardware" cases are choices, not measurements** `Sqrt` of a negative, `InverseSqrt` of `<= 0`, and `Sin`/`Cos` of `±inf` are undefined in GLSL.std.450, and the interpreter now picks NaN (`Sqrt`), signed infinity (`InverseSqrt(-0)`), and NaN for the trig infinities. That is a reasonable oracle, and no current fixture asks those questions, but it is a guess rather than something measured on RDNA2 or in SPIRV-Cross; if a translator test ever asserts `sin(inf)` or `sqrt(-1)` against this helper, the expected value will be whatever this function says, not what the console does. A one-line comment on `_ieee_trig`/`_ieee_sqrt` saying "undefined by the spec; NaN is our choice" would keep a later reader from treating it as measured. Nothing to change for this PR. **Nit**: `tests/test_spirv_interp_signs.py` mixes the new edge-domain cases with older coverage (signed compares, SMIN/SMAX, shifts, conversions). Not wrong, but it hides which cases belong to this change - the NC above had to be read case by case to see it. **Not run**: the five pytest-based users of the helper (no pytest on this interpreter), and any GPU; this change cannot affect a compiled module. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
tiguan26 approved these changes 2026-11-27 04:37:27 +00:00
tiguan26 left a comment

Approving d1b14075a.

spirv_interp is what many translator tests compute expected values with, so beyond its own tests I ran every test that imports it on this batch: 781 passed (main: 770, the difference being this PR's tests), so the IEEE edge cases (sqrt/rsqrt/log2 of negatives and zeros, exp2 overflow, trig of infinities, an exact-zero FMA's sign) and the NaN-preserving negate move no existing result.

Merged onto main d1dff7949 in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_spirv_interp_signs.py: 11 passed (main: 8). AI-assisted (Claude Code, Claude Opus 5.5).

Approving d1b14075a. `spirv_interp` is what many translator tests compute expected values with, so beyond its own tests I ran every test that imports it on this batch: 781 passed (main: 770, the difference being this PR's tests), so the IEEE edge cases (sqrt/rsqrt/log2 of negatives and zeros, exp2 overflow, trig of infinities, an exact-zero FMA's sign) and the NaN-preserving negate move no existing result. Merged onto `main` `d1dff7949` in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_spirv_interp_signs.py`: 11 passed (main: 8). AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 10:35:10 +00:00

#2266 | head d1b14075a48fcd598bb9b7d2e173a38ccd4db60e (merged cleanly with main 6c7f79343) | python -I tests/test_spirv_interp_signs.py | red on main: FAILED (failures=1, errors=2) (ValueError: math domain error on Sqrt(-1), TypeError on vector OpFNegate, and Fma(-0.0, 1.0, -0.0) returning +0.0 instead of -0.0 0x80000000) | green on PR+main: 11 tests OK | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited _fneg (preserving _F32 sNaN/qNaN payload bits and tuple vectors), _ieee_sqrt / _ieee_rsqrt / _ieee_log2 / _ieee_exp2 / _ieee_trig, Fract(+-inf) -> NaN, and Fma exact-zero sign (-0.0 when a*b and c are both negative zero) + f64 OverflowError -> +-inf handling in tests/spirv_interp.py. Also passed tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), tests/test_runner_calls_all_tests.py (3 OK), and scripts/check_no_game_data.py.

#2266 | head d1b14075a48fcd598bb9b7d2e173a38ccd4db60e (merged cleanly with main 6c7f79343) | `python -I tests/test_spirv_interp_signs.py` | red on main: `FAILED (failures=1, errors=2)` (`ValueError: math domain error` on `Sqrt(-1)`, `TypeError` on vector `OpFNegate`, and `Fma(-0.0, 1.0, -0.0)` returning `+0.0` instead of `-0.0` `0x80000000`) | green on PR+main: `11 tests OK` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `_fneg` (preserving `_F32` sNaN/qNaN payload bits and tuple vectors), `_ieee_sqrt` / `_ieee_rsqrt` / `_ieee_log2` / `_ieee_exp2` / `_ieee_trig`, `Fract(+-inf) -> NaN`, and `Fma` exact-zero sign (`-0.0` when `a*b` and `c` are both negative zero) + `f64` `OverflowError -> +-inf` handling in `tests/spirv_interp.py`. Also passed `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 15:07:10 +00:00

Red/green: pytest tests/test_spirv_interp_signs.py fails on main (3 failed, 8 passed, 3 subtests passed) and passes on head d1b14075a48f merged with main (11 passed, 3 subtests passed).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: `pytest tests/test_spirv_interp_signs.py` fails on main (3 failed, 8 passed, 3 subtests passed) and passes on head d1b14075a48f merged with main (11 passed, 3 subtests passed). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.