blob: 0fb4565c1409dc4692e30967754c46a9fb1e7167 [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"
35
36using namespace llvm;
37using namespace polly;
38
39namespace {
40
41 class ScheduleOptimizer : public ScopPass {
42
43 public:
44 static char ID;
45 explicit ScheduleOptimizer() : ScopPass(ID) {}
46
47 virtual bool runOnScop(Scop &S);
48 void printScop(llvm::raw_ostream &OS) const;
49 void getAnalysisUsage(AnalysisUsage &AU) const;
50 };
51
52}
53
54char ScheduleOptimizer::ID = 0;
55
56static int getSingleMap(__isl_take isl_map *map, void *user) {
57 isl_map **singleMap = (isl_map **) user;
58 *singleMap = map;
59
60 return 0;
61}
62
Tobias Grosser76747f72011-05-24 12:20:07 +000063static void extendScattering(Scop &S, unsigned scatDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000064 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
65 ScopStmt *stmt = *SI;
66
67 if (stmt->isFinalRead())
68 continue;
69
70 isl_map *scattering = stmt->getScattering();
71 isl_dim *dim = isl_dim_alloc(isl_map_get_ctx(scattering),
72 isl_map_n_param(scattering),
73 isl_map_n_out(scattering),
74 scatDimensions);
75 isl_basic_map *changeScattering = isl_basic_map_universe(isl_dim_copy(dim));
76
77 for (unsigned i = 0; i < isl_map_n_out(scattering); i++) {
78 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
79 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
80 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
81 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
82 }
83
84 for (unsigned i = isl_map_n_out(scattering); i < scatDimensions; i++) {
85 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
86 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
87 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
88 }
89
90 isl_map *changeScatteringMap = isl_map_from_basic_map(changeScattering);
91
92 stmt->setScattering(isl_map_apply_range(scattering, changeScatteringMap));
93 }
94}
95
Tobias Grosserde68cc92011-06-30 20:01:02 +000096// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +000097//
Tobias Grosserde68cc92011-06-30 20:01:02 +000098// getTileMap creates a map from a n-dimensional scattering space into an
99// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000100//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000101// Example:
102// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
103//
104// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
105// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
106// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
107//
108// Before tiling:
109//
110// for (i = 0; i < N; i++)
111// for (j = 0; j < M; j++)
112// S(i,j)
113//
114// After tiling:
115//
116// for (t_i = 0; t_i < N; i+=32)
117// for (t_j = 0; t_j < M; j+=32)
118// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
119// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
120// S(i,j)
121//
122static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
123 int parameterDimensions, int tileSize = 32) {
124 // We construct
125 //
126 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
127 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
128 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
129 //
130 // and project out the auxilary dimensions a0 and a1.
131 isl_dim *dim = isl_dim_alloc(ctx, parameterDimensions, scheduleDimensions,
132 scheduleDimensions * 3);
133 isl_basic_map *tileMap = isl_basic_map_universe(isl_dim_copy(dim));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000134
Tobias Grosserde68cc92011-06-30 20:01:02 +0000135 for (int x = 0; x < scheduleDimensions; x++) {
136 int sX = x;
137 int tX = x;
138 int pX = scheduleDimensions + x;
139 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000140
Tobias Grosserde68cc92011-06-30 20:01:02 +0000141 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000142
Tobias Grosserde68cc92011-06-30 20:01:02 +0000143 // sX = aX * tileSize;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000144 c = isl_equality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000145 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
146 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
147 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000148
Tobias Grosserde68cc92011-06-30 20:01:02 +0000149 // pX = sX;
150 c = isl_equality_alloc(isl_dim_copy(dim));
151 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
152 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
153 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000154
Tobias Grosserde68cc92011-06-30 20:01:02 +0000155 // tX <= pX
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000156 c = isl_inequality_alloc(isl_dim_copy(dim));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000157 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
158 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
159 tileMap = isl_basic_map_add_constraint(tileMap, c);
160
161 // pX <= tX + (tileSize - 1)
162 c = isl_inequality_alloc(isl_dim_copy(dim));
163 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
164 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000165 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000166 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000167 }
168
Tobias Grosserde68cc92011-06-30 20:01:02 +0000169 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000170 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000171 // The auxilary dimensions are transformed into existentially quantified ones.
172 // This reduces the number of visible scattering dimensions and allows Cloog
173 // to produces better code.
174 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
175 2 * scheduleDimensions,
176 scheduleDimensions);
177 isl_dim_free(dim);
178 return tileMap;
179}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000180
Tobias Grosserde68cc92011-06-30 20:01:02 +0000181isl_union_map *getTiledPartialSchedule(isl_band *band) {
182 isl_union_map *partialSchedule;
183 int scheduleDimensions, parameterDimensions;
184 isl_ctx *ctx;
185 isl_dim *dim;
186 isl_basic_map *tileMap;
187 isl_union_map *tileUnionMap;
188
189 partialSchedule = isl_band_get_partial_schedule(band);
190 ctx = isl_union_map_get_ctx(partialSchedule);
191 dim = isl_union_map_get_dim(partialSchedule);
192 scheduleDimensions = isl_band_n_member(band);
193 parameterDimensions = isl_dim_size(dim, isl_dim_param);
194
195 tileMap = getTileMap(ctx, scheduleDimensions, parameterDimensions);
196 tileUnionMap = isl_union_map_from_map(isl_map_from_basic_map(tileMap));
197
198 partialSchedule = isl_union_map_apply_range(partialSchedule, tileUnionMap);
199
200 isl_dim_free(dim);
201 isl_ctx_free(ctx);
202
203 return partialSchedule;
204}
205
206// tileBandList - Tile all bands contained in a band forest.
207//
208// Recursively walk the band forest and tile all bands in the forest. Return
209// a schedule that describes the tiled scattering.
210static isl_union_map *tileBandList(isl_band_list *blist) {
211 int numBands = isl_band_list_n_band(blist);
212
213 isl_union_map *finalSchedule = 0;
214
215 for (int i = 0; i < numBands; i++) {
216 isl_band *band;
217 isl_union_map *partialSchedule;
218 band = isl_band_list_get_band(blist, i);
219 partialSchedule = getTiledPartialSchedule(band);
220
221 if (isl_band_has_children(band)) {
222 isl_band_list *children = isl_band_get_children(band);
223 isl_union_map *suffixSchedule = tileBandList(children);
224 partialSchedule = isl_union_map_flat_range_product(partialSchedule,
225 suffixSchedule);
226 }
227
228 if (finalSchedule)
229 isl_union_map_union(finalSchedule, partialSchedule);
230 else
231 finalSchedule = partialSchedule;
232
233 isl_band_free(band);
234 }
235
236 return finalSchedule;
237}
238
239static isl_union_map *tileSchedule(isl_schedule *schedule) {
240 isl_band_list *blist = isl_schedule_get_band_forest(schedule);
241 isl_union_map *tiledSchedule = tileBandList(blist);
242 isl_band_list_free(blist);
243 return tiledSchedule;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000244}
245
246bool ScheduleOptimizer::runOnScop(Scop &S) {
247 Dependences *D = &getAnalysis<Dependences>();
248
249 // Build input data.
250 int dependencyKinds = Dependences::TYPE_RAW
251 | Dependences::TYPE_WAR
252 | Dependences::TYPE_WAW;
253
254 isl_union_map *validity = D->getDependences(dependencyKinds);
255 isl_union_map *proximity = D->getDependences(dependencyKinds);
256 isl_union_set *domain = NULL;
257
258 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
259 if ((*SI)->isFinalRead())
260 continue;
261 else if (!domain)
262 domain = isl_union_set_from_set((*SI)->getDomain());
263 else
264 domain = isl_union_set_union(domain,
265 isl_union_set_from_set((*SI)->getDomain()));
266
267 if (!domain)
268 return false;
269
270 DEBUG(dbgs() << "\n\nCompute schedule from: ");
271 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
272 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
273 dbgs() << ";\n");
274 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
275 dbgs() << ";\n");
276
277 isl_schedule *schedule;
278
279 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
280
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000281 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000282 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000283 DEBUG(dbgs() << "Individual bands: ");
284
Tobias Grosserde68cc92011-06-30 20:01:02 +0000285 isl_union_map *tiledSchedule = tileSchedule(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000286
Tobias Grosserde68cc92011-06-30 20:01:02 +0000287 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
288 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000289
Tobias Grosserde68cc92011-06-30 20:01:02 +0000290 if (stmt->isFinalRead())
291 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000292
Tobias Grosserde68cc92011-06-30 20:01:02 +0000293 isl_set *domain = stmt->getDomain();
294 isl_union_map *stmtBand;
295 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(tiledSchedule),
296 isl_union_set_from_set(domain));
297 isl_map *stmtSchedule;
298 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
299 stmt->setScattering(stmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000300 }
301
Tobias Grosserde68cc92011-06-30 20:01:02 +0000302 isl_union_map_free(tiledSchedule);
303 isl_schedule_free(schedule);
304
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000305 unsigned maxScatDims = 0;
306
307 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
308 maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims);
309
310 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000311 return false;
312}
313
314void ScheduleOptimizer::printScop(raw_ostream &OS) const {
315}
316
317void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
318 ScopPass::getAnalysisUsage(AU);
319 AU.addRequired<Dependences>();
320}
321
322static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl",
323 "Polly - Calculate optimized "
324 "schedules using the isl schedule "
325 "calculator");
326
327Pass* polly::createScheduleOptimizerPass() {
328 return new ScheduleOptimizer();
329}