Skip to content

Commit 484524c

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 b4ae419 commit 484524c

File tree

2 files changed

+282
-13
lines changed

2 files changed

+282
-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: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
; RUN: opt < %s -passes=loop-interchange --pass-remarks-missed=loop-interchange -disable-output 2>&1 | FileCheck %s
2+
target triple = "aarch64-unknown-linux-gnu"
3+
4+
@A = dso_local local_unnamed_addr global [2048 x [2048 x i32]] zeroinitializer, align 4
5+
@B = dso_local local_unnamed_addr global [2048 x [2048 x i32]] zeroinitializer, align 4
6+
@C = dso_local local_unnamed_addr global [2048 x [2048 x i32]] zeroinitializer, align 4
7+
8+
; CHECK: Number of loads/stores in the loop are more than threshold.
9+
define dso_local noundef i32 @many_load_stores() {
10+
br label %1
11+
12+
1: ; preds = %9, %0
13+
%2 = phi i32 [ 0, %0 ], [ %10, %9 ]
14+
%3 = icmp slt i32 %2, 2048
15+
br i1 %3, label %5, label %4
16+
17+
4: ; preds = %1
18+
ret i32 0
19+
20+
5: ; preds = %1
21+
br label %6
22+
23+
6: ; preds = %11, %5
24+
%7 = phi i32 [ 0, %5 ], [ %208, %11 ]
25+
%8 = icmp slt i32 %7, 85
26+
br i1 %8, label %11, label %9
27+
28+
9: ; preds = %6
29+
%10 = add nsw i32 %2, 1
30+
br label %1
31+
32+
11: ; preds = %6
33+
%12 = sext i32 %2 to i64
34+
%13 = getelementptr inbounds [2048 x [2048 x i32]], [2048 x [2048 x i32]]* @B, i64 0, i64 %12
35+
%14 = sext i32 %7 to i64
36+
%15 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %14
37+
%16 = load i32, i32* %15, align 4
38+
%17 = getelementptr inbounds [2048 x [2048 x i32]], [2048 x [2048 x i32]]* @C, i64 0, i64 %12
39+
%18 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %14
40+
%19 = load i32, i32* %18, align 4
41+
%20 = add nsw i32 %16, %19
42+
%21 = getelementptr inbounds [2048 x [2048 x i32]], [2048 x [2048 x i32]]* @A, i64 0, i64 %12
43+
%22 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %14
44+
store i32 %20, i32* %22, align 4
45+
%23 = add nsw i32 %7, 1
46+
%24 = sext i32 %23 to i64
47+
%25 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %24
48+
%26 = load i32, i32* %25, align 4
49+
%27 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %24
50+
%28 = load i32, i32* %27, align 4
51+
%29 = add nsw i32 %26, %28
52+
%30 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %24
53+
store i32 %29, i32* %30, align 4
54+
%31 = add nsw i32 %23, 1
55+
%32 = sext i32 %31 to i64
56+
%33 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %32
57+
%34 = load i32, i32* %33, align 4
58+
%35 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %32
59+
%36 = load i32, i32* %35, align 4
60+
%37 = add nsw i32 %34, %36
61+
%38 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %32
62+
store i32 %37, i32* %38, align 4
63+
%39 = add nsw i32 %31, 1
64+
%40 = sext i32 %39 to i64
65+
%41 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %40
66+
%42 = load i32, i32* %41, align 4
67+
%43 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %40
68+
%44 = load i32, i32* %43, align 4
69+
%45 = add nsw i32 %42, %44
70+
%46 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %40
71+
store i32 %45, i32* %46, align 4
72+
%47 = add nsw i32 %39, 1
73+
%48 = sext i32 %47 to i64
74+
%49 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %48
75+
%50 = load i32, i32* %49, align 4
76+
%51 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %48
77+
%52 = load i32, i32* %51, align 4
78+
%53 = add nsw i32 %50, %52
79+
%54 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %48
80+
store i32 %53, i32* %54, align 4
81+
%55 = add nsw i32 %47, 1
82+
%56 = sext i32 %55 to i64
83+
%57 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %56
84+
%58 = load i32, i32* %57, align 4
85+
%59 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %56
86+
%60 = load i32, i32* %59, align 4
87+
%61 = add nsw i32 %58, %60
88+
%62 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %56
89+
store i32 %61, i32* %62, align 4
90+
%63 = add nsw i32 %55, 1
91+
%64 = sext i32 %63 to i64
92+
%65 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %64
93+
%66 = load i32, i32* %65, align 4
94+
%67 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %64
95+
%68 = load i32, i32* %67, align 4
96+
%69 = add nsw i32 %66, %68
97+
%70 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %64
98+
store i32 %69, i32* %70, align 4
99+
%71 = add nsw i32 %63, 1
100+
%72 = sext i32 %71 to i64
101+
%73 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %72
102+
%74 = load i32, i32* %73, align 4
103+
%75 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %72
104+
%76 = load i32, i32* %75, align 4
105+
%77 = add nsw i32 %74, %76
106+
%78 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %72
107+
store i32 %77, i32* %78, align 4
108+
%79 = add nsw i32 %71, 1
109+
%80 = sext i32 %79 to i64
110+
%81 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %80
111+
%82 = load i32, i32* %81, align 4
112+
%83 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %80
113+
%84 = load i32, i32* %83, align 4
114+
%85 = add nsw i32 %82, %84
115+
%86 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %80
116+
store i32 %85, i32* %86, align 4
117+
%87 = add nsw i32 %79, 1
118+
%88 = sext i32 %87 to i64
119+
%89 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %88
120+
%90 = load i32, i32* %89, align 4
121+
%91 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %88
122+
%92 = load i32, i32* %91, align 4
123+
%93 = add nsw i32 %90, %92
124+
%94 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %88
125+
store i32 %93, i32* %94, align 4
126+
%95 = add nsw i32 %87, 1
127+
%96 = sext i32 %95 to i64
128+
%97 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %96
129+
%98 = load i32, i32* %97, align 4
130+
%99 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %96
131+
%100 = load i32, i32* %99, align 4
132+
%101 = add nsw i32 %98, %100
133+
%102 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %96
134+
store i32 %101, i32* %102, align 4
135+
%103 = add nsw i32 %95, 1
136+
%104 = sext i32 %103 to i64
137+
%105 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %104
138+
%106 = load i32, i32* %105, align 4
139+
%107 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %104
140+
%108 = load i32, i32* %107, align 4
141+
%109 = add nsw i32 %106, %108
142+
%110 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %104
143+
store i32 %109, i32* %110, align 4
144+
%111 = add nsw i32 %103, 1
145+
%112 = sext i32 %111 to i64
146+
%113 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %112
147+
%114 = load i32, i32* %113, align 4
148+
%115 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %112
149+
%116 = load i32, i32* %115, align 4
150+
%117 = add nsw i32 %114, %116
151+
%118 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %112
152+
store i32 %117, i32* %118, align 4
153+
%119 = add nsw i32 %111, 1
154+
%120 = sext i32 %119 to i64
155+
%121 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %120
156+
%122 = load i32, i32* %121, align 4
157+
%123 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %120
158+
%124 = load i32, i32* %123, align 4
159+
%125 = add nsw i32 %122, %124
160+
%126 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %120
161+
store i32 %125, i32* %126, align 4
162+
%127 = add nsw i32 %119, 1
163+
%128 = sext i32 %127 to i64
164+
%129 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %128
165+
%130 = load i32, i32* %129, align 4
166+
%131 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %128
167+
%132 = load i32, i32* %131, align 4
168+
%133 = add nsw i32 %130, %132
169+
%134 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %128
170+
store i32 %133, i32* %134, align 4
171+
%135 = add nsw i32 %127, 1
172+
%136 = sext i32 %135 to i64
173+
%137 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %136
174+
%138 = load i32, i32* %137, align 4
175+
%139 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %136
176+
%140 = load i32, i32* %139, align 4
177+
%141 = add nsw i32 %138, %140
178+
%142 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %136
179+
store i32 %141, i32* %142, align 4
180+
%143 = add nsw i32 %135, 1
181+
%144 = sext i32 %143 to i64
182+
%145 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %144
183+
%146 = load i32, i32* %145, align 4
184+
%147 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %144
185+
%148 = load i32, i32* %147, align 4
186+
%149 = add nsw i32 %146, %148
187+
%150 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %144
188+
store i32 %149, i32* %150, align 4
189+
%151 = add nsw i32 %143, 1
190+
%152 = sext i32 %151 to i64
191+
%153 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %152
192+
%154 = load i32, i32* %153, align 4
193+
%155 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %152
194+
%156 = load i32, i32* %155, align 4
195+
%157 = add nsw i32 %154, %156
196+
%158 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %152
197+
store i32 %157, i32* %158, align 4
198+
%159 = add nsw i32 %151, 1
199+
%160 = sext i32 %159 to i64
200+
%161 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %160
201+
%162 = load i32, i32* %161, align 4
202+
%163 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %160
203+
%164 = load i32, i32* %163, align 4
204+
%165 = add nsw i32 %162, %164
205+
%166 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %160
206+
store i32 %165, i32* %166, align 4
207+
%167 = add nsw i32 %159, 1
208+
%168 = sext i32 %167 to i64
209+
%169 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %168
210+
%170 = load i32, i32* %169, align 4
211+
%171 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %168
212+
%172 = load i32, i32* %171, align 4
213+
%173 = add nsw i32 %170, %172
214+
%174 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %168
215+
store i32 %173, i32* %174, align 4
216+
%175 = add nsw i32 %167, 1
217+
%176 = sext i32 %175 to i64
218+
%177 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %176
219+
%178 = load i32, i32* %177, align 4
220+
%179 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %176
221+
%180 = load i32, i32* %179, align 4
222+
%181 = add nsw i32 %178, %180
223+
%182 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %176
224+
store i32 %181, i32* %182, align 4
225+
%183 = add nsw i32 %175, 1
226+
%184 = sext i32 %183 to i64
227+
%185 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %184
228+
%186 = load i32, i32* %185, align 4
229+
%187 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %184
230+
%188 = load i32, i32* %187, align 4
231+
%189 = add nsw i32 %186, %188
232+
%190 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %184
233+
store i32 %189, i32* %190, align 4
234+
%191 = add nsw i32 %183, 1
235+
%192 = sext i32 %191 to i64
236+
%193 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %192
237+
%194 = load i32, i32* %193, align 4
238+
%195 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %192
239+
%196 = load i32, i32* %195, align 4
240+
%197 = add nsw i32 %194, %196
241+
%198 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %192
242+
store i32 %197, i32* %198, align 4
243+
%199 = add nsw i32 %191, 1
244+
%200 = sext i32 %199 to i64
245+
%201 = getelementptr inbounds [2048 x i32], [2048 x i32]* %13, i64 0, i64 %200
246+
%202 = load i32, i32* %201, align 4
247+
%203 = getelementptr inbounds [2048 x i32], [2048 x i32]* %17, i64 0, i64 %200
248+
%204 = load i32, i32* %203, align 4
249+
%205 = add nsw i32 %202, %204
250+
%206 = getelementptr inbounds [2048 x i32], [2048 x i32]* %21, i64 0, i64 %200
251+
store i32 %205, i32* %206, align 4
252+
%207 = add nsw i32 %199, 1
253+
%208 = add nsw i32 %207, 24
254+
br label %6
255+
}

0 commit comments

Comments
 (0)