-
Notifications
You must be signed in to change notification settings - Fork 17.9k
[VPlan] Split out optimizeEVLMasks. NFC #174925
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
Changes from 5 commits
68eb4fc
abbb939
e5dbe66
5a2b815
acf669c
3a6438e
e550c42
d307310
3d5108c
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2979,8 +2979,45 @@ static VPRecipeBase *optimizeMaskToEVL(VPValue *HeaderMask, | |||||
| return nullptr; | ||||||
| } | ||||||
|
|
||||||
| /// Replace recipes with their EVL variants. | ||||||
| static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | ||||||
| /// Optimize away any EVL-based header masks to VP intrinsic based recipes. | ||||||
| /// The transforms here need to preserve the original semantics. | ||||||
| void VPlanTransforms::optimizeEVLMasks(VPlan &Plan) { | ||||||
| // Find the EVL-based header mask if it exists: icmp ult step-vector, EVL | ||||||
| VPValue *HeaderMask = nullptr, *EVL = nullptr; | ||||||
| for (VPRecipeBase &R : *Plan.getVectorLoopRegion()->getEntryBasicBlock()) { | ||||||
| if (match(&R, m_SpecificICmp(CmpInst::ICMP_ULT, m_StepVector(), | ||||||
| m_VPValue(EVL))) && | ||||||
| match(EVL, m_EVL(m_VPValue()))) { | ||||||
|
artagnon marked this conversation as resolved.
|
||||||
| HeaderMask = R.getVPSingleValue(); | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| if (!HeaderMask) | ||||||
| return; | ||||||
|
|
||||||
| VPTypeAnalysis TypeInfo(Plan); | ||||||
| SmallVector<VPRecipeBase *> OldRecipes; | ||||||
|
|
||||||
|
artagnon marked this conversation as resolved.
Outdated
|
||||||
| for (VPUser *U : collectUsersRecursively(HeaderMask)) { | ||||||
|
artagnon marked this conversation as resolved.
|
||||||
| VPRecipeBase *R = cast<VPRecipeBase>(U); | ||||||
| if (auto *NewR = optimizeMaskToEVL(HeaderMask, *R, TypeInfo, *EVL)) { | ||||||
| NewR->insertBefore(R); | ||||||
| for (auto [Old, New] : | ||||||
| zip_equal(R->definedValues(), NewR->definedValues())) | ||||||
| Old->replaceAllUsesWith(New); | ||||||
| OldRecipes.push_back(R); | ||||||
| } | ||||||
| } | ||||||
| // Erase recipes at the end so we don't invalidate TypeInfo. | ||||||
| for (VPRecipeBase *OldR : OldRecipes) | ||||||
| OldR->eraseFromParent(); | ||||||
| // Clean up any recipes now made dead. | ||||||
|
artagnon marked this conversation as resolved.
Outdated
|
||||||
| removeDeadRecipes(Plan); | ||||||
| } | ||||||
|
|
||||||
| /// After replacing the IV with a EVL-based IV, fixup recipes that use VF to use | ||||||
|
Contributor
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.
Suggested change
|
||||||
| /// the EVL instead to avoid incorrect updates on the penultimate iteration. | ||||||
| static void fixupVFUsersForEVL(VPlan &Plan, VPValue &EVL) { | ||||||
| VPTypeAnalysis TypeInfo(Plan); | ||||||
| VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion(); | ||||||
| VPBasicBlock *Header = LoopRegion->getEntryBasicBlock(); | ||||||
|
|
@@ -3008,10 +3045,6 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |||||
| return isa<VPWidenPointerInductionRecipe>(U); | ||||||
| }); | ||||||
|
|
||||||
| // Defer erasing recipes till the end so that we don't invalidate the | ||||||
| // VPTypeAnalysis cache. | ||||||
| SmallVector<VPRecipeBase *> ToErase; | ||||||
|
|
||||||
| // Create a scalar phi to track the previous EVL if fixed-order recurrence is | ||||||
| // contained. | ||||||
| bool ContainsFORs = | ||||||
|
|
@@ -3046,7 +3079,6 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |||||
| R.getDebugLoc()); | ||||||
| VPSplice->insertBefore(&R); | ||||||
| R.getVPSingleValue()->replaceAllUsesWith(VPSplice); | ||||||
| ToErase.push_back(&R); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -3067,43 +3099,6 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |||||
| CmpInst::ICMP_ULT, | ||||||
| Builder.createNaryOp(VPInstruction::StepVector, {}, EVLType), &EVL); | ||||||
| HeaderMask->replaceAllUsesWith(EVLMask); | ||||||
| ToErase.push_back(HeaderMask->getDefiningRecipe()); | ||||||
|
artagnon marked this conversation as resolved.
|
||||||
|
|
||||||
| // Try to optimize header mask recipes away to their EVL variants. | ||||||
| // TODO: Split optimizeMaskToEVL out and move into | ||||||
| // VPlanTransforms::optimize. transformRecipestoEVLRecipes should be run in | ||||||
| // tryToBuildVPlanWithVPRecipes beforehand. | ||||||
| for (VPUser *U : collectUsersRecursively(EVLMask)) { | ||||||
| auto *CurRecipe = cast<VPRecipeBase>(U); | ||||||
| VPRecipeBase *EVLRecipe = | ||||||
| optimizeMaskToEVL(EVLMask, *CurRecipe, TypeInfo, EVL); | ||||||
| if (!EVLRecipe) | ||||||
| continue; | ||||||
|
|
||||||
| unsigned NumDefVal = EVLRecipe->getNumDefinedValues(); | ||||||
| assert(NumDefVal == CurRecipe->getNumDefinedValues() && | ||||||
| "New recipe must define the same number of values as the " | ||||||
| "original."); | ||||||
| EVLRecipe->insertBefore(CurRecipe); | ||||||
| if (isa<VPSingleDefRecipe, VPWidenLoadEVLRecipe, VPInterleaveEVLRecipe>( | ||||||
| EVLRecipe)) { | ||||||
| for (unsigned I = 0; I < NumDefVal; ++I) { | ||||||
| VPValue *CurVPV = CurRecipe->getVPValue(I); | ||||||
| CurVPV->replaceAllUsesWith(EVLRecipe->getVPValue(I)); | ||||||
| } | ||||||
| } | ||||||
| ToErase.push_back(CurRecipe); | ||||||
| } | ||||||
| // Remove dead EVL mask. | ||||||
| if (EVLMask->getNumUsers() == 0) | ||||||
| ToErase.push_back(EVLMask->getDefiningRecipe()); | ||||||
|
|
||||||
| for (VPRecipeBase *R : reverse(ToErase)) { | ||||||
| SmallVector<VPValue *> PossiblyDead(R->operands()); | ||||||
| R->eraseFromParent(); | ||||||
| for (VPValue *Op : PossiblyDead) | ||||||
| recursivelyDeleteDeadRecipes(Op); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and | ||||||
|
|
@@ -3201,7 +3196,8 @@ void VPlanTransforms::addExplicitVectorLength( | |||||
| DebugLoc::getCompilerGenerated(), "avl.next"); | ||||||
| AVLPhi->addOperand(NextAVL); | ||||||
|
|
||||||
| transformRecipestoEVLRecipes(Plan, *VPEVL); | ||||||
| fixupVFUsersForEVL(Plan, *VPEVL); | ||||||
| removeDeadRecipes(Plan); | ||||||
|
|
||||||
| // Replace all uses of VPCanonicalIVPHIRecipe by | ||||||
| // VPEVLBasedIVPHIRecipe except for the canonical IV increment. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -266,6 +266,14 @@ struct VPlanTransforms { | |||||
| addExplicitVectorLength(VPlan &Plan, | ||||||
|
Contributor
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. migh be a good opporunity to clarfiy the documentation here, perhaps worth mentioning that this replaces the header mask.
it may also have to introduce a new phi to track the EVL of the previous iteration
Contributor
Author
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. Clarified the documentation in e550c42 |
||||||
| const std::optional<unsigned> &MaxEVLSafeElements); | ||||||
|
|
||||||
| /// Optimize recipes which use an EVL based header mask to a VP intrinsic: | ||||||
|
Contributor
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.
Suggested change
|
||||||
| /// | ||||||
| /// %mask = icmp ult step-vector, EVL | ||||||
| /// %load = load %ptr, %mask | ||||||
| /// --> | ||||||
| /// %load = vp.load %ptr, EVL | ||||||
| static void optimizeEVLMasks(VPlan &Plan); | ||||||
|
|
||||||
| // For each Interleave Group in \p InterleaveGroups replace the Recipes | ||||||
| // widening its memory instructions with a single VPInterleaveRecipe at its | ||||||
| // insertion point. | ||||||
|
|
||||||
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.
Can the code use m_SpecificICmp to look for icmp ult?
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.
Done in e5dbe66, but I had to add a specific VPRecipeBase overload for SpecificCmp_match so we can directly call match on VPRecipeBase. Regular Cmp_match does the same thing
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.
Grea,t hat useful in any cae