Skip to content

Commit e0b89f2

Browse files
committed
[AArch64][CostModel] Improve cost estimate of scalarizing a vector division
In the backend, last resort of finding the vector division cost is to use its scalar cost. However, without knowledge about the division operands, the cost can be off in certain cases. For SLP, this patch tries to pass scalars for better scalar cost estimation in the backend.
1 parent 4bcdb26 commit e0b89f2

File tree

3 files changed

+148
-145
lines changed

3 files changed

+148
-145
lines changed

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,6 +3572,20 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
35723572
Cost *= 4;
35733573
return Cost;
35743574
} else {
3575+
// If the information about individual scalars being vectorized is
3576+
// available, this yeilds better cost estimation.
3577+
if (auto *VTy = dyn_cast<FixedVectorType>(Ty); VTy && !Args.empty()) {
3578+
InstructionCost InsertExtractCost =
3579+
ST->getVectorInsertExtractBaseCost();
3580+
Cost = (3 * InsertExtractCost) * VTy->getNumElements();
3581+
for (int i = 0, Sz = Args.size(); i < Sz; i += 2) {
3582+
Cost += getArithmeticInstrCost(
3583+
Opcode, VTy->getScalarType(), CostKind,
3584+
TTI::getOperandInfo(Args[i]), TTI::getOperandInfo(Args[i + 1]));
3585+
}
3586+
return Cost;
3587+
}
3588+
35753589
// If one of the operands is a uniform constant then the cost for each
35763590
// element is Cost for insertion, extraction and division.
35773591
// Insertion cost = 2, Extraction Cost = 2, Division = cost for the

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11650,9 +11650,20 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
1165011650
unsigned OpIdx = isa<UnaryOperator>(VL0) ? 0 : 1;
1165111651
TTI::OperandValueInfo Op1Info = getOperandInfo(E->getOperand(0));
1165211652
TTI::OperandValueInfo Op2Info = getOperandInfo(E->getOperand(OpIdx));
11653-
return TTI->getArithmeticInstrCost(ShuffleOrOp, VecTy, CostKind, Op1Info,
11654-
Op2Info, {}, nullptr, TLI) +
11655-
CommonCost;
11653+
SmallVector<Value *, 16> Operands;
11654+
if (all_of(E->Scalars, [ShuffleOrOp](Value *V) {
11655+
return !IsaPred<UndefValue, PoisonValue>(V) &&
11656+
cast<Instruction>(V)->getOpcode() == ShuffleOrOp;
11657+
})) {
11658+
for (auto *Scalar : E->Scalars) {
11659+
Instruction *I = cast<Instruction>(Scalar);
11660+
auto IOperands = I->operand_values();
11661+
Operands.insert(Operands.end(), IOperands.begin(), IOperands.end());
11662+
}
11663+
}
11664+
return CommonCost +
11665+
TTI->getArithmeticInstrCost(ShuffleOrOp, VecTy, CostKind, Op1Info,
11666+
Op2Info, Operands, nullptr, TLI);
1165611667
};
1165711668
return GetCostDiff(GetScalarCost, GetVectorCost);
1165811669
}

0 commit comments

Comments
 (0)