Skip to content

Commit 547ba97

Browse files
committed
[SLP]Fix mask generation after cost estimation
When estimating the cost of entries shuffles for buildvectors, need to rebuild original mask, not a generated submask, used for subregisters analysis. Fixes #122430
1 parent 7ebf0df commit 547ba97

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13443,14 +13443,15 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1344313443
for_each(SubMask, [&](int &Idx) {
1344413444
if (Idx == PoisonMaskElem)
1344513445
return;
13446-
Idx = (Idx % VF) - (MinElement % VF) +
13446+
Idx = (Idx % VF) - ((MinElement / NewVF) * NewVF) +
1344713447
(Idx >= static_cast<int>(VF) ? NewVF : 0);
1344813448
});
13449-
VF = NewVF;
13449+
} else {
13450+
NewVF = VF;
1345013451
}
1345113452

1345213453
constexpr TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
13453-
auto *VecTy = getWidenedType(VL.front()->getType(), VF);
13454+
auto *VecTy = getWidenedType(VL.front()->getType(), NewVF);
1345413455
auto *MaskVecTy = getWidenedType(VL.front()->getType(), SubMask.size());
1345513456
auto GetShuffleCost = [&,
1345613457
&TTI = *TTI](ArrayRef<int> Mask,
@@ -13475,7 +13476,7 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1347513476
APInt DemandedElts = APInt::getAllOnes(SubMask.size());
1347613477
bool IsIdentity = true;
1347713478
for (auto [I, Idx] : enumerate(FirstMask)) {
13478-
if (Idx >= static_cast<int>(VF)) {
13479+
if (Idx >= static_cast<int>(NewVF)) {
1347913480
Idx = PoisonMaskElem;
1348013481
} else {
1348113482
DemandedElts.clearBit(I);
@@ -13498,12 +13499,12 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1349813499
APInt DemandedElts = APInt::getAllOnes(SubMask.size());
1349913500
bool IsIdentity = true;
1350013501
for (auto [I, Idx] : enumerate(SecondMask)) {
13501-
if (Idx < static_cast<int>(VF) && Idx >= 0) {
13502+
if (Idx < static_cast<int>(NewVF) && Idx >= 0) {
1350213503
Idx = PoisonMaskElem;
1350313504
} else {
1350413505
DemandedElts.clearBit(I);
1350513506
if (Idx != PoisonMaskElem) {
13506-
Idx -= VF;
13507+
Idx -= NewVF;
1350713508
IsIdentity &= static_cast<int>(I) == Idx;
1350813509
}
1350913510
}
@@ -13523,12 +13524,24 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1352313524
/*Extract=*/false, CostKind);
1352413525
const TreeEntry *BestEntry = nullptr;
1352513526
if (FirstShuffleCost < ShuffleCost) {
13526-
copy(FirstMask, std::next(Mask.begin(), Part * VL.size()));
13527+
std::for_each(std::next(Mask.begin(), Part * VL.size()),
13528+
std::next(Mask.begin(), (Part + 1) * VL.size()),
13529+
[&](int &Idx) {
13530+
if (Idx >= static_cast<int>(VF))
13531+
Idx = PoisonMaskElem;
13532+
});
1352713533
BestEntry = Entries.front();
1352813534
ShuffleCost = FirstShuffleCost;
1352913535
}
1353013536
if (SecondShuffleCost < ShuffleCost) {
13531-
copy(SecondMask, std::next(Mask.begin(), Part * VL.size()));
13537+
std::for_each(std::next(Mask.begin(), Part * VL.size()),
13538+
std::next(Mask.begin(), (Part + 1) * VL.size()),
13539+
[&](int &Idx) {
13540+
if (Idx < static_cast<int>(VF))
13541+
Idx = PoisonMaskElem;
13542+
else
13543+
Idx -= VF;
13544+
});
1353213545
BestEntry = Entries[1];
1353313546
ShuffleCost = SecondShuffleCost;
1353413547
}

llvm/test/Transforms/SLPVectorizer/X86/bv-shuffle-mask.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ define i16 @test(i16 %v1, i16 %v2) {
1010
; CHECK-NEXT: [[TMP2:%.*]] = or <4 x i16> [[TMP0]], [[TMP1]]
1111
; CHECK-NEXT: [[TMP3:%.*]] = and <4 x i16> [[TMP0]], [[TMP1]]
1212
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <4 x i16> [[TMP2]], <4 x i16> [[TMP3]], <4 x i32> <i32 0, i32 1, i32 2, i32 7>
13-
; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <4 x i16> [[TMP1]], <4 x i16> poison, <2 x i32> <i32 0, i32 poison>
14-
; CHECK-NEXT: [[TMP6:%.*]] = insertelement <2 x i16> [[TMP5]], i16 [[V2]], i32 1
13+
; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <2 x i32> <i32 poison, i32 3>
14+
; CHECK-NEXT: [[TMP6:%.*]] = insertelement <2 x i16> [[TMP5]], i16 [[V1]], i32 0
1515
; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <2 x i16> [[TMP6]], <2 x i16> poison, <4 x i32> <i32 0, i32 0, i32 0, i32 1>
1616
; CHECK-NEXT: [[TMP8:%.*]] = or <4 x i16> [[TMP7]], zeroinitializer
1717
; CHECK-NEXT: [[TMP9:%.*]] = and <4 x i16> [[TMP4]], zeroinitializer

0 commit comments

Comments
 (0)