Skip to content

[SLP] Check if instructions exist after vectorization #120434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2024
Merged

Conversation

dianqk
Copy link
Member

@dianqk dianqk commented Dec 18, 2024

Fixes #120433.

@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2024

@llvm/pr-subscribers-llvm-transforms

Author: DianQK (DianQK)

Changes

Fixes #120433.


Full diff: https://github.com/llvm/llvm-project/pull/120434.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+4-1)
  • (added) llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll (+51)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index d967813075bb9f..88049897b29ab1 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -21148,8 +21148,11 @@ bool SLPVectorizerPass::vectorizeCmpInsts(iterator_range<ItT> CmpInsts,
     if (R.isDeleted(I))
       continue;
     for (Value *Op : I->operands())
-      if (auto *RootOp = dyn_cast<Instruction>(Op))
+      if (auto *RootOp = dyn_cast<Instruction>(Op)) {
         Changed |= vectorizeRootInstruction(nullptr, RootOp, BB, R);
+        if (R.isDeleted(I))
+          break;
+      }
   }
   // Try to vectorize operands as vector bundles.
   for (CmpInst *I : CmpInsts) {
diff --git a/llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll b/llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll
new file mode 100644
index 00000000000000..d3995f1bb7f85d
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=slp-vectorizer < %s | FileCheck %s
+
+define void @foo() {
+; CHECK-LABEL: define void @foo() {
+; CHECK-NEXT:  [[BB:.*]]:
+; CHECK-NEXT:    br label %[[BB1:.*]]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = phi <2 x i32> [ [[TMP11:%.*]], %[[BB3:.*]] ], [ zeroinitializer, %[[BB]] ]
+; CHECK-NEXT:    br label %[[BB3]]
+; CHECK:       [[BB3]]:
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc <2 x i32> [[TMP0]] to <2 x i1>
+; CHECK-NEXT:    [[TMP2:%.*]] = mul <2 x i1> [[TMP1]], zeroinitializer
+; CHECK-NEXT:    [[TMP3:%.*]] = or <2 x i1> zeroinitializer, [[TMP2]]
+; CHECK-NEXT:    [[TMP4:%.*]] = and <2 x i1> [[TMP3]], zeroinitializer
+; CHECK-NEXT:    [[TMP5:%.*]] = extractelement <2 x i1> [[TMP4]], i32 0
+; CHECK-NEXT:    [[TMP6:%.*]] = zext i1 [[TMP5]] to i32
+; CHECK-NEXT:    [[TMP7:%.*]] = extractelement <2 x i1> [[TMP4]], i32 1
+; CHECK-NEXT:    [[TMP8:%.*]] = zext i1 [[TMP7]] to i32
+; CHECK-NEXT:    [[I22:%.*]] = or i32 [[TMP6]], [[TMP8]]
+; CHECK-NEXT:    [[TMP9:%.*]] = insertelement <2 x i32> <i32 poison, i32 0>, i32 [[I22]], i32 0
+; CHECK-NEXT:    [[TMP10:%.*]] = icmp ult <2 x i32> [[TMP9]], zeroinitializer
+; CHECK-NEXT:    [[TMP11]] = select <2 x i1> [[TMP10]], <2 x i32> zeroinitializer, <2 x i32> zeroinitializer
+; CHECK-NEXT:    br label %[[BB1]]
+;
+bb:
+  br label %bb1
+
+bb1:                                              ; preds = %bb3, %bb
+  %i = phi i32 [ %i26, %bb3 ], [ 0, %bb ]
+  %i2 = phi i32 [ %i24, %bb3 ], [ 0, %bb ]
+  br label %bb3
+
+bb3:                                              ; preds = %bb1
+  %i4 = zext i32 %i2 to i64
+  %i5 = mul i64 %i4, 0
+  %i10 = or i64 0, %i5
+  %i11 = trunc i64 %i10 to i32
+  %i12 = and i32 %i11, 0
+  %i13 = zext i32 %i to i64
+  %i14 = mul i64 %i13, 0
+  %i19 = or i64 0, %i14
+  %i20 = trunc i64 %i19 to i32
+  %i21 = and i32 %i20, 0
+  %i22 = or i32 %i12, %i21
+  %i23 = icmp ult i32 %i22, 0
+  %i24 = select i1 %i23, i32 0, i32 0
+  %i25 = icmp ult i32 0, 0
+  %i26 = select i1 %i25, i32 0, i32 0
+  br label %bb1
+}

@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2024

@llvm/pr-subscribers-vectorizers

Author: DianQK (DianQK)

Changes

Fixes #120433.


Full diff: https://github.com/llvm/llvm-project/pull/120434.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+4-1)
  • (added) llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll (+51)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index d967813075bb9f..88049897b29ab1 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -21148,8 +21148,11 @@ bool SLPVectorizerPass::vectorizeCmpInsts(iterator_range<ItT> CmpInsts,
     if (R.isDeleted(I))
       continue;
     for (Value *Op : I->operands())
-      if (auto *RootOp = dyn_cast<Instruction>(Op))
+      if (auto *RootOp = dyn_cast<Instruction>(Op)) {
         Changed |= vectorizeRootInstruction(nullptr, RootOp, BB, R);
+        if (R.isDeleted(I))
+          break;
+      }
   }
   // Try to vectorize operands as vector bundles.
   for (CmpInst *I : CmpInsts) {
diff --git a/llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll b/llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll
new file mode 100644
index 00000000000000..d3995f1bb7f85d
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/slp-deleted-inst.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=slp-vectorizer < %s | FileCheck %s
+
+define void @foo() {
+; CHECK-LABEL: define void @foo() {
+; CHECK-NEXT:  [[BB:.*]]:
+; CHECK-NEXT:    br label %[[BB1:.*]]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = phi <2 x i32> [ [[TMP11:%.*]], %[[BB3:.*]] ], [ zeroinitializer, %[[BB]] ]
+; CHECK-NEXT:    br label %[[BB3]]
+; CHECK:       [[BB3]]:
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc <2 x i32> [[TMP0]] to <2 x i1>
+; CHECK-NEXT:    [[TMP2:%.*]] = mul <2 x i1> [[TMP1]], zeroinitializer
+; CHECK-NEXT:    [[TMP3:%.*]] = or <2 x i1> zeroinitializer, [[TMP2]]
+; CHECK-NEXT:    [[TMP4:%.*]] = and <2 x i1> [[TMP3]], zeroinitializer
+; CHECK-NEXT:    [[TMP5:%.*]] = extractelement <2 x i1> [[TMP4]], i32 0
+; CHECK-NEXT:    [[TMP6:%.*]] = zext i1 [[TMP5]] to i32
+; CHECK-NEXT:    [[TMP7:%.*]] = extractelement <2 x i1> [[TMP4]], i32 1
+; CHECK-NEXT:    [[TMP8:%.*]] = zext i1 [[TMP7]] to i32
+; CHECK-NEXT:    [[I22:%.*]] = or i32 [[TMP6]], [[TMP8]]
+; CHECK-NEXT:    [[TMP9:%.*]] = insertelement <2 x i32> <i32 poison, i32 0>, i32 [[I22]], i32 0
+; CHECK-NEXT:    [[TMP10:%.*]] = icmp ult <2 x i32> [[TMP9]], zeroinitializer
+; CHECK-NEXT:    [[TMP11]] = select <2 x i1> [[TMP10]], <2 x i32> zeroinitializer, <2 x i32> zeroinitializer
+; CHECK-NEXT:    br label %[[BB1]]
+;
+bb:
+  br label %bb1
+
+bb1:                                              ; preds = %bb3, %bb
+  %i = phi i32 [ %i26, %bb3 ], [ 0, %bb ]
+  %i2 = phi i32 [ %i24, %bb3 ], [ 0, %bb ]
+  br label %bb3
+
+bb3:                                              ; preds = %bb1
+  %i4 = zext i32 %i2 to i64
+  %i5 = mul i64 %i4, 0
+  %i10 = or i64 0, %i5
+  %i11 = trunc i64 %i10 to i32
+  %i12 = and i32 %i11, 0
+  %i13 = zext i32 %i to i64
+  %i14 = mul i64 %i13, 0
+  %i19 = or i64 0, %i14
+  %i20 = trunc i64 %i19 to i32
+  %i21 = and i32 %i20, 0
+  %i22 = or i32 %i12, %i21
+  %i23 = icmp ult i32 %i22, 0
+  %i24 = select i1 %i23, i32 0, i32 0
+  %i25 = icmp ult i32 0, 0
+  %i26 = select i1 %i25, i32 0, i32 0
+  br label %bb1
+}

Copy link
Member

@alexey-bataev alexey-bataev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

@dianqk dianqk merged commit e7a4d78 into llvm:main Dec 18, 2024
11 checks passed
@dianqk dianqk deleted the slp branch December 18, 2024 22:22
@dianqk dianqk added this to the LLVM 19.X Release milestone Dec 18, 2024
@dianqk
Copy link
Member Author

dianqk commented Dec 18, 2024

/cherry-pick e7a4d78

@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2024

Failed to cherry-pick: e7a4d78

https://github.com/llvm/llvm-project/actions/runs/12402342030

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

dianqk added a commit to dianqk/llvm-project that referenced this pull request Dec 19, 2024
@dianqk
Copy link
Member Author

dianqk commented Dec 19, 2024

PR: #120505

tru pushed a commit to dianqk/llvm-project that referenced this pull request Jan 14, 2025
tru pushed a commit to dianqk/llvm-project that referenced this pull request Jan 14, 2025
fhahn pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 29, 2025
fhahn added a commit to swiftlang/llvm-project that referenced this pull request Apr 29, 2025
[SLP] Check if instructions exist after vectorization (llvm#120434)

Fixes llvm#120433.

(cherry-picked from 5861191)

rdar://148832843
fhahn pushed a commit to fhahn/llvm-project that referenced this pull request May 8, 2025
fhahn pushed a commit to fhahn/llvm-project that referenced this pull request May 12, 2025
AnnaZaks added a commit to swiftlang/llvm-project that referenced this pull request May 13, 2025
[SLP] Check if instructions exist after vectorization (llvm#120434)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[SLP] Assertion `detail::isPresent(Val) && "dyn_cast on a non-existent value"' failed.
3 participants