Skip to content

Don't combine instructions after target finalization - #1113

Merged
maleadt merged 2 commits into
mainfrom
tb/terminal-ordering
Nov 19, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/terminal-ordering

Conversation

@maleadt

@maleadt maleadt commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

prepare_execution! runs after finish_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 widened bfloat operations, and forms overflow intrinsics.

Nothing breaks today, because back-ends put such rewrites in mcgen instead (SPIR-V's widen_bfloat16! and lower_mul_with_overflow!). The index narrowing of #1045 / CUDA.jl#3389 runs from finish_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:

  • Loads through a select or phi of 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-use select, and nothing handles a phi. A leftover slot is a private global that the Khronos translator rejects. ADCE removes the address phis and selects afterwards, which can form a dead cycle.
  • Dead references to boxed constants, which SPIR-V and Metal cannot refer to. InstCombine now runs during finalization instead, on the functions concerned: after demote_boxed_constants! copies the boxes (LLVM 17+), or on their users (SPIR-V before LLVM 17).

The guarantee covers finish_ir! up to mcgen. Metal's lower_air! is part of its mcgen and still runs its own InstCombine.

Tests

  • test/finalization.jl (new): adds a canary that InstCombine would rewrite (mul i32 %x, 2) at the end of finish_ir!, and checks that it reaches mcgen unchanged. Covers PTX, GCN, SPIR-V (both back-ends) and Metal, optimized and not, and a kernel with a boxed union constant. Fails on main.
  • Merged loads: unit tests in test/native.jl (cycles, non-constant addresses, weak globals, address-space mismatches), and select, phi and loop-carried shapes through the whole SPIR-V pipeline in test/spirv.jl.
  • Adjusted:
    • 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

#1114 is stacked on this.

`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
maleadt force-pushed the tb/terminal-ordering branch from 9d76f0a to cb0c9bc Compare November 19, 2026 00:04
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.75281% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.10%. Comparing base (e4f38a2) to head (aa4edfc).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/relocation.jl 97.46% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.
@maleadt
maleadt merged commit 8cea94b into main Nov 19, 2026
36 checks passed
@maleadt
maleadt deleted the tb/terminal-ordering branch November 19, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant