[llvm] [SLPVectorizer] Handle undef values in reused shuffle masks (PR #219662)

Oscar Priego via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 02:35:04 PDT 2026


https://github.com/Opriego created https://github.com/llvm/llvm-project/pull/219662

Fixes #219631.

`TreeEntry::isSame()` permits an `UndefValue` to match a `PoisonMaskElem`, but `processBuildVector()` only handled `PoisonValue` specially when reconstructing the shuffle mask.

As a result, an `undef` lane could be passed to `findLaneForValue()`, even though it does not correspond to a concrete scalar in `TreeEntry::Scalars`, triggering:

`Assertion 'FoundLane < getVectorFactor() && "Unable to find given value."' failed.`

Treat `UndefValue` as a poison mask element in `processBuildVector()`. Since `PoisonValue` derives from `UndefValue`, this preserves the existing poison behavior while also handling undef lanes consistently with `TreeEntry::isSame()`.

A reduced regression test is included covering a reordered/reused TreeEntry with an undef lane.


>From 3077edc7eb4e4201e0f0eddfc1c4208d4528a323 Mon Sep 17 00:00:00 2001
From: Oscar Priego Verdugo <oscar.priegov at gmail.com>
Date: Sat, 29 Aug 2026 03:33:47 -0600
Subject: [PATCH] [SLPVectorizer] Handle undef values in reused shuffle masks

---
 .../Transforms/Vectorize/SLPVectorizer.cpp    |  2 +-
 .../SLPVectorizer/X86/undef-reorder-reuse.ll  | 30 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/SLPVectorizer/X86/undef-reorder-reuse.ll

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index a7eaef3701415..3b181326b478b 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -22881,7 +22881,7 @@ ResTy BoUpSLP::processBuildVector(const TreeEntry *E, Type *ScalarTy,
           std::iota(Mask.begin(), Mask.end(), 0);
         } else {
           for (auto [I, V] : enumerate(E->Scalars)) {
-            if (isa<PoisonValue>(V)) {
+            if (isa<UndefValue>(V)) {
               Mask[I] = PoisonMaskElem;
               continue;
             }
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/undef-reorder-reuse.ll b/llvm/test/Transforms/SLPVectorizer/X86/undef-reorder-reuse.ll
new file mode 100644
index 0000000000000..93083a77b9982
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/undef-reorder-reuse.ll
@@ -0,0 +1,30 @@
+; RUN: opt -passes=slp-vectorizer -disable-output < %s
+
+; The undef lane matches a poison lane in a reordered, reused TreeEntry. It
+; must remain poison rather than being looked up as a concrete scalar lane.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i8 @undef_in_reordered_reuse() {
+entry:
+  %load.0 = load i8, ptr null, align 1
+  %load.1 = load i8, ptr getelementptr inbounds nuw (i8, ptr null, i64 1), align 1
+  br label %entry.next
+
+entry.next:
+  br i1 false, label %if, label %merge
+
+if:
+  %phi.load.1 = phi i8 [ %load.1, %entry.next ]
+  %phi.load.0 = phi i8 [ %load.0, %entry.next ]
+  %phi.undef = phi i8 [ undef, %entry.next ]
+  br label %merge
+
+merge:
+  %phi.0 = phi i8 [ %phi.load.1, %if ], [ %load.1, %entry.next ]
+  %phi.1 = phi i8 [ %phi.load.0, %if ], [ %load.0, %entry.next ]
+  %phi.2 = phi i8 [ %phi.load.1, %if ], [ %load.1, %entry.next ]
+  %result = phi i8 [ %phi.undef, %if ], [ poison, %entry.next ]
+  ret i8 %result
+}



More information about the llvm-commits mailing list