-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[VPlan] Implement VPReductionRecipe::computeCost(). NFC #107790
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
Merged
ElvisWang123
merged 7 commits into
llvm:main
from
ElvisWang123:impl-vp-reduction-recipe-cost
Oct 15, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
324659a
[VPlan] Implment VPReductionRecipe::computeCost(). NFC
ElvisWang123 91fcf39
Address comments.
ElvisWang123 4f2cc46
Address comments.
ElvisWang123 28b82ca
Address comments and add `isInLoopReduction()`.
ElvisWang123 041f41b
Fixup! Remove legacy model query
ElvisWang123 7acd294
Fixup! Using cast to avoid warning when compiling without assertion.
ElvisWang123 80faae5
Fixup! typo
ElvisWang123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2071,6 +2071,40 @@ void VPReductionEVLRecipe::execute(VPTransformState &State) { | |
State.set(this, NewRed, /*IsScalar*/ true); | ||
} | ||
|
||
InstructionCost VPReductionRecipe::computeCost(ElementCount VF, | ||
VPCostContext &Ctx) const { | ||
RecurKind RdxKind = RdxDesc.getRecurrenceKind(); | ||
Type *ElementTy = Ctx.Types.inferScalarType(this); | ||
auto *VectorTy = cast<VectorType>(ToVectorTy(ElementTy, VF)); | ||
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. infer the type for the recipe instead and assert that the type matches 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. Sure, thanks. |
||
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; | ||
unsigned Opcode = RdxDesc.getOpcode(); | ||
|
||
// TODO: Support any-of and in-loop reductions. | ||
assert( | ||
(!RecurrenceDescriptor::isAnyOfRecurrenceKind(RdxKind) || | ||
ForceTargetInstructionCost.getNumOccurrences() > 0) && | ||
"Any-of reduction not implemented in VPlan-based cost model currently."); | ||
assert( | ||
(!cast<VPReductionPHIRecipe>(getOperand(0))->isInLoop() || | ||
ForceTargetInstructionCost.getNumOccurrences() > 0) && | ||
"In-loop reduction not implemented in VPlan-based cost model currently."); | ||
|
||
assert(ElementTy->getTypeID() == RdxDesc.getRecurrenceType()->getTypeID() && | ||
"Inferred type and recurrence type mismatch."); | ||
|
||
// Cost = Reduction cost + BinOp cost | ||
InstructionCost Cost = | ||
Ctx.TTI.getArithmeticInstrCost(Opcode, ElementTy, CostKind); | ||
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(RdxKind)) { | ||
Intrinsic::ID Id = getMinMaxReductionIntrinsicOp(RdxKind); | ||
return Cost + Ctx.TTI.getMinMaxReductionCost( | ||
Id, VectorTy, RdxDesc.getFastMathFlags(), CostKind); | ||
} | ||
|
||
return Cost + Ctx.TTI.getArithmeticReductionCost( | ||
Opcode, VectorTy, RdxDesc.getFastMathFlags(), CostKind); | ||
} | ||
|
||
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | ||
void VPReductionRecipe::print(raw_ostream &O, const Twine &Indent, | ||
VPSlotTracker &SlotTracker) const { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This computes the cost for non-in loop and non-any-of reductions, correct? Would be good to add an assert an explanation why (for those the cost needs to be pre-computed at the moment)
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.
Thanks, I will add an assertion to check it is not an any-of reduction.
I tried to compute the cost of in-loop reductions by the vplan-based cost model.
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.
It would probably be better to add support for in-loop reductions separately, as it adds extra complexity and may introduce new divergences
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.
Sure, I will open another PR to address in-loop reduction.