-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SLP]Add cost estimation for gather node reshuffling #115201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9e240ac
7420027
19a0ab5
b32a441
343cecc
c35fdb7
3ba9081
f299aad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13199,6 +13199,7 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry( | |
// No perfect match, just shuffle, so choose the first tree node from the | ||
// tree. | ||
Entries.push_back(FirstEntries.front()); | ||
VF = FirstEntries.front()->getVectorFactor(); | ||
} else { | ||
// Try to find nodes with the same vector factor. | ||
assert(UsedTEs.size() == 2 && "Expected at max 2 permuted entries."); | ||
|
@@ -13239,6 +13240,8 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry( | |
Entries.push_back(SecondEntries.front()); | ||
VF = std::max(Entries.front()->getVectorFactor(), | ||
Entries.back()->getVectorFactor()); | ||
} else { | ||
VF = Entries.front()->getVectorFactor(); | ||
} | ||
} | ||
|
||
|
@@ -13350,17 +13353,141 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry( | |
: Entries[Pair.first]->findLaneForValue(VL[Pair.second])); | ||
IsIdentity &= Mask[Idx] == Pair.second; | ||
} | ||
switch (Entries.size()) { | ||
case 1: | ||
if (IsIdentity || EntryLanes.size() > 1 || VL.size() <= 2) | ||
return TargetTransformInfo::SK_PermuteSingleSrc; | ||
break; | ||
case 2: | ||
if (EntryLanes.size() > 2 || VL.size() <= 2) | ||
return TargetTransformInfo::SK_PermuteTwoSrc; | ||
break; | ||
default: | ||
break; | ||
if (ForOrder || IsIdentity || Entries.empty()) { | ||
switch (Entries.size()) { | ||
case 1: | ||
if (IsIdentity || EntryLanes.size() > 1 || VL.size() <= 2) | ||
return TargetTransformInfo::SK_PermuteSingleSrc; | ||
break; | ||
case 2: | ||
if (EntryLanes.size() > 2 || VL.size() <= 2) | ||
return TargetTransformInfo::SK_PermuteTwoSrc; | ||
break; | ||
default: | ||
break; | ||
} | ||
} else if (!isa<VectorType>(VL.front()->getType()) && | ||
(EntryLanes.size() > Entries.size() || VL.size() <= 2)) { | ||
// Do the cost estimation if shuffle beneficial than buildvector. | ||
SmallVector<int> SubMask(std::next(Mask.begin(), Part * VL.size()), | ||
std::next(Mask.begin(), (Part + 1) * VL.size())); | ||
int MinElement = SubMask.front(), MaxElement = SubMask.front(); | ||
for (int Idx : SubMask) { | ||
if (Idx == PoisonMaskElem) | ||
continue; | ||
if (MinElement == PoisonMaskElem || MinElement % VF > Idx % VF) | ||
MinElement = Idx; | ||
if (MaxElement == PoisonMaskElem || MaxElement % VF < Idx % VF) | ||
MaxElement = Idx; | ||
} | ||
alexey-bataev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(MaxElement >= 0 && MinElement >= 0 && | ||
MaxElement % VF >= MinElement % VF && | ||
"Expected at least single element."); | ||
alexey-bataev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsigned NewVF = std::max<unsigned>( | ||
VL.size(), getFullVectorNumberOfElements(*TTI, VL.front()->getType(), | ||
(MaxElement % VF) - | ||
(MinElement % VF) + 1)); | ||
if (NewVF < VF) { | ||
for_each(SubMask, [&](int &Idx) { | ||
if (Idx == PoisonMaskElem) | ||
return; | ||
Idx = (Idx % VF) - (MinElement % VF) + | ||
(Idx >= static_cast<int>(VF) ? NewVF : 0); | ||
}); | ||
VF = NewVF; | ||
} | ||
|
||
constexpr TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; | ||
auto *VecTy = getWidenedType(VL.front()->getType(), VF); | ||
auto *MaskVecTy = getWidenedType(VL.front()->getType(), SubMask.size()); | ||
auto GetShuffleCost = [&, | ||
&TTI = *TTI](ArrayRef<int> Mask, | ||
ArrayRef<const TreeEntry *> Entries, | ||
VectorType *VecTy) -> InstructionCost { | ||
if (Entries.size() == 1 && Entries.front()->getInterleaveFactor() > 0 && | ||
ShuffleVectorInst::isDeInterleaveMaskOfFactor( | ||
Mask, Entries.front()->getInterleaveFactor())) | ||
return TTI::TCC_Free; | ||
return ::getShuffleCost(TTI, | ||
Entries.size() > 1 ? TTI::SK_PermuteTwoSrc | ||
: TTI::SK_PermuteSingleSrc, | ||
VecTy, Mask, CostKind); | ||
}; | ||
InstructionCost ShuffleCost = GetShuffleCost(SubMask, Entries, VecTy); | ||
InstructionCost FirstShuffleCost = 0; | ||
SmallVector<int> FirstMask(SubMask.begin(), SubMask.end()); | ||
if (Entries.size() == 1 || !Entries[0]->isGather()) { | ||
FirstShuffleCost = ShuffleCost; | ||
} else { | ||
// Transform mask to include only first entry. | ||
APInt DemandedElts = APInt::getAllOnes(SubMask.size()); | ||
bool IsIdentity = true; | ||
for (auto [I, Idx] : enumerate(FirstMask)) { | ||
if (Idx >= static_cast<int>(VF)) { | ||
Idx = PoisonMaskElem; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is Idx a reference? Make it explicit in the auto? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot do it here, since only Idx is reference. Syntax does not allow to express that only Idx is reference here |
||
} else { | ||
DemandedElts.clearBit(I); | ||
if (Idx != PoisonMaskElem) | ||
IsIdentity &= static_cast<int>(I) == Idx; | ||
} | ||
} | ||
if (!IsIdentity) | ||
FirstShuffleCost = GetShuffleCost(FirstMask, Entries.front(), VecTy); | ||
FirstShuffleCost += TTI->getScalarizationOverhead( | ||
MaskVecTy, DemandedElts, /*Insert=*/true, | ||
/*Extract=*/false, CostKind); | ||
} | ||
InstructionCost SecondShuffleCost = 0; | ||
SmallVector<int> SecondMask(SubMask.begin(), SubMask.end()); | ||
if (Entries.size() == 1 || !Entries[1]->isGather()) { | ||
SecondShuffleCost = ShuffleCost; | ||
} else { | ||
// Transform mask to include only first entry. | ||
APInt DemandedElts = APInt::getAllOnes(SubMask.size()); | ||
bool IsIdentity = true; | ||
for (auto [I, Idx] : enumerate(SecondMask)) { | ||
if (Idx < static_cast<int>(VF) && Idx >= 0) { | ||
Idx = PoisonMaskElem; | ||
alexey-bataev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
DemandedElts.clearBit(I); | ||
if (Idx != PoisonMaskElem) { | ||
Idx -= VF; | ||
IsIdentity &= static_cast<int>(I) == Idx; | ||
} | ||
} | ||
} | ||
if (!IsIdentity) | ||
SecondShuffleCost = GetShuffleCost(SecondMask, Entries[1], VecTy); | ||
SecondShuffleCost += TTI->getScalarizationOverhead( | ||
MaskVecTy, DemandedElts, /*Insert=*/true, | ||
/*Extract=*/false, CostKind); | ||
} | ||
APInt DemandedElts = APInt::getAllOnes(SubMask.size()); | ||
for (auto [I, Idx] : enumerate(SubMask)) | ||
if (Idx == PoisonMaskElem) | ||
DemandedElts.clearBit(I); | ||
InstructionCost BuildVectorCost = | ||
TTI->getScalarizationOverhead(MaskVecTy, DemandedElts, /*Insert=*/true, | ||
/*Extract=*/false, CostKind); | ||
const TreeEntry *BestEntry = nullptr; | ||
if (FirstShuffleCost < ShuffleCost) { | ||
copy(FirstMask, std::next(Mask.begin(), Part * VL.size())); | ||
BestEntry = Entries.front(); | ||
ShuffleCost = FirstShuffleCost; | ||
} | ||
if (SecondShuffleCost < ShuffleCost) { | ||
copy(SecondMask, std::next(Mask.begin(), Part * VL.size())); | ||
BestEntry = Entries[1]; | ||
ShuffleCost = SecondShuffleCost; | ||
} | ||
if (BuildVectorCost >= ShuffleCost) { | ||
if (BestEntry) { | ||
Entries.clear(); | ||
Entries.push_back(BestEntry); | ||
} | ||
return Entries.size() > 1 ? TargetTransformInfo::SK_PermuteTwoSrc | ||
: TargetTransformInfo::SK_PermuteSingleSrc; | ||
} | ||
} | ||
Entries.clear(); | ||
// Clear the corresponding mask elements. | ||
|
Uh oh!
There was an error while loading. Please reload this page.