blob: cb0a8edbf9cc2511e9ace6c6cba67ed78b881826 [file] [log] [blame]
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001//===- 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 Grosser2219d152016-08-03 05:28:09 +000010// This pass generates an entirely new schedule tree from the data dependences
Tobias Grosser234a4822015-08-15 09:34:33 +000011// and iteration domains. The new schedule tree is computed in two steps:
Tobias Grosser30aa24c2011-05-14 19:02:06 +000012//
Tobias Grosser234a4822015-08-15 09:34:33 +000013// 1) The isl scheduling optimizer is run
14//
15// The isl scheduling optimizer creates a new schedule tree that maximizes
16// parallelism and tileability and minimizes data-dependence distances. The
17// algorithm used is a modified version of the ``Pluto'' algorithm:
18//
19// U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan.
20// A Practical Automatic Polyhedral Parallelizer and Locality Optimizer.
21// In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language
22// Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008.
23//
24// 2) A set of post-scheduling transformations is applied on the schedule tree.
25//
26// These optimizations include:
27//
28// - Tiling of the innermost tilable bands
29// - Prevectorization - The coice of a possible outer loop that is strip-mined
30// to the innermost level to enable inner-loop
31// vectorization.
32// - Some optimizations for spatial locality are also planned.
33//
34// For a detailed description of the schedule tree itself please see section 6
35// of:
36//
37// Polyhedral AST generation is more than scanning polyhedra
38// Tobias Grosser, Sven Verdoolaege, Albert Cohen
39// ACM Transations on Programming Languages and Systems (TOPLAS),
40// 37(4), July 2015
41// http://www.grosser.es/#pub-polyhedral-AST-generation
42//
43// This publication also contains a detailed discussion of the different options
44// for polyhedral loop unrolling, full/partial tile separation and other uses
45// of the schedule tree.
46//
Tobias Grosser30aa24c2011-05-14 19:02:06 +000047//===----------------------------------------------------------------------===//
48
Tobias Grosser967239c2011-10-23 20:59:44 +000049#include "polly/ScheduleOptimizer.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000050#include "polly/CodeGen/CodeGeneration.h"
51#include "polly/DependenceInfo.h"
52#include "polly/LinkAllPasses.h"
53#include "polly/Options.h"
54#include "polly/ScopInfo.h"
55#include "polly/Support/GICHelper.h"
Roman Gareev42402c92016-06-22 09:52:37 +000056#include "llvm/Analysis/TargetTransformInfo.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000057#include "llvm/Support/Debug.h"
Tobias Grosser2493e922011-12-07 07:42:57 +000058#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000059#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000060#include "isl/constraint.h"
61#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000062#include "isl/options.h"
Tobias Grosser97d87452015-05-30 06:46:59 +000063#include "isl/printer.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000064#include "isl/schedule.h"
Tobias Grosserbbb4cec2015-03-22 12:06:39 +000065#include "isl/schedule_node.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000066#include "isl/space.h"
Tobias Grossercd524dc2015-05-09 09:36:38 +000067#include "isl/union_map.h"
68#include "isl/union_set.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000069
70using namespace llvm;
71using namespace polly;
72
Chandler Carruth95fef942014-04-22 03:30:19 +000073#define DEBUG_TYPE "polly-opt-isl"
74
Tobias Grossera26db472012-01-30 19:38:43 +000075static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000076 OptimizeDeps("polly-opt-optimize-only",
77 cl::desc("Only a certain kind of dependences (all/raw)"),
78 cl::Hidden, cl::init("all"), cl::ZeroOrMore,
79 cl::cat(PollyCategory));
Tobias Grosser1deda292012-02-14 14:02:48 +000080
81static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000082 SimplifyDeps("polly-opt-simplify-deps",
83 cl::desc("Dependences should be simplified (yes/no)"),
84 cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
85 cl::cat(PollyCategory));
Tobias Grossera26db472012-01-30 19:38:43 +000086
Tobias Grosser483a90d2014-07-09 10:50:10 +000087static cl::opt<int> MaxConstantTerm(
88 "polly-opt-max-constant-term",
89 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
90 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser992e60c2012-02-20 08:41:15 +000091
Tobias Grosser483a90d2014-07-09 10:50:10 +000092static cl::opt<int> MaxCoefficient(
93 "polly-opt-max-coefficient",
94 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
95 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
96
97static cl::opt<std::string> FusionStrategy(
98 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
99 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser92f54802012-02-20 08:41:47 +0000100
Tobias Grossere602a072013-05-07 07:30:56 +0000101static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000102 MaximizeBandDepth("polly-opt-maximize-bands",
103 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
104 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserb3ad85b2012-01-30 19:38:50 +0000105
Michael Kruse315aa322016-05-02 11:35:27 +0000106static cl::opt<std::string> OuterCoincidence(
107 "polly-opt-outer-coincidence",
108 cl::desc("Try to construct schedules where the outer member of each band "
109 "satisfies the coincidence constraints (yes/no)"),
110 cl::Hidden, cl::init("no"), cl::ZeroOrMore, cl::cat(PollyCategory));
111
Tobias Grosser07c1c2f2015-08-19 08:46:11 +0000112static cl::opt<int> PrevectorWidth(
113 "polly-prevect-width",
114 cl::desc(
115 "The number of loop iterations to strip-mine for pre-vectorization"),
116 cl::Hidden, cl::init(4), cl::ZeroOrMore, cl::cat(PollyCategory));
117
Tobias Grosser04832712015-08-20 13:45:02 +0000118static cl::opt<bool> FirstLevelTiling("polly-tiling",
119 cl::desc("Enable loop tiling"),
120 cl::init(true), cl::ZeroOrMore,
121 cl::cat(PollyCategory));
122
Roman Gareev42402c92016-06-22 09:52:37 +0000123static cl::opt<int> LatencyVectorFma(
124 "polly-target-latency-vector-fma",
125 cl::desc("The minimal number of cycles between issuing two "
126 "dependent consecutive vector fused multiply-add "
127 "instructions."),
128 cl::Hidden, cl::init(8), cl::ZeroOrMore, cl::cat(PollyCategory));
129
Tobias Grosser0791d5f2016-12-23 07:33:39 +0000130static cl::opt<int> ThroughputVectorFma(
131 "polly-target-throughput-vector-fma",
Roman Gareev42402c92016-06-22 09:52:37 +0000132 cl::desc("A throughput of the processor floating-point arithmetic units "
133 "expressed in the number of vector fused multiply-add "
134 "instructions per clock cycle."),
135 cl::Hidden, cl::init(1), cl::ZeroOrMore, cl::cat(PollyCategory));
136
Roman Gareev1c2927b2016-12-25 16:32:28 +0000137// This option, along with --polly-target-2nd-cache-level-associativity,
138// --polly-target-1st-cache-level-size, and --polly-target-2st-cache-level-size
139// represent the parameters of the target cache, which do not have typical
140// values that can be used by default. However, to apply the pattern matching
141// optimizations, we use the values of the parameters of Intel Core i7-3820
142// SandyBridge in case the parameters are not specified. Such an approach helps
143// also to attain the high-performance on IBM POWER System S822 and IBM Power
144// 730 Express server.
145static cl::opt<int> FirstCacheLevelAssociativity(
146 "polly-target-1st-cache-level-associativity",
147 cl::desc("The associativity of the first cache level."), cl::Hidden,
148 cl::init(8), cl::ZeroOrMore, cl::cat(PollyCategory));
Roman Gareev3a18a932016-07-25 09:42:53 +0000149
Roman Gareev1c2927b2016-12-25 16:32:28 +0000150static cl::opt<int> SecondCacheLevelAssociativity(
151 "polly-target-2nd-cache-level-associativity",
152 cl::desc("The associativity of the second cache level."), cl::Hidden,
153 cl::init(8), cl::ZeroOrMore, cl::cat(PollyCategory));
154
155static cl::opt<int> FirstCacheLevelSize(
156 "polly-target-1st-cache-level-size",
157 cl::desc("The size of the first cache level specified in bytes."),
158 cl::Hidden, cl::init(32768), cl::ZeroOrMore, cl::cat(PollyCategory));
159
160static cl::opt<int> SecondCacheLevelSize(
161 "polly-target-2nd-cache-level-size",
162 cl::desc("The size of the second level specified in bytes."), cl::Hidden,
163 cl::init(262144), cl::ZeroOrMore, cl::cat(PollyCategory));
Roman Gareev3a18a932016-07-25 09:42:53 +0000164
Tobias Grosser67e94fb2017-01-14 07:14:54 +0000165static cl::opt<int> VectorRegisterBitwidth(
166 "polly-target-vector-register-bitwidth",
167 cl::desc("The size in bits of a vector register (if not set, this "
168 "information is taken from LLVM's target information."),
169 cl::Hidden, cl::init(-1), cl::ZeroOrMore, cl::cat(PollyCategory));
170
Tobias Grosser04832712015-08-20 13:45:02 +0000171static cl::opt<int> FirstLevelDefaultTileSize(
Tobias Grosser483a90d2014-07-09 10:50:10 +0000172 "polly-default-tile-size",
173 cl::desc("The default tile size (if not enough were provided by"
174 " --polly-tile-sizes)"),
175 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertc3958b22014-05-28 17:21:02 +0000176
Tobias Grosser21a059a2017-01-16 14:08:10 +0000177static cl::list<int>
178 FirstLevelTileSizes("polly-tile-sizes",
179 cl::desc("A tile size for each loop dimension, filled "
Tobias Grosser04832712015-08-20 13:45:02 +0000180 "with --polly-default-tile-size"),
Tobias Grosser21a059a2017-01-16 14:08:10 +0000181 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
182 cl::cat(PollyCategory));
Tobias Grosser04832712015-08-20 13:45:02 +0000183
184static cl::opt<bool>
185 SecondLevelTiling("polly-2nd-level-tiling",
186 cl::desc("Enable a 2nd level loop of loop tiling"),
187 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
188
189static cl::opt<int> SecondLevelDefaultTileSize(
190 "polly-2nd-level-default-tile-size",
191 cl::desc("The default 2nd-level tile size (if not enough were provided by"
192 " --polly-2nd-level-tile-sizes)"),
193 cl::Hidden, cl::init(16), cl::ZeroOrMore, cl::cat(PollyCategory));
194
195static cl::list<int>
196 SecondLevelTileSizes("polly-2nd-level-tile-sizes",
197 cl::desc("A tile size for each loop dimension, filled "
198 "with --polly-default-tile-size"),
199 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
200 cl::cat(PollyCategory));
201
Tobias Grosser42e24892015-08-20 13:45:05 +0000202static cl::opt<bool> RegisterTiling("polly-register-tiling",
203 cl::desc("Enable register tiling"),
204 cl::init(false), cl::ZeroOrMore,
205 cl::cat(PollyCategory));
206
207static cl::opt<int> RegisterDefaultTileSize(
208 "polly-register-tiling-default-tile-size",
209 cl::desc("The default register tile size (if not enough were provided by"
210 " --polly-register-tile-sizes)"),
211 cl::Hidden, cl::init(2), cl::ZeroOrMore, cl::cat(PollyCategory));
212
Roman Gareevbe5299a2016-12-21 12:51:12 +0000213static cl::opt<int> PollyPatternMatchingNcQuotient(
214 "polly-pattern-matching-nc-quotient",
215 cl::desc("Quotient that is obtained by dividing Nc, the parameter of the"
216 "macro-kernel, by Nr, the parameter of the micro-kernel"),
217 cl::Hidden, cl::init(256), cl::ZeroOrMore, cl::cat(PollyCategory));
218
Tobias Grosser42e24892015-08-20 13:45:05 +0000219static cl::list<int>
220 RegisterTileSizes("polly-register-tile-sizes",
221 cl::desc("A tile size for each loop dimension, filled "
222 "with --polly-register-tile-size"),
223 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
224 cl::cat(PollyCategory));
225
Roman Gareev9c3eb592016-05-28 16:17:58 +0000226static cl::opt<bool>
227 PMBasedOpts("polly-pattern-matching-based-opts",
228 cl::desc("Perform optimizations based on pattern matching"),
229 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
230
Roman Gareev5f99f862016-08-21 11:20:39 +0000231static cl::opt<bool> OptimizedScops(
232 "polly-optimized-scops",
233 cl::desc("Polly - Dump polyhedral description of Scops optimized with "
234 "the isl scheduling optimizer and the set of post-scheduling "
235 "transformations is applied on the schedule tree"),
236 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
237
Tobias Grosserc80d6972016-09-02 06:33:33 +0000238/// Create an isl_union_set, which describes the isolate option based on
239/// IsoalteDomain.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000240///
241/// @param IsolateDomain An isl_set whose last dimension is the only one that
242/// should belong to the current band node.
243static __isl_give isl_union_set *
244getIsolateOptions(__isl_take isl_set *IsolateDomain) {
245 auto Dims = isl_set_dim(IsolateDomain, isl_dim_set);
246 auto *IsolateRelation = isl_map_from_domain(IsolateDomain);
247 IsolateRelation = isl_map_move_dims(IsolateRelation, isl_dim_out, 0,
248 isl_dim_in, Dims - 1, 1);
249 auto *IsolateOption = isl_map_wrap(IsolateRelation);
Tobias Grosser8dd653d2016-06-22 16:22:00 +0000250 auto *Id = isl_id_alloc(isl_set_get_ctx(IsolateOption), "isolate", nullptr);
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000251 return isl_union_set_from_set(isl_set_set_tuple_id(IsolateOption, Id));
252}
253
Tobias Grosserc80d6972016-09-02 06:33:33 +0000254/// Create an isl_union_set, which describes the atomic option for the dimension
255/// of the current node.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000256///
257/// It may help to reduce the size of generated code.
258///
259/// @param Ctx An isl_ctx, which is used to create the isl_union_set.
260static __isl_give isl_union_set *getAtomicOptions(__isl_take isl_ctx *Ctx) {
261 auto *Space = isl_space_set_alloc(Ctx, 0, 1);
262 auto *AtomicOption = isl_set_universe(Space);
Tobias Grosser8dd653d2016-06-22 16:22:00 +0000263 auto *Id = isl_id_alloc(Ctx, "atomic", nullptr);
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000264 return isl_union_set_from_set(isl_set_set_tuple_id(AtomicOption, Id));
265}
266
Tobias Grosserc80d6972016-09-02 06:33:33 +0000267/// Make the last dimension of Set to take values from 0 to VectorWidth - 1.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000268///
269/// @param Set A set, which should be modified.
270/// @param VectorWidth A parameter, which determines the constraint.
271static __isl_give isl_set *addExtentConstraints(__isl_take isl_set *Set,
272 int VectorWidth) {
273 auto Dims = isl_set_dim(Set, isl_dim_set);
274 auto Space = isl_set_get_space(Set);
275 auto *LocalSpace = isl_local_space_from_space(Space);
276 auto *ExtConstr =
277 isl_constraint_alloc_inequality(isl_local_space_copy(LocalSpace));
278 ExtConstr = isl_constraint_set_constant_si(ExtConstr, 0);
279 ExtConstr =
280 isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, 1);
281 Set = isl_set_add_constraint(Set, ExtConstr);
282 ExtConstr = isl_constraint_alloc_inequality(LocalSpace);
283 ExtConstr = isl_constraint_set_constant_si(ExtConstr, VectorWidth - 1);
284 ExtConstr =
285 isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, -1);
286 return isl_set_add_constraint(Set, ExtConstr);
287}
288
Tobias Grosserc80d6972016-09-02 06:33:33 +0000289/// Build the desired set of partial tile prefixes.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000290///
291/// We build a set of partial tile prefixes, which are prefixes of the vector
292/// loop that have exactly VectorWidth iterations.
293///
294/// 1. Get all prefixes of the vector loop.
295/// 2. Extend it to a set, which has exactly VectorWidth iterations for
296/// any prefix from the set that was built on the previous step.
297/// 3. Subtract loop domain from it, project out the vector loop dimension and
Roman Gareev76614d32016-05-31 11:22:21 +0000298/// get a set of prefixes, which don't have exactly VectorWidth iterations.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000299/// 4. Subtract it from all prefixes of the vector loop and get the desired
300/// set.
301///
302/// @param ScheduleRange A range of a map, which describes a prefix schedule
303/// relation.
304static __isl_give isl_set *
305getPartialTilePrefixes(__isl_take isl_set *ScheduleRange, int VectorWidth) {
306 auto Dims = isl_set_dim(ScheduleRange, isl_dim_set);
307 auto *LoopPrefixes = isl_set_project_out(isl_set_copy(ScheduleRange),
308 isl_dim_set, Dims - 1, 1);
309 auto *ExtentPrefixes =
310 isl_set_add_dims(isl_set_copy(LoopPrefixes), isl_dim_set, 1);
311 ExtentPrefixes = addExtentConstraints(ExtentPrefixes, VectorWidth);
312 auto *BadPrefixes = isl_set_subtract(ExtentPrefixes, ScheduleRange);
313 BadPrefixes = isl_set_project_out(BadPrefixes, isl_dim_set, Dims - 1, 1);
314 return isl_set_subtract(LoopPrefixes, BadPrefixes);
315}
316
317__isl_give isl_schedule_node *ScheduleTreeOptimizer::isolateFullPartialTiles(
318 __isl_take isl_schedule_node *Node, int VectorWidth) {
319 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
320 Node = isl_schedule_node_child(Node, 0);
321 Node = isl_schedule_node_child(Node, 0);
322 auto *SchedRelUMap = isl_schedule_node_get_prefix_schedule_relation(Node);
323 auto *ScheduleRelation = isl_map_from_union_map(SchedRelUMap);
324 auto *ScheduleRange = isl_map_range(ScheduleRelation);
325 auto *IsolateDomain = getPartialTilePrefixes(ScheduleRange, VectorWidth);
326 auto *AtomicOption = getAtomicOptions(isl_set_get_ctx(IsolateDomain));
327 auto *IsolateOption = getIsolateOptions(IsolateDomain);
328 Node = isl_schedule_node_parent(Node);
329 Node = isl_schedule_node_parent(Node);
330 auto *Options = isl_union_set_union(IsolateOption, AtomicOption);
331 Node = isl_schedule_node_band_set_ast_build_options(Node, Options);
332 return Node;
333}
334
Tobias Grosserb241d922015-07-28 18:03:36 +0000335__isl_give isl_schedule_node *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000336ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
337 unsigned DimToVectorize,
338 int VectorWidth) {
Tobias Grosserb241d922015-07-28 18:03:36 +0000339 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000340
Tobias Grosserb241d922015-07-28 18:03:36 +0000341 auto Space = isl_schedule_node_band_get_space(Node);
342 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
343 isl_space_free(Space);
344 assert(DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000345
Tobias Grosserb241d922015-07-28 18:03:36 +0000346 if (DimToVectorize > 0) {
347 Node = isl_schedule_node_band_split(Node, DimToVectorize);
348 Node = isl_schedule_node_child(Node, 0);
349 }
350 if (DimToVectorize < ScheduleDimensions - 1)
351 Node = isl_schedule_node_band_split(Node, 1);
352 Space = isl_schedule_node_band_get_space(Node);
353 auto Sizes = isl_multi_val_zero(Space);
354 auto Ctx = isl_schedule_node_get_ctx(Node);
355 Sizes =
356 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
357 Node = isl_schedule_node_band_tile(Node, Sizes);
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000358 Node = isolateFullPartialTiles(Node, VectorWidth);
Tobias Grosserb241d922015-07-28 18:03:36 +0000359 Node = isl_schedule_node_child(Node, 0);
Tobias Grosser42e24892015-08-20 13:45:05 +0000360 // Make sure the "trivially vectorizable loop" is not unrolled. Otherwise,
361 // we will have troubles to match it in the backend.
362 Node = isl_schedule_node_band_set_ast_build_options(
Tobias Grosserfc490a92015-08-20 19:08:16 +0000363 Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }"));
364 Node = isl_schedule_node_band_sink(Node);
Tobias Grosserb241d922015-07-28 18:03:36 +0000365 Node = isl_schedule_node_child(Node, 0);
Roman Gareev11001e12016-02-23 09:00:13 +0000366 if (isl_schedule_node_get_type(Node) == isl_schedule_node_leaf)
367 Node = isl_schedule_node_parent(Node);
368 isl_id *LoopMarker = isl_id_alloc(Ctx, "SIMD", nullptr);
369 Node = isl_schedule_node_insert_mark(Node, LoopMarker);
Tobias Grosserb241d922015-07-28 18:03:36 +0000370 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000371}
372
Tobias Grosserd891b542015-08-20 12:16:23 +0000373__isl_give isl_schedule_node *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000374ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node,
375 const char *Identifier, ArrayRef<int> TileSizes,
376 int DefaultTileSize) {
Tobias Grosser9bdea572015-08-20 12:22:37 +0000377 auto Ctx = isl_schedule_node_get_ctx(Node);
378 auto Space = isl_schedule_node_band_get_space(Node);
379 auto Dims = isl_space_dim(Space, isl_dim_set);
380 auto Sizes = isl_multi_val_zero(Space);
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000381 std::string IdentifierString(Identifier);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000382 for (unsigned i = 0; i < Dims; i++) {
383 auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize;
384 Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
385 }
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000386 auto TileLoopMarkerStr = IdentifierString + " - Tiles";
387 isl_id *TileLoopMarker =
388 isl_id_alloc(Ctx, TileLoopMarkerStr.c_str(), nullptr);
389 Node = isl_schedule_node_insert_mark(Node, TileLoopMarker);
390 Node = isl_schedule_node_child(Node, 0);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000391 Node = isl_schedule_node_band_tile(Node, Sizes);
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000392 Node = isl_schedule_node_child(Node, 0);
393 auto PointLoopMarkerStr = IdentifierString + " - Points";
394 isl_id *PointLoopMarker =
395 isl_id_alloc(Ctx, PointLoopMarkerStr.c_str(), nullptr);
396 Node = isl_schedule_node_insert_mark(Node, PointLoopMarker);
397 Node = isl_schedule_node_child(Node, 0);
398 return Node;
Tobias Grosser9bdea572015-08-20 12:22:37 +0000399}
400
Roman Gareevb17b9a82016-06-12 17:20:05 +0000401__isl_give isl_schedule_node *
402ScheduleTreeOptimizer::applyRegisterTiling(__isl_take isl_schedule_node *Node,
403 llvm::ArrayRef<int> TileSizes,
404 int DefaultTileSize) {
405 auto *Ctx = isl_schedule_node_get_ctx(Node);
406 Node = tileNode(Node, "Register tiling", TileSizes, DefaultTileSize);
407 Node = isl_schedule_node_band_set_ast_build_options(
408 Node, isl_union_set_read_from_str(Ctx, "{unroll[x]}"));
409 return Node;
410}
411
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000412bool ScheduleTreeOptimizer::isTileableBandNode(
Tobias Grosser862b9b52015-08-20 12:32:45 +0000413 __isl_keep isl_schedule_node *Node) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000414 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000415 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000416
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000417 if (isl_schedule_node_n_children(Node) != 1)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000418 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000419
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000420 if (!isl_schedule_node_band_get_permutable(Node))
Tobias Grosser862b9b52015-08-20 12:32:45 +0000421 return false;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000422
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000423 auto Space = isl_schedule_node_band_get_space(Node);
424 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000425 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000426
Tobias Grosser9bdea572015-08-20 12:22:37 +0000427 if (Dims <= 1)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000428 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000429
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000430 auto Child = isl_schedule_node_get_child(Node, 0);
431 auto Type = isl_schedule_node_get_type(Child);
432 isl_schedule_node_free(Child);
433
Tobias Grosser9bdea572015-08-20 12:22:37 +0000434 if (Type != isl_schedule_node_leaf)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000435 return false;
436
437 return true;
438}
439
440__isl_give isl_schedule_node *
Roman Gareev9c3eb592016-05-28 16:17:58 +0000441ScheduleTreeOptimizer::standardBandOpts(__isl_take isl_schedule_node *Node,
442 void *User) {
Tobias Grosser04832712015-08-20 13:45:02 +0000443 if (FirstLevelTiling)
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000444 Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes,
445 FirstLevelDefaultTileSize);
Tobias Grosser04832712015-08-20 13:45:02 +0000446
447 if (SecondLevelTiling)
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000448 Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes,
449 SecondLevelDefaultTileSize);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000450
Roman Gareevb17b9a82016-06-12 17:20:05 +0000451 if (RegisterTiling)
452 Node =
453 applyRegisterTiling(Node, RegisterTileSizes, RegisterDefaultTileSize);
Tobias Grosser42e24892015-08-20 13:45:05 +0000454
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000455 if (PollyVectorizerChoice == VECTORIZER_NONE)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000456 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000457
Tobias Grosser862b9b52015-08-20 12:32:45 +0000458 auto Space = isl_schedule_node_band_get_space(Node);
459 auto Dims = isl_space_dim(Space, isl_dim_set);
460 isl_space_free(Space);
461
Tobias Grosserb241d922015-07-28 18:03:36 +0000462 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000463 if (isl_schedule_node_band_member_get_coincident(Node, i)) {
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000464 Node = prevectSchedBand(Node, i, PrevectorWidth);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000465 break;
466 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000467
Tobias Grosserf10f4632015-08-19 08:03:37 +0000468 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000469}
470
Roman Gareev98075fe2017-02-02 14:23:14 +0000471/// Get the position of a dimension with a non-zero coefficient.
Roman Gareev9c3eb592016-05-28 16:17:58 +0000472///
Roman Gareev98075fe2017-02-02 14:23:14 +0000473/// Check that isl constraint @p Constraint has only one non-zero
474/// coefficient for dimensions that have type @p DimType. If this is true,
475/// return the position of the dimension corresponding to the non-zero
476/// coefficient and negative value, otherwise.
477///
478/// @param Constraint The isl constraint to be checked.
479/// @param DimType The type of the dimensions.
480/// @return The position of the dimension in case the isl
481/// constraint satisfies the requirements, a negative
482/// value, otherwise.
483static int getMatMulConstraintDim(__isl_keep isl_constraint *Constraint,
484 enum isl_dim_type DimType) {
485 int DimPos = -1;
486 auto *LocalSpace = isl_constraint_get_local_space(Constraint);
487 int LocalSpaceDimNum = isl_local_space_dim(LocalSpace, DimType);
488 for (int i = 0; i < LocalSpaceDimNum; i++) {
489 auto *Val = isl_constraint_get_coefficient_val(Constraint, DimType, i);
490 if (isl_val_is_zero(Val)) {
491 isl_val_free(Val);
492 continue;
493 }
494 if (DimPos >= 0 || (DimType == isl_dim_out && !isl_val_is_one(Val)) ||
495 (DimType == isl_dim_in && !isl_val_is_negone(Val))) {
496 isl_val_free(Val);
497 isl_local_space_free(LocalSpace);
498 return -1;
499 }
500 DimPos = i;
501 isl_val_free(Val);
502 }
503 isl_local_space_free(LocalSpace);
504 return DimPos;
505}
506
507/// Check the form of the isl constraint.
508///
509/// Check that the @p DimInPos input dimension of the isl constraint
510/// @p Constraint has a coefficient that is equal to negative one, the @p
511/// DimOutPos has a coefficient that is equal to one and others
512/// have coefficients equal to zero.
513///
514/// @param Constraint The isl constraint to be checked.
515/// @param DimInPos The input dimension of the isl constraint.
516/// @param DimOutPos The output dimension of the isl constraint.
517/// @return isl_stat_ok in case the isl constraint satisfies
518/// the requirements, isl_stat_error otherwise.
519static isl_stat isMatMulOperandConstraint(__isl_keep isl_constraint *Constraint,
520 int &DimInPos, int &DimOutPos) {
521 auto *Val = isl_constraint_get_constant_val(Constraint);
522 if (!isl_constraint_is_equality(Constraint) || !isl_val_is_zero(Val)) {
523 isl_val_free(Val);
524 return isl_stat_error;
525 }
526 isl_val_free(Val);
527 DimInPos = getMatMulConstraintDim(Constraint, isl_dim_in);
528 if (DimInPos < 0)
529 return isl_stat_error;
530 DimOutPos = getMatMulConstraintDim(Constraint, isl_dim_out);
531 if (DimOutPos < 0)
532 return isl_stat_error;
533 return isl_stat_ok;
534}
535
536/// Check that the access relation corresponds to a non-constant operand
537/// of the matrix multiplication.
538///
539/// Access relations that correspond to non-constant operands of the matrix
540/// multiplication depend only on two input dimensions and have two output
541/// dimensions. The function checks that the isl basic map @p bmap satisfies
542/// the requirements. The two input dimensions can be specified via @p user
543/// array.
544///
545/// @param bmap The isl basic map to be checked.
546/// @param user The input dimensions of @p bmap.
547/// @return isl_stat_ok in case isl basic map satisfies the requirements,
548/// isl_stat_error otherwise.
549static isl_stat isMatMulOperandBasicMap(__isl_take isl_basic_map *bmap,
550 void *user) {
551 auto *Constraints = isl_basic_map_get_constraint_list(bmap);
552 isl_basic_map_free(bmap);
553 if (isl_constraint_list_n_constraint(Constraints) != 2) {
554 isl_constraint_list_free(Constraints);
555 return isl_stat_error;
556 }
557 int InPosPair[] = {-1, -1};
558 auto DimInPos = user ? static_cast<int *>(user) : InPosPair;
559 for (int i = 0; i < 2; i++) {
560 auto *Constraint = isl_constraint_list_get_constraint(Constraints, i);
561 int InPos, OutPos;
562 if (isMatMulOperandConstraint(Constraint, InPos, OutPos) ==
563 isl_stat_error ||
564 OutPos > 1 || (DimInPos[OutPos] >= 0 && DimInPos[OutPos] != InPos)) {
565 isl_constraint_free(Constraint);
566 isl_constraint_list_free(Constraints);
567 return isl_stat_error;
568 }
569 DimInPos[OutPos] = InPos;
570 isl_constraint_free(Constraint);
571 }
572 isl_constraint_list_free(Constraints);
573 return isl_stat_ok;
574}
575
576/// Permute the two dimensions of the isl map.
577///
578/// Permute @p DstPos and @p SrcPos dimensions of the isl map @p Map that
579/// have type @p DimType.
580///
581/// @param Map The isl map to be modified.
582/// @param DimType The type of the dimensions.
583/// @param DstPos The first dimension.
584/// @param SrcPos The second dimension.
585/// @return The modified map.
586__isl_give isl_map *permuteDimensions(__isl_take isl_map *Map,
587 enum isl_dim_type DimType,
588 unsigned DstPos, unsigned SrcPos) {
589 assert(DstPos < isl_map_dim(Map, DimType) &&
590 SrcPos < isl_map_dim(Map, DimType));
591 if (DstPos == SrcPos)
592 return Map;
593 isl_id *DimId = nullptr;
594 if (isl_map_has_tuple_id(Map, DimType))
595 DimId = isl_map_get_tuple_id(Map, DimType);
596 auto FreeDim = DimType == isl_dim_in ? isl_dim_out : isl_dim_in;
597 isl_id *FreeDimId = nullptr;
598 if (isl_map_has_tuple_id(Map, FreeDim))
599 FreeDimId = isl_map_get_tuple_id(Map, FreeDim);
600 auto MaxDim = std::max(DstPos, SrcPos);
601 auto MinDim = std::min(DstPos, SrcPos);
602 Map = isl_map_move_dims(Map, FreeDim, 0, DimType, MaxDim, 1);
603 Map = isl_map_move_dims(Map, FreeDim, 0, DimType, MinDim, 1);
604 Map = isl_map_move_dims(Map, DimType, MinDim, FreeDim, 1, 1);
605 Map = isl_map_move_dims(Map, DimType, MaxDim, FreeDim, 0, 1);
606 if (DimId)
607 Map = isl_map_set_tuple_id(Map, DimType, DimId);
608 if (FreeDimId)
609 Map = isl_map_set_tuple_id(Map, FreeDim, FreeDimId);
610 return Map;
611}
612
613/// Check the form of the access relation.
614///
615/// Check that the access relation @p AccMap has the form M[i][j], where i
616/// is a @p FirstPos and j is a @p SecondPos.
617///
618/// @param AccMap The access relation to be checked.
619/// @param FirstPos The index of the input dimension that is mapped to
620/// the first output dimension.
621/// @param SecondPos The index of the input dimension that is mapped to the
622/// second output dimension.
623/// @return True in case @p AccMap has the expected form and false,
624/// otherwise.
625static bool isMatMulOperandAcc(__isl_keep isl_map *AccMap, int &FirstPos,
626 int &SecondPos) {
627 int DimInPos[] = {FirstPos, SecondPos};
628 if (isl_map_foreach_basic_map(AccMap, isMatMulOperandBasicMap,
629 static_cast<void *>(DimInPos)) != isl_stat_ok ||
630 DimInPos[0] < 0 || DimInPos[1] < 0)
631 return false;
632 FirstPos = DimInPos[0];
633 SecondPos = DimInPos[1];
634 return true;
635}
636
637/// Does the memory access represent a non-scalar operand of the matrix
638/// multiplication.
639///
640/// Check that the memory access @p MemAccess is the read access to a non-scalar
641/// operand of the matrix multiplication or its result.
642///
643/// @param MemAccess The memory access to be checked.
644/// @param MMI Parameters of the matrix multiplication operands.
645/// @return True in case the memory access represents the read access
646/// to a non-scalar operand of the matrix multiplication and
647/// false, otherwise.
648static bool isMatMulNonScalarReadAccess(MemoryAccess *MemAccess,
649 MatMulInfoTy &MMI) {
650 if (!MemAccess->isArrayKind() || !MemAccess->isRead())
651 return false;
652 isl_map *AccMap = MemAccess->getAccessRelation();
653 if (isMatMulOperandAcc(AccMap, MMI.i, MMI.j) && !MMI.ReadFromC &&
654 isl_map_n_basic_map(AccMap) == 1) {
655 MMI.ReadFromC = MemAccess;
656 isl_map_free(AccMap);
657 return true;
658 }
659 if (isMatMulOperandAcc(AccMap, MMI.i, MMI.k) && !MMI.A &&
660 isl_map_n_basic_map(AccMap) == 1) {
661 MMI.A = MemAccess;
662 isl_map_free(AccMap);
663 return true;
664 }
665 if (isMatMulOperandAcc(AccMap, MMI.k, MMI.j) && !MMI.B &&
666 isl_map_n_basic_map(AccMap) == 1) {
667 MMI.B = MemAccess;
668 isl_map_free(AccMap);
669 return true;
670 }
671 isl_map_free(AccMap);
672 return false;
673}
674
675/// Check accesses to operands of the matrix multiplication.
676///
677/// Check that accesses of the SCoP statement, which corresponds to
678/// the partial schedule @p PartialSchedule, are scalar in terms of loops
679/// containing the matrix multiplication, in case they do not represent
680/// accesses to the non-scalar operands of the matrix multiplication or
681/// its result.
682///
683/// @param PartialSchedule The partial schedule of the SCoP statement.
684/// @param MMI Parameters of the matrix multiplication operands.
685/// @return True in case the corresponding SCoP statement
686/// represents matrix multiplication and false,
687/// otherwise.
688static bool containsOnlyMatrMultAcc(__isl_keep isl_map *PartialSchedule,
689 MatMulInfoTy &MMI) {
690 auto *InputDimId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in);
691 auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimId));
692 isl_id_free(InputDimId);
693 unsigned OutDimNum = isl_map_dim(PartialSchedule, isl_dim_out);
694 assert(OutDimNum > 2 && "In case of the matrix multiplication the loop nest "
695 "and, consequently, the corresponding scheduling "
696 "functions have at least three dimensions.");
697 auto *MapI = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out,
698 MMI.i, OutDimNum - 1);
699 auto *MapJ = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out,
700 MMI.j, OutDimNum - 1);
701 auto *MapK = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out,
702 MMI.k, OutDimNum - 1);
703 for (auto *MemA = Stmt->begin(); MemA != Stmt->end() - 1; MemA++) {
704 auto *MemAccessPtr = *MemA;
705 if (MemAccessPtr->isArrayKind() && MemAccessPtr != MMI.WriteToC &&
706 !isMatMulNonScalarReadAccess(MemAccessPtr, MMI) &&
707 !(MemAccessPtr->isStrideZero(isl_map_copy(MapI)) &&
708 MemAccessPtr->isStrideZero(isl_map_copy(MapJ)) &&
709 MemAccessPtr->isStrideZero(isl_map_copy(MapK)))) {
710 isl_map_free(MapI);
711 isl_map_free(MapJ);
712 isl_map_free(MapK);
713 return false;
714 }
715 }
716 isl_map_free(MapI);
717 isl_map_free(MapJ);
718 isl_map_free(MapK);
719 return true;
720}
721
722/// Check for dependencies corresponding to the matrix multiplication.
723///
724/// Check that there is only true dependence of the form
725/// S(..., k, ...) -> S(..., k + 1, …), where S is the SCoP statement
726/// represented by @p Schedule and k is @p Pos. Such a dependence corresponds
727/// to the dependency produced by the matrix multiplication.
728///
729/// @param Schedule The schedule of the SCoP statement.
730/// @param D The SCoP dependencies.
731/// @param Pos The parameter to desribe an acceptable true dependence.
732/// In case it has a negative value, try to determine its
733/// acceptable value.
734/// @return True in case dependencies correspond to the matrix multiplication
735/// and false, otherwise.
736static bool containsOnlyMatMulDep(__isl_keep isl_map *Schedule,
737 const Dependences *D, int &Pos) {
738 auto *WAR = D->getDependences(Dependences::TYPE_WAR);
739 if (!isl_union_map_is_empty(WAR)) {
740 isl_union_map_free(WAR);
741 return false;
742 }
743 isl_union_map_free(WAR);
744 auto *RAW = D->getDependences(Dependences::TYPE_RAW);
745 auto *Domain = isl_map_domain(isl_map_copy(Schedule));
746 auto *Space = isl_space_map_from_domain_and_range(isl_set_get_space(Domain),
747 isl_set_get_space(Domain));
748 isl_set_free(Domain);
749 auto *Deltas = isl_map_deltas(isl_union_map_extract_map(RAW, Space));
750 int DeltasDimNum = isl_set_dim(Deltas, isl_dim_set);
751 for (int i = 0; i < DeltasDimNum; i++) {
752 auto *Val = isl_set_plain_get_val_if_fixed(Deltas, isl_dim_set, i);
753 if (Pos < 0 && isl_val_is_one(Val))
754 Pos = i;
755 if (isl_val_is_nan(Val) ||
756 !(isl_val_is_zero(Val) || (i == Pos && isl_val_is_one(Val)))) {
757 isl_val_free(Val);
758 isl_union_map_free(RAW);
759 isl_set_free(Deltas);
760 return false;
761 }
762 isl_val_free(Val);
763 }
764 isl_union_map_free(RAW);
765 isl_set_free(Deltas);
766 return true;
Roman Gareev9c3eb592016-05-28 16:17:58 +0000767}
768
Tobias Grosserc80d6972016-09-02 06:33:33 +0000769/// Check if the SCoP statement could probably be optimized with analytical
770/// modeling.
Roman Gareev9c3eb592016-05-28 16:17:58 +0000771///
772/// containsMatrMult tries to determine whether the following conditions
773/// are true:
Roman Gareev98075fe2017-02-02 14:23:14 +0000774/// 1. The last memory access modeling an array, MA1, represents writing to
775/// memory and has the form S(..., i1, ..., i2, ...) -> M(i1, i2) or
776/// S(..., i2, ..., i1, ...) -> M(i1, i2), where S is the SCoP statement
777/// under consideration.
778/// 2. There is only one loop-carried true dependency, and it has the
779/// form S(..., i3, ...) -> S(..., i3 + 1, ...), and there are no
780/// loop-carried or anti dependencies.
781/// 3. SCoP contains three access relations, MA2, MA3, and MA4 that represent
782/// reading from memory and have the form S(..., i3, ...) -> M(i1, i3),
783/// S(..., i3, ...) -> M(i3, i2), S(...) -> M(i1, i2), respectively,
784/// and all memory accesses of the SCoP that are different from MA1, MA2,
785/// MA3, and MA4 have stride 0, if the innermost loop is exchanged with any
786/// of loops i1, i2 and i3.
Roman Gareev9c3eb592016-05-28 16:17:58 +0000787///
788/// @param PartialSchedule The PartialSchedule that contains a SCoP statement
789/// to check.
Roman Gareev98075fe2017-02-02 14:23:14 +0000790/// @D The SCoP dependencies.
791/// @MMI Parameters of the matrix multiplication operands.
792static bool containsMatrMult(__isl_keep isl_map *PartialSchedule,
793 const Dependences *D, MatMulInfoTy &MMI) {
794 auto *InputDimsId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in);
795 auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId));
Roman Gareev9c3eb592016-05-28 16:17:58 +0000796 isl_id_free(InputDimsId);
Roman Gareev98075fe2017-02-02 14:23:14 +0000797 if (Stmt->size() <= 1)
Roman Gareev9c3eb592016-05-28 16:17:58 +0000798 return false;
Roman Gareev98075fe2017-02-02 14:23:14 +0000799 for (auto *MemA = Stmt->end() - 1; MemA != Stmt->begin(); MemA--) {
800 auto *MemAccessPtr = *MemA;
801 if (!MemAccessPtr->isArrayKind())
802 continue;
803 if (!MemAccessPtr->isWrite())
Roman Gareev9c3eb592016-05-28 16:17:58 +0000804 return false;
Roman Gareev98075fe2017-02-02 14:23:14 +0000805 auto *AccMap = MemAccessPtr->getAccessRelation();
806 if (isl_map_n_basic_map(AccMap) != 1 ||
807 !isMatMulOperandAcc(AccMap, MMI.i, MMI.j)) {
808 isl_map_free(AccMap);
809 return false;
810 }
811 isl_map_free(AccMap);
812 MMI.WriteToC = MemAccessPtr;
813 break;
814 }
Roman Gareev9c3eb592016-05-28 16:17:58 +0000815
Roman Gareev98075fe2017-02-02 14:23:14 +0000816 if (!containsOnlyMatMulDep(PartialSchedule, D, MMI.k))
817 return false;
818
819 if (!MMI.WriteToC || !containsOnlyMatrMultAcc(PartialSchedule, MMI))
820 return false;
821
822 if (!MMI.A || !MMI.B || !MMI.ReadFromC)
823 return false;
824 return true;
Roman Gareev9c3eb592016-05-28 16:17:58 +0000825}
826
Tobias Grosserc80d6972016-09-02 06:33:33 +0000827/// Permute two dimensions of the band node.
Roman Gareev3a18a932016-07-25 09:42:53 +0000828///
829/// Permute FirstDim and SecondDim dimensions of the Node.
830///
831/// @param Node The band node to be modified.
832/// @param FirstDim The first dimension to be permuted.
833/// @param SecondDim The second dimension to be permuted.
834static __isl_give isl_schedule_node *
835permuteBandNodeDimensions(__isl_take isl_schedule_node *Node, unsigned FirstDim,
836 unsigned SecondDim) {
837 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band &&
838 isl_schedule_node_band_n_member(Node) > std::max(FirstDim, SecondDim));
839 auto PartialSchedule = isl_schedule_node_band_get_partial_schedule(Node);
840 auto PartialScheduleFirstDim =
841 isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, FirstDim);
842 auto PartialScheduleSecondDim =
843 isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, SecondDim);
844 PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff(
845 PartialSchedule, SecondDim, PartialScheduleFirstDim);
846 PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff(
847 PartialSchedule, FirstDim, PartialScheduleSecondDim);
848 Node = isl_schedule_node_delete(Node);
849 Node = isl_schedule_node_insert_partial_schedule(Node, PartialSchedule);
850 return Node;
851}
852
Roman Gareev2cb4d132016-07-25 07:27:59 +0000853__isl_give isl_schedule_node *ScheduleTreeOptimizer::createMicroKernel(
854 __isl_take isl_schedule_node *Node, MicroKernelParamsTy MicroKernelParams) {
Roman Gareev8babe1a2016-12-15 11:47:38 +0000855 applyRegisterTiling(Node, {MicroKernelParams.Mr, MicroKernelParams.Nr}, 1);
856 Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
857 Node = permuteBandNodeDimensions(Node, 0, 1);
858 return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
Roman Gareev2cb4d132016-07-25 07:27:59 +0000859}
860
Roman Gareev3a18a932016-07-25 09:42:53 +0000861__isl_give isl_schedule_node *ScheduleTreeOptimizer::createMacroKernel(
862 __isl_take isl_schedule_node *Node, MacroKernelParamsTy MacroKernelParams) {
863 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
864 if (MacroKernelParams.Mc == 1 && MacroKernelParams.Nc == 1 &&
865 MacroKernelParams.Kc == 1)
866 return Node;
Roman Gareev98075fe2017-02-02 14:23:14 +0000867 int DimOutNum = isl_schedule_node_band_n_member(Node);
868 std::vector<int> TileSizes(DimOutNum, 1);
869 TileSizes[DimOutNum - 3] = MacroKernelParams.Mc;
870 TileSizes[DimOutNum - 2] = MacroKernelParams.Nc;
871 TileSizes[DimOutNum - 1] = MacroKernelParams.Kc;
872 Node = tileNode(Node, "1st level tiling", TileSizes, 1);
Roman Gareev3a18a932016-07-25 09:42:53 +0000873 Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
Roman Gareev98075fe2017-02-02 14:23:14 +0000874 Node = permuteBandNodeDimensions(Node, DimOutNum - 2, DimOutNum - 1);
875 Node = permuteBandNodeDimensions(Node, DimOutNum - 3, DimOutNum - 1);
Roman Gareev3a18a932016-07-25 09:42:53 +0000876 return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
877}
878
Roman Gareev2cb4d132016-07-25 07:27:59 +0000879/// Get parameters of the BLIS micro kernel.
880///
881/// We choose the Mr and Nr parameters of the micro kernel to be large enough
882/// such that no stalls caused by the combination of latencies and dependencies
883/// are introduced during the updates of the resulting matrix of the matrix
884/// multiplication. However, they should also be as small as possible to
885/// release more registers for entries of multiplied matrices.
886///
887/// @param TTI Target Transform Info.
888/// @return The structure of type MicroKernelParamsTy.
889/// @see MicroKernelParamsTy
890static struct MicroKernelParamsTy
891getMicroKernelParams(const llvm::TargetTransformInfo *TTI) {
Roman Gareev42402c92016-06-22 09:52:37 +0000892 assert(TTI && "The target transform info should be provided.");
Roman Gareev2cb4d132016-07-25 07:27:59 +0000893
Roman Gareev42402c92016-06-22 09:52:37 +0000894 // Nvec - Number of double-precision floating-point numbers that can be hold
895 // by a vector register. Use 2 by default.
Tobias Grosser67e94fb2017-01-14 07:14:54 +0000896 long RegisterBitwidth = VectorRegisterBitwidth;
897
898 if (RegisterBitwidth == -1)
899 RegisterBitwidth = TTI->getRegisterBitWidth(true);
900 auto Nvec = RegisterBitwidth / 64;
Roman Gareev42402c92016-06-22 09:52:37 +0000901 if (Nvec == 0)
902 Nvec = 2;
903 int Nr =
Tobias Grosser0791d5f2016-12-23 07:33:39 +0000904 ceil(sqrt(Nvec * LatencyVectorFma * ThroughputVectorFma) / Nvec) * Nvec;
905 int Mr = ceil(Nvec * LatencyVectorFma * ThroughputVectorFma / Nr);
Roman Gareev2cb4d132016-07-25 07:27:59 +0000906 return {Mr, Nr};
907}
908
Roman Gareev3a18a932016-07-25 09:42:53 +0000909/// Get parameters of the BLIS macro kernel.
910///
911/// During the computation of matrix multiplication, blocks of partitioned
912/// matrices are mapped to different layers of the memory hierarchy.
913/// To optimize data reuse, blocks should be ideally kept in cache between
914/// iterations. Since parameters of the macro kernel determine sizes of these
915/// blocks, there are upper and lower bounds on these parameters.
916///
917/// @param MicroKernelParams Parameters of the micro-kernel
918/// to be taken into account.
919/// @return The structure of type MacroKernelParamsTy.
920/// @see MacroKernelParamsTy
921/// @see MicroKernelParamsTy
922static struct MacroKernelParamsTy
923getMacroKernelParams(const MicroKernelParamsTy &MicroKernelParams) {
924 // According to www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf,
925 // it requires information about the first two levels of a cache to determine
926 // all the parameters of a macro-kernel. It also checks that an associativity
927 // degree of a cache level is greater than two. Otherwise, another algorithm
928 // for determination of the parameters should be used.
929 if (!(MicroKernelParams.Mr > 0 && MicroKernelParams.Nr > 0 &&
Roman Gareev1c2927b2016-12-25 16:32:28 +0000930 FirstCacheLevelSize > 0 && SecondCacheLevelSize > 0 &&
931 FirstCacheLevelAssociativity > 2 && SecondCacheLevelAssociativity > 2))
Roman Gareev3a18a932016-07-25 09:42:53 +0000932 return {1, 1, 1};
Roman Gareevbe5299a2016-12-21 12:51:12 +0000933 // The quotient should be greater than zero.
934 if (PollyPatternMatchingNcQuotient <= 0)
935 return {1, 1, 1};
Roman Gareev15db81e2016-12-15 12:00:57 +0000936 int Car = floor(
Roman Gareev1c2927b2016-12-25 16:32:28 +0000937 (FirstCacheLevelAssociativity - 1) /
Roman Gareev8babe1a2016-12-15 11:47:38 +0000938 (1 + static_cast<double>(MicroKernelParams.Nr) / MicroKernelParams.Mr));
Roman Gareev1c2927b2016-12-25 16:32:28 +0000939 int Kc = (Car * FirstCacheLevelSize) /
940 (MicroKernelParams.Mr * FirstCacheLevelAssociativity * 8);
941 double Cac = static_cast<double>(Kc * 8 * SecondCacheLevelAssociativity) /
942 SecondCacheLevelSize;
943 int Mc = floor((SecondCacheLevelAssociativity - 2) / Cac);
Roman Gareevbe5299a2016-12-21 12:51:12 +0000944 int Nc = PollyPatternMatchingNcQuotient * MicroKernelParams.Nr;
Roman Gareev3a18a932016-07-25 09:42:53 +0000945 return {Mc, Nc, Kc};
946}
947
Tobias Grosserc80d6972016-09-02 06:33:33 +0000948/// Create an access relation that is specific to
Roman Gareev1c892e92016-08-15 12:22:54 +0000949/// the matrix multiplication pattern.
950///
951/// Create an access relation of the following form:
Roman Gareev92c44602016-12-21 11:18:42 +0000952/// [O0, O1, O2, O3, O4, O5, O6, O7, O8] -> [OI, O5, OJ]
953/// where I is @p FirstDim, J is @p SecondDim.
Roman Gareev1c892e92016-08-15 12:22:54 +0000954///
955/// It can be used, for example, to create relations that helps to consequently
956/// access elements of operands of a matrix multiplication after creation of
957/// the BLIS micro and macro kernels.
958///
959/// @see ScheduleTreeOptimizer::createMicroKernel
960/// @see ScheduleTreeOptimizer::createMacroKernel
961///
962/// Subsequently, the described access relation is applied to the range of
963/// @p MapOldIndVar, that is used to map original induction variables to
964/// the ones, which are produced by schedule transformations. It helps to
965/// define relations using a new space and, at the same time, keep them
966/// in the original one.
967///
968/// @param MapOldIndVar The relation, which maps original induction variables
969/// to the ones, which are produced by schedule
970/// transformations.
Roman Gareev1c892e92016-08-15 12:22:54 +0000971/// @param FirstDim, SecondDim The input dimensions that are used to define
972/// the specified access relation.
973/// @return The specified access relation.
974__isl_give isl_map *getMatMulAccRel(__isl_take isl_map *MapOldIndVar,
Roman Gareev92c44602016-12-21 11:18:42 +0000975 unsigned FirstDim, unsigned SecondDim) {
Roman Gareev1c892e92016-08-15 12:22:54 +0000976 auto *Ctx = isl_map_get_ctx(MapOldIndVar);
Roman Gareev92c44602016-12-21 11:18:42 +0000977 auto *AccessRelSpace = isl_space_alloc(Ctx, 0, 9, 3);
978 auto *AccessRel = isl_map_universe(AccessRelSpace);
979 AccessRel = isl_map_equate(AccessRel, isl_dim_in, FirstDim, isl_dim_out, 0);
980 AccessRel = isl_map_equate(AccessRel, isl_dim_in, 5, isl_dim_out, 1);
981 AccessRel = isl_map_equate(AccessRel, isl_dim_in, SecondDim, isl_dim_out, 2);
Roman Gareev1c892e92016-08-15 12:22:54 +0000982 return isl_map_apply_range(MapOldIndVar, AccessRel);
983}
984
Roman Gareevb3224ad2016-09-14 06:26:09 +0000985__isl_give isl_schedule_node *
986createExtensionNode(__isl_take isl_schedule_node *Node,
987 __isl_take isl_map *ExtensionMap) {
988 auto *Extension = isl_union_map_from_map(ExtensionMap);
989 auto *NewNode = isl_schedule_node_from_extension(Extension);
990 return isl_schedule_node_graft_before(Node, NewNode);
991}
992
Tobias Grosserc80d6972016-09-02 06:33:33 +0000993/// Apply the packing transformation.
Roman Gareev1c892e92016-08-15 12:22:54 +0000994///
995/// The packing transformation can be described as a data-layout
996/// transformation that requires to introduce a new array, copy data
Roman Gareev7758a2a2017-01-29 10:37:50 +0000997/// to the array, and change memory access locations to reference the array.
998/// It can be used to ensure that elements of the new array are read in-stride
999/// access, aligned to cache lines boundaries, and preloaded into certain cache
1000/// levels.
1001///
1002/// As an example let us consider the packing of the array A that would help
1003/// to read its elements with in-stride access. An access to the array A
1004/// is represented by an access relation that has the form
1005/// S[i, j, k] -> A[i, k]. The scheduling function of the SCoP statement S has
1006/// the form S[i,j, k] -> [floor((j mod Nc) / Nr), floor((i mod Mc) / Mr),
1007/// k mod Kc, j mod Nr, i mod Mr].
1008///
1009/// To ensure that elements of the array A are read in-stride access, we add
1010/// a new array Packed_A[Mc/Mr][Kc][Mr] to the SCoP, using
1011/// Scop::createScopArrayInfo, change the access relation
1012/// S[i, j, k] -> A[i, k] to
1013/// S[i, j, k] -> Packed_A[floor((i mod Mc) / Mr), k mod Kc, i mod Mr], using
1014/// MemoryAccess::setNewAccessRelation, and copy the data to the array, using
1015/// the copy statement created by Scop::addScopStmt.
Roman Gareev1c892e92016-08-15 12:22:54 +00001016///
1017/// @param Node The schedule node to be optimized.
1018/// @param MapOldIndVar The relation, which maps original induction variables
1019/// to the ones, which are produced by schedule
1020/// transformations.
1021/// @param MicroParams, MacroParams Parameters of the BLIS kernel
1022/// to be taken into account.
Roman Gareev98075fe2017-02-02 14:23:14 +00001023/// @param MMI Parameters of the matrix multiplication operands.
Roman Gareev1c892e92016-08-15 12:22:54 +00001024/// @return The optimized schedule node.
Roman Gareevb3224ad2016-09-14 06:26:09 +00001025static __isl_give isl_schedule_node *optimizeDataLayoutMatrMulPattern(
1026 __isl_take isl_schedule_node *Node, __isl_take isl_map *MapOldIndVar,
Roman Gareev98075fe2017-02-02 14:23:14 +00001027 MicroKernelParamsTy MicroParams, MacroKernelParamsTy MacroParams,
1028 MatMulInfoTy &MMI) {
Roman Gareev1c892e92016-08-15 12:22:54 +00001029 auto InputDimsId = isl_map_get_tuple_id(MapOldIndVar, isl_dim_in);
1030 auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId));
1031 isl_id_free(InputDimsId);
Roman Gareev2606c482016-12-15 12:35:59 +00001032
1033 // Create a copy statement that corresponds to the memory access to the
1034 // matrix B, the second operand of the matrix multiplication.
Roman Gareevb3224ad2016-09-14 06:26:09 +00001035 Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
1036 Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
1037 Node = isl_schedule_node_parent(Node);
1038 Node = isl_schedule_node_child(isl_schedule_node_band_split(Node, 2), 0);
Roman Gareev92c44602016-12-21 11:18:42 +00001039 auto *AccRel = getMatMulAccRel(isl_map_copy(MapOldIndVar), 3, 7);
1040 unsigned FirstDimSize = MacroParams.Nc / MicroParams.Nr;
1041 unsigned SecondDimSize = MacroParams.Kc;
1042 unsigned ThirdDimSize = MicroParams.Nr;
Roman Gareev1c892e92016-08-15 12:22:54 +00001043 auto *SAI = Stmt->getParent()->createScopArrayInfo(
Roman Gareev98075fe2017-02-02 14:23:14 +00001044 MMI.B->getElementType(), "Packed_B",
Roman Gareev92c44602016-12-21 11:18:42 +00001045 {FirstDimSize, SecondDimSize, ThirdDimSize});
Roman Gareev1c892e92016-08-15 12:22:54 +00001046 AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId());
Roman Gareev98075fe2017-02-02 14:23:14 +00001047 auto *OldAcc = MMI.B->getAccessRelation();
1048 MMI.B->setNewAccessRelation(AccRel);
Roman Gareevb3224ad2016-09-14 06:26:09 +00001049 auto *ExtMap =
Roman Gareev98075fe2017-02-02 14:23:14 +00001050 isl_map_project_out(isl_map_copy(MapOldIndVar), isl_dim_out, 2,
1051 isl_map_dim(MapOldIndVar, isl_dim_out) - 2);
1052 ExtMap = isl_map_reverse(ExtMap);
1053 ExtMap = isl_map_fix_si(ExtMap, isl_dim_out, MMI.i, 0);
Roman Gareevb3224ad2016-09-14 06:26:09 +00001054 auto *Domain = Stmt->getDomain();
Roman Gareev2606c482016-12-15 12:35:59 +00001055
1056 // Restrict the domains of the copy statements to only execute when also its
1057 // originating statement is executed.
1058 auto *DomainId = isl_set_get_tuple_id(Domain);
Roman Gareevb3224ad2016-09-14 06:26:09 +00001059 auto *NewStmt = Stmt->getParent()->addScopStmt(
Roman Gareev98075fe2017-02-02 14:23:14 +00001060 OldAcc, MMI.B->getAccessRelation(), isl_set_copy(Domain));
Roman Gareev2606c482016-12-15 12:35:59 +00001061 ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, isl_id_copy(DomainId));
1062 ExtMap = isl_map_intersect_range(ExtMap, isl_set_copy(Domain));
Roman Gareevb3224ad2016-09-14 06:26:09 +00001063 ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId());
1064 Node = createExtensionNode(Node, ExtMap);
Roman Gareev2606c482016-12-15 12:35:59 +00001065
1066 // Create a copy statement that corresponds to the memory access
1067 // to the matrix A, the first operand of the matrix multiplication.
Roman Gareevb3224ad2016-09-14 06:26:09 +00001068 Node = isl_schedule_node_child(Node, 0);
Roman Gareev98075fe2017-02-02 14:23:14 +00001069 AccRel = getMatMulAccRel(isl_map_copy(MapOldIndVar), 4, 6);
Roman Gareev92c44602016-12-21 11:18:42 +00001070 FirstDimSize = MacroParams.Mc / MicroParams.Mr;
1071 ThirdDimSize = MicroParams.Mr;
Roman Gareev1c892e92016-08-15 12:22:54 +00001072 SAI = Stmt->getParent()->createScopArrayInfo(
Roman Gareev98075fe2017-02-02 14:23:14 +00001073 MMI.A->getElementType(), "Packed_A",
Roman Gareev92c44602016-12-21 11:18:42 +00001074 {FirstDimSize, SecondDimSize, ThirdDimSize});
Roman Gareev1c892e92016-08-15 12:22:54 +00001075 AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId());
Roman Gareev98075fe2017-02-02 14:23:14 +00001076 OldAcc = MMI.A->getAccessRelation();
1077 MMI.A->setNewAccessRelation(AccRel);
1078 ExtMap = isl_map_project_out(MapOldIndVar, isl_dim_out, 3,
1079 isl_map_dim(MapOldIndVar, isl_dim_out) - 3);
1080 ExtMap = isl_map_reverse(ExtMap);
1081 ExtMap = isl_map_fix_si(ExtMap, isl_dim_out, MMI.j, 0);
1082 NewStmt = Stmt->getParent()->addScopStmt(OldAcc, MMI.A->getAccessRelation(),
1083 isl_set_copy(Domain));
Roman Gareev2606c482016-12-15 12:35:59 +00001084
1085 // Restrict the domains of the copy statements to only execute when also its
1086 // originating statement is executed.
1087 ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, DomainId);
1088 ExtMap = isl_map_intersect_range(ExtMap, Domain);
Roman Gareevb3224ad2016-09-14 06:26:09 +00001089 ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId());
1090 Node = createExtensionNode(Node, ExtMap);
1091 Node = isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
1092 return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
Roman Gareev1c892e92016-08-15 12:22:54 +00001093}
1094
Tobias Grosserc80d6972016-09-02 06:33:33 +00001095/// Get a relation mapping induction variables produced by schedule
1096/// transformations to the original ones.
Roman Gareev1c892e92016-08-15 12:22:54 +00001097///
1098/// @param Node The schedule node produced as the result of creation
1099/// of the BLIS kernels.
1100/// @param MicroKernelParams, MacroKernelParams Parameters of the BLIS kernel
1101/// to be taken into account.
1102/// @return The relation mapping original induction variables to the ones
1103/// produced by schedule transformation.
1104/// @see ScheduleTreeOptimizer::createMicroKernel
1105/// @see ScheduleTreeOptimizer::createMacroKernel
1106/// @see getMacroKernelParams
1107__isl_give isl_map *
1108getInductionVariablesSubstitution(__isl_take isl_schedule_node *Node,
1109 MicroKernelParamsTy MicroKernelParams,
1110 MacroKernelParamsTy MacroKernelParams) {
1111 auto *Child = isl_schedule_node_get_child(Node, 0);
1112 auto *UnMapOldIndVar = isl_schedule_node_get_prefix_schedule_union_map(Child);
1113 isl_schedule_node_free(Child);
1114 auto *MapOldIndVar = isl_map_from_union_map(UnMapOldIndVar);
1115 if (isl_map_dim(MapOldIndVar, isl_dim_out) > 9)
1116 MapOldIndVar =
1117 isl_map_project_out(MapOldIndVar, isl_dim_out, 0,
1118 isl_map_dim(MapOldIndVar, isl_dim_out) - 9);
1119 return MapOldIndVar;
1120}
1121
Roman Gareev2cb4d132016-07-25 07:27:59 +00001122__isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeMatMulPattern(
Roman Gareev98075fe2017-02-02 14:23:14 +00001123 __isl_take isl_schedule_node *Node, const llvm::TargetTransformInfo *TTI,
1124 MatMulInfoTy &MMI) {
Roman Gareev2cb4d132016-07-25 07:27:59 +00001125 assert(TTI && "The target transform info should be provided.");
Roman Gareev98075fe2017-02-02 14:23:14 +00001126 int DimOutNum = isl_schedule_node_band_n_member(Node);
1127 assert(DimOutNum > 2 && "In case of the matrix multiplication the loop nest "
1128 "and, consequently, the corresponding scheduling "
1129 "functions have at least three dimensions.");
1130 Node = permuteBandNodeDimensions(Node, MMI.i, DimOutNum - 3);
1131 int NewJ = MMI.j == DimOutNum - 3 ? MMI.i : MMI.j;
1132 int NewK = MMI.k == DimOutNum - 3 ? MMI.i : MMI.k;
1133 Node = permuteBandNodeDimensions(Node, NewJ, DimOutNum - 2);
1134 NewK = MMI.k == DimOutNum - 2 ? MMI.j : MMI.k;
1135 Node = permuteBandNodeDimensions(Node, NewK, DimOutNum - 1);
Roman Gareev2cb4d132016-07-25 07:27:59 +00001136 auto MicroKernelParams = getMicroKernelParams(TTI);
Roman Gareev3a18a932016-07-25 09:42:53 +00001137 auto MacroKernelParams = getMacroKernelParams(MicroKernelParams);
1138 Node = createMacroKernel(Node, MacroKernelParams);
Roman Gareev2cb4d132016-07-25 07:27:59 +00001139 Node = createMicroKernel(Node, MicroKernelParams);
Roman Gareev1c892e92016-08-15 12:22:54 +00001140 if (MacroKernelParams.Mc == 1 || MacroKernelParams.Nc == 1 ||
1141 MacroKernelParams.Kc == 1)
1142 return Node;
1143 auto *MapOldIndVar = getInductionVariablesSubstitution(
1144 Node, MicroKernelParams, MacroKernelParams);
1145 if (!MapOldIndVar)
1146 return Node;
Roman Gareevb3224ad2016-09-14 06:26:09 +00001147 return optimizeDataLayoutMatrMulPattern(Node, MapOldIndVar, MicroKernelParams,
Roman Gareev98075fe2017-02-02 14:23:14 +00001148 MacroKernelParams, MMI);
Roman Gareev42402c92016-06-22 09:52:37 +00001149}
1150
Roman Gareev9c3eb592016-05-28 16:17:58 +00001151bool ScheduleTreeOptimizer::isMatrMultPattern(
Roman Gareev98075fe2017-02-02 14:23:14 +00001152 __isl_keep isl_schedule_node *Node, const Dependences *D,
1153 MatMulInfoTy &MMI) {
Roman Gareev9c3eb592016-05-28 16:17:58 +00001154 auto *PartialSchedule =
1155 isl_schedule_node_band_get_partial_schedule_union_map(Node);
Roman Gareev98075fe2017-02-02 14:23:14 +00001156 if (isl_schedule_node_band_n_member(Node) < 3 ||
Roman Gareev397a34a2016-06-22 12:11:30 +00001157 isl_union_map_n_map(PartialSchedule) != 1) {
1158 isl_union_map_free(PartialSchedule);
Roman Gareev9c3eb592016-05-28 16:17:58 +00001159 return false;
1160 }
Roman Gareev397a34a2016-06-22 12:11:30 +00001161 auto *NewPartialSchedule = isl_map_from_union_map(PartialSchedule);
Roman Gareev98075fe2017-02-02 14:23:14 +00001162 if (containsMatrMult(NewPartialSchedule, D, MMI)) {
Roman Gareev9c3eb592016-05-28 16:17:58 +00001163 isl_map_free(NewPartialSchedule);
1164 return true;
1165 }
1166 isl_map_free(NewPartialSchedule);
1167 return false;
1168}
1169
1170__isl_give isl_schedule_node *
1171ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node,
1172 void *User) {
1173 if (!isTileableBandNode(Node))
1174 return Node;
1175
Roman Gareev98075fe2017-02-02 14:23:14 +00001176 const OptimizerAdditionalInfoTy *OAI =
1177 static_cast<const OptimizerAdditionalInfoTy *>(User);
1178
1179 MatMulInfoTy MMI;
1180 if (PMBasedOpts && User && isMatrMultPattern(Node, OAI->D, MMI)) {
Roman Gareev9c3eb592016-05-28 16:17:58 +00001181 DEBUG(dbgs() << "The matrix multiplication pattern was detected\n");
Roman Gareev98075fe2017-02-02 14:23:14 +00001182 Node = optimizeMatMulPattern(Node, OAI->TTI, MMI);
Roman Gareev42402c92016-06-22 09:52:37 +00001183 }
Roman Gareev9c3eb592016-05-28 16:17:58 +00001184
1185 return standardBandOpts(Node, User);
1186}
1187
Tobias Grosser808cd692015-07-14 09:33:13 +00001188__isl_give isl_schedule *
Roman Gareev42402c92016-06-22 09:52:37 +00001189ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule,
Roman Gareev98075fe2017-02-02 14:23:14 +00001190 const OptimizerAdditionalInfoTy *OAI) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +00001191 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Roman Gareev98075fe2017-02-02 14:23:14 +00001192 Root = optimizeScheduleNode(Root, OAI);
Tobias Grosser808cd692015-07-14 09:33:13 +00001193 isl_schedule_free(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +00001194 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +00001195 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +00001196 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001197}
1198
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001199__isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode(
Roman Gareev98075fe2017-02-02 14:23:14 +00001200 __isl_take isl_schedule_node *Node, const OptimizerAdditionalInfoTy *OAI) {
Roman Gareev42402c92016-06-22 09:52:37 +00001201 Node = isl_schedule_node_map_descendant_bottom_up(
Roman Gareev98075fe2017-02-02 14:23:14 +00001202 Node, optimizeBand, const_cast<void *>(static_cast<const void *>(OAI)));
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001203 return Node;
1204}
1205
1206bool ScheduleTreeOptimizer::isProfitableSchedule(
Roman Gareevb3224ad2016-09-14 06:26:09 +00001207 Scop &S, __isl_keep isl_schedule *NewSchedule) {
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001208 // To understand if the schedule has been optimized we check if the schedule
1209 // has changed at all.
1210 // TODO: We can improve this by tracking if any necessarily beneficial
1211 // transformations have been performed. This can e.g. be tiling, loop
1212 // interchange, or ...) We can track this either at the place where the
1213 // transformation has been performed or, in case of automatic ILP based
1214 // optimizations, by comparing (yet to be defined) performance metrics
1215 // before/after the scheduling optimizer
1216 // (e.g., #stride-one accesses)
Roman Gareevb3224ad2016-09-14 06:26:09 +00001217 if (S.containsExtensionNode(NewSchedule))
1218 return true;
1219 auto *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001220 isl_union_map *OldSchedule = S.getSchedule();
Tobias Grosserff400872017-02-01 10:12:09 +00001221 assert(OldSchedule && "Only IslScheduleOptimizer can insert extension nodes "
1222 "that make Scop::getSchedule() return nullptr.");
Roman Gareevb3224ad2016-09-14 06:26:09 +00001223 bool changed = !isl_union_map_is_equal(OldSchedule, NewScheduleMap);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001224 isl_union_map_free(OldSchedule);
Roman Gareevb3224ad2016-09-14 06:26:09 +00001225 isl_union_map_free(NewScheduleMap);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001226 return changed;
1227}
1228
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001229namespace {
1230class IslScheduleOptimizer : public ScopPass {
1231public:
1232 static char ID;
1233 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
1234
1235 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
1236
Tobias Grosserc80d6972016-09-02 06:33:33 +00001237 /// Optimize the schedule of the SCoP @p S.
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001238 bool runOnScop(Scop &S) override;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001239
Tobias Grosserc80d6972016-09-02 06:33:33 +00001240 /// Print the new schedule for the SCoP @p S.
Johannes Doerfert45be6442015-09-27 15:43:29 +00001241 void printScop(raw_ostream &OS, Scop &S) const override;
1242
Tobias Grosserc80d6972016-09-02 06:33:33 +00001243 /// Register all analyses and transformation required.
Johannes Doerfert45be6442015-09-27 15:43:29 +00001244 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001245
Tobias Grosserc80d6972016-09-02 06:33:33 +00001246 /// Release the internal memory.
Johannes Doerfert0f376302015-09-27 15:42:28 +00001247 void releaseMemory() override {
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001248 isl_schedule_free(LastSchedule);
1249 LastSchedule = nullptr;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001250 }
Johannes Doerfert45be6442015-09-27 15:43:29 +00001251
1252private:
1253 isl_schedule *LastSchedule;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001254};
Tobias Grosser522478d2016-06-23 22:17:27 +00001255} // namespace
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001256
1257char IslScheduleOptimizer::ID = 0;
1258
Tobias Grosser73600b82011-10-08 00:30:40 +00001259bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +00001260
1261 // Skip empty SCoPs but still allow code generation as it will delete the
1262 // loops present but not needed.
1263 if (S.getSize() == 0) {
1264 S.markAsOptimized();
1265 return false;
1266 }
1267
Hongbin Zheng2a798852016-03-03 08:15:33 +00001268 const Dependences &D =
1269 getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001270
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001271 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +00001272 return false;
1273
Tobias Grosser28781422012-10-16 07:29:19 +00001274 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001275 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +00001276
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001277 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001278 int ValidityKinds =
1279 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001280 int ProximityKinds;
1281
1282 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001283 ProximityKinds =
1284 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001285 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001286 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001287 else {
1288 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001289 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001290 ProximityKinds =
1291 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001292 }
1293
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001294 isl_union_set *Domain = S.getDomains();
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001295
Tobias Grosser98610ee2012-02-13 23:31:39 +00001296 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001297 return false;
1298
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001299 isl_union_map *Validity = D.getDependences(ValidityKinds);
1300 isl_union_map *Proximity = D.getDependences(ProximityKinds);
Tobias Grosser8a507022012-03-16 11:51:41 +00001301
Tobias Grossera26db472012-01-30 19:38:43 +00001302 // Simplify the dependences by removing the constraints introduced by the
1303 // domains. This can speed up the scheduling time significantly, as large
1304 // constant coefficients will be removed from the dependences. The
1305 // introduction of some additional dependences reduces the possible
1306 // transformations, but in most cases, such transformation do not seem to be
1307 // interesting anyway. In some cases this option may stop the scheduler to
1308 // find any schedule.
1309 if (SimplifyDeps == "yes") {
Tobias Grosser00383a72012-02-14 14:02:44 +00001310 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
1311 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001312 Proximity =
1313 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
Tobias Grosser00383a72012-02-14 14:02:44 +00001314 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain));
Tobias Grossera26db472012-01-30 19:38:43 +00001315 } else if (SimplifyDeps != "no") {
1316 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
1317 "or 'no'. Falling back to default: 'yes'\n";
1318 }
1319
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001320 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser01aea582014-10-22 23:16:28 +00001321 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
1322 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
1323 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001324
Michael Krusec59f22c2015-06-18 16:45:40 +00001325 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001326
1327 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +00001328 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001329 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +00001330 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001331 } else {
1332 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
1333 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +00001334 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001335 }
1336
Tobias Grosser95e860c2012-01-30 19:38:54 +00001337 int IslMaximizeBands;
1338
Tobias Grossera4ea90b2012-01-30 22:43:56 +00001339 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +00001340 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +00001341 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +00001342 IslMaximizeBands = 0;
1343 } else {
1344 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
1345 " or 'no'. Falling back to default: 'yes'\n";
1346 IslMaximizeBands = 1;
1347 }
1348
Michael Kruse315aa322016-05-02 11:35:27 +00001349 int IslOuterCoincidence;
1350
1351 if (OuterCoincidence == "yes") {
1352 IslOuterCoincidence = 1;
1353 } else if (OuterCoincidence == "no") {
1354 IslOuterCoincidence = 0;
1355 } else {
1356 errs() << "warning: Option -polly-opt-outer-coincidence should either be "
1357 "'yes' or 'no'. Falling back to default: 'no'\n";
1358 IslOuterCoincidence = 0;
1359 }
1360
Tobias Grosseraf149932016-06-30 20:42:56 +00001361 isl_ctx *Ctx = S.getIslCtx();
Tobias Grosser42152ff2012-01-30 19:38:47 +00001362
Tobias Grosseraf149932016-06-30 20:42:56 +00001363 isl_options_set_schedule_outer_coincidence(Ctx, IslOuterCoincidence);
1364 isl_options_set_schedule_serialize_sccs(Ctx, IslSerializeSCCs);
1365 isl_options_set_schedule_maximize_band_depth(Ctx, IslMaximizeBands);
1366 isl_options_set_schedule_max_constant_term(Ctx, MaxConstantTerm);
1367 isl_options_set_schedule_max_coefficient(Ctx, MaxCoefficient);
1368 isl_options_set_tile_scale_tile_loops(Ctx, 0);
1369
Tobias Grosser3898a042016-06-30 20:42:58 +00001370 auto OnErrorStatus = isl_options_get_on_error(Ctx);
Tobias Grosseraf149932016-06-30 20:42:56 +00001371 isl_options_set_on_error(Ctx, ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +00001372
1373 isl_schedule_constraints *ScheduleConstraints;
1374 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
1375 ScheduleConstraints =
1376 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
1377 ScheduleConstraints = isl_schedule_constraints_set_validity(
1378 ScheduleConstraints, isl_union_map_copy(Validity));
1379 ScheduleConstraints =
1380 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +00001381 isl_schedule *Schedule;
Tobias Grossera38c9242014-01-26 19:36:28 +00001382 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
Tobias Grosser3898a042016-06-30 20:42:58 +00001383 isl_options_set_on_error(Ctx, OnErrorStatus);
Tobias Grosser42152ff2012-01-30 19:38:47 +00001384
1385 // In cases the scheduler is not able to optimize the code, we just do not
1386 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +00001387 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +00001388 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001389
Tobias Grosser97d87452015-05-30 06:46:59 +00001390 DEBUG({
Tobias Grosseraf149932016-06-30 20:42:56 +00001391 auto *P = isl_printer_to_str(Ctx);
Tobias Grosser97d87452015-05-30 06:46:59 +00001392 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
1393 P = isl_printer_print_schedule(P, Schedule);
Michael Kruse79c01732016-12-12 14:51:06 +00001394 auto *str = isl_printer_get_str(P);
1395 dbgs() << "NewScheduleTree: \n" << str << "\n";
1396 free(str);
Tobias Grosser97d87452015-05-30 06:46:59 +00001397 isl_printer_free(P);
1398 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +00001399
Roman Gareev42402c92016-06-22 09:52:37 +00001400 Function &F = S.getFunction();
1401 auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Roman Gareev98075fe2017-02-02 14:23:14 +00001402 const OptimizerAdditionalInfoTy OAI = {TTI, const_cast<Dependences *>(&D)};
Roman Gareev42402c92016-06-22 09:52:37 +00001403 isl_schedule *NewSchedule =
Roman Gareev98075fe2017-02-02 14:23:14 +00001404 ScheduleTreeOptimizer::optimizeSchedule(Schedule, &OAI);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001405
Roman Gareevb3224ad2016-09-14 06:26:09 +00001406 if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewSchedule)) {
Tobias Grosser808cd692015-07-14 09:33:13 +00001407 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001408 return false;
1409 }
1410
Tobias Grosser808cd692015-07-14 09:33:13 +00001411 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001412 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001413
Roman Gareev5f99f862016-08-21 11:20:39 +00001414 if (OptimizedScops)
1415 S.dump();
1416
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001417 return false;
1418}
1419
Johannes Doerfert3fe584d2015-03-01 18:40:25 +00001420void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +00001421 isl_printer *p;
1422 char *ScheduleStr;
1423
1424 OS << "Calculated schedule:\n";
1425
1426 if (!LastSchedule) {
1427 OS << "n/a\n";
1428 return;
1429 }
1430
1431 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
1432 p = isl_printer_print_schedule(p, LastSchedule);
1433 ScheduleStr = isl_printer_get_str(p);
1434 isl_printer_free(p);
1435
1436 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001437}
1438
Tobias Grosser73600b82011-10-08 00:30:40 +00001439void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001440 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +00001441 AU.addRequired<DependenceInfo>();
Roman Gareev42402c92016-06-22 09:52:37 +00001442 AU.addRequired<TargetTransformInfoWrapperPass>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001443}
1444
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001445Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +00001446 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001447}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001448
1449INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
1450 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +00001451INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Johannes Doerfert99191c72016-05-31 09:41:04 +00001452INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
Roman Gareev42402c92016-06-22 09:52:37 +00001453INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001454INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
1455 "Polly - Optimize schedule of SCoP", false, false)