Skip to content

[SLP]Add GEP cost estimation for gathered loads. #80867

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
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
38 changes: 36 additions & 2 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7089,6 +7089,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
}))) &&
!all_of(Gathers, [&](Value *V) { return R.getTreeEntry(V); }) &&
!isSplat(Gathers)) {
InstructionCost BaseCost = R.getGatherCost(Gathers, !Root);
SetVector<Value *> VectorizedLoads;
SmallVector<unsigned> VectorizedStarts;
SmallVector<unsigned> ScatterVectorized;
Expand Down Expand Up @@ -7170,14 +7171,46 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
TTI.getMemoryOpCost(Instruction::Load, LoadTy, Alignment,
LI->getPointerAddressSpace(), CostKind,
TTI::OperandValueInfo(), LI);
// Estimate GEP cost.
SmallVector<Value *> PointerOps(VF);
for (auto [I, V] : enumerate(VL.slice(P, VF)))
PointerOps[I] = cast<LoadInst>(V)->getPointerOperand();
auto [ScalarGEPCost, VectorGEPCost] =
getGEPCosts(TTI, PointerOps, LI->getPointerOperand(),
Instruction::Load, CostKind, LI->getType(), LoadTy);
GatherCost += VectorGEPCost - ScalarGEPCost;
}
for (unsigned P : ScatterVectorized) {
auto *LI0 = cast<LoadInst>(VL[P]);
Align CommonAlignment =
computeCommonAlignment<LoadInst>(VL.slice(P, VF));
ArrayRef<Value *> Slice = VL.slice(P, VF);
Align CommonAlignment = computeCommonAlignment<LoadInst>(Slice);
GatherCost += TTI.getGatherScatterOpCost(
Instruction::Load, LoadTy, LI0->getPointerOperand(),
/*VariableMask=*/false, CommonAlignment, CostKind, LI0);
// Estimate GEP cost.
SmallVector<Value *> PointerOps(VF);
for (auto [I, V] : enumerate(Slice))
PointerOps[I] = cast<LoadInst>(V)->getPointerOperand();
OrdersType Order;
if (sortPtrAccesses(PointerOps, LI0->getType(), *R.DL, *R.SE,
Order)) {
// TODO: improve checks if GEPs can be vectorized.
Value *Ptr0 = PointerOps.front();
Type *ScalarTy = Ptr0->getType();
auto *VecTy = FixedVectorType::get(ScalarTy, VF);
auto [ScalarGEPCost, VectorGEPCost] =
getGEPCosts(TTI, PointerOps, Ptr0, Instruction::GetElementPtr,
CostKind, ScalarTy, VecTy);
GatherCost += VectorGEPCost - ScalarGEPCost;
if (!Order.empty()) {
SmallVector<int> Mask;
inversePermutation(Order, Mask);
GatherCost += ::getShuffleCost(TTI, TTI::SK_PermuteSingleSrc,
VecTy, Mask, CostKind);
}
} else {
GatherCost += R.getGatherCost(PointerOps, /*ForPoisonSrc=*/true);
}
}
if (NeedInsertSubvectorAnalysis) {
// Add the cost for the subvectors insert.
Expand All @@ -7187,6 +7220,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
}
GatherCost -= ScalarsCost;
}
GatherCost = std::min(BaseCost, GatherCost);
} else if (!Root && isSplat(VL)) {
// Found the broadcasting of the single scalar, calculate the cost as
// the broadcast.
Expand Down
Loading