Repository navigation
[LCSSA][SCEV] Compile-time fixes for loops with many exits - #72
Open
KristofferC wants to merge 3 commits into
Open
KristofferC wants to merge 3 commits into
KristofferC wants to merge 3 commits into
Conversation
Member
It'd be good to open PRs upstream once you have this in a shape you're happy with, they're probably better equipped to review this. |
`BackedgeTakenInfo::getExact` builds the umin over all exit counts on every query. For a loop with many computable exits this is expensive, and e.g. `rewriteLoopExitValues` queries it once per exit value. Cache the expression, like `SymbolicMax` already is. Assisted-by: Claude Code (Fable 5.2) (cherry picked from llvm#248210, commits 3770cc5 and its parent)
For every instruction with a use outside its loop, LCSSA creates a PHI in each exit block the instruction dominates and erases the unused ones afterwards. A loop with N exits to blocks that end in `unreachable` and each use a different value from the loop (e.g. failed bounds checks that report the index) thus creates N^2 PHIs, almost all of which are erased again. Nothing is reachable from an exit block without successors, so a PHI in it can only be used by that block. Skip such blocks unless they contain a use of the instruction. A debug value in a skipped block keeps referring to the instruction, which dominates it, so the PHIs created do not depend on the presence of debug info. The output only changes in the numbering of value and block names. Assisted-by: Claude Code (Fable 5.2) (cherry picked from llvm#248214, commits 87b392c and its parent)
…icate `isBasicBlockEntryGuardedByCond` and `isLoopBackedgeGuardedByCond` try every dominating branch condition, and proving the implication from one of them can walk the dominating conditions of the loop again: the walk in `isBasicBlockEntryGuardedByCond` reaches `isLoopBackedgeGuardedByCond` through `isKnownViaInduction`. In a loop with N guarded exits IndVarSimplify asks for each of the N compares and for the arithmetic feeding them, so the time is cubic in N. Stop after `-scalar-evolution-max-dominating-conds` (32) conditions, nearest first. Assisted-by: Claude Code (Fable 5.2) (cherry picked from llvm#248223, commit 217bb7a, with the option as a cl::opt)
KristofferC
force-pushed
the
kc/23.x-many-exit-loops
branch
from
December 3, 2026 21:47
d0ee079 to
cc03d4b
Compare
Member
Author
|
I have open PRs with these 3 commits. I have also run julia itself with them (although with a slightly different version of them) in JuliaLang/julia#65439. Should we wait until the whole LLVM review process goes through or can we make a julia release with these before. I don't really want to block the julia release on the LLVM review process... |
vchuravy
marked this pull request as ready for review
December 4, 2026 09:37
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.
Draft, for discussion: four compile-time fixes for loops with many exits, found while looking at JuliaLang/julia#65432. None of these are upstream yet.
Since JuliaLang/julia#61535, a loop with N bounds-checked array accesses has N exits to noreturn blocks that each use a value computed in the loop, and nothing in the loop writes memory, so SCEV can analyze every exit. That makes several places superlinear in N:
isBasicBlockEntryGuardedByCond/isLoopBackedgeGuardedByCondtry every dominating condition, nested inside each other. IndVarSimplify'seliminateIVComparisonasks for each of the N compares. Adds-scalar-evolution-max-dominating-conds(default 32, picked without much science).getSequentialMinMaxExprrestarts from scratch after folding one pair of operands. Fold all pairs in one sweep.BackedgeTakenInfo::getExactrebuilds the umin over all exit counts on every query, andrewriteLoopExitValuesqueries it per exit value. Cache it.Reproducer: the script in JuliaLang/julia#65432, here with JuliaLang/julia#65435 applied ("varargs"), and with the old index-tuple thrower ("tuple") for comparison. Times are
optwall time on the unoptimized IR of thestencilfunction.Stock passes, X86 Release build of
julia-23.1.4-1:function(loop-simplify,lcssa), N=4000loop(indvars), N=125loop(indvars), N=250loop(indvars)at N=125 after each patch: 10.2 s → 2.3 s (2) → 1.3 s (3) → 0.1 s (4).Julia pipeline (
opt --load-pass-plugin=libjulia-codegen.so -passes='julia<level=L>', with a shared libLLVM built from this branch):-O2,--check-bounds=yes, varargs, N=125-O2,--check-bounds=yes, tuple, N=500-O2, default bounds checks, varargs, N=250-O0,--check-bounds=yes, varargs, N=4000The optimized IR is identical before and after, up to value names, for N = 2, 8, 32 and 125 in both forms.
Known gaps:
getExact(getMinMaxExprre-sorting the operands on every pairwise merge).Analysis/ScalarEvolutionandTransforms/{LCSSA,IndVarSimplify,LoopUnroll,IRCE,LoopDeletion,LoopIdiom,LoopStrengthReduce,SimpleLoopUnswitch,LICM,LoopSimplify,LoopRotate,LoopPredication,LoopVectorize,PhaseOrdering}, all pass except one that needslli, which I did not build.optplus thelibjulia-codegenplugin.Written with Claude Code (Fable 5.2).
🤖 Generated with Claude Code