-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
[LV] Ignore some costs when loop gets fully unrolled #106699
Changes from all commits
d7a1335
22528ca
59f8c86
a4948b8
dac9e7e
4f6f702
e9763fa
d2f9ad9
7417e08
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 |
---|---|---|
|
@@ -2682,6 +2682,25 @@ static Value *getExpandedStep(const InductionDescriptor &ID, | |
return I->second; | ||
} | ||
|
||
/// Knowing that loop \p L executes a single vector iteration, add instructions | ||
/// that will get simplified and thus should not have any cost to \p | ||
/// InstsToIgnore. | ||
static void addFullyUnrolledInstructionsToIgnore( | ||
Loop *L, const LoopVectorizationLegality::InductionList &IL, | ||
SmallPtrSetImpl<Instruction *> &InstsToIgnore) { | ||
auto *Cmp = L->getLatchCmpInst(); | ||
if (Cmp) | ||
InstsToIgnore.insert(Cmp); | ||
for (const auto &[IV, IndDesc] : IL) { | ||
// Get next iteration value of the induction variable. | ||
Instruction *IVInst = | ||
cast<Instruction>(IV->getIncomingValueForBlock(L->getLoopLatch())); | ||
if (all_of(IVInst->users(), | ||
[&](const User *U) { return U == IV || U == Cmp; })) | ||
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. https://lab.llvm.org/buildbot/#/builders/78/builds/11135:
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 9099d69 thanks @kazutakahirata 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've fixed the warning with: 9099d69 |
||
InstsToIgnore.insert(IVInst); | ||
} | ||
} | ||
|
||
void InnerLoopVectorizer::createInductionResumeVPValues( | ||
const SCEV2ValueTy &ExpandedSCEVs, Value *MainVectorTripCount, | ||
SmallPtrSetImpl<PHINode *> *IVSubset) { | ||
|
@@ -5592,14 +5611,23 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount( | |
InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) { | ||
InstructionCost Cost; | ||
|
||
// If the vector loop gets executed exactly once with the given VF, ignore the | ||
// costs of comparison and induction instructions, as they'll get simplified | ||
// away. | ||
SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF; | ||
auto TC = PSE.getSE()->getSmallConstantTripCount(TheLoop); | ||
if (VF.isFixed() && TC == VF.getFixedValue() && !foldTailByMasking()) | ||
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. If TC == VF, no tail should remain, so we shouldn't try to fold the tail by masking. Could you make this an assert instead? 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. Of course you're right! There is code in computeMaxVF that disables tail-folding. In that case an assert does make sense. 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. @fhahn Well, if I make it as assert, these tests fail:
Without assert two RISCV tests show different output (that's why I added this check in the first place) 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 is because in 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. For example, suppose TC=8 and the max VF=16. In this case we could choose a VF of 16 and there will be a remainder, but the cost model may prefer VF=8 where there isn't a remainder. 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. Thanks for checking, that makes sense. |
||
addFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(), | ||
ValuesToIgnoreForVF); | ||
|
||
// For each block. | ||
for (BasicBlock *BB : TheLoop->blocks()) { | ||
InstructionCost BlockCost; | ||
|
||
// For each instruction in the old loop. | ||
for (Instruction &I : BB->instructionsWithoutDebug()) { | ||
// Skip ignored values. | ||
if (ValuesToIgnore.count(&I) || | ||
if (ValuesToIgnore.count(&I) || ValuesToIgnoreForVF.count(&I) || | ||
(VF.isVector() && VecValuesToIgnore.count(&I))) | ||
continue; | ||
|
||
|
@@ -7281,6 +7309,17 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF, | |
continue; | ||
IVInsts.push_back(CI); | ||
} | ||
|
||
// If the vector loop gets executed exactly once with the given VF, ignore | ||
// the costs of comparison and induction instructions, as they'll get | ||
// simplified away. | ||
// TODO: Remove this code after stepping away from the legacy cost model and | ||
// adding code to simplify VPlans before calculating their costs. | ||
auto TC = PSE.getSE()->getSmallConstantTripCount(OrigLoop); | ||
if (VF.isFixed() && TC == VF.getFixedValue() && !CM.foldTailByMasking()) | ||
addFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(), | ||
CostCtx.SkipCostComputation); | ||
|
||
for (Instruction *IVInst : IVInsts) { | ||
if (CostCtx.skipCostComputation(IVInst, VF.isVector())) | ||
continue; | ||
|
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.
Hmm, I just thought of something. What if there is an additional use of
Cmp
outside the loop? Should we also be checking that there is only a single use?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.
Since it is the only latch instruction, its value would be simplified to
false
when the control flow leaves the loop. So, we shouldn't worry about thatThere 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.
You may be right, but have you tried this out with real IR? There would be more than one use of the cmp so I wasn't sure if InstCombine would apply the fold in this case. In any case, adding an extra one use check wouldn't affect the loops you care about I think?
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.
Do you mean
getLatchCmpInst
ensures there is only a single user? Would be good to make sure we have a test (and maybe assert that there's a single user)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.
No, it doesn't check the number of users but checks that there's only one latch in the loop. The latch is something that makes a loop to be a loop. If there is a single latch and we don't jump to the loop header, then we are exiting the loop, and the value of the condition is constant.
See, it simplifies this condition outside away (pre LV):
https://godbolt.org/z/s9q1558zY
And if the compiler couldn't do it, we should've better taught him to do it rather than adding unnecessary checks
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.
I was actually thinking about something else @igogo-x86:
This is a case where in theory the final comparison is used after the loop. That's what I was hoping you could try out. Anyway, I ran this with the following command:
opt -mcpu=neoverse-v1 -p loop-vectorize,loop-unroll -force-vector-width=4 -force-vector-interleave=1 -S -debug-only=loop-vectorize < foo.ll
and it looks like we do indeed unroll the loop and the comparison disappears. Similary we unroll this and the comparison disappears:
In this case I'm happy that the comparison disappears, although I don't think an extra use check would have done any harm.
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 good to add a test case ,if possible