blob: 4ceb11d9a8e92f2e2ad127c87a6b4878e50f51ca [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 Grosser42152ff2012-01-30 19:38:47 +000035#include "isl/options.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000036
Tobias Grosser4dca4392011-11-22 19:40:19 +000037#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000038#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000039#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000040
Tobias Grosser768140c2011-12-14 08:58:39 +000041static const int CONSTANT_BOUND = 20;
42
Tobias Grosser30aa24c2011-05-14 19:02:06 +000043using namespace llvm;
44using namespace polly;
45
Tobias Grosser967239c2011-10-23 20:59:44 +000046namespace polly {
47 bool DisablePollyTiling;
48}
49static cl::opt<bool, true>
Tobias Grosser353a2682011-10-23 20:59:26 +000050DisableTiling("polly-no-tiling",
Tobias Grosser967239c2011-10-23 20:59:44 +000051 cl::desc("Disable tiling in the scheduler"), cl::Hidden,
52 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000053
Tobias Grossera26db472012-01-30 19:38:43 +000054static cl::opt<std::string>
55SimplifyDeps("polly-opt-simplify-deps",
56 cl::desc("Dependences should be simplified (yes/no)"),
57 cl::Hidden, cl::init("yes"));
58
Tobias Grosser30aa24c2011-05-14 19:02:06 +000059namespace {
60
Tobias Grosser73600b82011-10-08 00:30:40 +000061 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000062
63 public:
64 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000065 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000066
67 virtual bool runOnScop(Scop &S);
68 void printScop(llvm::raw_ostream &OS) const;
69 void getAnalysisUsage(AnalysisUsage &AU) const;
70 };
71
72}
73
Tobias Grosser73600b82011-10-08 00:30:40 +000074char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000075
76static int getSingleMap(__isl_take isl_map *map, void *user) {
77 isl_map **singleMap = (isl_map **) user;
78 *singleMap = map;
79
80 return 0;
81}
82
Tobias Grossercf3942d2011-10-06 00:04:05 +000083static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000084 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +000085 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000086
Tobias Grossercf3942d2011-10-06 00:04:05 +000087 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +000088 continue;
89
Tobias Grossercf3942d2011-10-06 00:04:05 +000090 unsigned OldDimensions = Stmt->getNumScattering();
91 isl_space *Space;
92 isl_basic_map *ChangeScattering;
93
94 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
95 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +000096 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000097
Tobias Grossercf3942d2011-10-06 00:04:05 +000098 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +000099 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000100 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
101 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 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000106 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000107 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000108 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000109 }
110
Tobias Grossercf3942d2011-10-06 00:04:05 +0000111 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000112
Tobias Grossercf3942d2011-10-06 00:04:05 +0000113 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
114 S.getParamSpace());
115 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
116 ChangeScatteringMap);
117 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000118 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000119 }
120}
121
Tobias Grosserde68cc92011-06-30 20:01:02 +0000122// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000123//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000124// getTileMap creates a map from a n-dimensional scattering space into an
125// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000126//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000127// Example:
128// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
129//
130// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
131// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
132// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
133//
134// Before tiling:
135//
136// for (i = 0; i < N; i++)
137// for (j = 0; j < M; j++)
138// S(i,j)
139//
140// After tiling:
141//
142// for (t_i = 0; t_i < N; i+=32)
143// for (t_j = 0; t_j < M; j+=32)
144// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
145// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
146// S(i,j)
147//
148static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000149 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000150 // We construct
151 //
152 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
153 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
154 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
155 //
156 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000157 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
158 scheduleDimensions * 3);
159 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
160
161 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000162
Tobias Grosserde68cc92011-06-30 20:01:02 +0000163 for (int x = 0; x < scheduleDimensions; x++) {
164 int sX = x;
165 int tX = x;
166 int pX = scheduleDimensions + x;
167 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000168
Tobias Grosserde68cc92011-06-30 20:01:02 +0000169 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000170
Tobias Grosserde68cc92011-06-30 20:01:02 +0000171 // sX = aX * tileSize;
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, sX, 1);
174 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
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 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000178 c = isl_equality_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_in, sX, -1);
181 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000182
Tobias Grosserde68cc92011-06-30 20:01:02 +0000183 // tX <= pX
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, pX, 1);
186 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
187 tileMap = isl_basic_map_add_constraint(tileMap, c);
188
189 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000190 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000191 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
192 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000193 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000194 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000195 }
196
Tobias Grosserde68cc92011-06-30 20:01:02 +0000197 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000198 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000199 // The auxilary dimensions are transformed into existentially quantified ones.
200 // This reduces the number of visible scattering dimensions and allows Cloog
201 // to produces better code.
202 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
203 2 * scheduleDimensions,
204 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000205 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000206 return tileMap;
207}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000208
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000209// getScheduleForBand - Get the schedule for this band.
210//
Tobias Grosserb6033392011-12-08 13:02:58 +0000211// Polly applies transformations like tiling on top of the isl calculated value.
212// This can influence the number of scheduling dimension. The number of
213// schedule dimensions is returned in the parameter 'Dimension'.
214isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000215 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000216 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000217 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000218 isl_basic_map *TileMap;
219 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000220
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000221 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000222 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000223
Tobias Grosser79b30202011-11-17 12:56:00 +0000224 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000225 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000226
Tobias Grosserb6033392011-12-08 13:02:58 +0000227 // It does not make any sense to tile a band with just one dimension.
228 if (*Dimensions == 1)
229 return PartialSchedule;
230
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000231 ctx = isl_union_map_get_ctx(PartialSchedule);
232 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000233
Tobias Grosserb6033392011-12-08 13:02:58 +0000234 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000235 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
236 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000237 *Dimensions = 2 * *Dimensions;
238
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000239 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000240}
241
Tobias Grosser2493e922011-12-07 07:42:57 +0000242// Create a map that pre-vectorizes one scheduling dimension.
243//
244// getPrevectorMap creates a map that maps each input dimension to the same
245// output dimension, except for the dimension DimToVectorize. DimToVectorize is
246// strip mined by 'VectorWidth' and the newly created point loop of
247// DimToVectorize is moved to the innermost level.
248//
249// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
250//
251// | Before transformation
252// |
253// | A[i,j] -> [i,j]
254// |
255// | for (i = 0; i < 128; i++)
256// | for (j = 0; j < 128; j++)
257// | A(i,j);
258//
259// Prevector map:
260// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
261//
262// | After transformation:
263// |
264// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
265// |
266// | for (it = 0; it < 128; it+=4)
267// | for (j = 0; j < 128; j++)
268// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
269// | A(ip,j);
270//
271// The goal of this transformation is to create a trivially vectorizable loop.
272// This means a parallel loop at the innermost level that has a constant number
273// of iterations corresponding to the target vector width.
274//
275// This transformation creates a loop at the innermost level. The loop has a
276// constant number of iterations, if the number of loop iterations at
277// DimToVectorize can be devided by VectorWidth. The default VectorWidth is
278// currently constant and not yet target specific. This function does not reason
279// about parallelism.
280static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
281 int ScheduleDimensions,
282 int VectorWidth = 4) {
283 isl_space *Space;
284 isl_local_space *LocalSpace, *LocalSpaceRange;
285 isl_set *Modulo;
286 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000287 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000288 isl_aff *Aff;
289 int PointDimension; /* ip */
290 int TileDimension; /* it */
291 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000292
Tobias Grosser2493e922011-12-07 07:42:57 +0000293 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000294
Tobias Grosser2493e922011-12-07 07:42:57 +0000295 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
296 TilingMap = isl_map_universe(isl_space_copy(Space));
297 LocalSpace = isl_local_space_from_space(Space);
298 PointDimension = ScheduleDimensions;
299 TileDimension = DimToVectorize;
300
301 // Create an identity map for everything except DimToVectorize and map
302 // DimToVectorize to the point loop at the innermost dimension.
303 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000304 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000305 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000306
307 if (i == DimToVectorize)
308 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
309 else
310 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
311
312 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000313 }
314
Tobias Grosser2493e922011-12-07 07:42:57 +0000315 // it % 'VectorWidth' = 0
316 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
317 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
318 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
319 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
320 isl_int_init(VectorWidthMP);
321 isl_int_set_si(VectorWidthMP, VectorWidth);
322 Aff = isl_aff_mod(Aff, VectorWidthMP);
323 isl_int_clear(VectorWidthMP);
324 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
325 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000326
Tobias Grosser2493e922011-12-07 07:42:57 +0000327 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000328 c = isl_inequality_alloc(isl_local_space_copy(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 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000332
Tobias Grosser2493e922011-12-07 07:42:57 +0000333 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000334 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000335 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
336 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
337 isl_constraint_set_constant_si(c, VectorWidth - 1);
338 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000339
Tobias Grosser2493e922011-12-07 07:42:57 +0000340 isl_map_dump(TilingMap);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000341
Tobias Grosser2493e922011-12-07 07:42:57 +0000342 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000343}
344
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000345// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000346//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000347// We walk recursively the forest of bands to combine the schedules of the
348// individual bands to the overall schedule. In case tiling is requested,
349// the individual bands are tiled.
350static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
351 int NumBands;
352 isl_union_map *Schedule;
353 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000354
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000355 ctx = isl_band_list_get_ctx(BandList);
356 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000357 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000358
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000359 for (int i = 0; i < NumBands; i++) {
360 isl_band *Band;
361 isl_union_map *PartialSchedule;
362 int ScheduleDimensions;
363 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000364
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000365 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000366 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000367 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000368
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000369 if (isl_band_has_children(Band)) {
370 isl_band_list *Children;
371 isl_union_map *SuffixSchedule;
372
373 Children = isl_band_get_children(Band);
374 SuffixSchedule = getScheduleForBandList(Children);
375 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
376 SuffixSchedule);
377 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000378 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000379 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
380 if (isl_band_member_is_zero_distance(Band, i)) {
381 isl_map *TileMap;
382 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000383
Tobias Grosserb6033392011-12-08 13:02:58 +0000384 TileMap = getPrevectorMap(ctx, i, ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000385 TileUMap = isl_union_map_from_map(TileMap);
386 TileUMap = isl_union_map_align_params(TileUMap,
387 isl_space_copy(Space));
388 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
389 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000390 break;
391 }
392 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000393 }
394
Tobias Grosser62872012011-11-17 12:56:04 +0000395 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000396
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000397 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000398 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000399 }
400
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000401 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000402}
403
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000404static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
405 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
406 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
407 isl_band_list_free(BandList);
408 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000409}
410
Tobias Grosser73600b82011-10-08 00:30:40 +0000411bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000412 Dependences *D = &getAnalysis<Dependences>();
413
414 // Build input data.
415 int dependencyKinds = Dependences::TYPE_RAW
416 | Dependences::TYPE_WAR
417 | Dependences::TYPE_WAW;
418
Tobias Grossera26db472012-01-30 19:38:43 +0000419 isl_union_map *dependences = D->getDependences(dependencyKinds);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000420 isl_union_set *domain = NULL;
421
422 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
423 if ((*SI)->isFinalRead())
424 continue;
425 else if (!domain)
426 domain = isl_union_set_from_set((*SI)->getDomain());
427 else
428 domain = isl_union_set_union(domain,
429 isl_union_set_from_set((*SI)->getDomain()));
430
431 if (!domain)
432 return false;
433
Tobias Grossera26db472012-01-30 19:38:43 +0000434 // Simplify the dependences by removing the constraints introduced by the
435 // domains. This can speed up the scheduling time significantly, as large
436 // constant coefficients will be removed from the dependences. The
437 // introduction of some additional dependences reduces the possible
438 // transformations, but in most cases, such transformation do not seem to be
439 // interesting anyway. In some cases this option may stop the scheduler to
440 // find any schedule.
441 if (SimplifyDeps == "yes") {
442 dependences = isl_union_map_gist_domain(dependences,
443 isl_union_set_copy(domain));
444 dependences = isl_union_map_gist_range(dependences,
445 isl_union_set_copy(domain));
446 } else if (SimplifyDeps != "no") {
447 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
448 "or 'no'. Falling back to default: 'yes'\n";
449 }
450
451 isl_schedule *schedule;
452 isl_union_map *proximity = isl_union_map_copy(dependences);
453 isl_union_map *validity = dependences;
454
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000455 DEBUG(dbgs() << "\n\nCompute schedule from: ");
456 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
457 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
458 dbgs() << ";\n");
459 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
460 dbgs() << ";\n");
461
Tobias Grosser768140c2011-12-14 08:58:39 +0000462 isl_options_set_schedule_max_constant_term(S.getIslCtx(), CONSTANT_BOUND);
Tobias Grosserf4bea392011-12-14 08:58:43 +0000463 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), 1);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000464
465 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000466 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000467 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
468
469 // In cases the scheduler is not able to optimize the code, we just do not
470 // touch the schedule.
471 if (!schedule)
472 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000473
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000474 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000475 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000476 DEBUG(dbgs() << "Individual bands: ");
477
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000478 isl_union_map *ScheduleMap = getScheduleMap(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000479
Tobias Grosserde68cc92011-06-30 20:01:02 +0000480 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
481 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000482
Tobias Grosserde68cc92011-06-30 20:01:02 +0000483 if (stmt->isFinalRead())
484 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000485
Tobias Grosserde68cc92011-06-30 20:01:02 +0000486 isl_set *domain = stmt->getDomain();
487 isl_union_map *stmtBand;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000488 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosserde68cc92011-06-30 20:01:02 +0000489 isl_union_set_from_set(domain));
490 isl_map *stmtSchedule;
491 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000492 stmt->setScattering(stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000493 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000494 }
495
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000496 isl_union_map_free(ScheduleMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000497 isl_schedule_free(schedule);
498
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000499 unsigned maxScatDims = 0;
500
501 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grossercf3942d2011-10-06 00:04:05 +0000502 maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000503
504 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000505 return false;
506}
507
Tobias Grosser73600b82011-10-08 00:30:40 +0000508void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000509}
510
Tobias Grosser73600b82011-10-08 00:30:40 +0000511void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000512 ScopPass::getAnalysisUsage(AU);
513 AU.addRequired<Dependences>();
514}
515
Tobias Grosser4dca4392011-11-22 19:40:19 +0000516INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000517 "Polly - Optimize schedule of SCoP", false, false)
518INITIALIZE_PASS_DEPENDENCY(Dependences)
519INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000520INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000521 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000522
Tobias Grosser73600b82011-10-08 00:30:40 +0000523Pass* polly::createIslScheduleOptimizerPass() {
524 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000525}