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