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 | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 10 | // This pass generates an entirely new schedule tree from the data dependences |
Tobias Grosser | 234a482 | 2015-08-15 09:34:33 +0000 | [diff] [blame] | 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" |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 56 | #include "llvm/Analysis/TargetTransformInfo.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 57 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 58 | #include "isl/aff.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 59 | #include "isl/band.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 60 | #include "isl/constraint.h" |
| 61 | #include "isl/map.h" |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 62 | #include "isl/options.h" |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 63 | #include "isl/printer.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 64 | #include "isl/schedule.h" |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 65 | #include "isl/schedule_node.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 66 | #include "isl/space.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 67 | #include "isl/union_map.h" |
| 68 | #include "isl/union_set.h" |
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 | |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 123 | static cl::opt<int> LatencyVectorFma( |
| 124 | "polly-target-latency-vector-fma", |
| 125 | cl::desc("The minimal number of cycles between issuing two " |
| 126 | "dependent consecutive vector fused multiply-add " |
| 127 | "instructions."), |
| 128 | cl::Hidden, cl::init(8), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 129 | |
| 130 | static cl::opt<int> ThrougputVectorFma( |
| 131 | "polly-target-througput-vector-fma", |
| 132 | cl::desc("A throughput of the processor floating-point arithmetic units " |
| 133 | "expressed in the number of vector fused multiply-add " |
| 134 | "instructions per clock cycle."), |
| 135 | cl::Hidden, cl::init(1), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 136 | |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 137 | static cl::list<int> |
| 138 | CacheLevelAssociativity("polly-target-cache-level-associativity", |
| 139 | cl::desc("The associativity of each cache level."), |
| 140 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, |
| 141 | cl::cat(PollyCategory)); |
| 142 | |
| 143 | static cl::list<int> CacheLevelSizes( |
| 144 | "polly-target-cache-level-sizes", |
| 145 | cl::desc("The size of each cache level specified in bytes."), cl::Hidden, |
| 146 | cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory)); |
| 147 | |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 148 | static cl::opt<int> FirstLevelDefaultTileSize( |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 149 | "polly-default-tile-size", |
| 150 | cl::desc("The default tile size (if not enough were provided by" |
| 151 | " --polly-tile-sizes)"), |
| 152 | cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Johannes Doerfert | c3958b2 | 2014-05-28 17:21:02 +0000 | [diff] [blame] | 153 | |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 154 | static cl::list<int> FirstLevelTileSizes( |
| 155 | "polly-tile-sizes", cl::desc("A tile size for each loop dimension, filled " |
| 156 | "with --polly-default-tile-size"), |
| 157 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory)); |
| 158 | |
| 159 | static cl::opt<bool> |
| 160 | SecondLevelTiling("polly-2nd-level-tiling", |
| 161 | cl::desc("Enable a 2nd level loop of loop tiling"), |
| 162 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 163 | |
| 164 | static cl::opt<int> SecondLevelDefaultTileSize( |
| 165 | "polly-2nd-level-default-tile-size", |
| 166 | cl::desc("The default 2nd-level tile size (if not enough were provided by" |
| 167 | " --polly-2nd-level-tile-sizes)"), |
| 168 | cl::Hidden, cl::init(16), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 169 | |
| 170 | static cl::list<int> |
| 171 | SecondLevelTileSizes("polly-2nd-level-tile-sizes", |
| 172 | cl::desc("A tile size for each loop dimension, filled " |
| 173 | "with --polly-default-tile-size"), |
| 174 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, |
| 175 | cl::cat(PollyCategory)); |
| 176 | |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 177 | static cl::opt<bool> RegisterTiling("polly-register-tiling", |
| 178 | cl::desc("Enable register tiling"), |
| 179 | cl::init(false), cl::ZeroOrMore, |
| 180 | cl::cat(PollyCategory)); |
| 181 | |
| 182 | static cl::opt<int> RegisterDefaultTileSize( |
| 183 | "polly-register-tiling-default-tile-size", |
| 184 | cl::desc("The default register tile size (if not enough were provided by" |
| 185 | " --polly-register-tile-sizes)"), |
| 186 | cl::Hidden, cl::init(2), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 187 | |
| 188 | static cl::list<int> |
| 189 | RegisterTileSizes("polly-register-tile-sizes", |
| 190 | cl::desc("A tile size for each loop dimension, filled " |
| 191 | "with --polly-register-tile-size"), |
| 192 | cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, |
| 193 | cl::cat(PollyCategory)); |
| 194 | |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 195 | static cl::opt<bool> |
| 196 | PMBasedOpts("polly-pattern-matching-based-opts", |
| 197 | cl::desc("Perform optimizations based on pattern matching"), |
| 198 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 199 | |
Roman Gareev | 5f99f86 | 2016-08-21 11:20:39 +0000 | [diff] [blame] | 200 | static cl::opt<bool> OptimizedScops( |
| 201 | "polly-optimized-scops", |
| 202 | cl::desc("Polly - Dump polyhedral description of Scops optimized with " |
| 203 | "the isl scheduling optimizer and the set of post-scheduling " |
| 204 | "transformations is applied on the schedule tree"), |
| 205 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 206 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 207 | /// Create an isl_union_set, which describes the isolate option based on |
| 208 | /// IsoalteDomain. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 209 | /// |
| 210 | /// @param IsolateDomain An isl_set whose last dimension is the only one that |
| 211 | /// should belong to the current band node. |
| 212 | static __isl_give isl_union_set * |
| 213 | getIsolateOptions(__isl_take isl_set *IsolateDomain) { |
| 214 | auto Dims = isl_set_dim(IsolateDomain, isl_dim_set); |
| 215 | auto *IsolateRelation = isl_map_from_domain(IsolateDomain); |
| 216 | IsolateRelation = isl_map_move_dims(IsolateRelation, isl_dim_out, 0, |
| 217 | isl_dim_in, Dims - 1, 1); |
| 218 | auto *IsolateOption = isl_map_wrap(IsolateRelation); |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 219 | auto *Id = isl_id_alloc(isl_set_get_ctx(IsolateOption), "isolate", nullptr); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 220 | return isl_union_set_from_set(isl_set_set_tuple_id(IsolateOption, Id)); |
| 221 | } |
| 222 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 223 | /// Create an isl_union_set, which describes the atomic option for the dimension |
| 224 | /// of the current node. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 225 | /// |
| 226 | /// It may help to reduce the size of generated code. |
| 227 | /// |
| 228 | /// @param Ctx An isl_ctx, which is used to create the isl_union_set. |
| 229 | static __isl_give isl_union_set *getAtomicOptions(__isl_take isl_ctx *Ctx) { |
| 230 | auto *Space = isl_space_set_alloc(Ctx, 0, 1); |
| 231 | auto *AtomicOption = isl_set_universe(Space); |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 232 | auto *Id = isl_id_alloc(Ctx, "atomic", nullptr); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 233 | return isl_union_set_from_set(isl_set_set_tuple_id(AtomicOption, Id)); |
| 234 | } |
| 235 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 236 | /// Make the last dimension of Set to take values from 0 to VectorWidth - 1. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 237 | /// |
| 238 | /// @param Set A set, which should be modified. |
| 239 | /// @param VectorWidth A parameter, which determines the constraint. |
| 240 | static __isl_give isl_set *addExtentConstraints(__isl_take isl_set *Set, |
| 241 | int VectorWidth) { |
| 242 | auto Dims = isl_set_dim(Set, isl_dim_set); |
| 243 | auto Space = isl_set_get_space(Set); |
| 244 | auto *LocalSpace = isl_local_space_from_space(Space); |
| 245 | auto *ExtConstr = |
| 246 | isl_constraint_alloc_inequality(isl_local_space_copy(LocalSpace)); |
| 247 | ExtConstr = isl_constraint_set_constant_si(ExtConstr, 0); |
| 248 | ExtConstr = |
| 249 | isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, 1); |
| 250 | Set = isl_set_add_constraint(Set, ExtConstr); |
| 251 | ExtConstr = isl_constraint_alloc_inequality(LocalSpace); |
| 252 | ExtConstr = isl_constraint_set_constant_si(ExtConstr, VectorWidth - 1); |
| 253 | ExtConstr = |
| 254 | isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, -1); |
| 255 | return isl_set_add_constraint(Set, ExtConstr); |
| 256 | } |
| 257 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 258 | /// Build the desired set of partial tile prefixes. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 259 | /// |
| 260 | /// We build a set of partial tile prefixes, which are prefixes of the vector |
| 261 | /// loop that have exactly VectorWidth iterations. |
| 262 | /// |
| 263 | /// 1. Get all prefixes of the vector loop. |
| 264 | /// 2. Extend it to a set, which has exactly VectorWidth iterations for |
| 265 | /// any prefix from the set that was built on the previous step. |
| 266 | /// 3. Subtract loop domain from it, project out the vector loop dimension and |
Roman Gareev | 76614d3 | 2016-05-31 11:22:21 +0000 | [diff] [blame] | 267 | /// get a set of prefixes, which don't have exactly VectorWidth iterations. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 268 | /// 4. Subtract it from all prefixes of the vector loop and get the desired |
| 269 | /// set. |
| 270 | /// |
| 271 | /// @param ScheduleRange A range of a map, which describes a prefix schedule |
| 272 | /// relation. |
| 273 | static __isl_give isl_set * |
| 274 | getPartialTilePrefixes(__isl_take isl_set *ScheduleRange, int VectorWidth) { |
| 275 | auto Dims = isl_set_dim(ScheduleRange, isl_dim_set); |
| 276 | auto *LoopPrefixes = isl_set_project_out(isl_set_copy(ScheduleRange), |
| 277 | isl_dim_set, Dims - 1, 1); |
| 278 | auto *ExtentPrefixes = |
| 279 | isl_set_add_dims(isl_set_copy(LoopPrefixes), isl_dim_set, 1); |
| 280 | ExtentPrefixes = addExtentConstraints(ExtentPrefixes, VectorWidth); |
| 281 | auto *BadPrefixes = isl_set_subtract(ExtentPrefixes, ScheduleRange); |
| 282 | BadPrefixes = isl_set_project_out(BadPrefixes, isl_dim_set, Dims - 1, 1); |
| 283 | return isl_set_subtract(LoopPrefixes, BadPrefixes); |
| 284 | } |
| 285 | |
| 286 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::isolateFullPartialTiles( |
| 287 | __isl_take isl_schedule_node *Node, int VectorWidth) { |
| 288 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
| 289 | Node = isl_schedule_node_child(Node, 0); |
| 290 | Node = isl_schedule_node_child(Node, 0); |
| 291 | auto *SchedRelUMap = isl_schedule_node_get_prefix_schedule_relation(Node); |
| 292 | auto *ScheduleRelation = isl_map_from_union_map(SchedRelUMap); |
| 293 | auto *ScheduleRange = isl_map_range(ScheduleRelation); |
| 294 | auto *IsolateDomain = getPartialTilePrefixes(ScheduleRange, VectorWidth); |
| 295 | auto *AtomicOption = getAtomicOptions(isl_set_get_ctx(IsolateDomain)); |
| 296 | auto *IsolateOption = getIsolateOptions(IsolateDomain); |
| 297 | Node = isl_schedule_node_parent(Node); |
| 298 | Node = isl_schedule_node_parent(Node); |
| 299 | auto *Options = isl_union_set_union(IsolateOption, AtomicOption); |
| 300 | Node = isl_schedule_node_band_set_ast_build_options(Node, Options); |
| 301 | return Node; |
| 302 | } |
| 303 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 304 | __isl_give isl_schedule_node * |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 305 | ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node, |
| 306 | unsigned DimToVectorize, |
| 307 | int VectorWidth) { |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 308 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 309 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 310 | auto Space = isl_schedule_node_band_get_space(Node); |
| 311 | auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set); |
| 312 | isl_space_free(Space); |
| 313 | assert(DimToVectorize < ScheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 314 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 315 | if (DimToVectorize > 0) { |
| 316 | Node = isl_schedule_node_band_split(Node, DimToVectorize); |
| 317 | Node = isl_schedule_node_child(Node, 0); |
| 318 | } |
| 319 | if (DimToVectorize < ScheduleDimensions - 1) |
| 320 | Node = isl_schedule_node_band_split(Node, 1); |
| 321 | Space = isl_schedule_node_band_get_space(Node); |
| 322 | auto Sizes = isl_multi_val_zero(Space); |
| 323 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 324 | Sizes = |
| 325 | isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth)); |
| 326 | Node = isl_schedule_node_band_tile(Node, Sizes); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 327 | Node = isolateFullPartialTiles(Node, VectorWidth); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 328 | Node = isl_schedule_node_child(Node, 0); |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 329 | // Make sure the "trivially vectorizable loop" is not unrolled. Otherwise, |
| 330 | // we will have troubles to match it in the backend. |
| 331 | Node = isl_schedule_node_band_set_ast_build_options( |
Tobias Grosser | fc490a9 | 2015-08-20 19:08:16 +0000 | [diff] [blame] | 332 | Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }")); |
| 333 | Node = isl_schedule_node_band_sink(Node); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 334 | Node = isl_schedule_node_child(Node, 0); |
Roman Gareev | 11001e1 | 2016-02-23 09:00:13 +0000 | [diff] [blame] | 335 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_leaf) |
| 336 | Node = isl_schedule_node_parent(Node); |
| 337 | isl_id *LoopMarker = isl_id_alloc(Ctx, "SIMD", nullptr); |
| 338 | Node = isl_schedule_node_insert_mark(Node, LoopMarker); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 339 | return Node; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Tobias Grosser | d891b54 | 2015-08-20 12:16:23 +0000 | [diff] [blame] | 342 | __isl_give isl_schedule_node * |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 343 | ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node, |
| 344 | const char *Identifier, ArrayRef<int> TileSizes, |
| 345 | int DefaultTileSize) { |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 346 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 347 | auto Space = isl_schedule_node_band_get_space(Node); |
| 348 | auto Dims = isl_space_dim(Space, isl_dim_set); |
| 349 | auto Sizes = isl_multi_val_zero(Space); |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 350 | std::string IdentifierString(Identifier); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 351 | for (unsigned i = 0; i < Dims; i++) { |
| 352 | auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize; |
| 353 | Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize)); |
| 354 | } |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 355 | auto TileLoopMarkerStr = IdentifierString + " - Tiles"; |
| 356 | isl_id *TileLoopMarker = |
| 357 | isl_id_alloc(Ctx, TileLoopMarkerStr.c_str(), nullptr); |
| 358 | Node = isl_schedule_node_insert_mark(Node, TileLoopMarker); |
| 359 | Node = isl_schedule_node_child(Node, 0); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 360 | Node = isl_schedule_node_band_tile(Node, Sizes); |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 361 | Node = isl_schedule_node_child(Node, 0); |
| 362 | auto PointLoopMarkerStr = IdentifierString + " - Points"; |
| 363 | isl_id *PointLoopMarker = |
| 364 | isl_id_alloc(Ctx, PointLoopMarkerStr.c_str(), nullptr); |
| 365 | Node = isl_schedule_node_insert_mark(Node, PointLoopMarker); |
| 366 | Node = isl_schedule_node_child(Node, 0); |
| 367 | return Node; |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Roman Gareev | b17b9a8 | 2016-06-12 17:20:05 +0000 | [diff] [blame] | 370 | __isl_give isl_schedule_node * |
| 371 | ScheduleTreeOptimizer::applyRegisterTiling(__isl_take isl_schedule_node *Node, |
| 372 | llvm::ArrayRef<int> TileSizes, |
| 373 | int DefaultTileSize) { |
| 374 | auto *Ctx = isl_schedule_node_get_ctx(Node); |
| 375 | Node = tileNode(Node, "Register tiling", TileSizes, DefaultTileSize); |
| 376 | Node = isl_schedule_node_band_set_ast_build_options( |
| 377 | Node, isl_union_set_read_from_str(Ctx, "{unroll[x]}")); |
| 378 | return Node; |
| 379 | } |
| 380 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 381 | bool ScheduleTreeOptimizer::isTileableBandNode( |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 382 | __isl_keep isl_schedule_node *Node) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 383 | if (isl_schedule_node_get_type(Node) != isl_schedule_node_band) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 384 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 385 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 386 | if (isl_schedule_node_n_children(Node) != 1) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 387 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 388 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 389 | if (!isl_schedule_node_band_get_permutable(Node)) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 390 | return false; |
Tobias Grosser | 44f19ac | 2011-07-05 22:15:53 +0000 | [diff] [blame] | 391 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 392 | auto Space = isl_schedule_node_band_get_space(Node); |
| 393 | auto Dims = isl_space_dim(Space, isl_dim_set); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 394 | isl_space_free(Space); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 395 | |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 396 | if (Dims <= 1) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 397 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 398 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 399 | auto Child = isl_schedule_node_get_child(Node, 0); |
| 400 | auto Type = isl_schedule_node_get_type(Child); |
| 401 | isl_schedule_node_free(Child); |
| 402 | |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 403 | if (Type != isl_schedule_node_leaf) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 404 | return false; |
| 405 | |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | __isl_give isl_schedule_node * |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 410 | ScheduleTreeOptimizer::standardBandOpts(__isl_take isl_schedule_node *Node, |
| 411 | void *User) { |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 412 | if (FirstLevelTiling) |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 413 | Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes, |
| 414 | FirstLevelDefaultTileSize); |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 415 | |
| 416 | if (SecondLevelTiling) |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 417 | Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes, |
| 418 | SecondLevelDefaultTileSize); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 419 | |
Roman Gareev | b17b9a8 | 2016-06-12 17:20:05 +0000 | [diff] [blame] | 420 | if (RegisterTiling) |
| 421 | Node = |
| 422 | applyRegisterTiling(Node, RegisterTileSizes, RegisterDefaultTileSize); |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 423 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 424 | if (PollyVectorizerChoice == VECTORIZER_NONE) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 425 | return Node; |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 426 | |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 427 | auto Space = isl_schedule_node_band_get_space(Node); |
| 428 | auto Dims = isl_space_dim(Space, isl_dim_set); |
| 429 | isl_space_free(Space); |
| 430 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 431 | for (int i = Dims - 1; i >= 0; i--) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 432 | if (isl_schedule_node_band_member_get_coincident(Node, i)) { |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 433 | Node = prevectSchedBand(Node, i, PrevectorWidth); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 434 | break; |
| 435 | } |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 436 | |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 437 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 440 | /// Check whether output dimensions of the map rely on the specified input |
| 441 | /// dimension. |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 442 | /// |
| 443 | /// @param IslMap The isl map to be considered. |
| 444 | /// @param DimNum The number of an input dimension to be checked. |
| 445 | static bool isInputDimUsed(__isl_take isl_map *IslMap, unsigned DimNum) { |
| 446 | auto *CheckedAccessRelation = |
| 447 | isl_map_project_out(isl_map_copy(IslMap), isl_dim_in, DimNum, 1); |
| 448 | CheckedAccessRelation = |
| 449 | isl_map_insert_dims(CheckedAccessRelation, isl_dim_in, DimNum, 1); |
| 450 | auto *InputDimsId = isl_map_get_tuple_id(IslMap, isl_dim_in); |
| 451 | CheckedAccessRelation = |
| 452 | isl_map_set_tuple_id(CheckedAccessRelation, isl_dim_in, InputDimsId); |
| 453 | InputDimsId = isl_map_get_tuple_id(IslMap, isl_dim_out); |
| 454 | CheckedAccessRelation = |
| 455 | isl_map_set_tuple_id(CheckedAccessRelation, isl_dim_out, InputDimsId); |
| 456 | auto res = !isl_map_is_equal(CheckedAccessRelation, IslMap); |
| 457 | isl_map_free(CheckedAccessRelation); |
| 458 | isl_map_free(IslMap); |
| 459 | return res; |
| 460 | } |
| 461 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 462 | /// Check if the SCoP statement could probably be optimized with analytical |
| 463 | /// modeling. |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 464 | /// |
| 465 | /// containsMatrMult tries to determine whether the following conditions |
| 466 | /// are true: |
| 467 | /// 1. all memory accesses of the statement will have stride 0 or 1, |
| 468 | /// if we interchange loops (switch the variable used in the inner |
| 469 | /// loop to the outer loop). |
| 470 | /// 2. all memory accesses of the statement except from the last one, are |
| 471 | /// read memory access and the last one is write memory access. |
Roman Gareev | 76614d3 | 2016-05-31 11:22:21 +0000 | [diff] [blame] | 472 | /// 3. all subscripts of the last memory access of the statement don't contain |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 473 | /// the variable used in the inner loop. |
| 474 | /// |
| 475 | /// @param PartialSchedule The PartialSchedule that contains a SCoP statement |
| 476 | /// to check. |
| 477 | static bool containsMatrMult(__isl_keep isl_map *PartialSchedule) { |
| 478 | auto InputDimsId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in); |
| 479 | auto *ScpStmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId)); |
| 480 | isl_id_free(InputDimsId); |
| 481 | if (ScpStmt->size() <= 1) |
| 482 | return false; |
| 483 | auto MemA = ScpStmt->begin(); |
| 484 | for (unsigned i = 0; i < ScpStmt->size() - 2 && MemA != ScpStmt->end(); |
| 485 | i++, MemA++) |
Roman Gareev | 76614d3 | 2016-05-31 11:22:21 +0000 | [diff] [blame] | 486 | if (!(*MemA)->isRead() || |
| 487 | ((*MemA)->isArrayKind() && |
| 488 | !((*MemA)->isStrideOne(isl_map_copy(PartialSchedule)) || |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 489 | (*MemA)->isStrideZero(isl_map_copy(PartialSchedule))))) |
| 490 | return false; |
| 491 | MemA++; |
Roman Gareev | 76614d3 | 2016-05-31 11:22:21 +0000 | [diff] [blame] | 492 | if (!(*MemA)->isWrite() || !(*MemA)->isArrayKind() || |
| 493 | !((*MemA)->isStrideOne(isl_map_copy(PartialSchedule)) || |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 494 | (*MemA)->isStrideZero(isl_map_copy(PartialSchedule)))) |
| 495 | return false; |
| 496 | auto DimNum = isl_map_dim(PartialSchedule, isl_dim_in); |
| 497 | return !isInputDimUsed((*MemA)->getAccessRelation(), DimNum - 1); |
| 498 | } |
| 499 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 500 | /// Circular shift of output dimensions of the integer map. |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 501 | /// |
| 502 | /// @param IslMap The isl map to be modified. |
| 503 | static __isl_give isl_map *circularShiftOutputDims(__isl_take isl_map *IslMap) { |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 504 | auto DimNum = isl_map_dim(IslMap, isl_dim_out); |
Roman Gareev | 4b8c7ae | 2016-06-03 18:46:29 +0000 | [diff] [blame] | 505 | if (DimNum == 0) |
| 506 | return IslMap; |
| 507 | auto InputDimsId = isl_map_get_tuple_id(IslMap, isl_dim_in); |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 508 | IslMap = isl_map_move_dims(IslMap, isl_dim_in, 0, isl_dim_out, DimNum - 1, 1); |
| 509 | IslMap = isl_map_move_dims(IslMap, isl_dim_out, 0, isl_dim_in, 0, 1); |
| 510 | return isl_map_set_tuple_id(IslMap, isl_dim_in, InputDimsId); |
| 511 | } |
| 512 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 513 | /// Permute two dimensions of the band node. |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 514 | /// |
| 515 | /// Permute FirstDim and SecondDim dimensions of the Node. |
| 516 | /// |
| 517 | /// @param Node The band node to be modified. |
| 518 | /// @param FirstDim The first dimension to be permuted. |
| 519 | /// @param SecondDim The second dimension to be permuted. |
| 520 | static __isl_give isl_schedule_node * |
| 521 | permuteBandNodeDimensions(__isl_take isl_schedule_node *Node, unsigned FirstDim, |
| 522 | unsigned SecondDim) { |
| 523 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band && |
| 524 | isl_schedule_node_band_n_member(Node) > std::max(FirstDim, SecondDim)); |
| 525 | auto PartialSchedule = isl_schedule_node_band_get_partial_schedule(Node); |
| 526 | auto PartialScheduleFirstDim = |
| 527 | isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, FirstDim); |
| 528 | auto PartialScheduleSecondDim = |
| 529 | isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, SecondDim); |
| 530 | PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff( |
| 531 | PartialSchedule, SecondDim, PartialScheduleFirstDim); |
| 532 | PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff( |
| 533 | PartialSchedule, FirstDim, PartialScheduleSecondDim); |
| 534 | Node = isl_schedule_node_delete(Node); |
| 535 | Node = isl_schedule_node_insert_partial_schedule(Node, PartialSchedule); |
| 536 | return Node; |
| 537 | } |
| 538 | |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 539 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::createMicroKernel( |
| 540 | __isl_take isl_schedule_node *Node, MicroKernelParamsTy MicroKernelParams) { |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 541 | applyRegisterTiling(Node, {MicroKernelParams.Mr, MicroKernelParams.Nr}, 1); |
| 542 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 543 | Node = permuteBandNodeDimensions(Node, 0, 1); |
| 544 | return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 547 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::createMacroKernel( |
| 548 | __isl_take isl_schedule_node *Node, MacroKernelParamsTy MacroKernelParams) { |
| 549 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
| 550 | if (MacroKernelParams.Mc == 1 && MacroKernelParams.Nc == 1 && |
| 551 | MacroKernelParams.Kc == 1) |
| 552 | return Node; |
| 553 | Node = tileNode( |
| 554 | Node, "1st level tiling", |
| 555 | {MacroKernelParams.Mc, MacroKernelParams.Nc, MacroKernelParams.Kc}, 1); |
| 556 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 557 | Node = permuteBandNodeDimensions(Node, 1, 2); |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 558 | Node = permuteBandNodeDimensions(Node, 0, 2); |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 559 | return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
| 560 | } |
| 561 | |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 562 | /// Get parameters of the BLIS micro kernel. |
| 563 | /// |
| 564 | /// We choose the Mr and Nr parameters of the micro kernel to be large enough |
| 565 | /// such that no stalls caused by the combination of latencies and dependencies |
| 566 | /// are introduced during the updates of the resulting matrix of the matrix |
| 567 | /// multiplication. However, they should also be as small as possible to |
| 568 | /// release more registers for entries of multiplied matrices. |
| 569 | /// |
| 570 | /// @param TTI Target Transform Info. |
| 571 | /// @return The structure of type MicroKernelParamsTy. |
| 572 | /// @see MicroKernelParamsTy |
| 573 | static struct MicroKernelParamsTy |
| 574 | getMicroKernelParams(const llvm::TargetTransformInfo *TTI) { |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 575 | assert(TTI && "The target transform info should be provided."); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 576 | |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 577 | // Nvec - Number of double-precision floating-point numbers that can be hold |
| 578 | // by a vector register. Use 2 by default. |
| 579 | auto Nvec = TTI->getRegisterBitWidth(true) / 64; |
| 580 | if (Nvec == 0) |
| 581 | Nvec = 2; |
| 582 | int Nr = |
| 583 | ceil(sqrt(Nvec * LatencyVectorFma * ThrougputVectorFma) / Nvec) * Nvec; |
| 584 | int Mr = ceil(Nvec * LatencyVectorFma * ThrougputVectorFma / Nr); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 585 | return {Mr, Nr}; |
| 586 | } |
| 587 | |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 588 | /// Get parameters of the BLIS macro kernel. |
| 589 | /// |
| 590 | /// During the computation of matrix multiplication, blocks of partitioned |
| 591 | /// matrices are mapped to different layers of the memory hierarchy. |
| 592 | /// To optimize data reuse, blocks should be ideally kept in cache between |
| 593 | /// iterations. Since parameters of the macro kernel determine sizes of these |
| 594 | /// blocks, there are upper and lower bounds on these parameters. |
| 595 | /// |
| 596 | /// @param MicroKernelParams Parameters of the micro-kernel |
| 597 | /// to be taken into account. |
| 598 | /// @return The structure of type MacroKernelParamsTy. |
| 599 | /// @see MacroKernelParamsTy |
| 600 | /// @see MicroKernelParamsTy |
| 601 | static struct MacroKernelParamsTy |
| 602 | getMacroKernelParams(const MicroKernelParamsTy &MicroKernelParams) { |
| 603 | // According to www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf, |
| 604 | // it requires information about the first two levels of a cache to determine |
| 605 | // all the parameters of a macro-kernel. It also checks that an associativity |
| 606 | // degree of a cache level is greater than two. Otherwise, another algorithm |
| 607 | // for determination of the parameters should be used. |
| 608 | if (!(MicroKernelParams.Mr > 0 && MicroKernelParams.Nr > 0 && |
| 609 | CacheLevelSizes.size() >= 2 && CacheLevelAssociativity.size() >= 2 && |
| 610 | CacheLevelSizes[0] > 0 && CacheLevelSizes[1] > 0 && |
| 611 | CacheLevelAssociativity[0] > 2 && CacheLevelAssociativity[1] > 2)) |
| 612 | return {1, 1, 1}; |
Roman Gareev | 15db81e | 2016-12-15 12:00:57 +0000 | [diff] [blame] | 613 | int Car = floor( |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 614 | (CacheLevelAssociativity[0] - 1) / |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 615 | (1 + static_cast<double>(MicroKernelParams.Nr) / MicroKernelParams.Mr)); |
Roman Gareev | 15db81e | 2016-12-15 12:00:57 +0000 | [diff] [blame] | 616 | int Kc = (Car * CacheLevelSizes[0]) / |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 617 | (MicroKernelParams.Mr * CacheLevelAssociativity[0] * 8); |
| 618 | double Cac = static_cast<double>(Kc * 8 * CacheLevelAssociativity[1]) / |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 619 | CacheLevelSizes[1]; |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 620 | int Mc = floor((CacheLevelAssociativity[1] - 2) / Cac); |
Roman Gareev | 15db81e | 2016-12-15 12:00:57 +0000 | [diff] [blame] | 621 | int Nc = floor(1 / Cac); |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 622 | return {Mc, Nc, Kc}; |
| 623 | } |
| 624 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 625 | /// Identify a memory access through the shape of its memory access relation. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 626 | /// |
| 627 | /// Identify the unique memory access in @p Stmt, that has an access relation |
| 628 | /// equal to @p ExpectedAccessRelation. |
| 629 | /// |
| 630 | /// @param Stmt The SCoP statement that contains the memory accesses under |
| 631 | /// consideration. |
| 632 | /// @param ExpectedAccessRelation The access relation that identifies |
| 633 | /// the memory access. |
| 634 | /// @return The memory access of @p Stmt whose memory access relation is equal |
| 635 | /// to @p ExpectedAccessRelation. nullptr in case there is no or more |
| 636 | /// than one such access. |
| 637 | MemoryAccess * |
| 638 | identifyAccessByAccessRelation(ScopStmt *Stmt, |
| 639 | __isl_take isl_map *ExpectedAccessRelation) { |
| 640 | if (isl_map_has_tuple_id(ExpectedAccessRelation, isl_dim_out)) |
| 641 | ExpectedAccessRelation = |
| 642 | isl_map_reset_tuple_id(ExpectedAccessRelation, isl_dim_out); |
| 643 | MemoryAccess *IdentifiedAccess = nullptr; |
| 644 | for (auto *Access : *Stmt) { |
| 645 | auto *AccessRelation = Access->getAccessRelation(); |
| 646 | AccessRelation = isl_map_reset_tuple_id(AccessRelation, isl_dim_out); |
| 647 | if (isl_map_is_equal(ExpectedAccessRelation, AccessRelation)) { |
| 648 | if (IdentifiedAccess) { |
| 649 | isl_map_free(AccessRelation); |
| 650 | isl_map_free(ExpectedAccessRelation); |
| 651 | return nullptr; |
| 652 | } |
| 653 | IdentifiedAccess = Access; |
| 654 | } |
| 655 | isl_map_free(AccessRelation); |
| 656 | } |
| 657 | isl_map_free(ExpectedAccessRelation); |
| 658 | return IdentifiedAccess; |
| 659 | } |
| 660 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 661 | /// Add constrains to @Dim dimension of @p ExtMap. |
| 662 | /// |
| 663 | /// If @ExtMap has the following form [O0, O1, O2]->[I1, I2, I3], |
| 664 | /// the following constraint will be added |
| 665 | /// Bound * OM <= IM <= Bound * (OM + 1) - 1, |
| 666 | /// where M is @p Dim and Bound is @p Bound. |
| 667 | /// |
| 668 | /// @param ExtMap The isl map to be modified. |
| 669 | /// @param Dim The output dimension to be modfied. |
| 670 | /// @param Bound The value that is used to specify the constraint. |
| 671 | /// @return The modified isl map |
| 672 | __isl_give isl_map * |
| 673 | addExtensionMapMatMulDimConstraint(__isl_take isl_map *ExtMap, unsigned Dim, |
| 674 | unsigned Bound) { |
| 675 | assert(Bound != 0); |
| 676 | auto *ExtMapSpace = isl_map_get_space(ExtMap); |
| 677 | auto *ConstrSpace = isl_local_space_from_space(ExtMapSpace); |
| 678 | auto *Constr = |
| 679 | isl_constraint_alloc_inequality(isl_local_space_copy(ConstrSpace)); |
| 680 | Constr = isl_constraint_set_coefficient_si(Constr, isl_dim_out, Dim, 1); |
| 681 | Constr = |
| 682 | isl_constraint_set_coefficient_si(Constr, isl_dim_in, Dim, Bound * (-1)); |
| 683 | ExtMap = isl_map_add_constraint(ExtMap, Constr); |
| 684 | Constr = isl_constraint_alloc_inequality(ConstrSpace); |
| 685 | Constr = isl_constraint_set_coefficient_si(Constr, isl_dim_out, Dim, -1); |
| 686 | Constr = isl_constraint_set_coefficient_si(Constr, isl_dim_in, Dim, Bound); |
| 687 | Constr = isl_constraint_set_constant_si(Constr, Bound - 1); |
| 688 | return isl_map_add_constraint(ExtMap, Constr); |
| 689 | } |
| 690 | |
| 691 | /// Create an access relation that is specific for matrix multiplication |
| 692 | /// pattern. |
| 693 | /// |
| 694 | /// Create an access relation of the following form: |
| 695 | /// { [O0, O1, O2]->[I1, I2, I3] : |
| 696 | /// FirstOutputDimBound * O0 <= I1 <= FirstOutputDimBound * (O0 + 1) - 1 |
| 697 | /// and SecondOutputDimBound * O1 <= I2 <= SecondOutputDimBound * (O1 + 1) - 1 |
| 698 | /// and ThirdOutputDimBound * O2 <= I3 <= ThirdOutputDimBound * (O2 + 1) - 1} |
| 699 | /// where FirstOutputDimBound is @p FirstOutputDimBound, |
| 700 | /// SecondOutputDimBound is @p SecondOutputDimBound, |
| 701 | /// ThirdOutputDimBound is @p ThirdOutputDimBound |
| 702 | /// |
| 703 | /// @param Ctx The isl context. |
| 704 | /// @param FirstOutputDimBound, |
| 705 | /// SecondOutputDimBound, |
| 706 | /// ThirdOutputDimBound The parameters of the access relation. |
| 707 | /// @return The specified access relation. |
| 708 | __isl_give isl_map *getMatMulExt(isl_ctx *Ctx, unsigned FirstOutputDimBound, |
| 709 | unsigned SecondOutputDimBound, |
| 710 | unsigned ThirdOutputDimBound) { |
| 711 | auto *NewRelSpace = isl_space_alloc(Ctx, 0, 3, 3); |
| 712 | auto *extensionMap = isl_map_universe(NewRelSpace); |
| 713 | if (!FirstOutputDimBound) |
| 714 | extensionMap = isl_map_fix_si(extensionMap, isl_dim_out, 0, 0); |
| 715 | else |
| 716 | extensionMap = addExtensionMapMatMulDimConstraint(extensionMap, 0, |
| 717 | FirstOutputDimBound); |
| 718 | if (!SecondOutputDimBound) |
| 719 | extensionMap = isl_map_fix_si(extensionMap, isl_dim_out, 1, 0); |
| 720 | else |
| 721 | extensionMap = addExtensionMapMatMulDimConstraint(extensionMap, 1, |
| 722 | SecondOutputDimBound); |
| 723 | if (!ThirdOutputDimBound) |
| 724 | extensionMap = isl_map_fix_si(extensionMap, isl_dim_out, 2, 0); |
| 725 | else |
| 726 | extensionMap = addExtensionMapMatMulDimConstraint(extensionMap, 2, |
| 727 | ThirdOutputDimBound); |
| 728 | return extensionMap; |
| 729 | } |
| 730 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 731 | /// Create an access relation that is specific to the matrix |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 732 | /// multiplication pattern. |
| 733 | /// |
| 734 | /// Create an access relation of the following form: |
| 735 | /// Stmt[O0, O1, O2]->[OI, OJ], |
| 736 | /// where I is @p I, J is @J |
| 737 | /// |
| 738 | /// @param Stmt The SCoP statement for which to generate the access relation. |
| 739 | /// @param I The index of the input dimension that is mapped to the first output |
| 740 | /// dimension. |
| 741 | /// @param J The index of the input dimension that is mapped to the second |
| 742 | /// output dimension. |
| 743 | /// @return The specified access relation. |
| 744 | __isl_give isl_map * |
| 745 | getMatMulPatternOriginalAccessRelation(ScopStmt *Stmt, unsigned I, unsigned J) { |
| 746 | auto *AccessRelSpace = isl_space_alloc(Stmt->getIslCtx(), 0, 3, 2); |
| 747 | auto *AccessRel = isl_map_universe(AccessRelSpace); |
| 748 | AccessRel = isl_map_equate(AccessRel, isl_dim_in, I, isl_dim_out, 0); |
| 749 | AccessRel = isl_map_equate(AccessRel, isl_dim_in, J, isl_dim_out, 1); |
| 750 | AccessRel = isl_map_set_tuple_id(AccessRel, isl_dim_in, Stmt->getDomainId()); |
| 751 | return AccessRel; |
| 752 | } |
| 753 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 754 | /// Identify the memory access that corresponds to the access to the second |
| 755 | /// operand of the matrix multiplication. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 756 | /// |
| 757 | /// Identify the memory access that corresponds to the access |
| 758 | /// to the matrix B of the matrix multiplication C = A x B. |
| 759 | /// |
| 760 | /// @param Stmt The SCoP statement that contains the memory accesses |
| 761 | /// under consideration. |
| 762 | /// @return The memory access of @p Stmt that corresponds to the access |
| 763 | /// to the second operand of the matrix multiplication. |
| 764 | MemoryAccess *identifyAccessA(ScopStmt *Stmt) { |
| 765 | auto *OriginalRel = getMatMulPatternOriginalAccessRelation(Stmt, 0, 2); |
| 766 | return identifyAccessByAccessRelation(Stmt, OriginalRel); |
| 767 | } |
| 768 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 769 | /// Identify the memory access that corresponds to the access to the first |
| 770 | /// operand of the matrix multiplication. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 771 | /// |
| 772 | /// Identify the memory access that corresponds to the access |
| 773 | /// to the matrix A of the matrix multiplication C = A x B. |
| 774 | /// |
| 775 | /// @param Stmt The SCoP statement that contains the memory accesses |
| 776 | /// under consideration. |
| 777 | /// @return The memory access of @p Stmt that corresponds to the access |
| 778 | /// to the first operand of the matrix multiplication. |
| 779 | MemoryAccess *identifyAccessB(ScopStmt *Stmt) { |
| 780 | auto *OriginalRel = getMatMulPatternOriginalAccessRelation(Stmt, 2, 1); |
| 781 | return identifyAccessByAccessRelation(Stmt, OriginalRel); |
| 782 | } |
| 783 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 784 | /// Create an access relation that is specific to |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 785 | /// the matrix multiplication pattern. |
| 786 | /// |
| 787 | /// Create an access relation of the following form: |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 788 | /// [O0, O1, O2, O3, O4, O5, O6, O7, O8] -> [O5 + K * OI, OJ], |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 789 | /// where K is @p Coeff, I is @p FirstDim, J is @p SecondDim. |
| 790 | /// |
| 791 | /// It can be used, for example, to create relations that helps to consequently |
| 792 | /// access elements of operands of a matrix multiplication after creation of |
| 793 | /// the BLIS micro and macro kernels. |
| 794 | /// |
| 795 | /// @see ScheduleTreeOptimizer::createMicroKernel |
| 796 | /// @see ScheduleTreeOptimizer::createMacroKernel |
| 797 | /// |
| 798 | /// Subsequently, the described access relation is applied to the range of |
| 799 | /// @p MapOldIndVar, that is used to map original induction variables to |
| 800 | /// the ones, which are produced by schedule transformations. It helps to |
| 801 | /// define relations using a new space and, at the same time, keep them |
| 802 | /// in the original one. |
| 803 | /// |
| 804 | /// @param MapOldIndVar The relation, which maps original induction variables |
| 805 | /// to the ones, which are produced by schedule |
| 806 | /// transformations. |
| 807 | /// @param Coeff The coefficient that is used to define the specified access |
| 808 | /// relation. |
| 809 | /// @param FirstDim, SecondDim The input dimensions that are used to define |
| 810 | /// the specified access relation. |
| 811 | /// @return The specified access relation. |
| 812 | __isl_give isl_map *getMatMulAccRel(__isl_take isl_map *MapOldIndVar, |
| 813 | unsigned Coeff, unsigned FirstDim, |
| 814 | unsigned SecondDim) { |
| 815 | auto *Ctx = isl_map_get_ctx(MapOldIndVar); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 816 | auto *AccessRelSpace = isl_space_alloc(Ctx, 0, 9, 2); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 817 | auto *AccessRel = isl_map_universe(isl_space_copy(AccessRelSpace)); |
| 818 | auto *ConstrSpace = isl_local_space_from_space(AccessRelSpace); |
| 819 | auto *Constr = isl_constraint_alloc_equality(ConstrSpace); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 820 | Constr = isl_constraint_set_coefficient_si(Constr, isl_dim_out, 0, -1); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 821 | Constr = isl_constraint_set_coefficient_si(Constr, isl_dim_in, 5, 1); |
| 822 | Constr = |
| 823 | isl_constraint_set_coefficient_si(Constr, isl_dim_in, FirstDim, Coeff); |
| 824 | AccessRel = isl_map_add_constraint(AccessRel, Constr); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 825 | AccessRel = isl_map_equate(AccessRel, isl_dim_in, SecondDim, isl_dim_out, 1); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 826 | return isl_map_apply_range(MapOldIndVar, AccessRel); |
| 827 | } |
| 828 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 829 | __isl_give isl_schedule_node * |
| 830 | createExtensionNode(__isl_take isl_schedule_node *Node, |
| 831 | __isl_take isl_map *ExtensionMap) { |
| 832 | auto *Extension = isl_union_map_from_map(ExtensionMap); |
| 833 | auto *NewNode = isl_schedule_node_from_extension(Extension); |
| 834 | return isl_schedule_node_graft_before(Node, NewNode); |
| 835 | } |
| 836 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 837 | /// Apply the packing transformation. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 838 | /// |
| 839 | /// The packing transformation can be described as a data-layout |
| 840 | /// transformation that requires to introduce a new array, copy data |
| 841 | /// to the array, and change memory access locations of the compute kernel |
| 842 | /// to reference the array. |
| 843 | /// |
| 844 | /// @param Node The schedule node to be optimized. |
| 845 | /// @param MapOldIndVar The relation, which maps original induction variables |
| 846 | /// to the ones, which are produced by schedule |
| 847 | /// transformations. |
| 848 | /// @param MicroParams, MacroParams Parameters of the BLIS kernel |
| 849 | /// to be taken into account. |
| 850 | /// @return The optimized schedule node. |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 851 | static __isl_give isl_schedule_node *optimizeDataLayoutMatrMulPattern( |
| 852 | __isl_take isl_schedule_node *Node, __isl_take isl_map *MapOldIndVar, |
| 853 | MicroKernelParamsTy MicroParams, MacroKernelParamsTy MacroParams) { |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 854 | // Check whether memory accesses of the SCoP statement correspond to |
| 855 | // the matrix multiplication pattern and if this is true, obtain them. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 856 | auto InputDimsId = isl_map_get_tuple_id(MapOldIndVar, isl_dim_in); |
| 857 | auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId)); |
| 858 | isl_id_free(InputDimsId); |
| 859 | MemoryAccess *MemAccessA = identifyAccessA(Stmt); |
| 860 | MemoryAccess *MemAccessB = identifyAccessB(Stmt); |
| 861 | if (!MemAccessA || !MemAccessB) { |
| 862 | isl_map_free(MapOldIndVar); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 863 | return Node; |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 864 | } |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 865 | |
| 866 | // Create a copy statement that corresponds to the memory access to the |
| 867 | // matrix B, the second operand of the matrix multiplication. |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 868 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 869 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 870 | Node = isl_schedule_node_parent(Node); |
| 871 | Node = isl_schedule_node_child(isl_schedule_node_band_split(Node, 2), 0); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 872 | auto *AccRel = |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 873 | getMatMulAccRel(isl_map_copy(MapOldIndVar), MacroParams.Kc, 3, 7); |
| 874 | unsigned FirstDimSize = MacroParams.Nc * MacroParams.Kc / MicroParams.Nr; |
| 875 | unsigned SecondDimSize = MicroParams.Nr; |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 876 | auto *SAI = Stmt->getParent()->createScopArrayInfo( |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 877 | MemAccessB->getElementType(), "Packed_B", {FirstDimSize, SecondDimSize}); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 878 | AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId()); |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 879 | auto *OldAcc = MemAccessB->getAccessRelation(); |
| 880 | MemAccessB->setNewAccessRelation(AccRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 881 | auto *ExtMap = |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 882 | getMatMulExt(Stmt->getIslCtx(), 0, MacroParams.Nc, MacroParams.Kc); |
| 883 | isl_map_move_dims(ExtMap, isl_dim_out, 0, isl_dim_in, 0, 1); |
| 884 | isl_map_move_dims(ExtMap, isl_dim_in, 2, isl_dim_out, 0, 1); |
| 885 | ExtMap = isl_map_project_out(ExtMap, isl_dim_in, 2, 1); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 886 | auto *Domain = Stmt->getDomain(); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 887 | |
| 888 | // Restrict the domains of the copy statements to only execute when also its |
| 889 | // originating statement is executed. |
| 890 | auto *DomainId = isl_set_get_tuple_id(Domain); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 891 | auto *NewStmt = Stmt->getParent()->addScopStmt( |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 892 | OldAcc, MemAccessB->getAccessRelation(), isl_set_copy(Domain)); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 893 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, isl_id_copy(DomainId)); |
| 894 | ExtMap = isl_map_intersect_range(ExtMap, isl_set_copy(Domain)); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 895 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId()); |
| 896 | Node = createExtensionNode(Node, ExtMap); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 897 | |
| 898 | // Create a copy statement that corresponds to the memory access |
| 899 | // to the matrix A, the first operand of the matrix multiplication. |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 900 | Node = isl_schedule_node_child(Node, 0); |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 901 | AccRel = getMatMulAccRel(MapOldIndVar, MacroParams.Kc, 4, 6); |
| 902 | FirstDimSize = MacroParams.Mc * MacroParams.Kc / MicroParams.Mr; |
| 903 | SecondDimSize = MicroParams.Mr; |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 904 | SAI = Stmt->getParent()->createScopArrayInfo( |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 905 | MemAccessA->getElementType(), "Packed_A", {FirstDimSize, SecondDimSize}); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 906 | AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId()); |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 907 | OldAcc = MemAccessA->getAccessRelation(); |
| 908 | MemAccessA->setNewAccessRelation(AccRel); |
| 909 | ExtMap = getMatMulExt(Stmt->getIslCtx(), MacroParams.Mc, 0, MacroParams.Kc); |
| 910 | isl_map_move_dims(ExtMap, isl_dim_out, 0, isl_dim_in, 0, 1); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 911 | isl_map_move_dims(ExtMap, isl_dim_in, 2, isl_dim_out, 0, 1); |
| 912 | NewStmt = Stmt->getParent()->addScopStmt( |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 913 | OldAcc, MemAccessA->getAccessRelation(), isl_set_copy(Domain)); |
| 914 | |
| 915 | // Restrict the domains of the copy statements to only execute when also its |
| 916 | // originating statement is executed. |
| 917 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, DomainId); |
| 918 | ExtMap = isl_map_intersect_range(ExtMap, Domain); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 919 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId()); |
| 920 | Node = createExtensionNode(Node, ExtMap); |
| 921 | Node = isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
| 922 | return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 925 | /// Get a relation mapping induction variables produced by schedule |
| 926 | /// transformations to the original ones. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 927 | /// |
| 928 | /// @param Node The schedule node produced as the result of creation |
| 929 | /// of the BLIS kernels. |
| 930 | /// @param MicroKernelParams, MacroKernelParams Parameters of the BLIS kernel |
| 931 | /// to be taken into account. |
| 932 | /// @return The relation mapping original induction variables to the ones |
| 933 | /// produced by schedule transformation. |
| 934 | /// @see ScheduleTreeOptimizer::createMicroKernel |
| 935 | /// @see ScheduleTreeOptimizer::createMacroKernel |
| 936 | /// @see getMacroKernelParams |
| 937 | __isl_give isl_map * |
| 938 | getInductionVariablesSubstitution(__isl_take isl_schedule_node *Node, |
| 939 | MicroKernelParamsTy MicroKernelParams, |
| 940 | MacroKernelParamsTy MacroKernelParams) { |
| 941 | auto *Child = isl_schedule_node_get_child(Node, 0); |
| 942 | auto *UnMapOldIndVar = isl_schedule_node_get_prefix_schedule_union_map(Child); |
| 943 | isl_schedule_node_free(Child); |
| 944 | auto *MapOldIndVar = isl_map_from_union_map(UnMapOldIndVar); |
| 945 | if (isl_map_dim(MapOldIndVar, isl_dim_out) > 9) |
| 946 | MapOldIndVar = |
| 947 | isl_map_project_out(MapOldIndVar, isl_dim_out, 0, |
| 948 | isl_map_dim(MapOldIndVar, isl_dim_out) - 9); |
| 949 | return MapOldIndVar; |
| 950 | } |
| 951 | |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 952 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeMatMulPattern( |
| 953 | __isl_take isl_schedule_node *Node, const llvm::TargetTransformInfo *TTI) { |
| 954 | assert(TTI && "The target transform info should be provided."); |
| 955 | auto MicroKernelParams = getMicroKernelParams(TTI); |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 956 | auto MacroKernelParams = getMacroKernelParams(MicroKernelParams); |
| 957 | Node = createMacroKernel(Node, MacroKernelParams); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 958 | Node = createMicroKernel(Node, MicroKernelParams); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 959 | if (MacroKernelParams.Mc == 1 || MacroKernelParams.Nc == 1 || |
| 960 | MacroKernelParams.Kc == 1) |
| 961 | return Node; |
| 962 | auto *MapOldIndVar = getInductionVariablesSubstitution( |
| 963 | Node, MicroKernelParams, MacroKernelParams); |
| 964 | if (!MapOldIndVar) |
| 965 | return Node; |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 966 | return optimizeDataLayoutMatrMulPattern(Node, MapOldIndVar, MicroKernelParams, |
| 967 | MacroKernelParams); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 970 | bool ScheduleTreeOptimizer::isMatrMultPattern( |
| 971 | __isl_keep isl_schedule_node *Node) { |
| 972 | auto *PartialSchedule = |
| 973 | isl_schedule_node_band_get_partial_schedule_union_map(Node); |
Roman Gareev | 397a34a | 2016-06-22 12:11:30 +0000 | [diff] [blame] | 974 | if (isl_schedule_node_band_n_member(Node) != 3 || |
| 975 | isl_union_map_n_map(PartialSchedule) != 1) { |
| 976 | isl_union_map_free(PartialSchedule); |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 977 | return false; |
| 978 | } |
Roman Gareev | 397a34a | 2016-06-22 12:11:30 +0000 | [diff] [blame] | 979 | auto *NewPartialSchedule = isl_map_from_union_map(PartialSchedule); |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 980 | NewPartialSchedule = circularShiftOutputDims(NewPartialSchedule); |
| 981 | if (containsMatrMult(NewPartialSchedule)) { |
| 982 | isl_map_free(NewPartialSchedule); |
| 983 | return true; |
| 984 | } |
| 985 | isl_map_free(NewPartialSchedule); |
| 986 | return false; |
| 987 | } |
| 988 | |
| 989 | __isl_give isl_schedule_node * |
| 990 | ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node, |
| 991 | void *User) { |
| 992 | if (!isTileableBandNode(Node)) |
| 993 | return Node; |
| 994 | |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 995 | if (PMBasedOpts && User && isMatrMultPattern(Node)) { |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 996 | DEBUG(dbgs() << "The matrix multiplication pattern was detected\n"); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 997 | const llvm::TargetTransformInfo *TTI; |
| 998 | TTI = static_cast<const llvm::TargetTransformInfo *>(User); |
| 999 | Node = optimizeMatMulPattern(Node, TTI); |
| 1000 | } |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 1001 | |
| 1002 | return standardBandOpts(Node, User); |
| 1003 | } |
| 1004 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1005 | __isl_give isl_schedule * |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1006 | ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule, |
| 1007 | const llvm::TargetTransformInfo *TTI) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 1008 | isl_schedule_node *Root = isl_schedule_get_root(Schedule); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1009 | Root = optimizeScheduleNode(Root, TTI); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1010 | isl_schedule_free(Schedule); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1011 | auto S = isl_schedule_node_get_schedule(Root); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 1012 | isl_schedule_node_free(Root); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1013 | return S; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1016 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode( |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1017 | __isl_take isl_schedule_node *Node, const llvm::TargetTransformInfo *TTI) { |
| 1018 | Node = isl_schedule_node_map_descendant_bottom_up( |
| 1019 | Node, optimizeBand, const_cast<void *>(static_cast<const void *>(TTI))); |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1020 | return Node; |
| 1021 | } |
| 1022 | |
| 1023 | bool ScheduleTreeOptimizer::isProfitableSchedule( |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1024 | Scop &S, __isl_keep isl_schedule *NewSchedule) { |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1025 | // To understand if the schedule has been optimized we check if the schedule |
| 1026 | // has changed at all. |
| 1027 | // TODO: We can improve this by tracking if any necessarily beneficial |
| 1028 | // transformations have been performed. This can e.g. be tiling, loop |
| 1029 | // interchange, or ...) We can track this either at the place where the |
| 1030 | // transformation has been performed or, in case of automatic ILP based |
| 1031 | // optimizations, by comparing (yet to be defined) performance metrics |
| 1032 | // before/after the scheduling optimizer |
| 1033 | // (e.g., #stride-one accesses) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1034 | if (S.containsExtensionNode(NewSchedule)) |
| 1035 | return true; |
| 1036 | auto *NewScheduleMap = isl_schedule_get_map(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1037 | isl_union_map *OldSchedule = S.getSchedule(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1038 | assert(OldSchedule && "Only IslScheduleOptimizer can insert extension nodes " |
| 1039 | "that make Scop::getSchedule() return nullptr."); |
| 1040 | bool changed = !isl_union_map_is_equal(OldSchedule, NewScheduleMap); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1041 | isl_union_map_free(OldSchedule); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1042 | isl_union_map_free(NewScheduleMap); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1043 | return changed; |
| 1044 | } |
| 1045 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1046 | namespace { |
| 1047 | class IslScheduleOptimizer : public ScopPass { |
| 1048 | public: |
| 1049 | static char ID; |
| 1050 | explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; } |
| 1051 | |
| 1052 | ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); } |
| 1053 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1054 | /// Optimize the schedule of the SCoP @p S. |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1055 | bool runOnScop(Scop &S) override; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1056 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1057 | /// Print the new schedule for the SCoP @p S. |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 1058 | void printScop(raw_ostream &OS, Scop &S) const override; |
| 1059 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1060 | /// Register all analyses and transformation required. |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 1061 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1062 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1063 | /// Release the internal memory. |
Johannes Doerfert | 0f37630 | 2015-09-27 15:42:28 +0000 | [diff] [blame] | 1064 | void releaseMemory() override { |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1065 | isl_schedule_free(LastSchedule); |
| 1066 | LastSchedule = nullptr; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1067 | } |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 1068 | |
| 1069 | private: |
| 1070 | isl_schedule *LastSchedule; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1071 | }; |
Tobias Grosser | 522478d | 2016-06-23 22:17:27 +0000 | [diff] [blame] | 1072 | } // namespace |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1073 | |
| 1074 | char IslScheduleOptimizer::ID = 0; |
| 1075 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1076 | bool IslScheduleOptimizer::runOnScop(Scop &S) { |
Johannes Doerfert | 6f7921f | 2015-02-14 12:02:24 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Skip empty SCoPs but still allow code generation as it will delete the |
| 1079 | // loops present but not needed. |
| 1080 | if (S.getSize() == 0) { |
| 1081 | S.markAsOptimized(); |
| 1082 | return false; |
| 1083 | } |
| 1084 | |
Hongbin Zheng | 2a79885 | 2016-03-03 08:15:33 +0000 | [diff] [blame] | 1085 | const Dependences &D = |
| 1086 | getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1087 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1088 | if (!D.hasValidDependences()) |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 1089 | return false; |
| 1090 | |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 1091 | isl_schedule_free(LastSchedule); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 1092 | LastSchedule = nullptr; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 1093 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1094 | // Build input data. |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1095 | int ValidityKinds = |
| 1096 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1097 | int ProximityKinds; |
| 1098 | |
| 1099 | if (OptimizeDeps == "all") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1100 | ProximityKinds = |
| 1101 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1102 | else if (OptimizeDeps == "raw") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1103 | ProximityKinds = Dependences::TYPE_RAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1104 | else { |
| 1105 | errs() << "Do not know how to optimize for '" << OptimizeDeps << "'" |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1106 | << " Falling back to optimizing all dependences.\n"; |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1107 | ProximityKinds = |
| 1108 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1111 | isl_union_set *Domain = S.getDomains(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1112 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 1113 | if (!Domain) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1114 | return false; |
| 1115 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1116 | isl_union_map *Validity = D.getDependences(ValidityKinds); |
| 1117 | isl_union_map *Proximity = D.getDependences(ProximityKinds); |
Tobias Grosser | 8a50702 | 2012-03-16 11:51:41 +0000 | [diff] [blame] | 1118 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 1119 | // Simplify the dependences by removing the constraints introduced by the |
| 1120 | // domains. This can speed up the scheduling time significantly, as large |
| 1121 | // constant coefficients will be removed from the dependences. The |
| 1122 | // introduction of some additional dependences reduces the possible |
| 1123 | // transformations, but in most cases, such transformation do not seem to be |
| 1124 | // interesting anyway. In some cases this option may stop the scheduler to |
| 1125 | // find any schedule. |
| 1126 | if (SimplifyDeps == "yes") { |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 1127 | Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain)); |
| 1128 | Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain)); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1129 | Proximity = |
| 1130 | isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 1131 | Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 1132 | } else if (SimplifyDeps != "no") { |
| 1133 | errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' " |
| 1134 | "or 'no'. Falling back to default: 'yes'\n"; |
| 1135 | } |
| 1136 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1137 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
Tobias Grosser | 01aea58 | 2014-10-22 23:16:28 +0000 | [diff] [blame] | 1138 | DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n"); |
| 1139 | DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n"); |
| 1140 | DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n"); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1141 | |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1142 | unsigned IslSerializeSCCs; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1143 | |
| 1144 | if (FusionStrategy == "max") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1145 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1146 | } else if (FusionStrategy == "min") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1147 | IslSerializeSCCs = 1; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1148 | } else { |
| 1149 | errs() << "warning: Unknown fusion strategy. Falling back to maximal " |
| 1150 | "fusion.\n"; |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1151 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 1154 | int IslMaximizeBands; |
| 1155 | |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 1156 | if (MaximizeBandDepth == "yes") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 1157 | IslMaximizeBands = 1; |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 1158 | } else if (MaximizeBandDepth == "no") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 1159 | IslMaximizeBands = 0; |
| 1160 | } else { |
| 1161 | errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'" |
| 1162 | " or 'no'. Falling back to default: 'yes'\n"; |
| 1163 | IslMaximizeBands = 1; |
| 1164 | } |
| 1165 | |
Michael Kruse | 315aa32 | 2016-05-02 11:35:27 +0000 | [diff] [blame] | 1166 | int IslOuterCoincidence; |
| 1167 | |
| 1168 | if (OuterCoincidence == "yes") { |
| 1169 | IslOuterCoincidence = 1; |
| 1170 | } else if (OuterCoincidence == "no") { |
| 1171 | IslOuterCoincidence = 0; |
| 1172 | } else { |
| 1173 | errs() << "warning: Option -polly-opt-outer-coincidence should either be " |
| 1174 | "'yes' or 'no'. Falling back to default: 'no'\n"; |
| 1175 | IslOuterCoincidence = 0; |
| 1176 | } |
| 1177 | |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1178 | isl_ctx *Ctx = S.getIslCtx(); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 1179 | |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1180 | isl_options_set_schedule_outer_coincidence(Ctx, IslOuterCoincidence); |
| 1181 | isl_options_set_schedule_serialize_sccs(Ctx, IslSerializeSCCs); |
| 1182 | isl_options_set_schedule_maximize_band_depth(Ctx, IslMaximizeBands); |
| 1183 | isl_options_set_schedule_max_constant_term(Ctx, MaxConstantTerm); |
| 1184 | isl_options_set_schedule_max_coefficient(Ctx, MaxCoefficient); |
| 1185 | isl_options_set_tile_scale_tile_loops(Ctx, 0); |
| 1186 | |
Tobias Grosser | 3898a04 | 2016-06-30 20:42:58 +0000 | [diff] [blame] | 1187 | auto OnErrorStatus = isl_options_get_on_error(Ctx); |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1188 | isl_options_set_on_error(Ctx, ISL_ON_ERROR_CONTINUE); |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 1189 | |
| 1190 | isl_schedule_constraints *ScheduleConstraints; |
| 1191 | ScheduleConstraints = isl_schedule_constraints_on_domain(Domain); |
| 1192 | ScheduleConstraints = |
| 1193 | isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity); |
| 1194 | ScheduleConstraints = isl_schedule_constraints_set_validity( |
| 1195 | ScheduleConstraints, isl_union_map_copy(Validity)); |
| 1196 | ScheduleConstraints = |
| 1197 | isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 1198 | isl_schedule *Schedule; |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 1199 | Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints); |
Tobias Grosser | 3898a04 | 2016-06-30 20:42:58 +0000 | [diff] [blame] | 1200 | isl_options_set_on_error(Ctx, OnErrorStatus); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 1201 | |
| 1202 | // In cases the scheduler is not able to optimize the code, we just do not |
| 1203 | // touch the schedule. |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 1204 | if (!Schedule) |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 1205 | return false; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1206 | |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 1207 | DEBUG({ |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1208 | auto *P = isl_printer_to_str(Ctx); |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 1209 | P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); |
| 1210 | P = isl_printer_print_schedule(P, Schedule); |
Michael Kruse | 79c0173 | 2016-12-12 14:51:06 +0000 | [diff] [blame] | 1211 | auto *str = isl_printer_get_str(P); |
| 1212 | dbgs() << "NewScheduleTree: \n" << str << "\n"; |
| 1213 | free(str); |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 1214 | isl_printer_free(P); |
| 1215 | }); |
Tobias Grosser | 4d63b9d | 2012-02-20 08:41:21 +0000 | [diff] [blame] | 1216 | |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1217 | Function &F = S.getFunction(); |
| 1218 | auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| 1219 | isl_schedule *NewSchedule = |
| 1220 | ScheduleTreeOptimizer::optimizeSchedule(Schedule, TTI); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1221 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1222 | if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewSchedule)) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1223 | isl_schedule_free(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1224 | return false; |
| 1225 | } |
| 1226 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1227 | S.setScheduleTree(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1228 | S.markAsOptimized(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1229 | |
Roman Gareev | 5f99f86 | 2016-08-21 11:20:39 +0000 | [diff] [blame] | 1230 | if (OptimizedScops) |
| 1231 | S.dump(); |
| 1232 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1233 | return false; |
| 1234 | } |
| 1235 | |
Johannes Doerfert | 3fe584d | 2015-03-01 18:40:25 +0000 | [diff] [blame] | 1236 | void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const { |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 1237 | isl_printer *p; |
| 1238 | char *ScheduleStr; |
| 1239 | |
| 1240 | OS << "Calculated schedule:\n"; |
| 1241 | |
| 1242 | if (!LastSchedule) { |
| 1243 | OS << "n/a\n"; |
| 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule)); |
| 1248 | p = isl_printer_print_schedule(p, LastSchedule); |
| 1249 | ScheduleStr = isl_printer_get_str(p); |
| 1250 | isl_printer_free(p); |
| 1251 | |
| 1252 | OS << ScheduleStr << "\n"; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1255 | void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1256 | ScopPass::getAnalysisUsage(AU); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 1257 | AU.addRequired<DependenceInfo>(); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1258 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1261 | Pass *polly::createIslScheduleOptimizerPass() { |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1262 | return new IslScheduleOptimizer(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1263 | } |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1264 | |
| 1265 | INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", |
| 1266 | "Polly - Optimize schedule of SCoP", false, false); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 1267 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 1268 | INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1269 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1270 | INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", |
| 1271 | "Polly - Optimize schedule of SCoP", false, false) |