blob: 48bbe530ba4fd4cc4e081bbca6d7a8e5493c9726 [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));
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000100 isl_dim_free(dim);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000101 }
102}
103
Tobias Grosserde68cc92011-06-30 20:01:02 +0000104// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000105//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000106// getTileMap creates a map from a n-dimensional scattering space into an
107// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000108//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000109// Example:
110// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
111//
112// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
113// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
114// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
115//
116// Before tiling:
117//
118// for (i = 0; i < N; i++)
119// for (j = 0; j < M; j++)
120// S(i,j)
121//
122// After tiling:
123//
124// for (t_i = 0; t_i < N; i+=32)
125// for (t_j = 0; t_j < M; j+=32)
126// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
127// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
128// S(i,j)
129//
130static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
131 int parameterDimensions, int tileSize = 32) {
132 // We construct
133 //
134 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
135 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
136 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
137 //
138 // and project out the auxilary dimensions a0 and a1.
139 isl_dim *dim = isl_dim_alloc(ctx, parameterDimensions, scheduleDimensions,
140 scheduleDimensions * 3);
141 isl_basic_map *tileMap = isl_basic_map_universe(isl_dim_copy(dim));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000142
Tobias Grosserde68cc92011-06-30 20:01:02 +0000143 for (int x = 0; x < scheduleDimensions; x++) {
144 int sX = x;
145 int tX = x;
146 int pX = scheduleDimensions + x;
147 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000148
Tobias Grosserde68cc92011-06-30 20:01:02 +0000149 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000150
Tobias Grosserde68cc92011-06-30 20:01:02 +0000151 // sX = aX * tileSize;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000152 c = isl_equality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000153 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
154 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
155 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000156
Tobias Grosserde68cc92011-06-30 20:01:02 +0000157 // pX = sX;
158 c = isl_equality_alloc(isl_dim_copy(dim));
159 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
160 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
161 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000162
Tobias Grosserde68cc92011-06-30 20:01:02 +0000163 // tX <= pX
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000164 c = isl_inequality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000165 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
166 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
167 tileMap = isl_basic_map_add_constraint(tileMap, c);
168
169 // pX <= tX + (tileSize - 1)
170 c = isl_inequality_alloc(isl_dim_copy(dim));
171 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
172 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000173 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000174 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000175 }
176
Tobias Grosserde68cc92011-06-30 20:01:02 +0000177 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000178 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000179 // The auxilary dimensions are transformed into existentially quantified ones.
180 // This reduces the number of visible scattering dimensions and allows Cloog
181 // to produces better code.
182 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
183 2 * scheduleDimensions,
184 scheduleDimensions);
185 isl_dim_free(dim);
186 return tileMap;
187}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000188
Tobias Grosserde68cc92011-06-30 20:01:02 +0000189isl_union_map *getTiledPartialSchedule(isl_band *band) {
190 isl_union_map *partialSchedule;
191 int scheduleDimensions, parameterDimensions;
192 isl_ctx *ctx;
193 isl_dim *dim;
194 isl_basic_map *tileMap;
195 isl_union_map *tileUnionMap;
196
197 partialSchedule = isl_band_get_partial_schedule(band);
198 ctx = isl_union_map_get_ctx(partialSchedule);
199 dim = isl_union_map_get_dim(partialSchedule);
200 scheduleDimensions = isl_band_n_member(band);
201 parameterDimensions = isl_dim_size(dim, isl_dim_param);
202
203 tileMap = getTileMap(ctx, scheduleDimensions, parameterDimensions);
204 tileUnionMap = isl_union_map_from_map(isl_map_from_basic_map(tileMap));
205
206 partialSchedule = isl_union_map_apply_range(partialSchedule, tileUnionMap);
207
208 isl_dim_free(dim);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000209
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) {
Tobias Grosser2bd3af12011-08-01 22:39:00 +0000217 assert (0 <= vectorDimension && vectorDimension < scheduleDimensions);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000218
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);
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000289 int scheduleDimensions = isl_band_n_member(band);
290 isl_dim *dim = isl_union_map_get_dim(partialSchedule);
291 int parameterDimensions = isl_dim_size(dim, isl_dim_param);
292 isl_dim_free(dim);
293
Tobias Grosserde68cc92011-06-30 20:01:02 +0000294
295 if (isl_band_has_children(band)) {
296 isl_band_list *children = isl_band_get_children(band);
297 isl_union_map *suffixSchedule = tileBandList(children);
298 partialSchedule = isl_union_map_flat_range_product(partialSchedule,
299 suffixSchedule);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000300 } else if (Prevector) {
301 isl_map *tileMap;
302 isl_union_map *tileUnionMap;
303 isl_ctx *ctx;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000304
305 ctx = isl_union_map_get_ctx(partialSchedule);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000306 for (int i = scheduleDimensions - 1 ; i >= 0 ; i--) {
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000307 if (isl_band_member_is_zero_distance(band, i)) {
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000308 tileMap = getPrevectorMap(ctx, scheduleDimensions + i,
309 scheduleDimensions * 2,
310 parameterDimensions);
311 tileUnionMap = isl_union_map_from_map(tileMap);
312 partialSchedule = isl_union_map_apply_range(partialSchedule,
313 tileUnionMap);
314 break;
315 }
316 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000317 }
318
319 if (finalSchedule)
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000320 finalSchedule = isl_union_map_union(finalSchedule, partialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000321 else
322 finalSchedule = partialSchedule;
323
324 isl_band_free(band);
325 }
326
327 return finalSchedule;
328}
329
330static isl_union_map *tileSchedule(isl_schedule *schedule) {
331 isl_band_list *blist = isl_schedule_get_band_forest(schedule);
332 isl_union_map *tiledSchedule = tileBandList(blist);
333 isl_band_list_free(blist);
334 return tiledSchedule;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000335}
336
337bool ScheduleOptimizer::runOnScop(Scop &S) {
338 Dependences *D = &getAnalysis<Dependences>();
339
340 // Build input data.
341 int dependencyKinds = Dependences::TYPE_RAW
342 | Dependences::TYPE_WAR
343 | Dependences::TYPE_WAW;
344
345 isl_union_map *validity = D->getDependences(dependencyKinds);
346 isl_union_map *proximity = D->getDependences(dependencyKinds);
347 isl_union_set *domain = NULL;
348
349 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
350 if ((*SI)->isFinalRead())
351 continue;
352 else if (!domain)
353 domain = isl_union_set_from_set((*SI)->getDomain());
354 else
355 domain = isl_union_set_union(domain,
356 isl_union_set_from_set((*SI)->getDomain()));
357
358 if (!domain)
359 return false;
360
361 DEBUG(dbgs() << "\n\nCompute schedule from: ");
362 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
363 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
364 dbgs() << ";\n");
365 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
366 dbgs() << ";\n");
367
368 isl_schedule *schedule;
369
370 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
371
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000372 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000373 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000374 DEBUG(dbgs() << "Individual bands: ");
375
Tobias Grosserde68cc92011-06-30 20:01:02 +0000376 isl_union_map *tiledSchedule = tileSchedule(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000377
Tobias Grosserde68cc92011-06-30 20:01:02 +0000378 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
379 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000380
Tobias Grosserde68cc92011-06-30 20:01:02 +0000381 if (stmt->isFinalRead())
382 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000383
Tobias Grosserde68cc92011-06-30 20:01:02 +0000384 isl_set *domain = stmt->getDomain();
385 isl_union_map *stmtBand;
386 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(tiledSchedule),
387 isl_union_set_from_set(domain));
388 isl_map *stmtSchedule;
389 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000390 stmt->setScattering(isl_map_copy(stmtSchedule));
391 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000392 }
393
Tobias Grosserde68cc92011-06-30 20:01:02 +0000394 isl_union_map_free(tiledSchedule);
395 isl_schedule_free(schedule);
396
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000397 unsigned maxScatDims = 0;
398
399 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
400 maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims);
401
402 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000403 return false;
404}
405
406void ScheduleOptimizer::printScop(raw_ostream &OS) const {
407}
408
409void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
410 ScopPass::getAnalysisUsage(AU);
411 AU.addRequired<Dependences>();
412}
413
414static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl",
415 "Polly - Calculate optimized "
416 "schedules using the isl schedule "
417 "calculator");
418
419Pass* polly::createScheduleOptimizerPass() {
420 return new ScheduleOptimizer();
421}