[llvm] af63546 - [SLP]Look for vector user when estimating the cost

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 27 04:14:38 PDT 2024


Author: Alexey Bataev
Date: 2024-09-27T04:14:28-07:00
New Revision: af6354634d2cec14570108ee038ca4b18cf6856a

URL: https://github.com/llvm/llvm-project/commit/af6354634d2cec14570108ee038ca4b18cf6856a
DIFF: https://github.com/llvm/llvm-project/commit/af6354634d2cec14570108ee038ca4b18cf6856a.diff

LOG: [SLP]Look for vector user when estimating the cost

Need to find the first vector node user, not the very first user node at
all. The very first user might be a gather, vectorized as clustered,
which may cause compiler crash.

Fixes https://github.com/llvm/llvm-project/issues/110193

Added: 
    llvm/test/Transforms/SLPVectorizer/X86/minbw-multiused-from-gather.ll

Modified: 
    llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 48a8627ab63e07..dee0b7e1f43714 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -10340,13 +10340,16 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
         InstructionCost VecCost = VectorCost(CommonCost);
         // Check if the current node must be resized, if the parent node is not
         // resized.
-        if (!UnaryInstruction::isCast(E->getOpcode()) && E->Idx != 0 &&
+        if (It != MinBWs.end() && !UnaryInstruction::isCast(E->getOpcode()) &&
+            E->Idx != 0 &&
             (E->getOpcode() != Instruction::Load ||
              !E->UserTreeIndices.empty())) {
-          const EdgeInfo &EI = E->UserTreeIndices.front();
-          if ((EI.UserTE->getOpcode() != Instruction::Select ||
-               EI.EdgeIdx != 0) &&
-              It != MinBWs.end()) {
+          const EdgeInfo &EI =
+              *find_if(E->UserTreeIndices, [](const EdgeInfo &EI) {
+                return !EI.UserTE->isGather() || EI.EdgeIdx != UINT_MAX;
+              });
+          if (EI.UserTE->getOpcode() != Instruction::Select ||
+              EI.EdgeIdx != 0) {
             auto UserBWIt = MinBWs.find(EI.UserTE);
             Type *UserScalarTy =
                 EI.UserTE->getOperand(EI.EdgeIdx).front()->getType();

diff  --git a/llvm/test/Transforms/SLPVectorizer/X86/minbw-multiused-from-gather.ll b/llvm/test/Transforms/SLPVectorizer/X86/minbw-multiused-from-gather.ll
new file mode 100644
index 00000000000000..8e4b2802710514
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/minbw-multiused-from-gather.ll
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+define i1 @test() {
+; CHECK-LABEL: define i1 @test() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = trunc i64 0 to i32
+; CHECK-NEXT:    [[CONV85_22_I333_I_I:%.*]] = or i32 0, [[TMP0]]
+; CHECK-NEXT:    [[CMP3_I_22_I334_I_I:%.*]] = icmp ugt i32 [[CONV85_22_I333_I_I]], 0
+; CHECK-NEXT:    [[SHL_I111_22_I335_I_I:%.*]] = select i1 [[CMP3_I_22_I334_I_I]], i32 0, i32 0
+; CHECK-NEXT:    [[C22_I336_I_I:%.*]] = shl i32 [[CONV85_22_I333_I_I]], [[SHL_I111_22_I335_I_I]]
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 0 to i32
+; CHECK-NEXT:    [[CONV85_23_I340_I_I:%.*]] = or i32 0, [[TMP1]]
+; CHECK-NEXT:    [[CMP3_I_23_I341_I_I:%.*]] = icmp ugt i32 [[CONV85_23_I340_I_I]], 0
+; CHECK-NEXT:    [[SHL_I111_23_I342_I_I:%.*]] = select i1 [[CMP3_I_23_I341_I_I]], i32 0, i32 0
+; CHECK-NEXT:    [[C23_I343_I_I:%.*]] = shl i32 [[CONV85_23_I340_I_I]], [[SHL_I111_23_I342_I_I]]
+; CHECK-NEXT:    [[TMP2:%.*]] = trunc i64 0 to i32
+; CHECK-NEXT:    [[CONV85_24_I347_I_I:%.*]] = or i32 0, [[TMP2]]
+; CHECK-NEXT:    [[CMP3_I_24_I348_I_I:%.*]] = icmp ugt i32 [[CONV85_24_I347_I_I]], 0
+; CHECK-NEXT:    [[SHL_I111_24_I349_I_I:%.*]] = select i1 [[CMP3_I_24_I348_I_I]], i32 0, i32 0
+; CHECK-NEXT:    [[C24_I350_I_I:%.*]] = shl i32 [[CONV85_24_I347_I_I]], [[SHL_I111_24_I349_I_I]]
+; CHECK-NEXT:    [[TMP3:%.*]] = trunc i64 0 to i32
+; CHECK-NEXT:    [[CONV85_25_I354_I_I:%.*]] = or i32 0, [[TMP3]]
+; CHECK-NEXT:    [[CMP3_I_25_I355_I_I:%.*]] = icmp ugt i32 [[CONV85_25_I354_I_I]], 0
+; CHECK-NEXT:    [[SHL_I111_25_I356_I_I:%.*]] = select i1 [[CMP3_I_25_I355_I_I]], i32 0, i32 0
+; CHECK-NEXT:    [[C25_I357_I_I:%.*]] = shl i32 [[CONV85_25_I354_I_I]], [[SHL_I111_25_I356_I_I]]
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.vector.reduce.and.v4i32(<4 x i32> zeroinitializer)
+; CHECK-NEXT:    [[OP_RDX:%.*]] = and i32 [[TMP4]], [[C22_I336_I_I]]
+; CHECK-NEXT:    [[OP_RDX1:%.*]] = and i32 [[C23_I343_I_I]], [[C24_I350_I_I]]
+; CHECK-NEXT:    [[OP_RDX2:%.*]] = and i32 [[OP_RDX]], [[OP_RDX1]]
+; CHECK-NEXT:    [[OP_RDX3:%.*]] = and i32 [[OP_RDX2]], [[C25_I357_I_I]]
+; CHECK-NEXT:    [[CONV109_I_I:%.*]] = trunc i32 [[OP_RDX3]] to i8
+; CHECK-NEXT:    [[CMP_I_I54_I:%.*]] = icmp eq i8 [[CONV109_I_I]], 0
+; CHECK-NEXT:    ret i1 [[CMP_I_I54_I]]
+;
+entry:
+  %c18.i308.i.i = shl i32 0, 0
+  %c19.i315.i.i = shl i32 0, 0
+  %and.19.i316.i.i = and i32 %c18.i308.i.i, %c19.i315.i.i
+  %c20.i322.i.i = shl i32 0, 0
+  %and.20.i323.i.i = and i32 %and.19.i316.i.i, %c20.i322.i.i
+  %c21.i329.i.i = shl i32 0, 0
+  %and.21.i330.i.i = and i32 %and.20.i323.i.i, %c21.i329.i.i
+  %0 = trunc i64 0 to i32
+  %conv85.22.i333.i.i = or i32 0, %0
+  %cmp3.i.22.i334.i.i = icmp ugt i32 %conv85.22.i333.i.i, 0
+  %shl.i111.22.i335.i.i = select i1 %cmp3.i.22.i334.i.i, i32 0, i32 0
+  %c22.i336.i.i = shl i32 %conv85.22.i333.i.i, %shl.i111.22.i335.i.i
+  %and.22.i337.i.i = and i32 %and.21.i330.i.i, %c22.i336.i.i
+  %1 = trunc i64 0 to i32
+  %conv85.23.i340.i.i = or i32 0, %1
+  %cmp3.i.23.i341.i.i = icmp ugt i32 %conv85.23.i340.i.i, 0
+  %shl.i111.23.i342.i.i = select i1 %cmp3.i.23.i341.i.i, i32 0, i32 0
+  %c23.i343.i.i = shl i32 %conv85.23.i340.i.i, %shl.i111.23.i342.i.i
+  %and.23.i344.i.i = and i32 %and.22.i337.i.i, %c23.i343.i.i
+  %2 = trunc i64 0 to i32
+  %conv85.24.i347.i.i = or i32 0, %2
+  %cmp3.i.24.i348.i.i = icmp ugt i32 %conv85.24.i347.i.i, 0
+  %shl.i111.24.i349.i.i = select i1 %cmp3.i.24.i348.i.i, i32 0, i32 0
+  %c24.i350.i.i = shl i32 %conv85.24.i347.i.i, %shl.i111.24.i349.i.i
+  %and.24.i351.i.i = and i32 %and.23.i344.i.i, %c24.i350.i.i
+  %3 = trunc i64 0 to i32
+  %conv85.25.i354.i.i = or i32 0, %3
+  %cmp3.i.25.i355.i.i = icmp ugt i32 %conv85.25.i354.i.i, 0
+  %shl.i111.25.i356.i.i = select i1 %cmp3.i.25.i355.i.i, i32 0, i32 0
+  %c25.i357.i.i = shl i32 %conv85.25.i354.i.i, %shl.i111.25.i356.i.i
+  %and.25.i358.i.i = and i32 %and.24.i351.i.i, %c25.i357.i.i
+  %conv109.i.i = trunc i32 %and.25.i358.i.i to i8
+  %cmp.i.i54.i = icmp eq i8 %conv109.i.i, 0
+  ret i1 %cmp.i.i54.i
+}


        


More information about the llvm-commits mailing list