blob: 26812ed50133bea8ae24a57baa9c29a988b81e25 [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//
10// This pass the isl to calculate a schedule that is optimized for parallelism
11// and tileablility. The algorithm used in isl is an optimized version of the
12// algorithm described in following paper:
13//
14// U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan.
15// A Practical Automatic Polyhedral Parallelizer and Locality Optimizer.
16// In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language
17// Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008.
18//===----------------------------------------------------------------------===//
19
Tobias Grosser967239c2011-10-23 20:59:44 +000020#include "polly/ScheduleOptimizer.h"
21
Tobias Grosser67707b72011-10-23 20:59:40 +000022#include "polly/CodeGeneration.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000023#include "polly/Dependences.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000024#include "polly/LinkAllPasses.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000025#include "polly/ScopInfo.h"
26
Tobias Grosser2493e922011-12-07 07:42:57 +000027#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000028#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000029#include "isl/constraint.h"
30#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000031#include "isl/options.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000032#include "isl/schedule.h"
33#include "isl/space.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000034
Tobias Grosser4dca4392011-11-22 19:40:19 +000035#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000036#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000037#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000038
39using namespace llvm;
40using namespace polly;
41
Tobias Grosser967239c2011-10-23 20:59:44 +000042namespace polly {
43 bool DisablePollyTiling;
44}
45static cl::opt<bool, true>
Tobias Grosser353a2682011-10-23 20:59:26 +000046DisableTiling("polly-no-tiling",
Tobias Grosser967239c2011-10-23 20:59:44 +000047 cl::desc("Disable tiling in the scheduler"), cl::Hidden,
48 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000049
Tobias Grossera26db472012-01-30 19:38:43 +000050static cl::opt<std::string>
Tobias Grosser1deda292012-02-14 14:02:48 +000051OptimizeDeps("polly-opt-optimize-only",
52 cl::desc("Only a certain kind of dependences (all/raw)"),
53 cl::Hidden, cl::init("all"));
54
55static cl::opt<std::string>
Tobias Grossera26db472012-01-30 19:38:43 +000056SimplifyDeps("polly-opt-simplify-deps",
57 cl::desc("Dependences should be simplified (yes/no)"),
58 cl::Hidden, cl::init("yes"));
59
Tobias Grosser992e60c2012-02-20 08:41:15 +000060static cl::opt<int>
61MaxConstantTerm("polly-opt-max-constant-term",
62 cl::desc("The maximal constant term allowed (-1 is unlimited)"),
63 cl::Hidden, cl::init(20));
64
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000065static cl::opt<std::string>
66FusionStrategy("polly-opt-fusion",
67 cl::desc("The fusion strategy to choose (min/max)"),
Tobias Grosser50ff31d2012-01-30 19:38:58 +000068 cl::Hidden, cl::init("min"));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000069
Tobias Grosser95e860c2012-01-30 19:38:54 +000070static cl::opt<std::string>
Tobias Grossera4ea90b2012-01-30 22:43:56 +000071MaximizeBandDepth("polly-opt-maximize-bands",
72 cl::desc("Maximize the band depth (yes/no)"),
Tobias Grosser95e860c2012-01-30 19:38:54 +000073 cl::Hidden, cl::init("yes"));
74
Tobias Grosser30aa24c2011-05-14 19:02:06 +000075namespace {
76
Tobias Grosser73600b82011-10-08 00:30:40 +000077 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000078
79 public:
80 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000081 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000082
83 virtual bool runOnScop(Scop &S);
84 void printScop(llvm::raw_ostream &OS) const;
85 void getAnalysisUsage(AnalysisUsage &AU) const;
86 };
87
88}
89
Tobias Grosser73600b82011-10-08 00:30:40 +000090char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000091
92static int getSingleMap(__isl_take isl_map *map, void *user) {
93 isl_map **singleMap = (isl_map **) user;
94 *singleMap = map;
95
96 return 0;
97}
98
Tobias Grossercf3942d2011-10-06 00:04:05 +000099static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000100 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000101 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000102
Tobias Grossercf3942d2011-10-06 00:04:05 +0000103 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000104 continue;
105
Tobias Grossercf3942d2011-10-06 00:04:05 +0000106 unsigned OldDimensions = Stmt->getNumScattering();
107 isl_space *Space;
108 isl_basic_map *ChangeScattering;
109
110 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
111 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +0000112 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000113
Tobias Grossercf3942d2011-10-06 00:04:05 +0000114 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000115 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000116 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
117 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000118 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000119 }
120
Tobias Grossercf3942d2011-10-06 00:04:05 +0000121 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000122 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000123 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000124 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000125 }
126
Tobias Grossercf3942d2011-10-06 00:04:05 +0000127 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000128
Tobias Grossercf3942d2011-10-06 00:04:05 +0000129 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
130 S.getParamSpace());
131 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
132 ChangeScatteringMap);
133 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000134 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000135 }
136}
137
Tobias Grosserde68cc92011-06-30 20:01:02 +0000138// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000139//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000140// getTileMap creates a map from a n-dimensional scattering space into an
141// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000142//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000143// Example:
144// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
145//
146// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
147// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
148// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
149//
150// Before tiling:
151//
152// for (i = 0; i < N; i++)
153// for (j = 0; j < M; j++)
154// S(i,j)
155//
156// After tiling:
157//
158// for (t_i = 0; t_i < N; i+=32)
159// for (t_j = 0; t_j < M; j+=32)
160// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
161// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
162// S(i,j)
163//
164static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000165 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000166 // We construct
167 //
168 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
169 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
170 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
171 //
172 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000173 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
174 scheduleDimensions * 3);
175 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
176
177 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000178
Tobias Grosserde68cc92011-06-30 20:01:02 +0000179 for (int x = 0; x < scheduleDimensions; x++) {
180 int sX = x;
181 int tX = x;
182 int pX = scheduleDimensions + x;
183 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000184
Tobias Grosserde68cc92011-06-30 20:01:02 +0000185 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000186
Tobias Grosserde68cc92011-06-30 20:01:02 +0000187 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000188 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000189 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
190 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
191 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000192
Tobias Grosserde68cc92011-06-30 20:01:02 +0000193 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000194 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000195 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
196 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
197 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000198
Tobias Grosserde68cc92011-06-30 20:01:02 +0000199 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000200 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000201 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
202 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
203 tileMap = isl_basic_map_add_constraint(tileMap, c);
204
205 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000206 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000207 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
208 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000209 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000210 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000211 }
212
Tobias Grosserde68cc92011-06-30 20:01:02 +0000213 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000214 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000215 // The auxilary dimensions are transformed into existentially quantified ones.
216 // This reduces the number of visible scattering dimensions and allows Cloog
217 // to produces better code.
218 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
219 2 * scheduleDimensions,
220 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000221 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000222 return tileMap;
223}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000224
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000225// getScheduleForBand - Get the schedule for this band.
226//
Tobias Grosserb6033392011-12-08 13:02:58 +0000227// Polly applies transformations like tiling on top of the isl calculated value.
228// This can influence the number of scheduling dimension. The number of
229// schedule dimensions is returned in the parameter 'Dimension'.
230isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000231 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000232 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000233 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000234 isl_basic_map *TileMap;
235 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000236
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000237 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000238 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000239
Tobias Grosser79b30202011-11-17 12:56:00 +0000240 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000241 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000242
Tobias Grosserb6033392011-12-08 13:02:58 +0000243 // It does not make any sense to tile a band with just one dimension.
244 if (*Dimensions == 1)
245 return PartialSchedule;
246
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000247 ctx = isl_union_map_get_ctx(PartialSchedule);
248 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000249
Tobias Grosserb6033392011-12-08 13:02:58 +0000250 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000251 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
252 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000253 *Dimensions = 2 * *Dimensions;
254
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000255 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000256}
257
Tobias Grosser2493e922011-12-07 07:42:57 +0000258// Create a map that pre-vectorizes one scheduling dimension.
259//
260// getPrevectorMap creates a map that maps each input dimension to the same
261// output dimension, except for the dimension DimToVectorize. DimToVectorize is
262// strip mined by 'VectorWidth' and the newly created point loop of
263// DimToVectorize is moved to the innermost level.
264//
265// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
266//
267// | Before transformation
268// |
269// | A[i,j] -> [i,j]
270// |
271// | for (i = 0; i < 128; i++)
272// | for (j = 0; j < 128; j++)
273// | A(i,j);
274//
275// Prevector map:
276// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
277//
278// | After transformation:
279// |
280// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
281// |
282// | for (it = 0; it < 128; it+=4)
283// | for (j = 0; j < 128; j++)
284// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
285// | A(ip,j);
286//
287// The goal of this transformation is to create a trivially vectorizable loop.
288// This means a parallel loop at the innermost level that has a constant number
289// of iterations corresponding to the target vector width.
290//
291// This transformation creates a loop at the innermost level. The loop has a
292// constant number of iterations, if the number of loop iterations at
293// DimToVectorize can be devided by VectorWidth. The default VectorWidth is
294// currently constant and not yet target specific. This function does not reason
295// about parallelism.
296static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
297 int ScheduleDimensions,
298 int VectorWidth = 4) {
299 isl_space *Space;
300 isl_local_space *LocalSpace, *LocalSpaceRange;
301 isl_set *Modulo;
302 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000303 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000304 isl_aff *Aff;
305 int PointDimension; /* ip */
306 int TileDimension; /* it */
307 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000308
Tobias Grosser2493e922011-12-07 07:42:57 +0000309 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000310
Tobias Grosser2493e922011-12-07 07:42:57 +0000311 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
312 TilingMap = isl_map_universe(isl_space_copy(Space));
313 LocalSpace = isl_local_space_from_space(Space);
314 PointDimension = ScheduleDimensions;
315 TileDimension = DimToVectorize;
316
317 // Create an identity map for everything except DimToVectorize and map
318 // DimToVectorize to the point loop at the innermost dimension.
319 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000320 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000321 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000322
323 if (i == DimToVectorize)
324 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
325 else
326 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
327
328 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000329 }
330
Tobias Grosser2493e922011-12-07 07:42:57 +0000331 // it % 'VectorWidth' = 0
332 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
333 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
334 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
335 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
336 isl_int_init(VectorWidthMP);
337 isl_int_set_si(VectorWidthMP, VectorWidth);
338 Aff = isl_aff_mod(Aff, VectorWidthMP);
339 isl_int_clear(VectorWidthMP);
340 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
341 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000342
Tobias Grosser2493e922011-12-07 07:42:57 +0000343 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000344 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000345 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
346 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
347 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000348
Tobias Grosser2493e922011-12-07 07:42:57 +0000349 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000350 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000351 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
352 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
353 isl_constraint_set_constant_si(c, VectorWidth - 1);
354 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000355
Tobias Grosser2493e922011-12-07 07:42:57 +0000356 isl_map_dump(TilingMap);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000357
Tobias Grosser2493e922011-12-07 07:42:57 +0000358 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000359}
360
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000361// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000362//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000363// We walk recursively the forest of bands to combine the schedules of the
364// individual bands to the overall schedule. In case tiling is requested,
365// the individual bands are tiled.
366static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
367 int NumBands;
368 isl_union_map *Schedule;
369 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000370
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000371 ctx = isl_band_list_get_ctx(BandList);
372 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000373 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000374
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000375 for (int i = 0; i < NumBands; i++) {
376 isl_band *Band;
377 isl_union_map *PartialSchedule;
378 int ScheduleDimensions;
379 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000380
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000381 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000382 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000383 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000384
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000385 if (isl_band_has_children(Band)) {
386 isl_band_list *Children;
387 isl_union_map *SuffixSchedule;
388
389 Children = isl_band_get_children(Band);
390 SuffixSchedule = getScheduleForBandList(Children);
391 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
392 SuffixSchedule);
393 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000394 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000395 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
396 if (isl_band_member_is_zero_distance(Band, i)) {
397 isl_map *TileMap;
398 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000399
Tobias Grosserb6033392011-12-08 13:02:58 +0000400 TileMap = getPrevectorMap(ctx, i, ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000401 TileUMap = isl_union_map_from_map(TileMap);
402 TileUMap = isl_union_map_align_params(TileUMap,
403 isl_space_copy(Space));
404 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
405 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000406 break;
407 }
408 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000409 }
410
Tobias Grosser62872012011-11-17 12:56:04 +0000411 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000412
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000413 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000414 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000415 }
416
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000417 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000418}
419
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000420static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
421 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
422 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
423 isl_band_list_free(BandList);
424 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000425}
426
Tobias Grosser73600b82011-10-08 00:30:40 +0000427bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000428 Dependences *D = &getAnalysis<Dependences>();
429
430 // Build input data.
Tobias Grosser00383a72012-02-14 14:02:44 +0000431 int ValidityKinds = Dependences::TYPE_RAW | Dependences::TYPE_WAR
432 | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000433 int ProximityKinds;
434
435 if (OptimizeDeps == "all")
436 ProximityKinds = Dependences::TYPE_RAW | Dependences::TYPE_WAR
437 | Dependences::TYPE_WAW;
438 else if (OptimizeDeps == "raw")
Tobias Grosser1e03ad72012-02-15 09:58:42 +0000439 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +0000440 else {
441 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
442 << " Falling back to optimizing all dependences.\n";
443 ProximityKinds = Dependences::TYPE_RAW | Dependences::TYPE_WAR
444 | Dependences::TYPE_WAW;
445
446 }
447
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000448
Tobias Grosser00383a72012-02-14 14:02:44 +0000449 isl_union_map *Validity = D->getDependences(ValidityKinds);
450 isl_union_map *Proximity = D->getDependences(ProximityKinds);
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000451 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000452
Tobias Grosser98610ee2012-02-13 23:31:39 +0000453 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000454 return false;
455
Tobias Grossera26db472012-01-30 19:38:43 +0000456 // Simplify the dependences by removing the constraints introduced by the
457 // domains. This can speed up the scheduling time significantly, as large
458 // constant coefficients will be removed from the dependences. The
459 // introduction of some additional dependences reduces the possible
460 // transformations, but in most cases, such transformation do not seem to be
461 // interesting anyway. In some cases this option may stop the scheduler to
462 // find any schedule.
463 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +0000464 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
465 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
466 Proximity = isl_union_map_gist_domain(Proximity,
467 isl_union_set_copy(Domain));
468 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000469 } else if (SimplifyDeps != "no") {
470 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
471 "or 'no'. Falling back to default: 'yes'\n";
472 }
473
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000474 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000475 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
476 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000477 dbgs() << ";\n");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000478 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000479 dbgs() << ";\n");
480
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000481 int IslFusionStrategy;
482
483 if (FusionStrategy == "max") {
484 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
485 } else if (FusionStrategy == "min") {
486 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
487 } else {
488 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
489 "fusion.\n";
490 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
491 }
492
Tobias Grosser95e860c2012-01-30 19:38:54 +0000493 int IslMaximizeBands;
494
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000495 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000496 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000497 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000498 IslMaximizeBands = 0;
499 } else {
500 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
501 " or 'no'. Falling back to default: 'yes'\n";
502 IslMaximizeBands = 1;
503 }
504
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000505 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000506 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser992e60c2012-02-20 08:41:15 +0000507 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000508
509 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grosser00383a72012-02-14 14:02:44 +0000510 isl_schedule *Schedule;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000511 Schedule = isl_union_set_compute_schedule(Domain, Validity, Proximity);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000512 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
513
514 // In cases the scheduler is not able to optimize the code, we just do not
515 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000516 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000517 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000518
Tobias Grosser4d63b9d2012-02-20 08:41:21 +0000519 DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule);
520 dbgs() << ";\n");
521
Tobias Grosser98610ee2012-02-13 23:31:39 +0000522 isl_union_map *ScheduleMap = getScheduleMap(Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000523
Tobias Grosserde68cc92011-06-30 20:01:02 +0000524 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grosser98610ee2012-02-13 23:31:39 +0000525 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000526
Tobias Grosser98610ee2012-02-13 23:31:39 +0000527 if (Stmt->isFinalRead())
Tobias Grosserde68cc92011-06-30 20:01:02 +0000528 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000529
Tobias Grosser98610ee2012-02-13 23:31:39 +0000530 isl_set *Domain = Stmt->getDomain();
531 isl_union_map *StmtBand;
532 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
533 isl_union_set_from_set(Domain));
534 isl_map *StmtSchedule;
535 isl_union_map_foreach_map(StmtBand, getSingleMap, &StmtSchedule);
536 Stmt->setScattering(StmtSchedule);
537 isl_union_map_free(StmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000538 }
539
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000540 isl_union_map_free(ScheduleMap);
Tobias Grosser98610ee2012-02-13 23:31:39 +0000541 isl_schedule_free(Schedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000542
Tobias Grosser98610ee2012-02-13 23:31:39 +0000543 unsigned MaxScatDims = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000544
545 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grosser98610ee2012-02-13 23:31:39 +0000546 MaxScatDims = std::max((*SI)->getNumScattering(), MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000547
Tobias Grosser98610ee2012-02-13 23:31:39 +0000548 extendScattering(S, MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000549 return false;
550}
551
Tobias Grosser73600b82011-10-08 00:30:40 +0000552void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000553}
554
Tobias Grosser73600b82011-10-08 00:30:40 +0000555void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000556 ScopPass::getAnalysisUsage(AU);
557 AU.addRequired<Dependences>();
558}
559
Tobias Grosser4dca4392011-11-22 19:40:19 +0000560INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000561 "Polly - Optimize schedule of SCoP", false, false)
562INITIALIZE_PASS_DEPENDENCY(Dependences)
563INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000564INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000565 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000566
Tobias Grosser73600b82011-10-08 00:30:40 +0000567Pass* polly::createIslScheduleOptimizerPass() {
568 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000569}