Skip to content

tests/spirv_interp: hold integers as 32-bit patterns; signed ops convert themselves (TEST-720) - #3645

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
kotorvr:fix/legacy-interp-signedness
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
kotorvr:fix/legacy-interp-signedness

Conversation

@kotorvr

@kotorvr kotorvr commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

TEST-720 (listed in #3574). Claim: #3232 (comment)

What was wrong

tests/spirv_interp.py runs translator output for tests/test_f16_hw_words.py, test_values.py, test_mode_round.py and others. It kept a value of a signed type as a negative Python int and a value of an unsigned type as a non-negative one, and used both raw. SPIR-V defines its instructions on the bits of the operands, not on their types, so wherever instruction and type disagreed the result was wrong:

module before right
OpSLessThanEqual 0x80000000u 1u false true
OpShiftRightLogical (int -8) 1 0xfffffffc 0x7ffffffc
OpULessThan (int -8) 1u true false
OpConvertUToF (int -8) 0xc1000000 (-8.0) 0x4f800000 (2^32)
SMin / SMax of uint values unsigned order signed order
OpFDiv 1.0 -0.0 +inf -inf

A translator change that feeds a uint into a signed compare (or an int into an unsigned one) would have been evaluated wrongly against the hardware-measured words, without any error.

What changed

An integer is always its unsigned 32-bit pattern (constants, OpBitcast, OpConvertFToS, OpBitFieldSExtract). OpSLessThanEqual, OpSGreaterThanEqual, SMin and SMax read their operands through s32(). OpFDiv by a zero takes the sign of the zero into account, and NaN / 0 is now a NaN (it was an infinity). The module docstring states the rule.

Tests

  • New tests/test_spirv_interp_signs.py (8 tests, hand-written modules). On main 7 of the 8 fail.
  • The tests that run translator output through this interpreter give the same results as before. On main aa2988cc that is nine files: python -m pytest tests/test_f16_hw_words.py tests/test_f16_op_sel_values.py tests/test_mode_round.py tests/test_values.py tests/test_rcp_iflag_mods.py tests/test_spirv_interp_bits.py tests/test_spirv_interp_f64_glsl.py tests/test_u16_high_half.py tests/semantic/test_valu_add_f64.py: 350 passed and 1 failed with this change, and the same 350 passed and 1 failed with main's interpreter. The failure is test_u16_high_half.py::test_d16_buffer_store_no_redundant_extract_after_shr16, which fails on main without this PR: spirv-val rejects the translated module ("Operand ... requires a previous definition"), before the interpreter runs.
  • tests/semantic/test_valu_add_f64.py is the only file under tests/semantic that imports this interpreter, and it is in that set.
  • python -m unittest discover -s tests/semantic: 7,456 tests; the only failures are 7 failures and 2 errors that main aa2988cc has without this PR (in test_loop_ballot_barrier, test_mutations and test_cmp_class_f64: the same 9, checked on a clean checkout of main), so this PR adds none.

Open #3526 (TEST-661, f16 NaN helpers) also edits tests/spirv_interp.py, on different lines; git merge-tree of the two is clean. Open #3642 (binary64 OpFDiv, OpFConvert, Sqrt/InverseSqrt) edits the same file next to the binary32 OpFDiv this changes; that trial merge is clean too.

Windows 11, Python 3.13.7. No GPU involved.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@slint6614-dev slint6614-dev 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.

Reviewed at 70154750. The interpreter now keeps integer values as 32-bit patterns and converts only where an operation is signed, which fixes signedness mismatches at those consumers. The new hand-written SPIR-V tests cover signed and unsigned comparisons, shifts, conversions, signed min/max, and signed-zero division.

Validation on this checkout: python -I tests/test_spirv_interp_signs.py (8/8), python -I tests/test_spirv_interp_bits.py (4/4), python -I tests/test_spirv_interp_f64_glsl.py (4/4), and python -I tests/semantic/test_valu_add_f64.py (8/8); git diff --check is clean. The broader pytest selection in the PR description could not be rerun because pytest is not installed in this environment.

@whattheflop whattheflop 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.

AI-assisted independent review at 70154750 versus f847748c. On uv CPython 3.12.13, the eight stock interpreter suites passed 297 tests and 72 subtests on both commits. The new suite passed 8/8 on head and 1/8 on parent. I assembled and validated another 46 synthetic modules for Vulkan 1.2, which confirmed the scalar signedness gains and exposed a vector regression.

Vector SMin/SMax: With valid signed vectors (1, 2) and (-1, 1), head raises TypeError in s32(tuple) for both instructions. Parent returns the expected words for these inputs: SMin ffffffff/00000001 and SMax 00000001/00000002. A crossed-component case also shows that the parent's lexicographic tuple comparison was already wrong in general, and head still uses that comparison for UMin/UMax. Use the scalar operators per component for these four GLSL instructions and test crossed vectors. Dispatch

The focused fixtures also use an interpreter-only scaffold: module-scope Function variables, stores with signed values through a uint pointer, a signed bitfield extract with different base/result types, and an out-of-range float-to-int conversion that assumes saturation. The independent modules used a validator-clean scaffold. Moving locals into the function, matching operand/store types, and choosing representable conversion inputs would let the committed cases run through the validator. Fixture

CPU only, no game data.

…ert themselves

The interpreter kept a value of a signed type as a negative Python int and a
value of an unsigned type as a non-negative one, and used both raw. SPIR-V
defines its signed and unsigned instructions on the bits, so wherever the
instruction and the type disagreed the result was wrong:

  OpSLessThanEqual 0x80000000u 1u      false (true)
  OpShiftRightLogical (int -8) 1       0xfffffffc (0x7ffffffc)
  OpULessThan (int -8) 1u              true (false)
  OpConvertUToF (int -8)               -8.0 (2^32)
  SMin / SMax of uint values           unsigned order

Integers are now always the unsigned 32-bit pattern, and OpS* compares, SMin
and SMax read them through s32(). Also x / -0.0 gave +inf for a positive x.
The 297 tests that run translator output through this interpreter give the
same results.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kotorvr
kotorvr force-pushed the fix/legacy-interp-signedness branch from 7015475 to f69db1a Compare November 22, 2026 22:12
@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main aa2988cc (head f69db1a0); one figure in the description changed: main gained a ninth test file that uses this interpreter (tests/test_u16_high_half.py), so the importer run is now 350 passed and 1 failed, the same with and without this PR (the failure is on main already, spirv-val rejects that test's translated module). The full semantic suite adds no failure: 7,456 tests, and the only 9 failing are the ones main aa2988cc has without this PR. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested f69db1a merged with main 98fa503: redundant. Merging this head into main gives a tree identical to main: the changes are already in main through merge commit 378c8d7 ("Merge pull request #3645"). Nothing left to test; this PR can probably be closed.

@ItsNotRexed
ItsNotRexed merged commit f361cde into ItsNotRexed:main Nov 23, 2026
2 of 7 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.

5 participants