Skip to content

Commit c8c472a

Browse files
committed
Check that induction variable has no unsimplifiable users
Add AArch64 test
1 parent 6730685 commit c8c472a

File tree

4 files changed

+139
-102
lines changed

4 files changed

+139
-102
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,33 @@ static Value *getExpandedStep(const InductionDescriptor &ID,
26522652
return I->second;
26532653
}
26542654

2655+
/// Knowing that loop \p L would be fully unrolled after vectorisation, add
2656+
/// instructions that will get simplified and thus should not have any cost to
2657+
/// \p InstsToIgnore
2658+
static void AddFullyUnrolledInstructionsToIgnore(
2659+
Loop *L, const LoopVectorizationLegality::InductionList &IL,
2660+
SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
2661+
auto *Cmp = L->getLatchCmpInst();
2662+
if (!Cmp)
2663+
return;
2664+
InstsToIgnore.insert(Cmp);
2665+
for (const auto &[IV, IndDesc] : IL) {
2666+
// Get next iteration value of the induction variable
2667+
Instruction *IVInst =
2668+
cast<Instruction>(IV->getIncomingValueForBlock(L->getLoopLatch()));
2669+
bool IsSimplifiedAway = true;
2670+
// Check that this value used only to exit the loop
2671+
for (auto *UIV : IVInst->users()) {
2672+
if (UIV != IV && UIV != Cmp) {
2673+
IsSimplifiedAway = false;
2674+
break;
2675+
}
2676+
}
2677+
if (IsSimplifiedAway)
2678+
InstsToIgnore.insert(IVInst);
2679+
}
2680+
}
2681+
26552682
void InnerLoopVectorizer::createInductionResumeValues(
26562683
const SCEV2ValueTy &ExpandedSCEVs,
26572684
std::pair<BasicBlock *, Value *> AdditionalBypass) {
@@ -5557,19 +5584,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
55575584
InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) {
55585585
InstructionCost Cost;
55595586

5560-
// If with the given VF loop gets fully unrolled, ignore the costs of
5561-
// comparison and induction instructions, as they'll get simplified away
5562-
SmallPtrSet<const Value *, 16> ValuesToIgnoreForVF;
5587+
// If with the given fixed width VF loop gets fully unrolled, ignore the costs
5588+
// of comparison and induction instructions, as they'll get simplified away
5589+
SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF;
55635590
auto TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
5564-
auto *Cmp = TheLoop->getLatchCmpInst();
5565-
if (Cmp && TC == VF.getKnownMinValue()) {
5566-
ValuesToIgnoreForVF.insert(Cmp);
5567-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
5568-
Instruction *IVInc = cast<Instruction>(
5569-
IV->getIncomingValueForBlock(TheLoop->getLoopLatch()));
5570-
ValuesToIgnoreForVF.insert(IVInc);
5571-
}
5572-
}
5591+
if (VF.isFixed() && TC == VF.getFixedValue())
5592+
AddFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
5593+
ValuesToIgnoreForVF);
55735594

55745595
// For each block.
55755596
for (BasicBlock *BB : TheLoop->blocks()) {
@@ -7263,16 +7284,10 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
72637284

72647285
// If with the given VF loop gets fully unrolled, ignore the costs of
72657286
// comparison and induction instructions, as they'll get simplified away
7266-
auto TC = CM.PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7267-
auto *Cmp = OrigLoop->getLatchCmpInst();
7268-
if (Cmp && TC == VF.getKnownMinValue()) {
7269-
CostCtx.SkipCostComputation.insert(Cmp);
7270-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
7271-
Instruction *IVInc = cast<Instruction>(
7272-
IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
7273-
CostCtx.SkipCostComputation.insert(IVInc);
7274-
}
7275-
}
7287+
auto TC = PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7288+
if (VF.isFixed() && TC == VF.getFixedValue())
7289+
AddFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
7290+
CostCtx.SkipCostComputation);
72767291

72777292
for (Instruction *IVInst : IVInsts) {
72787293
if (CostCtx.skipCostComputation(IVInst, VF.isVector()))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
; REQUIRES: asserts
2+
; RUN: opt < %s -mcpu=neoverse-v2 -passes=loop-vectorize -debug-only=loop-vectorize -disable-output -S 2>&1 | FileCheck %s
3+
4+
target triple="aarch64--linux-gnu"
5+
6+
define i64 @test(ptr %a, ptr %b) #0 {
7+
; CHECK: LV: Checking a loop in 'test'
8+
; CHECK: LV: Found an estimated cost of 1 for VF 8 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
9+
; CHECK: LV: Found an estimated cost of 1 for VF 8 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 16
10+
; CHECK: LV: Vector loop of width 8 costs: 3.
11+
; CHECK-NOT: LV: Found an estimated cost of 1 for VF 16 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
12+
; CHECK-NOT: LV: Found an estimated cost of 1 for VF 16 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 16
13+
; CHECK: LV: Vector loop of width 16 costs: 3.
14+
; CHECK: LV: Selecting VF: 16
15+
entry:
16+
br label %for.body
17+
18+
for.cond.cleanup: ; preds = %for.body
19+
%add.lcssa = phi i64 [ %add, %for.body ]
20+
ret i64 %add.lcssa
21+
22+
for.body: ; preds = %entry, %for.body
23+
%indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
24+
%sum.09 = phi i64 [ 0, %entry ], [ %add, %for.body ]
25+
%arrayidx = getelementptr inbounds i8, ptr %a, i64 %indvars.iv
26+
%0 = load i8, ptr %arrayidx, align 1
27+
%conv = zext i8 %0 to i64
28+
%arrayidx2 = getelementptr inbounds i8, ptr %b, i64 %indvars.iv
29+
%1 = load i8, ptr %arrayidx2, align 1
30+
%conv3 = zext i8 %1 to i64
31+
%mul = mul nuw nsw i64 %conv3, %conv
32+
%add = add i64 %mul, %sum.09
33+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
34+
%exitcond.not = icmp eq i64 %indvars.iv.next, 16
35+
br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
36+
}
37+
38+
attributes #0 = { vscale_range(1, 16) "target-features"="+sve" }

0 commit comments

Comments
 (0)