Skip to content

tests/semantic: fix three mini SPIR-V interpreter correctness bugs - #4802

Open
NethminaYasas wants to merge 7 commits into
ItsNotRexed:mainfrom
NethminaYasas:tests/semantic/mini_interp_correctness
Open

NethminaYasas wants to merge 7 commits into
ItsNotRexed:mainfrom
NethminaYasas:tests/semantic/mini_interp_correctness

Conversation

@NethminaYasas

@NethminaYasas NethminaYasas commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new TEST item, #4571 (comment)

What and why

Three correctness fixes in the mini SPIR-V interpreter (tests/semantic/spirv_mini_interp.py), plus one test-header interface correction. They are six separate commits, in the exact order below, so any one can be split out on request:

  1. tests/semantic: GLSL unsigned min/max/clamp vectors are component-wise in the mini interpreter
  2. tests/semantic: typed PushConstant loads in the mini interpreter
  3. tests/semantic: vector-bool OpSelect in the mini interpreter
  4. tests/semantic: unsigned vector min/max/clamp accept constant composites
  5. tests/semantic: OpSelect accepts constant-composite vector conditions
  6. tests/semantic: list the vector output in the test entry-point interface

The three fixes are:

  • Component-wise unsigned GLSL std.450 min/max/clamp on integer vectors — instructions 38/41/44 (UMin/UMax/UClamp) are computed per component. The interpreter compared whole tuples, so UMin((0xffffffff,1), (1,0xffffffff)) returned component 1 = 0xffffffff instead of 1. A local vector() helper also normalizes OpConstantComposite vectors (stored as lists) and mixed list/tuple operands, and the result is a tuple; CodeRabbit found the constant-composite gap, which is fixed in commit 4. When minVal > maxVal, both the vector and scalar branches raise UnsupportedConstruct (the vector message carries a GLSL.std.450 prefix), matching the spec's undefined case — verified by execution. The signed variants and their scalar paths are untouched.
  • Typed 32-bit float PushConstant OpLoad — a %float PushConstant word is decoded with bits_f32 instead of being returned as a raw int (OpFMul used to reject it); integer loads and f16/f64 loads are unchanged.
  • Vector-bool OpSelect — a vector-of-bool condition selects each component from the matching operand, including constant-composite conditions and operands (CodeRabbit's other finding, fixed in commit 5). Scalar bool conditions still take the scalar branch with unchanged checks and error messages; the only difference on that path is that a list-valued operand is normalized to a tuple before selection. Commit 6 lists the test header's %vout Output variable in the OpEntryPoint interface so the synthetic module is valid for SPIR-V 1.4+.

Tested

CPU-only, hand-written SPIR-V; no GPU or driver, no game files. Based directly on upstream/main 98fa503f, not stacked, no Depends on:. Upstream has since moved to 3faac557 without touching any file in this PR.

  • python -B -m unittest test_interp -v (from tests/semantic/): 49 tests OK (41 before this work; four new tests).
  • python -B -m unittest discover -s tests/semantic, same runner for both: upstream/main 98fa503f 6245 tests, 3 failures, 3 errors, 16 skipped, 1 xfailed; this branch 6255 tests (+10), 3 failures, 3 errors, 16 skipped, 1 xfailed. The failure/error/skip counts are pre-existing and unchanged by this PR. The remaining failures are unrelated to these files: test_nested_divergent_loops_emulate, test_bug4b_loop_carried_split_sgpr_pair_ballot, test_spirv_val_compliance, and three test_divergent_loop_wave64_emulate_and_native_cs cases.
  • python -m pytest tests/test_semantic_translator_use.py -q: 3 passed (no allowlist change). python -m compileall -q make_game.py tools tests: exit 0.
  • Mutation checks: breaking UMin, UMax and UClamp in turn makes only its own test fail; reverting the typed load makes only the PushConstant test fail; disabling vector OpSelect makes only that test fail. Restoring the unsigned branch's tuple-only guard fails only the two constant-composite unsigned tests; reverting OpSelect normalization fails only the two constant-vector select tests. All mutations were restored and none was committed.
  • Test-merged against tests/semantic: OpBitCount and the signed GLSL ops at the width of a 64-bit type in both interpreters (TEST-700) #3508: clean against its earlier head 829af7fd on 2026-11-23; its current head a25e265 conflicts in spirv_mini_interp.py (merge aborted). This PR touches the same area as tests/semantic: OpBitCount and the signed GLSL ops at the width of a 64-bit type in both interpreters (TEST-700) #3508; whichever lands second needs a trivial rebase. No Depends on: line is needed.
  • The exact-string replace in test_inputs_and_push_constants_can_be_seeded now asserts the entry-point line contains %in, and the test still checks the seeded result 0x3FC00100.

Out of scope / follow-ups: signed SMin/SMax/SClamp per-component is a follow-up after #3508. Mixed scalar/vector operands and mismatched-length vectors raise raw Python TypeError/IndexError; they are invalid SPIR-V (GLSL.std.450 requires equal component counts and widths) and out of scope. Older interpreter vector paths (NMin/NMax, FMax/FClamp, Fma) still use tuple-only checks and have the same constant-composite (list) limitation; that broader cleanup is a known follow-up, out of scope here.

AI-assisted: yes (OpenCode)

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. (N/A — no translator rule changed.)

…e in the mini interpreter

GLSL.std.450 UMin/UMax/UClamp take integer vectors and compute per component. The interpreter compared whole tuples, so e.g. UMin((0xffffffff,1),(1,0xffffffff)) returned 0xffffffff in component 1 instead of 1. Signed variants are deliberately left for a follow-up.
OpLoad's result type is the type of the loaded object. A %float PushConstant word was returned as a raw int, so OpFMul rejected it. Decode 32-bit floats while leaving integer loads unchanged.
SPIR-V OpSelect allows a vector of bool conditions; each component selects from the corresponding operand. The interpreter rejected it outright. Scalar behaviour and its error message are unchanged.
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 20a8df84 on Windows 11, CPython 3.14.7, CPU only (no GPU, no game files): pass.

All 45 tests in tests/semantic/test_interp.py pass. The 14 interpreter guard tests and 18 wave interpreter tests also pass.

I ran the same 45 tests with the interpreter from merge base 98fa503f. All three vector min/max/clamp tests fail. The float push-constant load and vector-condition select tests both raise errors. The other 40 tests pass.

I also loaded three float push constants and immediately bitcast each one back to an unsigned word. The results exactly match the inputs: 0x80000000 (negative zero), 0x7f800001 (signaling NaN), and 0xffc01234 (negative quiet NaN with a payload). This run covers the focused interpreter tests.

AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 20a8df84 on Windows 11, CPython 3.15.2, CPU only (no GPU, no game files): pass.

Command: python -I -m unittest discover -s tests/semantic -p test_interp.py -v

Result: 45/45 tests OK, including the six new tests (test_vector_umin_is_component_wise, test_vector_umax_is_component_wise, test_vector_uclamp_is_component_wise, the PushConstant typed-load test, test_vector_bool_select_is_component_wise).

Red control: git checkout origin/main -- tests/semantic/spirv_mini_interp.py → same command gives FAILED (failures=3, errors=2) — e.g. UMin((0xffffffff,1),(1,0xffffffff)) returns (1, 4294967295) instead of (1, 1), matching the PR's described bug. Restored the PR file; git status --porcelain clean.

AI-assisted (Devin); no game files.

@memorypressure

Copy link
Copy Markdown

Verified on current main: merges cleanly; tests/semantic/test_interp.py passes 45/45 via unittest discover, and the full semantic sweep (6,251 tests) shows no change versus a main baseline run in the same session — the only reds are the five pre-existing test_loop_ballot_barrier emulate-mode failures, which I re-confirmed on plain main. Vector-bool OpSelect, typed push-constant loads and component-wise UMin/UMax/UClamp all match spec with scalar-path guards included. No supersession — main has not touched the mini interpreter since your base. Checks pending; the sweep above is the CPU verdict. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@NethminaYasas
NethminaYasas marked this pull request as ready for review November 23, 2026 10:16
@NethminaYasas

Copy link
Copy Markdown
Contributor Author

thanks @shdann @Atomic258 @memorypressure

OpConstantComposite vectors are lists in the interpreter. Normalize list or tuple operands in the unsigned vector branch so UMin/UMax/UClamp compute per component for constant vectors and mixed list/tuple operands; the result is a tuple. Scalars and signed variants are unchanged.
OpConstantComposite vectors are lists in the interpreter. Normalize condition and operands with the same helper used by unsigned min/max/clamp, so vector OpSelect works with constant vectors and mixed list/tuple operands. Scalar bool conditions, their checks and error messages are unchanged.
HEADER declares %vout as an Output variable. Include it in OpEntryPoint's interface so the synthetic module is valid for SPIR-V 1.4+; update the exact-string replace in test_inputs_and_push_constants_can_be_seeded and assert the replacement happened.
@Atomic258

Copy link
Copy Markdown
Contributor

Tested 20a8df8 on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_interp.py" -> OK

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

Resolved the OpExtInst hunk by keeping both the unsigned vector branch and upstream's signed integer width validation.
@JRamos1993

Copy link
Copy Markdown
Contributor

Tested eff61588 merged onto main 561b65a8 on Windows 11, CPython 3.14.3, CPU only: pass.

  • python -I -m unittest discover -s tests/semantic -p test_interp.py: 60 tests OK.
  • py_compile on the 2 changed Python files: OK; git diff --check: clean.

No game files. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested eff6158 merged with main 8575d36 on Linux (WSL2) Python 3.12.16 (-I): pass
Clean merge. -I -m unittest discover -s tests/semantic -p test_interp.py: 62 tests OK; also OK (65 tests) with #4768 merged on top (same files, no conflict).
Negative control: spirv_mini_interp.py reverted to main gives 3 failures and 6 errors (push-constant float load, constant-composite select/UClamp/UMin/UMax, component-wise vector UClamp/UMin/UMax), so the new tests discriminate.

@NethminaYasas

Copy link
Copy Markdown
Contributor Author

thanks @JRamos1993 @kapitche @Atomic258

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested eff61588 on Linux, CPython 3.14, CPU only, merged onto main 867d922f: pass

  • python -m unittest discover -s tests/semantic -p test_interp.py: Ran 62 tests, OK. With only the interpreter file(s) reverted to main: FAILED (failures=3, errors=6), so the new tests exercise the change.

Beyond the PR's own tests, the agent ran a differential harness against an exact reference (Python integers and Fraction with independent rounding): 16 cases on vector OpSelect, the typed PushConstant load and vector UMin/UMax/UClamp, 0 disagreements. That harness run is the agent's; I re-ran only the test commands above.

Two notes from the same agent. A vector ordered comparison used as the select condition (OpULessThan on two-component operands) still gives the wrong result with this PR alone, because the comparison itself is the part our #8449 fixes; with both merged it is right. And the typed PushConstant load covers scalar float only: a v2float member still fails with a TypeError, which predates this PR. No conflict with #8449 or #8149 by its trial merges.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting.

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