[llvm] [SLP]Fix undef/poison matching in TreeEntry::isSame (PR #219704)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 10:33:21 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-vectorizers

Author: Alexey Bataev (alexey-bataev)

<details>
<summary>Changes</summary>

Undef no longer matches poison mask elements (poison is stronger than
undef and its lane is unrecoverable); poison matches any lane.

Fixes #<!-- -->219631


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


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+9-5) 
- (added) llvm/test/Transforms/SLPVectorizer/X86/perfect-match-gather-undef.ll (+110) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index a7eaef3701415..2b935f2402781 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -2994,8 +2994,7 @@ class slpvectorizer::BoUpSLP {
         return VL.size() == Mask.size() &&
                std::equal(VL.begin(), VL.end(), Mask.begin(),
                           [Scalars](Value *V, int Idx) {
-                            return (isa<UndefValue>(V) &&
-                                    Idx == PoisonMaskElem) ||
+                            return isa<PoisonValue>(V) ||
                                    (Idx != PoisonMaskElem && V == Scalars[Idx]);
                           });
       };
@@ -3307,9 +3306,14 @@ class slpvectorizer::BoUpSLP {
       if (!Res.second)
         return Res.first->second;
       unsigned &FoundLane = Res.first->getSecond();
-      for (auto *It = find(Scalars, V), *End = Scalars.end(); It != End;
-           std::advance(It, 1)) {
-        if (*It != V)
+      // Poison can take any lane, match it to the lane of the first non-poison
+      // scalar.
+      auto IsMatch = [V](Value *S) {
+        return isa<PoisonValue>(V) ? !isa<PoisonValue>(S) : S == V;
+      };
+      for (auto *It = find_if(Scalars, IsMatch), *End = Scalars.end();
+           It != End; std::advance(It, 1)) {
+        if (!IsMatch(*It))
           continue;
         FoundLane = std::distance(Scalars.begin(), It);
         assert(FoundLane < Scalars.size() && "Couldn't find extract lane");
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/perfect-match-gather-undef.ll b/llvm/test/Transforms/SLPVectorizer/X86/perfect-match-gather-undef.ll
new file mode 100644
index 0000000000000..e0fb704315983
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/perfect-match-gather-undef.ll
@@ -0,0 +1,110 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=slp-vectorizer -S -mtriple=x86_64-unknown-linux -slp-threshold=-99999 | FileCheck %s
+
+%struct.s0 = type { i8, i8 }
+
+ at g20 = external global %struct.s0
+
+define i8 @test(i1 %a4, i1 %tobool.not, i8 %sel18.sroa.0.0.peel, ptr %g4) {
+; CHECK-LABEL: define i8 @test(
+; CHECK-SAME: i1 [[A4:%.*]], i1 [[TOBOOL_NOT:%.*]], i8 [[SEL18_SROA_0_0_PEEL:%.*]], ptr [[G4:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[SEL18_SROA_6_0_COPYLOAD_PEEL:%.*]] = load i8, ptr getelementptr inbounds nuw (i8, ptr @g20, i64 1), align 1
+; CHECK-NEXT:    [[SEL18_SROA_0_0_COPYLOAD_PEEL:%.*]] = load i8, ptr @g20, align 1
+; CHECK-NEXT:    [[SEL18_SROA_0_0_COPYLOAD27:%.*]] = select i1 [[A4]], i8 [[SEL18_SROA_0_0_COPYLOAD_PEEL]], i8 0
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <2 x i1> poison, i1 [[A4]], i64 0
+; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <2 x i1> [[TMP1]], <2 x i1> poison, <2 x i32> zeroinitializer
+; CHECK-NEXT:    [[TMP12:%.*]] = insertelement <2 x i8> <i8 poison, i8 undef>, i8 [[SEL18_SROA_6_0_COPYLOAD_PEEL]], i64 0
+; CHECK-NEXT:    [[TMP3:%.*]] = insertelement <2 x i8> <i8 0, i8 poison>, i8 [[SEL18_SROA_0_0_PEEL]], i64 1
+; CHECK-NEXT:    [[TMP4:%.*]] = select <2 x i1> [[TMP2]], <2 x i8> [[TMP12]], <2 x i8> [[TMP3]]
+; CHECK-NEXT:    br label %[[LBL_ENTRY:.*]]
+; CHECK:       [[LBL_ENTRY]]:
+; CHECK-NEXT:    [[SEL18_SROA_0_0_COPYLOAD1836:%.*]] = phi i8 [ [[SEL18_SROA_0_0_COPYLOAD27]], %[[ENTRY]] ], [ 0, %[[LBL_SW35:.*]] ]
+; CHECK-NEXT:    [[TMP5:%.*]] = phi <2 x i8> [ [[TMP4]], %[[ENTRY]] ], [ zeroinitializer, %[[LBL_SW35]] ]
+; CHECK-NEXT:    [[TMP6:%.*]] = phi <2 x i8> [ zeroinitializer, %[[ENTRY]] ], [ [[TMP16:%.*]], %[[LBL_SW35]] ]
+; CHECK-NEXT:    [[TMP7:%.*]] = insertelement <4 x i8> <i8 poison, i8 0, i8 0, i8 poison>, i8 [[SEL18_SROA_0_0_COPYLOAD1836]], i64 0
+; CHECK-NEXT:    [[TMP8:%.*]] = shufflevector <2 x i8> [[TMP6]], <2 x i8> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
+; CHECK-NEXT:    [[TMP9:%.*]] = shufflevector <4 x i8> [[TMP7]], <4 x i8> [[TMP8]], <4 x i32> <i32 0, i32 1, i32 2, i32 5>
+; CHECK-NEXT:    br i1 [[TOBOOL_NOT]], label %[[IF_END3:.*]], label %[[LBL_SW43:.*]]
+; CHECK:       [[IF_END3]]:
+; CHECK-NEXT:    [[TMP10:%.*]] = phi <2 x i8> [ [[TMP24:%.*]], %[[LBL_BR73:.*]] ], [ [[TMP5]], %[[LBL_ENTRY]] ]
+; CHECK-NEXT:    [[TMP11:%.*]] = phi <4 x i8> [ [[TMP25:%.*]], %[[LBL_BR73]] ], [ [[TMP9]], %[[LBL_ENTRY]] ]
+; CHECK-NEXT:    br i1 [[TOBOOL_NOT]], label %[[IF_END6:.*]], label %[[LBL_SW35]]
+; CHECK:       [[IF_END6]]:
+; CHECK-NEXT:    [[SEL18_SROA_6_1:%.*]] = extractelement <4 x i8> [[TMP11]], i64 3
+; CHECK-NEXT:    store i8 [[SEL18_SROA_6_1]], ptr [[G4]], align 1
+; CHECK-NEXT:    [[TMP13:%.*]] = shufflevector <4 x i8> <i8 0, i8 poison, i8 poison, i8 poison>, <4 x i8> [[TMP11]], <4 x i32> <i32 0, i32 4, i32 poison, i32 poison>
+; CHECK-NEXT:    [[TMP14:%.*]] = shufflevector <2 x i8> [[TMP10]], <2 x i8> poison, <4 x i32> <i32 1, i32 0, i32 poison, i32 poison>
+; CHECK-NEXT:    [[TMP15:%.*]] = shufflevector <4 x i8> [[TMP13]], <4 x i8> [[TMP14]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+; CHECK-NEXT:    br label %[[LBL_BR73]]
+; CHECK:       [[LBL_SW35]]:
+; CHECK-NEXT:    [[TMP16]] = shufflevector <4 x i8> [[TMP11]], <4 x i8> poison, <2 x i32> <i32 1, i32 2>
+; CHECK-NEXT:    br i1 [[A4]], label %[[LBL_ENTRY]], label %[[IF_END12:.*]]
+; CHECK:       [[IF_END12]]:
+; CHECK-NEXT:    [[V19_1:%.*]] = extractelement <2 x i8> [[TMP10]], i64 1
+; CHECK-NEXT:    ret i8 [[V19_1]]
+; CHECK:       [[LBL_SW43]]:
+; CHECK-NEXT:    [[TMP18:%.*]] = insertelement <4 x i8> <i8 poison, i8 0, i8 poison, i8 0>, i8 [[SEL18_SROA_0_0_COPYLOAD1836]], i64 0
+; CHECK-NEXT:    [[TMP19:%.*]] = extractelement <2 x i8> [[TMP5]], i64 0
+; CHECK-NEXT:    br i1 [[A4]], label %[[LBL_BR73]], label %[[IF_END15:.*]]
+; CHECK:       [[IF_END15]]:
+; CHECK-NEXT:    [[TMP20:%.*]] = shufflevector <2 x i8> [[TMP6]], <2 x i8> poison, <4 x i32> <i32 0, i32 poison, i32 poison, i32 poison>
+; CHECK-NEXT:    [[TMP21:%.*]] = shufflevector <4 x i8> <i8 0, i8 0, i8 poison, i8 0>, <4 x i8> [[TMP8]], <4 x i32> <i32 0, i32 1, i32 4, i32 3>
+; CHECK-NEXT:    br label %[[LBL_BR73]]
+; CHECK:       [[LBL_BR73]]:
+; CHECK-NEXT:    [[SEL18_SROA_6_0_COPYLOAD1943:%.*]] = phi i8 [ [[TMP19]], %[[LBL_SW43]] ], [ 0, %[[IF_END6]] ], [ 0, %[[IF_END15]] ]
+; CHECK-NEXT:    [[TMP22:%.*]] = phi <4 x i8> [ [[TMP18]], %[[LBL_SW43]] ], [ [[TMP15]], %[[IF_END6]] ], [ [[TMP21]], %[[IF_END15]] ]
+; CHECK-NEXT:    [[TMP23:%.*]] = shufflevector <4 x i8> [[TMP22]], <4 x i8> poison, <2 x i32> <i32 poison, i32 2>
+; CHECK-NEXT:    [[TMP24]] = insertelement <2 x i8> [[TMP23]], i8 [[SEL18_SROA_6_0_COPYLOAD1943]], i64 0
+; CHECK-NEXT:    [[TMP25]] = shufflevector <4 x i8> [[TMP22]], <4 x i8> poison, <4 x i32> <i32 0, i32 1, i32 3, i32 3>
+; CHECK-NEXT:    br label %[[IF_END3]]
+;
+entry:
+  %sel18.sroa.6.0.copyload.peel = load i8, ptr getelementptr inbounds nuw (i8, ptr @g20, i64 1), align 1
+  %sel18.sroa.6.0.copyload34 = select i1 %a4, i8 %sel18.sroa.6.0.copyload.peel, i8 0
+  %sel18.sroa.0.0.copyload.peel = load i8, ptr @g20, align 1
+  %sel18.sroa.0.0.copyload27 = select i1 %a4, i8 %sel18.sroa.0.0.copyload.peel, i8 0
+  %v19.1.peel = select i1 %a4, i8 undef, i8 %sel18.sroa.0.0.peel
+  br label %lbl_entry
+
+lbl_entry:
+  %sel18.sroa.6.0.copyload1941 = phi i8 [ %sel18.sroa.6.0.copyload34, %entry ], [ 0, %lbl_sw35 ]
+  %sel18.sroa.0.0.copyload1836 = phi i8 [ %sel18.sroa.0.0.copyload27, %entry ], [ 0, %lbl_sw35 ]
+  %sel18.sroa.6.0.copyload = phi i8 [ 0, %entry ], [ %sel18.sroa.6.0.copyload30, %lbl_sw35 ]
+  %sel18.sroa.0.0.copyload = phi i8 [ 0, %entry ], [ %sel18.sroa.0.0.copyload23, %lbl_sw35 ]
+  %v19.0 = phi i8 [ %v19.1.peel, %entry ], [ 0, %lbl_sw35 ]
+  br i1 %tobool.not, label %if.end3, label %lbl_sw43
+
+if.end3:
+  %sel18.sroa.6.0.copyload1942 = phi i8 [ %sel18.sroa.6.0.copyload1943, %lbl_br73 ], [ %sel18.sroa.6.0.copyload1941, %lbl_entry ]
+  %sel18.sroa.0.0.copyload1837 = phi i8 [ %sel18.sroa.0.0.copyload1838, %lbl_br73 ], [ %sel18.sroa.0.0.copyload1836, %lbl_entry ]
+  %sel18.sroa.6.0.copyload30 = phi i8 [ %sel18.sroa.6.3, %lbl_br73 ], [ 0, %lbl_entry ]
+  %sel18.sroa.0.0.copyload23 = phi i8 [ %sel18.sroa.0.0.copyload24, %lbl_br73 ], [ 0, %lbl_entry ]
+  %sel18.sroa.6.1 = phi i8 [ %sel18.sroa.6.3, %lbl_br73 ], [ %sel18.sroa.6.0.copyload, %lbl_entry ]
+  %v19.1 = phi i8 [ %v19.4, %lbl_br73 ], [ %v19.0, %lbl_entry ]
+  br i1 %tobool.not, label %if.end6, label %lbl_sw35
+
+if.end6:
+  store i8 %sel18.sroa.6.1, ptr %g4, align 1
+  br label %lbl_br73
+
+lbl_sw35:
+  br i1 %a4, label %lbl_entry, label %if.end12
+
+if.end12:
+  ret i8 %v19.1
+
+lbl_sw43:
+  br i1 %a4, label %lbl_br73, label %if.end15
+
+if.end15:
+  br label %lbl_br73
+
+lbl_br73:
+  %sel18.sroa.6.0.copyload1943 = phi i8 [ %sel18.sroa.6.0.copyload1941, %lbl_sw43 ], [ 0, %if.end6 ], [ 0, %if.end15 ]
+  %sel18.sroa.0.0.copyload1838 = phi i8 [ %sel18.sroa.0.0.copyload1836, %lbl_sw43 ], [ 0, %if.end6 ], [ 0, %if.end15 ]
+  %sel18.sroa.0.0.copyload24 = phi i8 [ 0, %lbl_sw43 ], [ %sel18.sroa.0.0.copyload1837, %if.end6 ], [ 0, %if.end15 ]
+  %sel18.sroa.6.3 = phi i8 [ 0, %lbl_sw43 ], [ %sel18.sroa.6.0.copyload1942, %if.end6 ], [ 0, %if.end15 ]
+  %v19.4 = phi i8 [ poison, %lbl_sw43 ], [ %v19.1, %if.end6 ], [ %sel18.sroa.0.0.copyload, %if.end15 ]
+  br label %if.end3
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/219704


More information about the llvm-commits mailing list