[llvm] [SLP] Do not vectorize copyable operands of expanded binops (PR #203285)
Alexey Bataev via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 11 07:14:23 PDT 2026
https://github.com/alexey-bataev created https://github.com/llvm/llvm-project/pull/203285
An operand modeled as a copyable element on one operand edge of an
expanded binop (shl X, 1 represented as add X, X) leaves the duplicated
operand edge as a plain gathered operand with no copyable. The scheduler
then decrements that operand's ScheduleData for a use calculateDependencies
never counted for it, so its unscheduled-deps counter goes negative and
trips the assertion. Reject such bundles instead.
Fixes #203193
>From 10638b29ea02ccc8aadf893a688ef78484427704 Mon Sep 17 00:00:00 2001
From: Alexey Bataev <a.bataev at outlook.com>
Date: Thu, 11 Jun 2026 07:14:10 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 23 +++++++++++
.../expanded-binop-copyable-operand-deps.ll | 38 +++++++++++++++++++
2 files changed, 61 insertions(+)
create mode 100644 llvm/test/Transforms/SLPVectorizer/X86/expanded-binop-copyable-operand-deps.ll
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 5210088ded294..ebf4030e7b9f3 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -25403,6 +25403,29 @@ BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
}))
return std::nullopt;
+ // Reject modeling the duplicated source operand of an expanded binop as a
+ // copyable element. shl X, 1 is represented as add X, X, so the parent uses X
+ // on a second operand edge that stays a plain (gathered) operand. At schedule
+ // time that duplicated use is decremented against X's own ScheduleData, which
+ // calculateDependencies attributed to the copyable instead, so X's
+ // unscheduled-deps counter goes negative and trips the assertion. Only the
+ // values this bundle models as copyable that are the expanded binop source
+ // operand are checked, matched by value so the detection is unaffected by the
+ // parent node's operand reordering or reuse shuffle mask.
+ // TODO: investigate modeling such operands correctly.
+ if (S.areInstructionsWithCopyableElements() && EI.UserTE &&
+ EI.UserTE->hasState()) {
+ for (Value *V : VL) {
+ if (!S.isCopyableElement(V))
+ continue;
+ if (any_of(EI.UserTE->Scalars, [&](Value *PV) {
+ auto *I = dyn_cast<Instruction>(PV);
+ return I && EI.UserTE->isExpandedBinOp(I) && I->getOperand(0) == V;
+ }))
+ return std::nullopt;
+ }
+ }
+
// Initialize the instruction bundle.
Instruction *OldScheduleEnd = ScheduleEnd;
LLVM_DEBUG(dbgs() << "SLP: bundle: " << *S.getMainOp() << "\n");
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/expanded-binop-copyable-operand-deps.ll b/llvm/test/Transforms/SLPVectorizer/X86/expanded-binop-copyable-operand-deps.ll
new file mode 100644
index 0000000000000..d37e1ed5986c9
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/expanded-binop-copyable-operand-deps.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+define void @test() {
+; CHECK-LABEL: define void @test() {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[LBL_BR13:.*]]
+; CHECK: [[LBL_BR13]]:
+; CHECK-NEXT: [[TMP0:%.*]] = phi i16 [ 0, %[[ENTRY]] ], [ [[TMP6:%.*]], %[[LBL_BR13]] ]
+; CHECK-NEXT: [[TMP1:%.*]] = phi i16 [ 0, %[[ENTRY]] ], [ [[TMP4:%.*]], %[[LBL_BR13]] ]
+; CHECK-NEXT: [[TMP2:%.*]] = phi i16 [ 0, %[[ENTRY]] ], [ [[TMP6]], %[[LBL_BR13]] ]
+; CHECK-NEXT: [[TMP3:%.*]] = phi i16 [ 0, %[[ENTRY]] ], [ [[SUB32:%.*]], %[[LBL_BR13]] ]
+; CHECK-NEXT: [[SUB:%.*]] = sub i16 0, 0
+; CHECK-NEXT: store i16 [[SUB]], ptr null, align 2
+; CHECK-NEXT: [[TMP4]] = and i16 [[SUB]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = load i16, ptr null, align 2
+; CHECK-NEXT: [[SUB32]] = add i16 [[TMP5]], 1
+; CHECK-NEXT: [[CONV34:%.*]] = trunc i16 [[SUB32]] to i8
+; CHECK-NEXT: [[TMP6]] = shl i16 [[TMP5]], 1
+; CHECK-NEXT: br label %[[LBL_BR13]]
+;
+entry:
+ br label %lbl_br13
+
+lbl_br13:
+ %0 = phi i16 [ 0, %entry ], [ %6, %lbl_br13 ]
+ %1 = phi i16 [ 0, %entry ], [ %4, %lbl_br13 ]
+ %2 = phi i16 [ 0, %entry ], [ %6, %lbl_br13 ]
+ %3 = phi i16 [ 0, %entry ], [ %sub32, %lbl_br13 ]
+ %sub = sub i16 0, 0
+ store i16 %sub, ptr null, align 2
+ %4 = and i16 %sub, 0
+ %5 = load i16, ptr null, align 2
+ %sub32 = add i16 %5, 1
+ %conv34 = trunc i16 %sub32 to i8
+ %6 = shl i16 %5, 1
+ br label %lbl_br13
+}
More information about the llvm-commits
mailing list