blob: 607230f511e29caa50c89c8ed7872fee77468d4f [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
Hongbin Zheng8a846612012-04-25 13:18:28 +000022#include "polly/CodeGen/CodeGeneration.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000023#include "polly/Dependences.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000024#include "polly/LinkAllPasses.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000025#include "polly/ScopInfo.h"
26
Tobias Grosser2493e922011-12-07 07:42:57 +000027#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000028#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000029#include "isl/constraint.h"
30#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000031#include "isl/options.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000032#include "isl/schedule.h"
33#include "isl/space.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000034
Tobias Grosser4dca4392011-11-22 19:40:19 +000035#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000036#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000037#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000038
39using namespace llvm;
40using namespace polly;
41
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000042namespace polly { bool DisablePollyTiling; }
43static cl::opt<bool, true> DisableTiling(
44 "polly-no-tiling", cl::desc("Disable tiling in the scheduler"), cl::Hidden,
45 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000046
Tobias Grossera26db472012-01-30 19:38:43 +000047static cl::opt<std::string>
Tobias Grosser1deda292012-02-14 14:02:48 +000048OptimizeDeps("polly-opt-optimize-only",
49 cl::desc("Only a certain kind of dependences (all/raw)"),
50 cl::Hidden, cl::init("all"));
51
52static cl::opt<std::string>
Tobias Grossera26db472012-01-30 19:38:43 +000053SimplifyDeps("polly-opt-simplify-deps",
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000054 cl::desc("Dependences should be simplified (yes/no)"), cl::Hidden,
55 cl::init("yes"));
Tobias Grossera26db472012-01-30 19:38:43 +000056
Tobias Grosser992e60c2012-02-20 08:41:15 +000057static cl::opt<int>
58MaxConstantTerm("polly-opt-max-constant-term",
59 cl::desc("The maximal constant term allowed (-1 is unlimited)"),
60 cl::Hidden, cl::init(20));
61
Tobias Grosser92f54802012-02-20 08:41:47 +000062static cl::opt<int>
63MaxCoefficient("polly-opt-max-coefficient",
64 cl::desc("The maximal coefficient allowed (-1 is unlimited)"),
65 cl::Hidden, cl::init(20));
66
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000067static cl::opt<std::string> FusionStrategy(
68 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
69 cl::Hidden, cl::init("min"));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000070
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000071static cl::opt<std::string> MaximizeBandDepth(
72 "polly-opt-maximize-bands", cl::desc("Maximize the band depth (yes/no)"),
73 cl::Hidden, cl::init("yes"));
Tobias Grosser95e860c2012-01-30 19:38:54 +000074
Tobias Grosser30aa24c2011-05-14 19:02:06 +000075namespace {
76
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000077class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000078
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000079public:
80 static char ID;
81 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = NULL; }
Tobias Grosser28781422012-10-16 07:29:19 +000082
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000083 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +000084
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000085 virtual bool runOnScop(Scop &S);
86 void printScop(llvm::raw_ostream &OS) const;
87 void getAnalysisUsage(AnalysisUsage &AU) const;
Tobias Grosser460e9a42012-04-25 13:22:43 +000088
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000089private:
90 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +000091
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000092 static void extendScattering(Scop &S, unsigned NewDimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +000093
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000094 /// @brief Create a map that describes a n-dimensonal tiling.
95 ///
96 /// getTileMap creates a map from a n-dimensional scattering space into an
97 /// 2*n-dimensional scattering space. The map describes a rectangular
98 /// tiling.
99 ///
100 /// Example:
101 /// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
102 ///
103 /// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
104 /// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
105 /// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
106 ///
107 /// Before tiling:
108 ///
109 /// for (i = 0; i < N; i++)
110 /// for (j = 0; j < M; j++)
111 /// S(i,j)
112 ///
113 /// After tiling:
114 ///
115 /// for (t_i = 0; t_i < N; i+=32)
116 /// for (t_j = 0; t_j < M; j+=32)
117 /// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
118 /// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
119 /// S(i,j)
120 ///
121 static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
122 isl_space *SpaceModel, int tileSize = 32);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000123
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000124 /// @brief Get the schedule for this band.
125 ///
126 /// Polly applies transformations like tiling on top of the isl calculated
127 /// value. This can influence the number of scheduling dimension. The
128 /// number of schedule dimensions is returned in the parameter 'Dimension'.
129 static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000130
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000131 /// @brief Create a map that pre-vectorizes one scheduling dimension.
132 ///
133 /// getPrevectorMap creates a map that maps each input dimension to the same
134 /// output dimension, except for the dimension DimToVectorize.
135 /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
136 /// point loop of DimToVectorize is moved to the innermost level.
137 ///
138 /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
139 ///
140 /// | Before transformation
141 /// |
142 /// | A[i,j] -> [i,j]
143 /// |
144 /// | for (i = 0; i < 128; i++)
145 /// | for (j = 0; j < 128; j++)
146 /// | A(i,j);
147 ///
148 /// Prevector map:
149 /// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
150 ///
151 /// | After transformation:
152 /// |
153 /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
154 /// |
155 /// | for (it = 0; it < 128; it+=4)
156 /// | for (j = 0; j < 128; j++)
157 /// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
158 /// | A(ip,j);
159 ///
160 /// The goal of this transformation is to create a trivially vectorizable
161 /// loop. This means a parallel loop at the innermost level that has a
162 /// constant number of iterations corresponding to the target vector width.
163 ///
164 /// This transformation creates a loop at the innermost level. The loop has
165 /// a constant number of iterations, if the number of loop iterations at
166 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
167 /// currently constant and not yet target specific. This function does not
168 /// reason about parallelism.
169 static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
170 int ScheduleDimensions, int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000171
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000172 /// @brief Get the scheduling map for a list of bands.
173 ///
174 /// Walk recursively the forest of bands to combine the schedules of the
175 /// individual bands to the overall schedule. In case tiling is requested,
176 /// the individual bands are tiled.
177 static isl_union_map *getScheduleForBandList(isl_band_list *BandList);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000178
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000179 static isl_union_map *getScheduleMap(isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000180
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000181 bool doFinalization() {
182 isl_schedule_free(LastSchedule);
183 LastSchedule = NULL;
184 return true;
185 }
186};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000187
188}
189
Tobias Grosser73600b82011-10-08 00:30:40 +0000190char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000191
Tobias Grosser460e9a42012-04-25 13:22:43 +0000192void IslScheduleOptimizer::extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000193 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000194 ScopStmt *Stmt = *SI;
Tobias Grossercf3942d2011-10-06 00:04:05 +0000195 unsigned OldDimensions = Stmt->getNumScattering();
196 isl_space *Space;
Tobias Grosser29666112012-05-22 10:47:31 +0000197 isl_map *Map, *New;
Tobias Grossercf3942d2011-10-06 00:04:05 +0000198
199 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
Tobias Grosser29666112012-05-22 10:47:31 +0000200 Map = isl_map_universe(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000201
Tobias Grosser29666112012-05-22 10:47:31 +0000202 for (unsigned i = 0; i < OldDimensions; i++)
203 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000204
Tobias Grosser29666112012-05-22 10:47:31 +0000205 for (unsigned i = OldDimensions; i < NewDimensions; i++)
206 Map = isl_map_fix_si(Map, isl_dim_out, i, 0);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000207
Tobias Grosser29666112012-05-22 10:47:31 +0000208 Map = isl_map_align_params(Map, S.getParamSpace());
209 New = isl_map_apply_range(Stmt->getScattering(), Map);
210 Stmt->setScattering(New);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000211 }
212}
213
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000214isl_basic_map *IslScheduleOptimizer::getTileMap(
215 isl_ctx *ctx, int scheduleDimensions, isl_space *SpaceModel, int tileSize) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000216 // We construct
217 //
218 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
219 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
220 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
221 //
222 // and project out the auxilary dimensions a0 and a1.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000223 isl_space *Space =
224 isl_space_alloc(ctx, 0, scheduleDimensions, scheduleDimensions * 3);
Tobias Grosserf5338802011-10-06 00:03:35 +0000225 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
226
227 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000228
Tobias Grosserde68cc92011-06-30 20:01:02 +0000229 for (int x = 0; x < scheduleDimensions; x++) {
230 int sX = x;
231 int tX = x;
232 int pX = scheduleDimensions + x;
233 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000234
Tobias Grosserde68cc92011-06-30 20:01:02 +0000235 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000236
Tobias Grosserde68cc92011-06-30 20:01:02 +0000237 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000238 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000239 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
240 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
241 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000242
Tobias Grosserde68cc92011-06-30 20:01:02 +0000243 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000244 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000245 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
246 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
247 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000248
Tobias Grosserde68cc92011-06-30 20:01:02 +0000249 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000250 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000251 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
252 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
253 tileMap = isl_basic_map_add_constraint(tileMap, c);
254
255 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000256 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000257 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
258 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000259 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000260 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000261 }
262
Tobias Grosserde68cc92011-06-30 20:01:02 +0000263 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000264 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000265 // The auxilary dimensions are transformed into existentially quantified ones.
266 // This reduces the number of visible scattering dimensions and allows Cloog
267 // to produces better code.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000268 tileMap = isl_basic_map_project_out(
269 tileMap, isl_dim_out, 2 * scheduleDimensions, scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000270 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000271 return tileMap;
272}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000273
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000274isl_union_map *
275IslScheduleOptimizer::getScheduleForBand(isl_band *Band, int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000276 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000277 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000278 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000279 isl_basic_map *TileMap;
280 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000281
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000282 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000283 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000284
Tobias Grosser79b30202011-11-17 12:56:00 +0000285 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000286 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000287
Tobias Grosserb6033392011-12-08 13:02:58 +0000288 // It does not make any sense to tile a band with just one dimension.
289 if (*Dimensions == 1)
290 return PartialSchedule;
291
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000292 ctx = isl_union_map_get_ctx(PartialSchedule);
293 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000294
Tobias Grosserb6033392011-12-08 13:02:58 +0000295 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000296 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
297 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000298 *Dimensions = 2 * *Dimensions;
299
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000300 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000301}
302
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000303isl_map *IslScheduleOptimizer::getPrevectorMap(
304 isl_ctx *ctx, int DimToVectorize, int ScheduleDimensions, int VectorWidth) {
Tobias Grosser2493e922011-12-07 07:42:57 +0000305 isl_space *Space;
306 isl_local_space *LocalSpace, *LocalSpaceRange;
307 isl_set *Modulo;
308 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000309 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000310 isl_aff *Aff;
311 int PointDimension; /* ip */
312 int TileDimension; /* it */
313 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000314
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000315 assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000316
Tobias Grosser2493e922011-12-07 07:42:57 +0000317 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
318 TilingMap = isl_map_universe(isl_space_copy(Space));
319 LocalSpace = isl_local_space_from_space(Space);
320 PointDimension = ScheduleDimensions;
321 TileDimension = DimToVectorize;
322
323 // Create an identity map for everything except DimToVectorize and map
324 // DimToVectorize to the point loop at the innermost dimension.
325 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000326 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000327 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000328
329 if (i == DimToVectorize)
330 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
331 else
332 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
333
334 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000335 }
336
Tobias Grosser2493e922011-12-07 07:42:57 +0000337 // it % 'VectorWidth' = 0
338 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
339 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
340 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
341 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
342 isl_int_init(VectorWidthMP);
343 isl_int_set_si(VectorWidthMP, VectorWidth);
344 Aff = isl_aff_mod(Aff, VectorWidthMP);
345 isl_int_clear(VectorWidthMP);
346 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
347 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000348
Tobias Grosser2493e922011-12-07 07:42:57 +0000349 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000350 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000351 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
352 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
353 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000354
Tobias Grosser2493e922011-12-07 07:42:57 +0000355 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000356 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000357 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
358 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
359 isl_constraint_set_constant_si(c, VectorWidth - 1);
360 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000361
Tobias Grosser2493e922011-12-07 07:42:57 +0000362 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000363}
364
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000365isl_union_map *
366IslScheduleOptimizer::getScheduleForBandList(isl_band_list *BandList) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000367 int NumBands;
368 isl_union_map *Schedule;
369 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000370
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000371 ctx = isl_band_list_get_ctx(BandList);
372 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000373 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000374
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000375 for (int i = 0; i < NumBands; i++) {
376 isl_band *Band;
377 isl_union_map *PartialSchedule;
378 int ScheduleDimensions;
379 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000380
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000381 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000382 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000383 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000384
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000385 if (isl_band_has_children(Band)) {
386 isl_band_list *Children;
387 isl_union_map *SuffixSchedule;
388
389 Children = isl_band_get_children(Band);
390 SuffixSchedule = getScheduleForBandList(Children);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000391 PartialSchedule =
392 isl_union_map_flat_range_product(PartialSchedule, SuffixSchedule);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000393 isl_band_list_free(Children);
Hongbin Zheng68794212012-05-06 10:22:19 +0000394 } else if (PollyVectorizerChoice != VECTORIZER_NONE) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000395 for (int j = 0; j < isl_band_n_member(Band); j++) {
396 if (isl_band_member_is_zero_distance(Band, j)) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000397 isl_map *TileMap;
398 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000399
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000400 TileMap = getPrevectorMap(ctx, ScheduleDimensions - j - 1,
Tobias Grosser216ea582012-04-16 11:06:06 +0000401 ScheduleDimensions);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000402 TileUMap = isl_union_map_from_map(TileMap);
403 TileUMap =
404 isl_union_map_align_params(TileUMap, isl_space_copy(Space));
405 PartialSchedule =
406 isl_union_map_apply_range(PartialSchedule, TileUMap);
407 break;
408 }
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000409 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000410 }
411
Tobias Grosser62872012011-11-17 12:56:04 +0000412 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000413
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000414 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000415 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000416 }
417
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000418 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000419}
420
Tobias Grosser460e9a42012-04-25 13:22:43 +0000421isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000422 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
423 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
424 isl_band_list_free(BandList);
425 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000426}
427
Tobias Grosser73600b82011-10-08 00:30:40 +0000428bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000429 Dependences *D = &getAnalysis<Dependences>();
430
Tobias Grosser28781422012-10-16 07:29:19 +0000431 isl_schedule_free(LastSchedule);
432 LastSchedule = NULL;
433
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000434 // Build input data.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000435 int ValidityKinds =
436 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000437 int ProximityKinds;
438
439 if (OptimizeDeps == "all")
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000440 ProximityKinds =
441 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000442 else if (OptimizeDeps == "raw")
Tobias Grosser1e03ad72012-02-15 09:58:42 +0000443 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000444 else {
445 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000446 << " Falling back to optimizing all dependences.\n";
447 ProximityKinds =
448 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000449 }
450
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000451 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000452
Tobias Grosser98610ee2012-02-13 23:31:39 +0000453 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000454 return false;
455
Tobias Grosser8a507022012-03-16 11:51:41 +0000456 isl_union_map *Validity = D->getDependences(ValidityKinds);
457 isl_union_map *Proximity = D->getDependences(ProximityKinds);
458
Tobias Grossera26db472012-01-30 19:38:43 +0000459 // Simplify the dependences by removing the constraints introduced by the
460 // domains. This can speed up the scheduling time significantly, as large
461 // constant coefficients will be removed from the dependences. The
462 // introduction of some additional dependences reduces the possible
463 // transformations, but in most cases, such transformation do not seem to be
464 // interesting anyway. In some cases this option may stop the scheduler to
465 // find any schedule.
466 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000467 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
468 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000469 Proximity =
470 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000471 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000472 } else if (SimplifyDeps != "no") {
473 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
474 "or 'no'. Falling back to default: 'yes'\n";
475 }
476
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000477 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000478 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
479 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000480 dbgs() << ";\n");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000481 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000482 dbgs() << ";\n");
483
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000484 int IslFusionStrategy;
485
486 if (FusionStrategy == "max") {
487 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
488 } else if (FusionStrategy == "min") {
489 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
490 } else {
491 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
492 "fusion.\n";
493 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
494 }
495
Tobias Grosser95e860c2012-01-30 19:38:54 +0000496 int IslMaximizeBands;
497
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000498 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000499 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000500 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000501 IslMaximizeBands = 0;
502 } else {
503 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
504 " or 'no'. Falling back to default: 'yes'\n";
505 IslMaximizeBands = 1;
506 }
507
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000508 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000509 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000510 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000511 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000512
513 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grosser00383a72012-02-14 14:02:44 +0000514 isl_schedule *Schedule;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000515 Schedule = isl_union_set_compute_schedule(Domain, Validity, Proximity);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000516 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
517
518 // In cases the scheduler is not able to optimize the code, we just do not
519 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000520 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000521 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000522
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000523 DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule); dbgs() << ";\n");
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000524
Tobias Grosser98610ee2012-02-13 23:31:39 +0000525 isl_union_map *ScheduleMap = getScheduleMap(Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000526
Tobias Grosserde68cc92011-06-30 20:01:02 +0000527 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grosser98610ee2012-02-13 23:31:39 +0000528 ScopStmt *Stmt = *SI;
Tobias Grosser249c4b12013-04-11 05:55:13 +0000529 isl_map *StmtSchedule;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000530 isl_set *Domain = Stmt->getDomain();
531 isl_union_map *StmtBand;
532 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000533 isl_union_set_from_set(Domain));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000534 if (isl_union_map_is_empty(StmtBand)) {
535 // Statements with an empty iteration domain may not have a schedule
536 // assigned by the isl schedule optimizer. As Polly expects each statement
537 // to have a schedule, we keep the old schedule for this statement. As
538 // there are zero iterations to execute, the content of the schedule does
539 // not matter.
540 //
541 // TODO: Consider removing such statements when constructing the scop.
Tobias Grosserf242b802013-04-10 22:48:08 +0000542 StmtSchedule = Stmt->getScattering();
543 StmtSchedule = isl_map_set_tuple_id(StmtSchedule, isl_dim_out, NULL);
Tobias Grosser249c4b12013-04-11 05:55:13 +0000544 isl_union_map_free(StmtBand);
545 } else {
546 assert(isl_union_map_n_map(StmtBand) == 1);
547 StmtSchedule = isl_map_from_union_map(StmtBand);
Tobias Grosserf242b802013-04-10 22:48:08 +0000548 }
549
Tobias Grosser98610ee2012-02-13 23:31:39 +0000550 Stmt->setScattering(StmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000551 }
552
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000553 isl_union_map_free(ScheduleMap);
Tobias Grosser28781422012-10-16 07:29:19 +0000554 LastSchedule = Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000555
Tobias Grosser98610ee2012-02-13 23:31:39 +0000556 unsigned MaxScatDims = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000557
558 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grosser98610ee2012-02-13 23:31:39 +0000559 MaxScatDims = std::max((*SI)->getNumScattering(), MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000560
Tobias Grosser98610ee2012-02-13 23:31:39 +0000561 extendScattering(S, MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000562 return false;
563}
564
Tobias Grosser73600b82011-10-08 00:30:40 +0000565void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000566 isl_printer *p;
567 char *ScheduleStr;
568
569 OS << "Calculated schedule:\n";
570
571 if (!LastSchedule) {
572 OS << "n/a\n";
573 return;
574 }
575
576 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
577 p = isl_printer_print_schedule(p, LastSchedule);
578 ScheduleStr = isl_printer_get_str(p);
579 isl_printer_free(p);
580
581 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000582}
583
Tobias Grosser73600b82011-10-08 00:30:40 +0000584void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000585 ScopPass::getAnalysisUsage(AU);
586 AU.addRequired<Dependences>();
587}
588
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000589Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000590 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000591}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000592
593INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
594 "Polly - Optimize schedule of SCoP", false, false);
595INITIALIZE_PASS_DEPENDENCY(Dependences);
596INITIALIZE_PASS_DEPENDENCY(ScopInfo);
597INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
598 "Polly - Optimize schedule of SCoP", false, false)