-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[VPlan] Implement VPInterleaveRecipe::computeCost. #106067
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
Implement computing costs for VPInterleaveRecipe.
@llvm/pr-subscribers-llvm-transforms Author: Florian Hahn (fhahn) ChangesImplement computing costs for VPInterleaveRecipe. Full diff: https://github.com/llvm/llvm-project/pull/106067.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 36a1aa08654d5b..2ab0a1826bd216 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -2165,6 +2165,10 @@ class VPInterleaveRecipe : public VPRecipeBase {
/// Generate the wide load or store, and shuffles.
void execute(VPTransformState &State) override;
+ /// Return the cost of this VPInterleaveRecipe.
+ InstructionCost computeCost(ElementCount VF,
+ VPCostContext &Ctx) const override;
+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
/// Print the recipe.
void print(raw_ostream &O, const Twine &Indent,
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index fe1325f4163004..b1fcc13dc34717 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -269,8 +269,6 @@ void VPRecipeBase::moveBefore(VPBasicBlock &BB,
static Instruction *getInstructionForCost(const VPRecipeBase *R) {
if (auto *S = dyn_cast<VPSingleDefRecipe>(R))
return dyn_cast_or_null<Instruction>(S->getUnderlyingValue());
- if (auto *IG = dyn_cast<VPInterleaveRecipe>(R))
- return IG->getInsertPos();
if (auto *WidenMem = dyn_cast<VPWidenMemoryRecipe>(R))
return &WidenMem->getIngredient();
return nullptr;
@@ -2627,6 +2625,37 @@ void VPInterleaveRecipe::execute(VPTransformState &State) {
}
}
+InstructionCost VPInterleaveRecipe::computeCost(ElementCount VF,
+ VPCostContext &Ctx) const {
+ Instruction *I = getInsertPos();
+ Type *ValTy = Ctx.Types.inferScalarType(
+ getNumDefinedValues() > 0 ? getVPValue(0) : getStoredValues()[0]);
+ auto *VectorTy = cast<VectorType>(ToVectorTy(ValTy, VF));
+ unsigned AS = getLoadStoreAddressSpace(I);
+ enum TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
+
+ unsigned InterleaveFactor = IG->getFactor();
+ auto *WideVecTy = VectorType::get(ValTy, VF * InterleaveFactor);
+
+ // Holds the indices of existing members in the interleaved group.
+ SmallVector<unsigned, 4> Indices;
+ for (unsigned IF = 0; IF < InterleaveFactor; IF++)
+ if (IG->getMember(IF))
+ Indices.push_back(IF);
+
+ // Calculate the cost of the whole interleaved group.
+ InstructionCost Cost = Ctx.TTI.getInterleavedMemoryOpCost(
+ I->getOpcode(), WideVecTy, IG->getFactor(), Indices, IG->getAlign(), AS,
+ CostKind, getMask(), NeedsMaskForGaps);
+
+ if (!IG->isReverse())
+ return Cost;
+
+ return Cost + IG->getNumMembers() *
+ Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse,
+ VectorTy, std::nullopt, CostKind, 0);
+}
+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void VPInterleaveRecipe::print(raw_ostream &O, const Twine &Indent,
VPSlotTracker &SlotTracker) const {
|
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.
Where will this be used, do we already have tests that hit this code?
if (!IG->isReverse()) | ||
return Cost; | ||
|
||
return Cost + IG->getNumMembers() * |
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 looks a bit hard-coded. It would be better to get the attribute from IG
and pass it to getShuffleCost
, but this can be done in a future PR.
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.
IIUC this extra computation could/should also be handled by getInterleavedMemoryOpCost
above?
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.
Where will this be used, do we already have tests that hit this code?
The VPlan-based cost model is used to pick the best vectorization factor already. For recipes that do not implement ::computeCost
, we currently fall back to the legacy cost model (via the instruction returned by getInstructionForCost
.
With this patch, we use VPInterleaveRecipe::computeCost when computing the cost for all plans when selecting the best VF/Plan (and remove the need to retrieve the underlying instruction via getInstructionForCost
)
if (!IG->isReverse()) | ||
return Cost; | ||
|
||
return Cost + IG->getNumMembers() * |
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.
IIUC this extra computation could/should also be handled by getInterleavedMemoryOpCost
above?
ping :) |
@@ -270,8 +270,6 @@ void VPRecipeBase::moveBefore(VPBasicBlock &BB, | |||
static Instruction *getInstructionForCost(const VPRecipeBase *R) { | |||
if (auto *S = dyn_cast<VPSingleDefRecipe>(R)) | |||
return dyn_cast_or_null<Instruction>(S->getUnderlyingValue()); | |||
if (auto *IG = dyn_cast<VPInterleaveRecipe>(R)) |
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.
Remove VPinterleaveRecipe
here will prevent -force-target-instr-cost
overwriting recipe cost that may be inconsistent to the legacy 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.
The code has been reworked in fa3258e and the changes here are gone.
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
Implement computing costs for VPInterleaveRecipe. PR: llvm#106067
Implement computing costs for VPInterleaveRecipe.