Skip to content

Commit a9b9cad

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

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
@@ -2663,6 +2663,33 @@ static Value *getExpandedStep(const InductionDescriptor &ID,
26632663
return I->second;
26642664
}
26652665

2666+
/// Knowing that loop \p L would be fully unrolled after vectorisation, add
2667+
/// instructions that will get simplified and thus should not have any cost to
2668+
/// \p InstsToIgnore
2669+
static void AddFullyUnrolledInstructionsToIgnore(
2670+
Loop *L, const LoopVectorizationLegality::InductionList &IL,
2671+
SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
2672+
auto *Cmp = L->getLatchCmpInst();
2673+
if (!Cmp)
2674+
return;
2675+
InstsToIgnore.insert(Cmp);
2676+
for (const auto &[IV, IndDesc] : IL) {
2677+
// Get next iteration value of the induction variable
2678+
Instruction *IVInst =
2679+
cast<Instruction>(IV->getIncomingValueForBlock(L->getLoopLatch()));
2680+
bool IsSimplifiedAway = true;
2681+
// Check that this value used only to exit the loop
2682+
for (auto *UIV : IVInst->users()) {
2683+
if (UIV != IV && UIV != Cmp) {
2684+
IsSimplifiedAway = false;
2685+
break;
2686+
}
2687+
}
2688+
if (IsSimplifiedAway)
2689+
InstsToIgnore.insert(IVInst);
2690+
}
2691+
}
2692+
26662693
void InnerLoopVectorizer::createInductionResumeValues(
26672694
const SCEV2ValueTy &ExpandedSCEVs,
26682695
std::pair<BasicBlock *, Value *> AdditionalBypass) {
@@ -5545,19 +5572,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
55455572
InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) {
55465573
InstructionCost Cost;
55475574

5548-
// If with the given VF loop gets fully unrolled, ignore the costs of
5549-
// comparison and induction instructions, as they'll get simplified away
5550-
SmallPtrSet<const Value *, 16> ValuesToIgnoreForVF;
5575+
// If with the given fixed width VF loop gets fully unrolled, ignore the costs
5576+
// of comparison and induction instructions, as they'll get simplified away
5577+
SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF;
55515578
auto TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
5552-
auto *Cmp = TheLoop->getLatchCmpInst();
5553-
if (Cmp && TC == VF.getKnownMinValue()) {
5554-
ValuesToIgnoreForVF.insert(Cmp);
5555-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
5556-
Instruction *IVInc = cast<Instruction>(
5557-
IV->getIncomingValueForBlock(TheLoop->getLoopLatch()));
5558-
ValuesToIgnoreForVF.insert(IVInc);
5559-
}
5560-
}
5579+
if (VF.isFixed() && TC == VF.getFixedValue())
5580+
AddFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
5581+
ValuesToIgnoreForVF);
55615582

55625583
// For each block.
55635584
for (BasicBlock *BB : TheLoop->blocks()) {
@@ -7241,16 +7262,10 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
72417262

72427263
// If with the given VF loop gets fully unrolled, ignore the costs of
72437264
// comparison and induction instructions, as they'll get simplified away
7244-
auto TC = CM.PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7245-
auto *Cmp = OrigLoop->getLatchCmpInst();
7246-
if (Cmp && TC == VF.getKnownMinValue()) {
7247-
CostCtx.SkipCostComputation.insert(Cmp);
7248-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
7249-
Instruction *IVInc = cast<Instruction>(
7250-
IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
7251-
CostCtx.SkipCostComputation.insert(IVInc);
7252-
}
7253-
}
7265+
auto TC = PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7266+
if (VF.isFixed() && TC == VF.getFixedValue())
7267+
AddFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
7268+
CostCtx.SkipCostComputation);
72547269

72557270
for (Instruction *IVInst : IVInsts) {
72567271
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)