blob: c9982cdbbf2c1a642cd6a284a54f95a19fe13397 [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);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000208
209 return partialSchedule;
210}
211
Tobias Grosserc6699b72011-06-30 20:29:13 +0000212static isl_map *getPrevectorMap(isl_ctx *ctx, int vectorDimension,
213 int scheduleDimensions,
214 int parameterDimensions,
215 int vectorWidth = 4) {
216 assert (0 <= vectorDimension < scheduleDimensions);
217
218 isl_dim *dim = isl_dim_alloc(ctx, parameterDimensions, scheduleDimensions,
219 scheduleDimensions + 2);
220 isl_basic_map *tilingMap = isl_basic_map_universe(isl_dim_copy(dim));
221
222 isl_constraint *c;
223
224 for (int i = 0; i < vectorDimension; i++) {
225 c = isl_equality_alloc(isl_dim_copy(dim));
226 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
227 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
228 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
229 }
230
231 for (int i = vectorDimension + 1; i < scheduleDimensions; i++) {
232 c = isl_equality_alloc(isl_dim_copy(dim));
233 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
234 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
235 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
236 }
237
238 int stepDimension = scheduleDimensions;
239 int auxilaryDimension = scheduleDimensions + 1;
240
241 c = isl_equality_alloc(isl_dim_copy(dim));
242 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
243 isl_constraint_set_coefficient_si(c, isl_dim_out, auxilaryDimension,
244 -vectorWidth);
245 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
246
247 c = isl_equality_alloc(isl_dim_copy(dim));
248 isl_constraint_set_coefficient_si(c, isl_dim_in, vectorDimension, -1);
249 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
250 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
251
252 c = isl_inequality_alloc(isl_dim_copy(dim));
253 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, -1);
254 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
255 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
256
257 c = isl_inequality_alloc(isl_dim_copy(dim));
258 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
259 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, -1);
260 isl_constraint_set_constant_si(c, vectorWidth- 1);
261 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
262
263 // Project out auxilary dimensions (introduced to ensure 'ii % tileSize = 0')
264 //
265 // The real dimensions are transformed into existentially quantified ones.
266 // This reduces the number of visible scattering dimensions. Also, Cloog
267 // produces better code, if auxilary dimensions are existentially quantified.
268 tilingMap = isl_basic_map_project_out(tilingMap, isl_dim_out,
269 scheduleDimensions + 1, 1);
270
271 return isl_map_from_basic_map(tilingMap);
272}
273
Tobias Grosserde68cc92011-06-30 20:01:02 +0000274// tileBandList - Tile all bands contained in a band forest.
275//
276// Recursively walk the band forest and tile all bands in the forest. Return
277// a schedule that describes the tiled scattering.
278static isl_union_map *tileBandList(isl_band_list *blist) {
279 int numBands = isl_band_list_n_band(blist);
280
281 isl_union_map *finalSchedule = 0;
282
283 for (int i = 0; i < numBands; i++) {
284 isl_band *band;
285 isl_union_map *partialSchedule;
286 band = isl_band_list_get_band(blist, i);
287 partialSchedule = getTiledPartialSchedule(band);
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000288 int scheduleDimensions = isl_band_n_member(band);
289 isl_dim *dim = isl_union_map_get_dim(partialSchedule);
290 int parameterDimensions = isl_dim_size(dim, isl_dim_param);
291 isl_dim_free(dim);
292
Tobias Grosserde68cc92011-06-30 20:01:02 +0000293
294 if (isl_band_has_children(band)) {
295 isl_band_list *children = isl_band_get_children(band);
296 isl_union_map *suffixSchedule = tileBandList(children);
297 partialSchedule = isl_union_map_flat_range_product(partialSchedule,
298 suffixSchedule);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000299 } else if (Prevector) {
300 isl_map *tileMap;
301 isl_union_map *tileUnionMap;
302 isl_ctx *ctx;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000303
304 ctx = isl_union_map_get_ctx(partialSchedule);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000305 for (int i = scheduleDimensions - 1 ; i >= 0 ; i--) {
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000306 if (isl_band_member_is_zero_distance(band, i)) {
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000307 tileMap = getPrevectorMap(ctx, scheduleDimensions + i,
308 scheduleDimensions * 2,
309 parameterDimensions);
310 tileUnionMap = isl_union_map_from_map(tileMap);
311 partialSchedule = isl_union_map_apply_range(partialSchedule,
312 tileUnionMap);
313 break;
314 }
315 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000316 }
317
318 if (finalSchedule)
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000319 finalSchedule = isl_union_map_union(finalSchedule, partialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000320 else
321 finalSchedule = partialSchedule;
322
323 isl_band_free(band);
324 }
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);
389 stmt->setScattering(stmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000390 }
391
Tobias Grosserde68cc92011-06-30 20:01:02 +0000392 isl_union_map_free(tiledSchedule);
393 isl_schedule_free(schedule);
394
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000395 unsigned maxScatDims = 0;
396
397 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
398 maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims);
399
400 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000401 return false;
402}
403
404void ScheduleOptimizer::printScop(raw_ostream &OS) const {
405}
406
407void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
408 ScopPass::getAnalysisUsage(AU);
409 AU.addRequired<Dependences>();
410}
411
412static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl",
413 "Polly - Calculate optimized "
414 "schedules using the isl schedule "
415 "calculator");
416
417Pass* polly::createScheduleOptimizerPass() {
418 return new ScheduleOptimizer();
419}