-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[VectorCombine] foldShuffleOfShuffles - fold "shuffle (shuffle x, undef), (shuffle y, undef)" -> "shuffle x, y" #88743
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 |
---|---|---|
|
@@ -113,6 +113,7 @@ class VectorCombine { | |
bool scalarizeLoadExtract(Instruction &I); | ||
bool foldShuffleOfBinops(Instruction &I); | ||
bool foldShuffleOfCastops(Instruction &I); | ||
bool foldShuffleOfShuffles(Instruction &I); | ||
bool foldShuffleFromReductions(Instruction &I); | ||
bool foldTruncFromReductions(Instruction &I); | ||
bool foldSelectShuffle(Instruction &I, bool FromReduction = false); | ||
|
@@ -1552,6 +1553,86 @@ bool VectorCombine::foldShuffleOfCastops(Instruction &I) { | |
return true; | ||
} | ||
|
||
/// Try to convert "shuffle (shuffle x, undef), (shuffle y, undef)" | ||
/// into "shuffle x, y". | ||
bool VectorCombine::foldShuffleOfShuffles(Instruction &I) { | ||
Value *V0, *V1; | ||
UndefValue *U0, *U1; | ||
ArrayRef<int> OuterMask, InnerMask0, InnerMask1; | ||
if (!match(&I, m_Shuffle(m_OneUse(m_Shuffle(m_Value(V0), m_UndefValue(U0), | ||
m_Mask(InnerMask0))), | ||
m_OneUse(m_Shuffle(m_Value(V1), m_UndefValue(U1), | ||
m_Mask(InnerMask1))), | ||
m_Mask(OuterMask)))) | ||
return false; | ||
|
||
auto *ShuffleDstTy = dyn_cast<FixedVectorType>(I.getType()); | ||
auto *ShuffleSrcTy = dyn_cast<FixedVectorType>(V0->getType()); | ||
auto *ShuffleImmTy = dyn_cast<FixedVectorType>(I.getOperand(0)->getType()); | ||
if (!ShuffleDstTy || !ShuffleSrcTy || !ShuffleImmTy || | ||
V0->getType() != V1->getType()) | ||
return false; | ||
|
||
unsigned NumSrcElts = ShuffleSrcTy->getNumElements(); | ||
unsigned NumImmElts = ShuffleImmTy->getNumElements(); | ||
|
||
// Bail if either inner masks reference a RHS undef arg. | ||
if ((!isa<PoisonValue>(U0) && | ||
any_of(InnerMask0, [&](int M) { return M >= (int)NumSrcElts; })) || | ||
(!isa<PoisonValue>(U1) && | ||
any_of(InnerMask1, [&](int M) { return M >= (int)NumSrcElts; }))) | ||
return false; | ||
|
||
// Merge shuffles - replace index to the RHS poison arg with PoisonMaskElem, | ||
SmallVector<int, 16> NewMask(OuterMask.begin(), OuterMask.end()); | ||
for (int &M : NewMask) { | ||
if (0 <= M && M < (int)NumImmElts) { | ||
M = (InnerMask0[M] >= (int)NumSrcElts) ? PoisonMaskElem : InnerMask0[M]; | ||
} else if (M >= (int)NumImmElts) { | ||
if (InnerMask1[M - NumImmElts] >= (int)NumSrcElts) | ||
M = PoisonMaskElem; | ||
else | ||
M = InnerMask1[M - NumImmElts] + (V0 == V1 ? 0 : NumSrcElts); | ||
} | ||
} | ||
|
||
// Have we folded to an Identity shuffle? | ||
if (ShuffleVectorInst::isIdentityMask(NewMask, NumSrcElts)) { | ||
replaceValue(I, *V0); | ||
return true; | ||
} | ||
|
||
// Try to merge the shuffles if the new shuffle is not costly. | ||
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; | ||
|
||
InstructionCost OldCost = | ||
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, ShuffleSrcTy, | ||
InnerMask0, CostKind) + | ||
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, ShuffleSrcTy, | ||
InnerMask1, CostKind) + | ||
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, ShuffleImmTy, | ||
OuterMask, CostKind, 0, nullptr, std::nullopt, &I); | ||
|
||
InstructionCost NewCost = TTI.getShuffleCost( | ||
TargetTransformInfo::SK_PermuteTwoSrc, ShuffleSrcTy, NewMask, CostKind); | ||
|
||
LLVM_DEBUG(dbgs() << "Found a shuffle feeding two shuffles: " << I | ||
<< "\n OldCost: " << OldCost << " vs NewCost: " << NewCost | ||
<< "\n"); | ||
if (NewCost > OldCost) | ||
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. I feel this should be >=, unless there is a strong reason to do this more aggressively? 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. It comes down to the reduction in instruction count - if we fold 3 shuffle instructions into 1 (note we only fold if the inner shuffles having oneuse) for the same cost - isn't that better for further folds? |
||
return false; | ||
|
||
// Clear unused sources to poison. | ||
if (none_of(NewMask, [&](int M) { return 0 <= M && M < (int)NumSrcElts; })) | ||
V0 = PoisonValue::get(ShuffleSrcTy); | ||
if (none_of(NewMask, [&](int M) { return (int)NumSrcElts <= M; })) | ||
V1 = PoisonValue::get(ShuffleSrcTy); | ||
|
||
Value *Shuf = Builder.CreateShuffleVector(V0, V1, NewMask); | ||
replaceValue(I, *Shuf); | ||
return true; | ||
} | ||
|
||
/// Given a commutative reduction, the order of the input lanes does not alter | ||
/// the results. We can use this to remove certain shuffles feeding the | ||
/// reduction, removing the need to shuffle at all. | ||
|
@@ -2107,6 +2188,7 @@ bool VectorCombine::run() { | |
case Instruction::ShuffleVector: | ||
MadeChange |= foldShuffleOfBinops(I); | ||
MadeChange |= foldShuffleOfCastops(I); | ||
MadeChange |= foldShuffleOfShuffles(I); | ||
MadeChange |= foldSelectShuffle(I); | ||
break; | ||
case Instruction::BitCast: | ||
|
Uh oh!
There was an error while loading. Please reload this page.