-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[LV] Bundle sub reductions into VPExpressionRecipe #147255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: users/SamTebbs33/sub-reductions
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3116,7 +3116,10 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> { | |
|
||
InstructionCost | ||
getMulAccReductionCost(bool IsUnsigned, Type *ResTy, VectorType *Ty, | ||
bool Negated, | ||
TTI::TargetCostKind CostKind) const override { | ||
if (Negated) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't we add a cost for this? |
||
return InstructionCost::getInvalid(CostKind); | ||
// Without any native support, this is equivalent to the cost of | ||
// vecreduce.add(mul(ext(Ty A), ext(Ty B))) or | ||
// vecreduce.add(mul(A, B)). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5538,7 +5538,7 @@ LoopVectorizationCostModel::getReductionPatternCost(Instruction *I, | |
TTI::CastContextHint::None, CostKind, RedOp); | ||
|
||
InstructionCost RedCost = TTI.getMulAccReductionCost( | ||
IsUnsigned, RdxDesc.getRecurrenceType(), ExtType, CostKind); | ||
IsUnsigned, RdxDesc.getRecurrenceType(), ExtType, false, CostKind); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
|
||
if (RedCost.isValid() && | ||
RedCost < ExtCost * 2 + MulCost + Ext2Cost + BaseCost) | ||
|
@@ -5583,7 +5583,7 @@ LoopVectorizationCostModel::getReductionPatternCost(Instruction *I, | |
TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind); | ||
|
||
InstructionCost RedCost = TTI.getMulAccReductionCost( | ||
IsUnsigned, RdxDesc.getRecurrenceType(), ExtType, CostKind); | ||
IsUnsigned, RdxDesc.getRecurrenceType(), ExtType, false, CostKind); | ||
InstructionCost ExtraExtCost = 0; | ||
if (Op0Ty != LargestOpTy || Op1Ty != LargestOpTy) { | ||
Instruction *ExtraExtOp = (Op0Ty != LargestOpTy) ? Op0 : Op1; | ||
|
@@ -5602,7 +5602,7 @@ LoopVectorizationCostModel::getReductionPatternCost(Instruction *I, | |
TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind); | ||
|
||
InstructionCost RedCost = TTI.getMulAccReductionCost( | ||
true, RdxDesc.getRecurrenceType(), VectorTy, CostKind); | ||
true, RdxDesc.getRecurrenceType(), VectorTy, false, CostKind); | ||
|
||
if (RedCost.isValid() && RedCost < MulCost + BaseCost) | ||
return I == RetI ? RedCost : 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2757,6 +2757,12 @@ class VPExpressionRecipe : public VPSingleDefRecipe { | |
/// vector operands, performing a reduction.add on the result, and adding | ||
/// the scalar result to a chain. | ||
MulAccReduction, | ||
/// Represent an inloop multiply-accumulate reduction, multiplying the | ||
/// extended vector operands, negating the multiplication, performing a | ||
/// reduction.add | ||
/// on the result, and adding | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting of the comment looks a bit odd - can you fix it? |
||
/// the scalar result to a chain. | ||
ExtNegatedMulAccReduction, | ||
}; | ||
|
||
/// Type of the expression. | ||
|
@@ -2780,6 +2786,11 @@ class VPExpressionRecipe : public VPSingleDefRecipe { | |
VPWidenRecipe *Mul, VPReductionRecipe *Red) | ||
: VPExpressionRecipe(ExpressionTypes::ExtMulAccReduction, | ||
{Ext0, Ext1, Mul, Red}) {} | ||
VPExpressionRecipe(VPWidenCastRecipe *Ext0, VPWidenCastRecipe *Ext1, | ||
VPWidenRecipe *Mul, VPWidenRecipe *Sub, | ||
VPReductionRecipe *Red) | ||
: VPExpressionRecipe(ExpressionTypes::ExtNegatedMulAccReduction, | ||
{Ext0, Ext1, Mul, Sub, Red}) {} | ||
|
||
~VPExpressionRecipe() override { | ||
for (auto *R : reverse(ExpressionRecipes)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2672,13 +2672,17 @@ InstructionCost VPExpressionRecipe::computeCost(ElementCount VF, | |
RedTy, SrcVecTy, std::nullopt, Ctx.CostKind); | ||
} | ||
case ExpressionTypes::MulAccReduction: | ||
return Ctx.TTI.getMulAccReductionCost(false, RedTy, SrcVecTy, Ctx.CostKind); | ||
return Ctx.TTI.getMulAccReductionCost(false, RedTy, SrcVecTy, false, | ||
Ctx.CostKind); | ||
|
||
case ExpressionTypes::ExtMulAccReduction: | ||
case ExpressionTypes::ExtNegatedMulAccReduction: | ||
case ExpressionTypes::ExtMulAccReduction: { | ||
bool Negated = ExpressionType == ExpressionTypes::ExtNegatedMulAccReduction; | ||
return Ctx.TTI.getMulAccReductionCost( | ||
cast<VPWidenCastRecipe>(ExpressionRecipes.front())->getOpcode() == | ||
Instruction::ZExt, | ||
RedTy, SrcVecTy, Ctx.CostKind); | ||
RedTy, SrcVecTy, Negated, Ctx.CostKind); | ||
} | ||
} | ||
llvm_unreachable("Unknown VPExpressionRecipe::ExpressionTypes enum"); | ||
} | ||
|
@@ -2725,6 +2729,31 @@ void VPExpressionRecipe::print(raw_ostream &O, const Twine &Indent, | |
O << ")"; | ||
break; | ||
} | ||
case ExpressionTypes::ExtNegatedMulAccReduction: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to commonise this with the ExtMulAccReduction case if the only difference is a negate? |
||
getOperand(getNumOperands() - 1)->printAsOperand(O, SlotTracker); | ||
O << " + "; | ||
O << "reduce." | ||
<< Instruction::getOpcodeName( | ||
RecurrenceDescriptor::getOpcode(Red->getRecurrenceKind())) | ||
<< " (sub (0, mul"; | ||
auto *Mul = cast<VPWidenRecipe>(ExpressionRecipes[2]); | ||
Mul->printFlags(O); | ||
O << "("; | ||
getOperand(0)->printAsOperand(O, SlotTracker); | ||
auto *Ext0 = cast<VPWidenCastRecipe>(ExpressionRecipes[0]); | ||
O << " " << Instruction::getOpcodeName(Ext0->getOpcode()) << " to " | ||
<< *Ext0->getResultType() << "), ("; | ||
getOperand(1)->printAsOperand(O, SlotTracker); | ||
auto *Ext1 = cast<VPWidenCastRecipe>(ExpressionRecipes[1]); | ||
O << " " << Instruction::getOpcodeName(Ext1->getOpcode()) << " to " | ||
<< *Ext1->getResultType() << ")"; | ||
if (Red->isConditional()) { | ||
O << ", "; | ||
Red->getCondOp()->printAsOperand(O, SlotTracker); | ||
} | ||
O << "))"; | ||
break; | ||
} | ||
case ExpressionTypes::MulAccReduction: | ||
case ExpressionTypes::ExtMulAccReduction: { | ||
getOperand(getNumOperands() - 1)->printAsOperand(O, SlotTracker); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1401,8 +1401,8 @@ static void analyzeCostOfVecReduction(const IntrinsicInst &II, | |
TTI::CastContextHint::None, CostKind, RedOp); | ||
|
||
CostBeforeReduction = ExtCost * 2 + MulCost + Ext2Cost; | ||
CostAfterReduction = | ||
TTI.getMulAccReductionCost(IsUnsigned, II.getType(), ExtType, CostKind); | ||
CostAfterReduction = TTI.getMulAccReductionCost(IsUnsigned, II.getType(), | ||
ExtType, false, CostKind); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Probably better written as |
||
return; | ||
} | ||
CostAfterReduction = TTI.getArithmeticReductionCost(ReductionOpc, VecRedTy, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth keeping the booleans together, i.e. next to
IsUnsigned
?