Skip to content

Commit 851a121

Browse files
committed
[SLP]Fix the cost for the extractelements, used in several nodes.
Currently the compiler calculates the compensation cost for the extractelements, removed during vectorization. But if the extractelement instruction is used in several nodes, we can calculate the compensation for them several times. Differential Revision: https://reviews.llvm.org/D148806
1 parent d003c01 commit 851a121

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,8 @@ class BoUpSLP {
24332433

24342434
/// \returns the cost of the vectorizable entry.
24352435
InstructionCost getEntryCost(const TreeEntry *E,
2436-
ArrayRef<Value *> VectorizedVals);
2436+
ArrayRef<Value *> VectorizedVals,
2437+
SmallPtrSetImpl<Value *> &CheckedExtracts);
24372438

24382439
/// This is the recursive part of buildTree.
24392440
void buildTree_rec(ArrayRef<Value *> Roots, unsigned Depth,
@@ -6731,6 +6732,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
67316732
InstructionCost Cost = 0;
67326733
ArrayRef<Value *> VectorizedVals;
67336734
BoUpSLP &R;
6735+
SmallPtrSetImpl<Value *> &CheckedExtracts;
67346736
constexpr static TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
67356737

67366738
InstructionCost getBuildVectorCost(ArrayRef<Value *> VL, Value *Root) {
@@ -6923,8 +6925,10 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
69236925

69246926
public:
69256927
ShuffleCostEstimator(TargetTransformInfo &TTI,
6926-
ArrayRef<Value *> VectorizedVals, BoUpSLP &R)
6927-
: TTI(TTI), VectorizedVals(VectorizedVals), R(R) {}
6928+
ArrayRef<Value *> VectorizedVals, BoUpSLP &R,
6929+
SmallPtrSetImpl<Value *> &CheckedExtracts)
6930+
: TTI(TTI), VectorizedVals(VectorizedVals), R(R),
6931+
CheckedExtracts(CheckedExtracts) {}
69286932
Value *adjustExtracts(const TreeEntry *E, ArrayRef<int> Mask,
69296933
TTI::ShuffleKind ShuffleKind) {
69306934
if (Mask.empty())
@@ -6939,7 +6943,6 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
69396943
return nullptr;
69406944
}
69416945
DenseMap<Value *, int> ExtractVectorsTys;
6942-
SmallPtrSet<Value *, 4> CheckedExtracts;
69436946
for (auto [I, V] : enumerate(VL)) {
69446947
// Ignore non-extractelement scalars.
69456948
if (isa<UndefValue>(V) || (!Mask.empty() && Mask[I] == UndefMaskElem))
@@ -7070,8 +7073,9 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
70707073
}
70717074
};
70727075

7073-
InstructionCost BoUpSLP::getEntryCost(const TreeEntry *E,
7074-
ArrayRef<Value *> VectorizedVals) {
7076+
InstructionCost
7077+
BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
7078+
SmallPtrSetImpl<Value *> &CheckedExtracts) {
70757079
ArrayRef<Value *> VL = E->Scalars;
70767080

70777081
Type *ScalarTy = VL[0]->getType();
@@ -7098,7 +7102,8 @@ InstructionCost BoUpSLP::getEntryCost(const TreeEntry *E,
70987102
return 0;
70997103
if (isa<InsertElementInst>(VL[0]))
71007104
return InstructionCost::getInvalid();
7101-
ShuffleCostEstimator Estimator(*TTI, VectorizedVals, *this);
7105+
ShuffleCostEstimator Estimator(*TTI, VectorizedVals, *this,
7106+
CheckedExtracts);
71027107
unsigned VF = E->getVectorFactor();
71037108
SmallVector<int> ReuseShuffleIndicies(E->ReuseShuffleIndices.begin(),
71047109
E->ReuseShuffleIndices.end());
@@ -8224,6 +8229,7 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) {
82248229

82258230
unsigned BundleWidth = VectorizableTree[0]->Scalars.size();
82268231

8232+
SmallPtrSet<Value *, 4> CheckedExtracts;
82278233
for (unsigned I = 0, E = VectorizableTree.size(); I < E; ++I) {
82288234
TreeEntry &TE = *VectorizableTree[I];
82298235
if (TE.State == TreeEntry::NeedToGather) {
@@ -8239,7 +8245,7 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) {
82398245
}
82408246
}
82418247

8242-
InstructionCost C = getEntryCost(&TE, VectorizedVals);
8248+
InstructionCost C = getEntryCost(&TE, VectorizedVals, CheckedExtracts);
82438249
Cost += C;
82448250
LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C
82458251
<< " for bundle that starts with " << *TE.Scalars[0]

llvm/test/Transforms/SLPVectorizer/X86/reused-extractelements.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
; YAML-NEXT: Function: g
99
; YAML-NEXT: Args:
1010
; YAML-NEXT: - String: 'SLP vectorized with cost '
11-
; YAML-NEXT: - Cost: '-2'
11+
; YAML-NEXT: - Cost: '-1'
1212
; YAML-NEXT: - String: ' and with tree size '
1313
; YAML-NEXT: - TreeSize: '4'
1414

0 commit comments

Comments
 (0)