-
Notifications
You must be signed in to change notification settings - Fork 15k
[VPlan] Track VPValues instead of VPRecipes in calculateRegisterUsage. #155301
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
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2422e7
[VPlan] Track VPValues instead of VPRecipes in calculateRegisterUsage.
fhahn 50e7d74
Merge remote-tracking branch 'origin/main' into vplan-calc-reg-usage-…
fhahn 9639ac9
!fixup use none_of, check if DefV is in Ends.
fhahn 47fe302
Merge remote-tracking branch 'origin/main' into vplan-calc-reg-usage-…
fhahn 0225bca
!fixup update comment
fhahn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| #include "VPlan.h" | ||
| #include "VPlanCFG.h" | ||
| #include "VPlanDominatorTree.h" | ||
| #include "VPlanHelpers.h" | ||
| #include "llvm/ADT/PostOrderIterator.h" | ||
| #include "llvm/ADT/TypeSwitch.h" | ||
| #include "llvm/Analysis/ScalarEvolution.h" | ||
|
|
@@ -396,7 +397,7 @@ bool VPDominatorTree::properlyDominates(const VPRecipeBase *A, | |
|
|
||
| /// Get the VF scaling factor applied to the recipe's output, if the recipe has | ||
| /// one. | ||
| static unsigned getVFScaleFactor(VPRecipeBase *R) { | ||
| static unsigned getVFScaleFactor(VPValue *R) { | ||
| if (auto *RR = dyn_cast<VPReductionPHIRecipe>(R)) | ||
| return RR->getVFScaleFactor(); | ||
| if (auto *RR = dyn_cast<VPPartialReductionRecipe>(R)) | ||
|
|
@@ -422,15 +423,15 @@ SmallVector<VPRegisterUsage, 8> llvm::calculateRegisterUsageForPlan( | |
| const SmallPtrSetImpl<const Value *> &ValuesToIgnore) { | ||
| // Each 'key' in the map opens a new interval. The values | ||
| // of the map are the index of the 'last seen' usage of the | ||
| // recipe that is the key. | ||
| using IntervalMap = SmallDenseMap<VPRecipeBase *, unsigned, 16>; | ||
| // VPValue that is the key. | ||
| using IntervalMap = SmallDenseMap<VPValue *, unsigned, 16>; | ||
|
|
||
| // Maps indices to recipes. | ||
| SmallVector<VPRecipeBase *, 64> Idx2Recipe; | ||
| // Marks the end of each interval. | ||
| IntervalMap EndPoint; | ||
| // Saves the list of recipe indices that are used in the loop. | ||
| SmallPtrSet<VPRecipeBase *, 8> Ends; | ||
| // Saves the list of VPValues that are used in the loop. | ||
| SmallPtrSet<VPValue *, 8> Ends; | ||
| // Saves the list of values that are used in the loop but are defined outside | ||
| // the loop (not including non-recipe values such as arguments and | ||
| // constants). | ||
|
|
@@ -441,7 +442,7 @@ SmallVector<VPRegisterUsage, 8> llvm::calculateRegisterUsageForPlan( | |
| // each recipe. We use RPO to ensure that defs are met before their users. We | ||
| // assume that each recipe that has in-loop users starts an interval. We | ||
| // record every time that an in-loop value is used, so we have a list of the | ||
| // first and last occurrences of each recipe. | ||
| // first occurences of each recipe and last occurrence of each VPValue. | ||
| VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion(); | ||
| ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT( | ||
| LoopRegion); | ||
|
|
@@ -470,32 +471,32 @@ SmallVector<VPRegisterUsage, 8> llvm::calculateRegisterUsageForPlan( | |
| } | ||
|
|
||
| // Overwrite previous end points. | ||
| EndPoint[DefR] = Idx2Recipe.size(); | ||
| Ends.insert(DefR); | ||
| EndPoint[U] = Idx2Recipe.size(); | ||
| Ends.insert(U); | ||
| } | ||
| } | ||
| if (VPBB == LoopRegion->getExiting()) { | ||
| // VPWidenIntOrFpInductionRecipes are used implicitly at the end of the | ||
| // exiting block, where their increment will get materialized eventually. | ||
| for (auto &R : LoopRegion->getEntryBasicBlock()->phis()) { | ||
| if (isa<VPWidenIntOrFpInductionRecipe>(&R)) { | ||
| EndPoint[&R] = Idx2Recipe.size(); | ||
| Ends.insert(&R); | ||
| if (auto *WideIV = dyn_cast<VPWidenIntOrFpInductionRecipe>(&R)) { | ||
| EndPoint[WideIV] = Idx2Recipe.size(); | ||
| Ends.insert(WideIV); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Saves the list of intervals that end with the index in 'key'. | ||
| using RecipeList = SmallVector<VPRecipeBase *, 2>; | ||
| SmallDenseMap<unsigned, RecipeList, 16> TransposeEnds; | ||
| using VPValueList = SmallVector<VPValue *, 2>; | ||
| SmallDenseMap<unsigned, VPValueList, 16> TransposeEnds; | ||
|
|
||
| // Next, we transpose the EndPoints into a multi map that holds the list of | ||
| // intervals that *end* at a specific location. | ||
| for (auto &Interval : EndPoint) | ||
| TransposeEnds[Interval.second].push_back(Interval.first); | ||
|
|
||
| SmallPtrSet<VPRecipeBase *, 8> OpenIntervals; | ||
| SmallPtrSet<VPValue *, 8> OpenIntervals; | ||
| SmallVector<VPRegisterUsage, 8> RUs(VFs.size()); | ||
| SmallVector<SmallMapVector<unsigned, unsigned, 4>, 8> MaxUsages(VFs.size()); | ||
|
|
||
|
|
@@ -519,14 +520,16 @@ SmallVector<VPRegisterUsage, 8> llvm::calculateRegisterUsageForPlan( | |
| for (unsigned int Idx = 0, Sz = Idx2Recipe.size(); Idx < Sz; ++Idx) { | ||
| VPRecipeBase *R = Idx2Recipe[Idx]; | ||
|
|
||
| // Remove all of the recipes that end at this location. | ||
| RecipeList &List = TransposeEnds[Idx]; | ||
| for (VPRecipeBase *ToRemove : List) | ||
| // Remove all of the VPValues that end at this location. | ||
| VPValueList &List = TransposeEnds[Idx]; | ||
| for (VPValue *ToRemove : List) | ||
| OpenIntervals.erase(ToRemove); | ||
|
|
||
| // Ignore recipes that are never used within the loop and do not have side | ||
| // effects. | ||
| if (!Ends.count(R) && !R->mayHaveSideEffects()) | ||
| if (all_of(R->definedValues(), | ||
| [&Ends](VPValue *Def) { return !Ends.count(Def); }) && | ||
lukel97 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| !R->mayHaveSideEffects()) | ||
| continue; | ||
|
|
||
| // Skip recipes for ignored values. | ||
|
|
@@ -546,41 +549,38 @@ SmallVector<VPRegisterUsage, 8> llvm::calculateRegisterUsageForPlan( | |
| // there is no previous entry for ClassID. | ||
| SmallMapVector<unsigned, unsigned, 4> RegUsage; | ||
|
|
||
| for (auto *R : OpenIntervals) { | ||
| // Skip recipes that weren't present in the original loop. | ||
| for (auto *VPV : OpenIntervals) { | ||
| // Skip values that weren't present in the original loop. | ||
| // TODO: Remove after removing the legacy | ||
| // LoopVectorizationCostModel::calculateRegisterUsage | ||
| if (isa<VPVectorPointerRecipe, VPVectorEndPointerRecipe, | ||
| VPBranchOnMaskRecipe>(R)) | ||
| VPBranchOnMaskRecipe>(VPV)) | ||
| continue; | ||
|
|
||
| if (VFs[J].isScalar() || | ||
| isa<VPCanonicalIVPHIRecipe, VPReplicateRecipe, VPDerivedIVRecipe, | ||
| VPEVLBasedIVPHIRecipe, VPScalarIVStepsRecipe>(R) || | ||
| (isa<VPInstruction>(R) && | ||
| vputils::onlyScalarValuesUsed(cast<VPSingleDefRecipe>(R))) || | ||
| (isa<VPReductionPHIRecipe>(R) && | ||
| (cast<VPReductionPHIRecipe>(R))->isInLoop())) { | ||
| unsigned ClassID = TTI.getRegisterClassForType( | ||
| false, TypeInfo.inferScalarType(R->getVPSingleValue())); | ||
| VPEVLBasedIVPHIRecipe, VPScalarIVStepsRecipe>(VPV) || | ||
| (isa<VPInstruction>(VPV) && vputils::onlyScalarValuesUsed(VPV)) || | ||
| (isa<VPReductionPHIRecipe>(VPV) && | ||
| (cast<VPReductionPHIRecipe>(VPV))->isInLoop())) { | ||
| unsigned ClassID = | ||
| TTI.getRegisterClassForType(false, TypeInfo.inferScalarType(VPV)); | ||
| // FIXME: The target might use more than one register for the type | ||
| // even in the scalar case. | ||
| RegUsage[ClassID] += 1; | ||
| } else { | ||
| // The output from scaled phis and scaled reductions actually has | ||
| // fewer lanes than the VF. | ||
| unsigned ScaleFactor = getVFScaleFactor(R); | ||
| unsigned ScaleFactor = getVFScaleFactor(VPV); | ||
| ElementCount VF = VFs[J].divideCoefficientBy(ScaleFactor); | ||
| LLVM_DEBUG(if (VF != VFs[J]) { | ||
| dbgs() << "LV(REG): Scaled down VF from " << VFs[J] << " to " << VF | ||
| << " for " << *R << "\n"; | ||
| }); | ||
|
|
||
| for (VPValue *DefV : R->definedValues()) { | ||
| Type *ScalarTy = TypeInfo.inferScalarType(DefV); | ||
| unsigned ClassID = TTI.getRegisterClassForType(true, ScalarTy); | ||
| RegUsage[ClassID] += GetRegUsage(ScalarTy, VF); | ||
| } | ||
| Type *ScalarTy = TypeInfo.inferScalarType(VPV); | ||
| unsigned ClassID = TTI.getRegisterClassForType(true, ScalarTy); | ||
| RegUsage[ClassID] += GetRegUsage(ScalarTy, VF); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -593,8 +593,10 @@ SmallVector<VPRegisterUsage, 8> llvm::calculateRegisterUsageForPlan( | |
| LLVM_DEBUG(dbgs() << "LV(REG): At #" << Idx << " Interval # " | ||
| << OpenIntervals.size() << '\n'); | ||
|
|
||
| // Add the current recipe to the list of open intervals. | ||
| OpenIntervals.insert(R); | ||
| // Add the VPValues defined by the current recipe to the list of open | ||
| // intervals. | ||
|
||
| for (VPValue *DefV : R->definedValues()) | ||
| OpenIntervals.insert(DefV); | ||
lukel97 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // We also search for instructions that are defined outside the loop, but are | ||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.