blob: 26f439f87580b257441f94a5a3bf6d427e3adfce [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 Grosser967239c2011-10-23 20:59:44 +000042namespace polly {
43 bool DisablePollyTiling;
44}
45static cl::opt<bool, true>
Tobias Grosser353a2682011-10-23 20:59:26 +000046DisableTiling("polly-no-tiling",
Tobias Grosser967239c2011-10-23 20:59:44 +000047 cl::desc("Disable tiling in the scheduler"), cl::Hidden,
48 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000049
Tobias Grossera26db472012-01-30 19:38:43 +000050static cl::opt<std::string>
Tobias Grosser1deda292012-02-14 14:02:48 +000051OptimizeDeps("polly-opt-optimize-only",
52 cl::desc("Only a certain kind of dependences (all/raw)"),
53 cl::Hidden, cl::init("all"));
54
55static cl::opt<std::string>
Tobias Grossera26db472012-01-30 19:38:43 +000056SimplifyDeps("polly-opt-simplify-deps",
57 cl::desc("Dependences should be simplified (yes/no)"),
58 cl::Hidden, cl::init("yes"));
59
Tobias Grosser992e60c2012-02-20 08:41:15 +000060static cl::opt<int>
61MaxConstantTerm("polly-opt-max-constant-term",
62 cl::desc("The maximal constant term allowed (-1 is unlimited)"),
63 cl::Hidden, cl::init(20));
64
Tobias Grosser92f54802012-02-20 08:41:47 +000065static cl::opt<int>
66MaxCoefficient("polly-opt-max-coefficient",
67 cl::desc("The maximal coefficient allowed (-1 is unlimited)"),
68 cl::Hidden, cl::init(20));
69
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000070static cl::opt<std::string>
71FusionStrategy("polly-opt-fusion",
72 cl::desc("The fusion strategy to choose (min/max)"),
Tobias Grosser50ff31d2012-01-30 19:38:58 +000073 cl::Hidden, cl::init("min"));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000074
Tobias Grosser95e860c2012-01-30 19:38:54 +000075static cl::opt<std::string>
Tobias Grossera4ea90b2012-01-30 22:43:56 +000076MaximizeBandDepth("polly-opt-maximize-bands",
77 cl::desc("Maximize the band depth (yes/no)"),
Tobias Grosser95e860c2012-01-30 19:38:54 +000078 cl::Hidden, cl::init("yes"));
79
Tobias Grosser30aa24c2011-05-14 19:02:06 +000080namespace {
81
Tobias Grosser73600b82011-10-08 00:30:40 +000082 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000083
84 public:
85 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000086 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000087
88 virtual bool runOnScop(Scop &S);
89 void printScop(llvm::raw_ostream &OS) const;
90 void getAnalysisUsage(AnalysisUsage &AU) const;
Tobias Grosser460e9a42012-04-25 13:22:43 +000091
92 private:
93 static void extendScattering(Scop &S, unsigned NewDimensions);
94
95 /// @brief Create a map that describes a n-dimensonal tiling.
96 ///
97 /// getTileMap creates a map from a n-dimensional scattering space into an
98 /// 2*n-dimensional scattering space. The map describes a rectangular
99 /// tiling.
100 ///
101 /// Example:
102 /// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
103 ///
104 /// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
105 /// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
106 /// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
107 ///
108 /// Before tiling:
109 ///
110 /// for (i = 0; i < N; i++)
111 /// for (j = 0; j < M; j++)
112 /// S(i,j)
113 ///
114 /// After tiling:
115 ///
116 /// for (t_i = 0; t_i < N; i+=32)
117 /// for (t_j = 0; t_j < M; j+=32)
118 /// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
119 /// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
120 /// S(i,j)
121 ///
122 static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
123 isl_space *SpaceModel, int tileSize = 32);
124
125 /// @brief Get the schedule for this band.
126 ///
127 /// Polly applies transformations like tiling on top of the isl calculated
128 /// value. This can influence the number of scheduling dimension. The
129 /// number of schedule dimensions is returned in the parameter 'Dimension'.
130 static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions);
131
132 /// @brief Create a map that pre-vectorizes one scheduling dimension.
133 ///
134 /// getPrevectorMap creates a map that maps each input dimension to the same
135 /// output dimension, except for the dimension DimToVectorize.
136 /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
137 /// point loop of DimToVectorize is moved to the innermost level.
138 ///
139 /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
140 ///
141 /// | Before transformation
142 /// |
143 /// | A[i,j] -> [i,j]
144 /// |
145 /// | for (i = 0; i < 128; i++)
146 /// | for (j = 0; j < 128; j++)
147 /// | A(i,j);
148 ///
149 /// Prevector map:
150 /// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
151 ///
152 /// | After transformation:
153 /// |
154 /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
155 /// |
156 /// | for (it = 0; it < 128; it+=4)
157 /// | for (j = 0; j < 128; j++)
158 /// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
159 /// | A(ip,j);
160 ///
161 /// The goal of this transformation is to create a trivially vectorizable
162 /// loop. This means a parallel loop at the innermost level that has a
163 /// constant number of iterations corresponding to the target vector width.
164 ///
165 /// This transformation creates a loop at the innermost level. The loop has
166 /// a constant number of iterations, if the number of loop iterations at
167 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
168 /// currently constant and not yet target specific. This function does not
169 /// reason about parallelism.
170 static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
171 int ScheduleDimensions,
172 int VectorWidth = 4);
173
174 /// @brief Get the scheduling map for a list of bands.
175 ///
176 /// Walk recursively the forest of bands to combine the schedules of the
177 /// individual bands to the overall schedule. In case tiling is requested,
178 /// the individual bands are tiled.
179 static isl_union_map *getScheduleForBandList(isl_band_list *BandList);
180
181 static isl_union_map *getScheduleMap(isl_schedule *Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000182 };
183
184}
185
Tobias Grosser73600b82011-10-08 00:30:40 +0000186char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000187
188static int getSingleMap(__isl_take isl_map *map, void *user) {
189 isl_map **singleMap = (isl_map **) user;
190 *singleMap = map;
191
192 return 0;
193}
194
Tobias Grosser460e9a42012-04-25 13:22:43 +0000195void IslScheduleOptimizer::extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000196 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000197 ScopStmt *Stmt = *SI;
Tobias Grossercf3942d2011-10-06 00:04:05 +0000198 unsigned OldDimensions = Stmt->getNumScattering();
199 isl_space *Space;
200 isl_basic_map *ChangeScattering;
201
202 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
203 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +0000204 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000205
Tobias Grossercf3942d2011-10-06 00:04:05 +0000206 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000207 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000208 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
209 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000210 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000211 }
212
Tobias Grossercf3942d2011-10-06 00:04:05 +0000213 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000214 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000215 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000216 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000217 }
218
Tobias Grossercf3942d2011-10-06 00:04:05 +0000219 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000220
Tobias Grossercf3942d2011-10-06 00:04:05 +0000221 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
222 S.getParamSpace());
223 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
224 ChangeScatteringMap);
225 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000226 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000227 }
228}
229
Tobias Grosser460e9a42012-04-25 13:22:43 +0000230isl_basic_map *IslScheduleOptimizer::getTileMap(isl_ctx *ctx,
231 int scheduleDimensions,
232 isl_space *SpaceModel,
233 int tileSize) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000234 // We construct
235 //
236 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
237 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
238 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
239 //
240 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000241 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
242 scheduleDimensions * 3);
243 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
244
245 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000246
Tobias Grosserde68cc92011-06-30 20:01:02 +0000247 for (int x = 0; x < scheduleDimensions; x++) {
248 int sX = x;
249 int tX = x;
250 int pX = scheduleDimensions + x;
251 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000252
Tobias Grosserde68cc92011-06-30 20:01:02 +0000253 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000254
Tobias Grosserde68cc92011-06-30 20:01:02 +0000255 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000256 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000257 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
258 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
259 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000260
Tobias Grosserde68cc92011-06-30 20:01:02 +0000261 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000262 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000263 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
264 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
265 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000266
Tobias Grosserde68cc92011-06-30 20:01:02 +0000267 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000268 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000269 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
270 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
271 tileMap = isl_basic_map_add_constraint(tileMap, c);
272
273 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000274 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000275 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
276 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000277 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000278 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000279 }
280
Tobias Grosserde68cc92011-06-30 20:01:02 +0000281 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000282 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000283 // The auxilary dimensions are transformed into existentially quantified ones.
284 // This reduces the number of visible scattering dimensions and allows Cloog
285 // to produces better code.
286 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
287 2 * scheduleDimensions,
288 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000289 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000290 return tileMap;
291}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000292
Tobias Grosser460e9a42012-04-25 13:22:43 +0000293isl_union_map *IslScheduleOptimizer::getScheduleForBand(isl_band *Band,
294 int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000295 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000296 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000297 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000298 isl_basic_map *TileMap;
299 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000300
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000301 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000302 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000303
Tobias Grosser79b30202011-11-17 12:56:00 +0000304 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000305 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000306
Tobias Grosserb6033392011-12-08 13:02:58 +0000307 // It does not make any sense to tile a band with just one dimension.
308 if (*Dimensions == 1)
309 return PartialSchedule;
310
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000311 ctx = isl_union_map_get_ctx(PartialSchedule);
312 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000313
Tobias Grosserb6033392011-12-08 13:02:58 +0000314 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000315 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
316 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000317 *Dimensions = 2 * *Dimensions;
318
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000319 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000320}
321
Tobias Grosser460e9a42012-04-25 13:22:43 +0000322isl_map *IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx,
323 int DimToVectorize,
324 int ScheduleDimensions,
325 int VectorWidth) {
Tobias Grosser2493e922011-12-07 07:42:57 +0000326 isl_space *Space;
327 isl_local_space *LocalSpace, *LocalSpaceRange;
328 isl_set *Modulo;
329 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000330 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000331 isl_aff *Aff;
332 int PointDimension; /* ip */
333 int TileDimension; /* it */
334 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000335
Tobias Grosser2493e922011-12-07 07:42:57 +0000336 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000337
Tobias Grosser2493e922011-12-07 07:42:57 +0000338 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
339 TilingMap = isl_map_universe(isl_space_copy(Space));
340 LocalSpace = isl_local_space_from_space(Space);
341 PointDimension = ScheduleDimensions;
342 TileDimension = DimToVectorize;
343
344 // Create an identity map for everything except DimToVectorize and map
345 // DimToVectorize to the point loop at the innermost dimension.
346 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000347 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000348 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000349
350 if (i == DimToVectorize)
351 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
352 else
353 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
354
355 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000356 }
357
Tobias Grosser2493e922011-12-07 07:42:57 +0000358 // it % 'VectorWidth' = 0
359 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
360 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
361 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
362 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
363 isl_int_init(VectorWidthMP);
364 isl_int_set_si(VectorWidthMP, VectorWidth);
365 Aff = isl_aff_mod(Aff, VectorWidthMP);
366 isl_int_clear(VectorWidthMP);
367 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
368 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000369
Tobias Grosser2493e922011-12-07 07:42:57 +0000370 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000371 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000372 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
373 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
374 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000375
Tobias Grosser2493e922011-12-07 07:42:57 +0000376 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000377 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000378 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
379 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
380 isl_constraint_set_constant_si(c, VectorWidth - 1);
381 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000382
Tobias Grosser2493e922011-12-07 07:42:57 +0000383 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000384}
385
Tobias Grosser460e9a42012-04-25 13:22:43 +0000386isl_union_map *IslScheduleOptimizer::getScheduleForBandList(
387 isl_band_list *BandList) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000388 int NumBands;
389 isl_union_map *Schedule;
390 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000391
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000392 ctx = isl_band_list_get_ctx(BandList);
393 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000394 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000395
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000396 for (int i = 0; i < NumBands; i++) {
397 isl_band *Band;
398 isl_union_map *PartialSchedule;
399 int ScheduleDimensions;
400 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000401
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000402 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000403 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000404 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000405
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000406 if (isl_band_has_children(Band)) {
407 isl_band_list *Children;
408 isl_union_map *SuffixSchedule;
409
410 Children = isl_band_get_children(Band);
411 SuffixSchedule = getScheduleForBandList(Children);
412 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
413 SuffixSchedule);
414 isl_band_list_free(Children);
Hongbin Zheng68794212012-05-06 10:22:19 +0000415 } else if (PollyVectorizerChoice != VECTORIZER_NONE) {
Tobias Grosser216ea582012-04-16 11:06:06 +0000416 for (int j = 0; j < isl_band_n_member(Band); j++) {
417 if (isl_band_member_is_zero_distance(Band, j)) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000418 isl_map *TileMap;
419 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000420
Tobias Grosser216ea582012-04-16 11:06:06 +0000421 TileMap = getPrevectorMap(ctx, ScheduleDimensions - j - 1,
422 ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000423 TileUMap = isl_union_map_from_map(TileMap);
424 TileUMap = isl_union_map_align_params(TileUMap,
425 isl_space_copy(Space));
426 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
427 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000428 break;
429 }
430 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000431 }
432
Tobias Grosser62872012011-11-17 12:56:04 +0000433 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000434
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000435 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000436 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000437 }
438
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000439 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000440}
441
Tobias Grosser460e9a42012-04-25 13:22:43 +0000442isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000443 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
444 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
445 isl_band_list_free(BandList);
446 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000447}
448
Tobias Grosser73600b82011-10-08 00:30:40 +0000449bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000450 Dependences *D = &getAnalysis<Dependences>();
451
452 // Build input data.
Tobias Grosser00383a72012-02-14 14:02:44 +0000453 int ValidityKinds = Dependences::TYPE_RAW | Dependences::TYPE_WAR
454 | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000455 int ProximityKinds;
456
457 if (OptimizeDeps == "all")
458 ProximityKinds = Dependences::TYPE_RAW | Dependences::TYPE_WAR
459 | Dependences::TYPE_WAW;
460 else if (OptimizeDeps == "raw")
Tobias Grosser1e03ad72012-02-15 09:58:42 +0000461 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000462 else {
463 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
464 << " Falling back to optimizing all dependences.\n";
465 ProximityKinds = Dependences::TYPE_RAW | Dependences::TYPE_WAR
466 | Dependences::TYPE_WAW;
467
468 }
469
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000470
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000471 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000472
Tobias Grosser98610ee2012-02-13 23:31:39 +0000473 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000474 return false;
475
Tobias Grosser8a507022012-03-16 11:51:41 +0000476 isl_union_map *Validity = D->getDependences(ValidityKinds);
477 isl_union_map *Proximity = D->getDependences(ProximityKinds);
478
Tobias Grossera26db472012-01-30 19:38:43 +0000479 // Simplify the dependences by removing the constraints introduced by the
480 // domains. This can speed up the scheduling time significantly, as large
481 // constant coefficients will be removed from the dependences. The
482 // introduction of some additional dependences reduces the possible
483 // transformations, but in most cases, such transformation do not seem to be
484 // interesting anyway. In some cases this option may stop the scheduler to
485 // find any schedule.
486 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000487 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
488 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
489 Proximity = isl_union_map_gist_domain(Proximity,
490 isl_union_set_copy(Domain));
491 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000492 } else if (SimplifyDeps != "no") {
493 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
494 "or 'no'. Falling back to default: 'yes'\n";
495 }
496
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000497 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000498 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
499 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000500 dbgs() << ";\n");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000501 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000502 dbgs() << ";\n");
503
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000504 int IslFusionStrategy;
505
506 if (FusionStrategy == "max") {
507 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
508 } else if (FusionStrategy == "min") {
509 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
510 } else {
511 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
512 "fusion.\n";
513 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
514 }
515
Tobias Grosser95e860c2012-01-30 19:38:54 +0000516 int IslMaximizeBands;
517
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000518 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000519 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000520 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000521 IslMaximizeBands = 0;
522 } else {
523 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
524 " or 'no'. Falling back to default: 'yes'\n";
525 IslMaximizeBands = 1;
526 }
527
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000528 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000529 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000530 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000531 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000532
533 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grosser00383a72012-02-14 14:02:44 +0000534 isl_schedule *Schedule;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000535 Schedule = isl_union_set_compute_schedule(Domain, Validity, Proximity);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000536 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
537
538 // In cases the scheduler is not able to optimize the code, we just do not
539 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000540 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000541 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000542
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000543 DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule);
544 dbgs() << ";\n");
545
Tobias Grosser98610ee2012-02-13 23:31:39 +0000546 isl_union_map *ScheduleMap = getScheduleMap(Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000547
Tobias Grosserde68cc92011-06-30 20:01:02 +0000548 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grosser98610ee2012-02-13 23:31:39 +0000549 ScopStmt *Stmt = *SI;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000550 isl_set *Domain = Stmt->getDomain();
551 isl_union_map *StmtBand;
552 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
553 isl_union_set_from_set(Domain));
554 isl_map *StmtSchedule;
555 isl_union_map_foreach_map(StmtBand, getSingleMap, &StmtSchedule);
556 Stmt->setScattering(StmtSchedule);
557 isl_union_map_free(StmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000558 }
559
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000560 isl_union_map_free(ScheduleMap);
Tobias Grosser98610ee2012-02-13 23:31:39 +0000561 isl_schedule_free(Schedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000562
Tobias Grosser98610ee2012-02-13 23:31:39 +0000563 unsigned MaxScatDims = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000564
565 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grosser98610ee2012-02-13 23:31:39 +0000566 MaxScatDims = std::max((*SI)->getNumScattering(), MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000567
Tobias Grosser98610ee2012-02-13 23:31:39 +0000568 extendScattering(S, MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000569 return false;
570}
571
Tobias Grosser73600b82011-10-08 00:30:40 +0000572void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000573}
574
Tobias Grosser73600b82011-10-08 00:30:40 +0000575void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000576 ScopPass::getAnalysisUsage(AU);
577 AU.addRequired<Dependences>();
578}
579
Tobias Grosser4dca4392011-11-22 19:40:19 +0000580INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000581 "Polly - Optimize schedule of SCoP", false, false)
582INITIALIZE_PASS_DEPENDENCY(Dependences)
583INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000584INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000585 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000586
Tobias Grosser73600b82011-10-08 00:30:40 +0000587Pass* polly::createIslScheduleOptimizerPass() {
588 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000589}