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 | // |
| 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 Grosser | 967239c | 2011-10-23 20:59:44 +0000 | [diff] [blame] | 20 | #include "polly/ScheduleOptimizer.h" |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 21 | #include "isl/aff.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 22 | #include "isl/band.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 23 | #include "isl/constraint.h" |
| 24 | #include "isl/map.h" |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 25 | #include "isl/options.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 26 | #include "isl/schedule.h" |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 27 | #include "isl/schedule_node.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 28 | #include "isl/space.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 29 | #include "polly/CodeGen/CodeGeneration.h" |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 30 | #include "polly/DependenceInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 31 | #include "polly/LinkAllPasses.h" |
| 32 | #include "polly/Options.h" |
| 33 | #include "polly/ScopInfo.h" |
Tobias Grosser | 01aea58 | 2014-10-22 23:16:28 +0000 | [diff] [blame] | 34 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace polly; |
| 39 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "polly-opt-isl" |
| 41 | |
Tobias Grosser | 58032cb | 2013-06-23 01:29:29 +0000 | [diff] [blame] | 42 | namespace polly { |
| 43 | bool DisablePollyTiling; |
| 44 | } |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 45 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 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)); |
Tobias Grosser | 353a268 | 2011-10-23 20:59:26 +0000 | [diff] [blame] | 50 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 51 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 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)); |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 56 | |
| 57 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 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)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 62 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 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)); |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 67 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 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)); |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 76 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 77 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 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)); |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 81 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 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)); |
Johannes Doerfert | c3958b2 | 2014-05-28 17:21:02 +0000 | [diff] [blame] | 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)); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 94 | namespace { |
| 95 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 96 | class IslScheduleOptimizer : public ScopPass { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 97 | public: |
| 98 | static char ID; |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 99 | explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; } |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 100 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 101 | ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); } |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 102 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 103 | bool runOnScop(Scop &S) override; |
| 104 | void printScop(raw_ostream &OS, Scop &S) const override; |
| 105 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 106 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 107 | private: |
| 108 | isl_schedule *LastSchedule; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 109 | |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 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 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 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. |
Tobias Grosser | 442c6cc | 2015-03-19 07:43:35 +0000 | [diff] [blame] | 156 | static __isl_give isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize, |
| 157 | int ScheduleDimensions, |
| 158 | int VectorWidth = 4); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 159 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 160 | /// @brief Apply additional optimizations on the bands in the schedule tree. |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 161 | /// |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 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); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 175 | |
Tobias Grosser | 442c6cc | 2015-03-19 07:43:35 +0000 | [diff] [blame] | 176 | static __isl_give isl_union_map * |
| 177 | getScheduleMap(__isl_keep isl_schedule *Schedule); |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 178 | |
Tobias Grosser | 20532b8 | 2014-04-11 17:56:49 +0000 | [diff] [blame] | 179 | using llvm::Pass::doFinalization; |
| 180 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 181 | virtual bool doFinalization() override { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 182 | isl_schedule_free(LastSchedule); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 183 | LastSchedule = nullptr; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | }; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 189 | char IslScheduleOptimizer::ID = 0; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 190 | |
Tobias Grosser | 442c6cc | 2015-03-19 07:43:35 +0000 | [diff] [blame] | 191 | __isl_give isl_map * |
| 192 | IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize, |
| 193 | int ScheduleDimensions, int VectorWidth) { |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 194 | isl_space *Space; |
| 195 | isl_local_space *LocalSpace, *LocalSpaceRange; |
| 196 | isl_set *Modulo; |
| 197 | isl_map *TilingMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 198 | isl_constraint *c; |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 199 | isl_aff *Aff; |
| 200 | int PointDimension; /* ip */ |
| 201 | int TileDimension; /* it */ |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 202 | isl_val *VectorWidthMP; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 203 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 204 | assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 205 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 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++) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 215 | c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Johannes Doerfert | 495dd05 | 2014-08-20 17:15:34 +0000 | [diff] [blame] | 216 | c = isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 217 | |
| 218 | if (i == DimToVectorize) |
Johannes Doerfert | 495dd05 | 2014-08-20 17:15:34 +0000 | [diff] [blame] | 219 | c = isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 220 | else |
Johannes Doerfert | 495dd05 | 2014-08-20 17:15:34 +0000 | [diff] [blame] | 221 | c = isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 222 | |
| 223 | TilingMap = isl_map_add_constraint(TilingMap, c); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 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); |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 231 | VectorWidthMP = isl_val_int_from_si(ctx, VectorWidth); |
| 232 | Aff = isl_aff_mod_val(Aff, VectorWidthMP); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 233 | Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff)); |
| 234 | TilingMap = isl_map_intersect_range(TilingMap, Modulo); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 235 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 236 | // it <= ip |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 237 | c = isl_inequality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 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); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 241 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 242 | // ip <= it + ('VectorWidth' - 1) |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 243 | c = isl_inequality_alloc(LocalSpace); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 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); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 248 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 249 | return TilingMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 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; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 256 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 257 | if (isl_schedule_node_n_children(Node) != 1) |
| 258 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 259 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 260 | if (!isl_schedule_node_band_get_permutable(Node)) |
| 261 | return Node; |
Tobias Grosser | 44f19ac | 2011-07-05 22:15:53 +0000 | [diff] [blame] | 262 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 263 | auto Space = isl_schedule_node_band_get_space(Node); |
| 264 | auto Dims = isl_space_dim(Space, isl_dim_set); |
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 (Dims <= 1) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 267 | isl_space_free(Space); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 268 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 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 | |
Tobias Grosser | 02cf69a | 2015-04-05 21:52:21 +0000 | [diff] [blame] | 288 | isl_schedule_node *Res; |
| 289 | |
| 290 | if (DisableTiling) { |
| 291 | isl_multi_val_free(Sizes); |
| 292 | Res = Node; |
| 293 | } else { |
| 294 | Res = isl_schedule_node_band_tile(Node, Sizes); |
| 295 | } |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 296 | |
| 297 | if (PollyVectorizerChoice == VECTORIZER_NONE) |
| 298 | return Res; |
| 299 | |
| 300 | Child = isl_schedule_node_get_child(Res, 0); |
| 301 | auto ChildSchedule = isl_schedule_node_band_get_partial_schedule(Child); |
| 302 | |
| 303 | for (int i = Dims - 1; i >= 0; i--) { |
| 304 | if (isl_schedule_node_band_member_get_coincident(Child, i)) { |
| 305 | auto TileMap = IslScheduleOptimizer::getPrevectorMap(Ctx, i, Dims); |
| 306 | auto TileUMap = isl_union_map_from_map(TileMap); |
| 307 | auto ChildSchedule2 = isl_union_map_apply_range( |
| 308 | isl_union_map_from_multi_union_pw_aff(ChildSchedule), TileUMap); |
| 309 | ChildSchedule = isl_multi_union_pw_aff_from_union_map(ChildSchedule2); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | isl_schedule_node_free(Res); |
| 315 | Res = isl_schedule_node_delete(Child); |
| 316 | Res = isl_schedule_node_insert_partial_schedule(Res, ChildSchedule); |
| 317 | return Res; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Tobias Grosser | 442c6cc | 2015-03-19 07:43:35 +0000 | [diff] [blame] | 320 | __isl_give isl_union_map * |
| 321 | IslScheduleOptimizer::getScheduleMap(__isl_keep isl_schedule *Schedule) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 322 | isl_schedule_node *Root = isl_schedule_get_root(Schedule); |
| 323 | Root = isl_schedule_node_map_descendant( |
| 324 | Root, IslScheduleOptimizer::optimizeBand, NULL); |
| 325 | auto ScheduleMap = isl_schedule_node_get_subtree_schedule_union_map(Root); |
| 326 | ScheduleMap = isl_union_map_detect_equalities(ScheduleMap); |
| 327 | isl_schedule_node_free(Root); |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 328 | return ScheduleMap; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 331 | bool IslScheduleOptimizer::isProfitableSchedule( |
| 332 | Scop &S, __isl_keep isl_union_map *NewSchedule) { |
| 333 | // To understand if the schedule has been optimized we check if the schedule |
| 334 | // has changed at all. |
| 335 | // TODO: We can improve this by tracking if any necessarily beneficial |
| 336 | // transformations have been performed. This can e.g. be tiling, loop |
| 337 | // interchange, or ...) We can track this either at the place where the |
| 338 | // transformation has been performed or, in case of automatic ILP based |
| 339 | // optimizations, by comparing (yet to be defined) performance metrics |
| 340 | // before/after the scheduling optimizer |
| 341 | // (e.g., #stride-one accesses) |
| 342 | isl_union_map *OldSchedule = S.getSchedule(); |
| 343 | bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule); |
| 344 | isl_union_map_free(OldSchedule); |
| 345 | return changed; |
| 346 | } |
| 347 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 348 | bool IslScheduleOptimizer::runOnScop(Scop &S) { |
Johannes Doerfert | 6f7921f | 2015-02-14 12:02:24 +0000 | [diff] [blame] | 349 | |
| 350 | // Skip empty SCoPs but still allow code generation as it will delete the |
| 351 | // loops present but not needed. |
| 352 | if (S.getSize() == 0) { |
| 353 | S.markAsOptimized(); |
| 354 | return false; |
| 355 | } |
| 356 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 357 | const Dependences &D = getAnalysis<DependenceInfo>().getDependences(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 358 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 359 | if (!D.hasValidDependences()) |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 360 | return false; |
| 361 | |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 362 | isl_schedule_free(LastSchedule); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 363 | LastSchedule = nullptr; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 364 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 365 | // Build input data. |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 366 | int ValidityKinds = |
| 367 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 368 | int ProximityKinds; |
| 369 | |
| 370 | if (OptimizeDeps == "all") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 371 | ProximityKinds = |
| 372 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 373 | else if (OptimizeDeps == "raw") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 374 | ProximityKinds = Dependences::TYPE_RAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 375 | else { |
| 376 | errs() << "Do not know how to optimize for '" << OptimizeDeps << "'" |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 377 | << " Falling back to optimizing all dependences.\n"; |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 378 | ProximityKinds = |
| 379 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 382 | isl_union_set *Domain = S.getDomains(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 383 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 384 | if (!Domain) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 385 | return false; |
| 386 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 387 | isl_union_map *Validity = D.getDependences(ValidityKinds); |
| 388 | isl_union_map *Proximity = D.getDependences(ProximityKinds); |
Tobias Grosser | 8a50702 | 2012-03-16 11:51:41 +0000 | [diff] [blame] | 389 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 390 | // Simplify the dependences by removing the constraints introduced by the |
| 391 | // domains. This can speed up the scheduling time significantly, as large |
| 392 | // constant coefficients will be removed from the dependences. The |
| 393 | // introduction of some additional dependences reduces the possible |
| 394 | // transformations, but in most cases, such transformation do not seem to be |
| 395 | // interesting anyway. In some cases this option may stop the scheduler to |
| 396 | // find any schedule. |
| 397 | if (SimplifyDeps == "yes") { |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 398 | Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain)); |
| 399 | Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain)); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 400 | Proximity = |
| 401 | isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 402 | Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 403 | } else if (SimplifyDeps != "no") { |
| 404 | errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' " |
| 405 | "or 'no'. Falling back to default: 'yes'\n"; |
| 406 | } |
| 407 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 408 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
Tobias Grosser | 01aea58 | 2014-10-22 23:16:28 +0000 | [diff] [blame] | 409 | DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n"); |
| 410 | DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n"); |
| 411 | DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n"); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 412 | |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 413 | int IslFusionStrategy; |
| 414 | |
| 415 | if (FusionStrategy == "max") { |
| 416 | IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX; |
| 417 | } else if (FusionStrategy == "min") { |
| 418 | IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN; |
| 419 | } else { |
| 420 | errs() << "warning: Unknown fusion strategy. Falling back to maximal " |
| 421 | "fusion.\n"; |
| 422 | IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX; |
| 423 | } |
| 424 | |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 425 | int IslMaximizeBands; |
| 426 | |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 427 | if (MaximizeBandDepth == "yes") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 428 | IslMaximizeBands = 1; |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 429 | } else if (MaximizeBandDepth == "no") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 430 | IslMaximizeBands = 0; |
| 431 | } else { |
| 432 | errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'" |
| 433 | " or 'no'. Falling back to default: 'yes'\n"; |
| 434 | IslMaximizeBands = 1; |
| 435 | } |
| 436 | |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 437 | isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy); |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 438 | isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands); |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 439 | isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm); |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 440 | isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient); |
Tobias Grosser | 4f6bcef | 2015-03-31 07:52:36 +0000 | [diff] [blame] | 441 | isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 442 | |
| 443 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE); |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 444 | |
| 445 | isl_schedule_constraints *ScheduleConstraints; |
| 446 | ScheduleConstraints = isl_schedule_constraints_on_domain(Domain); |
| 447 | ScheduleConstraints = |
| 448 | isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity); |
| 449 | ScheduleConstraints = isl_schedule_constraints_set_validity( |
| 450 | ScheduleConstraints, isl_union_map_copy(Validity)); |
| 451 | ScheduleConstraints = |
| 452 | isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 453 | isl_schedule *Schedule; |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 454 | Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 455 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT); |
| 456 | |
| 457 | // In cases the scheduler is not able to optimize the code, we just do not |
| 458 | // touch the schedule. |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 459 | if (!Schedule) |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 460 | return false; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 461 | |
Tobias Grosser | 01aea58 | 2014-10-22 23:16:28 +0000 | [diff] [blame] | 462 | DEBUG(dbgs() << "Schedule := " << stringFromIslObj(Schedule) << ";\n"); |
Tobias Grosser | 4d63b9d | 2012-02-20 08:41:21 +0000 | [diff] [blame] | 463 | |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 464 | isl_union_map *NewSchedule = getScheduleMap(Schedule); |
| 465 | |
| 466 | // Check if the optimizations performed were profitable, otherwise exit early. |
| 467 | if (!isProfitableSchedule(S, NewSchedule)) { |
| 468 | isl_schedule_free(Schedule); |
| 469 | isl_union_map_free(NewSchedule); |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | S.markAsOptimized(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 474 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 475 | for (ScopStmt *Stmt : S) { |
Tobias Grosser | 249c4b1 | 2013-04-11 05:55:13 +0000 | [diff] [blame] | 476 | isl_map *StmtSchedule; |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 477 | isl_set *Domain = Stmt->getDomain(); |
| 478 | isl_union_map *StmtBand; |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 479 | StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(NewSchedule), |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 480 | isl_union_set_from_set(Domain)); |
Tobias Grosser | 249c4b1 | 2013-04-11 05:55:13 +0000 | [diff] [blame] | 481 | if (isl_union_map_is_empty(StmtBand)) { |
Tobias Grosser | 34f0613 | 2014-02-21 20:51:36 +0000 | [diff] [blame] | 482 | StmtSchedule = isl_map_from_domain(isl_set_empty(Stmt->getDomainSpace())); |
Tobias Grosser | 249c4b1 | 2013-04-11 05:55:13 +0000 | [diff] [blame] | 483 | isl_union_map_free(StmtBand); |
| 484 | } else { |
| 485 | assert(isl_union_map_n_map(StmtBand) == 1); |
| 486 | StmtSchedule = isl_map_from_union_map(StmtBand); |
Tobias Grosser | f242b80 | 2013-04-10 22:48:08 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 489 | Stmt->setSchedule(StmtSchedule); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 492 | isl_schedule_free(Schedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 493 | isl_union_map_free(NewSchedule); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | |
Johannes Doerfert | 3fe584d | 2015-03-01 18:40:25 +0000 | [diff] [blame] | 497 | void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const { |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 498 | isl_printer *p; |
| 499 | char *ScheduleStr; |
| 500 | |
| 501 | OS << "Calculated schedule:\n"; |
| 502 | |
| 503 | if (!LastSchedule) { |
| 504 | OS << "n/a\n"; |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule)); |
| 509 | p = isl_printer_print_schedule(p, LastSchedule); |
| 510 | ScheduleStr = isl_printer_get_str(p); |
| 511 | isl_printer_free(p); |
| 512 | |
| 513 | OS << ScheduleStr << "\n"; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 516 | void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 517 | ScopPass::getAnalysisUsage(AU); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 518 | AU.addRequired<DependenceInfo>(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 521 | Pass *polly::createIslScheduleOptimizerPass() { |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 522 | return new IslScheduleOptimizer(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 523 | } |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 524 | |
| 525 | INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", |
| 526 | "Polly - Optimize schedule of SCoP", false, false); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 527 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 528 | INITIALIZE_PASS_DEPENDENCY(ScopInfo); |
| 529 | INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", |
| 530 | "Polly - Optimize schedule of SCoP", false, false) |