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 | /// |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 241 | /// @param IsolateDomain An isl_set whose @p OutDimsNum last dimensions should |
| 242 | /// belong to the current band node. |
| 243 | /// @param OutDimsNum A number of dimensions that should belong to |
| 244 | /// the current band node. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 245 | static __isl_give isl_union_set * |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 246 | getIsolateOptions(__isl_take isl_set *IsolateDomain, unsigned OutDimsNum) { |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 247 | auto Dims = isl_set_dim(IsolateDomain, isl_dim_set); |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 248 | assert(OutDimsNum <= Dims && |
| 249 | "The isl_set IsolateDomain is used to describe the range of schedule " |
| 250 | "dimensions values, which should be isolated. Consequently, the " |
| 251 | "number of its dimensions should be greater than or equal to the " |
| 252 | "number of the schedule dimensions."); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 253 | auto *IsolateRelation = isl_map_from_domain(IsolateDomain); |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 254 | IsolateRelation = |
| 255 | isl_map_move_dims(IsolateRelation, isl_dim_out, 0, isl_dim_in, |
| 256 | Dims - OutDimsNum, OutDimsNum); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 257 | auto *IsolateOption = isl_map_wrap(IsolateRelation); |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 258 | auto *Id = isl_id_alloc(isl_set_get_ctx(IsolateOption), "isolate", nullptr); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 259 | return isl_union_set_from_set(isl_set_set_tuple_id(IsolateOption, Id)); |
| 260 | } |
| 261 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 262 | /// Create an isl_union_set, which describes the atomic option for the dimension |
| 263 | /// of the current node. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 264 | /// |
| 265 | /// It may help to reduce the size of generated code. |
| 266 | /// |
| 267 | /// @param Ctx An isl_ctx, which is used to create the isl_union_set. |
Roman Gareev | afcf026 | 2017-02-11 07:14:37 +0000 | [diff] [blame] | 268 | static __isl_give isl_union_set *getAtomicOptions(isl_ctx *Ctx) { |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 269 | auto *Space = isl_space_set_alloc(Ctx, 0, 1); |
| 270 | auto *AtomicOption = isl_set_universe(Space); |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 271 | auto *Id = isl_id_alloc(Ctx, "atomic", nullptr); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 272 | return isl_union_set_from_set(isl_set_set_tuple_id(AtomicOption, Id)); |
| 273 | } |
| 274 | |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 275 | /// Create an isl_union_set, which describes the option of the form |
| 276 | /// [isolate[] -> unroll[x]]. |
| 277 | /// |
| 278 | /// @param Ctx An isl_ctx, which is used to create the isl_union_set. |
| 279 | static __isl_give isl_union_set *getUnrollIsolatedSetOptions(isl_ctx *Ctx) { |
| 280 | auto *Space = isl_space_alloc(Ctx, 0, 0, 1); |
| 281 | auto *UnrollIsolatedSetOption = isl_map_universe(Space); |
| 282 | auto *DimInId = isl_id_alloc(Ctx, "isolate", nullptr); |
| 283 | auto *DimOutId = isl_id_alloc(Ctx, "unroll", nullptr); |
| 284 | UnrollIsolatedSetOption = |
| 285 | isl_map_set_tuple_id(UnrollIsolatedSetOption, isl_dim_in, DimInId); |
| 286 | UnrollIsolatedSetOption = |
| 287 | isl_map_set_tuple_id(UnrollIsolatedSetOption, isl_dim_out, DimOutId); |
| 288 | return isl_union_set_from_set(isl_map_wrap(UnrollIsolatedSetOption)); |
| 289 | } |
| 290 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 291 | /// 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] | 292 | /// |
| 293 | /// @param Set A set, which should be modified. |
| 294 | /// @param VectorWidth A parameter, which determines the constraint. |
| 295 | static __isl_give isl_set *addExtentConstraints(__isl_take isl_set *Set, |
| 296 | int VectorWidth) { |
| 297 | auto Dims = isl_set_dim(Set, isl_dim_set); |
| 298 | auto Space = isl_set_get_space(Set); |
| 299 | auto *LocalSpace = isl_local_space_from_space(Space); |
| 300 | auto *ExtConstr = |
| 301 | isl_constraint_alloc_inequality(isl_local_space_copy(LocalSpace)); |
| 302 | ExtConstr = isl_constraint_set_constant_si(ExtConstr, 0); |
| 303 | ExtConstr = |
| 304 | isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, 1); |
| 305 | Set = isl_set_add_constraint(Set, ExtConstr); |
| 306 | ExtConstr = isl_constraint_alloc_inequality(LocalSpace); |
| 307 | ExtConstr = isl_constraint_set_constant_si(ExtConstr, VectorWidth - 1); |
| 308 | ExtConstr = |
| 309 | isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, -1); |
| 310 | return isl_set_add_constraint(Set, ExtConstr); |
| 311 | } |
| 312 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 313 | /// Build the desired set of partial tile prefixes. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 314 | /// |
| 315 | /// We build a set of partial tile prefixes, which are prefixes of the vector |
| 316 | /// loop that have exactly VectorWidth iterations. |
| 317 | /// |
| 318 | /// 1. Get all prefixes of the vector loop. |
| 319 | /// 2. Extend it to a set, which has exactly VectorWidth iterations for |
| 320 | /// any prefix from the set that was built on the previous step. |
| 321 | /// 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] | 322 | /// get a set of prefixes, which don't have exactly VectorWidth iterations. |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 323 | /// 4. Subtract it from all prefixes of the vector loop and get the desired |
| 324 | /// set. |
| 325 | /// |
| 326 | /// @param ScheduleRange A range of a map, which describes a prefix schedule |
| 327 | /// relation. |
| 328 | static __isl_give isl_set * |
| 329 | getPartialTilePrefixes(__isl_take isl_set *ScheduleRange, int VectorWidth) { |
| 330 | auto Dims = isl_set_dim(ScheduleRange, isl_dim_set); |
| 331 | auto *LoopPrefixes = isl_set_project_out(isl_set_copy(ScheduleRange), |
| 332 | isl_dim_set, Dims - 1, 1); |
| 333 | auto *ExtentPrefixes = |
| 334 | isl_set_add_dims(isl_set_copy(LoopPrefixes), isl_dim_set, 1); |
| 335 | ExtentPrefixes = addExtentConstraints(ExtentPrefixes, VectorWidth); |
| 336 | auto *BadPrefixes = isl_set_subtract(ExtentPrefixes, ScheduleRange); |
| 337 | BadPrefixes = isl_set_project_out(BadPrefixes, isl_dim_set, Dims - 1, 1); |
| 338 | return isl_set_subtract(LoopPrefixes, BadPrefixes); |
| 339 | } |
| 340 | |
| 341 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::isolateFullPartialTiles( |
| 342 | __isl_take isl_schedule_node *Node, int VectorWidth) { |
| 343 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
| 344 | Node = isl_schedule_node_child(Node, 0); |
| 345 | Node = isl_schedule_node_child(Node, 0); |
| 346 | auto *SchedRelUMap = isl_schedule_node_get_prefix_schedule_relation(Node); |
| 347 | auto *ScheduleRelation = isl_map_from_union_map(SchedRelUMap); |
| 348 | auto *ScheduleRange = isl_map_range(ScheduleRelation); |
| 349 | auto *IsolateDomain = getPartialTilePrefixes(ScheduleRange, VectorWidth); |
| 350 | auto *AtomicOption = getAtomicOptions(isl_set_get_ctx(IsolateDomain)); |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 351 | auto *IsolateOption = getIsolateOptions(IsolateDomain, 1); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 352 | Node = isl_schedule_node_parent(Node); |
| 353 | Node = isl_schedule_node_parent(Node); |
| 354 | auto *Options = isl_union_set_union(IsolateOption, AtomicOption); |
| 355 | Node = isl_schedule_node_band_set_ast_build_options(Node, Options); |
| 356 | return Node; |
| 357 | } |
| 358 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 359 | __isl_give isl_schedule_node * |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 360 | ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node, |
| 361 | unsigned DimToVectorize, |
| 362 | int VectorWidth) { |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 363 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 364 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 365 | auto Space = isl_schedule_node_band_get_space(Node); |
| 366 | auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set); |
| 367 | isl_space_free(Space); |
| 368 | assert(DimToVectorize < ScheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 369 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 370 | if (DimToVectorize > 0) { |
| 371 | Node = isl_schedule_node_band_split(Node, DimToVectorize); |
| 372 | Node = isl_schedule_node_child(Node, 0); |
| 373 | } |
| 374 | if (DimToVectorize < ScheduleDimensions - 1) |
| 375 | Node = isl_schedule_node_band_split(Node, 1); |
| 376 | Space = isl_schedule_node_band_get_space(Node); |
| 377 | auto Sizes = isl_multi_val_zero(Space); |
| 378 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 379 | Sizes = |
| 380 | isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth)); |
| 381 | Node = isl_schedule_node_band_tile(Node, Sizes); |
Tobias Grosser | ca7f5bb | 2015-10-20 09:12:21 +0000 | [diff] [blame] | 382 | Node = isolateFullPartialTiles(Node, VectorWidth); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 383 | Node = isl_schedule_node_child(Node, 0); |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 384 | // Make sure the "trivially vectorizable loop" is not unrolled. Otherwise, |
| 385 | // we will have troubles to match it in the backend. |
| 386 | Node = isl_schedule_node_band_set_ast_build_options( |
Tobias Grosser | fc490a9 | 2015-08-20 19:08:16 +0000 | [diff] [blame] | 387 | Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }")); |
| 388 | Node = isl_schedule_node_band_sink(Node); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 389 | Node = isl_schedule_node_child(Node, 0); |
Roman Gareev | 11001e1 | 2016-02-23 09:00:13 +0000 | [diff] [blame] | 390 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_leaf) |
| 391 | Node = isl_schedule_node_parent(Node); |
| 392 | isl_id *LoopMarker = isl_id_alloc(Ctx, "SIMD", nullptr); |
| 393 | Node = isl_schedule_node_insert_mark(Node, LoopMarker); |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 394 | return Node; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Tobias Grosser | d891b54 | 2015-08-20 12:16:23 +0000 | [diff] [blame] | 397 | __isl_give isl_schedule_node * |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 398 | ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node, |
| 399 | const char *Identifier, ArrayRef<int> TileSizes, |
| 400 | int DefaultTileSize) { |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 401 | auto Ctx = isl_schedule_node_get_ctx(Node); |
| 402 | auto Space = isl_schedule_node_band_get_space(Node); |
| 403 | auto Dims = isl_space_dim(Space, isl_dim_set); |
| 404 | auto Sizes = isl_multi_val_zero(Space); |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 405 | std::string IdentifierString(Identifier); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 406 | for (unsigned i = 0; i < Dims; i++) { |
| 407 | auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize; |
| 408 | Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize)); |
| 409 | } |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 410 | auto TileLoopMarkerStr = IdentifierString + " - Tiles"; |
| 411 | isl_id *TileLoopMarker = |
| 412 | isl_id_alloc(Ctx, TileLoopMarkerStr.c_str(), nullptr); |
| 413 | Node = isl_schedule_node_insert_mark(Node, TileLoopMarker); |
| 414 | Node = isl_schedule_node_child(Node, 0); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 415 | Node = isl_schedule_node_band_tile(Node, Sizes); |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 416 | Node = isl_schedule_node_child(Node, 0); |
| 417 | auto PointLoopMarkerStr = IdentifierString + " - Points"; |
| 418 | isl_id *PointLoopMarker = |
| 419 | isl_id_alloc(Ctx, PointLoopMarkerStr.c_str(), nullptr); |
| 420 | Node = isl_schedule_node_insert_mark(Node, PointLoopMarker); |
| 421 | Node = isl_schedule_node_child(Node, 0); |
| 422 | return Node; |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Roman Gareev | b17b9a8 | 2016-06-12 17:20:05 +0000 | [diff] [blame] | 425 | __isl_give isl_schedule_node * |
| 426 | ScheduleTreeOptimizer::applyRegisterTiling(__isl_take isl_schedule_node *Node, |
| 427 | llvm::ArrayRef<int> TileSizes, |
| 428 | int DefaultTileSize) { |
| 429 | auto *Ctx = isl_schedule_node_get_ctx(Node); |
| 430 | Node = tileNode(Node, "Register tiling", TileSizes, DefaultTileSize); |
| 431 | Node = isl_schedule_node_band_set_ast_build_options( |
| 432 | Node, isl_union_set_read_from_str(Ctx, "{unroll[x]}")); |
| 433 | return Node; |
| 434 | } |
| 435 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 436 | bool ScheduleTreeOptimizer::isTileableBandNode( |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 437 | __isl_keep isl_schedule_node *Node) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 438 | if (isl_schedule_node_get_type(Node) != isl_schedule_node_band) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 439 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 440 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 441 | if (isl_schedule_node_n_children(Node) != 1) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 442 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 443 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 444 | if (!isl_schedule_node_band_get_permutable(Node)) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 445 | return false; |
Tobias Grosser | 44f19ac | 2011-07-05 22:15:53 +0000 | [diff] [blame] | 446 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 447 | auto Space = isl_schedule_node_band_get_space(Node); |
| 448 | auto Dims = isl_space_dim(Space, isl_dim_set); |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 449 | isl_space_free(Space); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 450 | |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 451 | if (Dims <= 1) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 452 | return false; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 453 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 454 | auto Child = isl_schedule_node_get_child(Node, 0); |
| 455 | auto Type = isl_schedule_node_get_type(Child); |
| 456 | isl_schedule_node_free(Child); |
| 457 | |
Tobias Grosser | 9bdea57 | 2015-08-20 12:22:37 +0000 | [diff] [blame] | 458 | if (Type != isl_schedule_node_leaf) |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 459 | return false; |
| 460 | |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | __isl_give isl_schedule_node * |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 465 | ScheduleTreeOptimizer::standardBandOpts(__isl_take isl_schedule_node *Node, |
| 466 | void *User) { |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 467 | if (FirstLevelTiling) |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 468 | Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes, |
| 469 | FirstLevelDefaultTileSize); |
Tobias Grosser | 0483271 | 2015-08-20 13:45:02 +0000 | [diff] [blame] | 470 | |
| 471 | if (SecondLevelTiling) |
Tobias Grosser | 1ac884d | 2015-08-23 09:11:00 +0000 | [diff] [blame] | 472 | Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes, |
| 473 | SecondLevelDefaultTileSize); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 474 | |
Roman Gareev | b17b9a8 | 2016-06-12 17:20:05 +0000 | [diff] [blame] | 475 | if (RegisterTiling) |
| 476 | Node = |
| 477 | applyRegisterTiling(Node, RegisterTileSizes, RegisterDefaultTileSize); |
Tobias Grosser | 42e2489 | 2015-08-20 13:45:05 +0000 | [diff] [blame] | 478 | |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 479 | if (PollyVectorizerChoice == VECTORIZER_NONE) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 480 | return Node; |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 481 | |
Tobias Grosser | 862b9b5 | 2015-08-20 12:32:45 +0000 | [diff] [blame] | 482 | auto Space = isl_schedule_node_band_get_space(Node); |
| 483 | auto Dims = isl_space_dim(Space, isl_dim_set); |
| 484 | isl_space_free(Space); |
| 485 | |
Tobias Grosser | b241d92 | 2015-07-28 18:03:36 +0000 | [diff] [blame] | 486 | for (int i = Dims - 1; i >= 0; i--) |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 487 | if (isl_schedule_node_band_member_get_coincident(Node, i)) { |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 488 | Node = prevectSchedBand(Node, i, PrevectorWidth); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 489 | break; |
| 490 | } |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 491 | |
Tobias Grosser | f10f463 | 2015-08-19 08:03:37 +0000 | [diff] [blame] | 492 | return Node; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 495 | /// Get the position of a dimension with a non-zero coefficient. |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 496 | /// |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 497 | /// Check that isl constraint @p Constraint has only one non-zero |
| 498 | /// coefficient for dimensions that have type @p DimType. If this is true, |
| 499 | /// return the position of the dimension corresponding to the non-zero |
| 500 | /// coefficient and negative value, otherwise. |
| 501 | /// |
| 502 | /// @param Constraint The isl constraint to be checked. |
| 503 | /// @param DimType The type of the dimensions. |
| 504 | /// @return The position of the dimension in case the isl |
| 505 | /// constraint satisfies the requirements, a negative |
| 506 | /// value, otherwise. |
| 507 | static int getMatMulConstraintDim(__isl_keep isl_constraint *Constraint, |
| 508 | enum isl_dim_type DimType) { |
| 509 | int DimPos = -1; |
| 510 | auto *LocalSpace = isl_constraint_get_local_space(Constraint); |
| 511 | int LocalSpaceDimNum = isl_local_space_dim(LocalSpace, DimType); |
| 512 | for (int i = 0; i < LocalSpaceDimNum; i++) { |
| 513 | auto *Val = isl_constraint_get_coefficient_val(Constraint, DimType, i); |
| 514 | if (isl_val_is_zero(Val)) { |
| 515 | isl_val_free(Val); |
| 516 | continue; |
| 517 | } |
| 518 | if (DimPos >= 0 || (DimType == isl_dim_out && !isl_val_is_one(Val)) || |
| 519 | (DimType == isl_dim_in && !isl_val_is_negone(Val))) { |
| 520 | isl_val_free(Val); |
| 521 | isl_local_space_free(LocalSpace); |
| 522 | return -1; |
| 523 | } |
| 524 | DimPos = i; |
| 525 | isl_val_free(Val); |
| 526 | } |
| 527 | isl_local_space_free(LocalSpace); |
| 528 | return DimPos; |
| 529 | } |
| 530 | |
| 531 | /// Check the form of the isl constraint. |
| 532 | /// |
| 533 | /// Check that the @p DimInPos input dimension of the isl constraint |
| 534 | /// @p Constraint has a coefficient that is equal to negative one, the @p |
| 535 | /// DimOutPos has a coefficient that is equal to one and others |
| 536 | /// have coefficients equal to zero. |
| 537 | /// |
| 538 | /// @param Constraint The isl constraint to be checked. |
| 539 | /// @param DimInPos The input dimension of the isl constraint. |
| 540 | /// @param DimOutPos The output dimension of the isl constraint. |
| 541 | /// @return isl_stat_ok in case the isl constraint satisfies |
| 542 | /// the requirements, isl_stat_error otherwise. |
| 543 | static isl_stat isMatMulOperandConstraint(__isl_keep isl_constraint *Constraint, |
| 544 | int &DimInPos, int &DimOutPos) { |
| 545 | auto *Val = isl_constraint_get_constant_val(Constraint); |
| 546 | if (!isl_constraint_is_equality(Constraint) || !isl_val_is_zero(Val)) { |
| 547 | isl_val_free(Val); |
| 548 | return isl_stat_error; |
| 549 | } |
| 550 | isl_val_free(Val); |
| 551 | DimInPos = getMatMulConstraintDim(Constraint, isl_dim_in); |
| 552 | if (DimInPos < 0) |
| 553 | return isl_stat_error; |
| 554 | DimOutPos = getMatMulConstraintDim(Constraint, isl_dim_out); |
| 555 | if (DimOutPos < 0) |
| 556 | return isl_stat_error; |
| 557 | return isl_stat_ok; |
| 558 | } |
| 559 | |
| 560 | /// Check that the access relation corresponds to a non-constant operand |
| 561 | /// of the matrix multiplication. |
| 562 | /// |
| 563 | /// Access relations that correspond to non-constant operands of the matrix |
| 564 | /// multiplication depend only on two input dimensions and have two output |
| 565 | /// dimensions. The function checks that the isl basic map @p bmap satisfies |
| 566 | /// the requirements. The two input dimensions can be specified via @p user |
| 567 | /// array. |
| 568 | /// |
| 569 | /// @param bmap The isl basic map to be checked. |
| 570 | /// @param user The input dimensions of @p bmap. |
| 571 | /// @return isl_stat_ok in case isl basic map satisfies the requirements, |
| 572 | /// isl_stat_error otherwise. |
| 573 | static isl_stat isMatMulOperandBasicMap(__isl_take isl_basic_map *bmap, |
| 574 | void *user) { |
| 575 | auto *Constraints = isl_basic_map_get_constraint_list(bmap); |
| 576 | isl_basic_map_free(bmap); |
| 577 | if (isl_constraint_list_n_constraint(Constraints) != 2) { |
| 578 | isl_constraint_list_free(Constraints); |
| 579 | return isl_stat_error; |
| 580 | } |
| 581 | int InPosPair[] = {-1, -1}; |
| 582 | auto DimInPos = user ? static_cast<int *>(user) : InPosPair; |
| 583 | for (int i = 0; i < 2; i++) { |
| 584 | auto *Constraint = isl_constraint_list_get_constraint(Constraints, i); |
| 585 | int InPos, OutPos; |
| 586 | if (isMatMulOperandConstraint(Constraint, InPos, OutPos) == |
| 587 | isl_stat_error || |
| 588 | OutPos > 1 || (DimInPos[OutPos] >= 0 && DimInPos[OutPos] != InPos)) { |
| 589 | isl_constraint_free(Constraint); |
| 590 | isl_constraint_list_free(Constraints); |
| 591 | return isl_stat_error; |
| 592 | } |
| 593 | DimInPos[OutPos] = InPos; |
| 594 | isl_constraint_free(Constraint); |
| 595 | } |
| 596 | isl_constraint_list_free(Constraints); |
| 597 | return isl_stat_ok; |
| 598 | } |
| 599 | |
| 600 | /// Permute the two dimensions of the isl map. |
| 601 | /// |
| 602 | /// Permute @p DstPos and @p SrcPos dimensions of the isl map @p Map that |
| 603 | /// have type @p DimType. |
| 604 | /// |
| 605 | /// @param Map The isl map to be modified. |
| 606 | /// @param DimType The type of the dimensions. |
| 607 | /// @param DstPos The first dimension. |
| 608 | /// @param SrcPos The second dimension. |
| 609 | /// @return The modified map. |
| 610 | __isl_give isl_map *permuteDimensions(__isl_take isl_map *Map, |
| 611 | enum isl_dim_type DimType, |
| 612 | unsigned DstPos, unsigned SrcPos) { |
| 613 | assert(DstPos < isl_map_dim(Map, DimType) && |
| 614 | SrcPos < isl_map_dim(Map, DimType)); |
| 615 | if (DstPos == SrcPos) |
| 616 | return Map; |
| 617 | isl_id *DimId = nullptr; |
| 618 | if (isl_map_has_tuple_id(Map, DimType)) |
| 619 | DimId = isl_map_get_tuple_id(Map, DimType); |
| 620 | auto FreeDim = DimType == isl_dim_in ? isl_dim_out : isl_dim_in; |
| 621 | isl_id *FreeDimId = nullptr; |
| 622 | if (isl_map_has_tuple_id(Map, FreeDim)) |
| 623 | FreeDimId = isl_map_get_tuple_id(Map, FreeDim); |
| 624 | auto MaxDim = std::max(DstPos, SrcPos); |
| 625 | auto MinDim = std::min(DstPos, SrcPos); |
| 626 | Map = isl_map_move_dims(Map, FreeDim, 0, DimType, MaxDim, 1); |
| 627 | Map = isl_map_move_dims(Map, FreeDim, 0, DimType, MinDim, 1); |
| 628 | Map = isl_map_move_dims(Map, DimType, MinDim, FreeDim, 1, 1); |
| 629 | Map = isl_map_move_dims(Map, DimType, MaxDim, FreeDim, 0, 1); |
| 630 | if (DimId) |
| 631 | Map = isl_map_set_tuple_id(Map, DimType, DimId); |
| 632 | if (FreeDimId) |
| 633 | Map = isl_map_set_tuple_id(Map, FreeDim, FreeDimId); |
| 634 | return Map; |
| 635 | } |
| 636 | |
| 637 | /// Check the form of the access relation. |
| 638 | /// |
| 639 | /// Check that the access relation @p AccMap has the form M[i][j], where i |
| 640 | /// is a @p FirstPos and j is a @p SecondPos. |
| 641 | /// |
| 642 | /// @param AccMap The access relation to be checked. |
| 643 | /// @param FirstPos The index of the input dimension that is mapped to |
| 644 | /// the first output dimension. |
| 645 | /// @param SecondPos The index of the input dimension that is mapped to the |
| 646 | /// second output dimension. |
| 647 | /// @return True in case @p AccMap has the expected form and false, |
| 648 | /// otherwise. |
| 649 | static bool isMatMulOperandAcc(__isl_keep isl_map *AccMap, int &FirstPos, |
| 650 | int &SecondPos) { |
| 651 | int DimInPos[] = {FirstPos, SecondPos}; |
| 652 | if (isl_map_foreach_basic_map(AccMap, isMatMulOperandBasicMap, |
| 653 | static_cast<void *>(DimInPos)) != isl_stat_ok || |
| 654 | DimInPos[0] < 0 || DimInPos[1] < 0) |
| 655 | return false; |
| 656 | FirstPos = DimInPos[0]; |
| 657 | SecondPos = DimInPos[1]; |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | /// Does the memory access represent a non-scalar operand of the matrix |
| 662 | /// multiplication. |
| 663 | /// |
| 664 | /// Check that the memory access @p MemAccess is the read access to a non-scalar |
| 665 | /// operand of the matrix multiplication or its result. |
| 666 | /// |
| 667 | /// @param MemAccess The memory access to be checked. |
| 668 | /// @param MMI Parameters of the matrix multiplication operands. |
| 669 | /// @return True in case the memory access represents the read access |
| 670 | /// to a non-scalar operand of the matrix multiplication and |
| 671 | /// false, otherwise. |
| 672 | static bool isMatMulNonScalarReadAccess(MemoryAccess *MemAccess, |
| 673 | MatMulInfoTy &MMI) { |
| 674 | if (!MemAccess->isArrayKind() || !MemAccess->isRead()) |
| 675 | return false; |
| 676 | isl_map *AccMap = MemAccess->getAccessRelation(); |
| 677 | if (isMatMulOperandAcc(AccMap, MMI.i, MMI.j) && !MMI.ReadFromC && |
| 678 | isl_map_n_basic_map(AccMap) == 1) { |
| 679 | MMI.ReadFromC = MemAccess; |
| 680 | isl_map_free(AccMap); |
| 681 | return true; |
| 682 | } |
| 683 | if (isMatMulOperandAcc(AccMap, MMI.i, MMI.k) && !MMI.A && |
| 684 | isl_map_n_basic_map(AccMap) == 1) { |
| 685 | MMI.A = MemAccess; |
| 686 | isl_map_free(AccMap); |
| 687 | return true; |
| 688 | } |
| 689 | if (isMatMulOperandAcc(AccMap, MMI.k, MMI.j) && !MMI.B && |
| 690 | isl_map_n_basic_map(AccMap) == 1) { |
| 691 | MMI.B = MemAccess; |
| 692 | isl_map_free(AccMap); |
| 693 | return true; |
| 694 | } |
| 695 | isl_map_free(AccMap); |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | /// Check accesses to operands of the matrix multiplication. |
| 700 | /// |
| 701 | /// Check that accesses of the SCoP statement, which corresponds to |
| 702 | /// the partial schedule @p PartialSchedule, are scalar in terms of loops |
| 703 | /// containing the matrix multiplication, in case they do not represent |
| 704 | /// accesses to the non-scalar operands of the matrix multiplication or |
| 705 | /// its result. |
| 706 | /// |
| 707 | /// @param PartialSchedule The partial schedule of the SCoP statement. |
| 708 | /// @param MMI Parameters of the matrix multiplication operands. |
| 709 | /// @return True in case the corresponding SCoP statement |
| 710 | /// represents matrix multiplication and false, |
| 711 | /// otherwise. |
| 712 | static bool containsOnlyMatrMultAcc(__isl_keep isl_map *PartialSchedule, |
| 713 | MatMulInfoTy &MMI) { |
| 714 | auto *InputDimId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in); |
| 715 | auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimId)); |
| 716 | isl_id_free(InputDimId); |
| 717 | unsigned OutDimNum = isl_map_dim(PartialSchedule, isl_dim_out); |
| 718 | assert(OutDimNum > 2 && "In case of the matrix multiplication the loop nest " |
| 719 | "and, consequently, the corresponding scheduling " |
| 720 | "functions have at least three dimensions."); |
| 721 | auto *MapI = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out, |
| 722 | MMI.i, OutDimNum - 1); |
| 723 | auto *MapJ = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out, |
| 724 | MMI.j, OutDimNum - 1); |
| 725 | auto *MapK = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out, |
| 726 | MMI.k, OutDimNum - 1); |
| 727 | for (auto *MemA = Stmt->begin(); MemA != Stmt->end() - 1; MemA++) { |
| 728 | auto *MemAccessPtr = *MemA; |
| 729 | if (MemAccessPtr->isArrayKind() && MemAccessPtr != MMI.WriteToC && |
| 730 | !isMatMulNonScalarReadAccess(MemAccessPtr, MMI) && |
| 731 | !(MemAccessPtr->isStrideZero(isl_map_copy(MapI)) && |
| 732 | MemAccessPtr->isStrideZero(isl_map_copy(MapJ)) && |
| 733 | MemAccessPtr->isStrideZero(isl_map_copy(MapK)))) { |
| 734 | isl_map_free(MapI); |
| 735 | isl_map_free(MapJ); |
| 736 | isl_map_free(MapK); |
| 737 | return false; |
| 738 | } |
| 739 | } |
| 740 | isl_map_free(MapI); |
| 741 | isl_map_free(MapJ); |
| 742 | isl_map_free(MapK); |
| 743 | return true; |
| 744 | } |
| 745 | |
| 746 | /// Check for dependencies corresponding to the matrix multiplication. |
| 747 | /// |
| 748 | /// Check that there is only true dependence of the form |
| 749 | /// S(..., k, ...) -> S(..., k + 1, …), where S is the SCoP statement |
| 750 | /// represented by @p Schedule and k is @p Pos. Such a dependence corresponds |
| 751 | /// to the dependency produced by the matrix multiplication. |
| 752 | /// |
| 753 | /// @param Schedule The schedule of the SCoP statement. |
| 754 | /// @param D The SCoP dependencies. |
| 755 | /// @param Pos The parameter to desribe an acceptable true dependence. |
| 756 | /// In case it has a negative value, try to determine its |
| 757 | /// acceptable value. |
| 758 | /// @return True in case dependencies correspond to the matrix multiplication |
| 759 | /// and false, otherwise. |
| 760 | static bool containsOnlyMatMulDep(__isl_keep isl_map *Schedule, |
| 761 | const Dependences *D, int &Pos) { |
| 762 | auto *WAR = D->getDependences(Dependences::TYPE_WAR); |
| 763 | if (!isl_union_map_is_empty(WAR)) { |
| 764 | isl_union_map_free(WAR); |
| 765 | return false; |
| 766 | } |
| 767 | isl_union_map_free(WAR); |
| 768 | auto *RAW = D->getDependences(Dependences::TYPE_RAW); |
Roman Gareev | afcf026 | 2017-02-11 07:14:37 +0000 | [diff] [blame] | 769 | auto *DomainSpace = isl_space_domain(isl_map_get_space(Schedule)); |
| 770 | auto *Space = isl_space_map_from_domain_and_range(isl_space_copy(DomainSpace), |
| 771 | DomainSpace); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 772 | auto *Deltas = isl_map_deltas(isl_union_map_extract_map(RAW, Space)); |
Roman Gareev | 5ef7e21 | 2017-02-11 08:43:41 +0000 | [diff] [blame^] | 773 | isl_union_map_free(RAW); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 774 | int DeltasDimNum = isl_set_dim(Deltas, isl_dim_set); |
Roman Gareev | afcf026 | 2017-02-11 07:14:37 +0000 | [diff] [blame] | 775 | isl_set_free(Deltas); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 776 | for (int i = 0; i < DeltasDimNum; i++) { |
| 777 | auto *Val = isl_set_plain_get_val_if_fixed(Deltas, isl_dim_set, i); |
Roman Gareev | afcf026 | 2017-02-11 07:14:37 +0000 | [diff] [blame] | 778 | Pos = Pos < 0 && isl_val_is_one(Val) ? i : Pos; |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 779 | if (isl_val_is_nan(Val) || |
| 780 | !(isl_val_is_zero(Val) || (i == Pos && isl_val_is_one(Val)))) { |
| 781 | isl_val_free(Val); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | isl_val_free(Val); |
| 785 | } |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 786 | return true; |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 789 | /// Check if the SCoP statement could probably be optimized with analytical |
| 790 | /// modeling. |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 791 | /// |
| 792 | /// containsMatrMult tries to determine whether the following conditions |
| 793 | /// are true: |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 794 | /// 1. The last memory access modeling an array, MA1, represents writing to |
| 795 | /// memory and has the form S(..., i1, ..., i2, ...) -> M(i1, i2) or |
| 796 | /// S(..., i2, ..., i1, ...) -> M(i1, i2), where S is the SCoP statement |
| 797 | /// under consideration. |
| 798 | /// 2. There is only one loop-carried true dependency, and it has the |
| 799 | /// form S(..., i3, ...) -> S(..., i3 + 1, ...), and there are no |
| 800 | /// loop-carried or anti dependencies. |
| 801 | /// 3. SCoP contains three access relations, MA2, MA3, and MA4 that represent |
| 802 | /// reading from memory and have the form S(..., i3, ...) -> M(i1, i3), |
| 803 | /// S(..., i3, ...) -> M(i3, i2), S(...) -> M(i1, i2), respectively, |
| 804 | /// and all memory accesses of the SCoP that are different from MA1, MA2, |
| 805 | /// MA3, and MA4 have stride 0, if the innermost loop is exchanged with any |
| 806 | /// of loops i1, i2 and i3. |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 807 | /// |
| 808 | /// @param PartialSchedule The PartialSchedule that contains a SCoP statement |
| 809 | /// to check. |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 810 | /// @D The SCoP dependencies. |
| 811 | /// @MMI Parameters of the matrix multiplication operands. |
| 812 | static bool containsMatrMult(__isl_keep isl_map *PartialSchedule, |
| 813 | const Dependences *D, MatMulInfoTy &MMI) { |
| 814 | auto *InputDimsId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in); |
| 815 | auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId)); |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 816 | isl_id_free(InputDimsId); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 817 | if (Stmt->size() <= 1) |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 818 | return false; |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 819 | for (auto *MemA = Stmt->end() - 1; MemA != Stmt->begin(); MemA--) { |
| 820 | auto *MemAccessPtr = *MemA; |
| 821 | if (!MemAccessPtr->isArrayKind()) |
| 822 | continue; |
| 823 | if (!MemAccessPtr->isWrite()) |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 824 | return false; |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 825 | auto *AccMap = MemAccessPtr->getAccessRelation(); |
| 826 | if (isl_map_n_basic_map(AccMap) != 1 || |
| 827 | !isMatMulOperandAcc(AccMap, MMI.i, MMI.j)) { |
| 828 | isl_map_free(AccMap); |
| 829 | return false; |
| 830 | } |
| 831 | isl_map_free(AccMap); |
| 832 | MMI.WriteToC = MemAccessPtr; |
| 833 | break; |
| 834 | } |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 835 | |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 836 | if (!containsOnlyMatMulDep(PartialSchedule, D, MMI.k)) |
| 837 | return false; |
| 838 | |
| 839 | if (!MMI.WriteToC || !containsOnlyMatrMultAcc(PartialSchedule, MMI)) |
| 840 | return false; |
| 841 | |
| 842 | if (!MMI.A || !MMI.B || !MMI.ReadFromC) |
| 843 | return false; |
| 844 | return true; |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 847 | /// Permute two dimensions of the band node. |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 848 | /// |
| 849 | /// Permute FirstDim and SecondDim dimensions of the Node. |
| 850 | /// |
| 851 | /// @param Node The band node to be modified. |
| 852 | /// @param FirstDim The first dimension to be permuted. |
| 853 | /// @param SecondDim The second dimension to be permuted. |
| 854 | static __isl_give isl_schedule_node * |
| 855 | permuteBandNodeDimensions(__isl_take isl_schedule_node *Node, unsigned FirstDim, |
| 856 | unsigned SecondDim) { |
| 857 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band && |
| 858 | isl_schedule_node_band_n_member(Node) > std::max(FirstDim, SecondDim)); |
| 859 | auto PartialSchedule = isl_schedule_node_band_get_partial_schedule(Node); |
| 860 | auto PartialScheduleFirstDim = |
| 861 | isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, FirstDim); |
| 862 | auto PartialScheduleSecondDim = |
| 863 | isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, SecondDim); |
| 864 | PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff( |
| 865 | PartialSchedule, SecondDim, PartialScheduleFirstDim); |
| 866 | PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff( |
| 867 | PartialSchedule, FirstDim, PartialScheduleSecondDim); |
| 868 | Node = isl_schedule_node_delete(Node); |
| 869 | Node = isl_schedule_node_insert_partial_schedule(Node, PartialSchedule); |
| 870 | return Node; |
| 871 | } |
| 872 | |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 873 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::createMicroKernel( |
| 874 | __isl_take isl_schedule_node *Node, MicroKernelParamsTy MicroKernelParams) { |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 875 | applyRegisterTiling(Node, {MicroKernelParams.Mr, MicroKernelParams.Nr}, 1); |
| 876 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 877 | Node = permuteBandNodeDimensions(Node, 0, 1); |
| 878 | return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 881 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::createMacroKernel( |
| 882 | __isl_take isl_schedule_node *Node, MacroKernelParamsTy MacroKernelParams) { |
| 883 | assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); |
| 884 | if (MacroKernelParams.Mc == 1 && MacroKernelParams.Nc == 1 && |
| 885 | MacroKernelParams.Kc == 1) |
| 886 | return Node; |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 887 | int DimOutNum = isl_schedule_node_band_n_member(Node); |
| 888 | std::vector<int> TileSizes(DimOutNum, 1); |
| 889 | TileSizes[DimOutNum - 3] = MacroKernelParams.Mc; |
| 890 | TileSizes[DimOutNum - 2] = MacroKernelParams.Nc; |
| 891 | TileSizes[DimOutNum - 1] = MacroKernelParams.Kc; |
| 892 | Node = tileNode(Node, "1st level tiling", TileSizes, 1); |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 893 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 894 | Node = permuteBandNodeDimensions(Node, DimOutNum - 2, DimOutNum - 1); |
| 895 | Node = permuteBandNodeDimensions(Node, DimOutNum - 3, DimOutNum - 1); |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 896 | return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
| 897 | } |
| 898 | |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 899 | /// Get the size of the widest type of the matrix multiplication operands |
| 900 | /// in bytes, including alignment padding. |
| 901 | /// |
| 902 | /// @param MMI Parameters of the matrix multiplication operands. |
| 903 | /// @return The size of the widest type of the matrix multiplication operands |
| 904 | /// in bytes, including alignment padding. |
| 905 | static uint64_t getMatMulAlignTypeSize(MatMulInfoTy MMI) { |
| 906 | auto *S = MMI.A->getStatement()->getParent(); |
| 907 | auto &DL = S->getFunction().getParent()->getDataLayout(); |
| 908 | auto ElementSizeA = DL.getTypeAllocSize(MMI.A->getElementType()); |
| 909 | auto ElementSizeB = DL.getTypeAllocSize(MMI.B->getElementType()); |
| 910 | auto ElementSizeC = DL.getTypeAllocSize(MMI.WriteToC->getElementType()); |
| 911 | return std::max({ElementSizeA, ElementSizeB, ElementSizeC}); |
| 912 | } |
| 913 | |
| 914 | /// Get the size of the widest type of the matrix multiplication operands |
| 915 | /// in bits. |
| 916 | /// |
| 917 | /// @param MMI Parameters of the matrix multiplication operands. |
| 918 | /// @return The size of the widest type of the matrix multiplication operands |
| 919 | /// in bits. |
| 920 | static uint64_t getMatMulTypeSize(MatMulInfoTy MMI) { |
| 921 | auto *S = MMI.A->getStatement()->getParent(); |
| 922 | auto &DL = S->getFunction().getParent()->getDataLayout(); |
| 923 | auto ElementSizeA = DL.getTypeSizeInBits(MMI.A->getElementType()); |
| 924 | auto ElementSizeB = DL.getTypeSizeInBits(MMI.B->getElementType()); |
| 925 | auto ElementSizeC = DL.getTypeSizeInBits(MMI.WriteToC->getElementType()); |
| 926 | return std::max({ElementSizeA, ElementSizeB, ElementSizeC}); |
| 927 | } |
| 928 | |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 929 | /// Get parameters of the BLIS micro kernel. |
| 930 | /// |
| 931 | /// We choose the Mr and Nr parameters of the micro kernel to be large enough |
| 932 | /// such that no stalls caused by the combination of latencies and dependencies |
| 933 | /// are introduced during the updates of the resulting matrix of the matrix |
| 934 | /// multiplication. However, they should also be as small as possible to |
| 935 | /// release more registers for entries of multiplied matrices. |
| 936 | /// |
| 937 | /// @param TTI Target Transform Info. |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 938 | /// @param MMI Parameters of the matrix multiplication operands. |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 939 | /// @return The structure of type MicroKernelParamsTy. |
| 940 | /// @see MicroKernelParamsTy |
| 941 | static struct MicroKernelParamsTy |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 942 | getMicroKernelParams(const llvm::TargetTransformInfo *TTI, MatMulInfoTy MMI) { |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 943 | assert(TTI && "The target transform info should be provided."); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 944 | |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 945 | // Nvec - Number of double-precision floating-point numbers that can be hold |
| 946 | // by a vector register. Use 2 by default. |
Tobias Grosser | 67e94fb | 2017-01-14 07:14:54 +0000 | [diff] [blame] | 947 | long RegisterBitwidth = VectorRegisterBitwidth; |
| 948 | |
| 949 | if (RegisterBitwidth == -1) |
| 950 | RegisterBitwidth = TTI->getRegisterBitWidth(true); |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 951 | auto ElementSize = getMatMulTypeSize(MMI); |
| 952 | assert(ElementSize > 0 && "The element size of the matrix multiplication " |
| 953 | "operands should be greater than zero."); |
| 954 | auto Nvec = RegisterBitwidth / ElementSize; |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 955 | if (Nvec == 0) |
| 956 | Nvec = 2; |
| 957 | int Nr = |
Tobias Grosser | 0791d5f | 2016-12-23 07:33:39 +0000 | [diff] [blame] | 958 | ceil(sqrt(Nvec * LatencyVectorFma * ThroughputVectorFma) / Nvec) * Nvec; |
| 959 | int Mr = ceil(Nvec * LatencyVectorFma * ThroughputVectorFma / Nr); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 960 | return {Mr, Nr}; |
| 961 | } |
| 962 | |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 963 | /// Get parameters of the BLIS macro kernel. |
| 964 | /// |
| 965 | /// During the computation of matrix multiplication, blocks of partitioned |
| 966 | /// matrices are mapped to different layers of the memory hierarchy. |
| 967 | /// To optimize data reuse, blocks should be ideally kept in cache between |
| 968 | /// iterations. Since parameters of the macro kernel determine sizes of these |
| 969 | /// blocks, there are upper and lower bounds on these parameters. |
| 970 | /// |
| 971 | /// @param MicroKernelParams Parameters of the micro-kernel |
| 972 | /// to be taken into account. |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 973 | /// @param MMI Parameters of the matrix multiplication operands. |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 974 | /// @return The structure of type MacroKernelParamsTy. |
| 975 | /// @see MacroKernelParamsTy |
| 976 | /// @see MicroKernelParamsTy |
| 977 | static struct MacroKernelParamsTy |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 978 | getMacroKernelParams(const MicroKernelParamsTy &MicroKernelParams, |
| 979 | MatMulInfoTy MMI) { |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 980 | // According to www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf, |
| 981 | // it requires information about the first two levels of a cache to determine |
| 982 | // all the parameters of a macro-kernel. It also checks that an associativity |
| 983 | // degree of a cache level is greater than two. Otherwise, another algorithm |
| 984 | // for determination of the parameters should be used. |
| 985 | if (!(MicroKernelParams.Mr > 0 && MicroKernelParams.Nr > 0 && |
Roman Gareev | 1c2927b | 2016-12-25 16:32:28 +0000 | [diff] [blame] | 986 | FirstCacheLevelSize > 0 && SecondCacheLevelSize > 0 && |
| 987 | FirstCacheLevelAssociativity > 2 && SecondCacheLevelAssociativity > 2)) |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 988 | return {1, 1, 1}; |
Roman Gareev | be5299a | 2016-12-21 12:51:12 +0000 | [diff] [blame] | 989 | // The quotient should be greater than zero. |
| 990 | if (PollyPatternMatchingNcQuotient <= 0) |
| 991 | return {1, 1, 1}; |
Roman Gareev | 15db81e | 2016-12-15 12:00:57 +0000 | [diff] [blame] | 992 | int Car = floor( |
Roman Gareev | 1c2927b | 2016-12-25 16:32:28 +0000 | [diff] [blame] | 993 | (FirstCacheLevelAssociativity - 1) / |
Roman Gareev | 8babe1a | 2016-12-15 11:47:38 +0000 | [diff] [blame] | 994 | (1 + static_cast<double>(MicroKernelParams.Nr) / MicroKernelParams.Mr)); |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 995 | auto ElementSize = getMatMulAlignTypeSize(MMI); |
| 996 | assert(ElementSize > 0 && "The element size of the matrix multiplication " |
| 997 | "operands should be greater than zero."); |
Roman Gareev | 1c2927b | 2016-12-25 16:32:28 +0000 | [diff] [blame] | 998 | int Kc = (Car * FirstCacheLevelSize) / |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 999 | (MicroKernelParams.Mr * FirstCacheLevelAssociativity * ElementSize); |
| 1000 | double Cac = |
| 1001 | static_cast<double>(Kc * ElementSize * SecondCacheLevelAssociativity) / |
| 1002 | SecondCacheLevelSize; |
Roman Gareev | 1c2927b | 2016-12-25 16:32:28 +0000 | [diff] [blame] | 1003 | int Mc = floor((SecondCacheLevelAssociativity - 2) / Cac); |
Roman Gareev | be5299a | 2016-12-21 12:51:12 +0000 | [diff] [blame] | 1004 | int Nc = PollyPatternMatchingNcQuotient * MicroKernelParams.Nr; |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 1005 | return {Mc, Nc, Kc}; |
| 1006 | } |
| 1007 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1008 | /// Create an access relation that is specific to |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1009 | /// the matrix multiplication pattern. |
| 1010 | /// |
| 1011 | /// Create an access relation of the following form: |
Roman Gareev | 92c4460 | 2016-12-21 11:18:42 +0000 | [diff] [blame] | 1012 | /// [O0, O1, O2, O3, O4, O5, O6, O7, O8] -> [OI, O5, OJ] |
| 1013 | /// where I is @p FirstDim, J is @p SecondDim. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1014 | /// |
| 1015 | /// It can be used, for example, to create relations that helps to consequently |
| 1016 | /// access elements of operands of a matrix multiplication after creation of |
| 1017 | /// the BLIS micro and macro kernels. |
| 1018 | /// |
| 1019 | /// @see ScheduleTreeOptimizer::createMicroKernel |
| 1020 | /// @see ScheduleTreeOptimizer::createMacroKernel |
| 1021 | /// |
| 1022 | /// Subsequently, the described access relation is applied to the range of |
| 1023 | /// @p MapOldIndVar, that is used to map original induction variables to |
| 1024 | /// the ones, which are produced by schedule transformations. It helps to |
| 1025 | /// define relations using a new space and, at the same time, keep them |
| 1026 | /// in the original one. |
| 1027 | /// |
| 1028 | /// @param MapOldIndVar The relation, which maps original induction variables |
| 1029 | /// to the ones, which are produced by schedule |
| 1030 | /// transformations. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1031 | /// @param FirstDim, SecondDim The input dimensions that are used to define |
| 1032 | /// the specified access relation. |
| 1033 | /// @return The specified access relation. |
| 1034 | __isl_give isl_map *getMatMulAccRel(__isl_take isl_map *MapOldIndVar, |
Roman Gareev | 92c4460 | 2016-12-21 11:18:42 +0000 | [diff] [blame] | 1035 | unsigned FirstDim, unsigned SecondDim) { |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1036 | auto *Ctx = isl_map_get_ctx(MapOldIndVar); |
Roman Gareev | 92c4460 | 2016-12-21 11:18:42 +0000 | [diff] [blame] | 1037 | auto *AccessRelSpace = isl_space_alloc(Ctx, 0, 9, 3); |
| 1038 | auto *AccessRel = isl_map_universe(AccessRelSpace); |
| 1039 | AccessRel = isl_map_equate(AccessRel, isl_dim_in, FirstDim, isl_dim_out, 0); |
| 1040 | AccessRel = isl_map_equate(AccessRel, isl_dim_in, 5, isl_dim_out, 1); |
| 1041 | 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] | 1042 | return isl_map_apply_range(MapOldIndVar, AccessRel); |
| 1043 | } |
| 1044 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1045 | __isl_give isl_schedule_node * |
| 1046 | createExtensionNode(__isl_take isl_schedule_node *Node, |
| 1047 | __isl_take isl_map *ExtensionMap) { |
| 1048 | auto *Extension = isl_union_map_from_map(ExtensionMap); |
| 1049 | auto *NewNode = isl_schedule_node_from_extension(Extension); |
| 1050 | return isl_schedule_node_graft_before(Node, NewNode); |
| 1051 | } |
| 1052 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1053 | /// Apply the packing transformation. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1054 | /// |
| 1055 | /// The packing transformation can be described as a data-layout |
| 1056 | /// transformation that requires to introduce a new array, copy data |
Roman Gareev | 7758a2a | 2017-01-29 10:37:50 +0000 | [diff] [blame] | 1057 | /// to the array, and change memory access locations to reference the array. |
| 1058 | /// It can be used to ensure that elements of the new array are read in-stride |
| 1059 | /// access, aligned to cache lines boundaries, and preloaded into certain cache |
| 1060 | /// levels. |
| 1061 | /// |
| 1062 | /// As an example let us consider the packing of the array A that would help |
| 1063 | /// to read its elements with in-stride access. An access to the array A |
| 1064 | /// is represented by an access relation that has the form |
| 1065 | /// S[i, j, k] -> A[i, k]. The scheduling function of the SCoP statement S has |
| 1066 | /// the form S[i,j, k] -> [floor((j mod Nc) / Nr), floor((i mod Mc) / Mr), |
| 1067 | /// k mod Kc, j mod Nr, i mod Mr]. |
| 1068 | /// |
| 1069 | /// To ensure that elements of the array A are read in-stride access, we add |
| 1070 | /// a new array Packed_A[Mc/Mr][Kc][Mr] to the SCoP, using |
| 1071 | /// Scop::createScopArrayInfo, change the access relation |
| 1072 | /// S[i, j, k] -> A[i, k] to |
| 1073 | /// S[i, j, k] -> Packed_A[floor((i mod Mc) / Mr), k mod Kc, i mod Mr], using |
| 1074 | /// MemoryAccess::setNewAccessRelation, and copy the data to the array, using |
| 1075 | /// the copy statement created by Scop::addScopStmt. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1076 | /// |
| 1077 | /// @param Node The schedule node to be optimized. |
| 1078 | /// @param MapOldIndVar The relation, which maps original induction variables |
| 1079 | /// to the ones, which are produced by schedule |
| 1080 | /// transformations. |
| 1081 | /// @param MicroParams, MacroParams Parameters of the BLIS kernel |
| 1082 | /// to be taken into account. |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1083 | /// @param MMI Parameters of the matrix multiplication operands. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1084 | /// @return The optimized schedule node. |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1085 | static __isl_give isl_schedule_node *optimizeDataLayoutMatrMulPattern( |
| 1086 | __isl_take isl_schedule_node *Node, __isl_take isl_map *MapOldIndVar, |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1087 | MicroKernelParamsTy MicroParams, MacroKernelParamsTy MacroParams, |
| 1088 | MatMulInfoTy &MMI) { |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1089 | auto InputDimsId = isl_map_get_tuple_id(MapOldIndVar, isl_dim_in); |
| 1090 | auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId)); |
| 1091 | isl_id_free(InputDimsId); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Create a copy statement that corresponds to the memory access to the |
| 1094 | // matrix B, the second operand of the matrix multiplication. |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1095 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 1096 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 1097 | Node = isl_schedule_node_parent(Node); |
| 1098 | 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] | 1099 | auto *AccRel = getMatMulAccRel(isl_map_copy(MapOldIndVar), 3, 7); |
| 1100 | unsigned FirstDimSize = MacroParams.Nc / MicroParams.Nr; |
| 1101 | unsigned SecondDimSize = MacroParams.Kc; |
| 1102 | unsigned ThirdDimSize = MicroParams.Nr; |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1103 | auto *SAI = Stmt->getParent()->createScopArrayInfo( |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1104 | MMI.B->getElementType(), "Packed_B", |
Roman Gareev | 92c4460 | 2016-12-21 11:18:42 +0000 | [diff] [blame] | 1105 | {FirstDimSize, SecondDimSize, ThirdDimSize}); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1106 | AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId()); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1107 | auto *OldAcc = MMI.B->getAccessRelation(); |
| 1108 | MMI.B->setNewAccessRelation(AccRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1109 | auto *ExtMap = |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1110 | isl_map_project_out(isl_map_copy(MapOldIndVar), isl_dim_out, 2, |
| 1111 | isl_map_dim(MapOldIndVar, isl_dim_out) - 2); |
| 1112 | ExtMap = isl_map_reverse(ExtMap); |
| 1113 | ExtMap = isl_map_fix_si(ExtMap, isl_dim_out, MMI.i, 0); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1114 | auto *Domain = Stmt->getDomain(); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 1115 | |
| 1116 | // Restrict the domains of the copy statements to only execute when also its |
| 1117 | // originating statement is executed. |
| 1118 | auto *DomainId = isl_set_get_tuple_id(Domain); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1119 | auto *NewStmt = Stmt->getParent()->addScopStmt( |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1120 | OldAcc, MMI.B->getAccessRelation(), isl_set_copy(Domain)); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 1121 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, isl_id_copy(DomainId)); |
| 1122 | ExtMap = isl_map_intersect_range(ExtMap, isl_set_copy(Domain)); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1123 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId()); |
| 1124 | Node = createExtensionNode(Node, ExtMap); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 1125 | |
| 1126 | // Create a copy statement that corresponds to the memory access |
| 1127 | // to the matrix A, the first operand of the matrix multiplication. |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1128 | Node = isl_schedule_node_child(Node, 0); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1129 | AccRel = getMatMulAccRel(isl_map_copy(MapOldIndVar), 4, 6); |
Roman Gareev | 92c4460 | 2016-12-21 11:18:42 +0000 | [diff] [blame] | 1130 | FirstDimSize = MacroParams.Mc / MicroParams.Mr; |
| 1131 | ThirdDimSize = MicroParams.Mr; |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1132 | SAI = Stmt->getParent()->createScopArrayInfo( |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1133 | MMI.A->getElementType(), "Packed_A", |
Roman Gareev | 92c4460 | 2016-12-21 11:18:42 +0000 | [diff] [blame] | 1134 | {FirstDimSize, SecondDimSize, ThirdDimSize}); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1135 | AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId()); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1136 | OldAcc = MMI.A->getAccessRelation(); |
| 1137 | MMI.A->setNewAccessRelation(AccRel); |
| 1138 | ExtMap = isl_map_project_out(MapOldIndVar, isl_dim_out, 3, |
| 1139 | isl_map_dim(MapOldIndVar, isl_dim_out) - 3); |
| 1140 | ExtMap = isl_map_reverse(ExtMap); |
| 1141 | ExtMap = isl_map_fix_si(ExtMap, isl_dim_out, MMI.j, 0); |
| 1142 | NewStmt = Stmt->getParent()->addScopStmt(OldAcc, MMI.A->getAccessRelation(), |
| 1143 | isl_set_copy(Domain)); |
Roman Gareev | 2606c48 | 2016-12-15 12:35:59 +0000 | [diff] [blame] | 1144 | |
| 1145 | // Restrict the domains of the copy statements to only execute when also its |
| 1146 | // originating statement is executed. |
| 1147 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, DomainId); |
| 1148 | ExtMap = isl_map_intersect_range(ExtMap, Domain); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1149 | ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId()); |
| 1150 | Node = createExtensionNode(Node, ExtMap); |
| 1151 | Node = isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
| 1152 | return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1155 | /// Get a relation mapping induction variables produced by schedule |
| 1156 | /// transformations to the original ones. |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1157 | /// |
| 1158 | /// @param Node The schedule node produced as the result of creation |
| 1159 | /// of the BLIS kernels. |
| 1160 | /// @param MicroKernelParams, MacroKernelParams Parameters of the BLIS kernel |
| 1161 | /// to be taken into account. |
| 1162 | /// @return The relation mapping original induction variables to the ones |
| 1163 | /// produced by schedule transformation. |
| 1164 | /// @see ScheduleTreeOptimizer::createMicroKernel |
| 1165 | /// @see ScheduleTreeOptimizer::createMacroKernel |
| 1166 | /// @see getMacroKernelParams |
| 1167 | __isl_give isl_map * |
| 1168 | getInductionVariablesSubstitution(__isl_take isl_schedule_node *Node, |
| 1169 | MicroKernelParamsTy MicroKernelParams, |
| 1170 | MacroKernelParamsTy MacroKernelParams) { |
| 1171 | auto *Child = isl_schedule_node_get_child(Node, 0); |
| 1172 | auto *UnMapOldIndVar = isl_schedule_node_get_prefix_schedule_union_map(Child); |
| 1173 | isl_schedule_node_free(Child); |
| 1174 | auto *MapOldIndVar = isl_map_from_union_map(UnMapOldIndVar); |
| 1175 | if (isl_map_dim(MapOldIndVar, isl_dim_out) > 9) |
| 1176 | MapOldIndVar = |
| 1177 | isl_map_project_out(MapOldIndVar, isl_dim_out, 0, |
| 1178 | isl_map_dim(MapOldIndVar, isl_dim_out) - 9); |
| 1179 | return MapOldIndVar; |
| 1180 | } |
| 1181 | |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 1182 | /// Isolate a set of partial tile prefixes and unroll the isolated part. |
| 1183 | /// |
| 1184 | /// The set should ensure that it contains only partial tile prefixes that have |
| 1185 | /// exactly Mr x Nr iterations of the two innermost loops produced by |
| 1186 | /// the optimization of the matrix multiplication. Mr and Nr are parameters of |
| 1187 | /// the micro-kernel. |
| 1188 | /// |
| 1189 | /// In case of parametric bounds, this helps to auto-vectorize the unrolled |
| 1190 | /// innermost loops, using the SLP vectorizer. |
| 1191 | /// |
| 1192 | /// @param Node The schedule node to be modified. |
| 1193 | /// @param MicroKernelParams Parameters of the micro-kernel |
| 1194 | /// to be taken into account. |
| 1195 | /// @return The modified isl_schedule_node. |
| 1196 | static __isl_give isl_schedule_node * |
| 1197 | isolateAndUnrollMatMulInnerLoops(__isl_take isl_schedule_node *Node, |
| 1198 | struct MicroKernelParamsTy MicroKernelParams) { |
| 1199 | auto *Child = isl_schedule_node_get_child(Node, 0); |
| 1200 | auto *UnMapOldIndVar = isl_schedule_node_get_prefix_schedule_relation(Child); |
| 1201 | isl_schedule_node_free(Child); |
| 1202 | auto *Prefix = isl_map_range(isl_map_from_union_map(UnMapOldIndVar)); |
| 1203 | auto Dims = isl_set_dim(Prefix, isl_dim_set); |
| 1204 | Prefix = isl_set_project_out(Prefix, isl_dim_set, Dims - 1, 1); |
| 1205 | Prefix = getPartialTilePrefixes(Prefix, MicroKernelParams.Nr); |
| 1206 | Prefix = getPartialTilePrefixes(Prefix, MicroKernelParams.Mr); |
| 1207 | auto *IsolateOption = getIsolateOptions( |
| 1208 | isl_set_add_dims(isl_set_copy(Prefix), isl_dim_set, 3), 3); |
| 1209 | auto *Ctx = isl_schedule_node_get_ctx(Node); |
| 1210 | auto *AtomicOption = getAtomicOptions(Ctx); |
| 1211 | auto *Options = |
| 1212 | isl_union_set_union(IsolateOption, isl_union_set_copy(AtomicOption)); |
| 1213 | Options = isl_union_set_union(Options, getUnrollIsolatedSetOptions(Ctx)); |
| 1214 | Node = isl_schedule_node_band_set_ast_build_options(Node, Options); |
| 1215 | Node = isl_schedule_node_parent(isl_schedule_node_parent(Node)); |
| 1216 | IsolateOption = getIsolateOptions(Prefix, 3); |
| 1217 | Options = isl_union_set_union(IsolateOption, AtomicOption); |
| 1218 | Node = isl_schedule_node_band_set_ast_build_options(Node, Options); |
| 1219 | Node = isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0); |
| 1220 | return Node; |
| 1221 | } |
| 1222 | |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 1223 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeMatMulPattern( |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1224 | __isl_take isl_schedule_node *Node, const llvm::TargetTransformInfo *TTI, |
| 1225 | MatMulInfoTy &MMI) { |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 1226 | assert(TTI && "The target transform info should be provided."); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1227 | int DimOutNum = isl_schedule_node_band_n_member(Node); |
| 1228 | assert(DimOutNum > 2 && "In case of the matrix multiplication the loop nest " |
| 1229 | "and, consequently, the corresponding scheduling " |
| 1230 | "functions have at least three dimensions."); |
| 1231 | Node = permuteBandNodeDimensions(Node, MMI.i, DimOutNum - 3); |
| 1232 | int NewJ = MMI.j == DimOutNum - 3 ? MMI.i : MMI.j; |
| 1233 | int NewK = MMI.k == DimOutNum - 3 ? MMI.i : MMI.k; |
| 1234 | Node = permuteBandNodeDimensions(Node, NewJ, DimOutNum - 2); |
| 1235 | NewK = MMI.k == DimOutNum - 2 ? MMI.j : MMI.k; |
| 1236 | Node = permuteBandNodeDimensions(Node, NewK, DimOutNum - 1); |
Roman Gareev | 3d4eae3 | 2017-02-11 07:00:05 +0000 | [diff] [blame] | 1237 | auto MicroKernelParams = getMicroKernelParams(TTI, MMI); |
| 1238 | auto MacroKernelParams = getMacroKernelParams(MicroKernelParams, MMI); |
Roman Gareev | 3a18a93 | 2016-07-25 09:42:53 +0000 | [diff] [blame] | 1239 | Node = createMacroKernel(Node, MacroKernelParams); |
Roman Gareev | 2cb4d13 | 2016-07-25 07:27:59 +0000 | [diff] [blame] | 1240 | Node = createMicroKernel(Node, MicroKernelParams); |
Roman Gareev | 1c892e9 | 2016-08-15 12:22:54 +0000 | [diff] [blame] | 1241 | if (MacroKernelParams.Mc == 1 || MacroKernelParams.Nc == 1 || |
| 1242 | MacroKernelParams.Kc == 1) |
| 1243 | return Node; |
| 1244 | auto *MapOldIndVar = getInductionVariablesSubstitution( |
| 1245 | Node, MicroKernelParams, MacroKernelParams); |
| 1246 | if (!MapOldIndVar) |
| 1247 | return Node; |
Roman Gareev | 9989088 | 2017-02-09 07:10:01 +0000 | [diff] [blame] | 1248 | Node = isolateAndUnrollMatMulInnerLoops(Node, MicroKernelParams); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1249 | return optimizeDataLayoutMatrMulPattern(Node, MapOldIndVar, MicroKernelParams, |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1250 | MacroKernelParams, MMI); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 1253 | bool ScheduleTreeOptimizer::isMatrMultPattern( |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1254 | __isl_keep isl_schedule_node *Node, const Dependences *D, |
| 1255 | MatMulInfoTy &MMI) { |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 1256 | auto *PartialSchedule = |
| 1257 | isl_schedule_node_band_get_partial_schedule_union_map(Node); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1258 | if (isl_schedule_node_band_n_member(Node) < 3 || |
Roman Gareev | 397a34a | 2016-06-22 12:11:30 +0000 | [diff] [blame] | 1259 | isl_union_map_n_map(PartialSchedule) != 1) { |
| 1260 | isl_union_map_free(PartialSchedule); |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 1261 | return false; |
| 1262 | } |
Roman Gareev | 397a34a | 2016-06-22 12:11:30 +0000 | [diff] [blame] | 1263 | auto *NewPartialSchedule = isl_map_from_union_map(PartialSchedule); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1264 | if (containsMatrMult(NewPartialSchedule, D, MMI)) { |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 1265 | isl_map_free(NewPartialSchedule); |
| 1266 | return true; |
| 1267 | } |
| 1268 | isl_map_free(NewPartialSchedule); |
| 1269 | return false; |
| 1270 | } |
| 1271 | |
| 1272 | __isl_give isl_schedule_node * |
| 1273 | ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node, |
| 1274 | void *User) { |
| 1275 | if (!isTileableBandNode(Node)) |
| 1276 | return Node; |
| 1277 | |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1278 | const OptimizerAdditionalInfoTy *OAI = |
| 1279 | static_cast<const OptimizerAdditionalInfoTy *>(User); |
| 1280 | |
| 1281 | MatMulInfoTy MMI; |
| 1282 | if (PMBasedOpts && User && isMatrMultPattern(Node, OAI->D, MMI)) { |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 1283 | DEBUG(dbgs() << "The matrix multiplication pattern was detected\n"); |
Roman Gareev | 772498d | 2017-02-08 13:29:06 +0000 | [diff] [blame] | 1284 | return optimizeMatMulPattern(Node, OAI->TTI, MMI); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1285 | } |
Roman Gareev | 9c3eb59 | 2016-05-28 16:17:58 +0000 | [diff] [blame] | 1286 | |
| 1287 | return standardBandOpts(Node, User); |
| 1288 | } |
| 1289 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1290 | __isl_give isl_schedule * |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1291 | ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule, |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1292 | const OptimizerAdditionalInfoTy *OAI) { |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 1293 | isl_schedule_node *Root = isl_schedule_get_root(Schedule); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1294 | Root = optimizeScheduleNode(Root, OAI); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1295 | isl_schedule_free(Schedule); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1296 | auto S = isl_schedule_node_get_schedule(Root); |
Tobias Grosser | bbb4cec | 2015-03-22 12:06:39 +0000 | [diff] [blame] | 1297 | isl_schedule_node_free(Root); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1298 | return S; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1301 | __isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode( |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1302 | __isl_take isl_schedule_node *Node, const OptimizerAdditionalInfoTy *OAI) { |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1303 | Node = isl_schedule_node_map_descendant_bottom_up( |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1304 | Node, optimizeBand, const_cast<void *>(static_cast<const void *>(OAI))); |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1305 | return Node; |
| 1306 | } |
| 1307 | |
| 1308 | bool ScheduleTreeOptimizer::isProfitableSchedule( |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1309 | Scop &S, __isl_keep isl_schedule *NewSchedule) { |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1310 | // To understand if the schedule has been optimized we check if the schedule |
| 1311 | // has changed at all. |
| 1312 | // TODO: We can improve this by tracking if any necessarily beneficial |
| 1313 | // transformations have been performed. This can e.g. be tiling, loop |
| 1314 | // interchange, or ...) We can track this either at the place where the |
| 1315 | // transformation has been performed or, in case of automatic ILP based |
| 1316 | // optimizations, by comparing (yet to be defined) performance metrics |
| 1317 | // before/after the scheduling optimizer |
| 1318 | // (e.g., #stride-one accesses) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1319 | if (S.containsExtensionNode(NewSchedule)) |
| 1320 | return true; |
| 1321 | auto *NewScheduleMap = isl_schedule_get_map(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1322 | isl_union_map *OldSchedule = S.getSchedule(); |
Tobias Grosser | ff40087 | 2017-02-01 10:12:09 +0000 | [diff] [blame] | 1323 | assert(OldSchedule && "Only IslScheduleOptimizer can insert extension nodes " |
| 1324 | "that make Scop::getSchedule() return nullptr."); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1325 | bool changed = !isl_union_map_is_equal(OldSchedule, NewScheduleMap); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1326 | isl_union_map_free(OldSchedule); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1327 | isl_union_map_free(NewScheduleMap); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1328 | return changed; |
| 1329 | } |
| 1330 | |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1331 | namespace { |
| 1332 | class IslScheduleOptimizer : public ScopPass { |
| 1333 | public: |
| 1334 | static char ID; |
| 1335 | explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; } |
| 1336 | |
| 1337 | ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); } |
| 1338 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1339 | /// Optimize the schedule of the SCoP @p S. |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1340 | bool runOnScop(Scop &S) override; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1341 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1342 | /// Print the new schedule for the SCoP @p S. |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 1343 | void printScop(raw_ostream &OS, Scop &S) const override; |
| 1344 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1345 | /// Register all analyses and transformation required. |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 1346 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1347 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1348 | /// Release the internal memory. |
Johannes Doerfert | 0f37630 | 2015-09-27 15:42:28 +0000 | [diff] [blame] | 1349 | void releaseMemory() override { |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1350 | isl_schedule_free(LastSchedule); |
| 1351 | LastSchedule = nullptr; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1352 | } |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 1353 | |
| 1354 | private: |
| 1355 | isl_schedule *LastSchedule; |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1356 | }; |
Tobias Grosser | 522478d | 2016-06-23 22:17:27 +0000 | [diff] [blame] | 1357 | } // namespace |
Tobias Grosser | fa57e9b | 2015-08-24 06:01:47 +0000 | [diff] [blame] | 1358 | |
| 1359 | char IslScheduleOptimizer::ID = 0; |
| 1360 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1361 | bool IslScheduleOptimizer::runOnScop(Scop &S) { |
Johannes Doerfert | 6f7921f | 2015-02-14 12:02:24 +0000 | [diff] [blame] | 1362 | |
| 1363 | // Skip empty SCoPs but still allow code generation as it will delete the |
| 1364 | // loops present but not needed. |
| 1365 | if (S.getSize() == 0) { |
| 1366 | S.markAsOptimized(); |
| 1367 | return false; |
| 1368 | } |
| 1369 | |
Hongbin Zheng | 2a79885 | 2016-03-03 08:15:33 +0000 | [diff] [blame] | 1370 | const Dependences &D = |
| 1371 | getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1372 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1373 | if (!D.hasValidDependences()) |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 1374 | return false; |
| 1375 | |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 1376 | isl_schedule_free(LastSchedule); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 1377 | LastSchedule = nullptr; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 1378 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1379 | // Build input data. |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1380 | int ValidityKinds = |
| 1381 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1382 | int ProximityKinds; |
| 1383 | |
| 1384 | if (OptimizeDeps == "all") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1385 | ProximityKinds = |
| 1386 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1387 | else if (OptimizeDeps == "raw") |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1388 | ProximityKinds = Dependences::TYPE_RAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1389 | else { |
| 1390 | errs() << "Do not know how to optimize for '" << OptimizeDeps << "'" |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1391 | << " Falling back to optimizing all dependences.\n"; |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1392 | ProximityKinds = |
| 1393 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1396 | isl_union_set *Domain = S.getDomains(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1397 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 1398 | if (!Domain) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1399 | return false; |
| 1400 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 1401 | isl_union_map *Validity = D.getDependences(ValidityKinds); |
| 1402 | isl_union_map *Proximity = D.getDependences(ProximityKinds); |
Tobias Grosser | 8a50702 | 2012-03-16 11:51:41 +0000 | [diff] [blame] | 1403 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 1404 | // Simplify the dependences by removing the constraints introduced by the |
| 1405 | // domains. This can speed up the scheduling time significantly, as large |
| 1406 | // constant coefficients will be removed from the dependences. The |
| 1407 | // introduction of some additional dependences reduces the possible |
| 1408 | // transformations, but in most cases, such transformation do not seem to be |
| 1409 | // interesting anyway. In some cases this option may stop the scheduler to |
| 1410 | // find any schedule. |
| 1411 | if (SimplifyDeps == "yes") { |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 1412 | Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain)); |
| 1413 | Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain)); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1414 | Proximity = |
| 1415 | isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 1416 | Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 1417 | } else if (SimplifyDeps != "no") { |
| 1418 | errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' " |
| 1419 | "or 'no'. Falling back to default: 'yes'\n"; |
| 1420 | } |
| 1421 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1422 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
Tobias Grosser | 01aea58 | 2014-10-22 23:16:28 +0000 | [diff] [blame] | 1423 | DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n"); |
| 1424 | DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n"); |
| 1425 | DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n"); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1426 | |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1427 | unsigned IslSerializeSCCs; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1428 | |
| 1429 | if (FusionStrategy == "max") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1430 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1431 | } else if (FusionStrategy == "min") { |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1432 | IslSerializeSCCs = 1; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1433 | } else { |
| 1434 | errs() << "warning: Unknown fusion strategy. Falling back to maximal " |
| 1435 | "fusion.\n"; |
Michael Kruse | c59f22c | 2015-06-18 16:45:40 +0000 | [diff] [blame] | 1436 | IslSerializeSCCs = 0; |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 1439 | int IslMaximizeBands; |
| 1440 | |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 1441 | if (MaximizeBandDepth == "yes") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 1442 | IslMaximizeBands = 1; |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 1443 | } else if (MaximizeBandDepth == "no") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 1444 | IslMaximizeBands = 0; |
| 1445 | } else { |
| 1446 | errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'" |
| 1447 | " or 'no'. Falling back to default: 'yes'\n"; |
| 1448 | IslMaximizeBands = 1; |
| 1449 | } |
| 1450 | |
Michael Kruse | 315aa32 | 2016-05-02 11:35:27 +0000 | [diff] [blame] | 1451 | int IslOuterCoincidence; |
| 1452 | |
| 1453 | if (OuterCoincidence == "yes") { |
| 1454 | IslOuterCoincidence = 1; |
| 1455 | } else if (OuterCoincidence == "no") { |
| 1456 | IslOuterCoincidence = 0; |
| 1457 | } else { |
| 1458 | errs() << "warning: Option -polly-opt-outer-coincidence should either be " |
| 1459 | "'yes' or 'no'. Falling back to default: 'no'\n"; |
| 1460 | IslOuterCoincidence = 0; |
| 1461 | } |
| 1462 | |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1463 | isl_ctx *Ctx = S.getIslCtx(); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 1464 | |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1465 | isl_options_set_schedule_outer_coincidence(Ctx, IslOuterCoincidence); |
| 1466 | isl_options_set_schedule_serialize_sccs(Ctx, IslSerializeSCCs); |
| 1467 | isl_options_set_schedule_maximize_band_depth(Ctx, IslMaximizeBands); |
| 1468 | isl_options_set_schedule_max_constant_term(Ctx, MaxConstantTerm); |
| 1469 | isl_options_set_schedule_max_coefficient(Ctx, MaxCoefficient); |
| 1470 | isl_options_set_tile_scale_tile_loops(Ctx, 0); |
| 1471 | |
Tobias Grosser | 3898a04 | 2016-06-30 20:42:58 +0000 | [diff] [blame] | 1472 | auto OnErrorStatus = isl_options_get_on_error(Ctx); |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1473 | isl_options_set_on_error(Ctx, ISL_ON_ERROR_CONTINUE); |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 1474 | |
| 1475 | isl_schedule_constraints *ScheduleConstraints; |
| 1476 | ScheduleConstraints = isl_schedule_constraints_on_domain(Domain); |
| 1477 | ScheduleConstraints = |
| 1478 | isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity); |
| 1479 | ScheduleConstraints = isl_schedule_constraints_set_validity( |
| 1480 | ScheduleConstraints, isl_union_map_copy(Validity)); |
| 1481 | ScheduleConstraints = |
| 1482 | isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 1483 | isl_schedule *Schedule; |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 1484 | Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints); |
Tobias Grosser | 3898a04 | 2016-06-30 20:42:58 +0000 | [diff] [blame] | 1485 | isl_options_set_on_error(Ctx, OnErrorStatus); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 1486 | |
| 1487 | // In cases the scheduler is not able to optimize the code, we just do not |
| 1488 | // touch the schedule. |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 1489 | if (!Schedule) |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 1490 | return false; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1491 | |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 1492 | DEBUG({ |
Tobias Grosser | af14993 | 2016-06-30 20:42:56 +0000 | [diff] [blame] | 1493 | auto *P = isl_printer_to_str(Ctx); |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 1494 | P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); |
| 1495 | P = isl_printer_print_schedule(P, Schedule); |
Michael Kruse | 79c0173 | 2016-12-12 14:51:06 +0000 | [diff] [blame] | 1496 | auto *str = isl_printer_get_str(P); |
| 1497 | dbgs() << "NewScheduleTree: \n" << str << "\n"; |
| 1498 | free(str); |
Tobias Grosser | 97d8745 | 2015-05-30 06:46:59 +0000 | [diff] [blame] | 1499 | isl_printer_free(P); |
| 1500 | }); |
Tobias Grosser | 4d63b9d | 2012-02-20 08:41:21 +0000 | [diff] [blame] | 1501 | |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1502 | Function &F = S.getFunction(); |
| 1503 | auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1504 | const OptimizerAdditionalInfoTy OAI = {TTI, const_cast<Dependences *>(&D)}; |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1505 | isl_schedule *NewSchedule = |
Roman Gareev | 98075fe | 2017-02-02 14:23:14 +0000 | [diff] [blame] | 1506 | ScheduleTreeOptimizer::optimizeSchedule(Schedule, &OAI); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1507 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1508 | if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewSchedule)) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1509 | isl_schedule_free(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1510 | return false; |
| 1511 | } |
| 1512 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1513 | S.setScheduleTree(NewSchedule); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1514 | S.markAsOptimized(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1515 | |
Roman Gareev | 5f99f86 | 2016-08-21 11:20:39 +0000 | [diff] [blame] | 1516 | if (OptimizedScops) |
| 1517 | S.dump(); |
| 1518 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1519 | return false; |
| 1520 | } |
| 1521 | |
Johannes Doerfert | 3fe584d | 2015-03-01 18:40:25 +0000 | [diff] [blame] | 1522 | void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const { |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 1523 | isl_printer *p; |
| 1524 | char *ScheduleStr; |
| 1525 | |
| 1526 | OS << "Calculated schedule:\n"; |
| 1527 | |
| 1528 | if (!LastSchedule) { |
| 1529 | OS << "n/a\n"; |
| 1530 | return; |
| 1531 | } |
| 1532 | |
| 1533 | p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule)); |
| 1534 | p = isl_printer_print_schedule(p, LastSchedule); |
| 1535 | ScheduleStr = isl_printer_get_str(p); |
| 1536 | isl_printer_free(p); |
| 1537 | |
| 1538 | OS << ScheduleStr << "\n"; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1541 | void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1542 | ScopPass::getAnalysisUsage(AU); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 1543 | AU.addRequired<DependenceInfo>(); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1544 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1547 | Pass *polly::createIslScheduleOptimizerPass() { |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1548 | return new IslScheduleOptimizer(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1549 | } |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1550 | |
| 1551 | INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", |
| 1552 | "Polly - Optimize schedule of SCoP", false, false); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 1553 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 1554 | INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass); |
Roman Gareev | 42402c9 | 2016-06-22 09:52:37 +0000 | [diff] [blame] | 1555 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1556 | INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", |
| 1557 | "Polly - Optimize schedule of SCoP", false, false) |