-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[CostModel][X86] Add initial costs for non-lane-crossing one/two input shuffles #114680
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
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 |
---|---|---|
|
@@ -1559,6 +1559,23 @@ InstructionCost X86TTIImpl::getShuffleCost( | |
return TTI::TCC_Free; | ||
} | ||
|
||
// Attempt to detect a cheaper inlane shuffle, avoiding 128-bit subvector | ||
// permutation. | ||
bool IsInLaneShuffle = false; | ||
if (BaseTp->getPrimitiveSizeInBits() > 0 && | ||
(BaseTp->getPrimitiveSizeInBits() % 128) == 0 && | ||
BaseTp->getScalarSizeInBits() == LT.second.getScalarSizeInBits() && | ||
Mask.size() == BaseTp->getElementCount().getKnownMinValue()) { | ||
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. Shall we also check that the original type of the vector element is the same as the type of the "legalized" (machine) vector type? 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. Sure, I think checking the scalar sizes match should be enough - I'll add that shortly |
||
unsigned NumLanes = BaseTp->getPrimitiveSizeInBits() / 128; | ||
unsigned NumEltsPerLane = Mask.size() / NumLanes; | ||
if ((Mask.size() % NumLanes) == 0) | ||
IsInLaneShuffle = all_of(enumerate(Mask), [&](const auto &P) { | ||
return P.value() == PoisonMaskElem || | ||
((P.value() % Mask.size()) / NumEltsPerLane) == | ||
(P.index() / NumEltsPerLane); | ||
}); | ||
} | ||
|
||
// Treat <X x bfloat> shuffles as <X x half>. | ||
if (LT.second.isVector() && LT.second.getScalarType() == MVT::bf16) | ||
LT.second = LT.second.changeVectorElementType(MVT::f16); | ||
|
@@ -1897,6 +1914,25 @@ InstructionCost X86TTIImpl::getShuffleCost( | |
if (auto KindCost = Entry->Cost[CostKind]) | ||
return LT.first * *KindCost; | ||
|
||
static const CostTblEntry AVX2InLaneShuffleTbl[] = { | ||
{TTI::SK_PermuteSingleSrc, MVT::v16i16, 1}, // vpshufb | ||
{TTI::SK_PermuteSingleSrc, MVT::v16f16, 1}, // vpshufb | ||
{TTI::SK_PermuteSingleSrc, MVT::v32i8, 1}, // vpshufb | ||
|
||
{TTI::SK_PermuteTwoSrc, MVT::v4f64, 2}, // 2*vshufpd + vblendpd | ||
{TTI::SK_PermuteTwoSrc, MVT::v8f32, 2}, // 2*vshufps + vblendps | ||
{TTI::SK_PermuteTwoSrc, MVT::v4i64, 2}, // 2*vpshufd + vpblendd | ||
{TTI::SK_PermuteTwoSrc, MVT::v8i32, 2}, // 2*vpshufd + vpblendd | ||
{TTI::SK_PermuteTwoSrc, MVT::v16i16, 2}, // 2*vpshufb + vpor | ||
{TTI::SK_PermuteTwoSrc, MVT::v16f16, 2}, // 2*vpshufb + vpor | ||
{TTI::SK_PermuteTwoSrc, MVT::v32i8, 2}, // 2*vpshufb + vpor | ||
}; | ||
|
||
if (IsInLaneShuffle && ST->hasAVX2()) | ||
if (const auto *Entry = | ||
CostTableLookup(AVX2InLaneShuffleTbl, Kind, LT.second)) | ||
return LT.first * Entry->Cost; | ||
|
||
static const CostTblEntry AVX2ShuffleTbl[] = { | ||
{TTI::SK_Broadcast, MVT::v4f64, 1}, // vbroadcastpd | ||
{TTI::SK_Broadcast, MVT::v8f32, 1}, // vbroadcastps | ||
|
@@ -1973,6 +2009,36 @@ InstructionCost X86TTIImpl::getShuffleCost( | |
if (const auto *Entry = CostTableLookup(XOPShuffleTbl, Kind, LT.second)) | ||
return LT.first * Entry->Cost; | ||
|
||
static const CostTblEntry AVX1InLaneShuffleTbl[] = { | ||
{TTI::SK_PermuteSingleSrc, MVT::v4f64, 1}, // vpermilpd | ||
{TTI::SK_PermuteSingleSrc, MVT::v4i64, 1}, // vpermilpd | ||
{TTI::SK_PermuteSingleSrc, MVT::v8f32, 1}, // vpermilps | ||
{TTI::SK_PermuteSingleSrc, MVT::v8i32, 1}, // vpermilps | ||
|
||
{TTI::SK_PermuteSingleSrc, MVT::v16i16, 4}, // vextractf128 + 2*pshufb | ||
// + vpor + vinsertf128 | ||
{TTI::SK_PermuteSingleSrc, MVT::v16f16, 4}, // vextractf128 + 2*pshufb | ||
// + vpor + vinsertf128 | ||
{TTI::SK_PermuteSingleSrc, MVT::v32i8, 4}, // vextractf128 + 2*pshufb | ||
// + vpor + vinsertf128 | ||
|
||
{TTI::SK_PermuteTwoSrc, MVT::v4f64, 2}, // 2*vshufpd + vblendpd | ||
{TTI::SK_PermuteTwoSrc, MVT::v8f32, 2}, // 2*vshufps + vblendps | ||
{TTI::SK_PermuteTwoSrc, MVT::v4i64, 2}, // 2*vpermilpd + vblendpd | ||
{TTI::SK_PermuteTwoSrc, MVT::v8i32, 2}, // 2*vpermilps + vblendps | ||
{TTI::SK_PermuteTwoSrc, MVT::v16i16, 9}, // 2*vextractf128 + 4*pshufb | ||
// + 2*vpor + vinsertf128 | ||
{TTI::SK_PermuteTwoSrc, MVT::v16f16, 9}, // 2*vextractf128 + 4*pshufb | ||
// + 2*vpor + vinsertf128 | ||
{TTI::SK_PermuteTwoSrc, MVT::v32i8, 9}, // 2*vextractf128 + 4*pshufb | ||
// + 2*vpor + vinsertf128 | ||
}; | ||
|
||
if (IsInLaneShuffle && ST->hasAVX()) | ||
if (const auto *Entry = | ||
CostTableLookup(AVX1InLaneShuffleTbl, Kind, LT.second)) | ||
return LT.first * Entry->Cost; | ||
|
||
static const CostTblEntry AVX1ShuffleTbl[] = { | ||
{TTI::SK_Broadcast, MVT::v4f64, 2}, // vperm2f128 + vpermilpd | ||
{TTI::SK_Broadcast, MVT::v8f32, 2}, // vperm2f128 + vpermilps | ||
|
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it work for pointers?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, thats what
BaseTp->getPrimitiveSizeInBits() > 0
is checking for as ptr vectors return 0