From 1cf013898bdd44bfd8420a04fa7b84fbb9d6d985 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 17 Nov 2020 15:09:06 -0500 Subject: [PATCH] [SLP] avoid unreachable code crash/infloop Example based on the post-commit comments for D88735. --- lib/Transforms/Vectorize/SLPVectorizer.cpp | 6 ++-- .../SLPVectorizer/X86/horizontal.ll | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/Vectorize/SLPVectorizer.cpp b/lib/Transforms/Vectorize/SLPVectorizer.cpp index 4e4b6a8dd1c..e14fa443fec 100644 --- a/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/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/test/Transforms/SLPVectorizer/X86/horizontal.ll b/test/Transforms/SLPVectorizer/X86/horizontal.ll index f2f3bd0f02a..5663c88b636 100644 --- a/test/Transforms/SLPVectorizer/X86/horizontal.ll +++ b/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(...)