Repository navigation
Don't combine instructions after target finalization - #1113
Merged
Merged
Conversation
maleadt
force-pushed
the
tb/terminal-ordering
branch
from
November 18, 2026 23:59
c119467 to
9d76f0a
Compare
`prepare_execution!` ran InstCombine after lowering relocations, i.e. after `finish_ir!`, so it could undo what target finalization prepares for the back-end, e.g. widen narrowed index arithmetic again. Back-ends worked around this by doing such rewrites in `mcgen` instead. Only simplify instructions there now (InstSimplify replaces them with existing values or constants, which folds the loads from baked relocation slots), and drop dead globals without running GlobalOpt. Two things relied on the late InstCombine: loads through a `select` or `phi` of baked slots, which are now folded by selecting between the slots' values, and the cleanup of dead references to boxed constants that SPIR-V and Metal cannot refer to, which now runs during finalization, on the functions those constants are copied into (or, for SPIR-V before LLVM 17, where they are not, on the functions using them). A new test adds an InstCombine canary at the end of `finish_ir!` for every GPU target, and checks that it reaches `mcgen` unchanged.
maleadt
force-pushed
the
tb/terminal-ordering
branch
from
November 19, 2026 00:04
9d76f0a to
cb0c9bc
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1113 +/- ##
==========================================
+ Coverage 93.03% 93.10% +0.06%
==========================================
Files 32 32
Lines 8211 8291 +80
==========================================
+ Hits 7639 7719 +80
Misses 572 572 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Folding a load through a loop-carried `phi` and `select` of baked slots left those address instructions behind as a dead cycle, which InstSimplify does not remove. The slots then stayed in the module, where the Khronos translator rejects them. Run ADCE after InstSimplify in `prepare_execution!`; it only deletes instructions, so target finalization still reaches the back-end as is. Also create the value `select` before visiting its operands, as was done for `phi`s: a `select` in a cycle got two values, and a cycle of only `select`s, which is valid in unreachable code, recursed without end. The SPIR-V test now covers a `phi` and a loop-carried cycle too, and also looks for leftover slots ahead of the selected value, which is where the LLVM back-end emits them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
prepare_execution!runs afterfinish_ir!, and ran InstCombine and GlobalOpt once relocations were lowered. InstCombine there can undo what target finalization prepared for the back-end: it widens narrowed index arithmetic again, narrows widenedbfloatoperations, and forms overflow intrinsics.Nothing breaks today, because back-ends put such rewrites in
mcgeninstead (SPIR-V'swiden_bfloat16!andlower_mul_with_overflow!). The index narrowing of #1045 / CUDA.jl#3389 runs fromfinish_ir!and needs this. LLVM's own codegen pipelines don't run InstCombine after target IR preparation either.Changes
prepare_execution!now only runs InstSimplify, ADCE, GlobalDCE and StripDeadPrototypes. These replace instructions by existing values or constants, or delete them.Two things relied on the late InstCombine:
selectorphiof baked slots.fold_merged_constant_loads!folds them by selecting between the slots' values. No LLVM pass does this on its own: InstCombine only handles a single-useselect, and nothing handles aphi. A leftover slot is a private global that the Khronos translator rejects. ADCE removes the addressphis andselects afterwards, which can form a dead cycle.demote_boxed_constants!copies the boxes (LLVM 17+), or on their users (SPIR-V before LLVM 17).The guarantee covers
finish_ir!up tomcgen. Metal'slower_air!is part of itsmcgenand still runs its own InstCombine.Tests
test/finalization.jl(new): adds a canary that InstCombine would rewrite (mul i32 %x, 2) at the end offinish_ir!, and checks that it reachesmcgenunchanged. Covers PTX, GCN, SPIR-V (both back-ends) and Metal, optimized and not, and a kernel with a boxed union constant. Fails on main.test/native.jl(cycles, non-constant addresses, weak globals, address-space mismatches), andselect,phiand loop-carried shapes through the whole SPIR-V pipeline intest/spirv.jl.spirv.jl: unoptimized jobs no longer get an overflow intrinsic formed late.metal.jl: no longer expects LLVM 20's fold of a comparison of relocation words. The words are still loaded as integers, which is what Load relocation table entries as words #975 fixed.Validation
finalization,spirv,nativeandmetalpass on Julia 1.11 and 1.13. On 1.12 the only failure ismetal.jl:717, which also fails on main (Work around stale return types dropping exception_sites on Julia 1.12/1.13 #1112).relocationstest passes on an M1, with and without shader validation.#1114 is stacked on this.