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