Skip to content

Commit 4e6c88b

Browse files
authored
[TTI] Remove Args argument from getOperandsScalarizationOverhead (NFC). (llvm#154126)
Remove the ArrayRef<const Value*> Args operand from getOperandsScalarizationOverhead and require that the callers de-duplicate arguments and filter constant operands. Removing the Value * based Args argument enables callers where no Value * operands are available to use the function in a follow-up: computing the scalarization cost directly for a VPlan recipe. It also allows more accurate cost-estimates in the future: for example, when vectorizing a loop, we could also skip operands that are live-ins, as those also do not require scalarization. PR: llvm#154126
1 parent 4875553 commit 4e6c88b

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -961,12 +961,10 @@ class TargetTransformInfo {
961961
TTI::TargetCostKind CostKind, bool ForPoisonSrc = true,
962962
ArrayRef<Value *> VL = {}) const;
963963

964-
/// Estimate the overhead of scalarizing an instructions unique
965-
/// non-constant operands. The (potentially vector) types to use for each of
966-
/// argument are passes via Tys.
964+
/// Estimate the overhead of scalarizing operands with the given types. The
965+
/// (potentially vector) types to use for each of argument are passes via Tys.
967966
LLVM_ABI InstructionCost getOperandsScalarizationOverhead(
968-
ArrayRef<const Value *> Args, ArrayRef<Type *> Tys,
969-
TTI::TargetCostKind CostKind) const;
967+
ArrayRef<Type *> Tys, TTI::TargetCostKind CostKind) const;
970968

971969
/// If target has efficient vector element load/store instructions, it can
972970
/// return true here so that insertion/extraction costs are not added to

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,7 @@ class TargetTransformInfoImplBase {
459459
}
460460

461461
virtual InstructionCost
462-
getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
463-
ArrayRef<Type *> Tys,
462+
getOperandsScalarizationOverhead(ArrayRef<Type *> Tys,
464463
TTI::TargetCostKind CostKind) const {
465464
return 0;
466465
}

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "llvm/ADT/APInt.h"
2020
#include "llvm/ADT/BitVector.h"
21+
#include "llvm/ADT/STLExtras.h"
2122
#include "llvm/ADT/SmallPtrSet.h"
2223
#include "llvm/ADT/SmallVector.h"
2324
#include "llvm/Analysis/LoopInfo.h"
@@ -347,6 +348,21 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
347348
return Cost;
348349
}
349350

351+
/// Filter out constant and duplicated entries in \p Ops and return a vector
352+
/// containing the types from \p Tys corresponding to the remaining operands.
353+
static SmallVector<Type *, 4>
354+
filterConstantAndDuplicatedOperands(ArrayRef<const Value *> Ops,
355+
ArrayRef<Type *> Tys) {
356+
SmallPtrSet<const Value *, 4> UniqueOperands;
357+
SmallVector<Type *, 4> FilteredTys;
358+
for (const auto &[Op, Ty] : zip_equal(Ops, Tys)) {
359+
if (isa<Constant>(Op) || !UniqueOperands.insert(Op).second)
360+
continue;
361+
FilteredTys.push_back(Ty);
362+
}
363+
return FilteredTys;
364+
}
365+
350366
protected:
351367
explicit BasicTTIImplBase(const TargetMachine *TM, const DataLayout &DL)
352368
: BaseT(DL) {}
@@ -935,29 +951,21 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
935951
CostKind);
936952
}
937953

938-
/// Estimate the overhead of scalarizing an instructions unique
939-
/// non-constant operands. The (potentially vector) types to use for each of
954+
/// Estimate the overhead of scalarizing an instruction's
955+
/// operands. The (potentially vector) types to use for each of
940956
/// argument are passes via Tys.
941957
InstructionCost getOperandsScalarizationOverhead(
942-
ArrayRef<const Value *> Args, ArrayRef<Type *> Tys,
943-
TTI::TargetCostKind CostKind) const override {
944-
assert(Args.size() == Tys.size() && "Expected matching Args and Tys");
945-
958+
ArrayRef<Type *> Tys, TTI::TargetCostKind CostKind) const override {
946959
InstructionCost Cost = 0;
947-
SmallPtrSet<const Value*, 4> UniqueOperands;
948-
for (int I = 0, E = Args.size(); I != E; I++) {
960+
for (Type *Ty : Tys) {
949961
// Disregard things like metadata arguments.
950-
const Value *A = Args[I];
951-
Type *Ty = Tys[I];
952962
if (!Ty->isIntOrIntVectorTy() && !Ty->isFPOrFPVectorTy() &&
953963
!Ty->isPtrOrPtrVectorTy())
954964
continue;
955965

956-
if (!isa<Constant>(A) && UniqueOperands.insert(A).second) {
957-
if (auto *VecTy = dyn_cast<VectorType>(Ty))
958-
Cost += getScalarizationOverhead(VecTy, /*Insert*/ false,
959-
/*Extract*/ true, CostKind);
960-
}
966+
if (auto *VecTy = dyn_cast<VectorType>(Ty))
967+
Cost += getScalarizationOverhead(VecTy, /*Insert*/ false,
968+
/*Extract*/ true, CostKind);
961969
}
962970

963971
return Cost;
@@ -974,7 +982,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
974982
InstructionCost Cost = getScalarizationOverhead(
975983
RetTy, /*Insert*/ true, /*Extract*/ false, CostKind);
976984
if (!Args.empty())
977-
Cost += getOperandsScalarizationOverhead(Args, Tys, CostKind);
985+
Cost += getOperandsScalarizationOverhead(
986+
filterConstantAndDuplicatedOperands(Args, Tys), CostKind);
978987
else
979988
// When no information on arguments is provided, we add the cost
980989
// associated with one argument as a heuristic.
@@ -2170,8 +2179,9 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
21702179
/*Insert=*/true, /*Extract=*/false, CostKind);
21712180
}
21722181
}
2173-
ScalarizationCost +=
2174-
getOperandsScalarizationOverhead(Args, ICA.getArgTypes(), CostKind);
2182+
ScalarizationCost += getOperandsScalarizationOverhead(
2183+
filterConstantAndDuplicatedOperands(Args, ICA.getArgTypes()),
2184+
CostKind);
21752185
}
21762186

21772187
IntrinsicCostAttributes Attrs(IID, RetTy, ICA.getArgTypes(), FMF, I,

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,8 @@ InstructionCost TargetTransformInfo::getScalarizationOverhead(
637637
}
638638

639639
InstructionCost TargetTransformInfo::getOperandsScalarizationOverhead(
640-
ArrayRef<const Value *> Args, ArrayRef<Type *> Tys,
641-
TTI::TargetCostKind CostKind) const {
642-
return TTIImpl->getOperandsScalarizationOverhead(Args, Tys, CostKind);
640+
ArrayRef<Type *> Tys, TTI::TargetCostKind CostKind) const {
641+
return TTIImpl->getOperandsScalarizationOverhead(Tys, CostKind);
643642
}
644643

645644
bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,8 +1697,16 @@ class LoopVectorizationCostModel {
16971697
/// Returns a range containing only operands needing to be extracted.
16981698
SmallVector<Value *, 4> filterExtractingOperands(Instruction::op_range Ops,
16991699
ElementCount VF) const {
1700-
return SmallVector<Value *, 4>(make_filter_range(
1701-
Ops, [this, VF](Value *V) { return this->needsExtract(V, VF); }));
1700+
1701+
SmallPtrSet<const Value *, 4> UniqueOperands;
1702+
SmallVector<Value *, 4> Res;
1703+
for (Value *Op : Ops) {
1704+
if (isa<Constant>(Op) || !UniqueOperands.insert(Op).second ||
1705+
!needsExtract(Op, VF))
1706+
continue;
1707+
Res.push_back(Op);
1708+
}
1709+
return Res;
17021710
}
17031711

17041712
public:
@@ -5610,8 +5618,7 @@ LoopVectorizationCostModel::getScalarizationOverhead(Instruction *I,
56105618
SmallVector<Type *> Tys;
56115619
for (auto *V : filterExtractingOperands(Ops, VF))
56125620
Tys.push_back(maybeVectorizeType(V->getType(), VF));
5613-
return Cost + TTI.getOperandsScalarizationOverhead(
5614-
filterExtractingOperands(Ops, VF), Tys, CostKind);
5621+
return Cost + TTI.getOperandsScalarizationOverhead(Tys, CostKind);
56155622
}
56165623

56175624
void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {

0 commit comments

Comments
 (0)