blob: 4670b10eb6e58645e42b88761133673580b347d3 [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 Grosser9bdea572015-08-20 12:22:37 +0000152 /// @brief Tile a schedule node.
153 ///
154 /// @param Node The node to tile.
155 /// @param TileSizes A vector of tile sizes that should be used for
156 /// tiling.
157 /// @param DefaultTileSize A default tile size that is used for dimensions
158 /// that are not covered by the TileSizes vector.
159 static __isl_give isl_schedule_node *
160 tileNode(__isl_take isl_schedule_node *Node, ArrayRef<int> TileSizes,
161 int DefaultTileSize);
162
Tobias Grosserb241d922015-07-28 18:03:36 +0000163 /// @brief Pre-vectorizes one scheduling dimension of a schedule band.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000164 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000165 /// prevectSchedBand splits out the dimension DimToVectorize, tiles it and
166 /// sinks the resulting point loop.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000167 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000168 /// Example (DimToVectorize=0, VectorWidth=4):
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000169 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000170 /// | Before transformation:
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000171 /// |
172 /// | A[i,j] -> [i,j]
173 /// |
174 /// | for (i = 0; i < 128; i++)
175 /// | for (j = 0; j < 128; j++)
176 /// | A(i,j);
177 ///
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000178 /// | After transformation:
179 /// |
Tobias Grosserb241d922015-07-28 18:03:36 +0000180 /// | for (it = 0; it < 32; it+=1)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000181 /// | for (j = 0; j < 128; j++)
Tobias Grosserb241d922015-07-28 18:03:36 +0000182 /// | for (ip = 0; ip <= 3; ip++)
183 /// | A(4 * it + ip,j);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000184 ///
185 /// The goal of this transformation is to create a trivially vectorizable
186 /// loop. This means a parallel loop at the innermost level that has a
187 /// constant number of iterations corresponding to the target vector width.
188 ///
189 /// This transformation creates a loop at the innermost level. The loop has
190 /// a constant number of iterations, if the number of loop iterations at
191 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
192 /// currently constant and not yet target specific. This function does not
193 /// reason about parallelism.
Tobias Grosserb241d922015-07-28 18:03:36 +0000194 static __isl_give isl_schedule_node *
195 prevectSchedBand(__isl_take isl_schedule_node *Node, unsigned DimToVectorize,
Tobias Grosser07c1c2f2015-08-19 08:46:11 +0000196 int VectorWidth);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000197
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000198 /// @brief Apply additional optimizations on the bands in the schedule tree.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000199 ///
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000200 /// We are looking for an innermost band node and apply the following
201 /// transformations:
202 ///
203 /// - Tile the band
204 /// - if the band is tileable
205 /// - if the band has more than one loop dimension
206 ///
Tobias Grosser3b10c942015-07-25 12:28:56 +0000207 /// - Prevectorize the schedule of the band (or the point loop in case of
Tobias Grosserb241d922015-07-28 18:03:36 +0000208 /// tiling).
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000209 /// - if vectorization is enabled
210 ///
211 /// @param Node The schedule node to (possibly) optimize.
212 /// @param User A pointer to forward some use information (currently unused).
213 static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000214
Tobias Grosser808cd692015-07-14 09:33:13 +0000215 /// @brief Apply post-scheduling transformations.
216 ///
217 /// This function applies a set of additional local transformations on the
218 /// schedule tree as it computed by the isl scheduler. Local transformations
219 /// applied include:
220 ///
221 /// - Tiling
222 /// - Prevectorization
223 ///
224 /// @param Schedule The schedule object post-transformations will be applied
225 /// on.
226 /// @returns The transformed schedule.
227 static __isl_give isl_schedule *
228 addPostTransforms(__isl_take isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000229
Tobias Grosser20532b82014-04-11 17:56:49 +0000230 using llvm::Pass::doFinalization;
231
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000232 virtual bool doFinalization() override {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000233 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000234 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000235 return true;
236 }
237};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000238}
239
Tobias Grosser73600b82011-10-08 00:30:40 +0000240char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000241
Tobias Grosserb241d922015-07-28 18:03:36 +0000242__isl_give isl_schedule_node *
243IslScheduleOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
244 unsigned DimToVectorize,
245 int VectorWidth) {
246 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000247
Tobias Grosserb241d922015-07-28 18:03:36 +0000248 auto Space = isl_schedule_node_band_get_space(Node);
249 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
250 isl_space_free(Space);
251 assert(DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000252
Tobias Grosserb241d922015-07-28 18:03:36 +0000253 if (DimToVectorize > 0) {
254 Node = isl_schedule_node_band_split(Node, DimToVectorize);
255 Node = isl_schedule_node_child(Node, 0);
256 }
257 if (DimToVectorize < ScheduleDimensions - 1)
258 Node = isl_schedule_node_band_split(Node, 1);
259 Space = isl_schedule_node_band_get_space(Node);
260 auto Sizes = isl_multi_val_zero(Space);
261 auto Ctx = isl_schedule_node_get_ctx(Node);
262 Sizes =
263 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
264 Node = isl_schedule_node_band_tile(Node, Sizes);
265 Node = isl_schedule_node_child(Node, 0);
266 Node = isl_schedule_node_band_sink(Node);
267 Node = isl_schedule_node_child(Node, 0);
268 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000269}
270
Tobias Grosserd891b542015-08-20 12:16:23 +0000271__isl_give isl_schedule_node *
Tobias Grosser9bdea572015-08-20 12:22:37 +0000272IslScheduleOptimizer::tileNode(__isl_take isl_schedule_node *Node,
273 ArrayRef<int> TileSizes, int DefaultTileSize) {
274 auto Ctx = isl_schedule_node_get_ctx(Node);
275 auto Space = isl_schedule_node_band_get_space(Node);
276 auto Dims = isl_space_dim(Space, isl_dim_set);
277 auto Sizes = isl_multi_val_zero(Space);
278 for (unsigned i = 0; i < Dims; i++) {
279 auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize;
280 Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
281 }
282 Node = isl_schedule_node_band_tile(Node, Sizes);
283 return isl_schedule_node_child(Node, 0);
284}
285
286__isl_give isl_schedule_node *
Tobias Grosserd891b542015-08-20 12:16:23 +0000287IslScheduleOptimizer::optimizeBand(__isl_take isl_schedule_node *Node,
288 void *User) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000289 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
290 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000291
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000292 if (isl_schedule_node_n_children(Node) != 1)
293 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000294
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000295 if (!isl_schedule_node_band_get_permutable(Node))
296 return Node;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000297
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000298 auto Space = isl_schedule_node_band_get_space(Node);
299 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000300 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000301
Tobias Grosser9bdea572015-08-20 12:22:37 +0000302 if (Dims <= 1)
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000303 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000304
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000305 auto Child = isl_schedule_node_get_child(Node, 0);
306 auto Type = isl_schedule_node_get_type(Child);
307 isl_schedule_node_free(Child);
308
Tobias Grosser9bdea572015-08-20 12:22:37 +0000309 if (Type != isl_schedule_node_leaf)
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000310 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000311
Tobias Grosser9bdea572015-08-20 12:22:37 +0000312 if (EnableTiling)
313 Node = tileNode(Node, TileSizes, DefaultTileSize);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000314
315 if (PollyVectorizerChoice == VECTORIZER_NONE)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000316 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000317
Tobias Grosserb241d922015-07-28 18:03:36 +0000318 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000319 if (isl_schedule_node_band_member_get_coincident(Node, i)) {
Tobias Grosser07c1c2f2015-08-19 08:46:11 +0000320 Node = IslScheduleOptimizer::prevectSchedBand(Node, i, PrevectorWidth);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000321 break;
322 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000323
Tobias Grosserf10f4632015-08-19 08:03:37 +0000324 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000325}
326
Tobias Grosser808cd692015-07-14 09:33:13 +0000327__isl_give isl_schedule *
328IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000329 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000330 isl_schedule_free(Schedule);
Tobias Grosserb2f39922015-05-28 13:32:11 +0000331 Root = isl_schedule_node_map_descendant_bottom_up(
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000332 Root, IslScheduleOptimizer::optimizeBand, NULL);
Tobias Grosser808cd692015-07-14 09:33:13 +0000333 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000334 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000335 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000336}
337
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000338bool IslScheduleOptimizer::isProfitableSchedule(
339 Scop &S, __isl_keep isl_union_map *NewSchedule) {
340 // To understand if the schedule has been optimized we check if the schedule
341 // has changed at all.
342 // TODO: We can improve this by tracking if any necessarily beneficial
343 // transformations have been performed. This can e.g. be tiling, loop
344 // interchange, or ...) We can track this either at the place where the
345 // transformation has been performed or, in case of automatic ILP based
346 // optimizations, by comparing (yet to be defined) performance metrics
347 // before/after the scheduling optimizer
348 // (e.g., #stride-one accesses)
349 isl_union_map *OldSchedule = S.getSchedule();
350 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
351 isl_union_map_free(OldSchedule);
352 return changed;
353}
354
Tobias Grosser73600b82011-10-08 00:30:40 +0000355bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +0000356
357 // Skip empty SCoPs but still allow code generation as it will delete the
358 // loops present but not needed.
359 if (S.getSize() == 0) {
360 S.markAsOptimized();
361 return false;
362 }
363
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000364 const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000365
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000366 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000367 return false;
368
Tobias Grosser28781422012-10-16 07:29:19 +0000369 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000370 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000371
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000372 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000373 int ValidityKinds =
374 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000375 int ProximityKinds;
376
377 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000378 ProximityKinds =
379 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000380 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000381 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000382 else {
383 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000384 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000385 ProximityKinds =
386 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000387 }
388
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000389 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000390
Tobias Grosser98610ee2012-02-13 23:31:39 +0000391 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000392 return false;
393
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000394 isl_union_map *Validity = D.getDependences(ValidityKinds);
395 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +0000396
Tobias Grossera26db472012-01-30 19:38:43 +0000397 // Simplify the dependences by removing the constraints introduced by the
398 // domains. This can speed up the scheduling time significantly, as large
399 // constant coefficients will be removed from the dependences. The
400 // introduction of some additional dependences reduces the possible
401 // transformations, but in most cases, such transformation do not seem to be
402 // interesting anyway. In some cases this option may stop the scheduler to
403 // find any schedule.
404 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000405 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
406 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000407 Proximity =
408 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000409 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000410 } else if (SimplifyDeps != "no") {
411 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
412 "or 'no'. Falling back to default: 'yes'\n";
413 }
414
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000415 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +0000416 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
417 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
418 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000419
Michael Krusec59f22c2015-06-18 16:45:40 +0000420 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000421
422 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000423 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000424 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000425 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000426 } else {
427 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
428 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +0000429 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000430 }
431
Tobias Grosser95e860c2012-01-30 19:38:54 +0000432 int IslMaximizeBands;
433
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000434 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000435 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000436 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000437 IslMaximizeBands = 0;
438 } else {
439 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
440 " or 'no'. Falling back to default: 'yes'\n";
441 IslMaximizeBands = 1;
442 }
443
Michael Krusec59f22c2015-06-18 16:45:40 +0000444 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000445 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000446 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000447 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser4f6bcef2015-03-31 07:52:36 +0000448 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000449
450 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000451
452 isl_schedule_constraints *ScheduleConstraints;
453 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
454 ScheduleConstraints =
455 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
456 ScheduleConstraints = isl_schedule_constraints_set_validity(
457 ScheduleConstraints, isl_union_map_copy(Validity));
458 ScheduleConstraints =
459 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000460 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000461 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000462 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
463
464 // In cases the scheduler is not able to optimize the code, we just do not
465 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000466 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000467 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000468
Tobias Grosser97d87452015-05-30 06:46:59 +0000469 DEBUG({
470 auto *P = isl_printer_to_str(S.getIslCtx());
471 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
472 P = isl_printer_print_schedule(P, Schedule);
473 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
474 isl_printer_free(P);
475 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000476
Tobias Grosser808cd692015-07-14 09:33:13 +0000477 isl_schedule *NewSchedule = addPostTransforms(Schedule);
478 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000479
Tobias Grosser808cd692015-07-14 09:33:13 +0000480 if (!isProfitableSchedule(S, NewScheduleMap)) {
481 isl_union_map_free(NewScheduleMap);
482 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000483 return false;
484 }
485
Tobias Grosser808cd692015-07-14 09:33:13 +0000486 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000487 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000488
Tobias Grosser808cd692015-07-14 09:33:13 +0000489 isl_union_map_free(NewScheduleMap);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000490 return false;
491}
492
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000493void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000494 isl_printer *p;
495 char *ScheduleStr;
496
497 OS << "Calculated schedule:\n";
498
499 if (!LastSchedule) {
500 OS << "n/a\n";
501 return;
502 }
503
504 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
505 p = isl_printer_print_schedule(p, LastSchedule);
506 ScheduleStr = isl_printer_get_str(p);
507 isl_printer_free(p);
508
509 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000510}
511
Tobias Grosser73600b82011-10-08 00:30:40 +0000512void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000513 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000514 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000515}
516
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000517Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000518 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000519}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000520
521INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
522 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000523INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000524INITIALIZE_PASS_DEPENDENCY(ScopInfo);
525INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
526 "Polly - Optimize schedule of SCoP", false, false)