blob: 9a54c1da67a0e5ca5b4423d1fd216c371b9cfd91 [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//
Tobias Grosser234a4822015-08-15 09:34:33 +000010// This pass generates an entirey new schedule tree from the data dependences
11// and iteration domains. The new schedule tree is computed in two steps:
Tobias Grosser30aa24c2011-05-14 19:02:06 +000012//
Tobias Grosser234a4822015-08-15 09:34:33 +000013// 1) The isl scheduling optimizer is run
14//
15// The isl scheduling optimizer creates a new schedule tree that maximizes
16// parallelism and tileability and minimizes data-dependence distances. The
17// algorithm used is a modified version of the ``Pluto'' algorithm:
18//
19// U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan.
20// A Practical Automatic Polyhedral Parallelizer and Locality Optimizer.
21// In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language
22// Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008.
23//
24// 2) A set of post-scheduling transformations is applied on the schedule tree.
25//
26// These optimizations include:
27//
28// - Tiling of the innermost tilable bands
29// - Prevectorization - The coice of a possible outer loop that is strip-mined
30// to the innermost level to enable inner-loop
31// vectorization.
32// - Some optimizations for spatial locality are also planned.
33//
34// For a detailed description of the schedule tree itself please see section 6
35// of:
36//
37// Polyhedral AST generation is more than scanning polyhedra
38// Tobias Grosser, Sven Verdoolaege, Albert Cohen
39// ACM Transations on Programming Languages and Systems (TOPLAS),
40// 37(4), July 2015
41// http://www.grosser.es/#pub-polyhedral-AST-generation
42//
43// This publication also contains a detailed discussion of the different options
44// for polyhedral loop unrolling, full/partial tile separation and other uses
45// of the schedule tree.
46//
Tobias Grosser30aa24c2011-05-14 19:02:06 +000047//===----------------------------------------------------------------------===//
48
Tobias Grosser967239c2011-10-23 20:59:44 +000049#include "polly/ScheduleOptimizer.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000050#include "polly/CodeGen/CodeGeneration.h"
51#include "polly/DependenceInfo.h"
52#include "polly/LinkAllPasses.h"
53#include "polly/Options.h"
54#include "polly/ScopInfo.h"
55#include "polly/Support/GICHelper.h"
56#include "llvm/Support/Debug.h"
Tobias Grosser2493e922011-12-07 07:42:57 +000057#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000058#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000059#include "isl/constraint.h"
60#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000061#include "isl/options.h"
Tobias Grosser97d87452015-05-30 06:46:59 +000062#include "isl/printer.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000063#include "isl/schedule.h"
Tobias Grosserbbb4cec2015-03-22 12:06:39 +000064#include "isl/schedule_node.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000065#include "isl/space.h"
Tobias Grossercd524dc2015-05-09 09:36:38 +000066#include "isl/union_map.h"
67#include "isl/union_set.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000068
69using namespace llvm;
70using namespace polly;
71
Chandler Carruth95fef942014-04-22 03:30:19 +000072#define DEBUG_TYPE "polly-opt-isl"
73
Tobias Grosser161c9082015-08-19 08:22:06 +000074static cl::opt<bool> EnableTiling("polly-tiling",
75 cl::desc("Enable loop tiling"),
76 cl::init(true), cl::ZeroOrMore,
77 cl::cat(PollyCategory));
Tobias Grosser353a2682011-10-23 20:59:26 +000078
Tobias Grossera26db472012-01-30 19:38:43 +000079static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000080 OptimizeDeps("polly-opt-optimize-only",
81 cl::desc("Only a certain kind of dependences (all/raw)"),
82 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
83 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000084
85static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000086 SimplifyDeps("polly-opt-simplify-deps",
87 cl::desc("Dependences should be simplified (yes/no)"),
88 cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
89 cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000090
Tobias Grosser483a90d2014-07-09 10:50:10 +000091static cl::opt<int> MaxConstantTerm(
92 "polly-opt-max-constant-term",
93 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
94 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000095
Tobias Grosser483a90d2014-07-09 10:50:10 +000096static cl::opt<int> MaxCoefficient(
97 "polly-opt-max-coefficient",
98 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
99 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
100
101static cl::opt<std::string> FusionStrategy(
102 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
103 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +0000104
Tobias Grossere602a072013-05-07 07:30:56 +0000105static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000106 MaximizeBandDepth("polly-opt-maximize-bands",
107 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
108 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000109
Tobias Grosser483a90d2014-07-09 10:50:10 +0000110static cl::opt<int> DefaultTileSize(
111 "polly-default-tile-size",
112 cl::desc("The default tile size (if not enough were provided by"
113 " --polly-tile-sizes)"),
114 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000115
116static cl::list<int> TileSizes("polly-tile-sizes",
117 cl::desc("A tile size"
118 " for each loop dimension, filled with"
119 " --polly-default-tile-size"),
120 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
121 cl::cat(PollyCategory));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000122namespace {
123
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000124class IslScheduleOptimizer : public ScopPass {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000125public:
126 static char ID;
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000127 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
Tobias Grosser28781422012-10-16 07:29:19 +0000128
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000129 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000130
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000131 bool runOnScop(Scop &S) override;
132 void printScop(raw_ostream &OS, Scop &S) const override;
133 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosser460e9a42012-04-25 13:22:43 +0000134
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000135private:
136 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +0000137
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000138 /// @brief Decide if the @p NewSchedule is profitable for @p S.
139 ///
140 /// @param S The SCoP we optimize.
141 /// @param NewSchedule The new schedule we computed.
142 ///
143 /// @return True, if we believe @p NewSchedule is an improvement for @p S.
144 bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule);
145
Tobias Grosserb241d922015-07-28 18:03:36 +0000146 /// @brief Pre-vectorizes one scheduling dimension of a schedule band.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000147 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000148 /// prevectSchedBand splits out the dimension DimToVectorize, tiles it and
149 /// sinks the resulting point loop.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000150 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000151 /// Example (DimToVectorize=0, VectorWidth=4):
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000152 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000153 /// | Before transformation:
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000154 /// |
155 /// | A[i,j] -> [i,j]
156 /// |
157 /// | for (i = 0; i < 128; i++)
158 /// | for (j = 0; j < 128; j++)
159 /// | A(i,j);
160 ///
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000161 /// | After transformation:
162 /// |
Tobias Grosserb241d922015-07-28 18:03:36 +0000163 /// | for (it = 0; it < 32; it+=1)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000164 /// | for (j = 0; j < 128; j++)
Tobias Grosserb241d922015-07-28 18:03:36 +0000165 /// | for (ip = 0; ip <= 3; ip++)
166 /// | A(4 * it + ip,j);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000167 ///
168 /// The goal of this transformation is to create a trivially vectorizable
169 /// loop. This means a parallel loop at the innermost level that has a
170 /// constant number of iterations corresponding to the target vector width.
171 ///
172 /// This transformation creates a loop at the innermost level. The loop has
173 /// a constant number of iterations, if the number of loop iterations at
174 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
175 /// currently constant and not yet target specific. This function does not
176 /// reason about parallelism.
Tobias Grosserb241d922015-07-28 18:03:36 +0000177 static __isl_give isl_schedule_node *
178 prevectSchedBand(__isl_take isl_schedule_node *Node, unsigned DimToVectorize,
179 int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000180
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000181 /// @brief Apply additional optimizations on the bands in the schedule tree.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000182 ///
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000183 /// We are looking for an innermost band node and apply the following
184 /// transformations:
185 ///
186 /// - Tile the band
187 /// - if the band is tileable
188 /// - if the band has more than one loop dimension
189 ///
Tobias Grosser3b10c942015-07-25 12:28:56 +0000190 /// - Prevectorize the schedule of the band (or the point loop in case of
Tobias Grosserb241d922015-07-28 18:03:36 +0000191 /// tiling).
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000192 /// - if vectorization is enabled
193 ///
194 /// @param Node The schedule node to (possibly) optimize.
195 /// @param User A pointer to forward some use information (currently unused).
196 static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000197
Tobias Grosser808cd692015-07-14 09:33:13 +0000198 /// @brief Apply post-scheduling transformations.
199 ///
200 /// This function applies a set of additional local transformations on the
201 /// schedule tree as it computed by the isl scheduler. Local transformations
202 /// applied include:
203 ///
204 /// - Tiling
205 /// - Prevectorization
206 ///
207 /// @param Schedule The schedule object post-transformations will be applied
208 /// on.
209 /// @returns The transformed schedule.
210 static __isl_give isl_schedule *
211 addPostTransforms(__isl_take isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000212
Tobias Grosser20532b82014-04-11 17:56:49 +0000213 using llvm::Pass::doFinalization;
214
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000215 virtual bool doFinalization() override {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000216 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000217 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000218 return true;
219 }
220};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000221}
222
Tobias Grosser73600b82011-10-08 00:30:40 +0000223char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000224
Tobias Grosserb241d922015-07-28 18:03:36 +0000225__isl_give isl_schedule_node *
226IslScheduleOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
227 unsigned DimToVectorize,
228 int VectorWidth) {
229 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000230
Tobias Grosserb241d922015-07-28 18:03:36 +0000231 auto Space = isl_schedule_node_band_get_space(Node);
232 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
233 isl_space_free(Space);
234 assert(DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000235
Tobias Grosserb241d922015-07-28 18:03:36 +0000236 if (DimToVectorize > 0) {
237 Node = isl_schedule_node_band_split(Node, DimToVectorize);
238 Node = isl_schedule_node_child(Node, 0);
239 }
240 if (DimToVectorize < ScheduleDimensions - 1)
241 Node = isl_schedule_node_band_split(Node, 1);
242 Space = isl_schedule_node_band_get_space(Node);
243 auto Sizes = isl_multi_val_zero(Space);
244 auto Ctx = isl_schedule_node_get_ctx(Node);
245 Sizes =
246 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
247 Node = isl_schedule_node_band_tile(Node, Sizes);
248 Node = isl_schedule_node_child(Node, 0);
249 Node = isl_schedule_node_band_sink(Node);
250 Node = isl_schedule_node_child(Node, 0);
251 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000252}
253
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000254isl_schedule_node *IslScheduleOptimizer::optimizeBand(isl_schedule_node *Node,
255 void *User) {
256 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
257 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000258
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000259 if (isl_schedule_node_n_children(Node) != 1)
260 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000261
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000262 if (!isl_schedule_node_band_get_permutable(Node))
263 return Node;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000264
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000265 auto Space = isl_schedule_node_band_get_space(Node);
266 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000267
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000268 if (Dims <= 1) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000269 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000270 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000271 }
272
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000273 auto Child = isl_schedule_node_get_child(Node, 0);
274 auto Type = isl_schedule_node_get_type(Child);
275 isl_schedule_node_free(Child);
276
277 if (Type != isl_schedule_node_leaf) {
278 isl_space_free(Space);
279 return Node;
280 }
281
Tobias Grosser161c9082015-08-19 08:22:06 +0000282 if (EnableTiling) {
Tobias Grosserf10f4632015-08-19 08:03:37 +0000283 auto Ctx = isl_schedule_node_get_ctx(Node);
284 auto Sizes = isl_multi_val_zero(isl_space_copy(Space));
285 for (unsigned i = 0; i < Dims; i++) {
286 auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
287 Sizes =
288 isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
289 }
290 Node = isl_schedule_node_band_tile(Node, Sizes);
291 Node = isl_schedule_node_child(Node, 0);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000292 }
293
Tobias Grosserf10f4632015-08-19 08:03:37 +0000294 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000295
296 if (PollyVectorizerChoice == VECTORIZER_NONE)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000297 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000298
Tobias Grosserb241d922015-07-28 18:03:36 +0000299 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000300 if (isl_schedule_node_band_member_get_coincident(Node, i)) {
301 Node = IslScheduleOptimizer::prevectSchedBand(Node, i);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000302 break;
303 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000304
Tobias Grosserf10f4632015-08-19 08:03:37 +0000305 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000306}
307
Tobias Grosser808cd692015-07-14 09:33:13 +0000308__isl_give isl_schedule *
309IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000310 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000311 isl_schedule_free(Schedule);
Tobias Grosserb2f39922015-05-28 13:32:11 +0000312 Root = isl_schedule_node_map_descendant_bottom_up(
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000313 Root, IslScheduleOptimizer::optimizeBand, NULL);
Tobias Grosser808cd692015-07-14 09:33:13 +0000314 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000315 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000316 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000317}
318
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000319bool IslScheduleOptimizer::isProfitableSchedule(
320 Scop &S, __isl_keep isl_union_map *NewSchedule) {
321 // To understand if the schedule has been optimized we check if the schedule
322 // has changed at all.
323 // TODO: We can improve this by tracking if any necessarily beneficial
324 // transformations have been performed. This can e.g. be tiling, loop
325 // interchange, or ...) We can track this either at the place where the
326 // transformation has been performed or, in case of automatic ILP based
327 // optimizations, by comparing (yet to be defined) performance metrics
328 // before/after the scheduling optimizer
329 // (e.g., #stride-one accesses)
330 isl_union_map *OldSchedule = S.getSchedule();
331 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
332 isl_union_map_free(OldSchedule);
333 return changed;
334}
335
Tobias Grosser73600b82011-10-08 00:30:40 +0000336bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +0000337
338 // Skip empty SCoPs but still allow code generation as it will delete the
339 // loops present but not needed.
340 if (S.getSize() == 0) {
341 S.markAsOptimized();
342 return false;
343 }
344
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000345 const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000346
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000347 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000348 return false;
349
Tobias Grosser28781422012-10-16 07:29:19 +0000350 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000351 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000352
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000353 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000354 int ValidityKinds =
355 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000356 int ProximityKinds;
357
358 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000359 ProximityKinds =
360 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000361 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000362 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000363 else {
364 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000365 << " Falling back to optimizing all dependences.\n";
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 }
369
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000370 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000371
Tobias Grosser98610ee2012-02-13 23:31:39 +0000372 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000373 return false;
374
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000375 isl_union_map *Validity = D.getDependences(ValidityKinds);
376 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +0000377
Tobias Grossera26db472012-01-30 19:38:43 +0000378 // Simplify the dependences by removing the constraints introduced by the
379 // domains. This can speed up the scheduling time significantly, as large
380 // constant coefficients will be removed from the dependences. The
381 // introduction of some additional dependences reduces the possible
382 // transformations, but in most cases, such transformation do not seem to be
383 // interesting anyway. In some cases this option may stop the scheduler to
384 // find any schedule.
385 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000386 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
387 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000388 Proximity =
389 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000390 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000391 } else if (SimplifyDeps != "no") {
392 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
393 "or 'no'. Falling back to default: 'yes'\n";
394 }
395
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000396 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +0000397 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
398 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
399 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000400
Michael Krusec59f22c2015-06-18 16:45:40 +0000401 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000402
403 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000404 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000405 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000406 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000407 } else {
408 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
409 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +0000410 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000411 }
412
Tobias Grosser95e860c2012-01-30 19:38:54 +0000413 int IslMaximizeBands;
414
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000415 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000416 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000417 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000418 IslMaximizeBands = 0;
419 } else {
420 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
421 " or 'no'. Falling back to default: 'yes'\n";
422 IslMaximizeBands = 1;
423 }
424
Michael Krusec59f22c2015-06-18 16:45:40 +0000425 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000426 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000427 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000428 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser4f6bcef2015-03-31 07:52:36 +0000429 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000430
431 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000432
433 isl_schedule_constraints *ScheduleConstraints;
434 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
435 ScheduleConstraints =
436 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
437 ScheduleConstraints = isl_schedule_constraints_set_validity(
438 ScheduleConstraints, isl_union_map_copy(Validity));
439 ScheduleConstraints =
440 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000441 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000442 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000443 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
444
445 // In cases the scheduler is not able to optimize the code, we just do not
446 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000447 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000448 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000449
Tobias Grosser97d87452015-05-30 06:46:59 +0000450 DEBUG({
451 auto *P = isl_printer_to_str(S.getIslCtx());
452 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
453 P = isl_printer_print_schedule(P, Schedule);
454 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
455 isl_printer_free(P);
456 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000457
Tobias Grosser808cd692015-07-14 09:33:13 +0000458 isl_schedule *NewSchedule = addPostTransforms(Schedule);
459 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000460
Tobias Grosser808cd692015-07-14 09:33:13 +0000461 if (!isProfitableSchedule(S, NewScheduleMap)) {
462 isl_union_map_free(NewScheduleMap);
463 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000464 return false;
465 }
466
Tobias Grosser808cd692015-07-14 09:33:13 +0000467 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000468 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000469
Tobias Grosser808cd692015-07-14 09:33:13 +0000470 isl_union_map_free(NewScheduleMap);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000471 return false;
472}
473
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000474void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000475 isl_printer *p;
476 char *ScheduleStr;
477
478 OS << "Calculated schedule:\n";
479
480 if (!LastSchedule) {
481 OS << "n/a\n";
482 return;
483 }
484
485 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
486 p = isl_printer_print_schedule(p, LastSchedule);
487 ScheduleStr = isl_printer_get_str(p);
488 isl_printer_free(p);
489
490 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000491}
492
Tobias Grosser73600b82011-10-08 00:30:40 +0000493void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000494 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000495 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000496}
497
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000498Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000499 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000500}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000501
502INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
503 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000504INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000505INITIALIZE_PASS_DEPENDENCY(ScopInfo);
506INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
507 "Polly - Optimize schedule of SCoP", false, false)