blob: 26b9668d9729d551f3c62aa68a49ae8af869ce1c [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 Grosser97d87452015-05-30 06:46:59 +000033#include "isl/printer.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000034#include "isl/schedule.h"
Tobias Grosserbbb4cec2015-03-22 12:06:39 +000035#include "isl/schedule_node.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000036#include "isl/space.h"
Tobias Grossercd524dc2015-05-09 09:36:38 +000037#include "isl/union_map.h"
38#include "isl/union_set.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000039
40using namespace llvm;
41using namespace polly;
42
Chandler Carruth95fef942014-04-22 03:30:19 +000043#define DEBUG_TYPE "polly-opt-isl"
44
Tobias Grosser58032cb2013-06-23 01:29:29 +000045namespace polly {
46bool DisablePollyTiling;
47}
Tobias Grossere602a072013-05-07 07:30:56 +000048static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +000049 DisableTiling("polly-no-tiling",
50 cl::desc("Disable tiling in the scheduler"),
51 cl::location(polly::DisablePollyTiling), cl::init(false),
52 cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser353a2682011-10-23 20:59:26 +000053
Tobias Grossera26db472012-01-30 19:38:43 +000054static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000055 OptimizeDeps("polly-opt-optimize-only",
56 cl::desc("Only a certain kind of dependences (all/raw)"),
57 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
58 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000059
60static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000061 SimplifyDeps("polly-opt-simplify-deps",
62 cl::desc("Dependences should be simplified (yes/no)"),
63 cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
64 cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000065
Tobias Grosser483a90d2014-07-09 10:50:10 +000066static cl::opt<int> MaxConstantTerm(
67 "polly-opt-max-constant-term",
68 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
69 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000070
Tobias Grosser483a90d2014-07-09 10:50:10 +000071static cl::opt<int> MaxCoefficient(
72 "polly-opt-max-coefficient",
73 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
74 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
75
76static cl::opt<std::string> FusionStrategy(
77 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
78 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +000079
Tobias Grossere602a072013-05-07 07:30:56 +000080static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000081 MaximizeBandDepth("polly-opt-maximize-bands",
82 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
83 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000084
Tobias Grosser483a90d2014-07-09 10:50:10 +000085static cl::opt<int> DefaultTileSize(
86 "polly-default-tile-size",
87 cl::desc("The default tile size (if not enough were provided by"
88 " --polly-tile-sizes)"),
89 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertc3958b22014-05-28 17:21:02 +000090
91static cl::list<int> TileSizes("polly-tile-sizes",
92 cl::desc("A tile size"
93 " for each loop dimension, filled with"
94 " --polly-default-tile-size"),
95 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
96 cl::cat(PollyCategory));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000097namespace {
98
Tobias Grosser4d96c8d2013-03-23 01:05:07 +000099class IslScheduleOptimizer : public ScopPass {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000100public:
101 static char ID;
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000102 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
Tobias Grosser28781422012-10-16 07:29:19 +0000103
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000104 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000105
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000106 bool runOnScop(Scop &S) override;
107 void printScop(raw_ostream &OS, Scop &S) const override;
108 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosser460e9a42012-04-25 13:22:43 +0000109
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000110private:
111 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +0000112
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000113 /// @brief Decide if the @p NewSchedule is profitable for @p S.
114 ///
115 /// @param S The SCoP we optimize.
116 /// @param NewSchedule The new schedule we computed.
117 ///
118 /// @return True, if we believe @p NewSchedule is an improvement for @p S.
119 bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule);
120
Tobias Grosserb241d922015-07-28 18:03:36 +0000121 /// @brief Pre-vectorizes one scheduling dimension of a schedule band.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000122 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000123 /// prevectSchedBand splits out the dimension DimToVectorize, tiles it and
124 /// sinks the resulting point loop.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000125 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000126 /// Example (DimToVectorize=0, VectorWidth=4):
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000127 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000128 /// | Before transformation:
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000129 /// |
130 /// | A[i,j] -> [i,j]
131 /// |
132 /// | for (i = 0; i < 128; i++)
133 /// | for (j = 0; j < 128; j++)
134 /// | A(i,j);
135 ///
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000136 /// | After transformation:
137 /// |
Tobias Grosserb241d922015-07-28 18:03:36 +0000138 /// | for (it = 0; it < 32; it+=1)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000139 /// | for (j = 0; j < 128; j++)
Tobias Grosserb241d922015-07-28 18:03:36 +0000140 /// | for (ip = 0; ip <= 3; ip++)
141 /// | A(4 * it + ip,j);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000142 ///
143 /// The goal of this transformation is to create a trivially vectorizable
144 /// loop. This means a parallel loop at the innermost level that has a
145 /// constant number of iterations corresponding to the target vector width.
146 ///
147 /// This transformation creates a loop at the innermost level. The loop has
148 /// a constant number of iterations, if the number of loop iterations at
149 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
150 /// currently constant and not yet target specific. This function does not
151 /// reason about parallelism.
Tobias Grosserb241d922015-07-28 18:03:36 +0000152 static __isl_give isl_schedule_node *
153 prevectSchedBand(__isl_take isl_schedule_node *Node, unsigned DimToVectorize,
154 int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000155
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000156 /// @brief Apply additional optimizations on the bands in the schedule tree.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000157 ///
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000158 /// We are looking for an innermost band node and apply the following
159 /// transformations:
160 ///
161 /// - Tile the band
162 /// - if the band is tileable
163 /// - if the band has more than one loop dimension
164 ///
Tobias Grosser3b10c942015-07-25 12:28:56 +0000165 /// - Prevectorize the schedule of the band (or the point loop in case of
Tobias Grosserb241d922015-07-28 18:03:36 +0000166 /// tiling).
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000167 /// - if vectorization is enabled
168 ///
169 /// @param Node The schedule node to (possibly) optimize.
170 /// @param User A pointer to forward some use information (currently unused).
171 static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000172
Tobias Grosser808cd692015-07-14 09:33:13 +0000173 /// @brief Apply post-scheduling transformations.
174 ///
175 /// This function applies a set of additional local transformations on the
176 /// schedule tree as it computed by the isl scheduler. Local transformations
177 /// applied include:
178 ///
179 /// - Tiling
180 /// - Prevectorization
181 ///
182 /// @param Schedule The schedule object post-transformations will be applied
183 /// on.
184 /// @returns The transformed schedule.
185 static __isl_give isl_schedule *
186 addPostTransforms(__isl_take isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000187
Tobias Grosser20532b82014-04-11 17:56:49 +0000188 using llvm::Pass::doFinalization;
189
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000190 virtual bool doFinalization() override {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000191 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000192 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000193 return true;
194 }
195};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000196}
197
Tobias Grosser73600b82011-10-08 00:30:40 +0000198char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000199
Tobias Grosserb241d922015-07-28 18:03:36 +0000200__isl_give isl_schedule_node *
201IslScheduleOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
202 unsigned DimToVectorize,
203 int VectorWidth) {
204 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000205
Tobias Grosserb241d922015-07-28 18:03:36 +0000206 auto Space = isl_schedule_node_band_get_space(Node);
207 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
208 isl_space_free(Space);
209 assert(DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000210
Tobias Grosserb241d922015-07-28 18:03:36 +0000211 if (DimToVectorize > 0) {
212 Node = isl_schedule_node_band_split(Node, DimToVectorize);
213 Node = isl_schedule_node_child(Node, 0);
214 }
215 if (DimToVectorize < ScheduleDimensions - 1)
216 Node = isl_schedule_node_band_split(Node, 1);
217 Space = isl_schedule_node_band_get_space(Node);
218 auto Sizes = isl_multi_val_zero(Space);
219 auto Ctx = isl_schedule_node_get_ctx(Node);
220 Sizes =
221 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
222 Node = isl_schedule_node_band_tile(Node, Sizes);
223 Node = isl_schedule_node_child(Node, 0);
224 Node = isl_schedule_node_band_sink(Node);
225 Node = isl_schedule_node_child(Node, 0);
226 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000227}
228
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000229isl_schedule_node *IslScheduleOptimizer::optimizeBand(isl_schedule_node *Node,
230 void *User) {
231 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
232 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000233
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000234 if (isl_schedule_node_n_children(Node) != 1)
235 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000236
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000237 if (!isl_schedule_node_band_get_permutable(Node))
238 return Node;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000239
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000240 auto Space = isl_schedule_node_band_get_space(Node);
241 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000242
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000243 if (Dims <= 1) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000244 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000245 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000246 }
247
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000248 auto Child = isl_schedule_node_get_child(Node, 0);
249 auto Type = isl_schedule_node_get_type(Child);
250 isl_schedule_node_free(Child);
251
252 if (Type != isl_schedule_node_leaf) {
253 isl_space_free(Space);
254 return Node;
255 }
256
257 auto Sizes = isl_multi_val_zero(Space);
258 auto Ctx = isl_schedule_node_get_ctx(Node);
259
260 for (unsigned i = 0; i < Dims; i++) {
261 auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
262 Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
263 }
264
Tobias Grosser02cf69a2015-04-05 21:52:21 +0000265 isl_schedule_node *Res;
266
267 if (DisableTiling) {
268 isl_multi_val_free(Sizes);
269 Res = Node;
270 } else {
271 Res = isl_schedule_node_band_tile(Node, Sizes);
Tobias Grosser27647942015-07-26 19:22:35 +0000272 Res = isl_schedule_node_child(Res, 0);
Tobias Grosser02cf69a2015-04-05 21:52:21 +0000273 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000274
275 if (PollyVectorizerChoice == VECTORIZER_NONE)
276 return Res;
277
Tobias Grosserb241d922015-07-28 18:03:36 +0000278 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosser3b10c942015-07-25 12:28:56 +0000279 if (isl_schedule_node_band_member_get_coincident(Res, i)) {
Tobias Grosserb241d922015-07-28 18:03:36 +0000280 Res = IslScheduleOptimizer::prevectSchedBand(Res, i);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000281 break;
282 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000283
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000284 return Res;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000285}
286
Tobias Grosser808cd692015-07-14 09:33:13 +0000287__isl_give isl_schedule *
288IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000289 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000290 isl_schedule_free(Schedule);
Tobias Grosserb2f39922015-05-28 13:32:11 +0000291 Root = isl_schedule_node_map_descendant_bottom_up(
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000292 Root, IslScheduleOptimizer::optimizeBand, NULL);
Tobias Grosser808cd692015-07-14 09:33:13 +0000293 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000294 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000295 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000296}
297
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000298bool IslScheduleOptimizer::isProfitableSchedule(
299 Scop &S, __isl_keep isl_union_map *NewSchedule) {
300 // To understand if the schedule has been optimized we check if the schedule
301 // has changed at all.
302 // TODO: We can improve this by tracking if any necessarily beneficial
303 // transformations have been performed. This can e.g. be tiling, loop
304 // interchange, or ...) We can track this either at the place where the
305 // transformation has been performed or, in case of automatic ILP based
306 // optimizations, by comparing (yet to be defined) performance metrics
307 // before/after the scheduling optimizer
308 // (e.g., #stride-one accesses)
309 isl_union_map *OldSchedule = S.getSchedule();
310 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
311 isl_union_map_free(OldSchedule);
312 return changed;
313}
314
Tobias Grosser73600b82011-10-08 00:30:40 +0000315bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +0000316
317 // Skip empty SCoPs but still allow code generation as it will delete the
318 // loops present but not needed.
319 if (S.getSize() == 0) {
320 S.markAsOptimized();
321 return false;
322 }
323
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000324 const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000325
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000326 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000327 return false;
328
Tobias Grosser28781422012-10-16 07:29:19 +0000329 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000330 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000331
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000332 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000333 int ValidityKinds =
334 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000335 int ProximityKinds;
336
337 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000338 ProximityKinds =
339 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000340 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000341 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000342 else {
343 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000344 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000345 ProximityKinds =
346 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000347 }
348
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000349 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000350
Tobias Grosser98610ee2012-02-13 23:31:39 +0000351 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000352 return false;
353
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000354 isl_union_map *Validity = D.getDependences(ValidityKinds);
355 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +0000356
Tobias Grossera26db472012-01-30 19:38:43 +0000357 // Simplify the dependences by removing the constraints introduced by the
358 // domains. This can speed up the scheduling time significantly, as large
359 // constant coefficients will be removed from the dependences. The
360 // introduction of some additional dependences reduces the possible
361 // transformations, but in most cases, such transformation do not seem to be
362 // interesting anyway. In some cases this option may stop the scheduler to
363 // find any schedule.
364 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000365 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
366 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000367 Proximity =
368 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000369 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000370 } else if (SimplifyDeps != "no") {
371 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
372 "or 'no'. Falling back to default: 'yes'\n";
373 }
374
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000375 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +0000376 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
377 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
378 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000379
Michael Krusec59f22c2015-06-18 16:45:40 +0000380 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000381
382 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000383 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000384 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000385 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000386 } else {
387 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
388 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +0000389 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000390 }
391
Tobias Grosser95e860c2012-01-30 19:38:54 +0000392 int IslMaximizeBands;
393
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000394 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000395 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000396 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000397 IslMaximizeBands = 0;
398 } else {
399 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
400 " or 'no'. Falling back to default: 'yes'\n";
401 IslMaximizeBands = 1;
402 }
403
Michael Krusec59f22c2015-06-18 16:45:40 +0000404 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000405 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000406 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000407 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser4f6bcef2015-03-31 07:52:36 +0000408 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000409
410 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000411
412 isl_schedule_constraints *ScheduleConstraints;
413 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
414 ScheduleConstraints =
415 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
416 ScheduleConstraints = isl_schedule_constraints_set_validity(
417 ScheduleConstraints, isl_union_map_copy(Validity));
418 ScheduleConstraints =
419 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000420 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000421 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000422 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
423
424 // In cases the scheduler is not able to optimize the code, we just do not
425 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000426 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000427 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000428
Tobias Grosser97d87452015-05-30 06:46:59 +0000429 DEBUG({
430 auto *P = isl_printer_to_str(S.getIslCtx());
431 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
432 P = isl_printer_print_schedule(P, Schedule);
433 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
434 isl_printer_free(P);
435 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000436
Tobias Grosser808cd692015-07-14 09:33:13 +0000437 isl_schedule *NewSchedule = addPostTransforms(Schedule);
438 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000439
Tobias Grosser808cd692015-07-14 09:33:13 +0000440 if (!isProfitableSchedule(S, NewScheduleMap)) {
441 isl_union_map_free(NewScheduleMap);
442 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000443 return false;
444 }
445
Tobias Grosser808cd692015-07-14 09:33:13 +0000446 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000447 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000448
Tobias Grosser808cd692015-07-14 09:33:13 +0000449 isl_union_map_free(NewScheduleMap);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000450 return false;
451}
452
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000453void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000454 isl_printer *p;
455 char *ScheduleStr;
456
457 OS << "Calculated schedule:\n";
458
459 if (!LastSchedule) {
460 OS << "n/a\n";
461 return;
462 }
463
464 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
465 p = isl_printer_print_schedule(p, LastSchedule);
466 ScheduleStr = isl_printer_get_str(p);
467 isl_printer_free(p);
468
469 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000470}
471
Tobias Grosser73600b82011-10-08 00:30:40 +0000472void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000473 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000474 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000475}
476
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000477Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000478 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000479}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000480
481INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
482 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000483INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000484INITIALIZE_PASS_DEPENDENCY(ScopInfo);
485INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
486 "Polly - Optimize schedule of SCoP", false, false)