Skip to content

Commit 5e8ec3b

Browse files
igogo-x86Igor.Kirillov@arm.com
authored andcommitted
[VectorCombine] Refine cost model and decision logic in foldSelectShuffle
After PR #136329, shuffle indices may differ, which can cause the existing cost-based logic to miss optimization opportunities for binop/shuffle sequences. This patch improves the cost model in foldSelectShuffle to more accurately assess costs, recognizing when certain duplicate shuffles do not require actual instructions. Additionally, in break-even cases, this change introduces a check for whether the pattern ultimately feeds into a vector reduction, allowing the transform to proceed when it is likely to be profitable overall.
1 parent e38f98f commit 5e8ec3b

File tree

2 files changed

+139
-27
lines changed

2 files changed

+139
-27
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3174,6 +3174,47 @@ bool VectorCombine::foldCastFromReductions(Instruction &I) {
31743174
return true;
31753175
}
31763176

3177+
/// Returns true if this ShuffleVectorInst eventually feeds into a
3178+
/// vector reduction intrinsic (e.g., vector_reduce_add) by only following
3179+
/// chains of shuffles and binary operators (in any combination/order).
3180+
static bool feedsIntoVectorReduction(ShuffleVectorInst *SVI) {
3181+
SmallPtrSet<Instruction *, 8> Visited;
3182+
SmallVector<Instruction *, 4> WorkList;
3183+
bool FoundReduction = false;
3184+
3185+
WorkList.push_back(SVI);
3186+
while (!WorkList.empty()) {
3187+
Instruction *I = WorkList.pop_back_val();
3188+
for (User *U : I->users()) {
3189+
auto *UI = dyn_cast<Instruction>(U);
3190+
if (!UI || !Visited.insert(UI).second)
3191+
continue;
3192+
if (auto *II = dyn_cast<IntrinsicInst>(UI)) {
3193+
// More than one reduction reached
3194+
if (FoundReduction)
3195+
return false;
3196+
switch (II->getIntrinsicID()) {
3197+
case Intrinsic::vector_reduce_add:
3198+
case Intrinsic::vector_reduce_mul:
3199+
case Intrinsic::vector_reduce_and:
3200+
case Intrinsic::vector_reduce_or:
3201+
case Intrinsic::vector_reduce_xor:
3202+
FoundReduction = true;
3203+
continue;
3204+
default:
3205+
return false;
3206+
}
3207+
}
3208+
3209+
if (!isa<BinaryOperator>(UI) && !isa<ShuffleVectorInst>(UI))
3210+
return false;
3211+
3212+
WorkList.emplace_back(UI);
3213+
}
3214+
}
3215+
return FoundReduction;
3216+
}
3217+
31773218
/// This method looks for groups of shuffles acting on binops, of the form:
31783219
/// %x = shuffle ...
31793220
/// %y = shuffle ...
@@ -3416,15 +3457,80 @@ bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
34163457
TTI.getShuffleCost(TTI::SK_PermuteTwoSrc, VT, VT, Mask, CostKind);
34173458
};
34183459

3460+
unsigned ElementSize = VT->getElementType()->getPrimitiveSizeInBits();
3461+
unsigned MaxVectorSize =
3462+
TTI.getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector);
3463+
unsigned MaxElementsInVector = MaxVectorSize / ElementSize;
3464+
// When there are multiple shufflevector operations on the same input,
3465+
// especially when the vector length is larger than the register size,
3466+
// identical shuffle patterns may occur across different groups of elements.
3467+
// To avoid overestimating the cost by counting these repeated shuffles more
3468+
// than once, we only account for unique shuffle patterns. This adjustment
3469+
// prevents inflated costs in the cost model for wide vectors split into
3470+
// several register-sized groups.
3471+
std::set<SmallVector<int, 4>> UniqueShuffles;
3472+
auto AddShuffleMaskAdjustedCost = [&](InstructionCost C, ArrayRef<int> Mask) {
3473+
// Compute the cost for performing the shuffle over the full vector.
3474+
auto ShuffleCost =
3475+
TTI.getShuffleCost(TTI::SK_PermuteTwoSrc, VT, VT, Mask, CostKind);
3476+
unsigned NumFullVectors = Mask.size() / MaxElementsInVector;
3477+
if (NumFullVectors < 2)
3478+
return C + ShuffleCost;
3479+
SmallVector<int, 4> SubShuffle(MaxElementsInVector);
3480+
unsigned NumUniqueGroups = 0;
3481+
unsigned NumGroups = Mask.size() / MaxElementsInVector;
3482+
// For each group of MaxElementsInVector contiguous elements,
3483+
// collect their shuffle pattern and insert into the set of unique patterns.
3484+
for (unsigned k = 0; k < NumFullVectors; ++k) {
3485+
for (unsigned l = 0; l < MaxElementsInVector; ++l)
3486+
SubShuffle[l] = Mask[MaxElementsInVector * k + l];
3487+
if (UniqueShuffles.insert(SubShuffle).second)
3488+
NumUniqueGroups += 1;
3489+
}
3490+
return C + ShuffleCost * NumUniqueGroups / NumGroups;
3491+
};
3492+
auto AddShuffleAdjustedCost = [&](InstructionCost C, Instruction *I) {
3493+
auto *SV = dyn_cast<ShuffleVectorInst>(I);
3494+
if (!SV)
3495+
return C;
3496+
SmallVector<int, 16> Mask;
3497+
SV->getShuffleMask(Mask);
3498+
return AddShuffleMaskAdjustedCost(C, Mask);
3499+
};
3500+
// Check that input consists of ShuffleVectors applied to the same input
3501+
auto AllShufflesHaveSameOperands =
3502+
[](SmallPtrSetImpl<Instruction *> &InputShuffles) {
3503+
if (InputShuffles.size() < 2)
3504+
return false;
3505+
ShuffleVectorInst *FirstSV =
3506+
dyn_cast<ShuffleVectorInst>(*InputShuffles.begin());
3507+
if (!FirstSV)
3508+
return false;
3509+
3510+
Value *In0 = FirstSV->getOperand(0), *In1 = FirstSV->getOperand(1);
3511+
return std::all_of(
3512+
std::next(InputShuffles.begin()), InputShuffles.end(),
3513+
[&](Instruction *I) {
3514+
ShuffleVectorInst *SV = dyn_cast<ShuffleVectorInst>(I);
3515+
return SV && SV->getOperand(0) == In0 && SV->getOperand(1) == In1;
3516+
});
3517+
};
3518+
34193519
// Get the costs of the shuffles + binops before and after with the new
34203520
// shuffle masks.
34213521
InstructionCost CostBefore =
34223522
TTI.getArithmeticInstrCost(Op0->getOpcode(), VT, CostKind) +
34233523
TTI.getArithmeticInstrCost(Op1->getOpcode(), VT, CostKind);
34243524
CostBefore += std::accumulate(Shuffles.begin(), Shuffles.end(),
34253525
InstructionCost(0), AddShuffleCost);
3426-
CostBefore += std::accumulate(InputShuffles.begin(), InputShuffles.end(),
3427-
InstructionCost(0), AddShuffleCost);
3526+
if (AllShufflesHaveSameOperands(InputShuffles)) {
3527+
UniqueShuffles.clear();
3528+
CostBefore += std::accumulate(InputShuffles.begin(), InputShuffles.end(),
3529+
InstructionCost(0), AddShuffleAdjustedCost);
3530+
} else {
3531+
CostBefore += std::accumulate(InputShuffles.begin(), InputShuffles.end(),
3532+
InstructionCost(0), AddShuffleCost);
3533+
}
34283534

34293535
// The new binops will be unused for lanes past the used shuffle lengths.
34303536
// These types attempt to get the correct cost for that from the target.
@@ -3435,8 +3541,9 @@ bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
34353541
InstructionCost CostAfter =
34363542
TTI.getArithmeticInstrCost(Op0->getOpcode(), Op0SmallVT, CostKind) +
34373543
TTI.getArithmeticInstrCost(Op1->getOpcode(), Op1SmallVT, CostKind);
3544+
UniqueShuffles.clear();
34383545
CostAfter += std::accumulate(ReconstructMasks.begin(), ReconstructMasks.end(),
3439-
InstructionCost(0), AddShuffleMaskCost);
3546+
InstructionCost(0), AddShuffleMaskAdjustedCost);
34403547
std::set<SmallVector<int>> OutputShuffleMasks({V1A, V1B, V2A, V2B});
34413548
CostAfter +=
34423549
std::accumulate(OutputShuffleMasks.begin(), OutputShuffleMasks.end(),
@@ -3445,7 +3552,8 @@ bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
34453552
LLVM_DEBUG(dbgs() << "Found a binop select shuffle pattern: " << I << "\n");
34463553
LLVM_DEBUG(dbgs() << " CostBefore: " << CostBefore
34473554
<< " vs CostAfter: " << CostAfter << "\n");
3448-
if (CostBefore <= CostAfter)
3555+
if (CostBefore < CostAfter || CostBefore == 0 ||
3556+
(CostBefore == CostAfter && !feedsIntoVectorReduction(SVI)))
34493557
return false;
34503558

34513559
// The cost model has passed, create the new instructions.

llvm/test/Transforms/PhaseOrdering/AArch64/slpordering.ll

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,33 @@ define i32 @slpordering(ptr noundef %p1, i32 noundef %ip1, ptr noundef %p2, i32
8080
; CHECK-NEXT: [[TMP47:%.*]] = shufflevector <16 x i32> [[TMP43]], <16 x i32> poison, <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
8181
; CHECK-NEXT: [[TMP48:%.*]] = add nsw <16 x i32> [[TMP45]], [[TMP47]]
8282
; CHECK-NEXT: [[TMP49:%.*]] = sub nsw <16 x i32> [[TMP44]], [[TMP46]]
83-
; CHECK-NEXT: [[TMP50:%.*]] = shufflevector <16 x i32> [[TMP48]], <16 x i32> [[TMP49]], <16 x i32> <i32 16, i32 0, i32 17, i32 1, i32 18, i32 2, i32 19, i32 3, i32 20, i32 4, i32 21, i32 5, i32 22, i32 6, i32 23, i32 7>
84-
; CHECK-NEXT: [[TMP51:%.*]] = shufflevector <16 x i32> [[TMP48]], <16 x i32> [[TMP49]], <16 x i32> <i32 17, i32 1, i32 16, i32 0, i32 19, i32 3, i32 18, i32 2, i32 21, i32 5, i32 20, i32 4, i32 23, i32 7, i32 22, i32 6>
85-
; CHECK-NEXT: [[TMP52:%.*]] = add nsw <16 x i32> [[TMP50]], [[TMP51]]
86-
; CHECK-NEXT: [[TMP53:%.*]] = sub nsw <16 x i32> [[TMP50]], [[TMP51]]
87-
; CHECK-NEXT: [[TMP54:%.*]] = shufflevector <16 x i32> [[TMP53]], <16 x i32> [[TMP52]], <16 x i32> <i32 0, i32 1, i32 18, i32 19, i32 4, i32 5, i32 22, i32 23, i32 8, i32 9, i32 26, i32 27, i32 12, i32 13, i32 30, i32 31>
88-
; CHECK-NEXT: [[TMP55:%.*]] = shufflevector <16 x i32> [[TMP54]], <16 x i32> poison, <16 x i32> <i32 4, i32 5, i32 6, i32 7, i32 0, i32 1, i32 2, i32 3, i32 12, i32 13, i32 14, i32 15, i32 8, i32 9, i32 10, i32 11>
89-
; CHECK-NEXT: [[TMP56:%.*]] = sub nsw <16 x i32> [[TMP54]], [[TMP55]]
90-
; CHECK-NEXT: [[TMP57:%.*]] = add nsw <16 x i32> [[TMP54]], [[TMP55]]
91-
; CHECK-NEXT: [[TMP58:%.*]] = shufflevector <16 x i32> [[TMP56]], <16 x i32> [[TMP57]], <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 20, i32 21, i32 22, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
92-
; CHECK-NEXT: [[TMP59:%.*]] = shufflevector <16 x i32> [[TMP56]], <16 x i32> [[TMP57]], <16 x i32> <i32 8, i32 9, i32 10, i32 11, i32 28, i32 29, i32 30, i32 31, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
93-
; CHECK-NEXT: [[TMP60:%.*]] = shufflevector <16 x i32> [[TMP56]], <16 x i32> [[TMP57]], <16 x i32> <i32 8, i32 9, i32 10, i32 11, i32 28, i32 29, i32 30, i32 31, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
94-
; CHECK-NEXT: [[TMP61:%.*]] = shufflevector <16 x i32> [[TMP56]], <16 x i32> [[TMP57]], <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 20, i32 21, i32 22, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
95-
; CHECK-NEXT: [[TMP62:%.*]] = add nsw <16 x i32> [[TMP59]], [[TMP61]]
96-
; CHECK-NEXT: [[TMP63:%.*]] = sub nsw <16 x i32> [[TMP58]], [[TMP60]]
97-
; CHECK-NEXT: [[TMP64:%.*]] = shufflevector <16 x i32> [[TMP62]], <16 x i32> [[TMP63]], <16 x i32> <i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
98-
; CHECK-NEXT: [[TMP65:%.*]] = lshr <16 x i32> [[TMP64]], splat (i32 15)
99-
; CHECK-NEXT: [[TMP66:%.*]] = and <16 x i32> [[TMP65]], splat (i32 65537)
100-
; CHECK-NEXT: [[TMP67:%.*]] = mul nuw <16 x i32> [[TMP66]], splat (i32 65535)
101-
; CHECK-NEXT: [[TMP68:%.*]] = add <16 x i32> [[TMP67]], [[TMP64]]
102-
; CHECK-NEXT: [[TMP69:%.*]] = xor <16 x i32> [[TMP68]], [[TMP67]]
103-
; CHECK-NEXT: [[TMP70:%.*]] = tail call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> [[TMP69]])
104-
; CHECK-NEXT: [[CONV118:%.*]] = and i32 [[TMP70]], 65535
105-
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[TMP70]], 16
83+
; CHECK-NEXT: [[TMP50:%.*]] = shufflevector <16 x i32> [[TMP48]], <16 x i32> [[TMP49]], <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 16, i32 18, i32 20, i32 22, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
84+
; CHECK-NEXT: [[TMP51:%.*]] = shufflevector <16 x i32> [[TMP48]], <16 x i32> [[TMP49]], <16 x i32> <i32 1, i32 3, i32 5, i32 7, i32 17, i32 19, i32 21, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
85+
; CHECK-NEXT: [[TMP52:%.*]] = shufflevector <16 x i32> [[TMP48]], <16 x i32> [[TMP49]], <16 x i32> <i32 1, i32 3, i32 5, i32 7, i32 17, i32 19, i32 21, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
86+
; CHECK-NEXT: [[TMP53:%.*]] = shufflevector <16 x i32> [[TMP48]], <16 x i32> [[TMP49]], <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 16, i32 18, i32 20, i32 22, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
87+
; CHECK-NEXT: [[TMP54:%.*]] = add nsw <16 x i32> [[TMP51]], [[TMP53]]
88+
; CHECK-NEXT: [[TMP55:%.*]] = sub nsw <16 x i32> [[TMP50]], [[TMP52]]
89+
; CHECK-NEXT: [[TMP56:%.*]] = shufflevector <16 x i32> [[TMP54]], <16 x i32> [[TMP55]], <16 x i32> <i32 1, i32 3, i32 5, i32 7, i32 17, i32 19, i32 21, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
90+
; CHECK-NEXT: [[TMP57:%.*]] = shufflevector <16 x i32> [[TMP54]], <16 x i32> [[TMP55]], <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 16, i32 18, i32 20, i32 22, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
91+
; CHECK-NEXT: [[TMP58:%.*]] = shufflevector <16 x i32> [[TMP54]], <16 x i32> [[TMP55]], <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 16, i32 18, i32 20, i32 22, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
92+
; CHECK-NEXT: [[TMP59:%.*]] = shufflevector <16 x i32> [[TMP54]], <16 x i32> [[TMP55]], <16 x i32> <i32 1, i32 3, i32 5, i32 7, i32 17, i32 19, i32 21, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
93+
; CHECK-NEXT: [[TMP60:%.*]] = sub nsw <16 x i32> [[TMP57]], [[TMP59]]
94+
; CHECK-NEXT: [[TMP61:%.*]] = add nsw <16 x i32> [[TMP56]], [[TMP58]]
95+
; CHECK-NEXT: [[TMP62:%.*]] = shufflevector <16 x i32> [[TMP60]], <16 x i32> [[TMP61]], <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 16, i32 18, i32 20, i32 22, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
96+
; CHECK-NEXT: [[TMP63:%.*]] = shufflevector <16 x i32> [[TMP60]], <16 x i32> [[TMP61]], <16 x i32> <i32 1, i32 3, i32 5, i32 7, i32 17, i32 19, i32 21, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
97+
; CHECK-NEXT: [[TMP64:%.*]] = shufflevector <16 x i32> [[TMP60]], <16 x i32> [[TMP61]], <16 x i32> <i32 1, i32 3, i32 5, i32 7, i32 17, i32 19, i32 21, i32 23, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
98+
; CHECK-NEXT: [[TMP65:%.*]] = shufflevector <16 x i32> [[TMP60]], <16 x i32> [[TMP61]], <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 16, i32 18, i32 20, i32 22, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
99+
; CHECK-NEXT: [[TMP66:%.*]] = add nsw <16 x i32> [[TMP63]], [[TMP65]]
100+
; CHECK-NEXT: [[TMP67:%.*]] = sub nsw <16 x i32> [[TMP62]], [[TMP64]]
101+
; CHECK-NEXT: [[TMP68:%.*]] = shufflevector <16 x i32> [[TMP66]], <16 x i32> [[TMP67]], <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23>
102+
; CHECK-NEXT: [[TMP69:%.*]] = lshr <16 x i32> [[TMP68]], splat (i32 15)
103+
; CHECK-NEXT: [[TMP70:%.*]] = and <16 x i32> [[TMP69]], splat (i32 65537)
104+
; CHECK-NEXT: [[TMP71:%.*]] = mul nuw <16 x i32> [[TMP70]], splat (i32 65535)
105+
; CHECK-NEXT: [[TMP72:%.*]] = add <16 x i32> [[TMP71]], [[TMP68]]
106+
; CHECK-NEXT: [[TMP73:%.*]] = xor <16 x i32> [[TMP72]], [[TMP71]]
107+
; CHECK-NEXT: [[TMP74:%.*]] = tail call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> [[TMP73]])
108+
; CHECK-NEXT: [[CONV118:%.*]] = and i32 [[TMP74]], 65535
109+
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[TMP74]], 16
106110
; CHECK-NEXT: [[RDD119:%.*]] = add nuw nsw i32 [[CONV118]], [[SHR]]
107111
; CHECK-NEXT: [[SHR120:%.*]] = lshr i32 [[RDD119]], 1
108112
; CHECK-NEXT: ret i32 [[SHR120]]

0 commit comments

Comments
 (0)