Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 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 | // |
Tobias Grosser | 234a482 | 2015-08-15 09:34:33 +0000 | [diff] [blame] | 10 | // This pass generates an entirey new schedule tree from the data dependences |
| 11 | // and iteration domains. The new schedule tree is computed in two steps: |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 12 | // |
Tobias Grosser | 234a482 | 2015-08-15 09:34:33 +0000 | [diff] [blame] | 13 | // 1) The isl scheduling optimizer is run |
| 14 | // |
| 15 | // The isl scheduling optimizer creates a new schedule tree that maximizes |
| 16 | // parallelism and tileability and minimizes data-dependence distances. The |
| 17 | // algorithm used is a modified version of the ``Pluto'' algorithm: |
| 18 | // |
| 19 | // U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan. |
| 20 | // A Practical Automatic Polyhedral Parallelizer and Locality Optimizer. |
| 21 | // In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language |
| 22 | // Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008. |
| 23 | // |
| 24 | // 2) A set of post-scheduling transformations is applied on the schedule tree. |
| 25 | // |
| 26 | // These optimizations include: |
| 27 | // |
| 28 | // - Tiling of the innermost tilable bands |
| 29 | // - Prevectorization - The coice of a possible outer loop that is strip-mined |
| 30 | // to the innermost level to enable inner-loop |
| 31 | // vectorization. |
| 32 | // - Some optimizations for spatial locality are also planned. |
| 33 | // |
| 34 | // For a detailed description of the schedule tree itself please see section 6 |
| 35 | // of: |
| 36 | // |
| 37 | // Polyhedral AST generation is more than scanning polyhedra |
| 38 | // Tobias Grosser, Sven Verdoolaege, Albert Cohen |
| 39 | // ACM Transations on Programming Languages and Systems (TOPLAS), |
| 40 | // 37(4), July 2015 |
| 41 | // http://www.grosser.es/#pub-polyhedral-AST-generation |
| 42 | // |
| 43 | // This publication also contains a detailed discussion of the different options |
| 44 | // for polyhedral loop unrolling, full/partial tile separation and other uses |
| 45 | // of the schedule tree. |
| 46 | // |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Tobias Grosser | 967239c | 2011-10-23 20:59:44 +0000 | [diff] [blame] | 49 | #include "polly/ScheduleOptimizer.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 50 | #include "polly/CodeGen/CodeGeneration.h" |
| 51 | #include "polly/DependenceInfo.h" |
| 52 | #include "polly/LinkAllPasses.h" |
| 53 | #include "polly/Options.h" |
| 54 | #include "polly/ScopInfo.h" |
| 55 | #include "polly/Support/GICHelper.h" |
| 56 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 57 | #include "isl/aff.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 58 | #include "isl/band.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 59 | #include "isl/constraint.h" |
| 60 | #include "isl/map.h" |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 61 | #include "isl/options.h" |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 62 | #include "isl/printer.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 63 | #include "isl/schedule.h" |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 64 | #include "isl/schedule_node.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 65 | #include "isl/space.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 66 | #include "isl/union_map.h" |
| 67 | #include "isl/union_set.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 68 | |
| 69 | using namespace llvm; |
| 70 | using namespace polly; |
| 71 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 72 | #define DEBUG_TYPE "polly-opt-isl" |
| 73 | |
Tobias Grosser | 161c908 | 2015-08-19 08:22:06 +0000 | [diff] [blame] | 74 | static cl::opt<bool> EnableTiling("polly-tiling", |
| 75 | cl::desc("Enable loop tiling"), |
| 76 | cl::init(true), cl::ZeroOrMore, |
| 77 | cl::cat(PollyCategory)); |
Tobias Grosser | 353a268 | 2011-10-23 20:59:26 +0000 | [diff] [blame] | 78 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 79 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 80 | OptimizeDeps("polly-opt-optimize-only", |
| 81 | cl::desc("Only a certain kind of dependences (all/raw)"), |
| 82 | cl::Hidden, cl::init("all"), cl::ZeroOrMore, |
| 83 | cl::cat(PollyCategory)); |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 84 | |
| 85 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 86 | SimplifyDeps("polly-opt-simplify-deps", |
| 87 | cl::desc("Dependences should be simplified (yes/no)"), |
| 88 | cl::Hidden, cl::init("yes"), cl::ZeroOrMore, |
| 89 | cl::cat(PollyCategory)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 90 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 91 | static cl::opt<int> MaxConstantTerm( |
| 92 | "polly-opt-max-constant-term", |
| 93 | cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden, |
| 94 | cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 95 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 96 | static cl::opt<int> MaxCoefficient( |
| 97 | "polly-opt-max-coefficient", |
| 98 | cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden, |
| 99 | cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 100 | |
| 101 | static cl::opt<std::string> FusionStrategy( |
| 102 | "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"), |
| 103 | cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 104 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 105 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 106 | MaximizeBandDepth("polly-opt-maximize-bands", |
| 107 | cl::desc("Maximize the band depth (yes/no)"), cl::Hidden, |
| 108 | cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 109 | |
Tobias Grosser | 07c1c2f | 2015-08-19 08:46:11 +0000 | [diff] [blame] | 110 | static cl::opt<int> PrevectorWidth( |
| 111 | "polly-prevect-width", |
| 112 | cl::desc( |
| 113 | "The number of loop iterations to strip-mine for pre-vectorization"), |
| 114 | cl::Hidden, cl::init(4), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 115 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 116 | static cl::opt<int> DefaultTileSize( |
| 117 | "polly-default-tile-size", |
| 118 | cl::desc("The default tile size (if not enough were provided by" |
| 119 | " --polly-tile-sizes)"), |
| 120 | cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Johannes Doerfert | c3958b2 | 2014-05-28 17:21:02 +0000 | [diff] [blame] | 121 | |
| 122 | static cl::list<int> TileSizes("polly-tile-sizes", |
| 123 | cl::desc("A tile size" |
| 124 | " for each loop dimension, filled with" |
| 125 | " --polly-default-tile-size"), |
| 126 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, |
| 127 | cl::cat(PollyCategory)); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 128 | namespace { |
| 129 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 130 | class IslScheduleOptimizer : public ScopPass { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 131 | public: |
| 132 | static char ID; |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 133 | explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; } |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 134 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 135 | ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); } |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 136 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 137 | bool runOnScop(Scop &S) override; |
| 138 | void printScop(raw_ostream &OS, Scop &S) const override; |
| 139 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 140 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 141 | private: |
| 142 | isl_schedule *LastSchedule; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 143 | |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 144 | /// @brief Decide if the @p NewSchedule is profitable for @p S. |
| 145 | /// |
| 146 | /// @param S The SCoP we optimize. |
| 147 | /// @param NewSchedule The new schedule we computed. |
| 148 | /// |
| 149 | /// @return True, if we believe @p NewSchedule is an improvement for @p S. |
| 150 | bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule); |
| 151 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 152 | /// @brief Pre-vectorizes one scheduling dimension of a schedule band. |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 153 | /// |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 154 | /// prevectSchedBand splits out the dimension DimToVectorize, tiles it and |
| 155 | /// sinks the resulting point loop. |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 156 | /// |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 157 | /// Example (DimToVectorize=0, VectorWidth=4): |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 158 | /// |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 159 | /// | Before transformation: |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 160 | /// | |
| 161 | /// | A[i,j] -> [i,j] |
| 162 | /// | |
| 163 | /// | for (i = 0; i < 128; i++) |
| 164 | /// | for (j = 0; j < 128; j++) |
| 165 | /// | A(i,j); |
| 166 | /// |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 167 | /// | After transformation: |
| 168 | /// | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 169 | /// | for (it = 0; it < 32; it+=1) |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 170 | /// | for (j = 0; j < 128; j++) |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 171 | /// | for (ip = 0; ip <= 3; ip++) |
| 172 | /// | A(4 * it + ip,j); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 173 | /// |
| 174 | /// The goal of this transformation is to create a trivially vectorizable |
| 175 | /// loop. This means a parallel loop at the innermost level that has a |
| 176 | /// constant number of iterations corresponding to the target vector width. |
| 177 | /// |
| 178 | /// This transformation creates a loop at the innermost level. The loop has |
| 179 | /// a constant number of iterations, if the number of loop iterations at |
| 180 | /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is |
| 181 | /// currently constant and not yet target specific. This function does not |
| 182 | /// reason about parallelism. |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 183 | static __isl_give isl_schedule_node * |
| 184 | prevectSchedBand(__isl_take isl_schedule_node *Node, unsigned DimToVectorize, |
Tobias Grosser | 07c1c2f | 2015-08-19 08:46:11 +0000 | [diff] [blame] | 185 | int VectorWidth); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 186 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 187 | /// @brief Apply additional optimizations on the bands in the schedule tree. |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 188 | /// |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 189 | /// We are looking for an innermost band node and apply the following |
| 190 | /// transformations: |
| 191 | /// |
| 192 | /// - Tile the band |
| 193 | /// - if the band is tileable |
| 194 | /// - if the band has more than one loop dimension |
| 195 | /// |
Tobias Grosser | 3b10c94 | 2015-07-25 12:28:56 +0000 | [diff] [blame] | 196 | /// - Prevectorize the schedule of the band (or the point loop in case of |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 197 | /// tiling). |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 198 | /// - if vectorization is enabled |
| 199 | /// |
| 200 | /// @param Node The schedule node to (possibly) optimize. |
| 201 | /// @param User A pointer to forward some use information (currently unused). |
| 202 | static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 203 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 204 | /// @brief Apply post-scheduling transformations. |
| 205 | /// |
| 206 | /// This function applies a set of additional local transformations on the |
| 207 | /// schedule tree as it computed by the isl scheduler. Local transformations |
| 208 | /// applied include: |
| 209 | /// |
| 210 | /// - Tiling |
| 211 | /// - Prevectorization |
| 212 | /// |
| 213 | /// @param Schedule The schedule object post-transformations will be applied |
| 214 | /// on. |
| 215 | /// @returns The transformed schedule. |
| 216 | static __isl_give isl_schedule * |
| 217 | addPostTransforms(__isl_take isl_schedule *Schedule); |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 218 | |
Tobias Grosser | 20532b8 | 2014-04-11 17:56:49 +0000 | [diff] [blame] | 219 | using llvm::Pass::doFinalization; |
| 220 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 221 | virtual bool doFinalization() override { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 222 | isl_schedule_free(LastSchedule); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 223 | LastSchedule = nullptr; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | }; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 229 | char IslScheduleOptimizer::ID = 0; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 230 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 231 | __isl_give isl_schedule_node * |
| 232 | IslScheduleOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node, |
| 233 | unsigned DimToVectorize, |
| 234 | int VectorWidth) { |
| 235 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 236 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 237 | auto Space = isl_schedule_node_band_get_space(Node); |
| 238 | auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set); |
| 239 | isl_space_free(Space); |
| 240 | assert(DimToVectorize < ScheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 241 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 242 | if (DimToVectorize > 0) { |
| 243 | Node = isl_schedule_node_band_split(Node, DimToVectorize); |
| 244 | Node = isl_schedule_node_child(Node, 0); |
| 245 | } |
| 246 | if (DimToVectorize < ScheduleDimensions - 1) |
| 247 | Node = isl_schedule_node_band_split(Node, 1); |
| 248 | Space = isl_schedule_node_band_get_space(Node); |
| 249 | auto Sizes = isl_multi_val_zero(Space); |
| 250 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 251 | Sizes = |
| 252 | isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth)); |
| 253 | Node = isl_schedule_node_band_tile(Node, Sizes); |
| 254 | Node = isl_schedule_node_child(Node, 0); |
| 255 | Node = isl_schedule_node_band_sink(Node); |
| 256 | Node = isl_schedule_node_child(Node, 0); |
| 257 | return Node; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Tobias Grosser | d891b54 | 2015-08-20 12:16:23 +0000 | [diff] [blame^] | 260 | __isl_give isl_schedule_node * |
| 261 | IslScheduleOptimizer::optimizeBand(__isl_take isl_schedule_node *Node, |
| 262 | void *User) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 263 | if (isl_schedule_node_get_type(Node) != isl_schedule_node_band) |
| 264 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 265 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 266 | if (isl_schedule_node_n_children(Node) != 1) |
| 267 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 268 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 269 | if (!isl_schedule_node_band_get_permutable(Node)) |
| 270 | return Node; |
Tobias Grosser | 44f19ac | 2011-07-05 22:15:53 +0000 | [diff] [blame] | 271 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 272 | auto Space = isl_schedule_node_band_get_space(Node); |
| 273 | auto Dims = isl_space_dim(Space, isl_dim_set); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 274 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 275 | if (Dims <= 1) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 276 | isl_space_free(Space); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 277 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 280 | auto Child = isl_schedule_node_get_child(Node, 0); |
| 281 | auto Type = isl_schedule_node_get_type(Child); |
| 282 | isl_schedule_node_free(Child); |
| 283 | |
| 284 | if (Type != isl_schedule_node_leaf) { |
| 285 | isl_space_free(Space); |
| 286 | return Node; |
| 287 | } |
| 288 | |
Tobias Grosser | 161c908 | 2015-08-19 08:22:06 +0000 | [diff] [blame] | 289 | if (EnableTiling) { |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 290 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 291 | auto Sizes = isl_multi_val_zero(isl_space_copy(Space)); |
| 292 | for (unsigned i = 0; i < Dims; i++) { |
| 293 | auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize; |
| 294 | Sizes = |
| 295 | isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize)); |
| 296 | } |
| 297 | Node = isl_schedule_node_band_tile(Node, Sizes); |
| 298 | Node = isl_schedule_node_child(Node, 0); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 301 | isl_space_free(Space); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 302 | |
| 303 | if (PollyVectorizerChoice == VECTORIZER_NONE) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 304 | return Node; |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 305 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 306 | for (int i = Dims - 1; i >= 0; i--) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 307 | if (isl_schedule_node_band_member_get_coincident(Node, i)) { |
Tobias Grosser | 07c1c2f | 2015-08-19 08:46:11 +0000 | [diff] [blame] | 308 | Node = IslScheduleOptimizer::prevectSchedBand(Node, i, PrevectorWidth); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 309 | break; |
| 310 | } |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 311 | |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 312 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 315 | __isl_give isl_schedule * |
| 316 | IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 317 | isl_schedule_node *Root = isl_schedule_get_root(Schedule); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 318 | isl_schedule_free(Schedule); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 319 | Root = isl_schedule_node_map_descendant_bottom_up( |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 320 | Root, IslScheduleOptimizer::optimizeBand, NULL); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 321 | auto S = isl_schedule_node_get_schedule(Root); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 322 | isl_schedule_node_free(Root); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 323 | return S; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 326 | bool IslScheduleOptimizer::isProfitableSchedule( |
| 327 | Scop &S, __isl_keep isl_union_map *NewSchedule) { |
| 328 | // To understand if the schedule has been optimized we check if the schedule |
| 329 | // has changed at all. |
| 330 | // TODO: We can improve this by tracking if any necessarily beneficial |
| 331 | // transformations have been performed. This can e.g. be tiling, loop |
| 332 | // interchange, or ...) We can track this either at the place where the |
| 333 | // transformation has been performed or, in case of automatic ILP based |
| 334 | // optimizations, by comparing (yet to be defined) performance metrics |
| 335 | // before/after the scheduling optimizer |
| 336 | // (e.g., #stride-one accesses) |
| 337 | isl_union_map *OldSchedule = S.getSchedule(); |
| 338 | bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule); |
| 339 | isl_union_map_free(OldSchedule); |
| 340 | return changed; |
| 341 | } |
| 342 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 343 | bool IslScheduleOptimizer::runOnScop(Scop &S) { |
Johannes Doerfert | 6f7921f | 2015-02-14 12:02:24 +0000 | [diff] [blame] | 344 | |
| 345 | // Skip empty SCoPs but still allow code generation as it will delete the |
| 346 | // loops present but not needed. |
| 347 | if (S.getSize() == 0) { |
| 348 | S.markAsOptimized(); |
| 349 | return false; |
| 350 | } |
| 351 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 352 | const Dependences &D = getAnalysis<DependenceInfo>().getDependences(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 353 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 354 | if (!D.hasValidDependences()) |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 355 | return false; |
| 356 | |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 357 | isl_schedule_free(LastSchedule); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 358 | LastSchedule = nullptr; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 359 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 360 | // Build input data. |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 361 | int ValidityKinds = |
| 362 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 363 | int ProximityKinds; |
| 364 | |
| 365 | if (OptimizeDeps == "all") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 366 | ProximityKinds = |
| 367 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 368 | else if (OptimizeDeps == "raw") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 369 | ProximityKinds = Dependences::TYPE_RAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 370 | else { |
| 371 | errs() << "Do not know how to optimize for '" << OptimizeDeps << "'" |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 372 | << " Falling back to optimizing all dependences.\n"; |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 373 | ProximityKinds = |
| 374 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 377 | isl_union_set *Domain = S.getDomains(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 378 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 379 | if (!Domain) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 380 | return false; |
| 381 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 382 | isl_union_map *Validity = D.getDependences(ValidityKinds); |
| 383 | isl_union_map *Proximity = D.getDependences(ProximityKinds); |
Tobias Grosser | 8a50702 | 2012-03-16 11:51:41 +0000 | [diff] [blame] | 384 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 385 | // Simplify the dependences by removing the constraints introduced by the |
| 386 | // domains. This can speed up the scheduling time significantly, as large |
| 387 | // constant coefficients will be removed from the dependences. The |
| 388 | // introduction of some additional dependences reduces the possible |
| 389 | // transformations, but in most cases, such transformation do not seem to be |
| 390 | // interesting anyway. In some cases this option may stop the scheduler to |
| 391 | // find any schedule. |
| 392 | if (SimplifyDeps == "yes") { |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 393 | Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain)); |
| 394 | Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain)); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 395 | Proximity = |
| 396 | isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 397 | Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 398 | } else if (SimplifyDeps != "no") { |
| 399 | errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' " |
| 400 | "or 'no'. Falling back to default: 'yes'\n"; |
| 401 | } |
| 402 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 403 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
Tobias Grosser | 01aea58 | 2014-10-22 23:16:28 +0000 | [diff] [blame] | 404 | DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n"); |
| 405 | DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n"); |
| 406 | DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n"); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 407 | |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 408 | unsigned IslSerializeSCCs; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 409 | |
| 410 | if (FusionStrategy == "max") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 411 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 412 | } else if (FusionStrategy == "min") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 413 | IslSerializeSCCs = 1; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 414 | } else { |
| 415 | errs() << "warning: Unknown fusion strategy. Falling back to maximal " |
| 416 | "fusion.\n"; |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 417 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 420 | int IslMaximizeBands; |
| 421 | |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 422 | if (MaximizeBandDepth == "yes") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 423 | IslMaximizeBands = 1; |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 424 | } else if (MaximizeBandDepth == "no") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 425 | IslMaximizeBands = 0; |
| 426 | } else { |
| 427 | errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'" |
| 428 | " or 'no'. Falling back to default: 'yes'\n"; |
| 429 | IslMaximizeBands = 1; |
| 430 | } |
| 431 | |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 432 | isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs); |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 433 | isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands); |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 434 | isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm); |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 435 | isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient); |
Tobias Grosser | 4f6bcef | 2015-03-31 07:52:36 +0000 | [diff] [blame] | 436 | isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 437 | |
| 438 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE); |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 439 | |
| 440 | isl_schedule_constraints *ScheduleConstraints; |
| 441 | ScheduleConstraints = isl_schedule_constraints_on_domain(Domain); |
| 442 | ScheduleConstraints = |
| 443 | isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity); |
| 444 | ScheduleConstraints = isl_schedule_constraints_set_validity( |
| 445 | ScheduleConstraints, isl_union_map_copy(Validity)); |
| 446 | ScheduleConstraints = |
| 447 | isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 448 | isl_schedule *Schedule; |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 449 | Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 450 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT); |
| 451 | |
| 452 | // In cases the scheduler is not able to optimize the code, we just do not |
| 453 | // touch the schedule. |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 454 | if (!Schedule) |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 455 | return false; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 456 | |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 457 | DEBUG({ |
| 458 | auto *P = isl_printer_to_str(S.getIslCtx()); |
| 459 | P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); |
| 460 | P = isl_printer_print_schedule(P, Schedule); |
| 461 | dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n"; |
| 462 | isl_printer_free(P); |
| 463 | }); |
Tobias Grosser | 4d63b9d | 2012-02-20 08:41:21 +0000 | [diff] [blame] | 464 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 465 | isl_schedule *NewSchedule = addPostTransforms(Schedule); |
| 466 | isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 467 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 468 | if (!isProfitableSchedule(S, NewScheduleMap)) { |
| 469 | isl_union_map_free(NewScheduleMap); |
| 470 | isl_schedule_free(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 471 | return false; |
| 472 | } |
| 473 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 474 | S.setScheduleTree(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 475 | S.markAsOptimized(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 476 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 477 | isl_union_map_free(NewScheduleMap); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | |
Johannes Doerfert | 3fe584d | 2015-03-01 18:40:25 +0000 | [diff] [blame] | 481 | void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const { |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 482 | isl_printer *p; |
| 483 | char *ScheduleStr; |
| 484 | |
| 485 | OS << "Calculated schedule:\n"; |
| 486 | |
| 487 | if (!LastSchedule) { |
| 488 | OS << "n/a\n"; |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule)); |
| 493 | p = isl_printer_print_schedule(p, LastSchedule); |
| 494 | ScheduleStr = isl_printer_get_str(p); |
| 495 | isl_printer_free(p); |
| 496 | |
| 497 | OS << ScheduleStr << "\n"; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 500 | void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 501 | ScopPass::getAnalysisUsage(AU); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 502 | AU.addRequired<DependenceInfo>(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 505 | Pass *polly::createIslScheduleOptimizerPass() { |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 506 | return new IslScheduleOptimizer(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 507 | } |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 508 | |
| 509 | INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", |
| 510 | "Polly - Optimize schedule of SCoP", false, false); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 511 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 512 | INITIALIZE_PASS_DEPENDENCY(ScopInfo); |
| 513 | INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", |
| 514 | "Polly - Optimize schedule of SCoP", false, false) |