blob: 1e0630930331174ce9a025656d6cb8ef58e342fd [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
286 auto Sizes = isl_multi_val_zero(Space);
287 auto Ctx = isl_schedule_node_get_ctx(Node);
288
289 for (unsigned i = 0; i < Dims; i++) {
290 auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
291 Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
292 }
293
Tobias Grosser02cf69a2015-04-05 21:52:21 +0000294 isl_schedule_node *Res;
295
296 if (DisableTiling) {
297 isl_multi_val_free(Sizes);
298 Res = Node;
299 } else {
300 Res = isl_schedule_node_band_tile(Node, Sizes);
Tobias Grosser27647942015-07-26 19:22:35 +0000301 Res = isl_schedule_node_child(Res, 0);
Tobias Grosser02cf69a2015-04-05 21:52:21 +0000302 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000303
304 if (PollyVectorizerChoice == VECTORIZER_NONE)
305 return Res;
306
Tobias Grosserb241d922015-07-28 18:03:36 +0000307 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosser3b10c942015-07-25 12:28:56 +0000308 if (isl_schedule_node_band_member_get_coincident(Res, i)) {
Tobias Grosserb241d922015-07-28 18:03:36 +0000309 Res = IslScheduleOptimizer::prevectSchedBand(Res, i);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000310 break;
311 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000312
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000313 return Res;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000314}
315
Tobias Grosser808cd692015-07-14 09:33:13 +0000316__isl_give isl_schedule *
317IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000318 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000319 isl_schedule_free(Schedule);
Tobias Grosserb2f39922015-05-28 13:32:11 +0000320 Root = isl_schedule_node_map_descendant_bottom_up(
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000321 Root, IslScheduleOptimizer::optimizeBand, NULL);
Tobias Grosser808cd692015-07-14 09:33:13 +0000322 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000323 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000324 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000325}
326
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000327bool IslScheduleOptimizer::isProfitableSchedule(
328 Scop &S, __isl_keep isl_union_map *NewSchedule) {
329 // To understand if the schedule has been optimized we check if the schedule
330 // has changed at all.
331 // TODO: We can improve this by tracking if any necessarily beneficial
332 // transformations have been performed. This can e.g. be tiling, loop
333 // interchange, or ...) We can track this either at the place where the
334 // transformation has been performed or, in case of automatic ILP based
335 // optimizations, by comparing (yet to be defined) performance metrics
336 // before/after the scheduling optimizer
337 // (e.g., #stride-one accesses)
338 isl_union_map *OldSchedule = S.getSchedule();
339 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
340 isl_union_map_free(OldSchedule);
341 return changed;
342}
343
Tobias Grosser73600b82011-10-08 00:30:40 +0000344bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +0000345
346 // Skip empty SCoPs but still allow code generation as it will delete the
347 // loops present but not needed.
348 if (S.getSize() == 0) {
349 S.markAsOptimized();
350 return false;
351 }
352
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000353 const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000354
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000355 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000356 return false;
357
Tobias Grosser28781422012-10-16 07:29:19 +0000358 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000359 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000360
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000361 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000362 int ValidityKinds =
363 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000364 int ProximityKinds;
365
366 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000367 ProximityKinds =
368 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000369 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000370 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000371 else {
372 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000373 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000374 ProximityKinds =
375 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000376 }
377
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000378 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000379
Tobias Grosser98610ee2012-02-13 23:31:39 +0000380 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000381 return false;
382
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000383 isl_union_map *Validity = D.getDependences(ValidityKinds);
384 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +0000385
Tobias Grossera26db472012-01-30 19:38:43 +0000386 // Simplify the dependences by removing the constraints introduced by the
387 // domains. This can speed up the scheduling time significantly, as large
388 // constant coefficients will be removed from the dependences. The
389 // introduction of some additional dependences reduces the possible
390 // transformations, but in most cases, such transformation do not seem to be
391 // interesting anyway. In some cases this option may stop the scheduler to
392 // find any schedule.
393 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000394 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
395 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000396 Proximity =
397 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000398 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000399 } else if (SimplifyDeps != "no") {
400 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
401 "or 'no'. Falling back to default: 'yes'\n";
402 }
403
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000404 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +0000405 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
406 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
407 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000408
Michael Krusec59f22c2015-06-18 16:45:40 +0000409 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000410
411 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000412 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000413 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000414 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000415 } else {
416 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
417 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +0000418 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000419 }
420
Tobias Grosser95e860c2012-01-30 19:38:54 +0000421 int IslMaximizeBands;
422
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000423 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000424 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000425 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000426 IslMaximizeBands = 0;
427 } else {
428 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
429 " or 'no'. Falling back to default: 'yes'\n";
430 IslMaximizeBands = 1;
431 }
432
Michael Krusec59f22c2015-06-18 16:45:40 +0000433 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000434 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000435 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000436 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser4f6bcef2015-03-31 07:52:36 +0000437 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000438
439 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000440
441 isl_schedule_constraints *ScheduleConstraints;
442 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
443 ScheduleConstraints =
444 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
445 ScheduleConstraints = isl_schedule_constraints_set_validity(
446 ScheduleConstraints, isl_union_map_copy(Validity));
447 ScheduleConstraints =
448 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000449 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000450 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000451 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
452
453 // In cases the scheduler is not able to optimize the code, we just do not
454 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000455 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000456 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000457
Tobias Grosser97d87452015-05-30 06:46:59 +0000458 DEBUG({
459 auto *P = isl_printer_to_str(S.getIslCtx());
460 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
461 P = isl_printer_print_schedule(P, Schedule);
462 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
463 isl_printer_free(P);
464 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000465
Tobias Grosser808cd692015-07-14 09:33:13 +0000466 isl_schedule *NewSchedule = addPostTransforms(Schedule);
467 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000468
Tobias Grosser808cd692015-07-14 09:33:13 +0000469 if (!isProfitableSchedule(S, NewScheduleMap)) {
470 isl_union_map_free(NewScheduleMap);
471 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000472 return false;
473 }
474
Tobias Grosser808cd692015-07-14 09:33:13 +0000475 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000476 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000477
Tobias Grosser808cd692015-07-14 09:33:13 +0000478 isl_union_map_free(NewScheduleMap);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000479 return false;
480}
481
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000482void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000483 isl_printer *p;
484 char *ScheduleStr;
485
486 OS << "Calculated schedule:\n";
487
488 if (!LastSchedule) {
489 OS << "n/a\n";
490 return;
491 }
492
493 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
494 p = isl_printer_print_schedule(p, LastSchedule);
495 ScheduleStr = isl_printer_get_str(p);
496 isl_printer_free(p);
497
498 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000499}
500
Tobias Grosser73600b82011-10-08 00:30:40 +0000501void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000502 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000503 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000504}
505
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000506Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000507 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000508}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000509
510INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
511 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000512INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000513INITIALIZE_PASS_DEPENDENCY(ScopInfo);
514INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
515 "Polly - Optimize schedule of SCoP", false, false)