1 //===- 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 
20 #include "polly/ScheduleOptimizer.h"
21 #include "isl/aff.h"
22 #include "isl/band.h"
23 #include "isl/constraint.h"
24 #include "isl/map.h"
25 #include "isl/options.h"
26 #include "isl/schedule.h"
27 #include "isl/space.h"
28 #include "polly/CodeGen/CodeGeneration.h"
29 #include "polly/Dependences.h"
30 #include "polly/LinkAllPasses.h"
31 #include "polly/Options.h"
32 #include "polly/ScopInfo.h"
33 #include "polly/Support/GICHelper.h"
34 #include "llvm/Support/Debug.h"
35 
36 using namespace llvm;
37 using namespace polly;
38 
39 #define DEBUG_TYPE "polly-opt-isl"
40 
41 namespace polly {
42 bool DisablePollyTiling;
43 }
44 static cl::opt<bool, true>
45     DisableTiling("polly-no-tiling",
46                   cl::desc("Disable tiling in the scheduler"),
47                   cl::location(polly::DisablePollyTiling), cl::init(false),
48                   cl::ZeroOrMore, cl::cat(PollyCategory));
49 
50 static cl::opt<std::string>
51     OptimizeDeps("polly-opt-optimize-only",
52                  cl::desc("Only a certain kind of dependences (all/raw)"),
53                  cl::Hidden, cl::init("all"), cl::ZeroOrMore,
54                  cl::cat(PollyCategory));
55 
56 static cl::opt<std::string>
57     SimplifyDeps("polly-opt-simplify-deps",
58                  cl::desc("Dependences should be simplified (yes/no)"),
59                  cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
60                  cl::cat(PollyCategory));
61 
62 static cl::opt<int> MaxConstantTerm(
63     "polly-opt-max-constant-term",
64     cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
65     cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
66 
67 static cl::opt<int> MaxCoefficient(
68     "polly-opt-max-coefficient",
69     cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
70     cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
71 
72 static cl::opt<std::string> FusionStrategy(
73     "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
74     cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
75 
76 static cl::opt<std::string>
77     MaximizeBandDepth("polly-opt-maximize-bands",
78                       cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
79                       cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
80 
81 static cl::opt<int> DefaultTileSize(
82     "polly-default-tile-size",
83     cl::desc("The default tile size (if not enough were provided by"
84              " --polly-tile-sizes)"),
85     cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
86 
87 static cl::list<int> TileSizes("polly-tile-sizes",
88                                cl::desc("A tile size"
89                                         " for each loop dimension, filled with"
90                                         " --polly-default-tile-size"),
91                                cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
92                                cl::cat(PollyCategory));
93 namespace {
94 
95 class IslScheduleOptimizer : public ScopPass {
96 public:
97   static char ID;
98   explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
99 
100   ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
101 
102   virtual bool runOnScop(Scop &S);
103   void printScop(llvm::raw_ostream &OS) const;
104   void getAnalysisUsage(AnalysisUsage &AU) const;
105 
106 private:
107   isl_schedule *LastSchedule;
108 
109   static void extendScattering(Scop &S, unsigned NewDimensions);
110 
111   /// @brief Create a map that describes a n-dimensonal tiling.
112   ///
113   /// getTileMap creates a map from a n-dimensional scattering space into an
114   /// 2*n-dimensional scattering space. The map describes a rectangular
115   /// tiling.
116   ///
117   /// Example:
118   ///   scheduleDimensions = 2, parameterDimensions = 1, TileSizes = <32, 64>
119   ///
120   ///   tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
121   ///                        t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
122   ///                        t1 % 64 = 0 and t1 <= s1 < t1 + 64}
123   ///
124   ///  Before tiling:
125   ///
126   ///  for (i = 0; i < N; i++)
127   ///    for (j = 0; j < M; j++)
128   ///	S(i,j)
129   ///
130   ///  After tiling:
131   ///
132   ///  for (t_i = 0; t_i < N; i+=32)
133   ///    for (t_j = 0; t_j < M; j+=64)
134   ///	for (i = t_i; i < min(t_i + 32, N); i++)  | Unknown that N % 32 = 0
135   ///	  for (j = t_j; j < t_j + 64; j++)        |   Known that M % 64 = 0
136   ///	    S(i,j)
137   ///
138   static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions);
139 
140   /// @brief Get the schedule for this band.
141   ///
142   /// Polly applies transformations like tiling on top of the isl calculated
143   /// value.  This can influence the number of scheduling dimension. The
144   /// number of schedule dimensions is returned in the parameter 'Dimension'.
145   static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions);
146 
147   /// @brief Create a map that pre-vectorizes one scheduling dimension.
148   ///
149   /// getPrevectorMap creates a map that maps each input dimension to the same
150   /// output dimension, except for the dimension DimToVectorize.
151   /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
152   /// point loop of DimToVectorize is moved to the innermost level.
153   ///
154   /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
155   ///
156   /// | Before transformation
157   /// |
158   /// | A[i,j] -> [i,j]
159   /// |
160   /// | for (i = 0; i < 128; i++)
161   /// |    for (j = 0; j < 128; j++)
162   /// |      A(i,j);
163   ///
164   ///   Prevector map:
165   ///   [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
166   ///
167   /// | After transformation:
168   /// |
169   /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
170   /// |
171   /// | for (it = 0; it < 128; it+=4)
172   /// |    for (j = 0; j < 128; j++)
173   /// |      for (ip = max(0,it); ip < min(128, it + 3); ip++)
174   /// |        A(ip,j);
175   ///
176   /// The goal of this transformation is to create a trivially vectorizable
177   /// loop.  This means a parallel loop at the innermost level that has a
178   /// constant number of iterations corresponding to the target vector width.
179   ///
180   /// This transformation creates a loop at the innermost level. The loop has
181   /// a constant number of iterations, if the number of loop iterations at
182   /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
183   /// currently constant and not yet target specific. This function does not
184   /// reason about parallelism.
185   static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
186                                   int ScheduleDimensions, int VectorWidth = 4);
187 
188   /// @brief Get the scheduling map for a list of bands.
189   ///
190   /// Walk recursively the forest of bands to combine the schedules of the
191   /// individual bands to the overall schedule. In case tiling is requested,
192   /// the individual bands are tiled.
193   static isl_union_map *getScheduleForBandList(isl_band_list *BandList);
194 
195   static isl_union_map *getScheduleMap(isl_schedule *Schedule);
196 
197   using llvm::Pass::doFinalization;
198 
199   virtual bool doFinalization() {
200     isl_schedule_free(LastSchedule);
201     LastSchedule = nullptr;
202     return true;
203   }
204 };
205 }
206 
207 char IslScheduleOptimizer::ID = 0;
208 
209 void IslScheduleOptimizer::extendScattering(Scop &S, unsigned NewDimensions) {
210   for (ScopStmt *Stmt : S) {
211     unsigned OldDimensions = Stmt->getNumScattering();
212     isl_space *Space;
213     isl_map *Map, *New;
214 
215     Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
216     Map = isl_map_universe(Space);
217 
218     for (unsigned i = 0; i < OldDimensions; i++)
219       Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
220 
221     for (unsigned i = OldDimensions; i < NewDimensions; i++)
222       Map = isl_map_fix_si(Map, isl_dim_out, i, 0);
223 
224     Map = isl_map_align_params(Map, S.getParamSpace());
225     New = isl_map_apply_range(Stmt->getScattering(), Map);
226     Stmt->setScattering(New);
227   }
228 }
229 
230 isl_basic_map *IslScheduleOptimizer::getTileMap(isl_ctx *ctx,
231                                                 int scheduleDimensions) {
232   // We construct
233   //
234   // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
235   //	                  s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 64 and
236   //	                  s1 = a1 * 64 and s1 = p1 and t1 <= p1 < t1 + 64}
237   //
238   // and project out the auxilary dimensions a0 and a1.
239   isl_space *Space =
240       isl_space_alloc(ctx, 0, scheduleDimensions, scheduleDimensions * 3);
241   isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
242 
243   isl_local_space *LocalSpace = isl_local_space_from_space(Space);
244 
245   for (int x = 0; x < scheduleDimensions; x++) {
246     int sX = x;
247     int tX = x;
248     int pX = scheduleDimensions + x;
249     int aX = 2 * scheduleDimensions + x;
250     int tileSize = (int)TileSizes.size() > x ? TileSizes[x] : DefaultTileSize;
251     assert(tileSize > 0 && "Invalid tile size");
252 
253     isl_constraint *c;
254 
255     // sX = aX * tileSize;
256     c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
257     isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
258     isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
259     tileMap = isl_basic_map_add_constraint(tileMap, c);
260 
261     // pX = sX;
262     c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
263     isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
264     isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
265     tileMap = isl_basic_map_add_constraint(tileMap, c);
266 
267     // tX <= pX
268     c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
269     isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
270     isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
271     tileMap = isl_basic_map_add_constraint(tileMap, c);
272 
273     // pX <= tX + (tileSize - 1)
274     c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
275     isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
276     isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
277     isl_constraint_set_constant_si(c, tileSize - 1);
278     tileMap = isl_basic_map_add_constraint(tileMap, c);
279   }
280 
281   // Project out auxilary dimensions.
282   //
283   // The auxilary dimensions are transformed into existentially quantified ones.
284   // This reduces the number of visible scattering dimensions and allows Cloog
285   // to produces better code.
286   tileMap = isl_basic_map_project_out(
287       tileMap, isl_dim_out, 2 * scheduleDimensions, scheduleDimensions);
288   isl_local_space_free(LocalSpace);
289   return tileMap;
290 }
291 
292 isl_union_map *IslScheduleOptimizer::getScheduleForBand(isl_band *Band,
293                                                         int *Dimensions) {
294   isl_union_map *PartialSchedule;
295   isl_ctx *ctx;
296   isl_space *Space;
297   isl_basic_map *TileMap;
298   isl_union_map *TileUMap;
299 
300   PartialSchedule = isl_band_get_partial_schedule(Band);
301   *Dimensions = isl_band_n_member(Band);
302 
303   if (DisableTiling)
304     return PartialSchedule;
305 
306   // It does not make any sense to tile a band with just one dimension.
307   if (*Dimensions == 1)
308     return PartialSchedule;
309 
310   ctx = isl_union_map_get_ctx(PartialSchedule);
311   Space = isl_union_map_get_space(PartialSchedule);
312 
313   TileMap = getTileMap(ctx, *Dimensions);
314   TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
315   TileUMap = isl_union_map_align_params(TileUMap, Space);
316   *Dimensions = 2 * *Dimensions;
317 
318   return isl_union_map_apply_range(PartialSchedule, TileUMap);
319 }
320 
321 isl_map *IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
322                                                int ScheduleDimensions,
323                                                int VectorWidth) {
324   isl_space *Space;
325   isl_local_space *LocalSpace, *LocalSpaceRange;
326   isl_set *Modulo;
327   isl_map *TilingMap;
328   isl_constraint *c;
329   isl_aff *Aff;
330   int PointDimension; /* ip */
331   int TileDimension;  /* it */
332   isl_val *VectorWidthMP;
333 
334   assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
335 
336   Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
337   TilingMap = isl_map_universe(isl_space_copy(Space));
338   LocalSpace = isl_local_space_from_space(Space);
339   PointDimension = ScheduleDimensions;
340   TileDimension = DimToVectorize;
341 
342   // Create an identity map for everything except DimToVectorize and map
343   // DimToVectorize to the point loop at the innermost dimension.
344   for (int i = 0; i < ScheduleDimensions; i++) {
345     c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
346     c = isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
347 
348     if (i == DimToVectorize)
349       c = isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
350     else
351       c = isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
352 
353     TilingMap = isl_map_add_constraint(TilingMap, c);
354   }
355 
356   // it % 'VectorWidth' = 0
357   LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
358   Aff = isl_aff_zero_on_domain(LocalSpaceRange);
359   Aff = isl_aff_set_constant_si(Aff, VectorWidth);
360   Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
361   VectorWidthMP = isl_val_int_from_si(ctx, VectorWidth);
362   Aff = isl_aff_mod_val(Aff, VectorWidthMP);
363   Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
364   TilingMap = isl_map_intersect_range(TilingMap, Modulo);
365 
366   // it <= ip
367   c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
368   isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
369   isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
370   TilingMap = isl_map_add_constraint(TilingMap, c);
371 
372   // ip <= it + ('VectorWidth' - 1)
373   c = isl_inequality_alloc(LocalSpace);
374   isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
375   isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
376   isl_constraint_set_constant_si(c, VectorWidth - 1);
377   TilingMap = isl_map_add_constraint(TilingMap, c);
378 
379   return TilingMap;
380 }
381 
382 isl_union_map *
383 IslScheduleOptimizer::getScheduleForBandList(isl_band_list *BandList) {
384   int NumBands;
385   isl_union_map *Schedule;
386   isl_ctx *ctx;
387 
388   ctx = isl_band_list_get_ctx(BandList);
389   NumBands = isl_band_list_n_band(BandList);
390   Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
391 
392   for (int i = 0; i < NumBands; i++) {
393     isl_band *Band;
394     isl_union_map *PartialSchedule;
395     int ScheduleDimensions;
396     isl_space *Space;
397 
398     Band = isl_band_list_get_band(BandList, i);
399     PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
400     Space = isl_union_map_get_space(PartialSchedule);
401 
402     if (isl_band_has_children(Band)) {
403       isl_band_list *Children;
404       isl_union_map *SuffixSchedule;
405 
406       Children = isl_band_get_children(Band);
407       SuffixSchedule = getScheduleForBandList(Children);
408       PartialSchedule =
409           isl_union_map_flat_range_product(PartialSchedule, SuffixSchedule);
410       isl_band_list_free(Children);
411     } else if (PollyVectorizerChoice != VECTORIZER_NONE) {
412       // In case we are at the innermost band, we try to prepare for
413       // vectorization. This means, we look for the innermost parallel loop
414       // and strip mine this loop to the innermost level using a strip-mine
415       // factor corresponding to the number of vector iterations.
416       int NumDims = isl_band_n_member(Band);
417       for (int j = NumDims - 1; j >= 0; j--) {
418         if (isl_band_member_is_coincident(Band, j)) {
419           isl_map *TileMap;
420           isl_union_map *TileUMap;
421 
422           TileMap = getPrevectorMap(ctx, ScheduleDimensions - NumDims + j,
423                                     ScheduleDimensions);
424           TileUMap = isl_union_map_from_map(TileMap);
425           TileUMap =
426               isl_union_map_align_params(TileUMap, isl_space_copy(Space));
427           PartialSchedule =
428               isl_union_map_apply_range(PartialSchedule, TileUMap);
429           break;
430         }
431       }
432     }
433 
434     Schedule = isl_union_map_union(Schedule, PartialSchedule);
435 
436     isl_band_free(Band);
437     isl_space_free(Space);
438   }
439 
440   return Schedule;
441 }
442 
443 isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) {
444   isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
445   isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
446   isl_band_list_free(BandList);
447   return ScheduleMap;
448 }
449 
450 bool IslScheduleOptimizer::runOnScop(Scop &S) {
451   Dependences *D = &getAnalysis<Dependences>();
452 
453   if (!D->hasValidDependences())
454     return false;
455 
456   isl_schedule_free(LastSchedule);
457   LastSchedule = nullptr;
458 
459   // Build input data.
460   int ValidityKinds =
461       Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
462   int ProximityKinds;
463 
464   if (OptimizeDeps == "all")
465     ProximityKinds =
466         Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
467   else if (OptimizeDeps == "raw")
468     ProximityKinds = Dependences::TYPE_RAW;
469   else {
470     errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
471            << " Falling back to optimizing all dependences.\n";
472     ProximityKinds =
473         Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
474   }
475 
476   isl_union_set *Domain = S.getDomains();
477 
478   if (!Domain)
479     return false;
480 
481   isl_union_map *Validity = D->getDependences(ValidityKinds);
482   isl_union_map *Proximity = D->getDependences(ProximityKinds);
483 
484   // Simplify the dependences by removing the constraints introduced by the
485   // domains. This can speed up the scheduling time significantly, as large
486   // constant coefficients will be removed from the dependences. The
487   // introduction of some additional dependences reduces the possible
488   // transformations, but in most cases, such transformation do not seem to be
489   // interesting anyway. In some cases this option may stop the scheduler to
490   // find any schedule.
491   if (SimplifyDeps == "yes") {
492     Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
493     Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
494     Proximity =
495         isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
496     Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
497   } else if (SimplifyDeps != "no") {
498     errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
499               "or 'no'. Falling back to default: 'yes'\n";
500   }
501 
502   DEBUG(dbgs() << "\n\nCompute schedule from: ");
503   DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
504   DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
505   DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
506 
507   int IslFusionStrategy;
508 
509   if (FusionStrategy == "max") {
510     IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
511   } else if (FusionStrategy == "min") {
512     IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
513   } else {
514     errs() << "warning: Unknown fusion strategy. Falling back to maximal "
515               "fusion.\n";
516     IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
517   }
518 
519   int IslMaximizeBands;
520 
521   if (MaximizeBandDepth == "yes") {
522     IslMaximizeBands = 1;
523   } else if (MaximizeBandDepth == "no") {
524     IslMaximizeBands = 0;
525   } else {
526     errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
527               " or 'no'. Falling back to default: 'yes'\n";
528     IslMaximizeBands = 1;
529   }
530 
531   isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
532   isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
533   isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
534   isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
535 
536   isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
537 
538   isl_schedule_constraints *ScheduleConstraints;
539   ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
540   ScheduleConstraints =
541       isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
542   ScheduleConstraints = isl_schedule_constraints_set_validity(
543       ScheduleConstraints, isl_union_map_copy(Validity));
544   ScheduleConstraints =
545       isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
546   isl_schedule *Schedule;
547   Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
548   isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
549 
550   // In cases the scheduler is not able to optimize the code, we just do not
551   // touch the schedule.
552   if (!Schedule)
553     return false;
554 
555   DEBUG(dbgs() << "Schedule := " << stringFromIslObj(Schedule) << ";\n");
556 
557   isl_union_map *ScheduleMap = getScheduleMap(Schedule);
558 
559   for (ScopStmt *Stmt : S) {
560     isl_map *StmtSchedule;
561     isl_set *Domain = Stmt->getDomain();
562     isl_union_map *StmtBand;
563     StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
564                                               isl_union_set_from_set(Domain));
565     if (isl_union_map_is_empty(StmtBand)) {
566       StmtSchedule = isl_map_from_domain(isl_set_empty(Stmt->getDomainSpace()));
567       isl_union_map_free(StmtBand);
568     } else {
569       assert(isl_union_map_n_map(StmtBand) == 1);
570       StmtSchedule = isl_map_from_union_map(StmtBand);
571     }
572 
573     Stmt->setScattering(StmtSchedule);
574   }
575 
576   isl_union_map_free(ScheduleMap);
577   LastSchedule = Schedule;
578 
579   unsigned MaxScatDims = 0;
580 
581   for (ScopStmt *Stmt : S)
582     MaxScatDims = std::max(Stmt->getNumScattering(), MaxScatDims);
583 
584   extendScattering(S, MaxScatDims);
585   return false;
586 }
587 
588 void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
589   isl_printer *p;
590   char *ScheduleStr;
591 
592   OS << "Calculated schedule:\n";
593 
594   if (!LastSchedule) {
595     OS << "n/a\n";
596     return;
597   }
598 
599   p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
600   p = isl_printer_print_schedule(p, LastSchedule);
601   ScheduleStr = isl_printer_get_str(p);
602   isl_printer_free(p);
603 
604   OS << ScheduleStr << "\n";
605 }
606 
607 void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
608   ScopPass::getAnalysisUsage(AU);
609   AU.addRequired<Dependences>();
610 }
611 
612 Pass *polly::createIslScheduleOptimizerPass() {
613   return new IslScheduleOptimizer();
614 }
615 
616 INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
617                       "Polly - Optimize schedule of SCoP", false, false);
618 INITIALIZE_PASS_DEPENDENCY(Dependences);
619 INITIALIZE_PASS_DEPENDENCY(ScopInfo);
620 INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
621                     "Polly - Optimize schedule of SCoP", false, false)
622