spirv_opt: enforce Result Type match on int/load folding and pin OpDecorateId operands from DCE (SPIRV-OPT-TYPE-DECORATE-SOUNDNESS) #1414

Merged
IlyassRS3 merged 2 commits from upstream-pr/1414 into main 2026-11-28 17:56:10 +00:00
IlyassRS3 commented 2026-11-24 23:42:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-24T23:42:23Z.


Item ID: SPIRV-OPT-TYPE-DECORATE-SOUNDNESS

Summary

Fixes two SPIR-V soundness bugs in tools/spirv_opt.py:

  • Mixed-signedness integer folding type mismatch (_fold_int_op, _optimize_function): SPIR-V permits signed (%int) and unsigned (%uint) operands in OpIAdd, OpISub, OpIMul, OpBitwiseAnd, OpBitwiseOr, OpBitwiseXor, shifts, and OpBitFieldUExtract whose Result Type has the opposite signedness. Previously _fold_int_op returned operand SSA IDs directly (e.g. %u = OpIAdd %uint %s %u0 folded to %s of type %int, and %u = OpIMul %uint %u %i0 folded to %i0), producing invalid SPIR-V with mismatched types.
    • Absorbing-constant folds (* 0, & 0, | ~0, 0 << x, 0 >> x) now look up int_const_by_val.get((res_ty, ...)) so they fold to the Result Type's constant when available.
    • Both _fold_int_op aliasing and OpLoad slot forwarding now guard id_type.get(folded) == args[0], and const_type also records OpUndef / OpSpecConstant* types without marking them as compile-time constants.
  • Dangling OpDecorateId operand IDs after DCE (_optimize_function, optimize): OpDecorateId %target <Decoration> %operand previously protected %operand from folding/CSE via decorated_ids, but backward DCE could still delete %operand while leaving OpDecorateId in the module header (header cleanup only drops OpDecorateId when toks[1], the target, is deleted). Operand IDs in OpDecorateId's args[2:] are now tracked in pinned_ids and excluded from DCE.

Tests & Verification

  • AI-assisted: yes
  • Host / hardware: macOS (Apple M5, arm64)
  • python3 -m compileall -q make_game.py tools tests — OK
  • python3 -I scripts/check_no_game_data.py tools/spirv_opt.py tests/test_spirv_opt.py — OK
  • python3 -I tests/test_spirv_opt.py -v — 14/14 OK
  • python3 -I scripts/run_semantic_tests.py tests/semantic — 475 files, 6220 tests, 0 files failed in 65 s
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I tests/test_ci_registration.py — 16/16 OK
  • Mutation proof (tests/test_spirv_opt.py on unpatched upstream/main): 2 failures (FAILED (failures=2)).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5482252661 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1414). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-24T23:42:23Z. --- Item ID: `SPIRV-OPT-TYPE-DECORATE-SOUNDNESS` ## Summary Fixes two SPIR-V soundness bugs in `tools/spirv_opt.py`: - **Mixed-signedness integer folding type mismatch (`_fold_int_op`, `_optimize_function`)**: SPIR-V permits signed (`%int`) and unsigned (`%uint`) operands in `OpIAdd`, `OpISub`, `OpIMul`, `OpBitwiseAnd`, `OpBitwiseOr`, `OpBitwiseXor`, shifts, and `OpBitFieldUExtract` whose `Result Type` has the opposite signedness. Previously `_fold_int_op` returned operand SSA IDs directly (e.g. `%u = OpIAdd %uint %s %u0` folded to `%s` of type `%int`, and `%u = OpIMul %uint %u %i0` folded to `%i0`), producing invalid SPIR-V with mismatched types. - Absorbing-constant folds (`* 0`, `& 0`, `| ~0`, `0 << x`, `0 >> x`) now look up `int_const_by_val.get((res_ty, ...))` so they fold to the `Result Type`'s constant when available. - Both `_fold_int_op` aliasing and `OpLoad` slot forwarding now guard `id_type.get(folded) == args[0]`, and `const_type` also records `OpUndef` / `OpSpecConstant*` types without marking them as compile-time constants. - **Dangling `OpDecorateId` operand IDs after DCE (`_optimize_function`, `optimize`)**: `OpDecorateId %target <Decoration> %operand` previously protected `%operand` from folding/CSE via `decorated_ids`, but backward DCE could still delete `%operand` while leaving `OpDecorateId` in the module header (header cleanup only drops `OpDecorateId` when `toks[1]`, the target, is deleted). Operand IDs in `OpDecorateId`'s `args[2:]` are now tracked in `pinned_ids` and excluded from DCE. ## Tests & Verification - **AI-assisted**: yes - **Host / hardware**: macOS (Apple M5, arm64) - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/spirv_opt.py tests/test_spirv_opt.py` — OK - `python3 -I tests/test_spirv_opt.py -v` — **14/14 OK** - `python3 -I scripts/run_semantic_tests.py tests/semantic` — **475 files, 6220 tests, 0 files failed in 65 s** - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I tests/test_ci_registration.py` — 16/16 OK - **Mutation proof** (`tests/test_spirv_opt.py` on unpatched `upstream/main`): **2 failures** (`FAILED (failures=2)`).
copilot-pull-request-reviewer[bot] commented 2026-11-24 23:42:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T23:42:39Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6049224540 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1414#pullrequestreview-6049224540). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-24T23:42:39Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
GeorgiyDemo commented 2026-11-25 00:00:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-25T00:00:32Z.


Tested fad7435bc on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 364ab4eab (Python tool only, no GPU involved):

  • python3 -I tests/test_spirv_opt.py at this head: 13/14 pass, 1 fails — test_mixed_signedness_folding_never_produces_type_mismatch fails inside _validate with SPIRV-Tools v2026.4 (spirv-val):
    error: line 34: Expected Base Type to be equal to Result Type: BitFieldUExtract
    %24 = OpBitFieldUExtract %uint %20 %uint_0 %uint_32
  • The same file on main fails 2 of 14: this test and test_decorate_id_operand_is_not_deleted_by_dce. The latter passes at this head, so the OpDecorateId/DCE half of the fix is verified.
  • The BFE case reads as a fixture/validator conflict rather than a remaining fold: the test asserts that the mixed-signedness OpBitFieldUExtract %uint %s survives (%s is %int), while spirv-val 2026.4 rejects exactly that Base/Result pair. Either the fixture needs a %uint base, or the optimizer has to rewrite the base to match; as written the new test cannot pass with this validator.

AI-assisted; synthetic fixtures only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7148034719 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1414#issuecomment-7148034719). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-25T00:00:32Z. --- Tested `fad7435bc` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `364ab4eab` (Python tool only, no GPU involved): - `python3 -I tests/test_spirv_opt.py` at this head: **13/14 pass, 1 fails** — `test_mixed_signedness_folding_never_produces_type_mismatch` fails inside `_validate` with **SPIRV-Tools v2026.4** (`spirv-val`): `error: line 34: Expected Base Type to be equal to Result Type: BitFieldUExtract` `%24 = OpBitFieldUExtract %uint %20 %uint_0 %uint_32` - The same file on `main` fails 2 of 14: this test and `test_decorate_id_operand_is_not_deleted_by_dce`. The latter passes at this head, so the `OpDecorateId`/DCE half of the fix is verified. - The BFE case reads as a fixture/validator conflict rather than a remaining fold: the test asserts that the mixed-signedness `OpBitFieldUExtract %uint %s` survives (`%s` is `%int`), while `spirv-val` 2026.4 rejects exactly that Base/Result pair. Either the fixture needs a `%uint` base, or the optimizer has to rewrite the base to match; as written the new test cannot pass with this validator. AI-assisted; synthetic fixtures only, no game data.
IlyassRS3 commented 2026-11-25 00:24:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T00:24:38Z.


Thanks for catching that with spirv-val @GeorgiyDemo! You're completely right: per SPIR-V §3.42.15, OpBitFieldUExtract requires Base to match Result Type (unlike OpIAdd, OpBitwiseAnd, and OpShiftLeftLogical, which explicitly allow signedness differences between Base/Operand and Result Type).

Pushed d9a6e2a58 replacing the invalid mixed-type OpBitFieldUExtract in the test fixture with OpShiftLeftLogical %uint %s %u0 (valid SPIR-V, exercises the exact same identity-fold guard). Verified 14/14 OK on this branch and FAILED (failures=2) on upstream/main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7148480238 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1414#issuecomment-7148480238). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T00:24:38Z. --- Thanks for catching that with `spirv-val` @GeorgiyDemo! You're completely right: per SPIR-V §3.42.15, `OpBitFieldUExtract` requires `Base` to match `Result Type` (unlike `OpIAdd`, `OpBitwiseAnd`, and `OpShiftLeftLogical`, which explicitly allow signedness differences between `Base`/`Operand` and `Result Type`). Pushed `d9a6e2a58` replacing the invalid mixed-type `OpBitFieldUExtract` in the test fixture with `OpShiftLeftLogical %uint %s %u0` (valid SPIR-V, exercises the exact same identity-fold guard). Verified `14/14 OK` on this branch and `FAILED (failures=2)` on `upstream/main`.
zmasi commented 2026-11-28 13:01:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T13:01:14Z.


#1414 | head d9a6e2a58c217af97bfc3658117c944497cd4846 (merged cleanly onto main d48a957bb9988afce424552a3c0c8d01ef12320f) | Windows 11 (10.0.26200), CPython 3.12.10, spirv-as / spirv-val (1.4.363.1), NVIDIA GeForce RTX 3090 (Vulkan 1.4.341, driver 610.88, LEONIDA_VK_VALIDATION=1)

  • Red on main (d48a957bb9988afce424552a3c0c8d01ef12320f): tests/test_spirv_opt.py (with spirv-val 1.4.363.1 active) fails on main (spirv-val rejects mixed %int/%uint identity/absorbing folds substituting %int IDs into %uint OpCompositeConstruct, and DCE deletes the OpDecorateId %pinned_ref operand).
  • Green on PR + main:
    • python -I tests/test_spirv_opt.py -v (with Khronos spirv-val 1.4.363.1 on PATH): pass (OK).
    • Live RTX 3090 vk_harness (LEONIDA_VK_VALIDATION=1, spirv-as + spirv-val clean) executing a spirv_opt.optimize()-processed mixed %int/%uint fragment shader (%s = -5 (0xfffffffb), OpIAdd %uint %s %u0, OpBitwiseAnd %uint %s %s, OpIMul %uint %u_add %i0, OpBitwiseOr %uint %u_add %im1): 4096/4096 pixels exact (0xfffffffb, 0xfffffffb, 0x0, 0xffffffff) (0 Khronos validation errors).
    • scripts/check_no_game_data.py and test_text_write_line_endings.py: pass (0 CRLF).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239631140 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1414#issuecomment-7239631140). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T13:01:14Z. --- #1414 | head `d9a6e2a58c217af97bfc3658117c944497cd4846` (merged cleanly onto `main` `d48a957bb9988afce424552a3c0c8d01ef12320f`) | Windows 11 (`10.0.26200`), CPython `3.12.10`, `spirv-as` / `spirv-val` (`1.4.363.1`), NVIDIA GeForce RTX 3090 (Vulkan `1.4.341`, driver `610.88`, `LEONIDA_VK_VALIDATION=1`) - **Red on `main` (`d48a957bb9988afce424552a3c0c8d01ef12320f`)**: `tests/test_spirv_opt.py` (with `spirv-val` `1.4.363.1` active) fails on `main` (`spirv-val` rejects mixed `%int`/`%uint` identity/absorbing folds substituting `%int` IDs into `%uint` `OpCompositeConstruct`, and DCE deletes the `OpDecorateId` `%pinned_ref` operand). - **Green on `PR + main`**: - `python -I tests/test_spirv_opt.py -v` (with Khronos `spirv-val` `1.4.363.1` on `PATH`): pass (`OK`). - Live RTX 3090 `vk_harness` (`LEONIDA_VK_VALIDATION=1`, `spirv-as` + `spirv-val` clean) executing a `spirv_opt.optimize()`-processed mixed `%int`/`%uint` fragment shader (`%s = -5` (`0xfffffffb`), `OpIAdd %uint %s %u0`, `OpBitwiseAnd %uint %s %s`, `OpIMul %uint %u_add %i0`, `OpBitwiseOr %uint %u_add %im1`): `4096/4096` pixels exact `(0xfffffffb, 0xfffffffb, 0x0, 0xffffffff)` (`0` Khronos validation errors). - `scripts/check_no_game_data.py` and `test_text_write_line_endings.py`: pass (`0` CRLF). AI-assisted.
MoonEyes commented 2026-11-28 17:56:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:56:07Z.


Red/green: tests/test_spirv_opt.py fails 2 assertions on main (mixed-signedness fold type mismatch, OpDecorateId operand deleted by DCE) and passes 14/14 with the PR.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243400893 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1414#issuecomment-7243400893). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:56:07Z. --- Red/green: tests/test_spirv_opt.py fails 2 assertions on main (mixed-signedness fold type mismatch, OpDecorateId operand deleted by DCE) and passes 14/14 with the PR. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1414
No description provided.