mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 22:00:52 +01:00
[PR #544] SPIRV-OPT-CFG-DSE-FOLDS: type-safe folding, constant branch/bitcast/select folds, liveness DSE, and mark-sweep DCE #592
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#592
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/544
Author: @xroneyaa
Created: 11/24/2026
Status: 🔄 Open
Base:
main← Head:fix/spirv-opt-cfg-dse-folds📝 Commits (1)
60668dfspirv_opt: enforce type-safe folding, add constant branch/bitcast/select folds, liveness DSE, and mark-sweep DCE📊 Changes
2 files changed (+731 additions, -152 deletions)
View changed files
📝
tests/test_spirv_opt.py(+238 -0)📝
tools/spirv_opt.py(+493 -152)📄 Description
Claimed in the coordination issue as: SPIRV-OPT-CFG-DSE-FOLDS
What and why
tools/spirv_opt.pyhad a few type-safety gaps in_fold_int_opand missed several common SPIR-V simplification opportunities left behind by translated RDNA2 shaders:_fold_int_op&id_type):_fold_int_opreturned raw operand IDs for algebraic identities (+ 0,* 1,& ~0,| 0,^ 0, shifts by0,x & x,x | x,OpBitFieldUExtract(..., 0, width)) and constant-folded comparisons without checking operand/result type equality. With mixed%int/%uintoperands (e.g.,OpIAdd %uint %ix %u0) or vector comparisons (OpIEqual %v4bool %v %v), this could substitute an%intresult ID where%uintwas expected or a scalar%boolconstant into a%v4boolinstruction._fold_int_opnow checksid_typeequality viasame_ty(...), restricts boolean comparison folds tobool_type_id, maps absorbing zero/all-ones identities (* 0,& 0,| ~0,0 << s,0 >> s) strictly throughint_const_by_val.get((res_ty, ...)), and foldsOpBitFieldUExtract(..., width, 0)to zero.optimize()also records types ofOpSpecConstant*,OpUndef, and module-scopeOpVariable, as well asOpDecorateIdoperand IDs so DCE never deletes an operand referenced byOpDecorateId.OpBitcast: eliminates identity casts (id_type[src] == dst_ty) and folds constantOpBitcastacross matching-width 32/64-bit int and exact finite IEEE-754 float constants (distinguishing-0.0from+0.0).OpSelect& comparisons: simplifies chained same-condition selects (select(c, select(c, a, b), d) -> select(c, a, d)), foldsselect(c, false, true)to!c, and generalizes constant-outcomeOpSelectcomparisons (OpIEqual/OpINotEqualagainst any integer constant).OpBranchConditionalfolding & unreachable block cleanup:%true/%false(Case Awhen taken target is mergeM,Case Bwhen not-taken target isMand the taken block is single-predecessor, merge-free, ending inOpBranch M), removes dead incomingOpPhipairs cleanly, and deletes unreachable blocks not referenced by surviving instructions.slot in loaded_slotsDSE with iterative backward slot-liveness analysis across basic blocks onstatic_trackableslots (falling back if a block has an unrecognized terminator), eliminating cross-block overwritten stores while keeping loop-live stores intact.use_countDCE with mark-and-sweep reachability from impure/decorated roots so dead self-referentialOpPhicycles and unusedFunctionOpVariables are removed cleanly.Tested
python -m unittest tests/test_spirv_opt.py -v-> 15/15 Passed (0.071 s), including new regression tests verifying mixed%int/%uintand%v4booltype safety, constantOpBitcast(with-0.0/+0.0),OpSelectcomparisons, constantOpBranchConditionalfolding (spirv_mini_interpparity), cross-block DSE, deadOpPhicycle DCE, andOpDecorateIdoperand preservationtests/semantic/fixtures/disasm/ps_*.sfixtures:spirv_asm.assemble(opt)-> 52/52 succeeded (0 errors)spirv_mini_interpexecution parity (rawvsopt) -> 51/51 executable PS fixtures matched bit-for-bit (0 mismatches; 1 fixture requires external descriptors)58343 -> 26510(54.6%reduction)python -I scripts/check_no_game_data.py tools/spirv_opt.py tests/test_spirv_opt.py-> exit 0GPUs tested: n/a (CPU-only optimizer unit tests,
spirv_asm, andspirv_mini_interp)AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed (n/a — optimizer pass only, no new ISA translation rule).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.