blob: e7fc8398c709d9899dacb6dddf7576936babc7a2 [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 Grossera26db472012-01-30 19:38:43 +000053static cl::opt<std::string>
54SimplifyDeps("polly-opt-simplify-deps",
55 cl::desc("Dependences should be simplified (yes/no)"),
56 cl::Hidden, cl::init("yes"));
57
Tobias Grosser30aa24c2011-05-14 19:02:06 +000058namespace {
59
Tobias Grosser73600b82011-10-08 00:30:40 +000060 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000061
62 public:
63 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000064 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000065
66 virtual bool runOnScop(Scop &S);
67 void printScop(llvm::raw_ostream &OS) const;
68 void getAnalysisUsage(AnalysisUsage &AU) const;
69 };
70
71}
72
Tobias Grosser73600b82011-10-08 00:30:40 +000073char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000074
75static int getSingleMap(__isl_take isl_map *map, void *user) {
76 isl_map **singleMap = (isl_map **) user;
77 *singleMap = map;
78
79 return 0;
80}
81
Tobias Grossercf3942d2011-10-06 00:04:05 +000082static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000083 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +000084 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000085
Tobias Grossercf3942d2011-10-06 00:04:05 +000086 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +000087 continue;
88
Tobias Grossercf3942d2011-10-06 00:04:05 +000089 unsigned OldDimensions = Stmt->getNumScattering();
90 isl_space *Space;
91 isl_basic_map *ChangeScattering;
92
93 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
94 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +000095 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000096
Tobias Grossercf3942d2011-10-06 00:04:05 +000097 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +000098 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000099 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
100 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000101 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000102 }
103
Tobias Grossercf3942d2011-10-06 00:04:05 +0000104 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000105 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000106 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000107 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000108 }
109
Tobias Grossercf3942d2011-10-06 00:04:05 +0000110 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000111
Tobias Grossercf3942d2011-10-06 00:04:05 +0000112 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
113 S.getParamSpace());
114 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
115 ChangeScatteringMap);
116 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000117 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000118 }
119}
120
Tobias Grosserde68cc92011-06-30 20:01:02 +0000121// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000122//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000123// getTileMap creates a map from a n-dimensional scattering space into an
124// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000125//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000126// Example:
127// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
128//
129// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
130// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
131// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
132//
133// Before tiling:
134//
135// for (i = 0; i < N; i++)
136// for (j = 0; j < M; j++)
137// S(i,j)
138//
139// After tiling:
140//
141// for (t_i = 0; t_i < N; i+=32)
142// for (t_j = 0; t_j < M; j+=32)
143// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
144// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
145// S(i,j)
146//
147static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000148 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000149 // We construct
150 //
151 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
152 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
153 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
154 //
155 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000156 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
157 scheduleDimensions * 3);
158 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
159
160 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000161
Tobias Grosserde68cc92011-06-30 20:01:02 +0000162 for (int x = 0; x < scheduleDimensions; x++) {
163 int sX = x;
164 int tX = x;
165 int pX = scheduleDimensions + x;
166 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000167
Tobias Grosserde68cc92011-06-30 20:01:02 +0000168 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000169
Tobias Grosserde68cc92011-06-30 20:01:02 +0000170 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000171 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000172 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
173 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
174 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000175
Tobias Grosserde68cc92011-06-30 20:01:02 +0000176 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000177 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000178 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
179 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
180 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000181
Tobias Grosserde68cc92011-06-30 20:01:02 +0000182 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000183 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000184 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
185 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
186 tileMap = isl_basic_map_add_constraint(tileMap, c);
187
188 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000189 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000190 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
191 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000192 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000193 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000194 }
195
Tobias Grosserde68cc92011-06-30 20:01:02 +0000196 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000197 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000198 // The auxilary dimensions are transformed into existentially quantified ones.
199 // This reduces the number of visible scattering dimensions and allows Cloog
200 // to produces better code.
201 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
202 2 * scheduleDimensions,
203 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000204 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000205 return tileMap;
206}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000207
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000208// getScheduleForBand - Get the schedule for this band.
209//
Tobias Grosserb6033392011-12-08 13:02:58 +0000210// Polly applies transformations like tiling on top of the isl calculated value.
211// This can influence the number of scheduling dimension. The number of
212// schedule dimensions is returned in the parameter 'Dimension'.
213isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000214 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000215 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000216 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000217 isl_basic_map *TileMap;
218 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000219
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000220 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000221 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000222
Tobias Grosser79b30202011-11-17 12:56:00 +0000223 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000224 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000225
Tobias Grosserb6033392011-12-08 13:02:58 +0000226 // It does not make any sense to tile a band with just one dimension.
227 if (*Dimensions == 1)
228 return PartialSchedule;
229
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000230 ctx = isl_union_map_get_ctx(PartialSchedule);
231 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000232
Tobias Grosserb6033392011-12-08 13:02:58 +0000233 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000234 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
235 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000236 *Dimensions = 2 * *Dimensions;
237
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000238 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000239}
240
Tobias Grosser2493e922011-12-07 07:42:57 +0000241// Create a map that pre-vectorizes one scheduling dimension.
242//
243// getPrevectorMap creates a map that maps each input dimension to the same
244// output dimension, except for the dimension DimToVectorize. DimToVectorize is
245// strip mined by 'VectorWidth' and the newly created point loop of
246// DimToVectorize is moved to the innermost level.
247//
248// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
249//
250// | Before transformation
251// |
252// | A[i,j] -> [i,j]
253// |
254// | for (i = 0; i < 128; i++)
255// | for (j = 0; j < 128; j++)
256// | A(i,j);
257//
258// Prevector map:
259// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
260//
261// | After transformation:
262// |
263// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
264// |
265// | for (it = 0; it < 128; it+=4)
266// | for (j = 0; j < 128; j++)
267// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
268// | A(ip,j);
269//
270// The goal of this transformation is to create a trivially vectorizable loop.
271// This means a parallel loop at the innermost level that has a constant number
272// of iterations corresponding to the target vector width.
273//
274// This transformation creates a loop at the innermost level. The loop has a
275// constant number of iterations, if the number of loop iterations at
276// DimToVectorize can be devided by VectorWidth. The default VectorWidth is
277// currently constant and not yet target specific. This function does not reason
278// about parallelism.
279static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
280 int ScheduleDimensions,
281 int VectorWidth = 4) {
282 isl_space *Space;
283 isl_local_space *LocalSpace, *LocalSpaceRange;
284 isl_set *Modulo;
285 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000286 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000287 isl_aff *Aff;
288 int PointDimension; /* ip */
289 int TileDimension; /* it */
290 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000291
Tobias Grosser2493e922011-12-07 07:42:57 +0000292 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000293
Tobias Grosser2493e922011-12-07 07:42:57 +0000294 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
295 TilingMap = isl_map_universe(isl_space_copy(Space));
296 LocalSpace = isl_local_space_from_space(Space);
297 PointDimension = ScheduleDimensions;
298 TileDimension = DimToVectorize;
299
300 // Create an identity map for everything except DimToVectorize and map
301 // DimToVectorize to the point loop at the innermost dimension.
302 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000303 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000304 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000305
306 if (i == DimToVectorize)
307 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
308 else
309 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
310
311 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000312 }
313
Tobias Grosser2493e922011-12-07 07:42:57 +0000314 // it % 'VectorWidth' = 0
315 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
316 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
317 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
318 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
319 isl_int_init(VectorWidthMP);
320 isl_int_set_si(VectorWidthMP, VectorWidth);
321 Aff = isl_aff_mod(Aff, VectorWidthMP);
322 isl_int_clear(VectorWidthMP);
323 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
324 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000325
Tobias Grosser2493e922011-12-07 07:42:57 +0000326 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000327 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000328 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
329 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
330 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000331
Tobias Grosser2493e922011-12-07 07:42:57 +0000332 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000333 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000334 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
335 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
336 isl_constraint_set_constant_si(c, VectorWidth - 1);
337 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000338
Tobias Grosser2493e922011-12-07 07:42:57 +0000339 isl_map_dump(TilingMap);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000340
Tobias Grosser2493e922011-12-07 07:42:57 +0000341 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000342}
343
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000344// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000345//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000346// We walk recursively the forest of bands to combine the schedules of the
347// individual bands to the overall schedule. In case tiling is requested,
348// the individual bands are tiled.
349static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
350 int NumBands;
351 isl_union_map *Schedule;
352 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000353
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000354 ctx = isl_band_list_get_ctx(BandList);
355 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000356 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000357
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000358 for (int i = 0; i < NumBands; i++) {
359 isl_band *Band;
360 isl_union_map *PartialSchedule;
361 int ScheduleDimensions;
362 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000363
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000364 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000365 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000366 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000367
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000368 if (isl_band_has_children(Band)) {
369 isl_band_list *Children;
370 isl_union_map *SuffixSchedule;
371
372 Children = isl_band_get_children(Band);
373 SuffixSchedule = getScheduleForBandList(Children);
374 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
375 SuffixSchedule);
376 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000377 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000378 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
379 if (isl_band_member_is_zero_distance(Band, i)) {
380 isl_map *TileMap;
381 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000382
Tobias Grosserb6033392011-12-08 13:02:58 +0000383 TileMap = getPrevectorMap(ctx, i, ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000384 TileUMap = isl_union_map_from_map(TileMap);
385 TileUMap = isl_union_map_align_params(TileUMap,
386 isl_space_copy(Space));
387 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
388 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000389 break;
390 }
391 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000392 }
393
Tobias Grosser62872012011-11-17 12:56:04 +0000394 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000395
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000396 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000397 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000398 }
399
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000400 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000401}
402
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000403static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
404 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
405 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
406 isl_band_list_free(BandList);
407 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000408}
409
Tobias Grosser73600b82011-10-08 00:30:40 +0000410bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000411 Dependences *D = &getAnalysis<Dependences>();
412
413 // Build input data.
414 int dependencyKinds = Dependences::TYPE_RAW
415 | Dependences::TYPE_WAR
416 | Dependences::TYPE_WAW;
417
Tobias Grossera26db472012-01-30 19:38:43 +0000418 isl_union_map *dependences = D->getDependences(dependencyKinds);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000419 isl_union_set *domain = NULL;
420
421 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
422 if ((*SI)->isFinalRead())
423 continue;
424 else if (!domain)
425 domain = isl_union_set_from_set((*SI)->getDomain());
426 else
427 domain = isl_union_set_union(domain,
428 isl_union_set_from_set((*SI)->getDomain()));
429
430 if (!domain)
431 return false;
432
Tobias Grossera26db472012-01-30 19:38:43 +0000433 // Simplify the dependences by removing the constraints introduced by the
434 // domains. This can speed up the scheduling time significantly, as large
435 // constant coefficients will be removed from the dependences. The
436 // introduction of some additional dependences reduces the possible
437 // transformations, but in most cases, such transformation do not seem to be
438 // interesting anyway. In some cases this option may stop the scheduler to
439 // find any schedule.
440 if (SimplifyDeps == "yes") {
441 dependences = isl_union_map_gist_domain(dependences,
442 isl_union_set_copy(domain));
443 dependences = isl_union_map_gist_range(dependences,
444 isl_union_set_copy(domain));
445 } else if (SimplifyDeps != "no") {
446 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
447 "or 'no'. Falling back to default: 'yes'\n";
448 }
449
450 isl_schedule *schedule;
451 isl_union_map *proximity = isl_union_map_copy(dependences);
452 isl_union_map *validity = dependences;
453
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000454 DEBUG(dbgs() << "\n\nCompute schedule from: ");
455 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
456 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
457 dbgs() << ";\n");
458 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
459 dbgs() << ";\n");
460
Tobias Grosser768140c2011-12-14 08:58:39 +0000461 isl_options_set_schedule_max_constant_term(S.getIslCtx(), CONSTANT_BOUND);
Tobias Grosserf4bea392011-12-14 08:58:43 +0000462 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), 1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000463 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
464
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000465 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000466 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000467 DEBUG(dbgs() << "Individual bands: ");
468
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000469 isl_union_map *ScheduleMap = getScheduleMap(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000470
Tobias Grosserde68cc92011-06-30 20:01:02 +0000471 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
472 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000473
Tobias Grosserde68cc92011-06-30 20:01:02 +0000474 if (stmt->isFinalRead())
475 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000476
Tobias Grosserde68cc92011-06-30 20:01:02 +0000477 isl_set *domain = stmt->getDomain();
478 isl_union_map *stmtBand;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000479 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosserde68cc92011-06-30 20:01:02 +0000480 isl_union_set_from_set(domain));
481 isl_map *stmtSchedule;
482 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000483 stmt->setScattering(stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000484 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000485 }
486
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000487 isl_union_map_free(ScheduleMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000488 isl_schedule_free(schedule);
489
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000490 unsigned maxScatDims = 0;
491
492 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grossercf3942d2011-10-06 00:04:05 +0000493 maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000494
495 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000496 return false;
497}
498
Tobias Grosser73600b82011-10-08 00:30:40 +0000499void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000500}
501
Tobias Grosser73600b82011-10-08 00:30:40 +0000502void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000503 ScopPass::getAnalysisUsage(AU);
504 AU.addRequired<Dependences>();
505}
506
Tobias Grosser4dca4392011-11-22 19:40:19 +0000507INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000508 "Polly - Optimize schedule of SCoP", false, false)
509INITIALIZE_PASS_DEPENDENCY(Dependences)
510INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000511INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000512 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000513
Tobias Grosser73600b82011-10-08 00:30:40 +0000514Pass* polly::createIslScheduleOptimizerPass() {
515 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000516}