[llvm] 0883497 - [SLP] avoid unreachable code crash/infloop
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 17 12:10:35 PST 2020
Author: Sanjay Patel
Date: 2020-11-17T15:10:23-05:00
New Revision: 08834979e3aca762b0db05fbc73998aa07753bc2
URL: https://github.com/llvm/llvm-project/commit/08834979e3aca762b0db05fbc73998aa07753bc2
DIFF: https://github.com/llvm/llvm-project/commit/08834979e3aca762b0db05fbc73998aa07753bc2.diff
LOG: [SLP] avoid unreachable code crash/infloop
Example based on the post-commit comments for D88735.
Added:
Modified:
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
llvm/test/Transforms/SLPVectorizer/X86/horizontal.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 4e4b6a8dd1ce..e14fa443fec7 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -7686,10 +7686,12 @@ bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
// Try to vectorize the incoming values of the PHI, to catch reductions
// that feed into PHIs.
for (unsigned I = 0, E = P->getNumIncomingValues(); I != E; I++) {
- // Skip if the incoming block is the current BB for now.
+ // Skip if the incoming block is the current BB for now. Also, bypass
+ // unreachable IR for efficiency and to avoid crashing.
// TODO: Collect the skipped incoming values and try to vectorize them
// after processing BB.
- if (BB == P->getIncomingBlock(I))
+ if (BB == P->getIncomingBlock(I) ||
+ !DT->isReachableFromEntry(P->getIncomingBlock(I)))
continue;
Changed |= vectorizeRootInstruction(nullptr, P->getIncomingValue(I),
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/horizontal.ll b/llvm/test/Transforms/SLPVectorizer/X86/horizontal.ll
index f2f3bd0f02af..5663c88b6366 100644
--- a/llvm/test/Transforms/SLPVectorizer/X86/horizontal.ll
+++ b/llvm/test/Transforms/SLPVectorizer/X86/horizontal.ll
@@ -1731,4 +1731,39 @@ exit:
ret i32 %sum.1
}
+; Make sure we do not crash or infinite loop on ill-formed IR.
+
+define void @unreachable_block() {
+; CHECK-LABEL: @unreachable_block(
+; CHECK-NEXT: bb.0:
+; CHECK-NEXT: br label [[BB_1:%.*]]
+; CHECK: dead:
+; CHECK-NEXT: [[T0:%.*]] = add i16 [[T0]], undef
+; CHECK-NEXT: br label [[BB_1]]
+; CHECK: bb.1:
+; CHECK-NEXT: [[T1:%.*]] = phi i16 [ undef, [[BB_0:%.*]] ], [ [[T0]], [[DEAD:%.*]] ]
+; CHECK-NEXT: ret void
+;
+; STORE-LABEL: @unreachable_block(
+; STORE-NEXT: bb.0:
+; STORE-NEXT: br label [[BB_1:%.*]]
+; STORE: dead:
+; STORE-NEXT: [[T0:%.*]] = add i16 [[T0]], undef
+; STORE-NEXT: br label [[BB_1]]
+; STORE: bb.1:
+; STORE-NEXT: [[T1:%.*]] = phi i16 [ undef, [[BB_0:%.*]] ], [ [[T0]], [[DEAD:%.*]] ]
+; STORE-NEXT: ret void
+;
+bb.0:
+ br label %bb.1
+
+dead:
+ %t0 = add i16 %t0, undef ; unreachable IR may depend on itself
+ br label %bb.1
+
+bb.1:
+ %t1 = phi i16 [ undef, %bb.0 ], [ %t0, %dead ]
+ ret void
+}
+
declare i32 @__gxx_personality_v0(...)
More information about the llvm-commits
mailing list