Skip to content

[VectorCombine] foldPermuteOfBinops - fold "shuffle (binop (shuffle, other)), undef" --> "binop (shuffle), (shuffle)". #122118

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

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1592,17 +1592,21 @@ bool VectorCombine::foldPermuteOfBinops(Instruction &I) {
if (BinOp->isIntDivRem() && llvm::is_contained(OuterMask, PoisonMaskElem))
return false;

Value *Op00, *Op01;
ArrayRef<int> Mask0;
if (!match(BinOp->getOperand(0),
m_OneUse(m_Shuffle(m_Value(Op00), m_Value(Op01), m_Mask(Mask0)))))
Value *Op00, *Op01, *Op10, *Op11;
ArrayRef<int> Mask0, Mask1;
bool Match0 =
match(BinOp->getOperand(0),
m_OneUse(m_Shuffle(m_Value(Op00), m_Value(Op01), m_Mask(Mask0))));
bool Match1 =
match(BinOp->getOperand(1),
m_OneUse(m_Shuffle(m_Value(Op10), m_Value(Op11), m_Mask(Mask1))));
if (!Match0 && !Match1)
return false;

Value *Op10, *Op11;
ArrayRef<int> Mask1;
if (!match(BinOp->getOperand(1),
m_OneUse(m_Shuffle(m_Value(Op10), m_Value(Op11), m_Mask(Mask1)))))
return false;
Op00 = Match0 ? Op00 : BinOp->getOperand(0);
Op01 = Match0 ? Op01 : BinOp->getOperand(0);
Op10 = Match1 ? Op10 : BinOp->getOperand(1);
Op11 = Match1 ? Op11 : BinOp->getOperand(1);

Instruction::BinaryOps Opcode = BinOp->getOpcode();
auto *ShuffleDstTy = dyn_cast<FixedVectorType>(I.getType());
Expand All @@ -1620,37 +1624,46 @@ bool VectorCombine::foldPermuteOfBinops(Instruction &I) {
any_of(OuterMask, [NumSrcElts](int M) { return M >= (int)NumSrcElts; }))
return false;

// Merge outer / inner shuffles.
// Merge outer / inner (or identity if no match) shuffles.
SmallVector<int> NewMask0, NewMask1;
for (int M : OuterMask) {
if (M < 0 || M >= (int)NumSrcElts) {
NewMask0.push_back(PoisonMaskElem);
NewMask1.push_back(PoisonMaskElem);
} else {
NewMask0.push_back(Mask0[M]);
NewMask1.push_back(Mask1[M]);
NewMask0.push_back(Match0 ? Mask0[M] : M);
NewMask1.push_back(Match1 ? Mask1[M] : M);
}
}

unsigned NumOpElts = Op0Ty->getNumElements();
bool IsIdentity0 = ShuffleVectorInst::isIdentityMask(NewMask0, NumOpElts);
bool IsIdentity1 = ShuffleVectorInst::isIdentityMask(NewMask1, NumOpElts);

// Try to merge shuffles across the binop if the new shuffles are not costly.
InstructionCost OldCost =
TTI.getArithmeticInstrCost(Opcode, BinOpTy, CostKind) +
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, BinOpTy,
OuterMask, CostKind, 0, nullptr, {BinOp}, &I) +
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op0Ty, Mask0,
CostKind, 0, nullptr, {Op00, Op01},
cast<Instruction>(BinOp->getOperand(0))) +
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op1Ty, Mask1,
CostKind, 0, nullptr, {Op10, Op11},
cast<Instruction>(BinOp->getOperand(1)));
OuterMask, CostKind, 0, nullptr, {BinOp}, &I);
if (Match0)
OldCost += TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op0Ty,
Mask0, CostKind, 0, nullptr, {Op00, Op01},
cast<Instruction>(BinOp->getOperand(0)));
if (Match1)
OldCost += TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op1Ty,
Mask1, CostKind, 0, nullptr, {Op10, Op11},
cast<Instruction>(BinOp->getOperand(1)));
Comment on lines +1649 to +1655
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use getInstructionCost?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getInstructionCost is still causing some regressions for some shuffle costs - I haven't been able to get to the bottom of them all yet - but the plan is to eventually move as much of the VectorCombine OldCost calculations over to getInstructionCost as possible.


InstructionCost NewCost =
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op0Ty, NewMask0,
CostKind, 0, nullptr, {Op00, Op01}) +
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op1Ty, NewMask1,
CostKind, 0, nullptr, {Op10, Op11}) +
TTI.getArithmeticInstrCost(Opcode, ShuffleDstTy, CostKind);

if (!IsIdentity0)
NewCost += TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op0Ty,
NewMask0, CostKind, 0, nullptr, {Op00, Op01});
if (!IsIdentity1)
NewCost += TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, Op1Ty,
NewMask1, CostKind, 0, nullptr, {Op10, Op11});

LLVM_DEBUG(dbgs() << "Found a shuffle feeding a shuffled binop: " << I
<< "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
<< "\n");
Expand All @@ -1659,16 +1672,18 @@ bool VectorCombine::foldPermuteOfBinops(Instruction &I) {
if (NewCost > OldCost)
return false;

Value *Shuf0 = Builder.CreateShuffleVector(Op00, Op01, NewMask0);
Value *Shuf1 = Builder.CreateShuffleVector(Op10, Op11, NewMask1);
Value *NewBO = Builder.CreateBinOp(Opcode, Shuf0, Shuf1);
Value *LHS =
IsIdentity0 ? Op00 : Builder.CreateShuffleVector(Op00, Op01, NewMask0);
Value *RHS =
IsIdentity1 ? Op10 : Builder.CreateShuffleVector(Op10, Op11, NewMask1);
Value *NewBO = Builder.CreateBinOp(Opcode, LHS, RHS);

// Intersect flags from the old binops.
if (auto *NewInst = dyn_cast<Instruction>(NewBO))
NewInst->copyIRFlags(BinOp);

Worklist.pushValue(Shuf0);
Worklist.pushValue(Shuf1);
Worklist.pushValue(LHS);
Worklist.pushValue(RHS);
replaceValue(I, *NewBO);
return true;
}
Expand Down
Loading
Loading