blob: b19c91ed3c27efe602e8dcf9dfb31425d776a9a3 [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 Grosser30aa24c2011-05-14 19:02:06 +000022#include "polly/Cloog.h"
23#include "polly/LinkAllPasses.h"
Tobias Grosser67707b72011-10-23 20:59:40 +000024#include "polly/CodeGeneration.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000025#include "polly/Support/GICHelper.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000026#include "polly/Dependences.h"
27#include "polly/ScopInfo.h"
28
Tobias Grosser2493e922011-12-07 07:42:57 +000029#include "isl/aff.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000030#include "isl/space.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000031#include "isl/map.h"
32#include "isl/constraint.h"
33#include "isl/schedule.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000034#include "isl/band.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000035#include "isl/options.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000036
Tobias Grosser4dca4392011-11-22 19:40:19 +000037#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000038#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000039#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000040
41using namespace llvm;
42using namespace polly;
43
Tobias Grosser967239c2011-10-23 20:59:44 +000044namespace polly {
45 bool DisablePollyTiling;
46}
47static cl::opt<bool, true>
Tobias Grosser353a2682011-10-23 20:59:26 +000048DisableTiling("polly-no-tiling",
Tobias Grosser967239c2011-10-23 20:59:44 +000049 cl::desc("Disable tiling in the scheduler"), cl::Hidden,
50 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000051
Tobias Grossera26db472012-01-30 19:38:43 +000052static cl::opt<std::string>
53SimplifyDeps("polly-opt-simplify-deps",
54 cl::desc("Dependences should be simplified (yes/no)"),
55 cl::Hidden, cl::init("yes"));
56
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000057static cl::opt<std::string>
58FusionStrategy("polly-opt-fusion",
59 cl::desc("The fusion strategy to choose (min/max)"),
Tobias Grosser50ff31d2012-01-30 19:38:58 +000060 cl::Hidden, cl::init("min"));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +000061
Tobias Grosser95e860c2012-01-30 19:38:54 +000062static cl::opt<std::string>
Tobias Grossera4ea90b2012-01-30 22:43:56 +000063MaximizeBandDepth("polly-opt-maximize-bands",
64 cl::desc("Maximize the band depth (yes/no)"),
Tobias Grosser95e860c2012-01-30 19:38:54 +000065 cl::Hidden, cl::init("yes"));
66
Tobias Grosser30aa24c2011-05-14 19:02:06 +000067namespace {
68
Tobias Grosser73600b82011-10-08 00:30:40 +000069 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000070
71 public:
72 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000073 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000074
75 virtual bool runOnScop(Scop &S);
76 void printScop(llvm::raw_ostream &OS) const;
77 void getAnalysisUsage(AnalysisUsage &AU) const;
78 };
79
80}
81
Tobias Grosser73600b82011-10-08 00:30:40 +000082char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000083
84static int getSingleMap(__isl_take isl_map *map, void *user) {
85 isl_map **singleMap = (isl_map **) user;
86 *singleMap = map;
87
88 return 0;
89}
90
Tobias Grossercf3942d2011-10-06 00:04:05 +000091static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000092 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +000093 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000094
Tobias Grossercf3942d2011-10-06 00:04:05 +000095 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +000096 continue;
97
Tobias Grossercf3942d2011-10-06 00:04:05 +000098 unsigned OldDimensions = Stmt->getNumScattering();
99 isl_space *Space;
100 isl_basic_map *ChangeScattering;
101
102 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
103 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +0000104 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000105
Tobias Grossercf3942d2011-10-06 00:04:05 +0000106 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000107 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000108 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
109 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000110 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000111 }
112
Tobias Grossercf3942d2011-10-06 00:04:05 +0000113 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000114 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000115 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000116 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000117 }
118
Tobias Grossercf3942d2011-10-06 00:04:05 +0000119 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000120
Tobias Grossercf3942d2011-10-06 00:04:05 +0000121 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
122 S.getParamSpace());
123 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
124 ChangeScatteringMap);
125 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000126 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000127 }
128}
129
Tobias Grosserde68cc92011-06-30 20:01:02 +0000130// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000131//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000132// getTileMap creates a map from a n-dimensional scattering space into an
133// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000134//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000135// Example:
136// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
137//
138// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
139// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
140// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
141//
142// Before tiling:
143//
144// for (i = 0; i < N; i++)
145// for (j = 0; j < M; j++)
146// S(i,j)
147//
148// After tiling:
149//
150// for (t_i = 0; t_i < N; i+=32)
151// for (t_j = 0; t_j < M; j+=32)
152// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
153// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
154// S(i,j)
155//
156static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000157 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000158 // We construct
159 //
160 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
161 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
162 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
163 //
164 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000165 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
166 scheduleDimensions * 3);
167 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
168
169 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000170
Tobias Grosserde68cc92011-06-30 20:01:02 +0000171 for (int x = 0; x < scheduleDimensions; x++) {
172 int sX = x;
173 int tX = x;
174 int pX = scheduleDimensions + x;
175 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000176
Tobias Grosserde68cc92011-06-30 20:01:02 +0000177 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000178
Tobias Grosserde68cc92011-06-30 20:01:02 +0000179 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000180 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000181 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
182 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
183 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000184
Tobias Grosserde68cc92011-06-30 20:01:02 +0000185 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000186 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000187 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
188 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
189 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000190
Tobias Grosserde68cc92011-06-30 20:01:02 +0000191 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000192 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000193 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
194 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
195 tileMap = isl_basic_map_add_constraint(tileMap, c);
196
197 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000198 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000199 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
200 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000201 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000202 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000203 }
204
Tobias Grosserde68cc92011-06-30 20:01:02 +0000205 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000206 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000207 // The auxilary dimensions are transformed into existentially quantified ones.
208 // This reduces the number of visible scattering dimensions and allows Cloog
209 // to produces better code.
210 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
211 2 * scheduleDimensions,
212 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000213 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000214 return tileMap;
215}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000216
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000217// getScheduleForBand - Get the schedule for this band.
218//
Tobias Grosserb6033392011-12-08 13:02:58 +0000219// Polly applies transformations like tiling on top of the isl calculated value.
220// This can influence the number of scheduling dimension. The number of
221// schedule dimensions is returned in the parameter 'Dimension'.
222isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000223 isl_union_map *PartialSchedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000224 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000225 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000226 isl_basic_map *TileMap;
227 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000228
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000229 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserb6033392011-12-08 13:02:58 +0000230 *Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000231
Tobias Grosser79b30202011-11-17 12:56:00 +0000232 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000233 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000234
Tobias Grosserb6033392011-12-08 13:02:58 +0000235 // It does not make any sense to tile a band with just one dimension.
236 if (*Dimensions == 1)
237 return PartialSchedule;
238
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000239 ctx = isl_union_map_get_ctx(PartialSchedule);
240 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000241
Tobias Grosserb6033392011-12-08 13:02:58 +0000242 TileMap = getTileMap(ctx, *Dimensions, Space);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000243 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
244 TileUMap = isl_union_map_align_params(TileUMap, Space);
Tobias Grosserb6033392011-12-08 13:02:58 +0000245 *Dimensions = 2 * *Dimensions;
246
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000247 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000248}
249
Tobias Grosser2493e922011-12-07 07:42:57 +0000250// Create a map that pre-vectorizes one scheduling dimension.
251//
252// getPrevectorMap creates a map that maps each input dimension to the same
253// output dimension, except for the dimension DimToVectorize. DimToVectorize is
254// strip mined by 'VectorWidth' and the newly created point loop of
255// DimToVectorize is moved to the innermost level.
256//
257// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
258//
259// | Before transformation
260// |
261// | A[i,j] -> [i,j]
262// |
263// | for (i = 0; i < 128; i++)
264// | for (j = 0; j < 128; j++)
265// | A(i,j);
266//
267// Prevector map:
268// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
269//
270// | After transformation:
271// |
272// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
273// |
274// | for (it = 0; it < 128; it+=4)
275// | for (j = 0; j < 128; j++)
276// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
277// | A(ip,j);
278//
279// The goal of this transformation is to create a trivially vectorizable loop.
280// This means a parallel loop at the innermost level that has a constant number
281// of iterations corresponding to the target vector width.
282//
283// This transformation creates a loop at the innermost level. The loop has a
284// constant number of iterations, if the number of loop iterations at
285// DimToVectorize can be devided by VectorWidth. The default VectorWidth is
286// currently constant and not yet target specific. This function does not reason
287// about parallelism.
288static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
289 int ScheduleDimensions,
290 int VectorWidth = 4) {
291 isl_space *Space;
292 isl_local_space *LocalSpace, *LocalSpaceRange;
293 isl_set *Modulo;
294 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000295 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000296 isl_aff *Aff;
297 int PointDimension; /* ip */
298 int TileDimension; /* it */
299 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000300
Tobias Grosser2493e922011-12-07 07:42:57 +0000301 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000302
Tobias Grosser2493e922011-12-07 07:42:57 +0000303 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
304 TilingMap = isl_map_universe(isl_space_copy(Space));
305 LocalSpace = isl_local_space_from_space(Space);
306 PointDimension = ScheduleDimensions;
307 TileDimension = DimToVectorize;
308
309 // Create an identity map for everything except DimToVectorize and map
310 // DimToVectorize to the point loop at the innermost dimension.
311 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000312 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000313 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000314
315 if (i == DimToVectorize)
316 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
317 else
318 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
319
320 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000321 }
322
Tobias Grosser2493e922011-12-07 07:42:57 +0000323 // it % 'VectorWidth' = 0
324 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
325 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
326 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
327 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
328 isl_int_init(VectorWidthMP);
329 isl_int_set_si(VectorWidthMP, VectorWidth);
330 Aff = isl_aff_mod(Aff, VectorWidthMP);
331 isl_int_clear(VectorWidthMP);
332 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
333 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000334
Tobias Grosser2493e922011-12-07 07:42:57 +0000335 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000336 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000337 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
338 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
339 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000340
Tobias Grosser2493e922011-12-07 07:42:57 +0000341 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000342 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000343 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
344 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
345 isl_constraint_set_constant_si(c, VectorWidth - 1);
346 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000347
Tobias Grosser2493e922011-12-07 07:42:57 +0000348 isl_map_dump(TilingMap);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000349
Tobias Grosser2493e922011-12-07 07:42:57 +0000350 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000351}
352
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000353// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000354//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000355// We walk recursively the forest of bands to combine the schedules of the
356// individual bands to the overall schedule. In case tiling is requested,
357// the individual bands are tiled.
358static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
359 int NumBands;
360 isl_union_map *Schedule;
361 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000362
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000363 ctx = isl_band_list_get_ctx(BandList);
364 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000365 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000366
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000367 for (int i = 0; i < NumBands; i++) {
368 isl_band *Band;
369 isl_union_map *PartialSchedule;
370 int ScheduleDimensions;
371 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000372
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000373 Band = isl_band_list_get_band(BandList, i);
Tobias Grosserb6033392011-12-08 13:02:58 +0000374 PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000375 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000376
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000377 if (isl_band_has_children(Band)) {
378 isl_band_list *Children;
379 isl_union_map *SuffixSchedule;
380
381 Children = isl_band_get_children(Band);
382 SuffixSchedule = getScheduleForBandList(Children);
383 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
384 SuffixSchedule);
385 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000386 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000387 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
388 if (isl_band_member_is_zero_distance(Band, i)) {
389 isl_map *TileMap;
390 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000391
Tobias Grosserb6033392011-12-08 13:02:58 +0000392 TileMap = getPrevectorMap(ctx, i, ScheduleDimensions);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000393 TileUMap = isl_union_map_from_map(TileMap);
394 TileUMap = isl_union_map_align_params(TileUMap,
395 isl_space_copy(Space));
396 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
397 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000398 break;
399 }
400 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000401 }
402
Tobias Grosser62872012011-11-17 12:56:04 +0000403 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000404
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000405 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000406 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000407 }
408
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000409 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000410}
411
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000412static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
413 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
414 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
415 isl_band_list_free(BandList);
416 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000417}
418
Tobias Grosser73600b82011-10-08 00:30:40 +0000419bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000420 Dependences *D = &getAnalysis<Dependences>();
421
422 // Build input data.
423 int dependencyKinds = Dependences::TYPE_RAW
424 | Dependences::TYPE_WAR
425 | Dependences::TYPE_WAW;
426
Tobias Grossera26db472012-01-30 19:38:43 +0000427 isl_union_map *dependences = D->getDependences(dependencyKinds);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000428 isl_union_set *domain = NULL;
429
430 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
431 if ((*SI)->isFinalRead())
432 continue;
433 else if (!domain)
434 domain = isl_union_set_from_set((*SI)->getDomain());
435 else
436 domain = isl_union_set_union(domain,
437 isl_union_set_from_set((*SI)->getDomain()));
438
439 if (!domain)
440 return false;
441
Tobias Grossera26db472012-01-30 19:38:43 +0000442 // Simplify the dependences by removing the constraints introduced by the
443 // domains. This can speed up the scheduling time significantly, as large
444 // constant coefficients will be removed from the dependences. The
445 // introduction of some additional dependences reduces the possible
446 // transformations, but in most cases, such transformation do not seem to be
447 // interesting anyway. In some cases this option may stop the scheduler to
448 // find any schedule.
449 if (SimplifyDeps == "yes") {
450 dependences = isl_union_map_gist_domain(dependences,
451 isl_union_set_copy(domain));
452 dependences = isl_union_map_gist_range(dependences,
453 isl_union_set_copy(domain));
454 } else if (SimplifyDeps != "no") {
455 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
456 "or 'no'. Falling back to default: 'yes'\n";
457 }
458
459 isl_schedule *schedule;
460 isl_union_map *proximity = isl_union_map_copy(dependences);
461 isl_union_map *validity = dependences;
462
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000463 DEBUG(dbgs() << "\n\nCompute schedule from: ");
464 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
465 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
466 dbgs() << ";\n");
467 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
468 dbgs() << ";\n");
469
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000470 int IslFusionStrategy;
471
472 if (FusionStrategy == "max") {
473 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
474 } else if (FusionStrategy == "min") {
475 IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
476 } else {
477 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
478 "fusion.\n";
479 IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
480 }
481
Tobias Grosser95e860c2012-01-30 19:38:54 +0000482 int IslMaximizeBands;
483
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000484 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000485 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +0000486 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +0000487 IslMaximizeBands = 0;
488 } else {
489 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
490 " or 'no'. Falling back to default: 'yes'\n";
491 IslMaximizeBands = 1;
492 }
493
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000494 isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
Tobias Grosser95e860c2012-01-30 19:38:54 +0000495 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000496
497 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000498 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
Tobias Grosser42152ff2012-01-30 19:38:47 +0000499 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
500
501 // In cases the scheduler is not able to optimize the code, we just do not
502 // touch the schedule.
503 if (!schedule)
504 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000505
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000506 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000507 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000508 DEBUG(dbgs() << "Individual bands: ");
509
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000510 isl_union_map *ScheduleMap = getScheduleMap(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000511
Tobias Grosserde68cc92011-06-30 20:01:02 +0000512 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
513 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000514
Tobias Grosserde68cc92011-06-30 20:01:02 +0000515 if (stmt->isFinalRead())
516 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000517
Tobias Grosserde68cc92011-06-30 20:01:02 +0000518 isl_set *domain = stmt->getDomain();
519 isl_union_map *stmtBand;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000520 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosserde68cc92011-06-30 20:01:02 +0000521 isl_union_set_from_set(domain));
522 isl_map *stmtSchedule;
523 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000524 stmt->setScattering(stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000525 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000526 }
527
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000528 isl_union_map_free(ScheduleMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000529 isl_schedule_free(schedule);
530
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000531 unsigned maxScatDims = 0;
532
533 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grossercf3942d2011-10-06 00:04:05 +0000534 maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000535
536 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000537 return false;
538}
539
Tobias Grosser73600b82011-10-08 00:30:40 +0000540void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000541}
542
Tobias Grosser73600b82011-10-08 00:30:40 +0000543void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000544 ScopPass::getAnalysisUsage(AU);
545 AU.addRequired<Dependences>();
546}
547
Tobias Grosser4dca4392011-11-22 19:40:19 +0000548INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000549 "Polly - Optimize schedule of SCoP", false, false)
550INITIALIZE_PASS_DEPENDENCY(Dependences)
551INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000552INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000553 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000554
Tobias Grosser73600b82011-10-08 00:30:40 +0000555Pass* polly::createIslScheduleOptimizerPass() {
556 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000557}