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

Open
github-bot wants to merge 2 commits from github-bot/gh-1414 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 23:42 UTC · original
IlyassRS3:fix/spirv-opt-type-and-decorate-soundness → main · +116 −12 · 2 files · 2 commits · open

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)).
**@⁠IlyassRS3** on GitHub · 2026-11-24 23:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1414) `IlyassRS3:fix/spirv-opt-type-and-decorate-soundness` → `main` · +116 −12 · 2 files · 2 commits · open 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)`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:42 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 23:42 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 23:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1414#pullrequestreview-6049224540) · **reviewed** 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.
Author
Owner

@⁠GeorgiyDemo on GitHub · 2026-11-25 00:00 UTC · original

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.

**@⁠GeorgiyDemo** on GitHub · 2026-11-25 00:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1414#issuecomment-7148034719) 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.
Author
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 00:24 UTC · original

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.

**@⁠IlyassRS3** on GitHub · 2026-11-25 00:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1414#issuecomment-7148480238) 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`.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1414/head:github-bot/gh-1414'
git switch 'github-bot/gh-1414'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1414'
git switch 'github-bot/gh-1414'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1414'
git switch 'github-bot/gh-1414'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1414'
git switch 'main'
git merge --squash 'github-bot/gh-1414'
git switch 'main'
git merge --ff-only 'github-bot/gh-1414'
git switch 'main'
git merge 'github-bot/gh-1414'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.