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

Open
opened 2026-11-26 20:21:16 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1414
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/spirv-opt-type-and-decorate-soundness


📝 Commits (2)

  • fad7435 spirv_opt: enforce Result Type match on int/load folding and pin OpDecorateId operands from DCE
  • d9a6e2a test_spirv_opt: use OpShiftLeftLogical instead of invalid mixed-type OpBitFieldUExtract in fixture

📊 Changes

2 files changed (+116 additions, -12 deletions)

View changed files

📝 tests/test_spirv_opt.py (+95 -0)
📝 tools/spirv_opt.py (+21 -12)

📄 Description

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)).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1414 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/spirv-opt-type-and-decorate-soundness` --- ### 📝 Commits (2) - [`fad7435`](https://github.com/ubiquitarianism/leonida/commit/fad7435bcd741dd9b14602d8eb004d0c53b22771) spirv_opt: enforce Result Type match on int/load folding and pin OpDecorateId operands from DCE - [`d9a6e2a`](https://github.com/ubiquitarianism/leonida/commit/d9a6e2a58c217af97bfc3658117c944497cd4846) test_spirv_opt: use OpShiftLeftLogical instead of invalid mixed-type OpBitFieldUExtract in fixture ### 📊 Changes **2 files changed** (+116 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_spirv_opt.py` (+95 -0) 📝 `tools/spirv_opt.py` (+21 -12) </details> ### 📄 Description 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)`). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1443
No description provided.