[llvm] [SLP] Recompute copyable operand deps for nodes sharing an instruction (PR #202032)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 6 04:22:21 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Alexey Bataev (alexey-bataev)
<details>
<summary>Changes</summary>
When an instruction is vectorized in several nodes and one models an operand as
copyable while another (built later) uses it directly, the operand's dependency
count missed the direct def-use edge and the scheduler decremented it more times
than its count, tripping the unscheduled-deps assertion. Defer such operand dep
recomputation unconditionally via RecalcCopyableOperandDeps instead of the narrow
IsDuplicateCopyableNode gate.
Fixes #<!-- -->201855
---
Full diff: https://github.com/llvm/llvm-project/pull/202032.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+25-70)
- (added) llvm/test/Transforms/SLPVectorizer/X86/recalc-copyable-operand-deps-shared-inst.ll (+51)
``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index c0b7fcb2b5864..64fe45433af30 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -6432,15 +6432,15 @@ class slpvectorizer::BoUpSLP {
int ScheduleRegionSizeLimit = ScheduleRegionSizeBudget;
/// Operands that are modeled as copyable elements in a previously built
- /// vectorized node and that are used directly by a duplicate node with the
- /// same schedulable instructions. Their direct dependencies must be
- /// recomputed at the next bundle scheduling, when the duplicate node is
- /// already registered in the tree, so that the direct use is accounted for.
- /// If the duplicate node is the last scheduled bundle and no further
- /// scheduling consumes this list, the leftover entries are dropped on the
- /// next region reset and the dependencies are recomputed against the full
- /// tree in scheduleBlock instead. A set is used to avoid recomputing the
- /// same operand more than once.
+ /// vectorized node and that are used directly by another,
+ /// not-yet-registered node sharing a schedulable instruction with it. Their
+ /// direct dependencies must be recomputed at the next bundle scheduling,
+ /// when the new node is already registered in the tree, so that the direct
+ /// use is accounted for. If the new node is the last scheduled bundle and
+ /// no further scheduling consumes this list, the leftover entries are
+ /// dropped on the next region reset and the dependencies are recomputed
+ /// against the full tree in scheduleBlock instead. A set is used to avoid
+ /// recomputing the same operand more than once.
SmallSetVector<ScheduleData *, 8> RecalcCopyableOperandDeps;
/// The ID of the scheduling region. For a new vectorization iteration this
@@ -25415,9 +25415,9 @@ BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
// copyable. It may have memory deps, which must be recalculated.
SmallVector<ScheduleData *> ControlDependentMembers;
// Recompute the direct dependencies of copyable operands deferred by a
- // previous duplicate-copyable bundle. The duplicate node that introduced
- // the extra direct use is now part of the tree, so clearing and
- // recalculating the dependencies here accounts for that use.
+ // previously scheduled bundle. The node that introduced the extra direct
+ // use is now part of the tree, so clearing and recalculating the
+ // dependencies here accounts for that use.
for (ScheduleData *SD : RecalcCopyableOperandDeps) {
if (!isInSchedulingRegion(*SD))
continue;
@@ -25427,55 +25427,6 @@ BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
}
RecalcCopyableOperandDeps.clear();
auto CheckIfNeedToClearDeps = [&](ScheduleBundle &Bundle) {
- // The current set of values may duplicate a previously vectorized node
- // that has copyable elements (same schedulable instructions, just a
- // different copyable lane), while the parent node also has copyable
- // elements. Such a duplicate node is not part of the tree yet, so an
- // operand that is modeled as a copyable element in that previous node is
- // used directly by this node. Its direct dependencies recomputed now
- // would miss this extra direct use (the duplicate node is not registered
- // yet), making the scheduler decrement the operand more times than its
- // dependency count and tripping the unscheduled-deps assertion. Detect
- // this case to schedule a recalculation of those operand dependencies at
- // the next bundle scheduling, when the duplicate node is in the tree.
- // Lazily computed and cached: the detection scan is only needed when an
- // operand actually reaches the deferral branch below, so avoid running it
- // for every bundle whose user has copyable elements.
- std::optional<bool> IsDuplicateCopyableNodeCache;
- auto IsDuplicateCopyableNode = [&]() -> bool {
- if (IsDuplicateCopyableNodeCache)
- return *IsDuplicateCopyableNodeCache;
- bool Result = false;
- if (EI.UserTE && EI.UserTE->hasState() &&
- EI.UserTE->hasCopyableElements()) {
- SmallDenseSet<const Value *> BundleInsts;
- for (const ScheduleEntity *SE : Bundle.getBundle())
- if (isa<ScheduleData>(SE))
- BundleInsts.insert(SE->getInst());
- // Match a previously built entry E that contains every schedulable
- // instruction of the bundle as a non-copyable scalar. E need not be a
- // copyable node itself and may group additional values; a shared
- // instruction may still carry an operand modeled as a copyable
- // element through E's operand child.
- Result = !BundleInsts.empty() &&
- any_of(SLP->getTreeEntries(S.getMainOp()),
- [&](const TreeEntry *E) {
- if (!E->hasState())
- return false;
- SmallDenseSet<const Value *> MatchedInsts;
- for (Value *V : E->Scalars) {
- auto *I = dyn_cast<Instruction>(V);
- if (!I || E->isCopyableElement(I))
- continue;
- if (BundleInsts.contains(I))
- MatchedInsts.insert(I);
- }
- return MatchedInsts.size() == BundleInsts.size();
- });
- }
- IsDuplicateCopyableNodeCache = Result;
- return Result;
- };
SmallDenseMap<std::pair<Instruction *, Value *>, unsigned> UserOpToNumOps;
for (ScheduleEntity *SE : Bundle.getBundle()) {
if (ScheduleCopyableData *SD = dyn_cast<ScheduleCopyableData>(SE)) {
@@ -25514,15 +25465,19 @@ BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
if (RegionHasStackSave ||
!isGuaranteedToTransferExecutionToSuccessor(OpSD->getInst()))
ControlDependentMembers.push_back(OpSD);
- // The operand is modeled as a copyable element in the previously
- // built node, but it is used directly by this duplicate node,
- // which is not registered in the tree yet. Recomputing its
- // dependencies here would miss that direct use. Remember it and
- // recompute once the duplicate node is part of the tree (at the
- // next bundle scheduling), otherwise the scheduler decrements it
- // more times than its dependency count and crashes.
- if (IsDuplicateCopyableNode())
- RecalcCopyableOperandDeps.insert(OpSD);
+ // areAllOperandsReplacedByCopyableData() returned true, so every
+ // tree entry that currently contains this instruction models Op
+ // as a copyable element and Op's recomputed dependencies do not
+ // count this def-use edge. The bundle being scheduled is a new
+ // node for the same instruction that is not registered in the
+ // tree yet and that uses Op directly (Op is a real operand here).
+ // Once that node is added to the tree the direct use must be
+ // counted, otherwise the scheduler decrements Op more times than
+ // its dependency count and trips the unscheduled-deps assertion.
+ // Defer the operand's dependency recomputation to the next bundle
+ // scheduling, when the new node is part of the tree and the
+ // direct use is accounted for.
+ RecalcCopyableOperandDeps.insert(OpSD);
continue;
}
}
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/recalc-copyable-operand-deps-shared-inst.ll b/llvm/test/Transforms/SLPVectorizer/X86/recalc-copyable-operand-deps-shared-inst.ll
new file mode 100644
index 0000000000000..d730dc82ed7bb
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/recalc-copyable-operand-deps-shared-inst.ll
@@ -0,0 +1,51 @@
+; 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 -slp-threshold=-100 < %s | FileCheck %s
+
+define i32 @test(ptr %r, i32 %0) {
+; CHECK-LABEL: define i32 @test(
+; CHECK-SAME: ptr [[R:%.*]], i32 [[TMP0:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[SHR_I:%.*]] = lshr i32 6, [[TMP0]]
+; CHECK-NEXT: [[TMP1:%.*]] = insertelement <2 x i32> poison, i32 [[TMP0]], i32 0
+; CHECK-NEXT: [[TMP13:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
+; CHECK-NEXT: [[TMP15:%.*]] = add <2 x i32> [[TMP13]], splat (i32 1)
+; CHECK-NEXT: [[TMP16:%.*]] = and <2 x i32> [[TMP13]], splat (i32 1)
+; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <2 x i32> [[TMP15]], <2 x i32> [[TMP16]], <2 x i32> <i32 0, i32 3>
+; CHECK-NEXT: [[TMP20:%.*]] = insertelement <2 x i32> <i32 poison, i32 0>, i32 [[TMP0]], i32 0
+; CHECK-NEXT: [[TMP7:%.*]] = insertelement <2 x i32> <i32 -1, i32 poison>, i32 [[SHR_I]], i32 1
+; CHECK-NEXT: [[TMP8:%.*]] = sub <2 x i32> [[TMP20]], [[TMP7]]
+; CHECK-NEXT: [[TMP9:%.*]] = shufflevector <2 x i32> [[TMP13]], <2 x i32> [[TMP7]], <2 x i32> <i32 0, i32 3>
+; CHECK-NEXT: [[TMP10:%.*]] = lshr <2 x i32> <i32 8, i32 0>, [[TMP9]]
+; CHECK-NEXT: [[TMP11:%.*]] = sub <2 x i32> <i32 8, i32 0>, [[TMP9]]
+; CHECK-NEXT: [[TMP12:%.*]] = shufflevector <2 x i32> [[TMP10]], <2 x i32> [[TMP11]], <2 x i32> <i32 0, i32 3>
+; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[TMP12]], <i32 0, i32 1>
+; CHECK-NEXT: [[TMP14:%.*]] = add <2 x i32> [[TMP8]], [[TMP12]]
+; CHECK-NEXT: [[MUL47_NEG_I:%.*]] = extractelement <2 x i32> [[TMP14]], i32 1
+; CHECK-NEXT: store i32 [[MUL47_NEG_I]], ptr [[R]], align 4
+; CHECK-NEXT: [[TMP4:%.*]] = add <2 x i32> [[TMP2]], [[TMP3]]
+; CHECK-NEXT: [[TMP17:%.*]] = add <2 x i32> [[TMP4]], [[TMP14]]
+; CHECK-NEXT: [[TMP18:%.*]] = shufflevector <2 x i32> [[TMP17]], <2 x i32> <i32 -805306497, i32 poison>, <2 x i32> <i32 2, i32 1>
+; CHECK-NEXT: [[TMP19:%.*]] = add <2 x i32> [[TMP17]], [[TMP18]]
+; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x i32> [[TMP19]], i32 0
+; CHECK-NEXT: [[TMP6:%.*]] = extractelement <2 x i32> [[TMP19]], i32 1
+; CHECK-NEXT: [[SUB265_I:%.*]] = or i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT: ret i32 [[SUB265_I]]
+;
+entry:
+ %shr.i = lshr i32 6, %0
+ %and.i = and i32 %0, 1
+ %sub8.i = sub i32 0, %shr.i
+ %xor.i = xor i32 %sub8.i, 1
+ %add65.i = add i32 %and.i, %xor.i
+ %mul47.neg.i = shl i32 %sub8.i, 1
+ store i32 %mul47.neg.i, ptr %r, align 4
+ %not.i = add i32 %0, 1
+ %shr111.i = lshr i32 8, %0
+ %add112.i = add i32 %not.i, %shr111.i
+ %mul264.neg.i = shl i32 %add112.i, 1
+ %not263.i = add i32 %mul264.neg.i, -805306497
+ %sub66.i = add i32 %add65.i, %mul47.neg.i
+ %mul80.i = shl i32 %sub66.i, 1
+ %sub265.i = or i32 %not263.i, %mul80.i
+ ret i32 %sub265.i
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/202032
More information about the llvm-commits
mailing list