Skip to content

Commit ac0fad8

Browse files
committed
[LoopInterchange] Constrain load/stores in a loop
In the current state of the code, the transform computes entries for the dependency matrix until MaxMemInstrCount which is 100. After 99th entry it terminates and thus overall wastes compile-time. It would be nice if we can compute total number of entries upfront and early exit if the number of entries > 100. However, computing the number of entries is not always possible as it depends on two factors: 1. Number of load-store pairs in a loop. 2. Number of common loop levels for each of the pair. This patch just constrains the whole computation on the number of loads and store instructions in the loop. With 64, I see 39 interchanges compared to 42 with the trunk. (With 128, it is 42 vs 43 but I see increase in the compile-time.) In an other approach, I experimented with computing 1 and constraining the number of pairs but that did not lead to any additional benefit in terms of compile-time. However, when other issues are fixed, I can revisit this approach.
1 parent c4fb718 commit ac0fad8

File tree

2 files changed

+285
-13
lines changed

2 files changed

+285
-13
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ static cl::opt<int> LoopInterchangeCostThreshold(
5757
"loop-interchange-threshold", cl::init(0), cl::Hidden,
5858
cl::desc("Interchange if you gain more than this number"));
5959

60+
// Maximum number of load-stores that can be handled in the dependency matrix.
61+
static cl::opt<unsigned int> MaxMemInstrCount(
62+
"loop-interchange-max-meminstr-count", cl::init(64), cl::Hidden,
63+
cl::desc(
64+
"Maximum number of load-store instructions that should be handled "
65+
"in the dependency matrix. Higher value may lead to more interchanges "
66+
"at the cost of compile-time"));
67+
6068
namespace {
6169

6270
using LoopVector = SmallVector<Loop *, 8>;
@@ -66,9 +74,6 @@ using CharMatrix = std::vector<std::vector<char>>;
6674

6775
} // end anonymous namespace
6876

69-
// Maximum number of dependencies that can be handled in the dependency matrix.
70-
static const unsigned MaxMemInstrCount = 100;
71-
7277
// Maximum loop depth supported.
7378
static const unsigned MaxLoopNestDepth = 10;
7479

@@ -84,7 +89,8 @@ static void printDepMatrix(CharMatrix &DepMatrix) {
8489

8590
static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
8691
Loop *L, DependenceInfo *DI,
87-
ScalarEvolution *SE) {
92+
ScalarEvolution *SE,
93+
OptimizationRemarkEmitter *ORE) {
8894
using ValueVector = SmallVector<Value *, 16>;
8995

9096
ValueVector MemInstr;
@@ -109,7 +115,16 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
109115

110116
LLVM_DEBUG(dbgs() << "Found " << MemInstr.size()
111117
<< " Loads and Stores to analyze\n");
112-
118+
if (MemInstr.size() > MaxMemInstrCount) {
119+
LLVM_DEBUG(dbgs() << "The transform doesn't support more than "
120+
<< MaxMemInstrCount << " load stores in a loop\n");
121+
ORE->emit([&]() {
122+
return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLoop",
123+
L->getStartLoc(), L->getHeader())
124+
<< "Number of loads/stores in the loop are more than threshold.";
125+
});
126+
return false;
127+
}
113128
ValueVector::iterator I, IE, J, JE;
114129
StringSet<> Seen;
115130

@@ -161,12 +176,6 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
161176
// Make sure we only add unique entries to the dependency matrix.
162177
if (Seen.insert(StringRef(Dep.data(), Dep.size())).second)
163178
DepMatrix.push_back(Dep);
164-
165-
if (DepMatrix.size() > MaxMemInstrCount) {
166-
LLVM_DEBUG(dbgs() << "Cannot handle more than " << MaxMemInstrCount
167-
<< " dependencies inside loop\n");
168-
return false;
169-
}
170179
}
171180
}
172181
}
@@ -450,7 +459,7 @@ struct LoopInterchange {
450459
CharMatrix DependencyMatrix;
451460
Loop *OuterMostLoop = *(LoopList.begin());
452461
if (!populateDependencyMatrix(DependencyMatrix, LoopNestDepth,
453-
OuterMostLoop, DI, SE)) {
462+
OuterMostLoop, DI, SE, ORE)) {
454463
LLVM_DEBUG(dbgs() << "Populating dependency matrix failed\n");
455464
return false;
456465
}
@@ -1725,10 +1734,15 @@ PreservedAnalyses LoopInterchangePass::run(LoopNest &LN,
17251734
LPMUpdater &U) {
17261735
Function &F = *LN.getParent();
17271736
SmallVector<Loop *, 8> LoopList(LN.getLoops());
1737+
1738+
if (MaxMemInstrCount < 1) {
1739+
LLVM_DEBUG(dbgs() << "MaxMemInstrCount should be at least 1");
1740+
return PreservedAnalyses::all();
1741+
}
1742+
17281743
// Ensure minimum depth of the loop nest to do the interchange.
17291744
if (!hasMinimumLoopDepth(LoopList))
17301745
return PreservedAnalyses::all();
1731-
17321746
DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
17331747
std::unique_ptr<CacheCost> CC =
17341748
CacheCost::getCacheCost(LN.getOutermostLoop(), AR, DI);
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
; RUN: opt < %s -passes=loop-interchange --pass-remarks-missed=loop-interchange -disable-output 2>&1 | FileCheck %s
2+
; RUN: opt < %s -passes=loop-interchange --pass-remarks-missed=loop-interchange -loop-interchange-max-meminstr-count=75
3+
; -disable-output 2>&1 | FileCheck --check-prefix=CHECK_INSTR_COUNT %s
4+
target triple = "aarch64-unknown-linux-gnu"
5+
6+
@A = dso_local local_unnamed_addr global [2048 x [2048 x i32]] zeroinitializer, align 4
7+
@B = dso_local local_unnamed_addr global [2048 x [2048 x i32]] zeroinitializer, align 4
8+
@C = dso_local local_unnamed_addr global [2048 x [2048 x i32]] zeroinitializer, align 4
9+
10+
; CHECK: Number of loads/stores in the loop are more than threshold.
11+
; CHECK_INSTR_COUNT-NOT: Number of loads/stores in the loop are more than threshold.
12+
define dso_local noundef i32 @many_load_stores() {
13+
br label %1
14+
15+
1: ; preds = %9, %0
16+
%2 = phi i32 [ 0, %0 ], [ %10, %9 ]
17+
%3 = icmp slt i32 %2, 2048
18+
br i1 %3, label %5, label %4
19+
20+
4: ; preds = %1
21+
ret i32 0
22+
23+
5: ; preds = %1
24+
br label %6
25+
26+
6: ; preds = %11, %5
27+
%7 = phi i32 [ 0, %5 ], [ %208, %11 ]
28+
%8 = icmp slt i32 %7, 85
29+
br i1 %8, label %11, label %9
30+
31+
9: ; preds = %6
32+
%10 = add nsw i32 %2, 1
33+
br label %1
34+
35+
11: ; preds = %6
36+
%12 = sext i32 %2 to i64
37+
%13 = getelementptr inbounds [2048 x [2048 x i32]], [2048 x [2048 x i32]]* @B, i64 0, i64 %12
38+
%14 = sext i32 %7 to i64
39+
%15 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %14
40+
%16 = load i32, i32* %15, align 4
41+
%17 = getelementptr inbounds [2048 x [2048 x i32]], [2048 x [2048 x i32]]* @C, i64 0, i64 %12
42+
%18 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %14
43+
%19 = load i32, i32* %18, align 4
44+
%20 = add nsw i32 %16, %19
45+
%21 = getelementptr inbounds [2048 x [2048 x i32]], [2048 x [2048 x i32]]* @A, i64 0, i64 %12
46+
%22 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %14
47+
store i32 %20, i32* %22, align 4
48+
%23 = add nsw i32 %7, 1
49+
%24 = sext i32 %23 to i64
50+
%25 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %24
51+
%26 = load i32, i32* %25, align 4
52+
%27 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %24
53+
%28 = load i32, i32* %27, align 4
54+
%29 = add nsw i32 %26, %28
55+
%30 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %24
56+
store i32 %29, i32* %30, align 4
57+
%31 = add nsw i32 %23, 1
58+
%32 = sext i32 %31 to i64
59+
%33 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %32
60+
%34 = load i32, i32* %33, align 4
61+
%35 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %32
62+
%36 = load i32, i32* %35, align 4
63+
%37 = add nsw i32 %34, %36
64+
%38 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %32
65+
store i32 %37, i32* %38, align 4
66+
%39 = add nsw i32 %31, 1
67+
%40 = sext i32 %39 to i64
68+
%41 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %40
69+
%42 = load i32, i32* %41, align 4
70+
%43 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %40
71+
%44 = load i32, i32* %43, align 4
72+
%45 = add nsw i32 %42, %44
73+
%46 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %40
74+
store i32 %45, i32* %46, align 4
75+
%47 = add nsw i32 %39, 1
76+
%48 = sext i32 %47 to i64
77+
%49 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %48
78+
%50 = load i32, i32* %49, align 4
79+
%51 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %48
80+
%52 = load i32, i32* %51, align 4
81+
%53 = add nsw i32 %50, %52
82+
%54 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %48
83+
store i32 %53, i32* %54, align 4
84+
%55 = add nsw i32 %47, 1
85+
%56 = sext i32 %55 to i64
86+
%57 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %56
87+
%58 = load i32, i32* %57, align 4
88+
%59 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %56
89+
%60 = load i32, i32* %59, align 4
90+
%61 = add nsw i32 %58, %60
91+
%62 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %56
92+
store i32 %61, i32* %62, align 4
93+
%63 = add nsw i32 %55, 1
94+
%64 = sext i32 %63 to i64
95+
%65 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %64
96+
%66 = load i32, i32* %65, align 4
97+
%67 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %64
98+
%68 = load i32, i32* %67, align 4
99+
%69 = add nsw i32 %66, %68
100+
%70 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %64
101+
store i32 %69, i32* %70, align 4
102+
%71 = add nsw i32 %63, 1
103+
%72 = sext i32 %71 to i64
104+
%73 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %72
105+
%74 = load i32, i32* %73, align 4
106+
%75 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %72
107+
%76 = load i32, i32* %75, align 4
108+
%77 = add nsw i32 %74, %76
109+
%78 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %72
110+
store i32 %77, i32* %78, align 4
111+
%79 = add nsw i32 %71, 1
112+
%80 = sext i32 %79 to i64
113+
%81 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %80
114+
%82 = load i32, i32* %81, align 4
115+
%83 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %80
116+
%84 = load i32, i32* %83, align 4
117+
%85 = add nsw i32 %82, %84
118+
%86 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %80
119+
store i32 %85, i32* %86, align 4
120+
%87 = add nsw i32 %79, 1
121+
%88 = sext i32 %87 to i64
122+
%89 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %88
123+
%90 = load i32, i32* %89, align 4
124+
%91 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %88
125+
%92 = load i32, i32* %91, align 4
126+
%93 = add nsw i32 %90, %92
127+
%94 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %88
128+
store i32 %93, i32* %94, align 4
129+
%95 = add nsw i32 %87, 1
130+
%96 = sext i32 %95 to i64
131+
%97 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %96
132+
%98 = load i32, i32* %97, align 4
133+
%99 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %96
134+
%100 = load i32, i32* %99, align 4
135+
%101 = add nsw i32 %98, %100
136+
%102 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %96
137+
store i32 %101, i32* %102, align 4
138+
%103 = add nsw i32 %95, 1
139+
%104 = sext i32 %103 to i64
140+
%105 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %104
141+
%106 = load i32, i32* %105, align 4
142+
%107 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %104
143+
%108 = load i32, i32* %107, align 4
144+
%109 = add nsw i32 %106, %108
145+
%110 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %104
146+
store i32 %109, i32* %110, align 4
147+
%111 = add nsw i32 %103, 1
148+
%112 = sext i32 %111 to i64
149+
%113 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %112
150+
%114 = load i32, i32* %113, align 4
151+
%115 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %112
152+
%116 = load i32, i32* %115, align 4
153+
%117 = add nsw i32 %114, %116
154+
%118 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %112
155+
store i32 %117, i32* %118, align 4
156+
%119 = add nsw i32 %111, 1
157+
%120 = sext i32 %119 to i64
158+
%121 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %120
159+
%122 = load i32, i32* %121, align 4
160+
%123 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %120
161+
%124 = load i32, i32* %123, align 4
162+
%125 = add nsw i32 %122, %124
163+
%126 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %120
164+
store i32 %125, i32* %126, align 4
165+
%127 = add nsw i32 %119, 1
166+
%128 = sext i32 %127 to i64
167+
%129 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %128
168+
%130 = load i32, i32* %129, align 4
169+
%131 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %128
170+
%132 = load i32, i32* %131, align 4
171+
%133 = add nsw i32 %130, %132
172+
%134 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %128
173+
store i32 %133, i32* %134, align 4
174+
%135 = add nsw i32 %127, 1
175+
%136 = sext i32 %135 to i64
176+
%137 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %136
177+
%138 = load i32, i32* %137, align 4
178+
%139 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %136
179+
%140 = load i32, i32* %139, align 4
180+
%141 = add nsw i32 %138, %140
181+
%142 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %136
182+
store i32 %141, i32* %142, align 4
183+
%143 = add nsw i32 %135, 1
184+
%144 = sext i32 %143 to i64
185+
%145 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %144
186+
%146 = load i32, i32* %145, align 4
187+
%147 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %144
188+
%148 = load i32, i32* %147, align 4
189+
%149 = add nsw i32 %146, %148
190+
%150 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %144
191+
store i32 %149, i32* %150, align 4
192+
%151 = add nsw i32 %143, 1
193+
%152 = sext i32 %151 to i64
194+
%153 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %152
195+
%154 = load i32, i32* %153, align 4
196+
%155 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %152
197+
%156 = load i32, i32* %155, align 4
198+
%157 = add nsw i32 %154, %156
199+
%158 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %152
200+
store i32 %157, i32* %158, align 4
201+
%159 = add nsw i32 %151, 1
202+
%160 = sext i32 %159 to i64
203+
%161 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %160
204+
%162 = load i32, i32* %161, align 4
205+
%163 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %160
206+
%164 = load i32, i32* %163, align 4
207+
%165 = add nsw i32 %162, %164
208+
%166 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %160
209+
store i32 %165, i32* %166, align 4
210+
%167 = add nsw i32 %159, 1
211+
%168 = sext i32 %167 to i64
212+
%169 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %168
213+
%170 = load i32, i32* %169, align 4
214+
%171 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %168
215+
%172 = load i32, i32* %171, align 4
216+
%173 = add nsw i32 %170, %172
217+
%174 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %168
218+
store i32 %173, i32* %174, align 4
219+
%175 = add nsw i32 %167, 1
220+
%176 = sext i32 %175 to i64
221+
%177 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %176
222+
%178 = load i32, i32* %177, align 4
223+
%179 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %176
224+
%180 = load i32, i32* %179, align 4
225+
%181 = add nsw i32 %178, %180
226+
%182 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %176
227+
store i32 %181, i32* %182, align 4
228+
%183 = add nsw i32 %175, 1
229+
%184 = sext i32 %183 to i64
230+
%185 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %184
231+
%186 = load i32, i32* %185, align 4
232+
%187 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %184
233+
%188 = load i32, i32* %187, align 4
234+
%189 = add nsw i32 %186, %188
235+
%190 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %184
236+
store i32 %189, i32* %190, align 4
237+
%191 = add nsw i32 %183, 1
238+
%192 = sext i32 %191 to i64
239+
%193 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %192
240+
%194 = load i32, i32* %193, align 4
241+
%195 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %192
242+
%196 = load i32, i32* %195, align 4
243+
%197 = add nsw i32 %194, %196
244+
%198 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %192
245+
store i32 %197, i32* %198, align 4
246+
%199 = add nsw i32 %191, 1
247+
%200 = sext i32 %199 to i64
248+
%201 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %200
249+
%202 = load i32, i32* %201, align 4
250+
%203 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %200
251+
%204 = load i32, i32* %203, align 4
252+
%205 = add nsw i32 %202, %204
253+
%206 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %200
254+
store i32 %205, i32* %206, align 4
255+
%207 = add nsw i32 %199, 1
256+
%208 = add nsw i32 %207, 24
257+
br label %6
258+
}

0 commit comments

Comments
 (0)