blob: e81a2655eea11f8e09906cfcd587f6d5e3f90cd9 [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 Grossera26db472012-01-30 19:38:43 +000074static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000075 OptimizeDeps("polly-opt-optimize-only",
76 cl::desc("Only a certain kind of dependences (all/raw)"),
77 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
78 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000079
80static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000081 SimplifyDeps("polly-opt-simplify-deps",
82 cl::desc("Dependences should be simplified (yes/no)"),
83 cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
84 cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000085
Tobias Grosser483a90d2014-07-09 10:50:10 +000086static cl::opt<int> MaxConstantTerm(
87 "polly-opt-max-constant-term",
88 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
89 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000090
Tobias Grosser483a90d2014-07-09 10:50:10 +000091static cl::opt<int> MaxCoefficient(
92 "polly-opt-max-coefficient",
93 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
94 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
95
96static cl::opt<std::string> FusionStrategy(
97 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
98 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +000099
Tobias Grossere602a072013-05-07 07:30:56 +0000100static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000101 MaximizeBandDepth("polly-opt-maximize-bands",
102 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
103 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000104
Tobias Grosser07c1c2f2015-08-19 08:46:11 +0000105static cl::opt<int> PrevectorWidth(
106 "polly-prevect-width",
107 cl::desc(
108 "The number of loop iterations to strip-mine for pre-vectorization"),
109 cl::Hidden, cl::init(4), cl::ZeroOrMore, cl::cat(PollyCategory));
110
Tobias Grosser04832712015-08-20 13:45:02 +0000111static cl::opt<bool> FirstLevelTiling("polly-tiling",
112 cl::desc("Enable loop tiling"),
113 cl::init(true), cl::ZeroOrMore,
114 cl::cat(PollyCategory));
115
116static cl::opt<int> FirstLevelDefaultTileSize(
Tobias Grosser483a90d2014-07-09 10:50:10 +0000117 "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
Tobias Grosser04832712015-08-20 13:45:02 +0000122static cl::list<int> FirstLevelTileSizes(
123 "polly-tile-sizes", cl::desc("A tile size for each loop dimension, filled "
124 "with --polly-default-tile-size"),
125 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory));
126
127static cl::opt<bool>
128 SecondLevelTiling("polly-2nd-level-tiling",
129 cl::desc("Enable a 2nd level loop of loop tiling"),
130 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
131
132static cl::opt<int> SecondLevelDefaultTileSize(
133 "polly-2nd-level-default-tile-size",
134 cl::desc("The default 2nd-level tile size (if not enough were provided by"
135 " --polly-2nd-level-tile-sizes)"),
136 cl::Hidden, cl::init(16), cl::ZeroOrMore, cl::cat(PollyCategory));
137
138static cl::list<int>
139 SecondLevelTileSizes("polly-2nd-level-tile-sizes",
140 cl::desc("A tile size for each loop dimension, filled "
141 "with --polly-default-tile-size"),
142 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
143 cl::cat(PollyCategory));
144
Tobias Grosser42e24892015-08-20 13:45:05 +0000145static cl::opt<bool> RegisterTiling("polly-register-tiling",
146 cl::desc("Enable register tiling"),
147 cl::init(false), cl::ZeroOrMore,
148 cl::cat(PollyCategory));
149
150static cl::opt<int> RegisterDefaultTileSize(
151 "polly-register-tiling-default-tile-size",
152 cl::desc("The default register tile size (if not enough were provided by"
153 " --polly-register-tile-sizes)"),
154 cl::Hidden, cl::init(2), cl::ZeroOrMore, cl::cat(PollyCategory));
155
156static cl::list<int>
157 RegisterTileSizes("polly-register-tile-sizes",
158 cl::desc("A tile size for each loop dimension, filled "
159 "with --polly-register-tile-size"),
160 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
161 cl::cat(PollyCategory));
162
Tobias Grosserb241d922015-07-28 18:03:36 +0000163__isl_give isl_schedule_node *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000164ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
165 unsigned DimToVectorize,
166 int VectorWidth) {
Tobias Grosserb241d922015-07-28 18:03:36 +0000167 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000168
Tobias Grosserb241d922015-07-28 18:03:36 +0000169 auto Space = isl_schedule_node_band_get_space(Node);
170 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
171 isl_space_free(Space);
172 assert(DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000173
Tobias Grosserb241d922015-07-28 18:03:36 +0000174 if (DimToVectorize > 0) {
175 Node = isl_schedule_node_band_split(Node, DimToVectorize);
176 Node = isl_schedule_node_child(Node, 0);
177 }
178 if (DimToVectorize < ScheduleDimensions - 1)
179 Node = isl_schedule_node_band_split(Node, 1);
180 Space = isl_schedule_node_band_get_space(Node);
181 auto Sizes = isl_multi_val_zero(Space);
182 auto Ctx = isl_schedule_node_get_ctx(Node);
183 Sizes =
184 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
185 Node = isl_schedule_node_band_tile(Node, Sizes);
186 Node = isl_schedule_node_child(Node, 0);
Tobias Grosser42e24892015-08-20 13:45:05 +0000187 // Make sure the "trivially vectorizable loop" is not unrolled. Otherwise,
188 // we will have troubles to match it in the backend.
189 Node = isl_schedule_node_band_set_ast_build_options(
Tobias Grosserfc490a92015-08-20 19:08:16 +0000190 Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }"));
191 Node = isl_schedule_node_band_sink(Node);
Tobias Grosserb241d922015-07-28 18:03:36 +0000192 Node = isl_schedule_node_child(Node, 0);
193 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000194}
195
Tobias Grosserd891b542015-08-20 12:16:23 +0000196__isl_give isl_schedule_node *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000197ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node,
198 const char *Identifier, ArrayRef<int> TileSizes,
199 int DefaultTileSize) {
Tobias Grosser9bdea572015-08-20 12:22:37 +0000200 auto Ctx = isl_schedule_node_get_ctx(Node);
201 auto Space = isl_schedule_node_band_get_space(Node);
202 auto Dims = isl_space_dim(Space, isl_dim_set);
203 auto Sizes = isl_multi_val_zero(Space);
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000204 std::string IdentifierString(Identifier);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000205 for (unsigned i = 0; i < Dims; i++) {
206 auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize;
207 Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
208 }
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000209 auto TileLoopMarkerStr = IdentifierString + " - Tiles";
210 isl_id *TileLoopMarker =
211 isl_id_alloc(Ctx, TileLoopMarkerStr.c_str(), nullptr);
212 Node = isl_schedule_node_insert_mark(Node, TileLoopMarker);
213 Node = isl_schedule_node_child(Node, 0);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000214 Node = isl_schedule_node_band_tile(Node, Sizes);
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000215 Node = isl_schedule_node_child(Node, 0);
216 auto PointLoopMarkerStr = IdentifierString + " - Points";
217 isl_id *PointLoopMarker =
218 isl_id_alloc(Ctx, PointLoopMarkerStr.c_str(), nullptr);
219 Node = isl_schedule_node_insert_mark(Node, PointLoopMarker);
220 Node = isl_schedule_node_child(Node, 0);
221 return Node;
Tobias Grosser9bdea572015-08-20 12:22:37 +0000222}
223
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000224bool ScheduleTreeOptimizer::isTileableBandNode(
Tobias Grosser862b9b52015-08-20 12:32:45 +0000225 __isl_keep isl_schedule_node *Node) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000226 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000227 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000228
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000229 if (isl_schedule_node_n_children(Node) != 1)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000230 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000231
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000232 if (!isl_schedule_node_band_get_permutable(Node))
Tobias Grosser862b9b52015-08-20 12:32:45 +0000233 return false;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000234
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000235 auto Space = isl_schedule_node_band_get_space(Node);
236 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000237 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000238
Tobias Grosser9bdea572015-08-20 12:22:37 +0000239 if (Dims <= 1)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000240 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000241
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000242 auto Child = isl_schedule_node_get_child(Node, 0);
243 auto Type = isl_schedule_node_get_type(Child);
244 isl_schedule_node_free(Child);
245
Tobias Grosser9bdea572015-08-20 12:22:37 +0000246 if (Type != isl_schedule_node_leaf)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000247 return false;
248
249 return true;
250}
251
252__isl_give isl_schedule_node *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000253ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node,
254 void *User) {
Tobias Grosser862b9b52015-08-20 12:32:45 +0000255 if (!isTileableBandNode(Node))
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000256 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000257
Tobias Grosser04832712015-08-20 13:45:02 +0000258 if (FirstLevelTiling)
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000259 Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes,
260 FirstLevelDefaultTileSize);
Tobias Grosser04832712015-08-20 13:45:02 +0000261
262 if (SecondLevelTiling)
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000263 Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes,
264 SecondLevelDefaultTileSize);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000265
Tobias Grosser42e24892015-08-20 13:45:05 +0000266 if (RegisterTiling) {
267 auto *Ctx = isl_schedule_node_get_ctx(Node);
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000268 Node = tileNode(Node, "Register tiling", RegisterTileSizes,
269 RegisterDefaultTileSize);
Tobias Grosser42e24892015-08-20 13:45:05 +0000270 Node = isl_schedule_node_band_set_ast_build_options(
271 Node, isl_union_set_read_from_str(Ctx, "{unroll[x]}"));
272 }
273
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000274 if (PollyVectorizerChoice == VECTORIZER_NONE)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000275 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000276
Tobias Grosser862b9b52015-08-20 12:32:45 +0000277 auto Space = isl_schedule_node_band_get_space(Node);
278 auto Dims = isl_space_dim(Space, isl_dim_set);
279 isl_space_free(Space);
280
Tobias Grosserb241d922015-07-28 18:03:36 +0000281 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000282 if (isl_schedule_node_band_member_get_coincident(Node, i)) {
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000283 Node = prevectSchedBand(Node, i, PrevectorWidth);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000284 break;
285 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000286
Tobias Grosserf10f4632015-08-19 08:03:37 +0000287 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000288}
289
Tobias Grosser808cd692015-07-14 09:33:13 +0000290__isl_give isl_schedule *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000291ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000292 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000293 Root = optimizeScheduleNode(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000294 isl_schedule_free(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000295 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000296 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +0000297 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000298}
299
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000300__isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode(
301 __isl_take isl_schedule_node *Node) {
302 Node = isl_schedule_node_map_descendant_bottom_up(Node, optimizeBand, NULL);
303 return Node;
304}
305
306bool ScheduleTreeOptimizer::isProfitableSchedule(
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000307 Scop &S, __isl_keep isl_union_map *NewSchedule) {
308 // To understand if the schedule has been optimized we check if the schedule
309 // has changed at all.
310 // TODO: We can improve this by tracking if any necessarily beneficial
311 // transformations have been performed. This can e.g. be tiling, loop
312 // interchange, or ...) We can track this either at the place where the
313 // transformation has been performed or, in case of automatic ILP based
314 // optimizations, by comparing (yet to be defined) performance metrics
315 // before/after the scheduling optimizer
316 // (e.g., #stride-one accesses)
317 isl_union_map *OldSchedule = S.getSchedule();
318 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
319 isl_union_map_free(OldSchedule);
320 return changed;
321}
322
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000323namespace {
324class IslScheduleOptimizer : public ScopPass {
325public:
326 static char ID;
327 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
328
329 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
330
Johannes Doerfert45be6442015-09-27 15:43:29 +0000331 /// @brief Optimize the schedule of the SCoP @p S.
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000332 bool runOnScop(Scop &S) override;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000333
Johannes Doerfert45be6442015-09-27 15:43:29 +0000334 /// @brief Print the new schedule for the SCoP @p S.
335 void printScop(raw_ostream &OS, Scop &S) const override;
336
337 /// @brief Register all analyses and transformation required.
338 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000339
Johannes Doerfert0f376302015-09-27 15:42:28 +0000340 /// @brief Release the internal memory.
341 void releaseMemory() override {
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000342 isl_schedule_free(LastSchedule);
343 LastSchedule = nullptr;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000344 }
Johannes Doerfert45be6442015-09-27 15:43:29 +0000345
346private:
347 isl_schedule *LastSchedule;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000348};
349}
350
351char IslScheduleOptimizer::ID = 0;
352
Tobias Grosser73600b82011-10-08 00:30:40 +0000353bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +0000354
355 // Skip empty SCoPs but still allow code generation as it will delete the
356 // loops present but not needed.
357 if (S.getSize() == 0) {
358 S.markAsOptimized();
359 return false;
360 }
361
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000362 const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000363
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000364 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +0000365 return false;
366
Tobias Grosser28781422012-10-16 07:29:19 +0000367 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000368 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +0000369
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000370 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000371 int ValidityKinds =
372 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000373 int ProximityKinds;
374
375 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000376 ProximityKinds =
377 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000378 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000379 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000380 else {
381 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000382 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000383 ProximityKinds =
384 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000385 }
386
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000387 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000388
Tobias Grosser98610ee2012-02-13 23:31:39 +0000389 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000390 return false;
391
Johannes Doerfert7e6424b2015-03-05 00:43:48 +0000392 isl_union_map *Validity = D.getDependences(ValidityKinds);
393 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +0000394
Tobias Grossera26db472012-01-30 19:38:43 +0000395 // Simplify the dependences by removing the constraints introduced by the
396 // domains. This can speed up the scheduling time significantly, as large
397 // constant coefficients will be removed from the dependences. The
398 // introduction of some additional dependences reduces the possible
399 // transformations, but in most cases, such transformation do not seem to be
400 // interesting anyway. In some cases this option may stop the scheduler to
401 // find any schedule.
402 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000403 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
404 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000405 Proximity =
406 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +0000407 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000408 } else if (SimplifyDeps != "no") {
409 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
410 "or 'no'. Falling back to default: 'yes'\n";
411 }
412
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000413 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +0000414 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
415 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
416 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000417
Michael Krusec59f22c2015-06-18 16:45:40 +0000418 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000419
420 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000421 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000422 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +0000423 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000424 } else {
425 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
426 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +0000427 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000428 }
429
Tobias Grosser95e860c2012-01-30 19:38:54 +0000430 int IslMaximizeBands;
431
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000432 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000433 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000434 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000435 IslMaximizeBands = 0;
436 } else {
437 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
438 " or 'no'. Falling back to default: 'yes'\n";
439 IslMaximizeBands = 1;
440 }
441
Michael Krusec59f22c2015-06-18 16:45:40 +0000442 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000443 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000444 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser92f54802012-02-20 08:41:47 +0000445 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
Tobias Grosser4f6bcef2015-03-31 07:52:36 +0000446 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000447
448 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +0000449
450 isl_schedule_constraints *ScheduleConstraints;
451 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
452 ScheduleConstraints =
453 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
454 ScheduleConstraints = isl_schedule_constraints_set_validity(
455 ScheduleConstraints, isl_union_map_copy(Validity));
456 ScheduleConstraints =
457 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +0000458 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +0000459 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000460 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
461
462 // In cases the scheduler is not able to optimize the code, we just do not
463 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000464 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000465 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000466
Tobias Grosser97d87452015-05-30 06:46:59 +0000467 DEBUG({
468 auto *P = isl_printer_to_str(S.getIslCtx());
469 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
470 P = isl_printer_print_schedule(P, Schedule);
471 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
472 isl_printer_free(P);
473 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000474
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000475 isl_schedule *NewSchedule = ScheduleTreeOptimizer::optimizeSchedule(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +0000476 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000477
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000478 if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewScheduleMap)) {
Tobias Grosser808cd692015-07-14 09:33:13 +0000479 isl_union_map_free(NewScheduleMap);
480 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000481 return false;
482 }
483
Tobias Grosser808cd692015-07-14 09:33:13 +0000484 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000485 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000486
Tobias Grosser808cd692015-07-14 09:33:13 +0000487 isl_union_map_free(NewScheduleMap);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000488 return false;
489}
490
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000491void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +0000492 isl_printer *p;
493 char *ScheduleStr;
494
495 OS << "Calculated schedule:\n";
496
497 if (!LastSchedule) {
498 OS << "n/a\n";
499 return;
500 }
501
502 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
503 p = isl_printer_print_schedule(p, LastSchedule);
504 ScheduleStr = isl_printer_get_str(p);
505 isl_printer_free(p);
506
507 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000508}
509
Tobias Grosser73600b82011-10-08 00:30:40 +0000510void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000511 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000512 AU.addRequired<DependenceInfo>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000513}
514
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000515Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +0000516 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000517}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000518
519INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
520 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000521INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000522INITIALIZE_PASS_DEPENDENCY(ScopInfo);
523INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
524 "Polly - Optimize schedule of SCoP", false, false)