-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[VPlan] Update getBestPlan to return VF, use also for epilogue vec. #98821
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 all commits
f9fc443
59101ce
69fd80e
7e369ba
28f2930
e7705a8
31731b3
52f032c
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -7162,13 +7162,12 @@ InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan, | |||||||
return Cost; | ||||||||
} | ||||||||
|
||||||||
VPlan &LoopVectorizationPlanner::getBestPlan() const { | ||||||||
ElementCount LoopVectorizationPlanner::getBestVF() const { | ||||||||
// If there is a single VPlan with a single VF, return it directly. | ||||||||
VPlan &FirstPlan = *VPlans[0]; | ||||||||
if (VPlans.size() == 1 && size(FirstPlan.vectorFactors()) == 1) | ||||||||
return FirstPlan; | ||||||||
return *FirstPlan.vectorFactors().begin(); | ||||||||
|
||||||||
VPlan *BestPlan = &FirstPlan; | ||||||||
ElementCount ScalarVF = ElementCount::getFixed(1); | ||||||||
assert(hasPlanWithVF(ScalarVF) && | ||||||||
"More than a single plan/VF w/o any plan having scalar VF"); | ||||||||
|
@@ -7199,14 +7198,11 @@ VPlan &LoopVectorizationPlanner::getBestPlan() const { | |||||||
|
||||||||
InstructionCost Cost = cost(*P, VF); | ||||||||
VectorizationFactor CurrentFactor(VF, Cost, ScalarCost); | ||||||||
if (isMoreProfitable(CurrentFactor, BestFactor)) { | ||||||||
if (isMoreProfitable(CurrentFactor, BestFactor)) | ||||||||
BestFactor = CurrentFactor; | ||||||||
BestPlan = &*P; | ||||||||
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. nit: can now also drop the {} 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. Done, thanks! |
||||||||
} | ||||||||
} | ||||||||
} | ||||||||
BestPlan->setVF(BestFactor.Width); | ||||||||
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. Agreed it is better to avoid restricting BestPlan's VF's to BestFactor.Width alone, thereby discarding all other associated VF's, one of which may be reuseful for subsequent epilog vectorization. This was the original intent of LVP's getBestPlan(). Cloning could be employed to retain the other VF's, but may be redundant - in case they are not needed. |
||||||||
return *BestPlan; | ||||||||
return BestFactor.Width; | ||||||||
} | ||||||||
|
||||||||
VPlan &LoopVectorizationPlanner::getBestPlanFor(ElementCount VF) const { | ||||||||
|
@@ -10001,10 +9997,11 @@ bool LoopVectorizePass::processLoop(Loop *L) { | |||||||
InnerLoopUnroller Unroller(L, PSE, LI, DT, TLI, TTI, AC, ORE, IC, &LVL, | ||||||||
&CM, BFI, PSI, Checks); | ||||||||
|
||||||||
VPlan &BestPlan = LVP.getBestPlan(); | ||||||||
assert(BestPlan.hasScalarVFOnly() && | ||||||||
ElementCount BestVF = LVP.getBestVF(); | ||||||||
assert(BestVF.isScalar() && | ||||||||
"VPlan cost model and legacy cost model disagreed"); | ||||||||
LVP.executePlan(VF.Width, IC, BestPlan, Unroller, DT, false); | ||||||||
VPlan &BestPlan = LVP.getBestPlanFor(BestVF); | ||||||||
LVP.executePlan(BestVF, IC, BestPlan, Unroller, DT, false); | ||||||||
|
||||||||
ORE->emit([&]() { | ||||||||
return OptimizationRemark(LV_NAME, "Interleaved", L->getStartLoc(), | ||||||||
|
@@ -10015,21 +10012,25 @@ bool LoopVectorizePass::processLoop(Loop *L) { | |||||||
} else { | ||||||||
// If we decided that it is *legal* to vectorize the loop, then do it. | ||||||||
|
||||||||
ElementCount BestVF = LVP.getBestVF(); | ||||||||
LLVM_DEBUG(dbgs() << "VF picked by VPlan cost model: " << BestVF << "\n"); | ||||||||
assert(VF.Width == BestVF && | ||||||||
"VPlan cost model and legacy cost model disagreed"); | ||||||||
VPlan &BestPlan = LVP.getBestPlanFor(BestVF); | ||||||||
// Consider vectorizing the epilogue too if it's profitable. | ||||||||
VectorizationFactor EpilogueVF = | ||||||||
LVP.selectEpilogueVectorizationFactor(VF.Width, IC); | ||||||||
LVP.selectEpilogueVectorizationFactor(BestVF, IC); | ||||||||
if (EpilogueVF.Width.isVector()) { | ||||||||
|
||||||||
// The first pass vectorizes the main loop and creates a scalar epilogue | ||||||||
// to be vectorized by executing the plan (potentially with a different | ||||||||
// factor) again shortly afterwards. | ||||||||
EpilogueLoopVectorizationInfo EPI(VF.Width, IC, EpilogueVF.Width, 1); | ||||||||
EpilogueLoopVectorizationInfo EPI(BestVF, IC, EpilogueVF.Width, 1); | ||||||||
EpilogueVectorizerMainLoop MainILV(L, PSE, LI, DT, TLI, TTI, AC, ORE, | ||||||||
EPI, &LVL, &CM, BFI, PSI, Checks); | ||||||||
|
||||||||
assert(EPI.MainLoopVF == VF.Width && "VFs must match"); | ||||||||
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
or dropped - given that this holds by construction of EPI? Maybe worth revisiting the use of 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. Yep it is not needed any longer, removed in 5a9b9ef
Yes, this should be untangled. |
||||||||
std::unique_ptr<VPlan> BestMainPlan( | ||||||||
LVP.getBestPlanFor(VF.Width).duplicate()); | ||||||||
std::unique_ptr<VPlan> BestMainPlan(BestPlan.duplicate()); | ||||||||
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. Ah, right, BestPlan is now used here. |
||||||||
const auto &[ExpandedSCEVs, ReductionResumeValues] = LVP.executePlan( | ||||||||
EPI.MainLoopVF, EPI.MainLoopUF, *BestMainPlan, MainILV, DT, true); | ||||||||
++LoopsVectorized; | ||||||||
|
@@ -10120,18 +10121,10 @@ bool LoopVectorizePass::processLoop(Loop *L) { | |||||||
if (!MainILV.areSafetyChecksAdded()) | ||||||||
DisableRuntimeUnroll = true; | ||||||||
} else { | ||||||||
VPlan &BestPlan = LVP.getBestPlan(); | ||||||||
assert(size(BestPlan.vectorFactors()) == 1 && | ||||||||
"Plan should have a single VF"); | ||||||||
ElementCount Width = *BestPlan.vectorFactors().begin(); | ||||||||
LLVM_DEBUG(dbgs() << "VF picked by VPlan cost model: " << Width | ||||||||
<< "\n"); | ||||||||
assert(VF.Width == Width && | ||||||||
"VPlan cost model and legacy cost model disagreed"); | ||||||||
InnerLoopVectorizer LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, Width, | ||||||||
InnerLoopVectorizer LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, BestVF, | ||||||||
VF.MinProfitableTripCount, IC, &LVL, &CM, BFI, | ||||||||
PSI, Checks); | ||||||||
LVP.executePlan(Width, IC, BestPlan, LB, DT, false); | ||||||||
LVP.executePlan(BestVF, IC, BestPlan, LB, DT, false); | ||||||||
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
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. Left at the original position as it is shared by both sides of the if |
||||||||
++LoopsVectorized; | ||||||||
|
||||||||
// Add metadata to disable runtime unrolling a scalar loop when 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.
would be more accurate, independent of this patch.
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.
Will do separately, thanks!