Closed googlebleh closed 1 month ago
While bisecting using this reduced test case, I was able to reproduce this crash on several versions of LLVM between 15 and 18.1.8. I was also able to reproduce on the tip of main
(e439fdf4ea0dbc6f001428f4d4956700bf26bb97). However, this crash did not reproduce on 15.0.7. The commit on 15.0.7 that "fixed" the issue is
commit f3c5289e78462fb96015f79c954d95a0d527ba55
Author: Martin Storsjö <martin@martin.st>
Date: Wed Oct 5 14:44:21 2022 +0300
Revert "Recommit "[SCEV] Look through single value PHIs." (take 3)"
This reverts commit 20d798bd47ec5191de1b2a8a031da06a04e612e1.
This commit caused crashes in some cases, see github issue #58152.
This is fixed on main, but backporting it requires multiple
nontrivial cherrypicks.
Updating llvm/test/Transforms/LoopVectorize/create-induction-resume.ll
with update_test_checks.py, so this isn't an exact automatic revert,
as that test case was added after the reverted commit.
This fixes #58152 for the release branch.
llvm/lib/Analysis/ScalarEvolution.cpp | 7 ++-
llvm/test/Analysis/DependenceAnalysis/lcssa.ll | 2 +-
llvm/test/Analysis/ScalarEvolution/cycled_phis.ll | 4 +-
.../ScalarEvolution/incorrect-exit-count.ll | 2 +-
.../Analysis/ScalarEvolution/solve-quadratic-i1.ll | 4 +-
.../ScalarEvolution/solve-quadratic-overflow.ll | 6 +--
llvm/test/Analysis/ScalarEvolution/trivial-phis.ll | 2 +-
llvm/test/Transforms/LoopStrengthReduce/funclet.ll | 40 +++++++++-------
.../LoopVectorize/create-induction-resume.ll | 24 ++++------
llvm/test/Transforms/LoopVectorize/pr45259.ll | 55 +++++++++++-----------
10 files changed, 75 insertions(+), 71 deletions(-)
specifically, this (almost) 1-line diff
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 4b2db80bc1ec..3b73f9511eca 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -6023,8 +6023,13 @@ const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
if (const SCEV *S = createAddRecFromPHI(PN))
return S;
+ // If the PHI has a single incoming value, follow that value, unless the
+ // PHI's incoming blocks are in a different loop, in which case doing so
+ // risks breaking LCSSA form. Instcombine would normally zap these, but
+ // it doesn't have DominatorTree information, so it may miss cases.
if (Value *V = simplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC}))
- return getSCEV(V);
+ if (LI.replacementPreservesLCSSAForm(PN, V))
+ return getSCEV(V);
if (const SCEV *S = createNodeFromSelectLikePHI(PN))
return S;
Reduced:
; RUN: opt -passes="print<scalar-evolution>,loop-unroll" -unroll-runtime
define void @test(i1 %c, ptr %p) {
entry:
br label %loop
loop:
%phi = phi ptr [ null, %entry ], [ %p, %loop ]
%load = load i64, ptr %p, align 8
%add = add i64 %load, 1
br i1 %c, label %bb2, label %loop
bb2:
%add.lcssa = phi i64 [ %add, %loop ]
%gep = getelementptr i64, ptr %p, i64 %add.lcssa
br label %loop2
loop2:
%iv = phi ptr [ %p, %bb2 ], [ %iv.next, %loop2 ]
%iv.next = getelementptr i8, ptr %iv, i64 8
%icmp = icmp eq ptr %iv, %gep
br i1 %icmp, label %exit, label %loop2
exit:
ret void
}
From a cursory look, the problem here is that the BECount of loop2
depends on %load
, and after peeling it should instead depend on a phi node, but instead still depends on %load
. As the BECount does not dominate the loop, SCEV is invalid.
This issue sounds very familiar, we've fixed variants of this in the past.
/cherry-pick 5bcc82d43388bb0daa122d5fe7ecda5eca27fc16
/pull-request llvm/llvm-project#109624
clang crashes while linking
I followed the instructions here and narrowed it down to an LTO bug. File to reproduce is attached. a.out.0.2.internalize-reduced.bc.tar.gz
This happened after adding
-fsplit-lto-unit
to some parts of my code base.