Skip to content

tests/semantic: a NaN the interpreter creates no longer depends on the host CPU (SHADER-723) - #4100

Open
AirKyzzZ wants to merge 3 commits into
ItsNotRexed:mainfrom
AirKyzzZ:fix/interp-host-independent-nan
Open

AirKyzzZ wants to merge 3 commits into
ItsNotRexed:mainfrom
AirKyzzZ:fix/interp-host-independent-nan

Conversation

@AirKyzzZ

Copy link
Copy Markdown

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

What and why

test_alu_vop3_mods_hw.py and test_alu_sdwa_hw.py (round 42) pass on x86-64 and fail on ARM64: 27 and 2 subtests on an Apple M3 Pro, same commit. spirv_mini_interp.py does its float arithmetic with host floats, and when an operation creates a NaN from non-NaN operands (0 * inf, inf - inf, inf + -inf) the sign comes from the CPU. x86-64 gives a negative quiet NaN, which happens to be what RDNA2 gives (the measured 0xFE00 / 0xFFC00000). ARM64 gives a positive one. Every failing word differs only in that sign bit.

_pin_nan now returns the negative quiet NaN when OpFAdd, OpFSub, OpFMul or the non-finite path of fma_f32 (product, then sum) creates a NaN. A NaN that comes in through an operand is left alone, and both hosts already propagate it the same way (first NaN operand, quieted). So nothing changes on x86, and ARM gets the same words. The explicit math.nan returns elsewhere (_fract, inf/inf in _fdiv, NMin/NMax) were already host-independent and are untouched.

The second commit pins it in test_interp.py: five created-NaN cases expect 0xFFC00000, and three propagated ones keep 0x7FC00001.

Tested

python -m unittest discover -s tests/semantic, CPU only:

Host main aa2988cc this branch
Apple M3 Pro, macOS 27.0.1, Python 3.14.0 36 failures, 2 errors 7 failures, 2 errors
Ryzen 5 2600X, Windows 10, Python 3.14 7 failures, 3 errors 7 failures, 3 errors, same tests

The failures left are the same tests on both hosts and already claimed: TEST-853 (test_cmp_class_f64), the test_loop_ballot_barrier finding, test_mutations (#3883). The extra x86 error is test_spirv_val_compliance, a tool-path issue on that box. It fails the same way before and after.

The new test_interp.py cases fail 5 subtests on ARM64 without the interpreter change. On x86 they pass either way, since that's the host whose behaviour is being pinned. 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, test interpreter only)

…pends on the host CPU

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
…eter self-test

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
@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.

…atch a regression too

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
@AirKyzzZ

Copy link
Copy Markdown
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.

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.

3 participants