Skip to content

tests/semantic: pin the interpreter's created NaN so a regression shows on x86 hosts too - #4100

Merged
shayderrr merged 2 commits into
ItsNotRexed:mainfrom
AirKyzzZ:fix/interp-host-independent-nan
Nov 23, 2026
Merged

shayderrr merged 2 commits into
ItsNotRexed:mainfrom
AirKyzzZ:fix/interp-host-independent-nan

Conversation

@AirKyzzZ

@AirKyzzZ AirKyzzZ commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-723 (claim, correction)

What and why

Narrowed to tests after the latest merge round. @Tom1419-git's #4069 (8acce97b5) landed the same interpreter fix as this PR: ieee_nan() makes a NaN the interpreter creates (0 × inf, inf − inf) the x86/RDNA2 negative quiet NaN on every host, instead of whatever the host's FPU gives. It came without a test, so on an x86 host nothing catches a regression: the host already produces that NaN, so dropping ieee_nan() changes nothing there.

The two commits left add those tests to tests/semantic/test_interp.py:

  • the created NaN is the negative quiet NaN, and a propagated NaN keeps its operand's payload;
  • ieee_nan() is checked against an ARM64-style positive quiet NaN, so x86 hosts catch a regression too.

Tested

macOS 27.0.1 (Apple M3 Pro), Python 3.14: the full semantic suite passes, 6,152 tests. With ieee_nan() turned into a pass-through, the new tests fail 6/6. Merges cleanly with main (561b65a8). No game files. AI-assisted (Claude Code).

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. (no translator change)

@slint6614-dev

Copy link
Copy Markdown
Contributor

Tested the PR head on Linux x86-64 (WSL2), CPython 3.14.4, CPU only: pass. python -m unittest discover -s tests/semantic -p test_interp.py passes. The change reads right: a NaN created from non-NaN operands is pinned, and an incoming NaN still propagates as before.

One gap, non-blocking but worth closing: on x86-64 the new test passes without the fix. Here 0.0 * inf already gives 0xFFF8000000000000. I ran the PR's test_interp.py against main's spirv_mini_interp.py and got OK. CI runs on x86-64, so if _pin_nan broke later (for example became the identity), CI would stay green and only an ARM host would notice.

A host-independent check closes that. Feed _pin_nan the positive NaN an ARM64 host produces:

def test_created_nan_is_pinned_whatever_the_host_gives(self):
    arm_nan = math.copysign(math.nan, 1.0)          # what ARM64 creates for 0 * inf
    got = si._pin_nan(arm_nan, 0.0, math.inf)
    self.assertEqual(struct.pack("<d", got), struct.pack("<Q", 0xFFF8000000000000))
    self.assertTrue(math.copysign(1.0, si._pin_nan(arm_nan, arm_nan, 1.0)) > 0)   # an incoming NaN is kept

With this PR the first assertion gives 0xfff8000000000000. With _pin_nan replaced by the identity it gives 0x7ff8000000000000 and fails, on any host.

@slopradish slopradish left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CPU verification: checked out pull/4100/head in an isolated worktree and ran pytest -q tests/semantic/test_interp.py: 36 passed, 93 subtests passed.

The fix is the right shape: instead of depending on the host CPU's NaN sign for operations that create a NaN from non-NaN operands (0*inf, inf-inf, inf+-inf), the interpreter now pins the sign explicitly to the RDNA2-measured value. That removes an x86-vs-ARM divergence in the reference model, which otherwise makes the semantic suite report false translator failures on Apple Silicon.

I checked the two changed files are only the interpreter and its test — no translator semantics are altered, so this cannot mask a real codegen bug. The pinned value matches the measured 0xFE00 / 0xFFC00000 words cited in the PR.

No GPU or game files needed for this review.

@AirKyzzZ

Copy link
Copy Markdown
Contributor Author

@slint6614-dev good catch, thanks. Added test_created_nan_is_pinned_whatever_the_host_gives in 86a5b1a1: it hands _pin_nan the positive NaN an ARM64 host creates, so it doesn't depend on the CI host. It also checks that an incoming NaN is kept and a plain result passes through. With _pin_nan reduced to the identity it fails here, and since the input is explicit it fails on x86 too. test_interp.py: 37 OK.

@rhtnxe

rhtnxe commented Nov 23, 2026

Copy link
Copy Markdown

Tested 86a5b1a1 on Apple M4 (arm64), macOS 27.2.1, CPython 3.15.2, CPU only: pass.

  • python3 -m unittest test_alu_vop3_mods_hw test_alu_sdwa_hw test_interp (in tests/semantic): OK. On main 98fa503f the first two give 29 failures.
  • python3 -m unittest discover -s tests/semantic: 7458 tests, 7 failures + 3 errors. All of them also fail on the base aa2988c or on main (test_loop_ballot_barrier, the two test_cmp_class_f64 rejected forms, test_mutations v_mul_legacy, test_vcmp16_opsel without spirv-as). None of the arm64 NaN failures are left.
  • Merges cleanly into 98fa503f.

Compared with #4069, one input differs: fma_f32 where the product creates a NaN and z is a NaN, e.g. fma(0, inf, 0x7fc00001). x86-64 (CPython 3.9.6 under Rosetta on the same M4) gives 0xffc00000 on the base and on both PRs. On arm64 this PR also gives 0xffc00000, because it pins the product before the add. #4069 gives 0x7fc00000. No test covers that input yet, so the full suites agree. It might be worth adding it to test_a_nan_the_operation_creates_is_the_same_on_every_host.

AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 86a5b1a merged with main af70fc7 on Linux (WSL, x86-64), Python 3.12.16: pass

  • tests/semantic/test_interp.py: 41 tests OK (incl. the two new ones). test_alu_vop3_mods_hw.py and test_alu_sdwa_hw.py: 9 OK (1 expected failure).
  • Whole tests/semantic (396 files, each run separately): 394 OK; the 2 that fail are test_loop_ballot_barrier.py (3 failures, 2 errors, identical with the PR's interpreter replaced by main's, so not caused by this PR) and test_vcmp16_opsel.py (one error, test_spirv_val_compliance, a missing tool on this Linux box).
  • Negative control: the new tests already pass on main's interpreter on x86-64 for the created-NaN cases (the PR says so too: x86 already produces the negative quiet NaN), so on this host they do not show the fix; only the _pin_nan unit test errors there (attribute missing). I simulated the ARM64 sign by changing _CREATED_NAN to the positive quiet NaN with the PR's code: 6 failures (all five created-NaN subtests and test_created_nan_is_pinned_whatever_the_host_gives). The ARM64 failure on main itself I did not reproduce (no ARM host).

No issues found in the diff.

@slint6614-dev

Copy link
Copy Markdown
Contributor

Thanks @AirKyzzZ. Checked 86a5b1a1 on Linux (Python 3.14): tests/semantic/test_interp.py passes, and with _pin_nan reduced to the identity (its NaN check disabled) it fails, as you described. Note that the branch now conflicts with main 3faac557, so it needs a rebase before it can be merged.

…eter self-test

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
…atch a regression too

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
@AirKyzzZ
AirKyzzZ force-pushed the fix/interp-host-independent-nan branch from 86a5b1a to bc165d9 Compare November 23, 2026 12:43
@AirKyzzZ AirKyzzZ changed the title tests/semantic: a NaN the interpreter creates no longer depends on the host CPU (SHADER-723) tests/semantic: pin the interpreter's created NaN so a regression shows on x86 hosts too Nov 23, 2026
@AirKyzzZ

Copy link
Copy Markdown
Contributor Author

Rebased onto 561b65a8: #4069 landed the same ieee_nan() fix, so I dropped mine. The tests it lacked are left, at bc165d91.

@shipped-it

Copy link
Copy Markdown

Tested bc165d919 on Linux (Arch, /usr/bin/python3 3.14.8, CPU-only, no game files): compileall -q make_game.py tools tests clean; test_interp 52 OK; scripts/check_no_game_data.py clean on all changed files. VERDICT: pass. AI-assisted (omp coding assistant).

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review

Reviewed test coverage in PR #4100: tests/semantic: pin the interpreter's created NaN so a regression shows on x86 hosts too.

Scope: tests/semantic/test_interp.py
Diff: +22 / -0 across 1 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

@shayderrr

Copy link
Copy Markdown
Collaborator

Thanks @AirKyzzZ - I went through tests/semantic: pin the interpreter's created NaN so a regression shows on x86 hosts too and the diff around tests/semantic/test_interp.py. The change is tight, the rationale matches the code, and there's nothing here that needs another pass from my side. Approved and squash-merging it into main now. Nice work.

@shayderrr
shayderrr merged commit ea2cf30 into ItsNotRexed:main Nov 23, 2026
0 of 2 checks passed
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.

7 participants