Skip to content

spirv_mini_interp: pin the sign of a NaN made from non-NaN operands (semantic suite on arm64) - #5208

Closed
LloydNicholson wants to merge 1 commit into
ItsNotRexed:mainfrom
LloydNicholson:fix/semantic-nan-sign-arm64
Closed

LloydNicholson wants to merge 1 commit into
ItsNotRexed:mainfrom
LloydNicholson:fix/semantic-nan-sign-arm64

Conversation

@LloydNicholson

Copy link
Copy Markdown
Contributor

Fixes the finding in the coordination issue: #5156 (comment) (claimed in #5156 (comment); no item ID assigned yet).

Problem

IEEE 754 leaves the sign of a NaN that an operation makes from non-NaN operands (inf - inf, 0 * inf) to the implementation. spirv_mini_interp.py used Python's float arithmetic for these, so the sign came from the host CPU: 0xFFC00000 on x86-64, 0x7FC00000 on arm64. The RDNA2-measured words in test_alu_vop3_mods_hw and test_alu_sdwa_hw expect 0xFFC00000 (and 0xFE00 in f16 halves), so 29 MeasuredWords rows failed on Apple Silicon (27 + 2). The result was the same on Python 3.13 and 3.14, so this isn't the gh-130317 difference.

Change

Test-only, in tests/semantic/spirv_mini_interp.py. A small _pin_nan(r, *operands) returns 0xFFC00000 when r is a NaN and none of the operands was. It's applied to OpFAdd/OpFSub/OpFMul (binary32), binary64 OpFMul and the non-finite path of fma_f32. A NaN operand still propagates as before. x86-64 already produced this value, so results there shouldn't change.

Tested

Apple M4 Max, macOS 26.6.2, CPU only (no GPU involved; no game files):

before after
python -m unittest discover -s tests/semantic (Python 3.14.8) 6,245 tests: 32 failures, 2 errors 3 failures, 2 errors
test_alu_vop3_mods_hw, test_alu_sdwa_hw (3.13.12 and 3.14.8) 29 failures OK
python -m compileall OK OK

The 5 that remain are all in test_loop_ballot_barrier (test_divergent_loop_wave64_emulate_and_native_cs ×3, test_nested_divergent_loops_emulate and test_bug4b_loop_carried_split_sgpr_pair_ballot, Unsupported SPIR-V op OpFunctionCall). They're unrelated to this change and fail the same way on main before it (3faac557 and its parent). I haven't checked them on x86-64.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

IEEE 754 leaves the sign of a NaN that inf - inf or 0 * inf produces to the
implementation, and Python takes it from the host CPU: 0xFFC00000 on x86-64,
0x7FC00000 on arm64. The RDNA2-measured words in test_alu_vop3_mods_hw and
test_alu_sdwa_hw expect the negative one, so 29 of their rows failed on Apple
Silicon. OpFAdd, OpFSub, OpFMul (binary32 and binary64) and the non-finite path
of fma_f32 now return 0xFFC00000 for such a NaN; a NaN operand still propagates
unchanged, so x86-64 results are unchanged.

AI-assisted.

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

Copy link
Copy Markdown
Contributor

Independent CPU + SPIRV-Tools verification on Linux x86-64 (Python 3.13.5, spirv-as/spirv-val with LEONIDA_REQUIRE_SPIRV_TOOLS=1, head adaacaa9, base main 3faac557):

  • python3 -I -m py_compile tests/semantic/spirv_mini_interp.py — clean.
  • python3 -m unittest discover -s tests/semantic -p "test_alu_*_hw.py" -v — 9 tests OK (expected failures=1), including test_alu_vop3_mods_hw and test_alu_sdwa_hw.
  • Verified struct.unpack("<I", struct.pack("<f", f32(_MADE_NAN)))[0] == 0xFFC00000 and git diff --check clean.
  • Two small non-blocking notes:
    1. _fdiv right above (tests/semantic/spirv_mini_interp.py:966-967) still returns math.nan (0x7FC00000, positive NaN) for inf / inf instead of _MADE_NAN.
    2. A small direct unit test for _pin_nan (checking inf - inf, 0 * inf, fma(0, inf, 1), and verifying that a positive NaN input operand still propagates unchanged) would pin the helper directly on x86-64 CI too.

Looks good to merge.

@Atomic258

Copy link
Copy Markdown
Contributor

Independent check — interp change verified (Windows 11). All *nan* semantic suites pass at head (28/28: test_float_nan_fract, test_salu_nand_nor_saveexec_b64). Honest note: they pass on parent too — the PR ships no test pinning the NaN-sign-from-non-NaN-operands convention, so a regression of this choice wouldn't be caught. Worth a small case in test_float_nan_fract.py. AI-assisted (Devin); no game files.

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

At adaacaa on Windows AMD64, run_fragment and a two-lane run_workgroup agreed for generated and propagated NaNs. Invalid f32 arithmetic and FMA yielded 0xffc00000, packing to 0xfe00fe00. Input quiet NaNs retained their tested sign and payload in f32. For f64 multiply the generated high word was 0xfff80000. A control that forced freshly generated NaNs positive distinguished the pin from native x86 behavior. Physical ARM64 was outside this check.

AI-assisted independent CPU review.

@LloydNicholson

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #4069, which makes the same change and was claimed earlier; I missed it when I searched. The tests from this PR (29 MeasuredWords rows on Apple M4 Max) back up that fix.

@LloydNicholson
LloydNicholson deleted the fix/semantic-nan-sign-arm64 branch November 23, 2026 09:54
@sovljanskii

Copy link
Copy Markdown
Contributor

Tested adaacaa9 on arm64, the case this PR is for: Apple M4, macOS, Python 3.15.2, the whole tests/semantic suite (6,245 tests) on main 3faac557 and on this head.

failures errors
main 32 2
this PR 3 2
  • Fixed: exactly 29 tests pass only on the PR: 27 test_alu_vop3_mods_hw.MeasuredWords rows and 2 test_alu_sdwa_hw.MeasuredWords rows, the rows the finding lists.
  • Regressions: nothing fails only on the PR.
  • Left over: 3 failures and 2 errors in test_loop_ballot_barrier, identical on both. They aren't arm64-specific; the suite fails the same way on main's ubuntu CI run 45076072668 and is owned by other PRs.

This also answers the earlier note that the PR ships no test pinning the sign. On arm64 those 29 existing measured-word rows are that test: they fail without the change and pass with it. On x86 they pass either way. AI-assisted.

@dcastell1

Copy link
Copy Markdown
Contributor

Code read + CPU tests, adaacaa: pass for this change, with the caveat that the suite as a whole is not green on main yet.

  • Full semantic suite on this branch (56 min, 6,227 passed + 66,323 subtests): 6 failures, and all six are accounted for:
  • test_cmp_class_f64 is OK here too.

So nothing in #5208 regresses anything; it is the known red plus my toolchain, and it will look clean once #3995 lands.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested adaacaa on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_alu_*.py" -> Ran 1601 tests, OK (expected failures=1)
  • Negative control (spirv_mini_interp.py swapped to upstream/main): also OK - expected, the NaN-sign pin is a no-op on x86-64 by design (matters on arm64 hosts)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

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.

6 participants