blob: 80ea97ce4435ab5dbc18dbcb32902274a9209ab3 [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>
51SimplifyDeps("polly-opt-simplify-deps",
52 cl::desc("Dependences should be simplified (yes/no)"),
53 cl::Hidden, cl::init("yes"));
54
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000055static cl::opt<std::string>
56FusionStrategy("polly-opt-fusion",
57 cl::desc("The fusion strategy to choose (min/max)"),
Tobias Grosser50ff31d2012-01-30 19:38:58 +000058 cl::Hidden, cl::init("min"));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000059
Tobias Grosser95e860c2012-01-30 19:38:54 +000060static cl::opt<std::string>
Tobias Grossera4ea90b2012-01-30 22:43:56 +000061MaximizeBandDepth("polly-opt-maximize-bands",
62 cl::desc("Maximize the band depth (yes/no)"),
Tobias Grosser95e860c2012-01-30 19:38:54 +000063 cl::Hidden, cl::init("yes"));
64
Tobias Grosser30aa24c2011-05-14 19:02:06 +000065namespace {
66
Tobias Grosser73600b82011-10-08 00:30:40 +000067 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000068
69 public:
70 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000071 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000072
73 virtual bool runOnScop(Scop &S);
74 void printScop(llvm::raw_ostream &OS) const;
75 void getAnalysisUsage(AnalysisUsage &AU) const;
76 };
77
78}
79
Tobias Grosser73600b82011-10-08 00:30:40 +000080char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000081
82static int getSingleMap(__isl_take isl_map *map, void *user) {
83 isl_map **singleMap = (isl_map **) user;
84 *singleMap = map;
85
86 return 0;
87}
88
Tobias Grossercf3942d2011-10-06 00:04:05 +000089static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000090 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +000091 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000092
Tobias Grossercf3942d2011-10-06 00:04:05 +000093 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +000094 continue;
95
Tobias Grossercf3942d2011-10-06 00:04:05 +000096 unsigned OldDimensions = Stmt->getNumScattering();
97 isl_space *Space;
98 isl_basic_map *ChangeScattering;
99
100 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
101 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +0000102 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000103
Tobias Grossercf3942d2011-10-06 00:04:05 +0000104 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000105 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000106 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
107 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000108 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000109 }
110
Tobias Grossercf3942d2011-10-06 00:04:05 +0000111 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000112 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000113 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000114 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000115 }
116
Tobias Grossercf3942d2011-10-06 00:04:05 +0000117 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000118
Tobias Grossercf3942d2011-10-06 00:04:05 +0000119 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
120 S.getParamSpace());
121 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
122 ChangeScatteringMap);
123 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000124 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000125 }
126}
127
Tobias Grosserde68cc92011-06-30 20:01:02 +0000128// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000129//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000130// getTileMap creates a map from a n-dimensional scattering space into an
131// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000132//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000133// Example:
134// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
135//
136// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
137// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
138// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
139//
140// Before tiling:
141//
142// for (i = 0; i < N; i++)
143// for (j = 0; j < M; j++)
144// S(i,j)
145//
146// After tiling:
147//
148// for (t_i = 0; t_i < N; i+=32)
149// for (t_j = 0; t_j < M; j+=32)
150// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
151// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
152// S(i,j)
153//
154static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000155 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000156 // We construct
157 //
158 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
159 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
160 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
161 //
162 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000163 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
164 scheduleDimensions * 3);
165 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
166
167 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000168
Tobias Grosserde68cc92011-06-30 20:01:02 +0000169 for (int x = 0; x < scheduleDimensions; x++) {
170 int sX = x;
171 int tX = x;
172 int pX = scheduleDimensions + x;
173 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000174
Tobias Grosserde68cc92011-06-30 20:01:02 +0000175 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000176
Tobias Grosserde68cc92011-06-30 20:01:02 +0000177 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000178 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000179 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
180 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
181 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000182
Tobias Grosserde68cc92011-06-30 20:01:02 +0000183 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000184 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000185 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
186 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
187 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000188
Tobias Grosserde68cc92011-06-30 20:01:02 +0000189 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000190 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000191 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
192 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
193 tileMap = isl_basic_map_add_constraint(tileMap, c);
194
195 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000196 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000197 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
198 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000199 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000200 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000201 }
202
Tobias Grosserde68cc92011-06-30 20:01:02 +0000203 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000204 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000205 // The auxilary dimensions are transformed into existentially quantified ones.
206 // This reduces the number of visible scattering dimensions and allows Cloog
207 // to produces better code.
208 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
209 2 * scheduleDimensions,
210 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000211 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000212 return tileMap;
213}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000214
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000215// getScheduleForBand - Get the schedule for this band.
216//
Tobias Grosserb6033392011-12-08 13:02:58 +0000217// Polly applies transformations like tiling on top of the isl calculated value.
218// This can influence the number of scheduling dimension. The number of
219// schedule dimensions is returned in the parameter 'Dimension'.
220isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000221 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000222 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000223 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000224 isl_basic_map *TileMap;
225 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000226
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000227 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000228 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000229
Tobias Grosser79b30202011-11-17 12:56:00 +0000230 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000231 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000232
Tobias Grosserb6033392011-12-08 13:02:58 +0000233 // It does not make any sense to tile a band with just one dimension.
234 if (*Dimensions == 1)
235 return PartialSchedule;
236
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000237 ctx = isl_union_map_get_ctx(PartialSchedule);
238 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000239
Tobias Grosserb6033392011-12-08 13:02:58 +0000240 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000241 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
242 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000243 *Dimensions = 2 * *Dimensions;
244
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000245 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000246}
247
Tobias Grosser2493e922011-12-07 07:42:57 +0000248// Create a map that pre-vectorizes one scheduling dimension.
249//
250// getPrevectorMap creates a map that maps each input dimension to the same
251// output dimension, except for the dimension DimToVectorize. DimToVectorize is
252// strip mined by 'VectorWidth' and the newly created point loop of
253// DimToVectorize is moved to the innermost level.
254//
255// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
256//
257// | Before transformation
258// |
259// | A[i,j] -> [i,j]
260// |
261// | for (i = 0; i < 128; i++)
262// | for (j = 0; j < 128; j++)
263// | A(i,j);
264//
265// Prevector map:
266// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
267//
268// | After transformation:
269// |
270// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
271// |
272// | for (it = 0; it < 128; it+=4)
273// | for (j = 0; j < 128; j++)
274// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
275// | A(ip,j);
276//
277// The goal of this transformation is to create a trivially vectorizable loop.
278// This means a parallel loop at the innermost level that has a constant number
279// of iterations corresponding to the target vector width.
280//
281// This transformation creates a loop at the innermost level. The loop has a
282// constant number of iterations, if the number of loop iterations at
283// DimToVectorize can be devided by VectorWidth. The default VectorWidth is
284// currently constant and not yet target specific. This function does not reason
285// about parallelism.
286static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
287 int ScheduleDimensions,
288 int VectorWidth = 4) {
289 isl_space *Space;
290 isl_local_space *LocalSpace, *LocalSpaceRange;
291 isl_set *Modulo;
292 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000293 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000294 isl_aff *Aff;
295 int PointDimension; /* ip */
296 int TileDimension; /* it */
297 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000298
Tobias Grosser2493e922011-12-07 07:42:57 +0000299 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000300
Tobias Grosser2493e922011-12-07 07:42:57 +0000301 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
302 TilingMap = isl_map_universe(isl_space_copy(Space));
303 LocalSpace = isl_local_space_from_space(Space);
304 PointDimension = ScheduleDimensions;
305 TileDimension = DimToVectorize;
306
307 // Create an identity map for everything except DimToVectorize and map
308 // DimToVectorize to the point loop at the innermost dimension.
309 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000310 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000311 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000312
313 if (i == DimToVectorize)
314 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
315 else
316 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
317
318 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000319 }
320
Tobias Grosser2493e922011-12-07 07:42:57 +0000321 // it % 'VectorWidth' = 0
322 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
323 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
324 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
325 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
326 isl_int_init(VectorWidthMP);
327 isl_int_set_si(VectorWidthMP, VectorWidth);
328 Aff = isl_aff_mod(Aff, VectorWidthMP);
329 isl_int_clear(VectorWidthMP);
330 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
331 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000332
Tobias Grosser2493e922011-12-07 07:42:57 +0000333 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000334 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000335 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
336 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
337 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000338
Tobias Grosser2493e922011-12-07 07:42:57 +0000339 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000340 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000341 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
342 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
343 isl_constraint_set_constant_si(c, VectorWidth - 1);
344 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000345
Tobias Grosser2493e922011-12-07 07:42:57 +0000346 isl_map_dump(TilingMap);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000347
Tobias Grosser2493e922011-12-07 07:42:57 +0000348 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000349}
350
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000351// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000352//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000353// We walk recursively the forest of bands to combine the schedules of the
354// individual bands to the overall schedule. In case tiling is requested,
355// the individual bands are tiled.
356static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
357 int NumBands;
358 isl_union_map *Schedule;
359 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000360
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000361 ctx = isl_band_list_get_ctx(BandList);
362 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000363 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000364
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000365 for (int i = 0; i < NumBands; i++) {
366 isl_band *Band;
367 isl_union_map *PartialSchedule;
368 int ScheduleDimensions;
369 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000370
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000371 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000372 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000373 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000374
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000375 if (isl_band_has_children(Band)) {
376 isl_band_list *Children;
377 isl_union_map *SuffixSchedule;
378
379 Children = isl_band_get_children(Band);
380 SuffixSchedule = getScheduleForBandList(Children);
381 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
382 SuffixSchedule);
383 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000384 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000385 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
386 if (isl_band_member_is_zero_distance(Band, i)) {
387 isl_map *TileMap;
388 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000389
Tobias Grosserb6033392011-12-08 13:02:58 +0000390 TileMap = getPrevectorMap(ctx, i, ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000391 TileUMap = isl_union_map_from_map(TileMap);
392 TileUMap = isl_union_map_align_params(TileUMap,
393 isl_space_copy(Space));
394 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
395 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000396 break;
397 }
398 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000399 }
400
Tobias Grosser62872012011-11-17 12:56:04 +0000401 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000402
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000403 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000404 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000405 }
406
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000407 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000408}
409
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000410static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
411 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
412 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
413 isl_band_list_free(BandList);
414 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000415}
416
Tobias Grosser73600b82011-10-08 00:30:40 +0000417bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000418 Dependences *D = &getAnalysis<Dependences>();
419
420 // Build input data.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000421 int DependencyKinds = Dependences::TYPE_RAW
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000422 | Dependences::TYPE_WAR
423 | Dependences::TYPE_WAW;
424
Tobias Grosser98610ee2012-02-13 23:31:39 +0000425 isl_union_map *Dependences = D->getDependences(DependencyKinds);
426 isl_union_set *Domain = NULL;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000427
428 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
429 if ((*SI)->isFinalRead())
430 continue;
Tobias Grosser98610ee2012-02-13 23:31:39 +0000431 else if (!Domain)
432 Domain = isl_union_set_from_set((*SI)->getDomain());
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000433 else
Tobias Grosser98610ee2012-02-13 23:31:39 +0000434 Domain = isl_union_set_union(Domain,
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000435 isl_union_set_from_set((*SI)->getDomain()));
436
Tobias Grosser98610ee2012-02-13 23:31:39 +0000437 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000438 return false;
439
Tobias Grossera26db472012-01-30 19:38:43 +0000440 // Simplify the dependences by removing the constraints introduced by the
441 // domains. This can speed up the scheduling time significantly, as large
442 // constant coefficients will be removed from the dependences. The
443 // introduction of some additional dependences reduces the possible
444 // transformations, but in most cases, such transformation do not seem to be
445 // interesting anyway. In some cases this option may stop the scheduler to
446 // find any schedule.
447 if (SimplifyDeps == "yes") {
Tobias Grosser98610ee2012-02-13 23:31:39 +0000448 Dependences = isl_union_map_gist_domain(Dependences,
449 isl_union_set_copy(Domain));
450 Dependences = isl_union_map_gist_range(Dependences,
451 isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +0000452 } else if (SimplifyDeps != "no") {
453 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
454 "or 'no'. Falling back to default: 'yes'\n";
455 }
456
Tobias Grosser98610ee2012-02-13 23:31:39 +0000457 isl_schedule *Schedule;
458 isl_union_map *Proximity = isl_union_map_copy(Dependences);
459 isl_union_map *Validity = Dependences;
Tobias Grossera26db472012-01-30 19:38:43 +0000460
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000461 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000462 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
463 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000464 dbgs() << ";\n");
Tobias Grosser98610ee2012-02-13 23:31:39 +0000465 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000466 dbgs() << ";\n");
467
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000468 int IslFusionStrategy;
469
470 if (FusionStrategy == "max") {
471 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
472 } else if (FusionStrategy == "min") {
473 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
474 } else {
475 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
476 "fusion.\n";
477 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
478 }
479
Tobias Grosser95e860c2012-01-30 19:38:54 +0000480 int IslMaximizeBands;
481
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000482 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000483 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000484 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000485 IslMaximizeBands = 0;
486 } else {
487 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
488 " or 'no'. Falling back to default: 'yes'\n";
489 IslMaximizeBands = 1;
490 }
491
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000492 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000493 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000494
495 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grosser98610ee2012-02-13 23:31:39 +0000496 Schedule = isl_union_set_compute_schedule(Domain, Validity, Proximity);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000497 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
498
499 // In cases the scheduler is not able to optimize the code, we just do not
500 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +0000501 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +0000502 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000503
Tobias Grosser98610ee2012-02-13 23:31:39 +0000504 isl_union_map *ScheduleMap = getScheduleMap(Schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000505
Tobias Grosserde68cc92011-06-30 20:01:02 +0000506 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grosser98610ee2012-02-13 23:31:39 +0000507 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000508
Tobias Grosser98610ee2012-02-13 23:31:39 +0000509 if (Stmt->isFinalRead())
Tobias Grosserde68cc92011-06-30 20:01:02 +0000510 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000511
Tobias Grosser98610ee2012-02-13 23:31:39 +0000512 isl_set *Domain = Stmt->getDomain();
513 isl_union_map *StmtBand;
514 StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
515 isl_union_set_from_set(Domain));
516 isl_map *StmtSchedule;
517 isl_union_map_foreach_map(StmtBand, getSingleMap, &StmtSchedule);
518 Stmt->setScattering(StmtSchedule);
519 isl_union_map_free(StmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000520 }
521
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000522 isl_union_map_free(ScheduleMap);
Tobias Grosser98610ee2012-02-13 23:31:39 +0000523 isl_schedule_free(Schedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000524
Tobias Grosser98610ee2012-02-13 23:31:39 +0000525 unsigned MaxScatDims = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000526
527 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grosser98610ee2012-02-13 23:31:39 +0000528 MaxScatDims = std::max((*SI)->getNumScattering(), MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000529
Tobias Grosser98610ee2012-02-13 23:31:39 +0000530 extendScattering(S, MaxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000531 return false;
532}
533
Tobias Grosser73600b82011-10-08 00:30:40 +0000534void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000535}
536
Tobias Grosser73600b82011-10-08 00:30:40 +0000537void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000538 ScopPass::getAnalysisUsage(AU);
539 AU.addRequired<Dependences>();
540}
541
Tobias Grosser4dca4392011-11-22 19:40:19 +0000542INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000543 "Polly - Optimize schedule of SCoP", false, false)
544INITIALIZE_PASS_DEPENDENCY(Dependences)
545INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000546INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000547 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000548
Tobias Grosser73600b82011-10-08 00:30:40 +0000549Pass* polly::createIslScheduleOptimizerPass() {
550 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000551}