Skip to content

Commit 28b82ca

Browse files
committed
Address comments and add isInLoopReduction().
1 parent 4f2cc46 commit 28b82ca

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7137,6 +7137,14 @@ bool VPCostContext::skipCostComputation(Instruction *UI, bool IsVector) const {
71377137
SkipCostComputation.contains(UI);
71387138
}
71397139

7140+
bool VPCostContext::isInLoopReduction(const Instruction *UI, ElementCount VF,
7141+
Type *VectorTy) const {
7142+
return CM
7143+
.getReductionPatternCost(const_cast<Instruction *>(UI), VF, VectorTy,
7144+
TTI::TCK_RecipThroughput)
7145+
.has_value();
7146+
}
7147+
71407148
InstructionCost
71417149
LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
71427150
VPCostContext &CostCtx) const {

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,10 @@ struct VPCostContext {
718718
/// Return true if the cost for \p UI shouldn't be computed, e.g. because it
719719
/// has already been pre-computed.
720720
bool skipCostComputation(Instruction *UI, bool IsVector) const;
721+
722+
/// Return true if the \p UI is part of the in-loop reduction.
723+
bool isInLoopReduction(const Instruction *UI, ElementCount VF,
724+
Type *VectorTy) const;
721725
};
722726

723727
/// VPRecipeBase is a base class modeling a sequence of one or more output IR

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,18 +2074,24 @@ void VPReductionEVLRecipe::execute(VPTransformState &State) {
20742074
InstructionCost VPReductionRecipe::computeCost(ElementCount VF,
20752075
VPCostContext &Ctx) const {
20762076
RecurKind RdxKind = RdxDesc.getRecurrenceKind();
2077-
// TODO: Support any-of reduction and in-loop reduction
2078-
assert(!RecurrenceDescriptor::isAnyOfRecurrenceKind(RdxKind) &&
2079-
"Not support any-of reduction in VPlan-based cost model currently.");
2080-
2081-
Type *ElementTy = Ctx.Types.inferScalarType(this->getVPSingleValue());
2082-
assert(ElementTy->getTypeID() == RdxDesc.getRecurrenceType()->getTypeID() &&
2083-
"Infered type and recurrence type mismatch.");
2084-
2077+
Type *ElementTy = Ctx.Types.inferScalarType(this);
20852078
auto *VectorTy = cast<VectorType>(ToVectorTy(ElementTy, VF));
20862079
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
20872080
unsigned Opcode = RdxDesc.getOpcode();
20882081

2082+
// TODO: Support any-of reduction and in-loop reductions.
2083+
assert(
2084+
(!RecurrenceDescriptor::isAnyOfRecurrenceKind(RdxKind) ||
2085+
ForceTargetInstructionCost.getNumOccurrences() > 0) &&
2086+
"Any-of reduction not implemented in VPlan-based cost model currently.");
2087+
assert(
2088+
(!Ctx.isInLoopReduction(getUnderlyingInstr(), VF, VectorTy) ||
2089+
ForceTargetInstructionCost.getNumOccurrences() > 0) &&
2090+
"In-loop reduction not implemented in VPlan-based cost model currently.");
2091+
2092+
assert(ElementTy->getTypeID() == RdxDesc.getRecurrenceType()->getTypeID() &&
2093+
"Infered type and recurrence type mismatch.");
2094+
20892095
// Cost = Reduction cost + BinOp cost
20902096
InstructionCost Cost =
20912097
Ctx.TTI.getArithmeticInstrCost(Opcode, ElementTy, CostKind);

0 commit comments

Comments
 (0)