blob: d857142f2e54a73534305cd706ebda90fe948932 [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
Tobias Grosser967239c2011-10-23 20:59:44 +000020#include "polly/ScheduleOptimizer.h"
21
Tobias Grosser30aa24c2011-05-14 19:02:06 +000022#include "polly/Cloog.h"
23#include "polly/LinkAllPasses.h"
Tobias Grosser67707b72011-10-23 20:59:40 +000024#include "polly/CodeGeneration.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000025#include "polly/Support/GICHelper.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000026#include "polly/Dependences.h"
27#include "polly/ScopInfo.h"
28
Tobias Grosser2493e922011-12-07 07:42:57 +000029#include "isl/aff.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000030#include "isl/space.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000031#include "isl/map.h"
32#include "isl/constraint.h"
33#include "isl/schedule.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000034#include "isl/band.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000035
Tobias Grosser4dca4392011-11-22 19:40:19 +000036#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000037#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000038#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000039
Tobias Grosser768140c2011-12-14 08:58:39 +000040static const int CONSTANT_BOUND = 20;
41
Tobias Grosser30aa24c2011-05-14 19:02:06 +000042using namespace llvm;
43using namespace polly;
44
Tobias Grosser967239c2011-10-23 20:59:44 +000045namespace polly {
46 bool DisablePollyTiling;
47}
48static cl::opt<bool, true>
Tobias Grosser353a2682011-10-23 20:59:26 +000049DisableTiling("polly-no-tiling",
Tobias Grosser967239c2011-10-23 20:59:44 +000050 cl::desc("Disable tiling in the scheduler"), cl::Hidden,
51 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000052
Tobias Grosser30aa24c2011-05-14 19:02:06 +000053namespace {
54
Tobias Grosser73600b82011-10-08 00:30:40 +000055 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000056
57 public:
58 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000059 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000060
61 virtual bool runOnScop(Scop &S);
62 void printScop(llvm::raw_ostream &OS) const;
63 void getAnalysisUsage(AnalysisUsage &AU) const;
64 };
65
66}
67
Tobias Grosser73600b82011-10-08 00:30:40 +000068char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000069
70static int getSingleMap(__isl_take isl_map *map, void *user) {
71 isl_map **singleMap = (isl_map **) user;
72 *singleMap = map;
73
74 return 0;
75}
76
Tobias Grossercf3942d2011-10-06 00:04:05 +000077static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000078 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +000079 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000080
Tobias Grossercf3942d2011-10-06 00:04:05 +000081 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +000082 continue;
83
Tobias Grossercf3942d2011-10-06 00:04:05 +000084 unsigned OldDimensions = Stmt->getNumScattering();
85 isl_space *Space;
86 isl_basic_map *ChangeScattering;
87
88 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
89 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +000090 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000091
Tobias Grossercf3942d2011-10-06 00:04:05 +000092 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +000093 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000094 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
95 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +000096 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000097 }
98
Tobias Grossercf3942d2011-10-06 00:04:05 +000099 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000100 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000101 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000102 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000103 }
104
Tobias Grossercf3942d2011-10-06 00:04:05 +0000105 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000106
Tobias Grossercf3942d2011-10-06 00:04:05 +0000107 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
108 S.getParamSpace());
109 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
110 ChangeScatteringMap);
111 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000112 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000113 }
114}
115
Tobias Grosserde68cc92011-06-30 20:01:02 +0000116// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000117//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000118// getTileMap creates a map from a n-dimensional scattering space into an
119// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000120//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000121// Example:
122// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
123//
124// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
125// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
126// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
127//
128// Before tiling:
129//
130// for (i = 0; i < N; i++)
131// for (j = 0; j < M; j++)
132// S(i,j)
133//
134// After tiling:
135//
136// for (t_i = 0; t_i < N; i+=32)
137// for (t_j = 0; t_j < M; j+=32)
138// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
139// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
140// S(i,j)
141//
142static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000143 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000144 // We construct
145 //
146 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
147 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
148 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
149 //
150 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000151 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
152 scheduleDimensions * 3);
153 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
154
155 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000156
Tobias Grosserde68cc92011-06-30 20:01:02 +0000157 for (int x = 0; x < scheduleDimensions; x++) {
158 int sX = x;
159 int tX = x;
160 int pX = scheduleDimensions + x;
161 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000162
Tobias Grosserde68cc92011-06-30 20:01:02 +0000163 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000164
Tobias Grosserde68cc92011-06-30 20:01:02 +0000165 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000166 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000167 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
168 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
169 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000170
Tobias Grosserde68cc92011-06-30 20:01:02 +0000171 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000172 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000173 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
174 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
175 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000176
Tobias Grosserde68cc92011-06-30 20:01:02 +0000177 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000178 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000179 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
180 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
181 tileMap = isl_basic_map_add_constraint(tileMap, c);
182
183 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000184 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000185 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
186 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000187 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000188 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000189 }
190
Tobias Grosserde68cc92011-06-30 20:01:02 +0000191 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000192 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000193 // The auxilary dimensions are transformed into existentially quantified ones.
194 // This reduces the number of visible scattering dimensions and allows Cloog
195 // to produces better code.
196 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
197 2 * scheduleDimensions,
198 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000199 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000200 return tileMap;
201}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000202
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000203// getScheduleForBand - Get the schedule for this band.
204//
Tobias Grosserb6033392011-12-08 13:02:58 +0000205// Polly applies transformations like tiling on top of the isl calculated value.
206// This can influence the number of scheduling dimension. The number of
207// schedule dimensions is returned in the parameter 'Dimension'.
208isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000209 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000210 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000211 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000212 isl_basic_map *TileMap;
213 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000214
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000215 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000216 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000217
Tobias Grosser79b30202011-11-17 12:56:00 +0000218 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000219 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000220
Tobias Grosserb6033392011-12-08 13:02:58 +0000221 // It does not make any sense to tile a band with just one dimension.
222 if (*Dimensions == 1)
223 return PartialSchedule;
224
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000225 ctx = isl_union_map_get_ctx(PartialSchedule);
226 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000227
Tobias Grosserb6033392011-12-08 13:02:58 +0000228 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000229 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
230 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000231 *Dimensions = 2 * *Dimensions;
232
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000233 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000234}
235
Tobias Grosser2493e922011-12-07 07:42:57 +0000236// Create a map that pre-vectorizes one scheduling dimension.
237//
238// getPrevectorMap creates a map that maps each input dimension to the same
239// output dimension, except for the dimension DimToVectorize. DimToVectorize is
240// strip mined by 'VectorWidth' and the newly created point loop of
241// DimToVectorize is moved to the innermost level.
242//
243// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
244//
245// | Before transformation
246// |
247// | A[i,j] -> [i,j]
248// |
249// | for (i = 0; i < 128; i++)
250// | for (j = 0; j < 128; j++)
251// | A(i,j);
252//
253// Prevector map:
254// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
255//
256// | After transformation:
257// |
258// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
259// |
260// | for (it = 0; it < 128; it+=4)
261// | for (j = 0; j < 128; j++)
262// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
263// | A(ip,j);
264//
265// The goal of this transformation is to create a trivially vectorizable loop.
266// This means a parallel loop at the innermost level that has a constant number
267// of iterations corresponding to the target vector width.
268//
269// This transformation creates a loop at the innermost level. The loop has a
270// constant number of iterations, if the number of loop iterations at
271// DimToVectorize can be devided by VectorWidth. The default VectorWidth is
272// currently constant and not yet target specific. This function does not reason
273// about parallelism.
274static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
275 int ScheduleDimensions,
276 int VectorWidth = 4) {
277 isl_space *Space;
278 isl_local_space *LocalSpace, *LocalSpaceRange;
279 isl_set *Modulo;
280 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000281 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000282 isl_aff *Aff;
283 int PointDimension; /* ip */
284 int TileDimension; /* it */
285 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000286
Tobias Grosser2493e922011-12-07 07:42:57 +0000287 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000288
Tobias Grosser2493e922011-12-07 07:42:57 +0000289 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
290 TilingMap = isl_map_universe(isl_space_copy(Space));
291 LocalSpace = isl_local_space_from_space(Space);
292 PointDimension = ScheduleDimensions;
293 TileDimension = DimToVectorize;
294
295 // Create an identity map for everything except DimToVectorize and map
296 // DimToVectorize to the point loop at the innermost dimension.
297 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000298 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000299 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000300
301 if (i == DimToVectorize)
302 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
303 else
304 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
305
306 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000307 }
308
Tobias Grosser2493e922011-12-07 07:42:57 +0000309 // it % 'VectorWidth' = 0
310 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
311 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
312 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
313 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
314 isl_int_init(VectorWidthMP);
315 isl_int_set_si(VectorWidthMP, VectorWidth);
316 Aff = isl_aff_mod(Aff, VectorWidthMP);
317 isl_int_clear(VectorWidthMP);
318 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
319 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000320
Tobias Grosser2493e922011-12-07 07:42:57 +0000321 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000322 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000323 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
324 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
325 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000326
Tobias Grosser2493e922011-12-07 07:42:57 +0000327 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000328 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000329 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
330 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
331 isl_constraint_set_constant_si(c, VectorWidth - 1);
332 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000333
Tobias Grosser2493e922011-12-07 07:42:57 +0000334 isl_map_dump(TilingMap);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000335
Tobias Grosser2493e922011-12-07 07:42:57 +0000336 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000337}
338
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000339// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000340//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000341// We walk recursively the forest of bands to combine the schedules of the
342// individual bands to the overall schedule. In case tiling is requested,
343// the individual bands are tiled.
344static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
345 int NumBands;
346 isl_union_map *Schedule;
347 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000348
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000349 ctx = isl_band_list_get_ctx(BandList);
350 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000351 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000352
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000353 for (int i = 0; i < NumBands; i++) {
354 isl_band *Band;
355 isl_union_map *PartialSchedule;
356 int ScheduleDimensions;
357 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000358
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000359 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000360 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000361 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000362
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000363 if (isl_band_has_children(Band)) {
364 isl_band_list *Children;
365 isl_union_map *SuffixSchedule;
366
367 Children = isl_band_get_children(Band);
368 SuffixSchedule = getScheduleForBandList(Children);
369 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
370 SuffixSchedule);
371 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000372 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000373 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
374 if (isl_band_member_is_zero_distance(Band, i)) {
375 isl_map *TileMap;
376 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000377
Tobias Grosserb6033392011-12-08 13:02:58 +0000378 TileMap = getPrevectorMap(ctx, i, ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000379 TileUMap = isl_union_map_from_map(TileMap);
380 TileUMap = isl_union_map_align_params(TileUMap,
381 isl_space_copy(Space));
382 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
383 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000384 break;
385 }
386 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000387 }
388
Tobias Grosser62872012011-11-17 12:56:04 +0000389 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000390
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000391 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000392 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000393 }
394
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000395 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000396}
397
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000398static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
399 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
400 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
401 isl_band_list_free(BandList);
402 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000403}
404
Tobias Grosser73600b82011-10-08 00:30:40 +0000405bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000406 Dependences *D = &getAnalysis<Dependences>();
407
408 // Build input data.
409 int dependencyKinds = Dependences::TYPE_RAW
410 | Dependences::TYPE_WAR
411 | Dependences::TYPE_WAW;
412
413 isl_union_map *validity = D->getDependences(dependencyKinds);
414 isl_union_map *proximity = D->getDependences(dependencyKinds);
415 isl_union_set *domain = NULL;
416
417 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
418 if ((*SI)->isFinalRead())
419 continue;
420 else if (!domain)
421 domain = isl_union_set_from_set((*SI)->getDomain());
422 else
423 domain = isl_union_set_union(domain,
424 isl_union_set_from_set((*SI)->getDomain()));
425
426 if (!domain)
427 return false;
428
429 DEBUG(dbgs() << "\n\nCompute schedule from: ");
430 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
431 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
432 dbgs() << ";\n");
433 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
434 dbgs() << ";\n");
435
436 isl_schedule *schedule;
437
Tobias Grosser768140c2011-12-14 08:58:39 +0000438 isl_options_set_schedule_max_constant_term(S.getIslCtx(), CONSTANT_BOUND);
Tobias Grosserf4bea392011-12-14 08:58:43 +0000439 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), 1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000440 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
441
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000442 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000443 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000444 DEBUG(dbgs() << "Individual bands: ");
445
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000446 isl_union_map *ScheduleMap = getScheduleMap(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000447
Tobias Grosserde68cc92011-06-30 20:01:02 +0000448 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
449 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000450
Tobias Grosserde68cc92011-06-30 20:01:02 +0000451 if (stmt->isFinalRead())
452 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000453
Tobias Grosserde68cc92011-06-30 20:01:02 +0000454 isl_set *domain = stmt->getDomain();
455 isl_union_map *stmtBand;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000456 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosserde68cc92011-06-30 20:01:02 +0000457 isl_union_set_from_set(domain));
458 isl_map *stmtSchedule;
459 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000460 stmt->setScattering(stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000461 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000462 }
463
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000464 isl_union_map_free(ScheduleMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000465 isl_schedule_free(schedule);
466
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000467 unsigned maxScatDims = 0;
468
469 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grossercf3942d2011-10-06 00:04:05 +0000470 maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000471
472 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000473 return false;
474}
475
Tobias Grosser73600b82011-10-08 00:30:40 +0000476void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000477}
478
Tobias Grosser73600b82011-10-08 00:30:40 +0000479void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000480 ScopPass::getAnalysisUsage(AU);
481 AU.addRequired<Dependences>();
482}
483
Tobias Grosser4dca4392011-11-22 19:40:19 +0000484INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000485 "Polly - Optimize schedule of SCoP", false, false)
486INITIALIZE_PASS_DEPENDENCY(Dependences)
487INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000488INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000489 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000490
Tobias Grosser73600b82011-10-08 00:30:40 +0000491Pass* polly::createIslScheduleOptimizerPass() {
492 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000493}