-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[VPlan] Materialize constant vector trip counts before final opts. #142309
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
base: main
Are you sure you want to change the base?
Changes from all commits
8ed8191
4122565
68a47f7
c5e6bee
66c8d73
97043ee
e8d1dce
6cd2715
8d001b2
c49b0d6
30a4fe2
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 |
---|---|---|
|
@@ -7329,6 +7329,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan( | |
BestVPlan, BestVF); | ||
VPlanTransforms::optimizeForVFAndUF(BestVPlan, BestVF, BestUF, PSE); | ||
VPlanTransforms::simplifyRecipes(BestVPlan, *Legal->getWidestInductionType()); | ||
VPlanTransforms::removeBranchOnConst(BestVPlan); | ||
VPlanTransforms::narrowInterleaveGroups( | ||
BestVPlan, BestVF, | ||
TTI.getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector)); | ||
|
@@ -10261,6 +10262,11 @@ bool LoopVectorizePass::processLoop(Loop *L) { | |
L, PSE, LI, DT, TLI, TTI, AC, ORE, ElementCount::getFixed(1), | ||
ElementCount::getFixed(1), IC, &CM, BFI, PSI, Checks, BestPlan); | ||
|
||
// TODO: Move to general VPlan pipeline once epilogue loops are also | ||
// supported. | ||
VPlanTransforms::runPass(VPlanTransforms::materializeVectorTripCount, | ||
BestPlan, VF.Width, IC, PSE); | ||
|
||
Comment on lines
+10265
to
+10269
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. This disables materializeVectorTripCount() from running on both main and epilog loops, when both are vectorized, as opposed to running it in LVP.executePlan() under |
||
LVP.executePlan(VF.Width, IC, BestPlan, Unroller, DT, false); | ||
|
||
ORE->emit([&]() { | ||
|
@@ -10328,6 +10334,11 @@ bool LoopVectorizePass::processLoop(Loop *L) { | |
InnerLoopVectorizer LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width, | ||
VF.MinProfitableTripCount, IC, &CM, BFI, PSI, | ||
Checks, BestPlan); | ||
// TODO: Move to general VPlan pipeline once epilogue loops are also | ||
// supported. | ||
VPlanTransforms::runPass(VPlanTransforms::materializeVectorTripCount, | ||
BestPlan, VF.Width, IC, PSE); | ||
|
||
LVP.executePlan(VF.Width, IC, BestPlan, LB, DT, false); | ||
++LoopsVectorized; | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1875,9 +1875,7 @@ void VPlanTransforms::truncateToMinimalBitwidths( | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/// Remove BranchOnCond recipes with true or false conditions together with | ||||||||||
/// removing dead edges to their successors. | ||||||||||
static void removeBranchOnConst(VPlan &Plan) { | ||||||||||
void VPlanTransforms::removeBranchOnConst(VPlan &Plan) { | ||||||||||
using namespace llvm::VPlanPatternMatch; | ||||||||||
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>( | ||||||||||
vp_depth_first_shallow(Plan.getEntry()))) { | ||||||||||
|
@@ -1900,12 +1898,9 @@ static void removeBranchOnConst(VPlan &Plan) { | |||||||||
"There must be a single edge between VPBB and its successor"); | ||||||||||
// Values coming from VPBB into phi recipes of RemoveSucc are removed from | ||||||||||
// these recipes. | ||||||||||
for (VPRecipeBase &R : RemovedSucc->phis()) { | ||||||||||
auto *Phi = cast<VPPhiAccessors>(&R); | ||||||||||
assert((!isa<VPIRPhi>(&R) || RemovedSucc->getNumPredecessors() == 1) && | ||||||||||
"VPIRPhis must have a single predecessor"); | ||||||||||
Comment on lines
-1905
to
-1906
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. Now VPIRPhi's of middle-block may be handled here as well, having multiple predecessors, right? |
||||||||||
Phi->removeIncomingValueFor(VPBB); | ||||||||||
} | ||||||||||
for (VPRecipeBase &R : RemovedSucc->phis()) | ||||||||||
cast<VPPhiAccessors>(&R)->removeIncomingValueFor(VPBB); | ||||||||||
|
||||||||||
// Disconnect blocks and remove the terminator. RemovedSucc will be deleted | ||||||||||
// automatically on VPlan destruction if it becomes unreachable. | ||||||||||
VPBlockUtils::disconnectBlocks(VPBB, RemovedSucc); | ||||||||||
|
@@ -3120,6 +3115,29 @@ void VPlanTransforms::materializeBroadcasts(VPlan &Plan) { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
void VPlanTransforms::materializeVectorTripCount( | ||||||||||
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
? |
||||||||||
VPlan &Plan, ElementCount BestVF, unsigned BestUF, | ||||||||||
PredicatedScalarEvolution &PSE) { | ||||||||||
assert(Plan.hasVF(BestVF) && "BestVF is not available in Plan"); | ||||||||||
assert(Plan.hasUF(BestUF) && "BestUF is not available in Plan"); | ||||||||||
|
||||||||||
VPValue *TC = Plan.getTripCount(); | ||||||||||
// Skip cases for which the trip count may be non-trivial to materialize. | ||||||||||
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
An absent scalar tail due to knowing that VecTC is a multiple of VF*UF, requires materializing VecTC, so can be ignored here? |
||||||||||
if (!Plan.hasScalarTail() || | ||||||||||
Plan.getMiddleBlock()->getSingleSuccessor() == | ||||||||||
Plan.getScalarPreheader() || | ||||||||||
!TC->isLiveIn()) | ||||||||||
return; | ||||||||||
// Materialize vector trip counts for constants early if it can simply | ||||||||||
// be computed as (Original TC / VF * UF) * VF * UF. | ||||||||||
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. Worth adding a TODO to materialize constant VecTC also when tail is folded and when scalar epilog is required? Computing it then is quite simple too? |
||||||||||
ScalarEvolution &SE = *PSE.getSE(); | ||||||||||
auto *TCScev = SE.getSCEV(TC->getLiveInIRValue()); | ||||||||||
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. Worth bailing out early if TCScev is not a SCEVConstant? |
||||||||||
const SCEV *VFxUF = SE.getElementCount(TCScev->getType(), BestVF * BestUF); | ||||||||||
auto VecTCScev = SE.getMulExpr(SE.getUDivExpr(TCScev, VFxUF), VFxUF); | ||||||||||
if (auto *NewC = dyn_cast<SCEVConstant>(VecTCScev)) | ||||||||||
Plan.getVectorTripCount().setUnderlyingValue(NewC->getValue()); | ||||||||||
Comment on lines
+3137
to
+3138
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
|
||||||||||
} | ||||||||||
|
||||||||||
/// Returns true if \p V is VPWidenLoadRecipe or VPInterleaveRecipe that can be | ||||||||||
/// converted to a narrower recipe. \p V is used by a wide recipe \p WideMember | ||||||||||
/// that feeds a store interleave group at index \p Idx, \p WideMember0 is the | ||||||||||
|
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 is in addition to running it as part of optimize(), rather than instead of?