spirv_opt: enforce Result Type match on int/load folding and pin OpDecorateId operands from DCE (SPIRV-OPT-TYPE-DECORATE-SOUNDNESS) #1414
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!1414
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/spirv-opt-type-and-decorate-soundness"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request successfully merged and closed
Item ID:
SPIRV-OPT-TYPE-DECORATE-SOUNDNESSSummary
Fixes two SPIR-V soundness bugs in
tools/spirv_opt.py:_fold_int_op,_optimize_function): SPIR-V permits signed (%int) and unsigned (%uint) operands inOpIAdd,OpISub,OpIMul,OpBitwiseAnd,OpBitwiseOr,OpBitwiseXor, shifts, andOpBitFieldUExtractwhoseResult Typehas the opposite signedness. Previously_fold_int_opreturned operand SSA IDs directly (e.g.%u = OpIAdd %uint %s %u0folded to%sof type%int, and%u = OpIMul %uint %u %i0folded to%i0), producing invalid SPIR-V with mismatched types.* 0,& 0,| ~0,0 << x,0 >> x) now look upint_const_by_val.get((res_ty, ...))so they fold to theResult Type's constant when available._fold_int_opaliasing andOpLoadslot forwarding now guardid_type.get(folded) == args[0], andconst_typealso recordsOpUndef/OpSpecConstant*types without marking them as compile-time constants.OpDecorateIdoperand IDs after DCE (_optimize_function,optimize):OpDecorateId %target <Decoration> %operandpreviously protected%operandfrom folding/CSE viadecorated_ids, but backward DCE could still delete%operandwhile leavingOpDecorateIdin the module header (header cleanup only dropsOpDecorateIdwhentoks[1], the target, is deleted). Operand IDs inOpDecorateId'sargs[2:]are now tracked inpinned_idsand excluded from DCE.Tests & Verification
python3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/spirv_opt.py tests/test_spirv_opt.py— OKpython3 -I tests/test_spirv_opt.py -v— 14/14 OKpython3 -I scripts/run_semantic_tests.py tests/semantic— 475 files, 6220 tests, 0 files failed in 65 spython3 -I tests/test_text_write_line_endings.py— 3/3 OKpython3 -I tests/test_ci_registration.py— 16/16 OKtests/test_spirv_opt.pyon unpatchedupstream/main): 2 failures (FAILED (failures=2)).Tested
fad7435bcon Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), againstmain364ab4eab(Python tool only, no GPU involved):python3 -I tests/test_spirv_opt.pyat this head: 13/14 pass, 1 fails —test_mixed_signedness_folding_never_produces_type_mismatchfails inside_validatewith 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_32mainfails 2 of 14: this test andtest_decorate_id_operand_is_not_deleted_by_dce. The latter passes at this head, so theOpDecorateId/DCE half of the fix is verified.OpBitFieldUExtract %uint %ssurvives (%sis%int), whilespirv-val2026.4 rejects exactly that Base/Result pair. Either the fixture needs a%uintbase, 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.
Thanks for catching that with
spirv-val@GeorgiyDemo! You're completely right: per SPIR-V §3.42.15,OpBitFieldUExtractrequiresBaseto matchResult Type(unlikeOpIAdd,OpBitwiseAnd, andOpShiftLeftLogical, which explicitly allow signedness differences betweenBase/OperandandResult Type).Pushed
d9a6e2a58replacing the invalid mixed-typeOpBitFieldUExtractin the test fixture withOpShiftLeftLogical %uint %s %u0(valid SPIR-V, exercises the exact same identity-fold guard). Verified14/14 OKon this branch andFAILED (failures=2)onupstream/main.#1414 | head
d9a6e2a58c217af97bfc3658117c944497cd4846(merged cleanly ontomaind48a957bb9988afce424552a3c0c8d01ef12320f) | Windows 11 (10.0.26200), CPython3.12.10,spirv-as/spirv-val(1.4.363.1), NVIDIA GeForce RTX 3090 (Vulkan1.4.341, driver610.88,LEONIDA_VK_VALIDATION=1)main(d48a957bb9988afce424552a3c0c8d01ef12320f):tests/test_spirv_opt.py(withspirv-val1.4.363.1active) fails onmain(spirv-valrejects mixed%int/%uintidentity/absorbing folds substituting%intIDs into%uintOpCompositeConstruct, and DCE deletes theOpDecorateId%pinned_refoperand).PR + main:python -I tests/test_spirv_opt.py -v(with Khronosspirv-val1.4.363.1onPATH): pass (OK).vk_harness(LEONIDA_VK_VALIDATION=1,spirv-as+spirv-valclean) executing aspirv_opt.optimize()-processed mixed%int/%uintfragment shader (%s = -5(0xfffffffb),OpIAdd %uint %s %u0,OpBitwiseAnd %uint %s %s,OpIMul %uint %u_add %i0,OpBitwiseOr %uint %u_add %im1):4096/4096pixels exact(0xfffffffb, 0xfffffffb, 0x0, 0xffffffff)(0Khronos validation errors).scripts/check_no_game_data.pyandtest_text_write_line_endings.py: pass (0CRLF).AI-assisted.
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)