Skip to content

Commit 0e08891

Browse files
committed
[DA] control compile-time spent by MIV tests
Function exploreDirections() in DependenceAnalysis implements a recursive algorithm for refining direction vectors. This algorithm has worst-case complexity of O(3^(n+1)) where n is the number of common loop levels. In this patch I'm adding a threshold to control the amount of time we spend in doing MIV tests (which most of the time end up resulting in over pessimistic direction vectors anyway). Reviewed By: Meinersbur Differential Revision: https://reviews.llvm.org/D107159
1 parent 8d08a84 commit 0e08891

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ static cl::opt<bool> DisableDelinearizationChecks(
119119
"dependence vectors for languages that allow the subscript of one "
120120
"dimension to underflow or overflow into another dimension."));
121121

122+
static cl::opt<unsigned> MIVMaxLevelThreshold(
123+
"da-miv-max-level-threshold", cl::init(7), cl::Hidden, cl::ZeroOrMore,
124+
cl::desc("Maximum depth allowed for the recursive algorithm used to "
125+
"explore MIV direction vectors."));
126+
122127
//===----------------------------------------------------------------------===//
123128
// basics
124129

@@ -2602,6 +2607,19 @@ unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
26022607
const SmallBitVector &Loops,
26032608
unsigned &DepthExpanded,
26042609
const SCEV *Delta) const {
2610+
// This algorithm has worst case complexity of O(3^n), where 'n' is the number
2611+
// of common loop levels. To avoid excessive compile-time, pessimize all the
2612+
// results and immediately return when the number of common levels is beyond
2613+
// the given threshold.
2614+
if (CommonLevels > MIVMaxLevelThreshold) {
2615+
LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
2616+
"direction exploration is terminated.\n");
2617+
for (unsigned K = 1; K <= CommonLevels; ++K)
2618+
if (Loops[K])
2619+
Bound[K].DirSet = Dependence::DVEntry::ALL;
2620+
return 1;
2621+
}
2622+
26052623
if (Level > CommonLevels) {
26062624
// record result
26072625
LLVM_DEBUG(dbgs() << "\t[");
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
; RUN: opt < %s -aa-pipeline=default -passes='print<da>' -da-miv-max-level-threshold=2
2+
3+
;; Check to make sure when MIV tests reach a maximum depth level
4+
;; threshold, the resulting dependence is conservatively correct.
5+
6+
;; for (int i = 0; i < n; i++)
7+
;; for (int j = 0; j < m; j++)
8+
;; for (int k = 0; k < o; k++)
9+
;; A[i+j+k] = A[i-j+k] + B[i+j+k];
10+
11+
12+
; CHECK: Src: %2 = load float, float* %arrayidx, align 4 --> Dst: %5 = load float, float* %arrayidx12, align 4
13+
; CHECK-NEXT: da analyze - none!
14+
; CHECK: Src: %2 = load float, float* %arrayidx, align 4 --> Dst: store float %add13, float* %arrayidx17, align 4
15+
; CHECK-NEXT: da analyze - anti [* * *|<]!
16+
; CHECK: Src: %5 = load float, float* %arrayidx12, align 4 --> Dst: store float %add13, float* %arrayidx17, align 4
17+
; CHECK-NEXT: da analyze - none!
18+
19+
define void @foo(float* noalias %A, float* noalias %B, i32 signext %m, i32 signext %n, i32 signext %o) {
20+
entry:
21+
%cmp5 = icmp sgt i32 %n, 0
22+
br i1 %cmp5, label %for.body.preheader, label %for.end23
23+
24+
for.body.preheader: ; preds = %entry
25+
%wide.trip.count21 = zext i32 %n to i64
26+
br label %for.body
27+
28+
for.body: ; preds = %for.body.preheader, %for.inc21
29+
%indvars.iv19 = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next20, %for.inc21 ]
30+
%cmp23 = icmp sgt i32 %m, 0
31+
br i1 %cmp23, label %for.body4.preheader, label %for.inc21
32+
33+
for.body4.preheader: ; preds = %for.body
34+
%wide.trip.count17 = zext i32 %m to i64
35+
br label %for.body4
36+
37+
for.body4: ; preds = %for.body4.preheader, %for.inc18
38+
%indvars.iv12 = phi i64 [ 0, %for.body4.preheader ], [ %indvars.iv.next13, %for.inc18 ]
39+
%cmp61 = icmp sgt i32 %o, 0
40+
br i1 %cmp61, label %for.body8.preheader, label %for.inc18
41+
42+
for.body8.preheader: ; preds = %for.body4
43+
%wide.trip.count = zext i32 %o to i64
44+
br label %for.body8
45+
46+
for.body8: ; preds = %for.body8.preheader, %for.body8
47+
%indvars.iv = phi i64 [ 0, %for.body8.preheader ], [ %indvars.iv.next, %for.body8 ]
48+
%0 = sub nsw i64 %indvars.iv19, %indvars.iv12
49+
%1 = add nsw i64 %0, %indvars.iv
50+
%arrayidx = getelementptr inbounds float, float* %A, i64 %1
51+
%2 = load float, float* %arrayidx, align 4
52+
%3 = add nuw nsw i64 %indvars.iv19, %indvars.iv12
53+
%4 = add nuw nsw i64 %3, %indvars.iv
54+
%arrayidx12 = getelementptr inbounds float, float* %B, i64 %4
55+
%5 = load float, float* %arrayidx12, align 4
56+
%add13 = fadd fast float %2, %5
57+
%6 = add nuw nsw i64 %indvars.iv19, %indvars.iv12
58+
%7 = add nuw nsw i64 %6, %indvars.iv
59+
%arrayidx17 = getelementptr inbounds float, float* %A, i64 %7
60+
store float %add13, float* %arrayidx17, align 4
61+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
62+
%exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count
63+
br i1 %exitcond, label %for.body8, label %for.inc18.loopexit
64+
65+
for.inc18.loopexit: ; preds = %for.body8
66+
br label %for.inc18
67+
68+
for.inc18: ; preds = %for.inc18.loopexit, %for.body4
69+
%indvars.iv.next13 = add nuw nsw i64 %indvars.iv12, 1
70+
%exitcond18 = icmp ne i64 %indvars.iv.next13, %wide.trip.count17
71+
br i1 %exitcond18, label %for.body4, label %for.inc21.loopexit
72+
73+
for.inc21.loopexit: ; preds = %for.inc18
74+
br label %for.inc21
75+
76+
for.inc21: ; preds = %for.inc21.loopexit, %for.body
77+
%indvars.iv.next20 = add nuw nsw i64 %indvars.iv19, 1
78+
%exitcond22 = icmp ne i64 %indvars.iv.next20, %wide.trip.count21
79+
br i1 %exitcond22, label %for.body, label %for.end23.loopexit
80+
81+
for.end23.loopexit: ; preds = %for.inc21
82+
br label %for.end23
83+
84+
for.end23: ; preds = %for.end23.loopexit, %entry
85+
ret void
86+
}

0 commit comments

Comments
 (0)