From 2601d6f189cf1d4e4e77a7cba83e5d32c87bf079 Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Tue, 6 Aug 2024 12:27:36 -0700 Subject: [PATCH] [SLP]Fix PR102187: do not insert extractelement before landingpad instruction. Landingpad instruction must be the very first instruction after the phi nodes, so need to inser extractelement/shuffles after this instruction. Fixes https://github.com/llvm/llvm-project/issues/102187 --- .../Transforms/Vectorize/SLPVectorizer.cpp | 30 ++++++++++---- .../X86/extractelement-phi-in-landingpad.ll | 40 +++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 llvm/test/Transforms/SLPVectorizer/X86/extractelement-phi-in-landingpad.ll diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp index 9589d306581e..5c8873853a73 100644 --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -14049,12 +14049,19 @@ Value *BoUpSLP::vectorizeTree( "ExternallyUsedValues map or remain as scalar in vectorized " "instructions"); if (auto *VecI = dyn_cast(Vec)) { - if (auto *PHI = dyn_cast(VecI)) - Builder.SetInsertPoint(PHI->getParent(), - PHI->getParent()->getFirstNonPHIIt()); - else + if (auto *PHI = dyn_cast(VecI)) { + if (PHI->getParent()->isLandingPad()) + Builder.SetInsertPoint( + PHI->getParent(), + std::next( + PHI->getParent()->getLandingPadInst()->getIterator())); + else + Builder.SetInsertPoint(PHI->getParent(), + PHI->getParent()->getFirstNonPHIIt()); + } else { Builder.SetInsertPoint(VecI->getParent(), std::next(VecI->getIterator())); + } } else { Builder.SetInsertPoint(&F->getEntryBlock(), F->getEntryBlock().begin()); } @@ -14080,11 +14087,18 @@ Value *BoUpSLP::vectorizeTree( auto VecIt = VectorCasts.find(Key); if (VecIt == VectorCasts.end()) { IRBuilderBase::InsertPointGuard Guard(Builder); - if (auto *IVec = dyn_cast(Vec)) - Builder.SetInsertPoint( - IVec->getParent()->getFirstNonPHIOrDbgOrLifetime()); - else if (auto *IVec = dyn_cast(Vec)) + if (auto *IVec = dyn_cast(Vec)) { + if (IVec->getParent()->isLandingPad()) + Builder.SetInsertPoint(IVec->getParent(), + std::next(IVec->getParent() + ->getLandingPadInst() + ->getIterator())); + else + Builder.SetInsertPoint( + IVec->getParent()->getFirstNonPHIOrDbgOrLifetime()); + } else if (auto *IVec = dyn_cast(Vec)) { Builder.SetInsertPoint(IVec->getNextNonDebugInstruction()); + } Vec = Builder.CreateIntCast( Vec, getWidenedType( diff --git a/llvm/test/Transforms/SLPVectorizer/X86/extractelement-phi-in-landingpad.ll b/llvm/test/Transforms/SLPVectorizer/X86/extractelement-phi-in-landingpad.ll new file mode 100644 index 000000000000..7476c77c5832 --- /dev/null +++ b/llvm/test/Transforms/SLPVectorizer/X86/extractelement-phi-in-landingpad.ll @@ -0,0 +1,40 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt -S --passes=slp-vectorizer -slp-threshold=-99999 -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s + +define void @test() personality ptr null { +; CHECK-LABEL: define void @test() personality ptr null { +; CHECK-NEXT: [[BB:.*]]: +; CHECK-NEXT: invoke void null() +; CHECK-NEXT: to label %[[BB65:.*]] unwind label %[[BB4:.*]] +; CHECK: [[BB2:.*]]: +; CHECK-NEXT: invoke void null() +; CHECK-NEXT: to label %[[BB65]] unwind label %[[BB4]] +; CHECK: [[BB4]]: +; CHECK-NEXT: [[TMP0:%.*]] = phi <2 x i32> [ zeroinitializer, %[[BB]] ], [ poison, %[[BB2]] ] +; CHECK-NEXT: [[LANDINGPAD:%.*]] = landingpad { ptr, i32 } +; CHECK-NEXT: cleanup +; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[TMP0]], i32 1 +; CHECK-NEXT: call void null(i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]], i32 [[TMP1]]) +; CHECK-NEXT: ret void +; CHECK: [[BB65]]: +; CHECK-NEXT: ret void +; +bb: + invoke void null() + to label %bb65 unwind label %bb4 + +bb2: + invoke void null() + to label %bb65 unwind label %bb4 + +bb4: + %phi5 = phi i32 [ 0, %bb ], [ 0, %bb2 ] + %phi6 = phi i32 [ 0, %bb ], [ 0, %bb2 ] + %landingpad = landingpad { ptr, i32 } + cleanup + call void null(i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5) + ret void + +bb65: + ret void +}