blob: 03b30b95d69555cc621aef5ba17e2a500eacf61c [file] [log] [blame]
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001//===- Schedule.cpp - Calculate an optimized schedule ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass the isl to calculate a schedule that is optimized for parallelism
11// and tileablility. The algorithm used in isl is an optimized version of the
12// algorithm described in following paper:
13//
14// U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan.
15// A Practical Automatic Polyhedral Parallelizer and Locality Optimizer.
16// In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language
17// Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008.
18//===----------------------------------------------------------------------===//
19
Tobias Grosser967239c2011-10-23 20:59:44 +000020#include "polly/ScheduleOptimizer.h"
21
Hongbin Zheng8a846612012-04-25 13:18:28 +000022#include "polly/CodeGen/CodeGeneration.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000023#include "polly/Dependences.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000024#include "polly/LinkAllPasses.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000025#include "polly/ScopInfo.h"
26
Tobias Grosser2493e922011-12-07 07:42:57 +000027#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000028#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000029#include "isl/constraint.h"
30#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000031#include "isl/options.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000032#include "isl/schedule.h"
33#include "isl/space.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000034
Tobias Grosser4dca4392011-11-22 19:40:19 +000035#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000036#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000037#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000038
39using namespace llvm;
40using namespace polly;
41
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000042namespace polly { bool DisablePollyTiling; }
Tobias Grossere602a072013-05-07 07:30:56 +000043static cl::opt<bool, true>
44DisableTiling("polly-no-tiling", cl::desc("Disable tiling in the scheduler"),
45 cl::Hidden, cl::location(polly::DisablePollyTiling),
46 cl::init(false));
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)"),
51 cl::Hidden, cl::init("all"));
52
53static cl::opt<std::string>
Tobias Grossera26db472012-01-30 19:38:43 +000054SimplifyDeps("polly-opt-simplify-deps",
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000055 cl::desc("Dependences should be simplified (yes/no)"), cl::Hidden,
56 cl::init("yes"));
Tobias Grossera26db472012-01-30 19:38:43 +000057
Tobias Grosser992e60c2012-02-20 08:41:15 +000058static cl::opt<int>
59MaxConstantTerm("polly-opt-max-constant-term",
60 cl::desc("The maximal constant term allowed (-1 is unlimited)"),
61 cl::Hidden, cl::init(20));
62
Tobias Grosser92f54802012-02-20 08:41:47 +000063static cl::opt<int>
64MaxCoefficient("polly-opt-max-coefficient",
65 cl::desc("The maximal coefficient allowed (-1 is unlimited)"),
66 cl::Hidden, cl::init(20));
67
Tobias Grossere602a072013-05-07 07:30:56 +000068static cl::opt<std::string>
69FusionStrategy("polly-opt-fusion",
70 cl::desc("The fusion strategy to choose (min/max)"), cl::Hidden,
71 cl::init("min"));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000072
Tobias Grossere602a072013-05-07 07:30:56 +000073static cl::opt<std::string>
74MaximizeBandDepth("polly-opt-maximize-bands",
75 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
76 cl::init("yes"));
Tobias Grosser95e860c2012-01-30 19:38:54 +000077
Tobias Grosser30aa24c2011-05-14 19:02:06 +000078namespace {
79
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000080class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000081
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000082public:
83 static char ID;
84 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = NULL; }
Tobias Grosser28781422012-10-16 07:29:19 +000085
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000086 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +000087
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000088 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
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000092private:
93 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +000094
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000095 static void extendScattering(Scop &S, unsigned NewDimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +000096
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000097 /// @brief Create a map that describes a n-dimensonal tiling.
98 ///
99 /// getTileMap creates a map from a n-dimensional scattering space into an
100 /// 2*n-dimensional scattering space. The map describes a rectangular
101 /// tiling.
102 ///
103 /// Example:
104 /// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
105 ///
106 /// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
107 /// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
108 /// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
109 ///
110 /// Before tiling:
111 ///
112 /// for (i = 0; i < N; i++)
113 /// for (j = 0; j < M; j++)
114 /// S(i,j)
115 ///
116 /// After tiling:
117 ///
118 /// for (t_i = 0; t_i < N; i+=32)
119 /// for (t_j = 0; t_j < M; j+=32)
120 /// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
121 /// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
122 /// S(i,j)
123 ///
124 static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
125 isl_space *SpaceModel, int tileSize = 32);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000126
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000127 /// @brief Get the schedule for this band.
128 ///
129 /// Polly applies transformations like tiling on top of the isl calculated
130 /// value. This can influence the number of scheduling dimension. The
131 /// number of schedule dimensions is returned in the parameter 'Dimension'.
132 static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000133
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000134 /// @brief Create a map that pre-vectorizes one scheduling dimension.
135 ///
136 /// getPrevectorMap creates a map that maps each input dimension to the same
137 /// output dimension, except for the dimension DimToVectorize.
138 /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
139 /// point loop of DimToVectorize is moved to the innermost level.
140 ///
141 /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
142 ///
143 /// | Before transformation
144 /// |
145 /// | A[i,j] -> [i,j]
146 /// |
147 /// | for (i = 0; i < 128; i++)
148 /// | for (j = 0; j < 128; j++)
149 /// | A(i,j);
150 ///
151 /// Prevector map:
152 /// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
153 ///
154 /// | After transformation:
155 /// |
156 /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
157 /// |
158 /// | for (it = 0; it < 128; it+=4)
159 /// | for (j = 0; j < 128; j++)
160 /// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
161 /// | A(ip,j);
162 ///
163 /// The goal of this transformation is to create a trivially vectorizable
164 /// loop. This means a parallel loop at the innermost level that has a
165 /// constant number of iterations corresponding to the target vector width.
166 ///
167 /// This transformation creates a loop at the innermost level. The loop has
168 /// a constant number of iterations, if the number of loop iterations at
169 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
170 /// currently constant and not yet target specific. This function does not
171 /// reason about parallelism.
172 static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
173 int ScheduleDimensions, int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000174
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000175 /// @brief Get the scheduling map for a list of bands.
176 ///
177 /// Walk recursively the forest of bands to combine the schedules of the
178 /// individual bands to the overall schedule. In case tiling is requested,
179 /// the individual bands are tiled.
180 static isl_union_map *getScheduleForBandList(isl_band_list *BandList);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000181
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000182 static isl_union_map *getScheduleMap(isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000183
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000184 bool doFinalization() {
185 isl_schedule_free(LastSchedule);
186 LastSchedule = NULL;
187 return true;
188 }
189};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000190
191}
192
Tobias Grosser73600b82011-10-08 00:30:40 +0000193char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000194
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;
Tobias Grosser29666112012-05-22 10:47:31 +0000200 isl_map *Map, *New;
Tobias Grossercf3942d2011-10-06 00:04:05 +0000201
202 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
Tobias Grosser29666112012-05-22 10:47:31 +0000203 Map = isl_map_universe(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000204
Tobias Grosser29666112012-05-22 10:47:31 +0000205 for (unsigned i = 0; i < OldDimensions; i++)
206 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000207
Tobias Grosser29666112012-05-22 10:47:31 +0000208 for (unsigned i = OldDimensions; i < NewDimensions; i++)
209 Map = isl_map_fix_si(Map, isl_dim_out, i, 0);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000210
Tobias Grosser29666112012-05-22 10:47:31 +0000211 Map = isl_map_align_params(Map, S.getParamSpace());
212 New = isl_map_apply_range(Stmt->getScattering(), Map);
213 Stmt->setScattering(New);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000214 }
215}
216
Tobias Grossere602a072013-05-07 07:30:56 +0000217isl_basic_map *IslScheduleOptimizer::getTileMap(isl_ctx *ctx,
218 int scheduleDimensions,
219 isl_space *SpaceModel,
220 int tileSize) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000221 // We construct
222 //
223 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
224 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
225 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
226 //
227 // and project out the auxilary dimensions a0 and a1.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000228 isl_space *Space =
229 isl_space_alloc(ctx, 0, scheduleDimensions, scheduleDimensions * 3);
Tobias Grosserf5338802011-10-06 00:03:35 +0000230 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
231
232 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000233
Tobias Grosserde68cc92011-06-30 20:01:02 +0000234 for (int x = 0; x < scheduleDimensions; x++) {
235 int sX = x;
236 int tX = x;
237 int pX = scheduleDimensions + x;
238 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000239
Tobias Grosserde68cc92011-06-30 20:01:02 +0000240 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000241
Tobias Grosserde68cc92011-06-30 20:01:02 +0000242 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000243 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000244 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
245 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
246 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000247
Tobias Grosserde68cc92011-06-30 20:01:02 +0000248 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000249 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000250 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
251 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
252 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000253
Tobias Grosserde68cc92011-06-30 20:01:02 +0000254 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000255 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000256 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
257 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
258 tileMap = isl_basic_map_add_constraint(tileMap, c);
259
260 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000261 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000262 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
263 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000264 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000265 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000266 }
267
Tobias Grosserde68cc92011-06-30 20:01:02 +0000268 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000269 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000270 // The auxilary dimensions are transformed into existentially quantified ones.
271 // This reduces the number of visible scattering dimensions and allows Cloog
272 // to produces better code.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000273 tileMap = isl_basic_map_project_out(
274 tileMap, isl_dim_out, 2 * scheduleDimensions, scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000275 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000276 return tileMap;
277}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000278
Tobias Grossere602a072013-05-07 07:30:56 +0000279isl_union_map *IslScheduleOptimizer::getScheduleForBand(isl_band *Band,
280 int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000281 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000282 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000283 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000284 isl_basic_map *TileMap;
285 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000286
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000287 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000288 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000289
Tobias Grosser79b30202011-11-17 12:56:00 +0000290 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000291 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000292
Tobias Grosserb6033392011-12-08 13:02:58 +0000293 // It does not make any sense to tile a band with just one dimension.
294 if (*Dimensions == 1)
295 return PartialSchedule;
296
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000297 ctx = isl_union_map_get_ctx(PartialSchedule);
298 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000299
Tobias Grosserb6033392011-12-08 13:02:58 +0000300 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000301 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
302 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000303 *Dimensions = 2 * *Dimensions;
304
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000305 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000306}
307
Tobias Grossere602a072013-05-07 07:30:56 +0000308isl_map *IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
309 int ScheduleDimensions,
310 int VectorWidth) {
Tobias Grosser2493e922011-12-07 07:42:57 +0000311 isl_space *Space;
312 isl_local_space *LocalSpace, *LocalSpaceRange;
313 isl_set *Modulo;
314 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000315 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000316 isl_aff *Aff;
317 int PointDimension; /* ip */
318 int TileDimension; /* it */
319 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000320
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000321 assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000322
Tobias Grosser2493e922011-12-07 07:42:57 +0000323 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
324 TilingMap = isl_map_universe(isl_space_copy(Space));
325 LocalSpace = isl_local_space_from_space(Space);
326 PointDimension = ScheduleDimensions;
327 TileDimension = DimToVectorize;
328
329 // Create an identity map for everything except DimToVectorize and map
330 // DimToVectorize to the point loop at the innermost dimension.
331 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000332 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000333 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000334
335 if (i == DimToVectorize)
336 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
337 else
338 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
339
340 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000341 }
342
Tobias Grosser2493e922011-12-07 07:42:57 +0000343 // it % 'VectorWidth' = 0
344 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
345 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
346 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
347 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
348 isl_int_init(VectorWidthMP);
349 isl_int_set_si(VectorWidthMP, VectorWidth);
350 Aff = isl_aff_mod(Aff, VectorWidthMP);
351 isl_int_clear(VectorWidthMP);
352 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
353 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000354
Tobias Grosser2493e922011-12-07 07:42:57 +0000355 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000356 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000357 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
358 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
359 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000360
Tobias Grosser2493e922011-12-07 07:42:57 +0000361 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000362 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000363 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
364 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
365 isl_constraint_set_constant_si(c, VectorWidth - 1);
366 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000367
Tobias Grosser2493e922011-12-07 07:42:57 +0000368 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000369}
370
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000371isl_union_map *
372IslScheduleOptimizer::getScheduleForBandList(isl_band_list *BandList) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000373 int NumBands;
374 isl_union_map *Schedule;
375 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000376
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000377 ctx = isl_band_list_get_ctx(BandList);
378 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000379 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000380
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000381 for (int i = 0; i < NumBands; i++) {
382 isl_band *Band;
383 isl_union_map *PartialSchedule;
384 int ScheduleDimensions;
385 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000386
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000387 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000388 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000389 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000390
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000391 if (isl_band_has_children(Band)) {
392 isl_band_list *Children;
393 isl_union_map *SuffixSchedule;
394
395 Children = isl_band_get_children(Band);
396 SuffixSchedule = getScheduleForBandList(Children);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000397 PartialSchedule =
398 isl_union_map_flat_range_product(PartialSchedule, SuffixSchedule);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000399 isl_band_list_free(Children);
Hongbin Zheng68794212012-05-06 10:22:19 +0000400 } else if (PollyVectorizerChoice != VECTORIZER_NONE) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000401 for (int j = 0; j < isl_band_n_member(Band); j++) {
402 if (isl_band_member_is_zero_distance(Band, j)) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000403 isl_map *TileMap;
404 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000405
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000406 TileMap = getPrevectorMap(ctx, ScheduleDimensions - j - 1,
Tobias Grosser216ea582012-04-16 11:06:06 +0000407 ScheduleDimensions);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000408 TileUMap = isl_union_map_from_map(TileMap);
409 TileUMap =
410 isl_union_map_align_params(TileUMap, isl_space_copy(Space));
411 PartialSchedule =
412 isl_union_map_apply_range(PartialSchedule, TileUMap);
413 break;
414 }
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000415 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000416 }
417
Tobias Grosser62872012011-11-17 12:56:04 +0000418 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000419
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000420 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000421 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000422 }
423
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000424 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000425}
426
Tobias Grosser460e9a42012-04-25 13:22:43 +0000427isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000428 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
429 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
430 isl_band_list_free(BandList);
431 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000432}
433
Tobias Grosser73600b82011-10-08 00:30:40 +0000434bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000435 Dependences *D = &getAnalysis<Dependences>();
436
Tobias Grosser28781422012-10-16 07:29:19 +0000437 isl_schedule_free(LastSchedule);
438 LastSchedule = NULL;
439
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000440 // Build input data.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000441 int ValidityKinds =
442 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000443 int ProximityKinds;
444
445 if (OptimizeDeps == "all")
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000446 ProximityKinds =
447 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000448 else if (OptimizeDeps == "raw")
Tobias Grosser1e03ad72012-02-15 09:58:42 +0000449 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000450 else {
451 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000452 << " Falling back to optimizing all dependences.\n";
453 ProximityKinds =
454 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000455 }
456
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000457 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000458
Tobias Grosser98610ee2012-02-13 23:31:39 +0000459 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000460 return false;
461
Tobias Grosser8a507022012-03-16 11:51:41 +0000462 isl_union_map *Validity = D->getDependences(ValidityKinds);
463 isl_union_map *Proximity = D->getDependences(ProximityKinds);
464
Tobias Grossera26db472012-01-30 19:38:43 +0000465 // Simplify the dependences by removing the constraints introduced by the
466 // domains. This can speed up the scheduling time significantly, as large
467 // constant coefficients will be removed from the dependences. The
468 // introduction of some additional dependences reduces the possible
469 // transformations, but in most cases, such transformation do not seem to be
470 // interesting anyway. In some cases this option may stop the scheduler to
471 // find any schedule.
472 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000473 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
474 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000475 Proximity =
476 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000477 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000478 } else if (SimplifyDeps != "no") {
479 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
480 "or 'no'. Falling back to default: 'yes'\n";
481 }
482
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000483 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000484 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
485 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000486 dbgs() << ";\n");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000487 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000488 dbgs() << ";\n");
489
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000490 int IslFusionStrategy;
491
492 if (FusionStrategy == "max") {
493 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
494 } else if (FusionStrategy == "min") {
495 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
496 } else {
497 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
498 "fusion.\n";
499 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
500 }
501
Tobias Grosser95e860c2012-01-30 19:38:54 +0000502 int IslMaximizeBands;
503
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000504 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000505 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000506 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000507 IslMaximizeBands = 0;
508 } else {
509 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
510 " or 'no'. Falling back to default: 'yes'\n";
511 IslMaximizeBands = 1;
512 }
513
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000514 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000515 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000516 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000517 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000518
519 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grosser00383a72012-02-14 14:02:44 +0000520 isl_schedule *Schedule;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000521 Schedule = isl_union_set_compute_schedule(Domain, Validity, Proximity);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000522 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
523
524 // In cases the scheduler is not able to optimize the code, we just do not
525 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000526 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000527 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000528
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000529 DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule); dbgs() << ";\n");
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000530
Tobias Grosser98610ee2012-02-13 23:31:39 +0000531 isl_union_map *ScheduleMap = getScheduleMap(Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000532
Tobias Grosserde68cc92011-06-30 20:01:02 +0000533 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grosser98610ee2012-02-13 23:31:39 +0000534 ScopStmt *Stmt = *SI;
Tobias Grosser249c4b12013-04-11 05:55:13 +0000535 isl_map *StmtSchedule;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000536 isl_set *Domain = Stmt->getDomain();
537 isl_union_map *StmtBand;
538 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000539 isl_union_set_from_set(Domain));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000540 if (isl_union_map_is_empty(StmtBand)) {
541 // Statements with an empty iteration domain may not have a schedule
542 // assigned by the isl schedule optimizer. As Polly expects each statement
543 // to have a schedule, we keep the old schedule for this statement. As
544 // there are zero iterations to execute, the content of the schedule does
545 // not matter.
546 //
547 // TODO: Consider removing such statements when constructing the scop.
Tobias Grosserf242b802013-04-10 22:48:08 +0000548 StmtSchedule = Stmt->getScattering();
549 StmtSchedule = isl_map_set_tuple_id(StmtSchedule, isl_dim_out, NULL);
Tobias Grosser249c4b12013-04-11 05:55:13 +0000550 isl_union_map_free(StmtBand);
551 } else {
552 assert(isl_union_map_n_map(StmtBand) == 1);
553 StmtSchedule = isl_map_from_union_map(StmtBand);
Tobias Grosserf242b802013-04-10 22:48:08 +0000554 }
555
Tobias Grosser98610ee2012-02-13 23:31:39 +0000556 Stmt->setScattering(StmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000557 }
558
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000559 isl_union_map_free(ScheduleMap);
Tobias Grosser28781422012-10-16 07:29:19 +0000560 LastSchedule = Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000561
Tobias Grosser98610ee2012-02-13 23:31:39 +0000562 unsigned MaxScatDims = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000563
564 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grosser98610ee2012-02-13 23:31:39 +0000565 MaxScatDims = std::max((*SI)->getNumScattering(), MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000566
Tobias Grosser98610ee2012-02-13 23:31:39 +0000567 extendScattering(S, MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000568 return false;
569}
570
Tobias Grosser73600b82011-10-08 00:30:40 +0000571void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000572 isl_printer *p;
573 char *ScheduleStr;
574
575 OS << "Calculated schedule:\n";
576
577 if (!LastSchedule) {
578 OS << "n/a\n";
579 return;
580 }
581
582 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
583 p = isl_printer_print_schedule(p, LastSchedule);
584 ScheduleStr = isl_printer_get_str(p);
585 isl_printer_free(p);
586
587 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000588}
589
Tobias Grosser73600b82011-10-08 00:30:40 +0000590void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000591 ScopPass::getAnalysisUsage(AU);
592 AU.addRequired<Dependences>();
593}
594
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000595Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000596 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000597}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000598
599INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
600 "Polly - Optimize schedule of SCoP", false, false);
601INITIALIZE_PASS_DEPENDENCY(Dependences);
602INITIALIZE_PASS_DEPENDENCY(ScopInfo);
603INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
604 "Polly - Optimize schedule of SCoP", false, false)