blob: b66fcb59d08210d595811900808d91b767422e0b [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 Grosser07c1c2f2015-08-19 08:46:11 +0000110static cl::opt<int> PrevectorWidth(
111 "polly-prevect-width",
112 cl::desc(
113 "The number of loop iterations to strip-mine for pre-vectorization"),
114 cl::Hidden, cl::init(4), cl::ZeroOrMore, cl::cat(PollyCategory));
115
Tobias Grosser483a90d2014-07-09 10:50:10 +0000116static cl::opt<int> DefaultTileSize(
117 "polly-default-tile-size",
118 cl::desc("The default tile size (if not enough were provided by"
119 " --polly-tile-sizes)"),
120 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000121
122static cl::list<int> TileSizes("polly-tile-sizes",
123 cl::desc("A tile size"
124 " for each loop dimension, filled with"
125 " --polly-default-tile-size"),
126 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
127 cl::cat(PollyCategory));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000128namespace {
129
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000130class IslScheduleOptimizer : public ScopPass {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000131public:
132 static char ID;
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000133 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
Tobias Grosser28781422012-10-16 07:29:19 +0000134
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000135 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000136
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000137 bool runOnScop(Scop &S) override;
138 void printScop(raw_ostream &OS, Scop &S) const override;
139 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosser460e9a42012-04-25 13:22:43 +0000140
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000141private:
142 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +0000143
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000144 /// @brief Decide if the @p NewSchedule is profitable for @p S.
145 ///
146 /// @param S The SCoP we optimize.
147 /// @param NewSchedule The new schedule we computed.
148 ///
149 /// @return True, if we believe @p NewSchedule is an improvement for @p S.
150 bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule);
151
Tobias Grosserb241d922015-07-28 18:03:36 +0000152 /// @brief Pre-vectorizes one scheduling dimension of a schedule band.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000153 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000154 /// prevectSchedBand splits out the dimension DimToVectorize, tiles it and
155 /// sinks the resulting point loop.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000156 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000157 /// Example (DimToVectorize=0, VectorWidth=4):
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000158 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000159 /// | Before transformation:
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000160 /// |
161 /// | A[i,j] -> [i,j]
162 /// |
163 /// | for (i = 0; i < 128; i++)
164 /// | for (j = 0; j < 128; j++)
165 /// | A(i,j);
166 ///
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000167 /// | After transformation:
168 /// |
Tobias Grosserb241d922015-07-28 18:03:36 +0000169 /// | for (it = 0; it < 32; it+=1)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000170 /// | for (j = 0; j < 128; j++)
Tobias Grosserb241d922015-07-28 18:03:36 +0000171 /// | for (ip = 0; ip <= 3; ip++)
172 /// | A(4 * it + ip,j);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000173 ///
174 /// The goal of this transformation is to create a trivially vectorizable
175 /// loop. This means a parallel loop at the innermost level that has a
176 /// constant number of iterations corresponding to the target vector width.
177 ///
178 /// This transformation creates a loop at the innermost level. The loop has
179 /// a constant number of iterations, if the number of loop iterations at
180 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
181 /// currently constant and not yet target specific. This function does not
182 /// reason about parallelism.
Tobias Grosserb241d922015-07-28 18:03:36 +0000183 static __isl_give isl_schedule_node *
184 prevectSchedBand(__isl_take isl_schedule_node *Node, unsigned DimToVectorize,
Tobias Grosser07c1c2f2015-08-19 08:46:11 +0000185 int VectorWidth);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000186
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000187 /// @brief Apply additional optimizations on the bands in the schedule tree.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000188 ///
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000189 /// We are looking for an innermost band node and apply the following
190 /// transformations:
191 ///
192 /// - Tile the band
193 /// - if the band is tileable
194 /// - if the band has more than one loop dimension
195 ///
Tobias Grosser3b10c942015-07-25 12:28:56 +0000196 /// - Prevectorize the schedule of the band (or the point loop in case of
Tobias Grosserb241d922015-07-28 18:03:36 +0000197 /// tiling).
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000198 /// - if vectorization is enabled
199 ///
200 /// @param Node The schedule node to (possibly) optimize.
201 /// @param User A pointer to forward some use information (currently unused).
202 static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000203
Tobias Grosser808cd692015-07-14 09:33:13 +0000204 /// @brief Apply post-scheduling transformations.
205 ///
206 /// This function applies a set of additional local transformations on the
207 /// schedule tree as it computed by the isl scheduler. Local transformations
208 /// applied include:
209 ///
210 /// - Tiling
211 /// - Prevectorization
212 ///
213 /// @param Schedule The schedule object post-transformations will be applied
214 /// on.
215 /// @returns The transformed schedule.
216 static __isl_give isl_schedule *
217 addPostTransforms(__isl_take isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000218
Tobias Grosser20532b82014-04-11 17:56:49 +0000219 using llvm::Pass::doFinalization;
220
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000221 virtual bool doFinalization() override {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000222 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000223 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000224 return true;
225 }
226};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000227}
228
Tobias Grosser73600b82011-10-08 00:30:40 +0000229char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000230
Tobias Grosserb241d922015-07-28 18:03:36 +0000231__isl_give isl_schedule_node *
232IslScheduleOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
233 unsigned DimToVectorize,
234 int VectorWidth) {
235 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000236
Tobias Grosserb241d922015-07-28 18:03:36 +0000237 auto Space = isl_schedule_node_band_get_space(Node);
238 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
239 isl_space_free(Space);
240 assert(DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000241
Tobias Grosserb241d922015-07-28 18:03:36 +0000242 if (DimToVectorize > 0) {
243 Node = isl_schedule_node_band_split(Node, DimToVectorize);
244 Node = isl_schedule_node_child(Node, 0);
245 }
246 if (DimToVectorize < ScheduleDimensions - 1)
247 Node = isl_schedule_node_band_split(Node, 1);
248 Space = isl_schedule_node_band_get_space(Node);
249 auto Sizes = isl_multi_val_zero(Space);
250 auto Ctx = isl_schedule_node_get_ctx(Node);
251 Sizes =
252 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
253 Node = isl_schedule_node_band_tile(Node, Sizes);
254 Node = isl_schedule_node_child(Node, 0);
255 Node = isl_schedule_node_band_sink(Node);
256 Node = isl_schedule_node_child(Node, 0);
257 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000258}
259
Tobias Grosserd891b542015-08-20 12:16:23 +0000260__isl_give isl_schedule_node *
261IslScheduleOptimizer::optimizeBand(__isl_take isl_schedule_node *Node,
262 void *User) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000263 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
264 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000265
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000266 if (isl_schedule_node_n_children(Node) != 1)
267 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000268
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000269 if (!isl_schedule_node_band_get_permutable(Node))
270 return Node;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000271
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000272 auto Space = isl_schedule_node_band_get_space(Node);
273 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000274
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000275 if (Dims <= 1) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000276 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000277 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000278 }
279
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000280 auto Child = isl_schedule_node_get_child(Node, 0);
281 auto Type = isl_schedule_node_get_type(Child);
282 isl_schedule_node_free(Child);
283
284 if (Type != isl_schedule_node_leaf) {
285 isl_space_free(Space);
286 return Node;
287 }
288
Tobias Grosser161c9082015-08-19 08:22:06 +0000289 if (EnableTiling) {
Tobias Grosserf10f4632015-08-19 08:03:37 +0000290 auto Ctx = isl_schedule_node_get_ctx(Node);
291 auto Sizes = isl_multi_val_zero(isl_space_copy(Space));
292 for (unsigned i = 0; i < Dims; i++) {
293 auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
294 Sizes =
295 isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
296 }
297 Node = isl_schedule_node_band_tile(Node, Sizes);
298 Node = isl_schedule_node_child(Node, 0);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000299 }
300
Tobias Grosserf10f4632015-08-19 08:03:37 +0000301 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000302
303 if (PollyVectorizerChoice == VECTORIZER_NONE)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000304 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000305
Tobias Grosserb241d922015-07-28 18:03:36 +0000306 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000307 if (isl_schedule_node_band_member_get_coincident(Node, i)) {
Tobias Grosser07c1c2f2015-08-19 08:46:11 +0000308 Node = IslScheduleOptimizer::prevectSchedBand(Node, i, PrevectorWidth);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000309 break;
310 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000311
Tobias Grosserf10f4632015-08-19 08:03:37 +0000312 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000313}
314
Tobias Grosser808cd692015-07-14 09:33:13 +0000315__isl_give isl_schedule *
316IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000317 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000318 isl_schedule_free(Schedule);
Tobias Grosserb2f39922015-05-28 13:32:11 +0000319 Root = isl_schedule_node_map_descendant_bottom_up(
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000320 Root, IslScheduleOptimizer::optimizeBand, NULL);
Tobias Grosser808cd692015-07-14 09:33:13 +0000321 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000322 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000323 return S;
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
Michael Krusec59f22c2015-06-18 16:45:40 +0000408 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000409
410 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000411 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000412 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000413 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000414 } else {
415 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
416 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +0000417 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000418 }
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
Michael Krusec59f22c2015-06-18 16:45:40 +0000432 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
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 Grosser97d87452015-05-30 06:46:59 +0000457 DEBUG({
458 auto *P = isl_printer_to_str(S.getIslCtx());
459 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
460 P = isl_printer_print_schedule(P, Schedule);
461 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
462 isl_printer_free(P);
463 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000464
Tobias Grosser808cd692015-07-14 09:33:13 +0000465 isl_schedule *NewSchedule = addPostTransforms(Schedule);
466 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000467
Tobias Grosser808cd692015-07-14 09:33:13 +0000468 if (!isProfitableSchedule(S, NewScheduleMap)) {
469 isl_union_map_free(NewScheduleMap);
470 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000471 return false;
472 }
473
Tobias Grosser808cd692015-07-14 09:33:13 +0000474 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000475 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000476
Tobias Grosser808cd692015-07-14 09:33:13 +0000477 isl_union_map_free(NewScheduleMap);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000478 return false;
479}
480
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000481void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000482 isl_printer *p;
483 char *ScheduleStr;
484
485 OS << "Calculated schedule:\n";
486
487 if (!LastSchedule) {
488 OS << "n/a\n";
489 return;
490 }
491
492 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
493 p = isl_printer_print_schedule(p, LastSchedule);
494 ScheduleStr = isl_printer_get_str(p);
495 isl_printer_free(p);
496
497 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000498}
499
Tobias Grosser73600b82011-10-08 00:30:40 +0000500void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000501 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000502 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000503}
504
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000505Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000506 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000507}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000508
509INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
510 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000511INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000512INITIALIZE_PASS_DEPENDENCY(ScopInfo);
513INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
514 "Polly - Optimize schedule of SCoP", false, false)