blob: 4c9c97191115ee257d737f8c6f3da8bc6689a840 [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 Grosser58032cb2013-06-23 01:29:29 +000074namespace polly {
75bool DisablePollyTiling;
76}
Tobias Grossere602a072013-05-07 07:30:56 +000077static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +000078 DisableTiling("polly-no-tiling",
79 cl::desc("Disable tiling in the scheduler"),
80 cl::location(polly::DisablePollyTiling), cl::init(false),
81 cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser353a2682011-10-23 20:59:26 +000082
Tobias Grossera26db472012-01-30 19:38:43 +000083static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000084 OptimizeDeps("polly-opt-optimize-only",
85 cl::desc("Only a certain kind of dependences (all/raw)"),
86 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
87 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000088
89static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000090 SimplifyDeps("polly-opt-simplify-deps",
91 cl::desc("Dependences should be simplified (yes/no)"),
92 cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
93 cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000094
Tobias Grosser483a90d2014-07-09 10:50:10 +000095static cl::opt<int> MaxConstantTerm(
96 "polly-opt-max-constant-term",
97 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
98 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000099
Tobias Grosser483a90d2014-07-09 10:50:10 +0000100static cl::opt<int> MaxCoefficient(
101 "polly-opt-max-coefficient",
102 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
103 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
104
105static cl::opt<std::string> FusionStrategy(
106 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
107 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +0000108
Tobias Grossere602a072013-05-07 07:30:56 +0000109static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000110 MaximizeBandDepth("polly-opt-maximize-bands",
111 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
112 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000113
Tobias Grosser483a90d2014-07-09 10:50:10 +0000114static cl::opt<int> DefaultTileSize(
115 "polly-default-tile-size",
116 cl::desc("The default tile size (if not enough were provided by"
117 " --polly-tile-sizes)"),
118 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000119
120static cl::list<int> TileSizes("polly-tile-sizes",
121 cl::desc("A tile size"
122 " for each loop dimension, filled with"
123 " --polly-default-tile-size"),
124 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
125 cl::cat(PollyCategory));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000126namespace {
127
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000128class IslScheduleOptimizer : public ScopPass {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000129public:
130 static char ID;
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000131 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
Tobias Grosser28781422012-10-16 07:29:19 +0000132
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000133 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000134
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000135 bool runOnScop(Scop &S) override;
136 void printScop(raw_ostream &OS, Scop &S) const override;
137 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosser460e9a42012-04-25 13:22:43 +0000138
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000139private:
140 isl_schedule *LastSchedule;
Tobias Grosser28781422012-10-16 07:29:19 +0000141
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000142 /// @brief Decide if the @p NewSchedule is profitable for @p S.
143 ///
144 /// @param S The SCoP we optimize.
145 /// @param NewSchedule The new schedule we computed.
146 ///
147 /// @return True, if we believe @p NewSchedule is an improvement for @p S.
148 bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule);
149
Tobias Grosserb241d922015-07-28 18:03:36 +0000150 /// @brief Pre-vectorizes one scheduling dimension of a schedule band.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000151 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000152 /// prevectSchedBand splits out the dimension DimToVectorize, tiles it and
153 /// sinks the resulting point loop.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000154 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000155 /// Example (DimToVectorize=0, VectorWidth=4):
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000156 ///
Tobias Grosserb241d922015-07-28 18:03:36 +0000157 /// | Before transformation:
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000158 /// |
159 /// | A[i,j] -> [i,j]
160 /// |
161 /// | for (i = 0; i < 128; i++)
162 /// | for (j = 0; j < 128; j++)
163 /// | A(i,j);
164 ///
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000165 /// | After transformation:
166 /// |
Tobias Grosserb241d922015-07-28 18:03:36 +0000167 /// | for (it = 0; it < 32; it+=1)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000168 /// | for (j = 0; j < 128; j++)
Tobias Grosserb241d922015-07-28 18:03:36 +0000169 /// | for (ip = 0; ip <= 3; ip++)
170 /// | A(4 * it + ip,j);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000171 ///
172 /// The goal of this transformation is to create a trivially vectorizable
173 /// loop. This means a parallel loop at the innermost level that has a
174 /// constant number of iterations corresponding to the target vector width.
175 ///
176 /// This transformation creates a loop at the innermost level. The loop has
177 /// a constant number of iterations, if the number of loop iterations at
178 /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
179 /// currently constant and not yet target specific. This function does not
180 /// reason about parallelism.
Tobias Grosserb241d922015-07-28 18:03:36 +0000181 static __isl_give isl_schedule_node *
182 prevectSchedBand(__isl_take isl_schedule_node *Node, unsigned DimToVectorize,
183 int VectorWidth = 4);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000184
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000185 /// @brief Apply additional optimizations on the bands in the schedule tree.
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000186 ///
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000187 /// We are looking for an innermost band node and apply the following
188 /// transformations:
189 ///
190 /// - Tile the band
191 /// - if the band is tileable
192 /// - if the band has more than one loop dimension
193 ///
Tobias Grosser3b10c942015-07-25 12:28:56 +0000194 /// - Prevectorize the schedule of the band (or the point loop in case of
Tobias Grosserb241d922015-07-28 18:03:36 +0000195 /// tiling).
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000196 /// - if vectorization is enabled
197 ///
198 /// @param Node The schedule node to (possibly) optimize.
199 /// @param User A pointer to forward some use information (currently unused).
200 static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
Tobias Grosser460e9a42012-04-25 13:22:43 +0000201
Tobias Grosser808cd692015-07-14 09:33:13 +0000202 /// @brief Apply post-scheduling transformations.
203 ///
204 /// This function applies a set of additional local transformations on the
205 /// schedule tree as it computed by the isl scheduler. Local transformations
206 /// applied include:
207 ///
208 /// - Tiling
209 /// - Prevectorization
210 ///
211 /// @param Schedule The schedule object post-transformations will be applied
212 /// on.
213 /// @returns The transformed schedule.
214 static __isl_give isl_schedule *
215 addPostTransforms(__isl_take isl_schedule *Schedule);
Tobias Grosser28781422012-10-16 07:29:19 +0000216
Tobias Grosser20532b82014-04-11 17:56:49 +0000217 using llvm::Pass::doFinalization;
218
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000219 virtual bool doFinalization() override {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000220 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000221 LastSchedule = nullptr;
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000222 return true;
223 }
224};
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000225}
226
Tobias Grosser73600b82011-10-08 00:30:40 +0000227char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000228
Tobias Grosserb241d922015-07-28 18:03:36 +0000229__isl_give isl_schedule_node *
230IslScheduleOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
231 unsigned DimToVectorize,
232 int VectorWidth) {
233 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000234
Tobias Grosserb241d922015-07-28 18:03:36 +0000235 auto Space = isl_schedule_node_band_get_space(Node);
236 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
237 isl_space_free(Space);
238 assert(DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000239
Tobias Grosserb241d922015-07-28 18:03:36 +0000240 if (DimToVectorize > 0) {
241 Node = isl_schedule_node_band_split(Node, DimToVectorize);
242 Node = isl_schedule_node_child(Node, 0);
243 }
244 if (DimToVectorize < ScheduleDimensions - 1)
245 Node = isl_schedule_node_band_split(Node, 1);
246 Space = isl_schedule_node_band_get_space(Node);
247 auto Sizes = isl_multi_val_zero(Space);
248 auto Ctx = isl_schedule_node_get_ctx(Node);
249 Sizes =
250 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
251 Node = isl_schedule_node_band_tile(Node, Sizes);
252 Node = isl_schedule_node_child(Node, 0);
253 Node = isl_schedule_node_band_sink(Node);
254 Node = isl_schedule_node_child(Node, 0);
255 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000256}
257
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000258isl_schedule_node *IslScheduleOptimizer::optimizeBand(isl_schedule_node *Node,
259 void *User) {
260 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
261 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000262
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000263 if (isl_schedule_node_n_children(Node) != 1)
264 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000265
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000266 if (!isl_schedule_node_band_get_permutable(Node))
267 return Node;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000268
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000269 auto Space = isl_schedule_node_band_get_space(Node);
270 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000271
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000272 if (Dims <= 1) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000273 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000274 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000275 }
276
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000277 auto Child = isl_schedule_node_get_child(Node, 0);
278 auto Type = isl_schedule_node_get_type(Child);
279 isl_schedule_node_free(Child);
280
281 if (Type != isl_schedule_node_leaf) {
282 isl_space_free(Space);
283 return Node;
284 }
285
Tobias Grosserf10f4632015-08-19 08:03:37 +0000286 if (!DisableTiling) {
287 auto Ctx = isl_schedule_node_get_ctx(Node);
288 auto Sizes = isl_multi_val_zero(isl_space_copy(Space));
289 for (unsigned i = 0; i < Dims; i++) {
290 auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
291 Sizes =
292 isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
293 }
294 Node = isl_schedule_node_band_tile(Node, Sizes);
295 Node = isl_schedule_node_child(Node, 0);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000296 }
297
Tobias Grosserf10f4632015-08-19 08:03:37 +0000298 isl_space_free(Space);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000299
300 if (PollyVectorizerChoice == VECTORIZER_NONE)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000301 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000302
Tobias Grosserb241d922015-07-28 18:03:36 +0000303 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000304 if (isl_schedule_node_band_member_get_coincident(Node, i)) {
305 Node = IslScheduleOptimizer::prevectSchedBand(Node, i);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000306 break;
307 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000308
Tobias Grosserf10f4632015-08-19 08:03:37 +0000309 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000310}
311
Tobias Grosser808cd692015-07-14 09:33:13 +0000312__isl_give isl_schedule *
313IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000314 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000315 isl_schedule_free(Schedule);
Tobias Grosserb2f39922015-05-28 13:32:11 +0000316 Root = isl_schedule_node_map_descendant_bottom_up(
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000317 Root, IslScheduleOptimizer::optimizeBand, NULL);
Tobias Grosser808cd692015-07-14 09:33:13 +0000318 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000319 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000320 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000321}
322
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000323bool IslScheduleOptimizer::isProfitableSchedule(
324 Scop &S, __isl_keep isl_union_map *NewSchedule) {
325 // To understand if the schedule has been optimized we check if the schedule
326 // has changed at all.
327 // TODO: We can improve this by tracking if any necessarily beneficial
328 // transformations have been performed. This can e.g. be tiling, loop
329 // interchange, or ...) We can track this either at the place where the
330 // transformation has been performed or, in case of automatic ILP based
331 // optimizations, by comparing (yet to be defined) performance metrics
332 // before/after the scheduling optimizer
333 // (e.g., #stride-one accesses)
334 isl_union_map *OldSchedule = S.getSchedule();
335 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
336 isl_union_map_free(OldSchedule);
337 return changed;
338}
339
Tobias Grosser73600b82011-10-08 00:30:40 +0000340bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +0000341
342 // Skip empty SCoPs but still allow code generation as it will delete the
343 // loops present but not needed.
344 if (S.getSize() == 0) {
345 S.markAsOptimized();
346 return false;
347 }
348
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000349 const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000350
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000351 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000352 return false;
353
Tobias Grosser28781422012-10-16 07:29:19 +0000354 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000355 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000356
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000357 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000358 int ValidityKinds =
359 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000360 int ProximityKinds;
361
362 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000363 ProximityKinds =
364 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000365 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000366 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000367 else {
368 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000369 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000370 ProximityKinds =
371 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000372 }
373
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000374 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000375
Tobias Grosser98610ee2012-02-13 23:31:39 +0000376 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000377 return false;
378
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000379 isl_union_map *Validity = D.getDependences(ValidityKinds);
380 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +0000381
Tobias Grossera26db472012-01-30 19:38:43 +0000382 // Simplify the dependences by removing the constraints introduced by the
383 // domains. This can speed up the scheduling time significantly, as large
384 // constant coefficients will be removed from the dependences. The
385 // introduction of some additional dependences reduces the possible
386 // transformations, but in most cases, such transformation do not seem to be
387 // interesting anyway. In some cases this option may stop the scheduler to
388 // find any schedule.
389 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000390 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
391 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000392 Proximity =
393 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000394 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000395 } else if (SimplifyDeps != "no") {
396 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
397 "or 'no'. Falling back to default: 'yes'\n";
398 }
399
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000400 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +0000401 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
402 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
403 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000404
Michael Krusec59f22c2015-06-18 16:45:40 +0000405 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000406
407 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000408 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000409 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000410 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000411 } else {
412 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
413 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +0000414 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000415 }
416
Tobias Grosser95e860c2012-01-30 19:38:54 +0000417 int IslMaximizeBands;
418
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000419 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000420 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000421 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000422 IslMaximizeBands = 0;
423 } else {
424 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
425 " or 'no'. Falling back to default: 'yes'\n";
426 IslMaximizeBands = 1;
427 }
428
Michael Krusec59f22c2015-06-18 16:45:40 +0000429 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000430 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000431 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000432 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser4f6bcef2015-03-31 07:52:36 +0000433 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000434
435 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000436
437 isl_schedule_constraints *ScheduleConstraints;
438 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
439 ScheduleConstraints =
440 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
441 ScheduleConstraints = isl_schedule_constraints_set_validity(
442 ScheduleConstraints, isl_union_map_copy(Validity));
443 ScheduleConstraints =
444 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000445 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000446 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000447 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
448
449 // In cases the scheduler is not able to optimize the code, we just do not
450 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000451 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000452 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000453
Tobias Grosser97d87452015-05-30 06:46:59 +0000454 DEBUG({
455 auto *P = isl_printer_to_str(S.getIslCtx());
456 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
457 P = isl_printer_print_schedule(P, Schedule);
458 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
459 isl_printer_free(P);
460 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000461
Tobias Grosser808cd692015-07-14 09:33:13 +0000462 isl_schedule *NewSchedule = addPostTransforms(Schedule);
463 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000464
Tobias Grosser808cd692015-07-14 09:33:13 +0000465 if (!isProfitableSchedule(S, NewScheduleMap)) {
466 isl_union_map_free(NewScheduleMap);
467 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000468 return false;
469 }
470
Tobias Grosser808cd692015-07-14 09:33:13 +0000471 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000472 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000473
Tobias Grosser808cd692015-07-14 09:33:13 +0000474 isl_union_map_free(NewScheduleMap);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000475 return false;
476}
477
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000478void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000479 isl_printer *p;
480 char *ScheduleStr;
481
482 OS << "Calculated schedule:\n";
483
484 if (!LastSchedule) {
485 OS << "n/a\n";
486 return;
487 }
488
489 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
490 p = isl_printer_print_schedule(p, LastSchedule);
491 ScheduleStr = isl_printer_get_str(p);
492 isl_printer_free(p);
493
494 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000495}
496
Tobias Grosser73600b82011-10-08 00:30:40 +0000497void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000498 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000499 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000500}
501
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000502Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000503 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000504}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000505
506INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
507 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000508INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000509INITIALIZE_PASS_DEPENDENCY(ScopInfo);
510INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
511 "Polly - Optimize schedule of SCoP", false, false)