blob: bef2354066691a126e92c07b89865d972d5c0bf6 [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 Grosserba0d0922015-05-09 09:13:42 +000021#include "polly/CodeGen/CodeGeneration.h"
22#include "polly/DependenceInfo.h"
23#include "polly/LinkAllPasses.h"
24#include "polly/Options.h"
25#include "polly/ScopInfo.h"
26#include "polly/Support/GICHelper.h"
27#include "llvm/Support/Debug.h"
Tobias Grosser2493e922011-12-07 07:42:57 +000028#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000029#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000030#include "isl/constraint.h"
31#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000032#include "isl/options.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000033#include "isl/schedule.h"
Tobias Grosserbbb4cec2015-03-22 12:06:39 +000034#include "isl/schedule_node.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000035#include "isl/space.h"
Tobias Grossercd524dc2015-05-09 09:36:38 +000036#include "isl/union_map.h"
37#include "isl/union_set.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000038
39using namespace llvm;
40using namespace polly;
41
Chandler Carruth95fef942014-04-22 03:30:19 +000042#define DEBUG_TYPE "polly-opt-isl"
43
Tobias Grosser58032cb2013-06-23 01:29:29 +000044namespace polly {
45bool DisablePollyTiling;
46}
Tobias Grossere602a072013-05-07 07:30:56 +000047static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +000048 DisableTiling("polly-no-tiling",
49 cl::desc("Disable tiling in the scheduler"),
50 cl::location(polly::DisablePollyTiling), cl::init(false),
51 cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser353a2682011-10-23 20:59:26 +000052
Tobias Grossera26db472012-01-30 19:38:43 +000053static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000054 OptimizeDeps("polly-opt-optimize-only",
55 cl::desc("Only a certain kind of dependences (all/raw)"),
56 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
57 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000058
59static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000060 SimplifyDeps("polly-opt-simplify-deps",
61 cl::desc("Dependences should be simplified (yes/no)"),
62 cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
63 cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000064
Tobias Grosser483a90d2014-07-09 10:50:10 +000065static cl::opt<int> MaxConstantTerm(
66 "polly-opt-max-constant-term",
67 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
68 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000069
Tobias Grosser483a90d2014-07-09 10:50:10 +000070static cl::opt<int> MaxCoefficient(
71 "polly-opt-max-coefficient",
72 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
73 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
74
75static cl::opt<std::string> FusionStrategy(
76 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
77 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +000078
Tobias Grossere602a072013-05-07 07:30:56 +000079static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000080 MaximizeBandDepth("polly-opt-maximize-bands",
81 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
82 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000083
Tobias Grosser483a90d2014-07-09 10:50:10 +000084static cl::opt<int> DefaultTileSize(
85 "polly-default-tile-size",
86 cl::desc("The default tile size (if not enough were provided by"
87 " --polly-tile-sizes)"),
88 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertc3958b22014-05-28 17:21:02 +000089
90static cl::list<int> TileSizes("polly-tile-sizes",
91 cl::desc("A tile size"
92 " for each loop dimension, filled with"
93 " --polly-default-tile-size"),
94 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
95 cl::cat(PollyCategory));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000096namespace {
97
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000098class IslScheduleOptimizer : public ScopPass {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000099public:
100 static char ID;
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000101 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
Tobias Grosser28781422012-10-16 07:29:19 +0000102
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000103 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000104
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000105 bool runOnScop(Scop &S) override;
106 void printScop(raw_ostream &OS, Scop &S) const override;
107 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosser460e9a42012-04-25 13:22:43 +0000108
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000109private:
110 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +0000111
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000112 /// @brief Decide if the @p NewSchedule is profitable for @p S.
113 ///
114 /// @param S The SCoP we optimize.
115 /// @param NewSchedule The new schedule we computed.
116 ///
117 /// @return True, if we believe @p NewSchedule is an improvement for @p S.
118 bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule);
119
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000120 /// @brief Create a map that pre-vectorizes one scheduling dimension.
121 ///
122 /// getPrevectorMap creates a map that maps each input dimension to the same
123 /// output dimension, except for the dimension DimToVectorize.
124 /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
125 /// point loop of DimToVectorize is moved to the innermost level.
126 ///
127 /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
128 ///
129 /// | Before transformation
130 /// |
131 /// | A[i,j] -> [i,j]
132 /// |
133 /// | for (i = 0; i < 128; i++)
134 /// | for (j = 0; j < 128; j++)
135 /// | A(i,j);
136 ///
137 /// Prevector map:
138 /// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
139 ///
140 /// | After transformation:
141 /// |
142 /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
143 /// |
144 /// | for (it = 0; it < 128; it+=4)
145 /// | for (j = 0; j < 128; j++)
146 /// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
147 /// | A(ip,j);
148 ///
149 /// The goal of this transformation is to create a trivially vectorizable
150 /// loop. This means a parallel loop at the innermost level that has a
151 /// constant number of iterations corresponding to the target vector width.
152 ///
153 /// This transformation creates a loop at the innermost level. The loop has
154 /// a constant number of iterations, if the number of loop iterations at
155 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
156 /// currently constant and not yet target specific. This function does not
157 /// reason about parallelism.
Tobias Grosser442c6cc2015-03-19 07:43:35 +0000158 static __isl_give isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
159 int ScheduleDimensions,
160 int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000161
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000162 /// @brief Apply additional optimizations on the bands in the schedule tree.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000163 ///
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000164 /// We are looking for an innermost band node and apply the following
165 /// transformations:
166 ///
167 /// - Tile the band
168 /// - if the band is tileable
169 /// - if the band has more than one loop dimension
170 ///
171 /// - Prevectorize the point loop of the tile
172 /// - if vectorization is enabled
173 ///
174 /// @param Node The schedule node to (possibly) optimize.
175 /// @param User A pointer to forward some use information (currently unused).
176 static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000177
Tobias Grosser442c6cc2015-03-19 07:43:35 +0000178 static __isl_give isl_union_map *
179 getScheduleMap(__isl_keep isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000180
Tobias Grosser20532b82014-04-11 17:56:49 +0000181 using llvm::Pass::doFinalization;
182
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000183 virtual bool doFinalization() override {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000184 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000185 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000186 return true;
187 }
188};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000189}
190
Tobias Grosser73600b82011-10-08 00:30:40 +0000191char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000192
Tobias Grosser442c6cc2015-03-19 07:43:35 +0000193__isl_give isl_map *
194IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
195 int ScheduleDimensions, int VectorWidth) {
Tobias Grosser2493e922011-12-07 07:42:57 +0000196 isl_space *Space;
197 isl_local_space *LocalSpace, *LocalSpaceRange;
198 isl_set *Modulo;
199 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000200 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000201 isl_aff *Aff;
202 int PointDimension; /* ip */
203 int TileDimension; /* it */
Tobias Grosseredab1352013-06-21 06:41:31 +0000204 isl_val *VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000205
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000206 assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000207
Tobias Grosser2493e922011-12-07 07:42:57 +0000208 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
209 TilingMap = isl_map_universe(isl_space_copy(Space));
210 LocalSpace = isl_local_space_from_space(Space);
211 PointDimension = ScheduleDimensions;
212 TileDimension = DimToVectorize;
213
214 // Create an identity map for everything except DimToVectorize and map
215 // DimToVectorize to the point loop at the innermost dimension.
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000216 for (int i = 0; i < ScheduleDimensions; i++)
Tobias Grosser2493e922011-12-07 07:42:57 +0000217 if (i == DimToVectorize)
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000218 TilingMap =
219 isl_map_equate(TilingMap, isl_dim_in, i, isl_dim_out, PointDimension);
Tobias Grosser2493e922011-12-07 07:42:57 +0000220 else
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000221 TilingMap = isl_map_equate(TilingMap, isl_dim_in, i, isl_dim_out, i);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000222
Tobias Grosser2493e922011-12-07 07:42:57 +0000223 // it % 'VectorWidth' = 0
224 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
225 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
226 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
227 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
Tobias Grosseredab1352013-06-21 06:41:31 +0000228 VectorWidthMP = isl_val_int_from_si(ctx, VectorWidth);
229 Aff = isl_aff_mod_val(Aff, VectorWidthMP);
Tobias Grosser2493e922011-12-07 07:42:57 +0000230 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
231 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000232
Tobias Grosser2493e922011-12-07 07:42:57 +0000233 // it <= ip
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000234 TilingMap = isl_map_order_le(TilingMap, isl_dim_out, TileDimension,
235 isl_dim_out, PointDimension);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000236
Tobias Grosser2493e922011-12-07 07:42:57 +0000237 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000238 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000239 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
240 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
241 isl_constraint_set_constant_si(c, VectorWidth - 1);
242 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000243
Tobias Grosser2493e922011-12-07 07:42:57 +0000244 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000245}
246
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000247isl_schedule_node *IslScheduleOptimizer::optimizeBand(isl_schedule_node *Node,
248 void *User) {
249 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
250 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000251
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000252 if (isl_schedule_node_n_children(Node) != 1)
253 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000254
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000255 if (!isl_schedule_node_band_get_permutable(Node))
256 return Node;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000257
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000258 auto Space = isl_schedule_node_band_get_space(Node);
259 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000260
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000261 if (Dims <= 1) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000262 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000263 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000264 }
265
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000266 auto Child = isl_schedule_node_get_child(Node, 0);
267 auto Type = isl_schedule_node_get_type(Child);
268 isl_schedule_node_free(Child);
269
270 if (Type != isl_schedule_node_leaf) {
271 isl_space_free(Space);
272 return Node;
273 }
274
275 auto Sizes = isl_multi_val_zero(Space);
276 auto Ctx = isl_schedule_node_get_ctx(Node);
277
278 for (unsigned i = 0; i < Dims; i++) {
279 auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
280 Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
281 }
282
Tobias Grosser02cf69a2015-04-05 21:52:21 +0000283 isl_schedule_node *Res;
284
285 if (DisableTiling) {
286 isl_multi_val_free(Sizes);
287 Res = Node;
288 } else {
289 Res = isl_schedule_node_band_tile(Node, Sizes);
290 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000291
292 if (PollyVectorizerChoice == VECTORIZER_NONE)
293 return Res;
294
295 Child = isl_schedule_node_get_child(Res, 0);
296 auto ChildSchedule = isl_schedule_node_band_get_partial_schedule(Child);
297
298 for (int i = Dims - 1; i >= 0; i--) {
299 if (isl_schedule_node_band_member_get_coincident(Child, i)) {
300 auto TileMap = IslScheduleOptimizer::getPrevectorMap(Ctx, i, Dims);
301 auto TileUMap = isl_union_map_from_map(TileMap);
302 auto ChildSchedule2 = isl_union_map_apply_range(
303 isl_union_map_from_multi_union_pw_aff(ChildSchedule), TileUMap);
304 ChildSchedule = isl_multi_union_pw_aff_from_union_map(ChildSchedule2);
305 break;
306 }
307 }
308
309 isl_schedule_node_free(Res);
310 Res = isl_schedule_node_delete(Child);
311 Res = isl_schedule_node_insert_partial_schedule(Res, ChildSchedule);
312 return Res;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000313}
314
Tobias Grosser442c6cc2015-03-19 07:43:35 +0000315__isl_give isl_union_map *
316IslScheduleOptimizer::getScheduleMap(__isl_keep isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000317 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
318 Root = isl_schedule_node_map_descendant(
319 Root, IslScheduleOptimizer::optimizeBand, NULL);
320 auto ScheduleMap = isl_schedule_node_get_subtree_schedule_union_map(Root);
321 ScheduleMap = isl_union_map_detect_equalities(ScheduleMap);
322 isl_schedule_node_free(Root);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000323 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000324}
325
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000326bool IslScheduleOptimizer::isProfitableSchedule(
327 Scop &S, __isl_keep isl_union_map *NewSchedule) {
328 // To understand if the schedule has been optimized we check if the schedule
329 // has changed at all.
330 // TODO: We can improve this by tracking if any necessarily beneficial
331 // transformations have been performed. This can e.g. be tiling, loop
332 // interchange, or ...) We can track this either at the place where the
333 // transformation has been performed or, in case of automatic ILP based
334 // optimizations, by comparing (yet to be defined) performance metrics
335 // before/after the scheduling optimizer
336 // (e.g., #stride-one accesses)
337 isl_union_map *OldSchedule = S.getSchedule();
338 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
339 isl_union_map_free(OldSchedule);
340 return changed;
341}
342
Tobias Grosser73600b82011-10-08 00:30:40 +0000343bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +0000344
345 // Skip empty SCoPs but still allow code generation as it will delete the
346 // loops present but not needed.
347 if (S.getSize() == 0) {
348 S.markAsOptimized();
349 return false;
350 }
351
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000352 const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000353
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000354 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000355 return false;
356
Tobias Grosser28781422012-10-16 07:29:19 +0000357 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000358 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000359
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000360 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000361 int ValidityKinds =
362 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000363 int ProximityKinds;
364
365 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000366 ProximityKinds =
367 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000368 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000369 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000370 else {
371 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000372 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000373 ProximityKinds =
374 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000375 }
376
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000377 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000378
Tobias Grosser98610ee2012-02-13 23:31:39 +0000379 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000380 return false;
381
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000382 isl_union_map *Validity = D.getDependences(ValidityKinds);
383 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +0000384
Tobias Grossera26db472012-01-30 19:38:43 +0000385 // Simplify the dependences by removing the constraints introduced by the
386 // domains. This can speed up the scheduling time significantly, as large
387 // constant coefficients will be removed from the dependences. The
388 // introduction of some additional dependences reduces the possible
389 // transformations, but in most cases, such transformation do not seem to be
390 // interesting anyway. In some cases this option may stop the scheduler to
391 // find any schedule.
392 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000393 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
394 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000395 Proximity =
396 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000397 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000398 } else if (SimplifyDeps != "no") {
399 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
400 "or 'no'. Falling back to default: 'yes'\n";
401 }
402
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000403 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +0000404 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
405 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
406 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000407
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000408 int IslFusionStrategy;
409
410 if (FusionStrategy == "max") {
411 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
412 } else if (FusionStrategy == "min") {
413 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
414 } else {
415 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
416 "fusion.\n";
417 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
418 }
419
Tobias Grosser95e860c2012-01-30 19:38:54 +0000420 int IslMaximizeBands;
421
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000422 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000423 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000424 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000425 IslMaximizeBands = 0;
426 } else {
427 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
428 " or 'no'. Falling back to default: 'yes'\n";
429 IslMaximizeBands = 1;
430 }
431
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000432 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000433 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000434 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000435 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser4f6bcef2015-03-31 07:52:36 +0000436 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000437
438 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000439
440 isl_schedule_constraints *ScheduleConstraints;
441 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
442 ScheduleConstraints =
443 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
444 ScheduleConstraints = isl_schedule_constraints_set_validity(
445 ScheduleConstraints, isl_union_map_copy(Validity));
446 ScheduleConstraints =
447 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000448 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000449 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000450 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
451
452 // In cases the scheduler is not able to optimize the code, we just do not
453 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000454 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000455 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000456
Tobias Grosser01aea582014-10-22 23:16:28 +0000457 DEBUG(dbgs() << "Schedule := " << stringFromIslObj(Schedule) << ";\n");
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000458
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000459 isl_union_map *NewSchedule = getScheduleMap(Schedule);
460
461 // Check if the optimizations performed were profitable, otherwise exit early.
462 if (!isProfitableSchedule(S, NewSchedule)) {
463 isl_schedule_free(Schedule);
464 isl_union_map_free(NewSchedule);
465 return false;
466 }
467
468 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000469
Tobias Grosser7c3bad52015-05-27 05:16:57 +0000470 for (ScopStmt &Stmt : S) {
Tobias Grosser249c4b12013-04-11 05:55:13 +0000471 isl_map *StmtSchedule;
Tobias Grosser7c3bad52015-05-27 05:16:57 +0000472 isl_set *Domain = Stmt.getDomain();
Tobias Grosser98610ee2012-02-13 23:31:39 +0000473 isl_union_map *StmtBand;
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000474 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(NewSchedule),
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000475 isl_union_set_from_set(Domain));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000476 if (isl_union_map_is_empty(StmtBand)) {
Tobias Grosser7c3bad52015-05-27 05:16:57 +0000477 StmtSchedule = isl_map_from_domain(isl_set_empty(Stmt.getDomainSpace()));
Tobias Grosser249c4b12013-04-11 05:55:13 +0000478 isl_union_map_free(StmtBand);
479 } else {
480 assert(isl_union_map_n_map(StmtBand) == 1);
481 StmtSchedule = isl_map_from_union_map(StmtBand);
Tobias Grosserf242b802013-04-10 22:48:08 +0000482 }
483
Tobias Grosser7c3bad52015-05-27 05:16:57 +0000484 Stmt.setSchedule(StmtSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000485 }
486
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000487 isl_schedule_free(Schedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000488 isl_union_map_free(NewSchedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000489 return false;
490}
491
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000492void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000493 isl_printer *p;
494 char *ScheduleStr;
495
496 OS << "Calculated schedule:\n";
497
498 if (!LastSchedule) {
499 OS << "n/a\n";
500 return;
501 }
502
503 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
504 p = isl_printer_print_schedule(p, LastSchedule);
505 ScheduleStr = isl_printer_get_str(p);
506 isl_printer_free(p);
507
508 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000509}
510
Tobias Grosser73600b82011-10-08 00:30:40 +0000511void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000512 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000513 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000514}
515
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000516Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000517 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000518}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000519
520INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
521 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000522INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000523INITIALIZE_PASS_DEPENDENCY(ScopInfo);
524INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
525 "Polly - Optimize schedule of SCoP", false, false)