-
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 5 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 |
---|---|---|
|
@@ -7333,6 +7333,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)); | ||
|
@@ -10320,6 +10321,25 @@ 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); | ||
|
||
// 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. Can this be a VPlanTransform, perhaps part of optimizeForVFAndUF()? In any case worth outlining. Should requiresScalarEpilogue be known to VPlan, as a step towards modeling the epilog in VPlan along with the main loop? 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, moved to a separate transform; for now it must be separate and run here, as more work will be needed to make it work with epilogue vectorization.
We can check if middle.block unconditionally branches to scalar.ph. Updated, thanks |
||
if (BestPlan.hasScalarTail() && | ||
!CM.requiresScalarEpilogue(VF.Width.isVector())) { | ||
VPValue *TC = BestPlan.getTripCount(); | ||
if (TC->isLiveIn()) { | ||
ScalarEvolution &SE = *PSE.getSE(); | ||
auto *TCScev = SE.getSCEV(TC->getLiveInIRValue()); | ||
const SCEV *VFxUF = | ||
SE.getElementCount(TCScev->getType(), VF.Width * IC); | ||
auto VecTCScev = | ||
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. I'm a bit confused - even before this patch surely the vector trip count must have already been set to (OriginalTC / (VF * VF)) * (VF * UF), otherwise the vectorised code wouldn't even work? I just wonder if you can simply check that the existing vector trip count is already a constant. 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. Initially VectorTripCount is an opaque live-in VPValue and we can only compute it once we picked VF and UF. It is set just before executing the VPlan in With this patch, it now gets set slightly earlier if it is constant so VPlan transform can use it for optimizations. I updated |
||
SE.getMulExpr(SE.getUDivExpr(TCScev, VFxUF), VFxUF); | ||
if (auto *NewC = dyn_cast<SCEVConstant>(VecTCScev)) | ||
BestPlan.getVectorTripCount().setUnderlyingValue( | ||
NewC->getValue()); | ||
} | ||
} | ||
|
||
LVP.executePlan(VF.Width, IC, BestPlan, LB, DT, false); | ||
++LoopsVectorized; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1850,9 +1850,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()))) { | ||
|
@@ -1875,12 +1873,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); | ||
|
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?