blob: 2db2d01a2526a5b5db3dcb2031d1053655ea1532 [file] [log] [blame]
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001//===- Schedule.cpp - Calculate an optimized schedule ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass the isl to calculate a schedule that is optimized for parallelism
11// and tileablility. The algorithm used in isl is an optimized version of the
12// algorithm described in following paper:
13//
14// U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan.
15// A Practical Automatic Polyhedral Parallelizer and Locality Optimizer.
16// In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language
17// Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008.
18//===----------------------------------------------------------------------===//
19
20#include "polly/Cloog.h"
21#include "polly/LinkAllPasses.h"
22
Tobias Grosserde68cc92011-06-30 20:01:02 +000023#include "polly/Support/GICHelper.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000024#include "polly/Dependences.h"
25#include "polly/ScopInfo.h"
26
27#include "isl/dim.h"
28#include "isl/map.h"
29#include "isl/constraint.h"
30#include "isl/schedule.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000031#include "isl/band.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000032
33#define DEBUG_TYPE "polly-optimize-isl"
34#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000035#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000036
37using namespace llvm;
38using namespace polly;
39
Tobias Grosserc6699b72011-06-30 20:29:13 +000040static cl::opt<bool>
41Prevector("enable-schedule-prevector",
42 cl::desc("Enable the prevectorization in the scheduler"), cl::Hidden,
43 cl::value_desc("Prevectorization enabled"),
44 cl::init(false));
45
Tobias Grosser30aa24c2011-05-14 19:02:06 +000046namespace {
47
48 class ScheduleOptimizer : public ScopPass {
49
50 public:
51 static char ID;
52 explicit ScheduleOptimizer() : ScopPass(ID) {}
53
54 virtual bool runOnScop(Scop &S);
55 void printScop(llvm::raw_ostream &OS) const;
56 void getAnalysisUsage(AnalysisUsage &AU) const;
57 };
58
59}
60
61char ScheduleOptimizer::ID = 0;
62
63static int getSingleMap(__isl_take isl_map *map, void *user) {
64 isl_map **singleMap = (isl_map **) user;
65 *singleMap = map;
66
67 return 0;
68}
69
Tobias Grosser76747f72011-05-24 12:20:07 +000070static void extendScattering(Scop &S, unsigned scatDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000071 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
72 ScopStmt *stmt = *SI;
73
74 if (stmt->isFinalRead())
75 continue;
76
77 isl_map *scattering = stmt->getScattering();
78 isl_dim *dim = isl_dim_alloc(isl_map_get_ctx(scattering),
79 isl_map_n_param(scattering),
80 isl_map_n_out(scattering),
81 scatDimensions);
82 isl_basic_map *changeScattering = isl_basic_map_universe(isl_dim_copy(dim));
83
84 for (unsigned i = 0; i < isl_map_n_out(scattering); i++) {
85 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
86 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
87 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
88 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
89 }
90
91 for (unsigned i = isl_map_n_out(scattering); i < scatDimensions; i++) {
92 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
93 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
94 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
95 }
96
97 isl_map *changeScatteringMap = isl_map_from_basic_map(changeScattering);
98
99 stmt->setScattering(isl_map_apply_range(scattering, changeScatteringMap));
100 }
101}
102
Tobias Grosserde68cc92011-06-30 20:01:02 +0000103// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000104//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000105// getTileMap creates a map from a n-dimensional scattering space into an
106// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000107//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000108// Example:
109// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
110//
111// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
112// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
113// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
114//
115// Before tiling:
116//
117// for (i = 0; i < N; i++)
118// for (j = 0; j < M; j++)
119// S(i,j)
120//
121// After tiling:
122//
123// for (t_i = 0; t_i < N; i+=32)
124// for (t_j = 0; t_j < M; j+=32)
125// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
126// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
127// S(i,j)
128//
129static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
130 int parameterDimensions, int tileSize = 32) {
131 // We construct
132 //
133 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
134 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
135 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
136 //
137 // and project out the auxilary dimensions a0 and a1.
138 isl_dim *dim = isl_dim_alloc(ctx, parameterDimensions, scheduleDimensions,
139 scheduleDimensions * 3);
140 isl_basic_map *tileMap = isl_basic_map_universe(isl_dim_copy(dim));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000141
Tobias Grosserde68cc92011-06-30 20:01:02 +0000142 for (int x = 0; x < scheduleDimensions; x++) {
143 int sX = x;
144 int tX = x;
145 int pX = scheduleDimensions + x;
146 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000147
Tobias Grosserde68cc92011-06-30 20:01:02 +0000148 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000149
Tobias Grosserde68cc92011-06-30 20:01:02 +0000150 // sX = aX * tileSize;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000151 c = isl_equality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000152 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
153 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
154 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000155
Tobias Grosserde68cc92011-06-30 20:01:02 +0000156 // pX = sX;
157 c = isl_equality_alloc(isl_dim_copy(dim));
158 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
159 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
160 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000161
Tobias Grosserde68cc92011-06-30 20:01:02 +0000162 // tX <= pX
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000163 c = isl_inequality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000164 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
165 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
166 tileMap = isl_basic_map_add_constraint(tileMap, c);
167
168 // pX <= tX + (tileSize - 1)
169 c = isl_inequality_alloc(isl_dim_copy(dim));
170 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
171 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000172 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000173 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000174 }
175
Tobias Grosserde68cc92011-06-30 20:01:02 +0000176 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000177 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000178 // The auxilary dimensions are transformed into existentially quantified ones.
179 // This reduces the number of visible scattering dimensions and allows Cloog
180 // to produces better code.
181 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
182 2 * scheduleDimensions,
183 scheduleDimensions);
184 isl_dim_free(dim);
185 return tileMap;
186}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000187
Tobias Grosserde68cc92011-06-30 20:01:02 +0000188isl_union_map *getTiledPartialSchedule(isl_band *band) {
189 isl_union_map *partialSchedule;
190 int scheduleDimensions, parameterDimensions;
191 isl_ctx *ctx;
192 isl_dim *dim;
193 isl_basic_map *tileMap;
194 isl_union_map *tileUnionMap;
195
196 partialSchedule = isl_band_get_partial_schedule(band);
197 ctx = isl_union_map_get_ctx(partialSchedule);
198 dim = isl_union_map_get_dim(partialSchedule);
199 scheduleDimensions = isl_band_n_member(band);
200 parameterDimensions = isl_dim_size(dim, isl_dim_param);
201
202 tileMap = getTileMap(ctx, scheduleDimensions, parameterDimensions);
203 tileUnionMap = isl_union_map_from_map(isl_map_from_basic_map(tileMap));
204
205 partialSchedule = isl_union_map_apply_range(partialSchedule, tileUnionMap);
206
207 isl_dim_free(dim);
208 isl_ctx_free(ctx);
209
210 return partialSchedule;
211}
212
Tobias Grosserc6699b72011-06-30 20:29:13 +0000213static isl_map *getPrevectorMap(isl_ctx *ctx, int vectorDimension,
214 int scheduleDimensions,
215 int parameterDimensions,
216 int vectorWidth = 4) {
217 assert (0 <= vectorDimension < scheduleDimensions);
218
219 isl_dim *dim = isl_dim_alloc(ctx, parameterDimensions, scheduleDimensions,
220 scheduleDimensions + 2);
221 isl_basic_map *tilingMap = isl_basic_map_universe(isl_dim_copy(dim));
222
223 isl_constraint *c;
224
225 for (int i = 0; i < vectorDimension; i++) {
226 c = isl_equality_alloc(isl_dim_copy(dim));
227 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
228 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
229 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
230 }
231
232 for (int i = vectorDimension + 1; i < scheduleDimensions; i++) {
233 c = isl_equality_alloc(isl_dim_copy(dim));
234 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
235 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
236 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
237 }
238
239 int stepDimension = scheduleDimensions;
240 int auxilaryDimension = scheduleDimensions + 1;
241
242 c = isl_equality_alloc(isl_dim_copy(dim));
243 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
244 isl_constraint_set_coefficient_si(c, isl_dim_out, auxilaryDimension,
245 -vectorWidth);
246 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
247
248 c = isl_equality_alloc(isl_dim_copy(dim));
249 isl_constraint_set_coefficient_si(c, isl_dim_in, vectorDimension, -1);
250 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
251 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
252
253 c = isl_inequality_alloc(isl_dim_copy(dim));
254 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, -1);
255 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
256 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
257
258 c = isl_inequality_alloc(isl_dim_copy(dim));
259 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
260 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, -1);
261 isl_constraint_set_constant_si(c, vectorWidth- 1);
262 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
263
264 // Project out auxilary dimensions (introduced to ensure 'ii % tileSize = 0')
265 //
266 // The real dimensions are transformed into existentially quantified ones.
267 // This reduces the number of visible scattering dimensions. Also, Cloog
268 // produces better code, if auxilary dimensions are existentially quantified.
269 tilingMap = isl_basic_map_project_out(tilingMap, isl_dim_out,
270 scheduleDimensions + 1, 1);
271
272 return isl_map_from_basic_map(tilingMap);
273}
274
Tobias Grosserde68cc92011-06-30 20:01:02 +0000275// tileBandList - Tile all bands contained in a band forest.
276//
277// Recursively walk the band forest and tile all bands in the forest. Return
278// a schedule that describes the tiled scattering.
279static isl_union_map *tileBandList(isl_band_list *blist) {
280 int numBands = isl_band_list_n_band(blist);
281
282 isl_union_map *finalSchedule = 0;
283
284 for (int i = 0; i < numBands; i++) {
285 isl_band *band;
286 isl_union_map *partialSchedule;
287 band = isl_band_list_get_band(blist, i);
288 partialSchedule = getTiledPartialSchedule(band);
289
290 if (isl_band_has_children(band)) {
291 isl_band_list *children = isl_band_get_children(band);
292 isl_union_map *suffixSchedule = tileBandList(children);
293 partialSchedule = isl_union_map_flat_range_product(partialSchedule,
294 suffixSchedule);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000295 } else if (Prevector) {
296 isl_map *tileMap;
297 isl_union_map *tileUnionMap;
298 isl_ctx *ctx;
299 int scheduleDimensions, parameterDimensions;
300
301 ctx = isl_union_map_get_ctx(partialSchedule);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000302 for (int i = scheduleDimensions - 1 ; i >= 0 ; i--) {
303 if (isl_band_member_is_parallel(band, i)) {
304 tileMap = getPrevectorMap(ctx, scheduleDimensions + i,
305 scheduleDimensions * 2,
306 parameterDimensions);
307 tileUnionMap = isl_union_map_from_map(tileMap);
308 partialSchedule = isl_union_map_apply_range(partialSchedule,
309 tileUnionMap);
310 break;
311 }
312 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000313 }
314
315 if (finalSchedule)
316 isl_union_map_union(finalSchedule, partialSchedule);
317 else
318 finalSchedule = partialSchedule;
319
320 isl_band_free(band);
321 }
322
323 return finalSchedule;
324}
325
326static isl_union_map *tileSchedule(isl_schedule *schedule) {
327 isl_band_list *blist = isl_schedule_get_band_forest(schedule);
328 isl_union_map *tiledSchedule = tileBandList(blist);
329 isl_band_list_free(blist);
330 return tiledSchedule;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000331}
332
333bool ScheduleOptimizer::runOnScop(Scop &S) {
334 Dependences *D = &getAnalysis<Dependences>();
335
336 // Build input data.
337 int dependencyKinds = Dependences::TYPE_RAW
338 | Dependences::TYPE_WAR
339 | Dependences::TYPE_WAW;
340
341 isl_union_map *validity = D->getDependences(dependencyKinds);
342 isl_union_map *proximity = D->getDependences(dependencyKinds);
343 isl_union_set *domain = NULL;
344
345 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
346 if ((*SI)->isFinalRead())
347 continue;
348 else if (!domain)
349 domain = isl_union_set_from_set((*SI)->getDomain());
350 else
351 domain = isl_union_set_union(domain,
352 isl_union_set_from_set((*SI)->getDomain()));
353
354 if (!domain)
355 return false;
356
357 DEBUG(dbgs() << "\n\nCompute schedule from: ");
358 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
359 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
360 dbgs() << ";\n");
361 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
362 dbgs() << ";\n");
363
364 isl_schedule *schedule;
365
366 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
367
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000368 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000369 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000370 DEBUG(dbgs() << "Individual bands: ");
371
Tobias Grosserde68cc92011-06-30 20:01:02 +0000372 isl_union_map *tiledSchedule = tileSchedule(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000373
Tobias Grosserde68cc92011-06-30 20:01:02 +0000374 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
375 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000376
Tobias Grosserde68cc92011-06-30 20:01:02 +0000377 if (stmt->isFinalRead())
378 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000379
Tobias Grosserde68cc92011-06-30 20:01:02 +0000380 isl_set *domain = stmt->getDomain();
381 isl_union_map *stmtBand;
382 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(tiledSchedule),
383 isl_union_set_from_set(domain));
384 isl_map *stmtSchedule;
385 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
386 stmt->setScattering(stmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000387 }
388
Tobias Grosserde68cc92011-06-30 20:01:02 +0000389 isl_union_map_free(tiledSchedule);
390 isl_schedule_free(schedule);
391
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000392 unsigned maxScatDims = 0;
393
394 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
395 maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims);
396
397 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000398 return false;
399}
400
401void ScheduleOptimizer::printScop(raw_ostream &OS) const {
402}
403
404void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
405 ScopPass::getAnalysisUsage(AU);
406 AU.addRequired<Dependences>();
407}
408
409static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl",
410 "Polly - Calculate optimized "
411 "schedules using the isl schedule "
412 "calculator");
413
414Pass* polly::createScheduleOptimizerPass() {
415 return new ScheduleOptimizer();
416}