Skip to content

spirv_mini_interp: support OpUDiv, OpUMod, OpSDiv, and OpSRem - #7378

Open
check3D wants to merge 1 commit into
ItsNotRexed:mainfrom
check3D:spirv-mini-interp-umod
Open

check3D wants to merge 1 commit into
ItsNotRexed:mainfrom
check3D:spirv-mini-interp-umod

Conversation

@check3D

@check3D check3D commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #7231 (comment) (new TEST item, ID pending assignment)

What and why

tools/rdna2spirv.py emits OpUMod for every --wave64-emulate 3-slot exchange ring buffer (%x_it % 3), OpUDiv for scratch-swizzle chunking, and OpSRem for texture-wrap modulo, but tests/semantic/spirv_mini_interp.py rejected all of them with UnsupportedConstruct — blocking run_workgroup() from executing translated --wave64-emulate loop shaders.

  • tests/semantic/spirv_mini_interp.py: Implement OpUDiv, OpUMod, OpSDiv, and OpSRem (32-bit and 64-bit) and add them to _PURE:
    • OpUDiv/OpUMod: unsigned division and modulo over wmask(a[0]), returning UNDEF on divisor 0.
    • OpSDiv/OpSRem: signed truncating-toward-zero division and remainder whose sign matches operand 1 (SPIR-V spec, e.g. OpSRem(-1, 4) == -1), returning UNDEF on divisor 0 or signed INT_MIN / -1 overflow.
  • tests/semantic/test_interp.py: Replace OpUDiv with an unmodelled SPIR-V opcode (OpOuterProduct) in test_unsupported_constructs_are_rejected.
  • tests/semantic/test_interp_int_divmod.py: Add stdlib-only self-tests covering 32/64-bit OpUDiv/OpUMod/OpSDiv/OpSRem, all sign combinations, UNDEF handling and _PURE propagation, and an end-to-end synthetic --wave64-emulate divergent loop compute shader verified to emit OpUMod and execute accurately across all 64 lanes in run_workgroup().

Does not touch tools/rdna2spirv.py or overlap #4768 / #7200.

Tested

Linux (Ubuntu 24.04), Python 3.12.16, CPU only (no GPU, synthetic inputs only, no game data):

  • Negative control before the fix: python3 -I tests/semantic/test_interp_int_divmod.py fails on every test (UnsupportedConstruct: OpUDiv/OpSRem/OpUMod).
  • python3 -I tests/semantic/test_interp_int_divmod.py -v — 5 tests OK.
  • python3 -m unittest discover -s tests/semantic -p "test_interp*.py" — 206 tests OK.
  • python3 -I tests/semantic/test_wave64_scmp_masks.py — 15 tests OK.
  • python3 -I tests/test_ci_registration.py — 14 tests OK.
  • python3 -m compileall -q tests/semantic/spirv_mini_interp.py tests/semantic/test_interp.py tests/semantic/test_interp_int_divmod.py — clean.
  • git diff --check — clean.

AI-assisted: yes (Antigravity)

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 — test interpreter only; no translator rule added.)

@neoisg8

neoisg8 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Independent Review of PR #7378

Verdict: LGTM / Approve.

Environment

  • Windows 11 Pro x64, CPython 3.14.3.
  • CPU only; zero game data or GPU hardware needed.
  • Evaluated PR head commit debbef65d cleanly rebased onto latest origin/main (5cf3dafe3).

Negative Control Reproduction on main

On current upstream main (5cf3dafe3), evaluating hand-written or translated SPIR-V containing OpUDiv, OpUMod, OpSDiv, or OpSRem through spirv_mini_interp.py failed:

# Evaluating Fragment with %res = OpUDiv %uint %c7 %c2:
# Raised: UnsupportedConstruct: OpUDiv

As noted in the PR description, this blocked running spirv_mini_interp.run_workgroup on translated --wave64-emulate loop compute shaders (which emit OpUMod for the 3-slot ring exchange buffer) and shaders using OpSRem / OpUDiv.

Verification with PR #7378 Applied

  1. Instruction Lowering & Spec Compliance:
    • OpUDiv/OpUMod: properly computes unsigned quotient/remainder masked by wmask(a[0]). Divisor == 0 produces UNDEF (propagates safely through _PURE when masked/unselected by OpSelect, or raises if observed).
    • OpSDiv/OpSRem: properly models signed truncating-toward-zero division where the sign of OpSRem matches operand 1 (per SPIR-V spec). Divisor == 0 and signed INT_MIN / -1 overflow produce UNDEF.
    • Replaces OpUDiv with unmodeled OpOuterProduct in test_unsupported_constructs_are_rejected.
  2. Test Suite Verification on Windows:
    python -m unittest discover -s tests/semantic -p "test_interp_int_divmod.py"
    # Ran 5 tests in 0.132s - OK
    
    python -m unittest discover -s tests/semantic -p "test_interp*.py"
    # Ran 206 tests in 3.792s - OK
    
    python -I tests/test_ci_registration.py
    # Ran 14 tests in 4.541s - OK
    End-to-end execution of the synthetic --wave64-emulate divergent loop with OpUMod across 64 lanes in run_workgroup verified exact results.
  3. Hygiene Checks:
    • python -m compileall -q tests/semantic/spirv_mini_interp.py tests/semantic/test_interp.py tests/semantic/test_interp_int_divmod.py -> exit 0.
    • python -I scripts/check_no_game_data.py tests/semantic/spirv_mini_interp.py tests/semantic/test_interp_int_divmod.py -> exit 0.
    • Standard library only, synthetic shaders only.

AI-assisted.

@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

Code read + run of 51f1761 on Windows 11, Python 3.11.9: LGTM.

  • OpSDiv truncates toward zero and OpSRem takes the dividend's sign, as SPIR-V specifies (not Python's floor semantics); I cross-checked the SRem rule against x - trunc(x/y)*y for every pair in [-9, 9]: no mismatch.
  • Divisor 0 and INT_MIN / -1 give UNDEF, matching the spec's undefined cases; the values[res] = UNDEF; continue shape is the one the interpreter already uses (spirv_mini_interp.py:775-776).
  • test_interp_int_divmod.py: 5 tests pass. Full suite: 444 files, 5894 tests; the only 3 failing files are the same ones that fail on main (test_float_nan_fract, test_image_atomics_guard, test_image_store_d16, each with an open fix PR), so no regression.

AI-assisted (Claude Code).

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

Verified: passes all 57 tests in tests/semantic/test_interp_int_divmod.py and tests/semantic/test_interp.py. Models OpUDiv, OpUMod, OpSDiv, and OpSRem accurately including 32/64-bit signed truncation semantics and clean UNDEF handling on zero-division/INT_MIN overflow.

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 51f1761 on Windows 11 (26200.9974), Python 3.12.0: pass.

  • python -I tests/semantic/test_interp_int_divmod.py: 5/5 pass; python tests/semantic/test_interp.py (the file's own path setup, no -I): 52/52.
  • Baseline with the merge-base spirv_mini_interp.py and the new tests: 21 subtest errors, all UnsupportedConstruct for the new opcodes, so the suite exercises the change.
  • Spot checks against the spec: UDiv/UMod at the high bit and 64-bit maxima; SDiv/SRem in all four sign combinations truncate toward zero with the remainder's sign from operand 1; divisor 0 and INT_MIN / -1 give UNDEF (safe when unselected by OpSelect, raising when observed); UNDEF propagates through _PURE.
  • Code read: the four opcodes are added to _PURE and to the integer branch behind the width mask; existing ops are unchanged. The test_interp.py edit only replaces the old OpUDiv unsupported case with OpOuterProduct.
  • python -m compileall and scripts/check_no_game_data.py: pass.

Synthetic SPIR-V only; no game data.

AI-assisted.

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