Skip to content

[LCSSA][SCEV] Compile-time fixes for loops with many exits - #72

Open
KristofferC wants to merge 3 commits into
JuliaLang:julia-release/23.xfrom
KristofferC:kc/23.x-many-exit-loops
Open

KristofferC wants to merge 3 commits into
JuliaLang:julia-release/23.xfrom
KristofferC:kc/23.x-many-exit-loops

Conversation

@KristofferC

@KristofferC KristofferC commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

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:

  1. [LCSSA] creates a PHI in every exit block an instruction dominates and erases the unused ones afterwards: N² PHIs. Skip exit blocks without successors that contain no use. Output only changes in value/block name numbering (two LoopUnroll tests updated for that).
  2. [SCEV] isBasicBlockEntryGuardedByCond / isLoopBackedgeGuardedByCond try every dominating condition, nested inside each other. IndVarSimplify's eliminateIVComparison asks for each of the N compares. Adds -scalar-evolution-max-dominating-conds (default 32, picked without much science).
  3. [SCEV] getSequentialMinMaxExpr restarts from scratch after folding one pair of operands. Fold all pairs in one sweep.
  4. [SCEV] BackedgeTakenInfo::getExact rebuilds the umin over all exit counts on every query, and rewriteLoopExitValues queries 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 opt wall time on the unoptimized IR of the stencil function.

Stock passes, X86 Release build of julia-23.1.4-1:

before after
function(loop-simplify,lcssa), N=4000 30.8 s, 8555 MB 1.7 s, 236 MB
loop(indvars), N=125 10.2 s 0.1 s
loop(indvars), N=250 88.9 s 0.3 s

loop(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):

before after
-O2, --check-bounds=yes, varargs, N=125 10.6 s 0.2 s
N=250 ~90 s 0.6 s
N=500 2.2 s
N=1000 10.8 s
-O2, --check-bounds=yes, tuple, N=500 4.3 s 0.7 s
N=1000 19.7 s 1.6 s
-O2, default bounds checks, varargs, N=250 4.6 s 0.5 s
N=500 42.3 s 1.6 s
-O0, --check-bounds=yes, varargs, N=4000 42.4 s, 8537 MB 3.4 s, 243 MB

The optimized IR is identical before and after, up to value names, for N = 2, 8, 32 and 125 in both forms.

Known gaps:

  • Still superlinear for the varargs form at large N (N=2000: 69 s); what remains is building the N-operand umin in getExact (getMinMaxExpr re-sorting the operands on every pairwise merge).
  • No new lit tests. Of the 3089 existing tests under Analysis/ScalarEvolution and Transforms/{LCSSA,IndVarSimplify,LoopUnroll,IRCE,LoopDeletion,LoopIdiom,LoopStrengthReduce,SimpleLoopUnswitch,LICM,LoopSimplify,LoopRotate,LoopPredication,LoopVectorize,PhaseOrdering}, all pass except one that needs lli, which I did not build.
  • Julia itself was not built against this branch; only opt plus the libjulia-codegen plugin.

Written with Claude Code (Fable 5.2).

🤖 Generated with Claude Code

@giordano

giordano commented Dec 2, 2026

Copy link
Copy Markdown
Member

None of these are upstream yet.

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
KristofferC force-pushed the kc/23.x-many-exit-loops branch from d0ee079 to cc03d4b Compare December 3, 2026 21:47
@KristofferC

Copy link
Copy Markdown
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
vchuravy marked this pull request as ready for review December 4, 2026 09:37
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.

2 participants