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" |
Michael Kruse | 7410a27 | 2016-05-30 14:27:14 +0000 | [diff] [blame] | 68 | #include <ciso646> |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 69 | |
| 70 | using namespace llvm; |
| 71 | using namespace polly; |
| 72 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 73 | #define DEBUG_TYPE "polly-opt-isl" |
| 74 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 75 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 76 | OptimizeDeps("polly-opt-optimize-only", |
| 77 | cl::desc("Only a certain kind of dependences (all/raw)"), |
| 78 | cl::Hidden, cl::init("all"), cl::ZeroOrMore, |
| 79 | cl::cat(PollyCategory)); |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 80 | |
| 81 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 82 | SimplifyDeps("polly-opt-simplify-deps", |
| 83 | cl::desc("Dependences should be simplified (yes/no)"), |
| 84 | cl::Hidden, cl::init("yes"), cl::ZeroOrMore, |
| 85 | cl::cat(PollyCategory)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 86 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 87 | static cl::opt<int> MaxConstantTerm( |
| 88 | "polly-opt-max-constant-term", |
| 89 | cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden, |
| 90 | cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 91 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 92 | static cl::opt<int> MaxCoefficient( |
| 93 | "polly-opt-max-coefficient", |
| 94 | cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden, |
| 95 | cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 96 | |
| 97 | static cl::opt<std::string> FusionStrategy( |
| 98 | "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"), |
| 99 | cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 100 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 101 | static cl::opt<std::string> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 102 | MaximizeBandDepth("polly-opt-maximize-bands", |
| 103 | cl::desc("Maximize the band depth (yes/no)"), cl::Hidden, |
| 104 | cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 105 | |
Michael Kruse | 315aa32 | 2016-05-02 11:35:27 +0000 | [diff] [blame] | 106 | static cl::opt<std::string> OuterCoincidence( |
| 107 | "polly-opt-outer-coincidence", |
| 108 | cl::desc("Try to construct schedules where the outer member of each band " |
| 109 | "satisfies the coincidence constraints (yes/no)"), |
| 110 | cl::Hidden, cl::init("no"), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 111 | |
Tobias Grosser | 07c1c2f | 2015-08-19 08:46:11 +0000 | [diff] [blame] | 112 | static cl::opt<int> PrevectorWidth( |
| 113 | "polly-prevect-width", |
| 114 | cl::desc( |
| 115 | "The number of loop iterations to strip-mine for pre-vectorization"), |
| 116 | cl::Hidden, cl::init(4), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 117 | |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 118 | static cl::opt<bool> FirstLevelTiling("polly-tiling", |
| 119 | cl::desc("Enable loop tiling"), |
| 120 | cl::init(true), cl::ZeroOrMore, |
| 121 | cl::cat(PollyCategory)); |
| 122 | |
| 123 | static cl::opt<int> FirstLevelDefaultTileSize( |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 124 | "polly-default-tile-size", |
| 125 | cl::desc("The default tile size (if not enough were provided by" |
| 126 | " --polly-tile-sizes)"), |
| 127 | cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Johannes Doerfert | c3958b2 | 2014-05-28 17:21:02 +0000 | [diff] [blame] | 128 | |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 129 | static cl::list<int> FirstLevelTileSizes( |
| 130 | "polly-tile-sizes", cl::desc("A tile size for each loop dimension, filled " |
| 131 | "with --polly-default-tile-size"), |
| 132 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory)); |
| 133 | |
| 134 | static cl::opt<bool> |
| 135 | SecondLevelTiling("polly-2nd-level-tiling", |
| 136 | cl::desc("Enable a 2nd level loop of loop tiling"), |
| 137 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 138 | |
| 139 | static cl::opt<int> SecondLevelDefaultTileSize( |
| 140 | "polly-2nd-level-default-tile-size", |
| 141 | cl::desc("The default 2nd-level tile size (if not enough were provided by" |
| 142 | " --polly-2nd-level-tile-sizes)"), |
| 143 | cl::Hidden, cl::init(16), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 144 | |
| 145 | static cl::list<int> |
| 146 | SecondLevelTileSizes("polly-2nd-level-tile-sizes", |
| 147 | cl::desc("A tile size for each loop dimension, filled " |
| 148 | "with --polly-default-tile-size"), |
| 149 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, |
| 150 | cl::cat(PollyCategory)); |
| 151 | |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 152 | static cl::opt<bool> RegisterTiling("polly-register-tiling", |
| 153 | cl::desc("Enable register tiling"), |
| 154 | cl::init(false), cl::ZeroOrMore, |
| 155 | cl::cat(PollyCategory)); |
| 156 | |
| 157 | static cl::opt<int> RegisterDefaultTileSize( |
| 158 | "polly-register-tiling-default-tile-size", |
| 159 | cl::desc("The default register tile size (if not enough were provided by" |
| 160 | " --polly-register-tile-sizes)"), |
| 161 | cl::Hidden, cl::init(2), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 162 | |
| 163 | static cl::list<int> |
| 164 | RegisterTileSizes("polly-register-tile-sizes", |
| 165 | cl::desc("A tile size for each loop dimension, filled " |
| 166 | "with --polly-register-tile-size"), |
| 167 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, |
| 168 | cl::cat(PollyCategory)); |
| 169 | |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 170 | static cl::opt<bool> |
| 171 | PMBasedOpts("polly-pattern-matching-based-opts", |
| 172 | cl::desc("Perform optimizations based on pattern matching"), |
| 173 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 174 | |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 175 | /// @brief Create an isl_union_set, which describes the isolate option based |
| 176 | /// on IsoalteDomain. |
| 177 | /// |
| 178 | /// @param IsolateDomain An isl_set whose last dimension is the only one that |
| 179 | /// should belong to the current band node. |
| 180 | static __isl_give isl_union_set * |
| 181 | getIsolateOptions(__isl_take isl_set *IsolateDomain) { |
| 182 | auto Dims = isl_set_dim(IsolateDomain, isl_dim_set); |
| 183 | auto *IsolateRelation = isl_map_from_domain(IsolateDomain); |
| 184 | IsolateRelation = isl_map_move_dims(IsolateRelation, isl_dim_out, 0, |
| 185 | isl_dim_in, Dims - 1, 1); |
| 186 | auto *IsolateOption = isl_map_wrap(IsolateRelation); |
| 187 | auto *Id = isl_id_alloc(isl_set_get_ctx(IsolateOption), "isolate", NULL); |
| 188 | return isl_union_set_from_set(isl_set_set_tuple_id(IsolateOption, Id)); |
| 189 | } |
| 190 | |
| 191 | /// @brief Create an isl_union_set, which describes the atomic option for the |
| 192 | /// dimension of the current node. |
| 193 | /// |
| 194 | /// It may help to reduce the size of generated code. |
| 195 | /// |
| 196 | /// @param Ctx An isl_ctx, which is used to create the isl_union_set. |
| 197 | static __isl_give isl_union_set *getAtomicOptions(__isl_take isl_ctx *Ctx) { |
| 198 | auto *Space = isl_space_set_alloc(Ctx, 0, 1); |
| 199 | auto *AtomicOption = isl_set_universe(Space); |
| 200 | auto *Id = isl_id_alloc(Ctx, "atomic", NULL); |
| 201 | return isl_union_set_from_set(isl_set_set_tuple_id(AtomicOption, Id)); |
| 202 | } |
| 203 | |
| 204 | /// @brief Make the last dimension of Set to take values |
| 205 | /// from 0 to VectorWidth - 1. |
| 206 | /// |
| 207 | /// @param Set A set, which should be modified. |
| 208 | /// @param VectorWidth A parameter, which determines the constraint. |
| 209 | static __isl_give isl_set *addExtentConstraints(__isl_take isl_set *Set, |
| 210 | int VectorWidth) { |
| 211 | auto Dims = isl_set_dim(Set, isl_dim_set); |
| 212 | auto Space = isl_set_get_space(Set); |
| 213 | auto *LocalSpace = isl_local_space_from_space(Space); |
| 214 | auto *ExtConstr = |
| 215 | isl_constraint_alloc_inequality(isl_local_space_copy(LocalSpace)); |
| 216 | ExtConstr = isl_constraint_set_constant_si(ExtConstr, 0); |
| 217 | ExtConstr = |
| 218 | isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, 1); |
| 219 | Set = isl_set_add_constraint(Set, ExtConstr); |
| 220 | ExtConstr = isl_constraint_alloc_inequality(LocalSpace); |
| 221 | ExtConstr = isl_constraint_set_constant_si(ExtConstr, VectorWidth - 1); |
| 222 | ExtConstr = |
| 223 | isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, -1); |
| 224 | return isl_set_add_constraint(Set, ExtConstr); |
| 225 | } |
| 226 | |
| 227 | /// @brief Build the desired set of partial tile prefixes. |
| 228 | /// |
| 229 | /// We build a set of partial tile prefixes, which are prefixes of the vector |
| 230 | /// loop that have exactly VectorWidth iterations. |
| 231 | /// |
| 232 | /// 1. Get all prefixes of the vector loop. |
| 233 | /// 2. Extend it to a set, which has exactly VectorWidth iterations for |
| 234 | /// any prefix from the set that was built on the previous step. |
| 235 | /// 3. Subtract loop domain from it, project out the vector loop dimension and |
| 236 | /// get a set of prefixes, which don’t have exactly VectorWidth iterations. |
| 237 | /// 4. Subtract it from all prefixes of the vector loop and get the desired |
| 238 | /// set. |
| 239 | /// |
| 240 | /// @param ScheduleRange A range of a map, which describes a prefix schedule |
| 241 | /// relation. |
| 242 | static __isl_give isl_set * |
| 243 | getPartialTilePrefixes(__isl_take isl_set *ScheduleRange, int VectorWidth) { |
| 244 | auto Dims = isl_set_dim(ScheduleRange, isl_dim_set); |
| 245 | auto *LoopPrefixes = isl_set_project_out(isl_set_copy(ScheduleRange), |
| 246 | isl_dim_set, Dims - 1, 1); |
| 247 | auto *ExtentPrefixes = |
| 248 | isl_set_add_dims(isl_set_copy(LoopPrefixes), isl_dim_set, 1); |
| 249 | ExtentPrefixes = addExtentConstraints(ExtentPrefixes, VectorWidth); |
| 250 | auto *BadPrefixes = isl_set_subtract(ExtentPrefixes, ScheduleRange); |
| 251 | BadPrefixes = isl_set_project_out(BadPrefixes, isl_dim_set, Dims - 1, 1); |
| 252 | return isl_set_subtract(LoopPrefixes, BadPrefixes); |
| 253 | } |
| 254 | |
| 255 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::isolateFullPartialTiles( |
| 256 | __isl_take isl_schedule_node *Node, int VectorWidth) { |
| 257 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
| 258 | Node = isl_schedule_node_child(Node, 0); |
| 259 | Node = isl_schedule_node_child(Node, 0); |
| 260 | auto *SchedRelUMap = isl_schedule_node_get_prefix_schedule_relation(Node); |
| 261 | auto *ScheduleRelation = isl_map_from_union_map(SchedRelUMap); |
| 262 | auto *ScheduleRange = isl_map_range(ScheduleRelation); |
| 263 | auto *IsolateDomain = getPartialTilePrefixes(ScheduleRange, VectorWidth); |
| 264 | auto *AtomicOption = getAtomicOptions(isl_set_get_ctx(IsolateDomain)); |
| 265 | auto *IsolateOption = getIsolateOptions(IsolateDomain); |
| 266 | Node = isl_schedule_node_parent(Node); |
| 267 | Node = isl_schedule_node_parent(Node); |
| 268 | auto *Options = isl_union_set_union(IsolateOption, AtomicOption); |
| 269 | Node = isl_schedule_node_band_set_ast_build_options(Node, Options); |
| 270 | return Node; |
| 271 | } |
| 272 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 273 | __isl_give isl_schedule_node * |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 274 | ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node, |
| 275 | unsigned DimToVectorize, |
| 276 | int VectorWidth) { |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 277 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 278 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 279 | auto Space = isl_schedule_node_band_get_space(Node); |
| 280 | auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set); |
| 281 | isl_space_free(Space); |
| 282 | assert(DimToVectorize < ScheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 283 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 284 | if (DimToVectorize > 0) { |
| 285 | Node = isl_schedule_node_band_split(Node, DimToVectorize); |
| 286 | Node = isl_schedule_node_child(Node, 0); |
| 287 | } |
| 288 | if (DimToVectorize < ScheduleDimensions - 1) |
| 289 | Node = isl_schedule_node_band_split(Node, 1); |
| 290 | Space = isl_schedule_node_band_get_space(Node); |
| 291 | auto Sizes = isl_multi_val_zero(Space); |
| 292 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 293 | Sizes = |
| 294 | isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth)); |
| 295 | Node = isl_schedule_node_band_tile(Node, Sizes); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 296 | Node = isolateFullPartialTiles(Node, VectorWidth); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 297 | Node = isl_schedule_node_child(Node, 0); |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 298 | // Make sure the "trivially vectorizable loop" is not unrolled. Otherwise, |
| 299 | // we will have troubles to match it in the backend. |
| 300 | Node = isl_schedule_node_band_set_ast_build_options( |
Tobias Grosser | fc490a9 | 2015-08-20 19:08:16 +0000 | [diff] [blame] | 301 | Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }")); |
| 302 | Node = isl_schedule_node_band_sink(Node); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 303 | Node = isl_schedule_node_child(Node, 0); |
Roman Gareev | 11001e1 | 2016-02-23 09:00:13 +0000 | [diff] [blame] | 304 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_leaf) |
| 305 | Node = isl_schedule_node_parent(Node); |
| 306 | isl_id *LoopMarker = isl_id_alloc(Ctx, "SIMD", nullptr); |
| 307 | Node = isl_schedule_node_insert_mark(Node, LoopMarker); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 308 | return Node; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Tobias Grosser | d891b54 | 2015-08-20 12:16:23 +0000 | [diff] [blame] | 311 | __isl_give isl_schedule_node * |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 312 | ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node, |
| 313 | const char *Identifier, ArrayRef<int> TileSizes, |
| 314 | int DefaultTileSize) { |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 315 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 316 | auto Space = isl_schedule_node_band_get_space(Node); |
| 317 | auto Dims = isl_space_dim(Space, isl_dim_set); |
| 318 | auto Sizes = isl_multi_val_zero(Space); |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 319 | std::string IdentifierString(Identifier); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 320 | for (unsigned i = 0; i < Dims; i++) { |
| 321 | auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize; |
| 322 | Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize)); |
| 323 | } |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 324 | auto TileLoopMarkerStr = IdentifierString + " - Tiles"; |
| 325 | isl_id *TileLoopMarker = |
| 326 | isl_id_alloc(Ctx, TileLoopMarkerStr.c_str(), nullptr); |
| 327 | Node = isl_schedule_node_insert_mark(Node, TileLoopMarker); |
| 328 | Node = isl_schedule_node_child(Node, 0); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 329 | Node = isl_schedule_node_band_tile(Node, Sizes); |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 330 | Node = isl_schedule_node_child(Node, 0); |
| 331 | auto PointLoopMarkerStr = IdentifierString + " - Points"; |
| 332 | isl_id *PointLoopMarker = |
| 333 | isl_id_alloc(Ctx, PointLoopMarkerStr.c_str(), nullptr); |
| 334 | Node = isl_schedule_node_insert_mark(Node, PointLoopMarker); |
| 335 | Node = isl_schedule_node_child(Node, 0); |
| 336 | return Node; |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 339 | bool ScheduleTreeOptimizer::isTileableBandNode( |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 340 | __isl_keep isl_schedule_node *Node) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 341 | if (isl_schedule_node_get_type(Node) != isl_schedule_node_band) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 342 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 343 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 344 | if (isl_schedule_node_n_children(Node) != 1) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 345 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 346 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 347 | if (!isl_schedule_node_band_get_permutable(Node)) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 348 | return false; |
Tobias Grosser | 44f19ac | 2011-07-05 22:15:53 +0000 | [diff] [blame] | 349 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 350 | auto Space = isl_schedule_node_band_get_space(Node); |
| 351 | auto Dims = isl_space_dim(Space, isl_dim_set); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 352 | isl_space_free(Space); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 353 | |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 354 | if (Dims <= 1) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 355 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 356 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 357 | auto Child = isl_schedule_node_get_child(Node, 0); |
| 358 | auto Type = isl_schedule_node_get_type(Child); |
| 359 | isl_schedule_node_free(Child); |
| 360 | |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 361 | if (Type != isl_schedule_node_leaf) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 362 | return false; |
| 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | __isl_give isl_schedule_node * |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 368 | ScheduleTreeOptimizer::standardBandOpts(__isl_take isl_schedule_node *Node, |
| 369 | void *User) { |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 370 | if (FirstLevelTiling) |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 371 | Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes, |
| 372 | FirstLevelDefaultTileSize); |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 373 | |
| 374 | if (SecondLevelTiling) |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 375 | Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes, |
| 376 | SecondLevelDefaultTileSize); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 377 | |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 378 | if (RegisterTiling) { |
| 379 | auto *Ctx = isl_schedule_node_get_ctx(Node); |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 380 | Node = tileNode(Node, "Register tiling", RegisterTileSizes, |
| 381 | RegisterDefaultTileSize); |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 382 | Node = isl_schedule_node_band_set_ast_build_options( |
| 383 | Node, isl_union_set_read_from_str(Ctx, "{unroll[x]}")); |
| 384 | } |
| 385 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 386 | if (PollyVectorizerChoice == VECTORIZER_NONE) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 387 | return Node; |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 388 | |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 389 | auto Space = isl_schedule_node_band_get_space(Node); |
| 390 | auto Dims = isl_space_dim(Space, isl_dim_set); |
| 391 | isl_space_free(Space); |
| 392 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 393 | for (int i = Dims - 1; i >= 0; i--) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 394 | if (isl_schedule_node_band_member_get_coincident(Node, i)) { |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 395 | Node = prevectSchedBand(Node, i, PrevectorWidth); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 396 | break; |
| 397 | } |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 398 | |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 399 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 402 | /// @brief Check whether output dimensions of the map rely on the specified |
| 403 | /// input dimension. |
| 404 | /// |
| 405 | /// @param IslMap The isl map to be considered. |
| 406 | /// @param DimNum The number of an input dimension to be checked. |
| 407 | static bool isInputDimUsed(__isl_take isl_map *IslMap, unsigned DimNum) { |
| 408 | auto *CheckedAccessRelation = |
| 409 | isl_map_project_out(isl_map_copy(IslMap), isl_dim_in, DimNum, 1); |
| 410 | CheckedAccessRelation = |
| 411 | isl_map_insert_dims(CheckedAccessRelation, isl_dim_in, DimNum, 1); |
| 412 | auto *InputDimsId = isl_map_get_tuple_id(IslMap, isl_dim_in); |
| 413 | CheckedAccessRelation = |
| 414 | isl_map_set_tuple_id(CheckedAccessRelation, isl_dim_in, InputDimsId); |
| 415 | InputDimsId = isl_map_get_tuple_id(IslMap, isl_dim_out); |
| 416 | CheckedAccessRelation = |
| 417 | isl_map_set_tuple_id(CheckedAccessRelation, isl_dim_out, InputDimsId); |
| 418 | auto res = !isl_map_is_equal(CheckedAccessRelation, IslMap); |
| 419 | isl_map_free(CheckedAccessRelation); |
| 420 | isl_map_free(IslMap); |
| 421 | return res; |
| 422 | } |
| 423 | |
| 424 | /// @brief Check if the SCoP statement could probably be optimized with |
| 425 | /// analytical modeling. |
| 426 | /// |
| 427 | /// containsMatrMult tries to determine whether the following conditions |
| 428 | /// are true: |
| 429 | /// 1. all memory accesses of the statement will have stride 0 or 1, |
| 430 | /// if we interchange loops (switch the variable used in the inner |
| 431 | /// loop to the outer loop). |
| 432 | /// 2. all memory accesses of the statement except from the last one, are |
| 433 | /// read memory access and the last one is write memory access. |
| 434 | /// 3. all subscripts of the last memory access of the statement don’t contain |
| 435 | /// the variable used in the inner loop. |
| 436 | /// |
| 437 | /// @param PartialSchedule The PartialSchedule that contains a SCoP statement |
| 438 | /// to check. |
| 439 | static bool containsMatrMult(__isl_keep isl_map *PartialSchedule) { |
| 440 | auto InputDimsId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in); |
| 441 | auto *ScpStmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId)); |
| 442 | isl_id_free(InputDimsId); |
| 443 | if (ScpStmt->size() <= 1) |
| 444 | return false; |
| 445 | auto MemA = ScpStmt->begin(); |
| 446 | for (unsigned i = 0; i < ScpStmt->size() - 2 && MemA != ScpStmt->end(); |
| 447 | i++, MemA++) |
| 448 | if (!(*MemA)->isRead() or |
| 449 | ((*MemA)->isArrayKind() and |
| 450 | !((*MemA)->isStrideOne(isl_map_copy(PartialSchedule)) or |
| 451 | (*MemA)->isStrideZero(isl_map_copy(PartialSchedule))))) |
| 452 | return false; |
| 453 | MemA++; |
| 454 | if (!(*MemA)->isWrite() or !(*MemA)->isArrayKind() or |
| 455 | !((*MemA)->isStrideOne(isl_map_copy(PartialSchedule)) or |
| 456 | (*MemA)->isStrideZero(isl_map_copy(PartialSchedule)))) |
| 457 | return false; |
| 458 | auto DimNum = isl_map_dim(PartialSchedule, isl_dim_in); |
| 459 | return !isInputDimUsed((*MemA)->getAccessRelation(), DimNum - 1); |
| 460 | } |
| 461 | |
| 462 | /// @brief Circular shift of output dimensions of the integer map. |
| 463 | /// |
| 464 | /// @param IslMap The isl map to be modified. |
| 465 | static __isl_give isl_map *circularShiftOutputDims(__isl_take isl_map *IslMap) { |
| 466 | auto InputDimsId = isl_map_get_tuple_id(IslMap, isl_dim_in); |
| 467 | auto DimNum = isl_map_dim(IslMap, isl_dim_out); |
| 468 | IslMap = isl_map_move_dims(IslMap, isl_dim_in, 0, isl_dim_out, DimNum - 1, 1); |
| 469 | IslMap = isl_map_move_dims(IslMap, isl_dim_out, 0, isl_dim_in, 0, 1); |
| 470 | return isl_map_set_tuple_id(IslMap, isl_dim_in, InputDimsId); |
| 471 | } |
| 472 | |
| 473 | bool ScheduleTreeOptimizer::isMatrMultPattern( |
| 474 | __isl_keep isl_schedule_node *Node) { |
| 475 | auto *PartialSchedule = |
| 476 | isl_schedule_node_band_get_partial_schedule_union_map(Node); |
| 477 | if (isl_union_map_n_map(PartialSchedule) != 1) |
| 478 | return false; |
| 479 | auto *NewPartialSchedule = isl_map_from_union_map(PartialSchedule); |
| 480 | auto DimNum = isl_map_dim(NewPartialSchedule, isl_dim_in); |
| 481 | if (DimNum != 3) { |
| 482 | isl_map_free(NewPartialSchedule); |
| 483 | return false; |
| 484 | } |
| 485 | NewPartialSchedule = circularShiftOutputDims(NewPartialSchedule); |
| 486 | if (containsMatrMult(NewPartialSchedule)) { |
| 487 | isl_map_free(NewPartialSchedule); |
| 488 | return true; |
| 489 | } |
| 490 | isl_map_free(NewPartialSchedule); |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | __isl_give isl_schedule_node * |
| 495 | ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node, |
| 496 | void *User) { |
| 497 | if (!isTileableBandNode(Node)) |
| 498 | return Node; |
| 499 | |
| 500 | if (PMBasedOpts && isMatrMultPattern(Node)) |
| 501 | DEBUG(dbgs() << "The matrix multiplication pattern was detected\n"); |
| 502 | |
| 503 | return standardBandOpts(Node, User); |
| 504 | } |
| 505 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 506 | __isl_give isl_schedule * |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 507 | ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 508 | isl_schedule_node *Root = isl_schedule_get_root(Schedule); |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 509 | Root = optimizeScheduleNode(Root); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 510 | isl_schedule_free(Schedule); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 511 | auto S = isl_schedule_node_get_schedule(Root); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 512 | isl_schedule_node_free(Root); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 513 | return S; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 516 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode( |
| 517 | __isl_take isl_schedule_node *Node) { |
| 518 | Node = isl_schedule_node_map_descendant_bottom_up(Node, optimizeBand, NULL); |
| 519 | return Node; |
| 520 | } |
| 521 | |
| 522 | bool ScheduleTreeOptimizer::isProfitableSchedule( |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 523 | Scop &S, __isl_keep isl_union_map *NewSchedule) { |
| 524 | // To understand if the schedule has been optimized we check if the schedule |
| 525 | // has changed at all. |
| 526 | // TODO: We can improve this by tracking if any necessarily beneficial |
| 527 | // transformations have been performed. This can e.g. be tiling, loop |
| 528 | // interchange, or ...) We can track this either at the place where the |
| 529 | // transformation has been performed or, in case of automatic ILP based |
| 530 | // optimizations, by comparing (yet to be defined) performance metrics |
| 531 | // before/after the scheduling optimizer |
| 532 | // (e.g., #stride-one accesses) |
| 533 | isl_union_map *OldSchedule = S.getSchedule(); |
| 534 | bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule); |
| 535 | isl_union_map_free(OldSchedule); |
| 536 | return changed; |
| 537 | } |
| 538 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 539 | namespace { |
| 540 | class IslScheduleOptimizer : public ScopPass { |
| 541 | public: |
| 542 | static char ID; |
| 543 | explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; } |
| 544 | |
| 545 | ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); } |
| 546 | |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 547 | /// @brief Optimize the schedule of the SCoP @p S. |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 548 | bool runOnScop(Scop &S) override; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 549 | |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 550 | /// @brief Print the new schedule for the SCoP @p S. |
| 551 | void printScop(raw_ostream &OS, Scop &S) const override; |
| 552 | |
| 553 | /// @brief Register all analyses and transformation required. |
| 554 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 555 | |
Johannes Doerfert | 0f37630 | 2015-09-27 15:42:28 +0000 | [diff] [blame] | 556 | /// @brief Release the internal memory. |
| 557 | void releaseMemory() override { |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 558 | isl_schedule_free(LastSchedule); |
| 559 | LastSchedule = nullptr; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 560 | } |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 561 | |
| 562 | private: |
| 563 | isl_schedule *LastSchedule; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 564 | }; |
| 565 | } |
| 566 | |
| 567 | char IslScheduleOptimizer::ID = 0; |
| 568 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 569 | bool IslScheduleOptimizer::runOnScop(Scop &S) { |
Johannes Doerfert | 6f7921f | 2015-02-14 12:02:24 +0000 | [diff] [blame] | 570 | |
| 571 | // Skip empty SCoPs but still allow code generation as it will delete the |
| 572 | // loops present but not needed. |
| 573 | if (S.getSize() == 0) { |
| 574 | S.markAsOptimized(); |
| 575 | return false; |
| 576 | } |
| 577 | |
Hongbin Zheng | 2a79885 | 2016-03-03 08:15:33 +0000 | [diff] [blame] | 578 | const Dependences &D = |
| 579 | getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 580 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 581 | if (!D.hasValidDependences()) |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 582 | return false; |
| 583 | |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 584 | isl_schedule_free(LastSchedule); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 585 | LastSchedule = nullptr; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 586 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 587 | // Build input data. |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 588 | int ValidityKinds = |
| 589 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 590 | int ProximityKinds; |
| 591 | |
| 592 | if (OptimizeDeps == "all") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 593 | ProximityKinds = |
| 594 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 595 | else if (OptimizeDeps == "raw") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 596 | ProximityKinds = Dependences::TYPE_RAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 597 | else { |
| 598 | errs() << "Do not know how to optimize for '" << OptimizeDeps << "'" |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 599 | << " Falling back to optimizing all dependences.\n"; |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 600 | ProximityKinds = |
| 601 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 604 | isl_union_set *Domain = S.getDomains(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 605 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 606 | if (!Domain) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 607 | return false; |
| 608 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 609 | isl_union_map *Validity = D.getDependences(ValidityKinds); |
| 610 | isl_union_map *Proximity = D.getDependences(ProximityKinds); |
Tobias Grosser | 8a50702 | 2012-03-16 11:51:41 +0000 | [diff] [blame] | 611 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 612 | // Simplify the dependences by removing the constraints introduced by the |
| 613 | // domains. This can speed up the scheduling time significantly, as large |
| 614 | // constant coefficients will be removed from the dependences. The |
| 615 | // introduction of some additional dependences reduces the possible |
| 616 | // transformations, but in most cases, such transformation do not seem to be |
| 617 | // interesting anyway. In some cases this option may stop the scheduler to |
| 618 | // find any schedule. |
| 619 | if (SimplifyDeps == "yes") { |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 620 | Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain)); |
| 621 | Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain)); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 622 | Proximity = |
| 623 | isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 624 | Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 625 | } else if (SimplifyDeps != "no") { |
| 626 | errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' " |
| 627 | "or 'no'. Falling back to default: 'yes'\n"; |
| 628 | } |
| 629 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 630 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
Tobias Grosser | 01aea58 | 2014-10-22 23:16:28 +0000 | [diff] [blame] | 631 | DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n"); |
| 632 | DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n"); |
| 633 | DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n"); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 634 | |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 635 | unsigned IslSerializeSCCs; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 636 | |
| 637 | if (FusionStrategy == "max") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 638 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 639 | } else if (FusionStrategy == "min") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 640 | IslSerializeSCCs = 1; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 641 | } else { |
| 642 | errs() << "warning: Unknown fusion strategy. Falling back to maximal " |
| 643 | "fusion.\n"; |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 644 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 647 | int IslMaximizeBands; |
| 648 | |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 649 | if (MaximizeBandDepth == "yes") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 650 | IslMaximizeBands = 1; |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 651 | } else if (MaximizeBandDepth == "no") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 652 | IslMaximizeBands = 0; |
| 653 | } else { |
| 654 | errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'" |
| 655 | " or 'no'. Falling back to default: 'yes'\n"; |
| 656 | IslMaximizeBands = 1; |
| 657 | } |
| 658 | |
Michael Kruse | 315aa32 | 2016-05-02 11:35:27 +0000 | [diff] [blame] | 659 | int IslOuterCoincidence; |
| 660 | |
| 661 | if (OuterCoincidence == "yes") { |
| 662 | IslOuterCoincidence = 1; |
| 663 | } else if (OuterCoincidence == "no") { |
| 664 | IslOuterCoincidence = 0; |
| 665 | } else { |
| 666 | errs() << "warning: Option -polly-opt-outer-coincidence should either be " |
| 667 | "'yes' or 'no'. Falling back to default: 'no'\n"; |
| 668 | IslOuterCoincidence = 0; |
| 669 | } |
| 670 | |
| 671 | isl_options_set_schedule_outer_coincidence(S.getIslCtx(), |
| 672 | IslOuterCoincidence); |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 673 | isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs); |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 674 | isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands); |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 675 | isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm); |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 676 | isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient); |
Tobias Grosser | 4f6bcef | 2015-03-31 07:52:36 +0000 | [diff] [blame] | 677 | isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 678 | |
| 679 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE); |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 680 | |
| 681 | isl_schedule_constraints *ScheduleConstraints; |
| 682 | ScheduleConstraints = isl_schedule_constraints_on_domain(Domain); |
| 683 | ScheduleConstraints = |
| 684 | isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity); |
| 685 | ScheduleConstraints = isl_schedule_constraints_set_validity( |
| 686 | ScheduleConstraints, isl_union_map_copy(Validity)); |
| 687 | ScheduleConstraints = |
| 688 | isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 689 | isl_schedule *Schedule; |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 690 | Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 691 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT); |
| 692 | |
| 693 | // In cases the scheduler is not able to optimize the code, we just do not |
| 694 | // touch the schedule. |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 695 | if (!Schedule) |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 696 | return false; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 697 | |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 698 | DEBUG({ |
| 699 | auto *P = isl_printer_to_str(S.getIslCtx()); |
| 700 | P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); |
| 701 | P = isl_printer_print_schedule(P, Schedule); |
| 702 | dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n"; |
| 703 | isl_printer_free(P); |
| 704 | }); |
Tobias Grosser | 4d63b9d | 2012-02-20 08:41:21 +0000 | [diff] [blame] | 705 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 706 | isl_schedule *NewSchedule = ScheduleTreeOptimizer::optimizeSchedule(Schedule); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 707 | isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 708 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 709 | if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewScheduleMap)) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 710 | isl_union_map_free(NewScheduleMap); |
| 711 | isl_schedule_free(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 712 | return false; |
| 713 | } |
| 714 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 715 | S.setScheduleTree(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 716 | S.markAsOptimized(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 717 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 718 | isl_union_map_free(NewScheduleMap); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 719 | return false; |
| 720 | } |
| 721 | |
Johannes Doerfert | 3fe584d | 2015-03-01 18:40:25 +0000 | [diff] [blame] | 722 | void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const { |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 723 | isl_printer *p; |
| 724 | char *ScheduleStr; |
| 725 | |
| 726 | OS << "Calculated schedule:\n"; |
| 727 | |
| 728 | if (!LastSchedule) { |
| 729 | OS << "n/a\n"; |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule)); |
| 734 | p = isl_printer_print_schedule(p, LastSchedule); |
| 735 | ScheduleStr = isl_printer_get_str(p); |
| 736 | isl_printer_free(p); |
| 737 | |
| 738 | OS << ScheduleStr << "\n"; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 741 | void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 742 | ScopPass::getAnalysisUsage(AU); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 743 | AU.addRequired<DependenceInfo>(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 746 | Pass *polly::createIslScheduleOptimizerPass() { |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 747 | return new IslScheduleOptimizer(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 748 | } |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 749 | |
| 750 | INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", |
| 751 | "Polly - Optimize schedule of SCoP", false, false); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 752 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 753 | INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 754 | INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", |
| 755 | "Polly - Optimize schedule of SCoP", false, false) |