blob: 0e086d172a3207f63420081679654022c617f359 [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>
Tobias Grosser483a90d2014-07-09 10:50:10 +000044 DisableTiling("polly-no-tiling",
45 cl::desc("Disable tiling in the scheduler"),
46 cl::location(polly::DisablePollyTiling), cl::init(false),
47 cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser353a2682011-10-23 20:59:26 +000048
Tobias Grossera26db472012-01-30 19:38:43 +000049static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000050 OptimizeDeps("polly-opt-optimize-only",
51 cl::desc("Only a certain kind of dependences (all/raw)"),
52 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
53 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000054
55static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000056 SimplifyDeps("polly-opt-simplify-deps",
57 cl::desc("Dependences should be simplified (yes/no)"),
58 cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
59 cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000060
Tobias Grosser483a90d2014-07-09 10:50:10 +000061static cl::opt<int> MaxConstantTerm(
62 "polly-opt-max-constant-term",
63 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
64 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000065
Tobias Grosser483a90d2014-07-09 10:50:10 +000066static cl::opt<int> MaxCoefficient(
67 "polly-opt-max-coefficient",
68 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
69 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
70
71static cl::opt<std::string> FusionStrategy(
72 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
73 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +000074
Tobias Grossere602a072013-05-07 07:30:56 +000075static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000076 MaximizeBandDepth("polly-opt-maximize-bands",
77 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
78 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000079
Tobias Grosser483a90d2014-07-09 10:50:10 +000080static cl::opt<int> DefaultTileSize(
81 "polly-default-tile-size",
82 cl::desc("The default tile size (if not enough were provided by"
83 " --polly-tile-sizes)"),
84 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertc3958b22014-05-28 17:21:02 +000085
86static cl::list<int> TileSizes("polly-tile-sizes",
87 cl::desc("A tile size"
88 " for each loop dimension, filled with"
89 " --polly-default-tile-size"),
90 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
91 cl::cat(PollyCategory));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000092namespace {
93
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000094class IslScheduleOptimizer : public ScopPass {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000095public:
96 static char ID;
Tobias Grosser5a56cbf2014-04-16 07:33:47 +000097 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
Tobias Grosser28781422012-10-16 07:29:19 +000098
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000099 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000100
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000101 virtual bool runOnScop(Scop &S);
102 void printScop(llvm::raw_ostream &OS) const;
103 void getAnalysisUsage(AnalysisUsage &AU) const;
Tobias Grosser460e9a42012-04-25 13:22:43 +0000104
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000105private:
106 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +0000107
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000108 static void extendScattering(Scop &S, unsigned NewDimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000109
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000110 /// @brief Create a map that describes a n-dimensonal tiling.
111 ///
112 /// getTileMap creates a map from a n-dimensional scattering space into an
113 /// 2*n-dimensional scattering space. The map describes a rectangular
114 /// tiling.
115 ///
116 /// Example:
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000117 /// scheduleDimensions = 2, parameterDimensions = 1, TileSizes = <32, 64>
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000118 ///
119 /// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
120 /// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000121 /// t1 % 64 = 0 and t1 <= s1 < t1 + 64}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000122 ///
123 /// Before tiling:
124 ///
125 /// for (i = 0; i < N; i++)
126 /// for (j = 0; j < M; j++)
127 /// S(i,j)
128 ///
129 /// After tiling:
130 ///
131 /// for (t_i = 0; t_i < N; i+=32)
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000132 /// for (t_j = 0; t_j < M; j+=64)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000133 /// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000134 /// for (j = t_j; j < t_j + 64; j++) | Known that M % 64 = 0
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000135 /// S(i,j)
136 ///
137 static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000138 isl_space *SpaceModel);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000139
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000140 /// @brief Get the schedule for this band.
141 ///
142 /// Polly applies transformations like tiling on top of the isl calculated
143 /// value. This can influence the number of scheduling dimension. The
144 /// number of schedule dimensions is returned in the parameter 'Dimension'.
145 static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000146
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000147 /// @brief Create a map that pre-vectorizes one scheduling dimension.
148 ///
149 /// getPrevectorMap creates a map that maps each input dimension to the same
150 /// output dimension, except for the dimension DimToVectorize.
151 /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
152 /// point loop of DimToVectorize is moved to the innermost level.
153 ///
154 /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
155 ///
156 /// | Before transformation
157 /// |
158 /// | A[i,j] -> [i,j]
159 /// |
160 /// | for (i = 0; i < 128; i++)
161 /// | for (j = 0; j < 128; j++)
162 /// | A(i,j);
163 ///
164 /// Prevector map:
165 /// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
166 ///
167 /// | After transformation:
168 /// |
169 /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
170 /// |
171 /// | for (it = 0; it < 128; it+=4)
172 /// | for (j = 0; j < 128; j++)
173 /// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
174 /// | A(ip,j);
175 ///
176 /// The goal of this transformation is to create a trivially vectorizable
177 /// loop. This means a parallel loop at the innermost level that has a
178 /// constant number of iterations corresponding to the target vector width.
179 ///
180 /// This transformation creates a loop at the innermost level. The loop has
181 /// a constant number of iterations, if the number of loop iterations at
182 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
183 /// currently constant and not yet target specific. This function does not
184 /// reason about parallelism.
185 static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
186 int ScheduleDimensions, int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000187
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000188 /// @brief Get the scheduling map for a list of bands.
189 ///
190 /// Walk recursively the forest of bands to combine the schedules of the
191 /// individual bands to the overall schedule. In case tiling is requested,
192 /// the individual bands are tiled.
193 static isl_union_map *getScheduleForBandList(isl_band_list *BandList);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000194
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000195 static isl_union_map *getScheduleMap(isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000196
Tobias Grosser20532b82014-04-11 17:56:49 +0000197 using llvm::Pass::doFinalization;
198
199 virtual bool doFinalization() {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000200 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000201 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000202 return true;
203 }
204};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000205}
206
Tobias Grosser73600b82011-10-08 00:30:40 +0000207char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000208
Tobias Grosser460e9a42012-04-25 13:22:43 +0000209void IslScheduleOptimizer::extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser083d3d32014-06-28 08:59:45 +0000210 for (ScopStmt *Stmt : S) {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000211 unsigned OldDimensions = Stmt->getNumScattering();
212 isl_space *Space;
Tobias Grosser29666112012-05-22 10:47:31 +0000213 isl_map *Map, *New;
Tobias Grossercf3942d2011-10-06 00:04:05 +0000214
215 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
Tobias Grosser29666112012-05-22 10:47:31 +0000216 Map = isl_map_universe(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000217
Tobias Grosser29666112012-05-22 10:47:31 +0000218 for (unsigned i = 0; i < OldDimensions; i++)
219 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000220
Tobias Grosser29666112012-05-22 10:47:31 +0000221 for (unsigned i = OldDimensions; i < NewDimensions; i++)
222 Map = isl_map_fix_si(Map, isl_dim_out, i, 0);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000223
Tobias Grosser29666112012-05-22 10:47:31 +0000224 Map = isl_map_align_params(Map, S.getParamSpace());
225 New = isl_map_apply_range(Stmt->getScattering(), Map);
226 Stmt->setScattering(New);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000227 }
228}
229
Tobias Grossere602a072013-05-07 07:30:56 +0000230isl_basic_map *IslScheduleOptimizer::getTileMap(isl_ctx *ctx,
231 int scheduleDimensions,
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000232 isl_space *SpaceModel) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000233 // We construct
234 //
235 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000236 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 64 and
237 // s1 = a1 * 64 and s1 = p1 and t1 <= p1 < t1 + 64}
Tobias Grosserde68cc92011-06-30 20:01:02 +0000238 //
239 // and project out the auxilary dimensions a0 and a1.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000240 isl_space *Space =
241 isl_space_alloc(ctx, 0, scheduleDimensions, scheduleDimensions * 3);
Tobias Grosserf5338802011-10-06 00:03:35 +0000242 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
243
244 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000245
Tobias Grosserde68cc92011-06-30 20:01:02 +0000246 for (int x = 0; x < scheduleDimensions; x++) {
247 int sX = x;
248 int tX = x;
249 int pX = scheduleDimensions + x;
250 int aX = 2 * scheduleDimensions + x;
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000251 int tileSize = (int)TileSizes.size() > x ? TileSizes[x] : DefaultTileSize;
252 assert(tileSize > 0 && "Invalid tile size");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000253
Tobias Grosserde68cc92011-06-30 20:01:02 +0000254 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000255
Tobias Grosserde68cc92011-06-30 20:01:02 +0000256 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000257 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000258 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
259 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
260 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000261
Tobias Grosserde68cc92011-06-30 20:01:02 +0000262 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000263 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000264 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
265 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
266 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000267
Tobias Grosserde68cc92011-06-30 20:01:02 +0000268 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000269 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000270 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
271 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
272 tileMap = isl_basic_map_add_constraint(tileMap, c);
273
274 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000275 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000276 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
277 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000278 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000279 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000280 }
281
Tobias Grosserde68cc92011-06-30 20:01:02 +0000282 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000283 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000284 // The auxilary dimensions are transformed into existentially quantified ones.
285 // This reduces the number of visible scattering dimensions and allows Cloog
286 // to produces better code.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000287 tileMap = isl_basic_map_project_out(
288 tileMap, isl_dim_out, 2 * scheduleDimensions, 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 Grossere602a072013-05-07 07:30:56 +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 Grossere602a072013-05-07 07:30:56 +0000322isl_map *IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
323 int ScheduleDimensions,
324 int VectorWidth) {
Tobias Grosser2493e922011-12-07 07:42:57 +0000325 isl_space *Space;
326 isl_local_space *LocalSpace, *LocalSpaceRange;
327 isl_set *Modulo;
328 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000329 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000330 isl_aff *Aff;
331 int PointDimension; /* ip */
332 int TileDimension; /* it */
Tobias Grosseredab1352013-06-21 06:41:31 +0000333 isl_val *VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000334
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000335 assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000336
Tobias Grosser2493e922011-12-07 07:42:57 +0000337 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
338 TilingMap = isl_map_universe(isl_space_copy(Space));
339 LocalSpace = isl_local_space_from_space(Space);
340 PointDimension = ScheduleDimensions;
341 TileDimension = DimToVectorize;
342
343 // Create an identity map for everything except DimToVectorize and map
344 // DimToVectorize to the point loop at the innermost dimension.
345 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000346 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Johannes Doerfert495dd052014-08-20 17:15:34 +0000347 c = isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000348
349 if (i == DimToVectorize)
Johannes Doerfert495dd052014-08-20 17:15:34 +0000350 c = isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000351 else
Johannes Doerfert495dd052014-08-20 17:15:34 +0000352 c = isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000353
354 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000355 }
356
Tobias Grosser2493e922011-12-07 07:42:57 +0000357 // it % 'VectorWidth' = 0
358 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
359 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
360 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
361 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
Tobias Grosseredab1352013-06-21 06:41:31 +0000362 VectorWidthMP = isl_val_int_from_si(ctx, VectorWidth);
363 Aff = isl_aff_mod_val(Aff, VectorWidthMP);
Tobias Grosser2493e922011-12-07 07:42:57 +0000364 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
365 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000366
Tobias Grosser2493e922011-12-07 07:42:57 +0000367 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000368 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000369 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
370 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
371 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000372
Tobias Grosser2493e922011-12-07 07:42:57 +0000373 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000374 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000375 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
376 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
377 isl_constraint_set_constant_si(c, VectorWidth - 1);
378 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000379
Tobias Grosser2493e922011-12-07 07:42:57 +0000380 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000381}
382
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000383isl_union_map *
384IslScheduleOptimizer::getScheduleForBandList(isl_band_list *BandList) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000385 int NumBands;
386 isl_union_map *Schedule;
387 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000388
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000389 ctx = isl_band_list_get_ctx(BandList);
390 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000391 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000392
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000393 for (int i = 0; i < NumBands; i++) {
394 isl_band *Band;
395 isl_union_map *PartialSchedule;
396 int ScheduleDimensions;
397 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000398
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000399 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000400 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000401 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000402
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000403 if (isl_band_has_children(Band)) {
404 isl_band_list *Children;
405 isl_union_map *SuffixSchedule;
406
407 Children = isl_band_get_children(Band);
408 SuffixSchedule = getScheduleForBandList(Children);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000409 PartialSchedule =
410 isl_union_map_flat_range_product(PartialSchedule, SuffixSchedule);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000411 isl_band_list_free(Children);
Hongbin Zheng68794212012-05-06 10:22:19 +0000412 } else if (PollyVectorizerChoice != VECTORIZER_NONE) {
Tobias Grosser4ba60fe2014-03-11 06:27:36 +0000413 // In case we are at the innermost band, we try to prepare for
414 // vectorization. This means, we look for the innermost parallel loop
415 // and strip mine this loop to the innermost level using a strip-mine
416 // factor corresponding to the number of vector iterations.
417 int NumDims = isl_band_n_member(Band);
418 for (int j = NumDims - 1; j >= 0; j--) {
Tobias Grossera38c9242014-01-26 19:36:28 +0000419 if (isl_band_member_is_coincident(Band, j)) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000420 isl_map *TileMap;
421 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000422
Tobias Grosser4ba60fe2014-03-11 06:27:36 +0000423 TileMap = getPrevectorMap(ctx, ScheduleDimensions - NumDims + j,
Tobias Grosser216ea582012-04-16 11:06:06 +0000424 ScheduleDimensions);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000425 TileUMap = isl_union_map_from_map(TileMap);
426 TileUMap =
427 isl_union_map_align_params(TileUMap, isl_space_copy(Space));
428 PartialSchedule =
429 isl_union_map_apply_range(PartialSchedule, TileUMap);
430 break;
431 }
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000432 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000433 }
434
Tobias Grosser62872012011-11-17 12:56:04 +0000435 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000436
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000437 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000438 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000439 }
440
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000441 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000442}
443
Tobias Grosser460e9a42012-04-25 13:22:43 +0000444isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000445 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
446 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
447 isl_band_list_free(BandList);
448 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000449}
450
Tobias Grosser73600b82011-10-08 00:30:40 +0000451bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000452 Dependences *D = &getAnalysis<Dependences>();
453
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000454 if (!D->hasValidDependences())
455 return false;
456
Tobias Grosser28781422012-10-16 07:29:19 +0000457 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000458 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000459
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000460 // Build input data.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000461 int ValidityKinds =
462 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000463 int ProximityKinds;
464
465 if (OptimizeDeps == "all")
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000466 ProximityKinds =
467 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000468 else if (OptimizeDeps == "raw")
Tobias Grosser1e03ad72012-02-15 09:58:42 +0000469 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000470 else {
471 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000472 << " Falling back to optimizing all dependences.\n";
473 ProximityKinds =
474 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000475 }
476
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000477 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000478
Tobias Grosser98610ee2012-02-13 23:31:39 +0000479 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000480 return false;
481
Tobias Grosser8a507022012-03-16 11:51:41 +0000482 isl_union_map *Validity = D->getDependences(ValidityKinds);
483 isl_union_map *Proximity = D->getDependences(ProximityKinds);
484
Tobias Grossera26db472012-01-30 19:38:43 +0000485 // Simplify the dependences by removing the constraints introduced by the
486 // domains. This can speed up the scheduling time significantly, as large
487 // constant coefficients will be removed from the dependences. The
488 // introduction of some additional dependences reduces the possible
489 // transformations, but in most cases, such transformation do not seem to be
490 // interesting anyway. In some cases this option may stop the scheduler to
491 // find any schedule.
492 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000493 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
494 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000495 Proximity =
496 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000497 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000498 } else if (SimplifyDeps != "no") {
499 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
500 "or 'no'. Falling back to default: 'yes'\n";
501 }
502
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000503 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000504 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
505 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000506 dbgs() << ";\n");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000507 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000508 dbgs() << ";\n");
509
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000510 int IslFusionStrategy;
511
512 if (FusionStrategy == "max") {
513 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
514 } else if (FusionStrategy == "min") {
515 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
516 } else {
517 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
518 "fusion.\n";
519 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
520 }
521
Tobias Grosser95e860c2012-01-30 19:38:54 +0000522 int IslMaximizeBands;
523
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000524 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000525 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000526 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000527 IslMaximizeBands = 0;
528 } else {
529 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
530 " or 'no'. Falling back to default: 'yes'\n";
531 IslMaximizeBands = 1;
532 }
533
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000534 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000535 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000536 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000537 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000538
539 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000540
541 isl_schedule_constraints *ScheduleConstraints;
542 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
543 ScheduleConstraints =
544 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
545 ScheduleConstraints = isl_schedule_constraints_set_validity(
546 ScheduleConstraints, isl_union_map_copy(Validity));
547 ScheduleConstraints =
548 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000549 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000550 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000551 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
552
553 // In cases the scheduler is not able to optimize the code, we just do not
554 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000555 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000556 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000557
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000558 DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule); dbgs() << ";\n");
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000559
Tobias Grosser98610ee2012-02-13 23:31:39 +0000560 isl_union_map *ScheduleMap = getScheduleMap(Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000561
Tobias Grosser083d3d32014-06-28 08:59:45 +0000562 for (ScopStmt *Stmt : S) {
Tobias Grosser249c4b12013-04-11 05:55:13 +0000563 isl_map *StmtSchedule;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000564 isl_set *Domain = Stmt->getDomain();
565 isl_union_map *StmtBand;
566 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000567 isl_union_set_from_set(Domain));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000568 if (isl_union_map_is_empty(StmtBand)) {
Tobias Grosser34f06132014-02-21 20:51:36 +0000569 StmtSchedule = isl_map_from_domain(isl_set_empty(Stmt->getDomainSpace()));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000570 isl_union_map_free(StmtBand);
571 } else {
572 assert(isl_union_map_n_map(StmtBand) == 1);
573 StmtSchedule = isl_map_from_union_map(StmtBand);
Tobias Grosserf242b802013-04-10 22:48:08 +0000574 }
575
Tobias Grosser98610ee2012-02-13 23:31:39 +0000576 Stmt->setScattering(StmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000577 }
578
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000579 isl_union_map_free(ScheduleMap);
Tobias Grosser28781422012-10-16 07:29:19 +0000580 LastSchedule = Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000581
Tobias Grosser98610ee2012-02-13 23:31:39 +0000582 unsigned MaxScatDims = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000583
Tobias Grosser083d3d32014-06-28 08:59:45 +0000584 for (ScopStmt *Stmt : S)
585 MaxScatDims = std::max(Stmt->getNumScattering(), MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000586
Tobias Grosser98610ee2012-02-13 23:31:39 +0000587 extendScattering(S, MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000588 return false;
589}
590
Tobias Grosser73600b82011-10-08 00:30:40 +0000591void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000592 isl_printer *p;
593 char *ScheduleStr;
594
595 OS << "Calculated schedule:\n";
596
597 if (!LastSchedule) {
598 OS << "n/a\n";
599 return;
600 }
601
602 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
603 p = isl_printer_print_schedule(p, LastSchedule);
604 ScheduleStr = isl_printer_get_str(p);
605 isl_printer_free(p);
606
607 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000608}
609
Tobias Grosser73600b82011-10-08 00:30:40 +0000610void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000611 ScopPass::getAnalysisUsage(AU);
612 AU.addRequired<Dependences>();
613}
614
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000615Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000616 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000617}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000618
619INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
620 "Polly - Optimize schedule of SCoP", false, false);
621INITIALIZE_PASS_DEPENDENCY(Dependences);
622INITIALIZE_PASS_DEPENDENCY(ScopInfo);
623INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
624 "Polly - Optimize schedule of SCoP", false, false)