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/schedule_node.h"
28 #include "isl/space.h"
29 #include "polly/CodeGen/CodeGeneration.h"
30 #include "polly/DependenceInfo.h"
31 #include "polly/LinkAllPasses.h"
32 #include "polly/Options.h"
33 #include "polly/ScopInfo.h"
34 #include "polly/Support/GICHelper.h"
35 #include "llvm/Support/Debug.h"
36 
37 using namespace llvm;
38 using namespace polly;
39 
40 #define DEBUG_TYPE "polly-opt-isl"
41 
42 namespace polly {
43 bool DisablePollyTiling;
44 }
45 static cl::opt<bool, true>
46     DisableTiling("polly-no-tiling",
47                   cl::desc("Disable tiling in the scheduler"),
48                   cl::location(polly::DisablePollyTiling), cl::init(false),
49                   cl::ZeroOrMore, cl::cat(PollyCategory));
50 
51 static cl::opt<std::string>
52     OptimizeDeps("polly-opt-optimize-only",
53                  cl::desc("Only a certain kind of dependences (all/raw)"),
54                  cl::Hidden, cl::init("all"), cl::ZeroOrMore,
55                  cl::cat(PollyCategory));
56 
57 static cl::opt<std::string>
58     SimplifyDeps("polly-opt-simplify-deps",
59                  cl::desc("Dependences should be simplified (yes/no)"),
60                  cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
61                  cl::cat(PollyCategory));
62 
63 static cl::opt<int> MaxConstantTerm(
64     "polly-opt-max-constant-term",
65     cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
66     cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
67 
68 static cl::opt<int> MaxCoefficient(
69     "polly-opt-max-coefficient",
70     cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
71     cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
72 
73 static cl::opt<std::string> FusionStrategy(
74     "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
75     cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
76 
77 static cl::opt<std::string>
78     MaximizeBandDepth("polly-opt-maximize-bands",
79                       cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
80                       cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
81 
82 static cl::opt<int> DefaultTileSize(
83     "polly-default-tile-size",
84     cl::desc("The default tile size (if not enough were provided by"
85              " --polly-tile-sizes)"),
86     cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
87 
88 static cl::list<int> TileSizes("polly-tile-sizes",
89                                cl::desc("A tile size"
90                                         " for each loop dimension, filled with"
91                                         " --polly-default-tile-size"),
92                                cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
93                                cl::cat(PollyCategory));
94 namespace {
95 
96 class IslScheduleOptimizer : public ScopPass {
97 public:
98   static char ID;
99   explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
100 
101   ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
102 
103   bool runOnScop(Scop &S) override;
104   void printScop(raw_ostream &OS, Scop &S) const override;
105   void getAnalysisUsage(AnalysisUsage &AU) const override;
106 
107 private:
108   isl_schedule *LastSchedule;
109 
110   /// @brief Decide if the @p NewSchedule is profitable for @p S.
111   ///
112   /// @param S           The SCoP we optimize.
113   /// @param NewSchedule The new schedule we computed.
114   ///
115   /// @return True, if we believe @p NewSchedule is an improvement for @p S.
116   bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule);
117 
118   /// @brief Create a map that pre-vectorizes one scheduling dimension.
119   ///
120   /// getPrevectorMap creates a map that maps each input dimension to the same
121   /// output dimension, except for the dimension DimToVectorize.
122   /// DimToVectorize is strip mined by 'VectorWidth' and the newly created
123   /// point loop of DimToVectorize is moved to the innermost level.
124   ///
125   /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
126   ///
127   /// | Before transformation
128   /// |
129   /// | A[i,j] -> [i,j]
130   /// |
131   /// | for (i = 0; i < 128; i++)
132   /// |    for (j = 0; j < 128; j++)
133   /// |      A(i,j);
134   ///
135   ///   Prevector map:
136   ///   [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
137   ///
138   /// | After transformation:
139   /// |
140   /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
141   /// |
142   /// | for (it = 0; it < 128; it+=4)
143   /// |    for (j = 0; j < 128; j++)
144   /// |      for (ip = max(0,it); ip < min(128, it + 3); ip++)
145   /// |        A(ip,j);
146   ///
147   /// The goal of this transformation is to create a trivially vectorizable
148   /// loop.  This means a parallel loop at the innermost level that has a
149   /// constant number of iterations corresponding to the target vector width.
150   ///
151   /// This transformation creates a loop at the innermost level. The loop has
152   /// a constant number of iterations, if the number of loop iterations at
153   /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
154   /// currently constant and not yet target specific. This function does not
155   /// reason about parallelism.
156   static __isl_give isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
157                                              int ScheduleDimensions,
158                                              int VectorWidth = 4);
159 
160   /// @brief Apply additional optimizations on the bands in the schedule tree.
161   ///
162   /// We are looking for an innermost band node and apply the following
163   /// transformations:
164   ///
165   ///  - Tile the band
166   ///      - if the band is tileable
167   ///      - if the band has more than one loop dimension
168   ///
169   ///  - Prevectorize the point loop of the tile
170   ///      - if vectorization is enabled
171   ///
172   /// @param Node The schedule node to (possibly) optimize.
173   /// @param User A pointer to forward some use information (currently unused).
174   static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
175 
176   static __isl_give isl_union_map *
177   getScheduleMap(__isl_keep isl_schedule *Schedule);
178 
179   using llvm::Pass::doFinalization;
180 
181   virtual bool doFinalization() override {
182     isl_schedule_free(LastSchedule);
183     LastSchedule = nullptr;
184     return true;
185   }
186 };
187 }
188 
189 char IslScheduleOptimizer::ID = 0;
190 
191 __isl_give isl_map *
192 IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
193                                       int ScheduleDimensions, int VectorWidth) {
194   isl_space *Space;
195   isl_local_space *LocalSpace, *LocalSpaceRange;
196   isl_set *Modulo;
197   isl_map *TilingMap;
198   isl_constraint *c;
199   isl_aff *Aff;
200   int PointDimension; /* ip */
201   int TileDimension;  /* it */
202   isl_val *VectorWidthMP;
203 
204   assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
205 
206   Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
207   TilingMap = isl_map_universe(isl_space_copy(Space));
208   LocalSpace = isl_local_space_from_space(Space);
209   PointDimension = ScheduleDimensions;
210   TileDimension = DimToVectorize;
211 
212   // Create an identity map for everything except DimToVectorize and map
213   // DimToVectorize to the point loop at the innermost dimension.
214   for (int i = 0; i < ScheduleDimensions; i++) {
215     c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
216     c = isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
217 
218     if (i == DimToVectorize)
219       c = isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
220     else
221       c = isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
222 
223     TilingMap = isl_map_add_constraint(TilingMap, c);
224   }
225 
226   // it % 'VectorWidth' = 0
227   LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
228   Aff = isl_aff_zero_on_domain(LocalSpaceRange);
229   Aff = isl_aff_set_constant_si(Aff, VectorWidth);
230   Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
231   VectorWidthMP = isl_val_int_from_si(ctx, VectorWidth);
232   Aff = isl_aff_mod_val(Aff, VectorWidthMP);
233   Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
234   TilingMap = isl_map_intersect_range(TilingMap, Modulo);
235 
236   // it <= ip
237   c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
238   isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
239   isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
240   TilingMap = isl_map_add_constraint(TilingMap, c);
241 
242   // ip <= it + ('VectorWidth' - 1)
243   c = isl_inequality_alloc(LocalSpace);
244   isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
245   isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
246   isl_constraint_set_constant_si(c, VectorWidth - 1);
247   TilingMap = isl_map_add_constraint(TilingMap, c);
248 
249   return TilingMap;
250 }
251 
252 isl_schedule_node *IslScheduleOptimizer::optimizeBand(isl_schedule_node *Node,
253                                                       void *User) {
254   if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
255     return Node;
256 
257   if (isl_schedule_node_n_children(Node) != 1)
258     return Node;
259 
260   if (!isl_schedule_node_band_get_permutable(Node))
261     return Node;
262 
263   auto Space = isl_schedule_node_band_get_space(Node);
264   auto Dims = isl_space_dim(Space, isl_dim_set);
265 
266   if (Dims <= 1) {
267     isl_space_free(Space);
268     return Node;
269   }
270 
271   auto Child = isl_schedule_node_get_child(Node, 0);
272   auto Type = isl_schedule_node_get_type(Child);
273   isl_schedule_node_free(Child);
274 
275   if (Type != isl_schedule_node_leaf) {
276     isl_space_free(Space);
277     return Node;
278   }
279 
280   auto Sizes = isl_multi_val_zero(Space);
281   auto Ctx = isl_schedule_node_get_ctx(Node);
282 
283   for (unsigned i = 0; i < Dims; i++) {
284     auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
285     Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
286   }
287 
288   auto Res = isl_schedule_node_band_tile(Node, Sizes);
289 
290   if (PollyVectorizerChoice == VECTORIZER_NONE)
291     return Res;
292 
293   Child = isl_schedule_node_get_child(Res, 0);
294   auto ChildSchedule = isl_schedule_node_band_get_partial_schedule(Child);
295 
296   for (int i = Dims - 1; i >= 0; i--) {
297     if (isl_schedule_node_band_member_get_coincident(Child, i)) {
298       auto TileMap = IslScheduleOptimizer::getPrevectorMap(Ctx, i, Dims);
299       auto TileUMap = isl_union_map_from_map(TileMap);
300       auto ChildSchedule2 = isl_union_map_apply_range(
301           isl_union_map_from_multi_union_pw_aff(ChildSchedule), TileUMap);
302       ChildSchedule = isl_multi_union_pw_aff_from_union_map(ChildSchedule2);
303       break;
304     }
305   }
306 
307   isl_schedule_node_free(Res);
308   Res = isl_schedule_node_delete(Child);
309   Res = isl_schedule_node_insert_partial_schedule(Res, ChildSchedule);
310   return Res;
311 }
312 
313 __isl_give isl_union_map *
314 IslScheduleOptimizer::getScheduleMap(__isl_keep isl_schedule *Schedule) {
315   isl_schedule_node *Root = isl_schedule_get_root(Schedule);
316   Root = isl_schedule_node_map_descendant(
317       Root, IslScheduleOptimizer::optimizeBand, NULL);
318   auto ScheduleMap = isl_schedule_node_get_subtree_schedule_union_map(Root);
319   ScheduleMap = isl_union_map_detect_equalities(ScheduleMap);
320   isl_schedule_node_free(Root);
321   return ScheduleMap;
322 }
323 
324 bool IslScheduleOptimizer::isProfitableSchedule(
325     Scop &S, __isl_keep isl_union_map *NewSchedule) {
326   // To understand if the schedule has been optimized we check if the schedule
327   // has changed at all.
328   // TODO: We can improve this by tracking if any necessarily beneficial
329   // transformations have been performed. This can e.g. be tiling, loop
330   // interchange, or ...) We can track this either at the place where the
331   // transformation has been performed or, in case of automatic ILP based
332   // optimizations, by comparing (yet to be defined) performance metrics
333   // before/after the scheduling optimizer
334   // (e.g., #stride-one accesses)
335   isl_union_map *OldSchedule = S.getSchedule();
336   bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
337   isl_union_map_free(OldSchedule);
338   return changed;
339 }
340 
341 bool IslScheduleOptimizer::runOnScop(Scop &S) {
342 
343   // Skip empty SCoPs but still allow code generation as it will delete the
344   // loops present but not needed.
345   if (S.getSize() == 0) {
346     S.markAsOptimized();
347     return false;
348   }
349 
350   const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
351 
352   if (!D.hasValidDependences())
353     return false;
354 
355   isl_schedule_free(LastSchedule);
356   LastSchedule = nullptr;
357 
358   // Build input data.
359   int ValidityKinds =
360       Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
361   int ProximityKinds;
362 
363   if (OptimizeDeps == "all")
364     ProximityKinds =
365         Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
366   else if (OptimizeDeps == "raw")
367     ProximityKinds = Dependences::TYPE_RAW;
368   else {
369     errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
370            << " Falling back to optimizing all dependences.\n";
371     ProximityKinds =
372         Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
373   }
374 
375   isl_union_set *Domain = S.getDomains();
376 
377   if (!Domain)
378     return false;
379 
380   isl_union_map *Validity = D.getDependences(ValidityKinds);
381   isl_union_map *Proximity = D.getDependences(ProximityKinds);
382 
383   // Simplify the dependences by removing the constraints introduced by the
384   // domains. This can speed up the scheduling time significantly, as large
385   // constant coefficients will be removed from the dependences. The
386   // introduction of some additional dependences reduces the possible
387   // transformations, but in most cases, such transformation do not seem to be
388   // interesting anyway. In some cases this option may stop the scheduler to
389   // find any schedule.
390   if (SimplifyDeps == "yes") {
391     Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
392     Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
393     Proximity =
394         isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
395     Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
396   } else if (SimplifyDeps != "no") {
397     errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
398               "or 'no'. Falling back to default: 'yes'\n";
399   }
400 
401   DEBUG(dbgs() << "\n\nCompute schedule from: ");
402   DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
403   DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
404   DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
405 
406   int IslFusionStrategy;
407 
408   if (FusionStrategy == "max") {
409     IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
410   } else if (FusionStrategy == "min") {
411     IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
412   } else {
413     errs() << "warning: Unknown fusion strategy. Falling back to maximal "
414               "fusion.\n";
415     IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
416   }
417 
418   int IslMaximizeBands;
419 
420   if (MaximizeBandDepth == "yes") {
421     IslMaximizeBands = 1;
422   } else if (MaximizeBandDepth == "no") {
423     IslMaximizeBands = 0;
424   } else {
425     errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
426               " or 'no'. Falling back to default: 'yes'\n";
427     IslMaximizeBands = 1;
428   }
429 
430   isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
431   isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
432   isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
433   isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
434 
435   isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
436 
437   isl_schedule_constraints *ScheduleConstraints;
438   ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
439   ScheduleConstraints =
440       isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
441   ScheduleConstraints = isl_schedule_constraints_set_validity(
442       ScheduleConstraints, isl_union_map_copy(Validity));
443   ScheduleConstraints =
444       isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
445   isl_schedule *Schedule;
446   Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
447   isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
448 
449   // In cases the scheduler is not able to optimize the code, we just do not
450   // touch the schedule.
451   if (!Schedule)
452     return false;
453 
454   DEBUG(dbgs() << "Schedule := " << stringFromIslObj(Schedule) << ";\n");
455 
456   isl_union_map *NewSchedule = getScheduleMap(Schedule);
457 
458   // Check if the optimizations performed were profitable, otherwise exit early.
459   if (!isProfitableSchedule(S, NewSchedule)) {
460     isl_schedule_free(Schedule);
461     isl_union_map_free(NewSchedule);
462     return false;
463   }
464 
465   S.markAsOptimized();
466 
467   for (ScopStmt *Stmt : S) {
468     isl_map *StmtSchedule;
469     isl_set *Domain = Stmt->getDomain();
470     isl_union_map *StmtBand;
471     StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(NewSchedule),
472                                               isl_union_set_from_set(Domain));
473     if (isl_union_map_is_empty(StmtBand)) {
474       StmtSchedule = isl_map_from_domain(isl_set_empty(Stmt->getDomainSpace()));
475       isl_union_map_free(StmtBand);
476     } else {
477       assert(isl_union_map_n_map(StmtBand) == 1);
478       StmtSchedule = isl_map_from_union_map(StmtBand);
479     }
480 
481     Stmt->setScattering(StmtSchedule);
482   }
483 
484   isl_schedule_free(Schedule);
485   isl_union_map_free(NewSchedule);
486   return false;
487 }
488 
489 void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
490   isl_printer *p;
491   char *ScheduleStr;
492 
493   OS << "Calculated schedule:\n";
494 
495   if (!LastSchedule) {
496     OS << "n/a\n";
497     return;
498   }
499 
500   p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
501   p = isl_printer_print_schedule(p, LastSchedule);
502   ScheduleStr = isl_printer_get_str(p);
503   isl_printer_free(p);
504 
505   OS << ScheduleStr << "\n";
506 }
507 
508 void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
509   ScopPass::getAnalysisUsage(AU);
510   AU.addRequired<DependenceInfo>();
511 }
512 
513 Pass *polly::createIslScheduleOptimizerPass() {
514   return new IslScheduleOptimizer();
515 }
516 
517 INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
518                       "Polly - Optimize schedule of SCoP", false, false);
519 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
520 INITIALIZE_PASS_DEPENDENCY(ScopInfo);
521 INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
522                     "Polly - Optimize schedule of SCoP", false, false)
523