Skip to content

Commit 3203100

Browse files
Rename getReciprocalPredBlockProb and move CostKind handling into it.
1 parent 6c21bf9 commit 3203100

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,7 +3345,7 @@ LoopVectorizationCostModel::getDivRemSpeculationCost(Instruction *I,
33453345
// Scale the cost by the probability of executing the predicated blocks.
33463346
// This assumes the predicated block for each vector lane is equally
33473347
// likely.
3348-
ScalarizationCost = ScalarizationCost / getReciprocalPredBlockProb();
3348+
ScalarizationCost = ScalarizationCost / getPredBlockCostDivisor(CostKind);
33493349
}
33503350
InstructionCost SafeDivisorCost = 0;
33513351

@@ -5515,8 +5515,7 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
55155515
}
55165516

55175517
// Scale the total scalar cost by block probability.
5518-
if (CostKind != TTI::TCK_CodeSize)
5519-
ScalarCost /= getReciprocalPredBlockProb();
5518+
ScalarCost /= getPredBlockCostDivisor(CostKind);
55205519

55215520
// Compute the discount. A non-negative discount means the vector version
55225521
// of the instruction costs more, and scalarizing would be beneficial.
@@ -5568,9 +5567,8 @@ InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) {
55685567
// the predicated block, if it is an if-else block. Thus, scale the block's
55695568
// cost by the probability of executing it. blockNeedsPredication from
55705569
// Legal is used so as to not include all blocks in tail folded loops.
5571-
if (VF.isScalar() && Legal->blockNeedsPredication(BB) &&
5572-
CostKind != TTI::TCK_CodeSize)
5573-
BlockCost /= getReciprocalPredBlockProb();
5570+
if (VF.isScalar() && Legal->blockNeedsPredication(BB))
5571+
BlockCost /= getPredBlockCostDivisor(CostKind);
55745572

55755573
Cost += BlockCost;
55765574
}
@@ -5648,8 +5646,7 @@ LoopVectorizationCostModel::getMemInstScalarizationCost(Instruction *I,
56485646
// conditional branches, but may not be executed for each vector lane. Scale
56495647
// the cost by the probability of executing the predicated block.
56505648
if (isPredicatedInst(I)) {
5651-
if (CostKind != TTI::TCK_CodeSize)
5652-
Cost /= getReciprocalPredBlockProb();
5649+
Cost /= getPredBlockCostDivisor(CostKind);
56535650

56545651
// Add the cost of an i1 extract and a branch
56555652
auto *VecI1Ty =

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,8 @@ InstructionCost VPRegionBlock::cost(ElementCount VF, VPCostContext &Ctx) {
793793

794794
// For the scalar case, we may not always execute the original predicated
795795
// block, Thus, scale the block's cost by the probability of executing it.
796-
if (VF.isScalar() && Ctx.CostKind != TTI::TCK_CodeSize)
797-
return ThenCost / getReciprocalPredBlockProb();
796+
if (VF.isScalar())
797+
return ThenCost / getPredBlockCostDivisor(Ctx.CostKind);
798798

799799
return ThenCost;
800800
}

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,20 @@ Value *getRuntimeVF(IRBuilderBase &B, Type *Ty, ElementCount VF);
8383
Value *createStepForVF(IRBuilderBase &B, Type *Ty, ElementCount VF,
8484
int64_t Step);
8585

86-
/// A helper function that returns the reciprocal of the block probability of
87-
/// predicated blocks. If we return X, we are assuming the predicated block
88-
/// will execute once for every X iterations of the loop header.
86+
/// A helper function that returns how much we should divide the cost of a
87+
/// predicated block by. Typically this is the reciprocal of the block
88+
/// probability, i.e. if we return X we are assuming the predicated block will
89+
/// execute once for every X iterations of the loop header so the block should
90+
/// only contribute 1/X of its cost to the total cost calculation, but when
91+
/// optimizing for code size it will just be 1 as code size costs don't depend
92+
/// on execution probabilities.
8993
///
9094
/// TODO: We should use actual block probability here, if available. Currently,
9195
/// we always assume predicated blocks have a 50% chance of executing.
92-
inline unsigned getReciprocalPredBlockProb() { return 2; }
96+
inline unsigned
97+
getPredBlockCostDivisor(TargetTransformInfo::TargetCostKind CostKind) {
98+
return CostKind == TTI::TCK_CodeSize ? 1 : 2;
99+
}
93100

94101
/// A range of powers-of-2 vectorization factors with fixed start and
95102
/// adjustable end. The range includes start and excludes end, e.g.,:

0 commit comments

Comments
 (0)