-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[VPlan][NFC] Add cost for VPWidenMemoryRecipe
.
#105614
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
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Elvis Wang (ElvisWang123) ChangesIn this patch, we add the Full diff: https://github.com/llvm/llvm-project/pull/105614.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index a99f3882092c2c..1f6de54822ebcf 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -2529,6 +2529,13 @@ class VPWidenMemoryRecipe : public VPRecipeBase {
llvm_unreachable("VPWidenMemoryRecipe should not be instantiated.");
}
+ /// Get element Type
+ Type *getElementType() const { return getLoadStoreType(&Ingredient); }
+
+ /// Return the cost of this VPWidenMemoryRecipe.
+ InstructionCost computeCost(ElementCount VF,
+ VPCostContext &Ctx) const override;
+
Instruction &getIngredient() const { return Ingredient; }
};
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index c9d603612aecea..d3b36b145d470e 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -2084,6 +2084,37 @@ void VPPredInstPHIRecipe::print(raw_ostream &O, const Twine &Indent,
}
#endif
+InstructionCost VPWidenMemoryRecipe::computeCost(ElementCount VF,
+ VPCostContext &Ctx) const {
+ Instruction *I = getInstructionForCost(this);
+ Type *Ty = ToVectorTy(getElementType(), VF);
+ const Align Alignment = getLoadStoreAlignment(const_cast<Instruction *>(I));
+ const Value *Ptr = getLoadStorePointerOperand(I);
+ unsigned AS = getLoadStoreAddressSpace(const_cast<Instruction *>(I));
+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
+
+ if (Consecutive) {
+ InstructionCost Cost = 0;
+ if (IsMasked) {
+ Cost += Ctx.TTI.getMaskedMemoryOpCost(I->getOpcode(), Ty, Alignment, AS,
+ CostKind);
+ } else {
+ TTI::OperandValueInfo OpInfo = Ctx.TTI.getOperandInfo(I->getOperand(0));
+ Cost += Ctx.TTI.getMemoryOpCost(I->getOpcode(), Ty, Alignment, AS,
+ CostKind, OpInfo, I);
+ }
+ if (Reverse)
+ Cost += Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse,
+ cast<VectorType>(Ty), std::nullopt,
+ CostKind, 0);
+
+ return Cost;
+ }
+ return Ctx.TTI.getAddressComputationCost(Ty) +
+ Ctx.TTI.getGatherScatterOpCost(I->getOpcode(), Ty, Ptr, IsMasked,
+ Alignment, CostKind, I);
+}
+
void VPWidenLoadRecipe::execute(VPTransformState &State) {
auto *LI = cast<LoadInst>(&Ingredient);
|
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 should be effectively NFC, might be good to tag as such
|
||
InstructionCost VPWidenMemoryRecipe::computeCost(ElementCount VF, | ||
VPCostContext &Ctx) const { | ||
Instruction *I = getInstructionForCost(this); |
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 should probably use Ingredient
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.
Instruction *I = getInstructionForCost(this); | ||
Type *Ty = ToVectorTy(getElementType(), VF); | ||
const Align Alignment = getLoadStoreAlignment(const_cast<Instruction *>(I)); | ||
const Value *Ptr = getLoadStorePointerOperand(I); |
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.
Sink closer to use? Is this required? Using the original IR pointer here won't be accurate I think. If needed, please leave a TODO behind to refactor reliance on underlying IR here and explain why it is currently needed.
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.
Only ArmTTI is using the underlying IR to return different gather/scatter cost currently.
|
||
return Cost; | ||
} | ||
return Ctx.TTI.getAddressComputationCost(Ty) + |
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.
maybe handle the simpler case (!Consecutive)
first with an early return?
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.
} | ||
|
||
/// Get element Type | ||
Type *getElementType() const { return getLoadStoreType(&Ingredient); } |
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.
Simpler to just use getLoadStoreType
directly in ::computeCost
, like other getLoadStoreXXX
helpers there?
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 directly use the getLoadStoreType
.
VPWidenMemoryRecipe
VPWidenMemoryRecipe
.
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.
With this change, you should also be able to remove VPWidenMemoryRecipe
from getInstructionForCost
in VPlan.cpp
d11c570
to
53972d5
Compare
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.
With the change it should also be possible to remove VPWidenMemoryRecipe
from getInstructionForCost
in VPlan.cpp
if (Reverse) | ||
Cost += | ||
Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse, | ||
cast<VectorType>(Ty), std::nullopt, CostKind, 0); | ||
|
||
return Cost; |
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.
nit: handling the non-reverse case with an early exit may be slightly more compact
if (Reverse) | |
Cost += | |
Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse, | |
cast<VectorType>(Ty), std::nullopt, CostKind, 0); | |
return Cost; | |
if (!Reverse) | |
return Cost; | |
return | |
Cost + | |
Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse, | |
cast<VectorType>(Ty), std::nullopt, CostKind, 0); |
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.
Updated, thanks!
Still missing? |
When removing
Should we just remove the check of |
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.
not sure if this is still NFC but we can skip the shuffle cost if it is a store and the store value is uniform
Remove |
yes that would be great, together with an explanation that VPWidenMemoryRecipe case in getInstructionForCost still being needed for force-target-instruction handling. |
Done, thanks! |
In this patch, we add the `computeCost()` function for `VPWidenMemoryRecipe`.
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.
LGTM,thanks!
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.
// the latecy model. | |
// the legacy model. |
?
8e7c55c
to
6204144
Compare
In this patch, we add the
computeCost()
function forVPWidenMemoryRecipe
.