Skip to content

tests/semantic: OpBitcast keeps signalling-NaN bits in the mini interpreter (TEST-67) - #533

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
BBareth:fix/mini-interp-snan-bits
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
BBareth:fix/mini-interp-snan-bits

Conversation

@BBareth

@BBareth BBareth commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #509 (TEST-67 in #374).

What and why

bits_f32 / f32_bits in tests/semantic/spirv_mini_interp.py went through struct's binary32 ↔ double conversion, and that conversion quiets a signalling NaN by setting bit 22. Every word in 0x7f800001..0x7fbfffff / 0xff800001..0xffbfffff that passed through a float came back changed. That covers an OpBitcast round trip and an integer exported through a float output. SPIR-V OpBitcast is bit-exact, so this was an interpreter bug and the translator was not at fault. About 0.4% of random words hit it. Two seeded tests fail on main because of it:

Fix. A float made by OpBitcast is now a small float subclass (_F32) that keeps its exact 32-bit pattern, which is the wrapper #509 suggests. OpCopyObject, composites, OpSelect, OpPhi, variables and outputs pass the value on unchanged, and f32_bits returns the kept pattern. Arithmetic on it returns a plain float, so the pattern is dropped there, because SPIR-V leaves NaN result bits open. Float operations still see an ordinary NaN (OpIsNan is true, OpFOrdEqual is false).

Tested

Three new tests in tests/semantic/test_interp.py:

  • Four sNaN words plus a quiet NaN, 1.0 and -0.0 go uint → OpBitcast → OpCopyObject → composite → OpSelect → Function variable → both a uint and a float Output. Each comes out unchanged. Against main's interpreter exactly the four sNaN words fail.
  • OpIsNan / OpFOrdEqual on a bitcast sNaN.
  • OpFAdd / OpFMul / OpFNegate of one still give a NaN. Only "a NaN" is asserted, not its bits.

Results:

  • python -m unittest discover -s tests/semantic: 711 tests OK. Main has 708 tests with 1 failure, the seeded v_ashrrev_i64 case.
  • pytest tests/test_rdna2spirv_pixelinput.py tests/test_shader_diff.py tests/test_mem_diff.py tests/test_salu_diff.py: 35 passed. These are the other users of the interpreter. On main the s_load subtest fails.
  • python -m compileall -q make_game.py tools tests: clean.

Python 3.12.15 on macOS arm64. CPU only with hand-written modules, no game data. This change doesn't involve a GPU or driver.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • Translator contract is inapplicable: this changes only the test interpreter, not a translator rule.

🤖 Generated with Claude Code

…preter (TEST-67)

bits_f32/f32_bits went through struct's binary32 <-> double conversion,
which quiets a signalling NaN (sets bit 22). Any word in
0x7f800001..0x7fbfffff or 0xff800001..0xffbfffff that passed through a
float (an OpBitcast round trip, or an integer exported through a float
output) came back changed, although OpBitcast is bit-exact in SPIR-V.
About 0.4% of random words hit it, so seeded tests failed by chance:
test_alu_shift_b64's v_ashrrev_i64 case and test_mem_diff's s_load
family both fail on main.

A float made by OpBitcast is now a float subclass that keeps its exact
pattern; copies, composites, selects, variables and outputs pass it on,
and arithmetic returns a plain float, which drops it (SPIR-V leaves NaN
result bits open). Tests cover four sNaN words through every pass-through
path, OpIsNan/OpFOrdEqual on one, and that arithmetic still gives a NaN.

Fixes ItsNotRexed#509.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@NWX10

NWX10 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 3fe9203 on Windows 11, Python 3.12 (no GPU): python -I -m unittest discover -s tests/semantic -p "test_*.py" runs 711 tests, OK. tests/test_mem_diff.py passes (9 tests), including the s_load case with today's date seed 20261122, which fails on main.

I opened #543 for the same item with the same approach (a float subclass that carries the 32-bit pattern), so I'm closing it and this PR continues. If they aren't covered here yet, three cheap extra cases are in #543's tests: OpCopyObject and OpSelect on a signalling NaN, a float Output variable, and arithmetic on a signalling NaN (the result must still be the quiet NaN, 0xFFA3A095 + 1.0 gives 0xFFE3A095). Copy them if you want them.

@73N1X

73N1X commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Independent Windows x64 / Python 3.12.10 CPU validation on detached commit 3fe9203fc6e41832166912a3a2b9bc0eba9a34f2: pass.

  • python -I -m unittest discover -s tests/semantic: 711 tests OK.
  • python -I tests/test_mem_diff.py: 9 tests OK, including the s_load family and its negative-control canaries.

I also ran the failing cases in a separate clean worktree of main at 91b36cf, without either TEST-67 proposal:

  • python -I -m unittest discover -s tests/semantic -p test_alu_shift_b64.py: 4 tests, 1 failure; the seeded v_ashrrev_i64 case returns 18446744073707692181 rather than 18446744073703497877.
  • python -I tests/test_mem_diff.py: 9 tests, 1 failure; s_load seed 20261122 program 8 expects 0x7f8295f4 and observes 0x7fc295f4.

Both failures disappear with this exact PR head. This independently confirms that the two interpreter-related failures inherited by #388 are addressed. These are synthetic CPU-only tests; no GPU or game data was used. This report does not claim the whole project CI is green: the separate llvmpipe subgroup issue is TEST-78.

I tested both current TEST-67 proposals under the same environment and am reporting exact-head evidence on each, without proposing a third fix or changing either author's code. The choice/coordination between them remains with the authors and maintainer.

@IAmNotSiob IAmNotSiob left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: with this interpreter, #454's original seeded case (v_ashrrev_i64, n=0x3e32dea5, x=0xf47412b2f2e132ea, which exports 0xffa3a095) passes unchanged, and test_interp.py is OK. That's the same approach I had drafted as part of #575, a float subclass carrying the bits with arithmetic dropping them. I've removed my copy from #575 in favour of this one. LGTM. AI-assisted review (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 014e6e0 into ItsNotRexed:main Nov 22, 2026
7 of 9 checks passed
ItsNotRexed pushed a commit that referenced this pull request Nov 22, 2026
test_preview_with_no_fitting_layout_reports_the_problem runs the tool,
which imports numpy before it parses its arguments, so without numpy the
test sees a ModuleNotFoundError traceback instead of "no layout" and
fails. Its neighbours that run the CLI already skip without numpy. CI's
Windows check job has no numpy, so this is its next failure once the
mem_diff one (#533) is fixed. The pytest wrapper of the same test skips
too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested 3fe9203 on Windows 11 Pro 26200, Python 3.13.15: pass.

  • tests/semantic: OK, including test_alu_shift_b64, which fails on main (91b36cf) with Python 3.13 (v_ashrrev_i64, n=0x3e32dea5, x=0xf47412b2f2e132ea) and passes on main with Python 3.14

@samu-mi8

Copy link
Copy Markdown

Tested 3fe9203 on Windows 11, Python 3.11.0, CPU only (no GPU, no game data): pass.

  • python -I -m unittest discover -s tests/semantic: 711 tests OK. Current main (ee97b84) runs 708 with 1 failure, the seeded v_ashrrev_i64 case (n=0x3e32dea5, x=0xf47412b2f2e132ea).
  • python -I tests/test_mem_diff.py: 9 tests OK. On main, the s_load family fails.
  • Merged onto current main (ee97b84, no conflicts): the same results, and python -m compileall -q make_game.py tools tests is clean.

About the red checks: neither one comes from this change.

  • check fails in test_preview_with_no_fitting_layout_reports_the_problem because tools/swizzle_search.py can't import numpy on the runner. The latest CI run on main passes, so a rebase should clear it.
  • linux-gpu-tools fails cs_w32_lane_wrap and cs_wqm_w32 on llvmpipe. The Runtime workflow on main fails the same way.

Two small things, neither blocking:

  1. _F32 can't be copied or pickled. copy.deepcopy(bits_f32(0x7f800001)) raises struct.error: required argument is not an integer, because the default __reduce_ex__ passes the float value back to __new__, which expects the bits. Nothing does this today (the only copy in tests/semantic is a copy.copy of a list of masks in test_barrier_control.py), so this is only latent. One line fixes it, and I checked that deepcopy and pickle then keep 0x7f800001:
    def __getnewargs__(self):
        return (self.bits,)
  2. tests/spirv_interp.py, the older one-lane interpreter that tests/test_values.py and tests/test_f16_op_sel_values.py use, has the same u2f/f2u round trip: f2u(u2f(0xffa3a095)) gives 0xffe3a095. No current test hits it as far as I can see, but a seeded test there would hit it the same way. It could get the same treatment in this PR or a follow-up. (tools/cpu_ref.py canonicalizes NaN on purpose, so it's fine.)

tomgamer22 added a commit to tomgamer22/Leonida that referenced this pull request Nov 22, 2026
spirv_mini_interp.py: keep main's _F32 (sNaN bits, ItsNotRexed#533) and its vector-aware
RoundEven/Floor/Ceil/Trunc; keep this branch's fma_f32, OpFDiv and
Exp2/Log2/Sqrt/InverseSqrt. regen_fixtures.py: both sides' shaders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ItsNotRexed pushed a commit that referenced this pull request Nov 22, 2026
…533)

u2f()/f2u() went through struct's binary32 <-> double conversion, which
quiets a signalling NaN (sets bit 22), so a word in 0x7f800001..
0x7fbfffff / 0xff800001..0xffbfffff that passed through an OpBitcast
came back changed: 0xffa3a095 became 0xffe3a095. This is the second
interpreter (test_values, test_f16_op_sel_values, test_f16_hw_words,
test_interp_p1_p2); #533 fixed the same bug in spirv_mini_interp.

Same fix: u2f() returns a float subclass that keeps the pattern and
f2u() returns it; copies, selects, composites and stores pass it on,
and arithmetic gives a plain float. New test_spirv_interp_bits.py
checks four sNaN words through every pass-through path, OpIsNan, and
that arithmetic still gives a NaN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

spirv_mini_interp: OpBitcast and float outputs quiet signalling-NaN bit patterns (bit 22 flips)

7 participants