blob: f95a3b49a6a19a96fde79c0b059129d281e68eb1 [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"
Tobias Grosser2493e922011-12-07 07:42:57 +000021#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000022#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000023#include "isl/constraint.h"
24#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000025#include "isl/options.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000026#include "isl/schedule.h"
27#include "isl/space.h"
Tobias Grosser83628182013-05-07 08:11:54 +000028#include "polly/CodeGen/CodeGeneration.h"
29#include "polly/Dependences.h"
30#include "polly/LinkAllPasses.h"
31#include "polly/Options.h"
32#include "polly/ScopInfo.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000033#include "llvm/Support/Debug.h"
34
35using namespace llvm;
36using namespace polly;
37
Chandler Carruth95fef942014-04-22 03:30:19 +000038#define DEBUG_TYPE "polly-opt-isl"
39
Tobias Grosser58032cb2013-06-23 01:29:29 +000040namespace polly {
41bool DisablePollyTiling;
42}
Tobias Grossere602a072013-05-07 07:30:56 +000043static cl::opt<bool, true>
44DisableTiling("polly-no-tiling", cl::desc("Disable tiling in the scheduler"),
Tobias Grosser637bd632013-05-07 07:31:10 +000045 cl::location(polly::DisablePollyTiling), cl::init(false),
Tobias Grosser64e8e372014-03-13 23:37:43 +000046 cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser353a2682011-10-23 20:59:26 +000047
Tobias Grossera26db472012-01-30 19:38:43 +000048static cl::opt<std::string>
Tobias Grosser1deda292012-02-14 14:02:48 +000049OptimizeDeps("polly-opt-optimize-only",
50 cl::desc("Only a certain kind of dependences (all/raw)"),
Tobias Grosser64e8e372014-03-13 23:37:43 +000051 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
52 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000053
54static cl::opt<std::string>
Tobias Grossera26db472012-01-30 19:38:43 +000055SimplifyDeps("polly-opt-simplify-deps",
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000056 cl::desc("Dependences should be simplified (yes/no)"), cl::Hidden,
Tobias Grosser64e8e372014-03-13 23:37:43 +000057 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000058
Tobias Grosser992e60c2012-02-20 08:41:15 +000059static cl::opt<int>
60MaxConstantTerm("polly-opt-max-constant-term",
61 cl::desc("The maximal constant term allowed (-1 is unlimited)"),
Tobias Grosser64e8e372014-03-13 23:37:43 +000062 cl::Hidden, cl::init(20), cl::ZeroOrMore,
63 cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000064
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)"),
Tobias Grosser64e8e372014-03-13 23:37:43 +000068 cl::Hidden, cl::init(20), cl::ZeroOrMore,
69 cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +000070
Tobias Grossere602a072013-05-07 07:30:56 +000071static cl::opt<std::string>
72FusionStrategy("polly-opt-fusion",
73 cl::desc("The fusion strategy to choose (min/max)"), cl::Hidden,
Tobias Grosser64e8e372014-03-13 23:37:43 +000074 cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000075
Tobias Grossere602a072013-05-07 07:30:56 +000076static cl::opt<std::string>
77MaximizeBandDepth("polly-opt-maximize-bands",
78 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
Tobias Grosser64e8e372014-03-13 23:37:43 +000079 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser95e860c2012-01-30 19:38:54 +000080
Johannes Doerfertc3958b22014-05-28 17:21:02 +000081static cl::opt<int>
82DefaultTileSize("polly-default-tile-size",
83 cl::desc("The default tile size (if not enough were provided by"
84 " --polly-tile-sizes)"),
85 cl::Hidden, cl::init(32), cl::ZeroOrMore,
86 cl::cat(PollyCategory));
87
88static cl::list<int> TileSizes("polly-tile-sizes",
89 cl::desc("A tile size"
90 " for each loop dimension, filled with"
91 " --polly-default-tile-size"),
92 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
93 cl::cat(PollyCategory));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000094namespace {
95
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000096class IslScheduleOptimizer : public ScopPass {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000097public:
98 static char ID;
Tobias Grosser5a56cbf2014-04-16 07:33:47 +000099 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
Tobias Grosser28781422012-10-16 07:29:19 +0000100
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000101 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000102
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000103 virtual bool runOnScop(Scop &S);
104 void printScop(llvm::raw_ostream &OS) const;
105 void getAnalysisUsage(AnalysisUsage &AU) const;
Tobias Grosser460e9a42012-04-25 13:22:43 +0000106
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000107private:
108 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +0000109
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000110 static void extendScattering(Scop &S, unsigned NewDimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000111
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000112 /// @brief Create a map that describes a n-dimensonal tiling.
113 ///
114 /// getTileMap creates a map from a n-dimensional scattering space into an
115 /// 2*n-dimensional scattering space. The map describes a rectangular
116 /// tiling.
117 ///
118 /// Example:
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000119 /// scheduleDimensions = 2, parameterDimensions = 1, TileSizes = <32, 64>
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000120 ///
121 /// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
122 /// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000123 /// t1 % 64 = 0 and t1 <= s1 < t1 + 64}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000124 ///
125 /// Before tiling:
126 ///
127 /// for (i = 0; i < N; i++)
128 /// for (j = 0; j < M; j++)
129 /// S(i,j)
130 ///
131 /// After tiling:
132 ///
133 /// for (t_i = 0; t_i < N; i+=32)
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000134 /// for (t_j = 0; t_j < M; j+=64)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000135 /// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000136 /// for (j = t_j; j < t_j + 64; j++) | Known that M % 64 = 0
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000137 /// S(i,j)
138 ///
139 static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000140 isl_space *SpaceModel);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000141
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000142 /// @brief Get the schedule for this band.
143 ///
144 /// Polly applies transformations like tiling on top of the isl calculated
145 /// value. This can influence the number of scheduling dimension. The
146 /// number of schedule dimensions is returned in the parameter 'Dimension'.
147 static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000148
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000149 /// @brief Create a map that pre-vectorizes one scheduling dimension.
150 ///
151 /// getPrevectorMap creates a map that maps each input dimension to the same
152 /// output dimension, except for the dimension DimToVectorize.
153 /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
154 /// point loop of DimToVectorize is moved to the innermost level.
155 ///
156 /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
157 ///
158 /// | Before transformation
159 /// |
160 /// | A[i,j] -> [i,j]
161 /// |
162 /// | for (i = 0; i < 128; i++)
163 /// | for (j = 0; j < 128; j++)
164 /// | A(i,j);
165 ///
166 /// Prevector map:
167 /// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
168 ///
169 /// | After transformation:
170 /// |
171 /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
172 /// |
173 /// | for (it = 0; it < 128; it+=4)
174 /// | for (j = 0; j < 128; j++)
175 /// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
176 /// | A(ip,j);
177 ///
178 /// The goal of this transformation is to create a trivially vectorizable
179 /// loop. This means a parallel loop at the innermost level that has a
180 /// constant number of iterations corresponding to the target vector width.
181 ///
182 /// This transformation creates a loop at the innermost level. The loop has
183 /// a constant number of iterations, if the number of loop iterations at
184 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
185 /// currently constant and not yet target specific. This function does not
186 /// reason about parallelism.
187 static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
188 int ScheduleDimensions, int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000189
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000190 /// @brief Get the scheduling map for a list of bands.
191 ///
192 /// Walk recursively the forest of bands to combine the schedules of the
193 /// individual bands to the overall schedule. In case tiling is requested,
194 /// the individual bands are tiled.
195 static isl_union_map *getScheduleForBandList(isl_band_list *BandList);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000196
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000197 static isl_union_map *getScheduleMap(isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000198
Tobias Grosser20532b82014-04-11 17:56:49 +0000199 using llvm::Pass::doFinalization;
200
201 virtual bool doFinalization() {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000202 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000203 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000204 return true;
205 }
206};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000207}
208
Tobias Grosser73600b82011-10-08 00:30:40 +0000209char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000210
Tobias Grosser460e9a42012-04-25 13:22:43 +0000211void IslScheduleOptimizer::extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser083d3d32014-06-28 08:59:45 +0000212 for (ScopStmt *Stmt : S) {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000213 unsigned OldDimensions = Stmt->getNumScattering();
214 isl_space *Space;
Tobias Grosser29666112012-05-22 10:47:31 +0000215 isl_map *Map, *New;
Tobias Grossercf3942d2011-10-06 00:04:05 +0000216
217 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
Tobias Grosser29666112012-05-22 10:47:31 +0000218 Map = isl_map_universe(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000219
Tobias Grosser29666112012-05-22 10:47:31 +0000220 for (unsigned i = 0; i < OldDimensions; i++)
221 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000222
Tobias Grosser29666112012-05-22 10:47:31 +0000223 for (unsigned i = OldDimensions; i < NewDimensions; i++)
224 Map = isl_map_fix_si(Map, isl_dim_out, i, 0);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000225
Tobias Grosser29666112012-05-22 10:47:31 +0000226 Map = isl_map_align_params(Map, S.getParamSpace());
227 New = isl_map_apply_range(Stmt->getScattering(), Map);
228 Stmt->setScattering(New);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000229 }
230}
231
Tobias Grossere602a072013-05-07 07:30:56 +0000232isl_basic_map *IslScheduleOptimizer::getTileMap(isl_ctx *ctx,
233 int scheduleDimensions,
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000234 isl_space *SpaceModel) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000235 // We construct
236 //
237 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000238 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 64 and
239 // s1 = a1 * 64 and s1 = p1 and t1 <= p1 < t1 + 64}
Tobias Grosserde68cc92011-06-30 20:01:02 +0000240 //
241 // and project out the auxilary dimensions a0 and a1.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000242 isl_space *Space =
243 isl_space_alloc(ctx, 0, scheduleDimensions, scheduleDimensions * 3);
Tobias Grosserf5338802011-10-06 00:03:35 +0000244 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
245
246 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000247
Tobias Grosserde68cc92011-06-30 20:01:02 +0000248 for (int x = 0; x < scheduleDimensions; x++) {
249 int sX = x;
250 int tX = x;
251 int pX = scheduleDimensions + x;
252 int aX = 2 * scheduleDimensions + x;
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000253 int tileSize = (int)TileSizes.size() > x ? TileSizes[x] : DefaultTileSize;
254 assert(tileSize > 0 && "Invalid tile size");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000255
Tobias Grosserde68cc92011-06-30 20:01:02 +0000256 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000257
Tobias Grosserde68cc92011-06-30 20:01:02 +0000258 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000259 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000260 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
261 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
262 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000263
Tobias Grosserde68cc92011-06-30 20:01:02 +0000264 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000265 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000266 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
267 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
268 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000269
Tobias Grosserde68cc92011-06-30 20:01:02 +0000270 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000271 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000272 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
273 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
274 tileMap = isl_basic_map_add_constraint(tileMap, c);
275
276 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000277 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000278 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
279 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000280 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000281 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000282 }
283
Tobias Grosserde68cc92011-06-30 20:01:02 +0000284 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000285 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000286 // The auxilary dimensions are transformed into existentially quantified ones.
287 // This reduces the number of visible scattering dimensions and allows Cloog
288 // to produces better code.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000289 tileMap = isl_basic_map_project_out(
290 tileMap, isl_dim_out, 2 * scheduleDimensions, scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000291 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000292 return tileMap;
293}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000294
Tobias Grossere602a072013-05-07 07:30:56 +0000295isl_union_map *IslScheduleOptimizer::getScheduleForBand(isl_band *Band,
296 int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000297 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000298 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000299 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000300 isl_basic_map *TileMap;
301 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000302
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000303 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000304 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000305
Tobias Grosser79b30202011-11-17 12:56:00 +0000306 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000307 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000308
Tobias Grosserb6033392011-12-08 13:02:58 +0000309 // It does not make any sense to tile a band with just one dimension.
310 if (*Dimensions == 1)
311 return PartialSchedule;
312
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000313 ctx = isl_union_map_get_ctx(PartialSchedule);
314 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000315
Tobias Grosserb6033392011-12-08 13:02:58 +0000316 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000317 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
318 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000319 *Dimensions = 2 * *Dimensions;
320
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000321 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000322}
323
Tobias Grossere602a072013-05-07 07:30:56 +0000324isl_map *IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
325 int ScheduleDimensions,
326 int VectorWidth) {
Tobias Grosser2493e922011-12-07 07:42:57 +0000327 isl_space *Space;
328 isl_local_space *LocalSpace, *LocalSpaceRange;
329 isl_set *Modulo;
330 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000331 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000332 isl_aff *Aff;
333 int PointDimension; /* ip */
334 int TileDimension; /* it */
Tobias Grosseredab1352013-06-21 06:41:31 +0000335 isl_val *VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000336
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000337 assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000338
Tobias Grosser2493e922011-12-07 07:42:57 +0000339 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
340 TilingMap = isl_map_universe(isl_space_copy(Space));
341 LocalSpace = isl_local_space_from_space(Space);
342 PointDimension = ScheduleDimensions;
343 TileDimension = DimToVectorize;
344
345 // Create an identity map for everything except DimToVectorize and map
346 // DimToVectorize to the point loop at the innermost dimension.
347 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000348 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000349 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000350
351 if (i == DimToVectorize)
352 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
353 else
354 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
355
356 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000357 }
358
Tobias Grosser2493e922011-12-07 07:42:57 +0000359 // it % 'VectorWidth' = 0
360 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
361 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
362 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
363 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
Tobias Grosseredab1352013-06-21 06:41:31 +0000364 VectorWidthMP = isl_val_int_from_si(ctx, VectorWidth);
365 Aff = isl_aff_mod_val(Aff, VectorWidthMP);
Tobias Grosser2493e922011-12-07 07:42:57 +0000366 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
367 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000368
Tobias Grosser2493e922011-12-07 07:42:57 +0000369 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000370 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000371 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
372 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
373 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000374
Tobias Grosser2493e922011-12-07 07:42:57 +0000375 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000376 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000377 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
378 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
379 isl_constraint_set_constant_si(c, VectorWidth - 1);
380 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000381
Tobias Grosser2493e922011-12-07 07:42:57 +0000382 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000383}
384
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000385isl_union_map *
386IslScheduleOptimizer::getScheduleForBandList(isl_band_list *BandList) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000387 int NumBands;
388 isl_union_map *Schedule;
389 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000390
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000391 ctx = isl_band_list_get_ctx(BandList);
392 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000393 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000394
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000395 for (int i = 0; i < NumBands; i++) {
396 isl_band *Band;
397 isl_union_map *PartialSchedule;
398 int ScheduleDimensions;
399 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000400
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000401 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000402 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000403 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000404
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000405 if (isl_band_has_children(Band)) {
406 isl_band_list *Children;
407 isl_union_map *SuffixSchedule;
408
409 Children = isl_band_get_children(Band);
410 SuffixSchedule = getScheduleForBandList(Children);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000411 PartialSchedule =
412 isl_union_map_flat_range_product(PartialSchedule, SuffixSchedule);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000413 isl_band_list_free(Children);
Hongbin Zheng68794212012-05-06 10:22:19 +0000414 } else if (PollyVectorizerChoice != VECTORIZER_NONE) {
Tobias Grosser4ba60fe2014-03-11 06:27:36 +0000415 // In case we are at the innermost band, we try to prepare for
416 // vectorization. This means, we look for the innermost parallel loop
417 // and strip mine this loop to the innermost level using a strip-mine
418 // factor corresponding to the number of vector iterations.
419 int NumDims = isl_band_n_member(Band);
420 for (int j = NumDims - 1; j >= 0; j--) {
Tobias Grossera38c9242014-01-26 19:36:28 +0000421 if (isl_band_member_is_coincident(Band, j)) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000422 isl_map *TileMap;
423 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000424
Tobias Grosser4ba60fe2014-03-11 06:27:36 +0000425 TileMap = getPrevectorMap(ctx, ScheduleDimensions - NumDims + j,
Tobias Grosser216ea582012-04-16 11:06:06 +0000426 ScheduleDimensions);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000427 TileUMap = isl_union_map_from_map(TileMap);
428 TileUMap =
429 isl_union_map_align_params(TileUMap, isl_space_copy(Space));
430 PartialSchedule =
431 isl_union_map_apply_range(PartialSchedule, TileUMap);
432 break;
433 }
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000434 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000435 }
436
Tobias Grosser62872012011-11-17 12:56:04 +0000437 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000438
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000439 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000440 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000441 }
442
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000443 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000444}
445
Tobias Grosser460e9a42012-04-25 13:22:43 +0000446isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000447 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
448 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
449 isl_band_list_free(BandList);
450 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000451}
452
Tobias Grosser73600b82011-10-08 00:30:40 +0000453bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000454 Dependences *D = &getAnalysis<Dependences>();
455
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000456 if (!D->hasValidDependences())
457 return false;
458
Tobias Grosser28781422012-10-16 07:29:19 +0000459 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000460 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000461
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000462 // Build input data.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000463 int ValidityKinds =
464 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000465 int ProximityKinds;
466
467 if (OptimizeDeps == "all")
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000468 ProximityKinds =
469 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000470 else if (OptimizeDeps == "raw")
Tobias Grosser1e03ad72012-02-15 09:58:42 +0000471 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000472 else {
473 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000474 << " Falling back to optimizing all dependences.\n";
475 ProximityKinds =
476 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000477 }
478
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000479 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000480
Tobias Grosser98610ee2012-02-13 23:31:39 +0000481 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000482 return false;
483
Tobias Grosser8a507022012-03-16 11:51:41 +0000484 isl_union_map *Validity = D->getDependences(ValidityKinds);
485 isl_union_map *Proximity = D->getDependences(ProximityKinds);
486
Tobias Grossera26db472012-01-30 19:38:43 +0000487 // Simplify the dependences by removing the constraints introduced by the
488 // domains. This can speed up the scheduling time significantly, as large
489 // constant coefficients will be removed from the dependences. The
490 // introduction of some additional dependences reduces the possible
491 // transformations, but in most cases, such transformation do not seem to be
492 // interesting anyway. In some cases this option may stop the scheduler to
493 // find any schedule.
494 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000495 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
496 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000497 Proximity =
498 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000499 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000500 } else if (SimplifyDeps != "no") {
501 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
502 "or 'no'. Falling back to default: 'yes'\n";
503 }
504
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000505 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000506 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
507 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000508 dbgs() << ";\n");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000509 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000510 dbgs() << ";\n");
511
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000512 int IslFusionStrategy;
513
514 if (FusionStrategy == "max") {
515 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
516 } else if (FusionStrategy == "min") {
517 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
518 } else {
519 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
520 "fusion.\n";
521 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
522 }
523
Tobias Grosser95e860c2012-01-30 19:38:54 +0000524 int IslMaximizeBands;
525
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000526 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000527 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000528 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000529 IslMaximizeBands = 0;
530 } else {
531 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
532 " or 'no'. Falling back to default: 'yes'\n";
533 IslMaximizeBands = 1;
534 }
535
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000536 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000537 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000538 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000539 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000540
541 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000542
543 isl_schedule_constraints *ScheduleConstraints;
544 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
545 ScheduleConstraints =
546 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
547 ScheduleConstraints = isl_schedule_constraints_set_validity(
548 ScheduleConstraints, isl_union_map_copy(Validity));
549 ScheduleConstraints =
550 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000551 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000552 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000553 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
554
555 // In cases the scheduler is not able to optimize the code, we just do not
556 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000557 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000558 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000559
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000560 DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule); dbgs() << ";\n");
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000561
Tobias Grosser98610ee2012-02-13 23:31:39 +0000562 isl_union_map *ScheduleMap = getScheduleMap(Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000563
Tobias Grosser083d3d32014-06-28 08:59:45 +0000564 for (ScopStmt *Stmt : S) {
Tobias Grosser249c4b12013-04-11 05:55:13 +0000565 isl_map *StmtSchedule;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000566 isl_set *Domain = Stmt->getDomain();
567 isl_union_map *StmtBand;
568 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000569 isl_union_set_from_set(Domain));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000570 if (isl_union_map_is_empty(StmtBand)) {
Tobias Grosser34f06132014-02-21 20:51:36 +0000571 StmtSchedule = isl_map_from_domain(isl_set_empty(Stmt->getDomainSpace()));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000572 isl_union_map_free(StmtBand);
573 } else {
574 assert(isl_union_map_n_map(StmtBand) == 1);
575 StmtSchedule = isl_map_from_union_map(StmtBand);
Tobias Grosserf242b802013-04-10 22:48:08 +0000576 }
577
Tobias Grosser98610ee2012-02-13 23:31:39 +0000578 Stmt->setScattering(StmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000579 }
580
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000581 isl_union_map_free(ScheduleMap);
Tobias Grosser28781422012-10-16 07:29:19 +0000582 LastSchedule = Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000583
Tobias Grosser98610ee2012-02-13 23:31:39 +0000584 unsigned MaxScatDims = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000585
Tobias Grosser083d3d32014-06-28 08:59:45 +0000586 for (ScopStmt *Stmt : S)
587 MaxScatDims = std::max(Stmt->getNumScattering(), MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000588
Tobias Grosser98610ee2012-02-13 23:31:39 +0000589 extendScattering(S, MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000590 return false;
591}
592
Tobias Grosser73600b82011-10-08 00:30:40 +0000593void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000594 isl_printer *p;
595 char *ScheduleStr;
596
597 OS << "Calculated schedule:\n";
598
599 if (!LastSchedule) {
600 OS << "n/a\n";
601 return;
602 }
603
604 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
605 p = isl_printer_print_schedule(p, LastSchedule);
606 ScheduleStr = isl_printer_get_str(p);
607 isl_printer_free(p);
608
609 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000610}
611
Tobias Grosser73600b82011-10-08 00:30:40 +0000612void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000613 ScopPass::getAnalysisUsage(AU);
614 AU.addRequired<Dependences>();
615}
616
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000617Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000618 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000619}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000620
621INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
622 "Polly - Optimize schedule of SCoP", false, false);
623INITIALIZE_PASS_DEPENDENCY(Dependences);
624INITIALIZE_PASS_DEPENDENCY(ScopInfo);
625INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
626 "Polly - Optimize schedule of SCoP", false, false)