Skip to content

spirv_mini_interp: OpBitFieldInsert and OpBitField{SU}Extract use the result width - #8149

Open
ne1i wants to merge 1 commit into
ItsNotRexed:mainfrom
ne1i:spirv-mini-interp-bitfield-width
Open

ne1i wants to merge 1 commit into
ItsNotRexed:mainfrom
ne1i:spirv-mini-interp-bitfield-width

Conversation

@ne1i

@ne1i ne1i commented Nov 23, 2026

Copy link
Copy Markdown

The three bit-field opcodes in tests/semantic/spirv_mini_interp.py hard-coded 32 bits, although OpTypeInt 64 is a modelled type and SPIR-V defines them at the width of Result Type:

  • OpBitFieldInsert masked its result with the 32-bit mask, so every bit of a 64-bit Base outside the insert mask (the whole high word) was dropped.
  • OpBitFieldSExtract sign-extended to 32 bits only, so a 64-bit extract of a negative field gave 0x00000000FFFFFFFC where 0xFFFFFFFFFFFFFFFC is specified.
  • The undefined-input check was Offset + Count > 32, so a valid 64-bit extract at Offset 32 raised UnsupportedConstruct.

The width now comes from Result Type (types.bits / wmask), as spirv_wave_interp.py already does for the same opcodes; the two interpreters disagreed on these inputs before. 32-bit results and the 32-bit rejection are unchanged.

New test test_bitfield_operations_use_the_result_width in tests/semantic/test_interp.py: four hand-built modules compared against constants derived from the specification.

Tested on Linux, CPython 3.14, CPU only: python -m unittest discover -s tests/semantic -p test_interp.py 53 OK; with only the interpreter reverted, 2 failures and 1 error. The full suite (scripts/run_semantic_tests.py tests/semantic) was run once by the authoring agent: 447 files, 2 failing (test_image_atomics_guard, test_image_store_d16), the same two that fail on unmodified main. The translator does not emit 64-bit OpBitField* today, so this affects hand-written modules and the interpreter's own tests, not current translator output.

Separate from #7733 (carry / extended multiply at 64 bits), #7378 (integer division) and #4768 (binary64 Fma / Sqrt).

Claim: #8047.

AI-assisted (DeepSeek V4.1 Flash wrote it; reviewed and re-verified with Claude Code).

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

Independent CPU review of b3872f9eee3b177ad00f985c22389f678adfbaf9, applied to current main dde82f27e994337094afc476ded0b1426f6e87e2: no blocking correctness findings.

Native Windows, CPython 3.11.15; no GPU, game files, LLVM or SPIR-V toolchain:

  • python -m unittest discover -s tests/semantic -p test_interp.py: 53 tests passed.
  • Negative control: retained the PR's test and restored only spirv_mini_interp.py to main. The new regression gave two failures (insert high bits and signed extension) and one UnsupportedConstruct error (extract at offset 32). Restoring the fix returned all 53 tests to green.
  • Additional scratch probe: 288 valid scalar cases passed across both supported integer widths (32 and 64), all three opcodes, zero/all-one/alternating bases, offsets 0/1/half-width/last-bit/width, and zero/one/remaining-width counts. Expected values were obtained by splicing strings of bits rather than copying the interpreter's bitwise implementation. All 18 out-of-range cases (offset at width with count 1, count width+1, and offset width+1 with count 0, for each opcode/width) raised UnsupportedConstruct.
  • check_no_game_data.py on both changed files and git diff --check: passed.

The width lookup and mask match the scalar integer types this interpreter supports. Insert preserves Base outside the field; signed extraction fills the full result width; zero-count and exact-width cases remain correct. This agrees with the SPIR-V bit-field rules and the existing wave interpreter.

Optional coverage improvement: include a zero-count/offset-at-width case and a 64-bit out-of-range case in the committed regression; the implementation passed these in the scratch probe, but the new four subcases do not pin these boundaries. This review did not run the full semantic suite or claim hardware verification.

Claim: #8047 (comment)
AI-assisted (OpenAI Codex).

@MiguelMatMar

Copy link
Copy Markdown

Tested b3872f9 on Windows 11 Pro es-ES (cp1252), CPython 3.14.2, CPU only, no game data: pass. tests/semantic/test_interp.py (via unittest discover -s tests/semantic -p test_interp.py): 53 OK. With only tests/semantic/spirv_mini_interp.py reverted to main: failures=2, errors=1. Code read: the width now comes from the result type (types.bits / wmask), so 32-bit results keep their old masks and refusals. LGTM.

(AI-assisted check, Claude Code.)

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

Independently verified b3872f9e on Windows 11 (Python 3.14.8, CPU-only, no game data):

  • Clean merge check against upstream/main (dde82f27): git merge-tree --write-tree exit 0, git diff --check exit 0, python -I -m compileall -q tests/semantic/spirv_mini_interp.py tests/semantic/test_interp.py exit 0.
  • python -I -m unittest discover -s tests/semantic -p test_interp.py: 53/53 OK on HEAD b3872f9e.
  • Negative control (reverted tests/semantic/spirv_mini_interp.py to base dde82f27 with the PR tests): 2 failures, 1 error (OpBitFieldInsert %ulong, OpBitFieldSExtract %ulong, and UnsupportedConstruct: bit-field extract beyond 32 bits on OpBitFieldUExtract %ulong at offset 32).
  • Diff review: types.bits(a[0]) and wmask(a[0]) properly generalize bounds checks, sign extension, and result masking for OpBitField{U,S}Extract and OpBitFieldInsert to the result width (including cnt == 0 and 64-bit widths).

@AsgerCode

Copy link
Copy Markdown

Tested b3872f9 on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): pass

What I ran:

  • tests/semantic/test_interp.py: 53 tests, OK. With the merge base spirv_mini_interp.py: failures=2, errors=1, all in the new test.
  • Merge with current main (dfe4e4a, 12 commits ahead of the merge base): clean. main has not changed these files since the merge base.
  • The translator still emits only 32-bit OpBitField* (all its uses in tools/rdna2spirv.py are %uint/%int). The semantic files that cover those uses all pass: test_alu_bfe 14, test_alu_bfi 23, test_bfi_known_mask 3, test_alu_bfm_b32 5, test_bfm_known_operands 2, test_alu_bitfield_rev 41, test_salu_bfe_64 44, test_scalar_alu_bitfield 31, test_salu_bitfield_ins_u32 6, test_salu_bfm64_add_i32 11, test_wave32_bfm64_data 6, test_alu_int_misc 7, test_pack_u16 28, test_valu_int16 16.
  • To make sure that those files reach the changed code, I made two temporary mutations in the PR's interpreter:
    • With no sign extension in SExtract, test_alu_bfe gives 46 failures and test_scalar_alu_bitfield gives 1.
    • With Insert that drops Base, test_alu_bfi gives 1 failure and test_bfi_known_mask gives 2.
  • 26 hand-made modules, each checked against a bit-by-bit reading of the SPIR-V rules. Each module runs through both spirv_mini_interp and spirv_wave_interp:
Case 32-bit 64-bit
Offset + Count = width (U/SExtract at 28+4 / 60+4) ok ok (was refused)
SExtract of the top bit only; SExtract of the full width ok ok (was refused)
Count = 0, also with Offset = width: Extract gives 0, Insert gives Base ok ok (was refused)
Insert or SExtract across bit 31/32; Insert with bits above Count in Insert; full-width Insert ok ok (was refused)
Low field: Insert keeps the high word; SExtract fills 64 bits n/a ok (was 0x89abcd5f / 0xfffffffc)
Offset + Count = width + 1; Offset 33 with Count 0; Offset 0xffffffff refused refused
32-bit uint/int Offset and Count with a 64-bit Base n/a ok (was refused)

Merge base: 13/26 match the spec, and all of the 32-bit cases are among the 13. This PR: 26/26 match. The two interpreters now agree on every case. The PR also changes one more case: a vector result (OpBitFieldUExtract %v2uint). On the merge base it raised TypeError: unsupported operand type(s) for >>: 'list' and 'int'. Now it raises UnsupportedConstruct, as spirv_wave_interp does.

Overlap: no other open PR changes the OpBitField* code in spirv_mini_interp.py. #7378, #7733 and #4768 merge cleanly with this PR.

AI-assisted (Claude Code).

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