blob: 4bc5db334d03d1ccd8df6320543de2ca36ca70b6 [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),
Tobias Grosserc532f122011-08-25 08:40:59 +000079 0, isl_map_n_out(scattering),
Tobias Grosser30aa24c2011-05-14 19:02:06 +000080 scatDimensions);
81 isl_basic_map *changeScattering = isl_basic_map_universe(isl_dim_copy(dim));
82
83 for (unsigned i = 0; i < isl_map_n_out(scattering); i++) {
84 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
85 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
86 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
87 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
88 }
89
90 for (unsigned i = isl_map_n_out(scattering); i < scatDimensions; i++) {
91 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
92 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
93 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
94 }
95
96 isl_map *changeScatteringMap = isl_map_from_basic_map(changeScattering);
97
Tobias Grosserc532f122011-08-25 08:40:59 +000098 isl_dim *dimModel = isl_map_get_dim(scattering);
99 changeScatteringMap = isl_map_align_params(changeScatteringMap, dimModel);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000100 stmt->setScattering(isl_map_apply_range(scattering, changeScatteringMap));
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000101 isl_dim_free(dim);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000102 }
103}
104
Tobias Grosserde68cc92011-06-30 20:01:02 +0000105// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000106//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000107// getTileMap creates a map from a n-dimensional scattering space into an
108// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000109//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000110// Example:
111// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
112//
113// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
114// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
115// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
116//
117// Before tiling:
118//
119// for (i = 0; i < N; i++)
120// for (j = 0; j < M; j++)
121// S(i,j)
122//
123// After tiling:
124//
125// for (t_i = 0; t_i < N; i+=32)
126// for (t_j = 0; t_j < M; j+=32)
127// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
128// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
129// S(i,j)
130//
131static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserc532f122011-08-25 08:40:59 +0000132 isl_dim *dimModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000133 // We construct
134 //
135 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
136 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
137 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
138 //
139 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserc532f122011-08-25 08:40:59 +0000140 isl_dim *dim = isl_dim_alloc(ctx, 0, scheduleDimensions,
141 scheduleDimensions * 3);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000142 isl_basic_map *tileMap = isl_basic_map_universe(isl_dim_copy(dim));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000143
Tobias Grosserde68cc92011-06-30 20:01:02 +0000144 for (int x = 0; x < scheduleDimensions; x++) {
145 int sX = x;
146 int tX = x;
147 int pX = scheduleDimensions + x;
148 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000149
Tobias Grosserde68cc92011-06-30 20:01:02 +0000150 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000151
Tobias Grosserde68cc92011-06-30 20:01:02 +0000152 // sX = aX * tileSize;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000153 c = isl_equality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000154 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
155 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
156 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000157
Tobias Grosserde68cc92011-06-30 20:01:02 +0000158 // pX = sX;
159 c = isl_equality_alloc(isl_dim_copy(dim));
160 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
161 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
162 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000163
Tobias Grosserde68cc92011-06-30 20:01:02 +0000164 // tX <= pX
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000165 c = isl_inequality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000166 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
167 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
168 tileMap = isl_basic_map_add_constraint(tileMap, c);
169
170 // pX <= tX + (tileSize - 1)
171 c = isl_inequality_alloc(isl_dim_copy(dim));
172 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
173 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000174 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000175 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000176 }
177
Tobias Grosserde68cc92011-06-30 20:01:02 +0000178 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000179 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000180 // The auxilary dimensions are transformed into existentially quantified ones.
181 // This reduces the number of visible scattering dimensions and allows Cloog
182 // to produces better code.
183 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
184 2 * scheduleDimensions,
185 scheduleDimensions);
186 isl_dim_free(dim);
187 return tileMap;
188}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000189
Tobias Grosserde68cc92011-06-30 20:01:02 +0000190isl_union_map *getTiledPartialSchedule(isl_band *band) {
191 isl_union_map *partialSchedule;
Tobias Grosserc532f122011-08-25 08:40:59 +0000192 int scheduleDimensions;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000193 isl_ctx *ctx;
194 isl_dim *dim;
195 isl_basic_map *tileMap;
196 isl_union_map *tileUnionMap;
197
198 partialSchedule = isl_band_get_partial_schedule(band);
199 ctx = isl_union_map_get_ctx(partialSchedule);
200 dim = isl_union_map_get_dim(partialSchedule);
201 scheduleDimensions = isl_band_n_member(band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000202
Tobias Grosserc532f122011-08-25 08:40:59 +0000203 tileMap = getTileMap(ctx, scheduleDimensions, dim);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000204 tileUnionMap = isl_union_map_from_map(isl_map_from_basic_map(tileMap));
Tobias Grosserc532f122011-08-25 08:40:59 +0000205 tileUnionMap = isl_union_map_align_params(tileUnionMap, dim);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000206 partialSchedule = isl_union_map_apply_range(partialSchedule, tileUnionMap);
207
Tobias Grosserde68cc92011-06-30 20:01:02 +0000208 return partialSchedule;
209}
210
Tobias Grosserc6699b72011-06-30 20:29:13 +0000211static isl_map *getPrevectorMap(isl_ctx *ctx, int vectorDimension,
212 int scheduleDimensions,
213 int parameterDimensions,
214 int vectorWidth = 4) {
Tobias Grosser2bd3af12011-08-01 22:39:00 +0000215 assert (0 <= vectorDimension && vectorDimension < scheduleDimensions);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000216
217 isl_dim *dim = isl_dim_alloc(ctx, parameterDimensions, scheduleDimensions,
218 scheduleDimensions + 2);
219 isl_basic_map *tilingMap = isl_basic_map_universe(isl_dim_copy(dim));
220
221 isl_constraint *c;
222
223 for (int i = 0; i < vectorDimension; i++) {
224 c = isl_equality_alloc(isl_dim_copy(dim));
225 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
226 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
227 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
228 }
229
230 for (int i = vectorDimension + 1; i < scheduleDimensions; i++) {
231 c = isl_equality_alloc(isl_dim_copy(dim));
232 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
233 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
234 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
235 }
236
237 int stepDimension = scheduleDimensions;
238 int auxilaryDimension = scheduleDimensions + 1;
239
240 c = isl_equality_alloc(isl_dim_copy(dim));
241 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
242 isl_constraint_set_coefficient_si(c, isl_dim_out, auxilaryDimension,
243 -vectorWidth);
244 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
245
246 c = isl_equality_alloc(isl_dim_copy(dim));
247 isl_constraint_set_coefficient_si(c, isl_dim_in, vectorDimension, -1);
248 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
249 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
250
251 c = isl_inequality_alloc(isl_dim_copy(dim));
252 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, -1);
253 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
254 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
255
Tobias Grosserb406d222011-08-25 08:40:52 +0000256 c = isl_inequality_alloc(dim);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000257 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
258 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, -1);
259 isl_constraint_set_constant_si(c, vectorWidth- 1);
260 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
261
262 // Project out auxilary dimensions (introduced to ensure 'ii % tileSize = 0')
263 //
264 // The real dimensions are transformed into existentially quantified ones.
265 // This reduces the number of visible scattering dimensions. Also, Cloog
266 // produces better code, if auxilary dimensions are existentially quantified.
267 tilingMap = isl_basic_map_project_out(tilingMap, isl_dim_out,
268 scheduleDimensions + 1, 1);
269
270 return isl_map_from_basic_map(tilingMap);
271}
272
Tobias Grosserde68cc92011-06-30 20:01:02 +0000273// tileBandList - Tile all bands contained in a band forest.
274//
275// Recursively walk the band forest and tile all bands in the forest. Return
276// a schedule that describes the tiled scattering.
277static isl_union_map *tileBandList(isl_band_list *blist) {
278 int numBands = isl_band_list_n_band(blist);
279
280 isl_union_map *finalSchedule = 0;
281
282 for (int i = 0; i < numBands; i++) {
283 isl_band *band;
284 isl_union_map *partialSchedule;
285 band = isl_band_list_get_band(blist, i);
286 partialSchedule = getTiledPartialSchedule(band);
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000287 int scheduleDimensions = isl_band_n_member(band);
288 isl_dim *dim = isl_union_map_get_dim(partialSchedule);
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000289
Tobias Grosserde68cc92011-06-30 20:01:02 +0000290
291 if (isl_band_has_children(band)) {
292 isl_band_list *children = isl_band_get_children(band);
293 isl_union_map *suffixSchedule = tileBandList(children);
294 partialSchedule = isl_union_map_flat_range_product(partialSchedule,
295 suffixSchedule);
Tobias Grossera9542422011-08-23 22:35:23 +0000296 isl_band_list_free(children);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000297 } else if (Prevector) {
298 isl_map *tileMap;
299 isl_union_map *tileUnionMap;
300 isl_ctx *ctx;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000301
302 ctx = isl_union_map_get_ctx(partialSchedule);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000303 for (int i = scheduleDimensions - 1 ; i >= 0 ; i--) {
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000304 if (isl_band_member_is_zero_distance(band, i)) {
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000305 tileMap = getPrevectorMap(ctx, scheduleDimensions + i,
Tobias Grosserc532f122011-08-25 08:40:59 +0000306 scheduleDimensions * 2, 0);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000307 tileUnionMap = isl_union_map_from_map(tileMap);
Tobias Grosserc532f122011-08-25 08:40:59 +0000308 tileUnionMap = isl_union_map_align_params(tileUnionMap,
309 isl_dim_copy(dim));
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000310 partialSchedule = isl_union_map_apply_range(partialSchedule,
311 tileUnionMap);
312 break;
313 }
314 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000315 }
316
317 if (finalSchedule)
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000318 finalSchedule = isl_union_map_union(finalSchedule, partialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000319 else
320 finalSchedule = partialSchedule;
321
322 isl_band_free(band);
Tobias Grosserc532f122011-08-25 08:40:59 +0000323 isl_dim_free(dim);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000324 }
325
326 return finalSchedule;
327}
328
329static isl_union_map *tileSchedule(isl_schedule *schedule) {
330 isl_band_list *blist = isl_schedule_get_band_forest(schedule);
331 isl_union_map *tiledSchedule = tileBandList(blist);
332 isl_band_list_free(blist);
333 return tiledSchedule;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000334}
335
336bool ScheduleOptimizer::runOnScop(Scop &S) {
337 Dependences *D = &getAnalysis<Dependences>();
338
339 // Build input data.
340 int dependencyKinds = Dependences::TYPE_RAW
341 | Dependences::TYPE_WAR
342 | Dependences::TYPE_WAW;
343
344 isl_union_map *validity = D->getDependences(dependencyKinds);
345 isl_union_map *proximity = D->getDependences(dependencyKinds);
346 isl_union_set *domain = NULL;
347
348 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
349 if ((*SI)->isFinalRead())
350 continue;
351 else if (!domain)
352 domain = isl_union_set_from_set((*SI)->getDomain());
353 else
354 domain = isl_union_set_union(domain,
355 isl_union_set_from_set((*SI)->getDomain()));
356
357 if (!domain)
358 return false;
359
360 DEBUG(dbgs() << "\n\nCompute schedule from: ");
361 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
362 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
363 dbgs() << ";\n");
364 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
365 dbgs() << ";\n");
366
367 isl_schedule *schedule;
368
369 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
370
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000371 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000372 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000373 DEBUG(dbgs() << "Individual bands: ");
374
Tobias Grosserde68cc92011-06-30 20:01:02 +0000375 isl_union_map *tiledSchedule = tileSchedule(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000376
Tobias Grosserde68cc92011-06-30 20:01:02 +0000377 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
378 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000379
Tobias Grosserde68cc92011-06-30 20:01:02 +0000380 if (stmt->isFinalRead())
381 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000382
Tobias Grosserde68cc92011-06-30 20:01:02 +0000383 isl_set *domain = stmt->getDomain();
384 isl_union_map *stmtBand;
385 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(tiledSchedule),
386 isl_union_set_from_set(domain));
387 isl_map *stmtSchedule;
388 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000389 stmt->setScattering(isl_map_copy(stmtSchedule));
390 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000391 }
392
Tobias Grosserde68cc92011-06-30 20:01:02 +0000393 isl_union_map_free(tiledSchedule);
394 isl_schedule_free(schedule);
395
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000396 unsigned maxScatDims = 0;
397
398 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
399 maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims);
400
401 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000402 return false;
403}
404
405void ScheduleOptimizer::printScop(raw_ostream &OS) const {
406}
407
408void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
409 ScopPass::getAnalysisUsage(AU);
410 AU.addRequired<Dependences>();
411}
412
413static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl",
414 "Polly - Calculate optimized "
415 "schedules using the isl schedule "
416 "calculator");
417
418Pass* polly::createScheduleOptimizerPass() {
419 return new ScheduleOptimizer();
420}