Skip to content

Commit 9a087a3

Browse files
author
Zvi Rackover
committed
LoopVectorize: MaxVF should not be larger than the loop trip count
Summary: Improve how MaxVF is computed while taking into account that MaxVF should not be larger than the loop's trip count. Other than saving on compile-time by pruning the possible MaxVF candidates, this patch fixes pr34438 which exposed the following flow: 1. Short trip count identified -> Don't bail out, set OptForSize:=True to avoid tail-loop and runtime checks. 2. Compute MaxVF returned 16 on a target supporting AVX512. 3. OptForSize -> choose VF:=MaxVF. 4. Bail out because TripCount = 8, VF = 16, TripCount % VF !=0 means we need a tail loop. With this patch step 2. will choose MaxVF=8 based on TripCount. Reviewers: Ayal, dorit, mkuper, hfinkel Reviewed By: hfinkel Subscribers: hfinkel, llvm-commits Differential Revision: https://reviews.llvm.org/D37425 llvm-svn: 312472
1 parent 7cd826a commit 9a087a3

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,7 @@ class LoopVectorizationCostModel {
19601960
private:
19611961
/// \return An upper bound for the vectorization factor, larger than zero.
19621962
/// One is returned if vectorization should best be avoided due to cost.
1963-
unsigned computeFeasibleMaxVF(bool OptForSize);
1963+
unsigned computeFeasibleMaxVF(bool OptForSize, unsigned ConstTripCount = 0);
19641964

19651965
/// The vectorization cost is a combination of the cost itself and a boolean
19661966
/// indicating whether any of the contributing operations will actually
@@ -6187,7 +6187,7 @@ Optional<unsigned> LoopVectorizationCostModel::computeMaxVF(bool OptForSize) {
61876187
return None;
61886188
}
61896189

6190-
unsigned MaxVF = computeFeasibleMaxVF(OptForSize);
6190+
unsigned MaxVF = computeFeasibleMaxVF(OptForSize, TC);
61916191

61926192
if (TC % MaxVF != 0) {
61936193
// If the trip count that we found modulo the vectorization factor is not
@@ -6208,7 +6208,9 @@ Optional<unsigned> LoopVectorizationCostModel::computeMaxVF(bool OptForSize) {
62086208
return MaxVF;
62096209
}
62106210

6211-
unsigned LoopVectorizationCostModel::computeFeasibleMaxVF(bool OptForSize) {
6211+
unsigned
6212+
LoopVectorizationCostModel::computeFeasibleMaxVF(bool OptForSize,
6213+
unsigned ConstTripCount) {
62126214
MinBWs = computeMinimumValueSizes(TheLoop->getBlocks(), *DB, &TTI);
62136215
unsigned SmallestType, WidestType;
62146216
std::tie(SmallestType, WidestType) = getSmallestAndWidestTypes();
@@ -6237,7 +6239,9 @@ unsigned LoopVectorizationCostModel::computeFeasibleMaxVF(bool OptForSize) {
62376239
if (MaxVectorSize == 0) {
62386240
DEBUG(dbgs() << "LV: The target has no vector registers.\n");
62396241
MaxVectorSize = 1;
6240-
}
6242+
} else if (ConstTripCount && ConstTripCount < MaxVectorSize &&
6243+
isPowerOf2_32(ConstTripCount))
6244+
MaxVectorSize = ConstTripCount;
62416245

62426246
assert(MaxVectorSize <= 64 && "Did not expect to pack so many elements"
62436247
" into one vector!");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; PR34438
2+
; Loop has a short trip count of 8 iterations. It should be vectorized because no runtime checks or tail loop are necessary.
3+
; Two cases tested AVX (MaxVF=8 = TripCount) and AVX512 (MaxVF=16 > TripCount)
4+
5+
; RUN: opt < %s -loop-vectorize -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7-avx -S | FileCheck %s
6+
; RUN: opt < %s -loop-vectorize -mtriple=x86_64-apple-macosx10.8.0 -mcpu=skylake-avx512 -S | FileCheck %s
7+
8+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
9+
target triple = "x86_64-apple-macosx10.8.0"
10+
11+
define void @small_tc(float* noalias nocapture %A, float* noalias nocapture readonly %B) {
12+
; CHECK-LABEL: @small_tc
13+
; CHECK: load <8 x float>, <8 x float>*
14+
; CHECK: fadd fast <8 x float>
15+
entry:
16+
br label %for.body
17+
18+
for.body:
19+
%indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
20+
%arrayidx = getelementptr inbounds float, float* %B, i64 %indvars.iv
21+
%0 = load float, float* %arrayidx, align 4, !llvm.mem.parallel_loop_access !3
22+
%arrayidx2 = getelementptr inbounds float, float* %A, i64 %indvars.iv
23+
%1 = load float, float* %arrayidx2, align 4, !llvm.mem.parallel_loop_access !3
24+
%add = fadd fast float %0, %1
25+
store float %add, float* %arrayidx2, align 4, !llvm.mem.parallel_loop_access !3
26+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
27+
%exitcond = icmp eq i64 %indvars.iv.next, 8
28+
br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !4
29+
30+
for.end:
31+
ret void
32+
}
33+
34+
!3 = !{!3}
35+
!4 = !{!4}

0 commit comments

Comments
 (0)