blob: 3982ed4ecec700fa4d1eff86fa5121d8c260047c [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
Siddharth Bhat3e4a7d32017-03-17 14:52:19 +000029// - Prevectorization - The choice of a possible outer loop that is strip-mined
Tobias Grosser234a4822015-08-15 09:34:33 +000030// 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 Bhat3e4a7d32017-03-17 14:52:19 +000039// ACM Transactions on Programming Languages and Systems (TOPLAS),
Tobias Grosser234a4822015-08-15 09:34:33 +000040// 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"
Tobias Grosser7205f932017-05-21 16:21:33 +000056#include "polly/Support/ISLOStream.h"
Roman Gareev42402c92016-06-22 09:52:37 +000057#include "llvm/Analysis/TargetTransformInfo.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000058#include "llvm/Support/Debug.h"
Tobias Grosser2493e922011-12-07 07:42:57 +000059#include "isl/aff.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000060#include "isl/band.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000061#include "isl/constraint.h"
62#include "isl/map.h"
Tobias Grosser42152ff2012-01-30 19:38:47 +000063#include "isl/options.h"
Tobias Grosser97d87452015-05-30 06:46:59 +000064#include "isl/printer.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000065#include "isl/schedule.h"
Tobias Grosserbbb4cec2015-03-22 12:06:39 +000066#include "isl/schedule_node.h"
Tobias Grosser8ad6bc32012-01-31 13:26:29 +000067#include "isl/space.h"
Tobias Grossercd524dc2015-05-09 09:36:38 +000068#include "isl/union_map.h"
69#include "isl/union_set.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000070
71using namespace llvm;
72using namespace polly;
73
Chandler Carruth95fef942014-04-22 03:30:19 +000074#define DEBUG_TYPE "polly-opt-isl"
75
Tobias Grossera26db472012-01-30 19:38:43 +000076static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000077 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 Grosser1deda292012-02-14 14:02:48 +000081
82static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +000083 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 Grossera26db472012-01-30 19:38:43 +000087
Tobias Grosser483a90d2014-07-09 10:50:10 +000088static 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 Grosser992e60c2012-02-20 08:41:15 +000092
Tobias Grosser483a90d2014-07-09 10:50:10 +000093static 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
98static 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 Grosser92f54802012-02-20 08:41:47 +0000101
Tobias Grossere602a072013-05-07 07:30:56 +0000102static cl::opt<std::string>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000103 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 Grosserb3ad85b2012-01-30 19:38:50 +0000106
Michael Kruse315aa322016-05-02 11:35:27 +0000107static 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 Grosser07c1c2f2015-08-19 08:46:11 +0000113static 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 Grosser04832712015-08-20 13:45:02 +0000119static cl::opt<bool> FirstLevelTiling("polly-tiling",
120 cl::desc("Enable loop tiling"),
121 cl::init(true), cl::ZeroOrMore,
122 cl::cat(PollyCategory));
123
Roman Gareev42402c92016-06-22 09:52:37 +0000124static 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 Grosser0791d5f2016-12-23 07:33:39 +0000131static cl::opt<int> ThroughputVectorFma(
132 "polly-target-throughput-vector-fma",
Roman Gareev42402c92016-06-22 09:52:37 +0000133 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 Gareev1c2927b2016-12-25 16:32:28 +0000138// 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.
146static 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 Gareev3a18a932016-07-25 09:42:53 +0000150
Roman Gareev1c2927b2016-12-25 16:32:28 +0000151static 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
156static 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
161static 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 Gareev3a18a932016-07-25 09:42:53 +0000165
Tobias Grosser67e94fb2017-01-14 07:14:54 +0000166static 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 Grosser04832712015-08-20 13:45:02 +0000172static cl::opt<int> FirstLevelDefaultTileSize(
Tobias Grosser483a90d2014-07-09 10:50:10 +0000173 "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 Doerfertc3958b22014-05-28 17:21:02 +0000177
Tobias Grosser21a059a2017-01-16 14:08:10 +0000178static cl::list<int>
179 FirstLevelTileSizes("polly-tile-sizes",
180 cl::desc("A tile size for each loop dimension, filled "
Tobias Grosser04832712015-08-20 13:45:02 +0000181 "with --polly-default-tile-size"),
Tobias Grosser21a059a2017-01-16 14:08:10 +0000182 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
183 cl::cat(PollyCategory));
Tobias Grosser04832712015-08-20 13:45:02 +0000184
185static 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
190static 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
196static 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 Grosser42e24892015-08-20 13:45:05 +0000203static cl::opt<bool> RegisterTiling("polly-register-tiling",
204 cl::desc("Enable register tiling"),
205 cl::init(false), cl::ZeroOrMore,
206 cl::cat(PollyCategory));
207
208static 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 Gareevbe5299a2016-12-21 12:51:12 +0000214static 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 Grosser42e24892015-08-20 13:45:05 +0000220static 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 Gareev9c3eb592016-05-28 16:17:58 +0000227static cl::opt<bool>
228 PMBasedOpts("polly-pattern-matching-based-opts",
229 cl::desc("Perform optimizations based on pattern matching"),
Roman Gareev96e11192017-02-23 11:44:12 +0000230 cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
Roman Gareev9c3eb592016-05-28 16:17:58 +0000231
Roman Gareev5f99f862016-08-21 11:20:39 +0000232static 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 Grosser2fb3ed22017-06-19 10:40:12 +0000239/// Create an isl::union_set, which describes the isolate option based on
240/// IsolateDomain.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000241///
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000242/// @param IsolateDomain An isl::set whose @p OutDimsNum last dimensions should
Roman Gareev99890882017-02-09 07:10:01 +0000243/// belong to the current band node.
244/// @param OutDimsNum A number of dimensions that should belong to
245/// the current band node.
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000246static isl::union_set getIsolateOptions(isl::set IsolateDomain,
247 unsigned OutDimsNum) {
248 unsigned Dims = IsolateDomain.dim(isl::dim::set);
Roman Gareev99890882017-02-09 07:10:01 +0000249 assert(OutDimsNum <= Dims &&
Tobias Grosserdcd94e32017-06-19 16:55:48 +0000250 "The isl::set IsolateDomain is used to describe the range of schedule "
Roman Gareev99890882017-02-09 07:10:01 +0000251 "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 Grosser2fb3ed22017-06-19 10:40:12 +0000254 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 Grosserca7f5bb2015-10-20 09:12:21 +0000261}
262
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000263/// Create an isl::union_set, which describes the atomic option for the
264/// dimension of the current node.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000265///
266/// It may help to reduce the size of generated code.
267///
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000268/// @param Ctx An isl::ctx, which is used to create the isl::union_set.
269static 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 Grosserca7f5bb2015-10-20 09:12:21 +0000275}
276
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000277/// Create an isl::union_set, which describes the option of the form
Roman Gareev99890882017-02-09 07:10:01 +0000278/// [isolate[] -> unroll[x]].
279///
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000280/// @param Ctx An isl::ctx, which is used to create the isl::union_set.
281static 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 Gareev99890882017-02-09 07:10:01 +0000286 UnrollIsolatedSetOption =
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000287 UnrollIsolatedSetOption.set_tuple_id(isl::dim::in, DimInId);
Roman Gareev99890882017-02-09 07:10:01 +0000288 UnrollIsolatedSetOption =
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000289 UnrollIsolatedSetOption.set_tuple_id(isl::dim::out, DimOutId);
290 return UnrollIsolatedSetOption.wrap();
Roman Gareev99890882017-02-09 07:10:01 +0000291}
292
Tobias Grosserc80d6972016-09-02 06:33:33 +0000293/// Make the last dimension of Set to take values from 0 to VectorWidth - 1.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000294///
295/// @param Set A set, which should be modified.
296/// @param VectorWidth A parameter, which determines the constraint.
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000297static 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 Grosserca7f5bb2015-10-20 09:12:21 +0000309}
310
Tobias Grosserc80d6972016-09-02 06:33:33 +0000311/// Build the desired set of partial tile prefixes.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000312///
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 Gareev76614d32016-05-31 11:22:21 +0000320/// get a set of prefixes, which don't have exactly VectorWidth iterations.
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000321/// 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 Grosser2fb3ed22017-06-19 10:40:12 +0000326static 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 Grosserca7f5bb2015-10-20 09:12:21 +0000331 ExtentPrefixes = addExtentConstraints(ExtentPrefixes, VectorWidth);
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000332 isl::set BadPrefixes = ExtentPrefixes.subtract(ScheduleRange);
333 BadPrefixes = BadPrefixes.project_out(isl::dim::set, Dims - 1, 1);
334 return LoopPrefixes.subtract(BadPrefixes);
Tobias Grosserca7f5bb2015-10-20 09:12:21 +0000335}
336
Tobias Grosser2fb3ed22017-06-19 10:40:12 +0000337isl::schedule_node
338ScheduleTreeOptimizer::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 Grosserca7f5bb2015-10-20 09:12:21 +0000351 return Node;
352}
353
Tobias Grosserb241d922015-07-28 18:03:36 +0000354__isl_give isl_schedule_node *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000355ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
356 unsigned DimToVectorize,
357 int VectorWidth) {
Tobias Grosserb241d922015-07-28 18:03:36 +0000358 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000359
Tobias Grosserb241d922015-07-28 18:03:36 +0000360 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 Grosserf5338802011-10-06 00:03:35 +0000364
Tobias Grosserb241d922015-07-28 18:03:36 +0000365 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 Grosser2fb3ed22017-06-19 10:40:12 +0000377 Node = isolateFullPartialTiles(give(Node), VectorWidth).release();
Tobias Grosserb241d922015-07-28 18:03:36 +0000378 Node = isl_schedule_node_child(Node, 0);
Tobias Grosser42e24892015-08-20 13:45:05 +0000379 // 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 Grosserfc490a92015-08-20 19:08:16 +0000382 Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }"));
383 Node = isl_schedule_node_band_sink(Node);
Tobias Grosserb241d922015-07-28 18:03:36 +0000384 Node = isl_schedule_node_child(Node, 0);
Roman Gareev11001e12016-02-23 09:00:13 +0000385 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 Grosserb241d922015-07-28 18:03:36 +0000389 return Node;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000390}
391
Tobias Grosserd891b542015-08-20 12:16:23 +0000392__isl_give isl_schedule_node *
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000393ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node,
394 const char *Identifier, ArrayRef<int> TileSizes,
395 int DefaultTileSize) {
Tobias Grosser9bdea572015-08-20 12:22:37 +0000396 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 Grosser1ac884d2015-08-23 09:11:00 +0000400 std::string IdentifierString(Identifier);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000401 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 Grosser1ac884d2015-08-23 09:11:00 +0000405 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 Grosser9bdea572015-08-20 12:22:37 +0000410 Node = isl_schedule_node_band_tile(Node, Sizes);
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000411 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 Grosser9bdea572015-08-20 12:22:37 +0000418}
419
Roman Gareevb17b9a82016-06-12 17:20:05 +0000420__isl_give isl_schedule_node *
421ScheduleTreeOptimizer::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 Grosserc9d4cb22017-03-12 19:02:31 +0000431namespace {
432bool 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 Grosserfa57e9b2015-08-24 06:01:47 +0000459bool ScheduleTreeOptimizer::isTileableBandNode(
Tobias Grosser862b9b52015-08-20 12:32:45 +0000460 __isl_keep isl_schedule_node *Node) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000461 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000462 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000463
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000464 if (isl_schedule_node_n_children(Node) != 1)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000465 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000466
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000467 if (!isl_schedule_node_band_get_permutable(Node))
Tobias Grosser862b9b52015-08-20 12:32:45 +0000468 return false;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000469
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000470 auto Space = isl_schedule_node_band_get_space(Node);
471 auto Dims = isl_space_dim(Space, isl_dim_set);
Tobias Grosser9bdea572015-08-20 12:22:37 +0000472 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000473
Tobias Grosser9bdea572015-08-20 12:22:37 +0000474 if (Dims <= 1)
Tobias Grosser862b9b52015-08-20 12:32:45 +0000475 return false;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000476
Tobias Grosserc9d4cb22017-03-12 19:02:31 +0000477 auto ManagedNode = isl::manage(isl_schedule_node_copy(Node));
478 return isSimpleInnermostBand(ManagedNode);
Tobias Grosser862b9b52015-08-20 12:32:45 +0000479}
480
481__isl_give isl_schedule_node *
Roman Gareev9c3eb592016-05-28 16:17:58 +0000482ScheduleTreeOptimizer::standardBandOpts(__isl_take isl_schedule_node *Node,
483 void *User) {
Tobias Grosser04832712015-08-20 13:45:02 +0000484 if (FirstLevelTiling)
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000485 Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes,
486 FirstLevelDefaultTileSize);
Tobias Grosser04832712015-08-20 13:45:02 +0000487
488 if (SecondLevelTiling)
Tobias Grosser1ac884d2015-08-23 09:11:00 +0000489 Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes,
490 SecondLevelDefaultTileSize);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000491
Roman Gareevb17b9a82016-06-12 17:20:05 +0000492 if (RegisterTiling)
493 Node =
494 applyRegisterTiling(Node, RegisterTileSizes, RegisterDefaultTileSize);
Tobias Grosser42e24892015-08-20 13:45:05 +0000495
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000496 if (PollyVectorizerChoice == VECTORIZER_NONE)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000497 return Node;
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000498
Tobias Grosser862b9b52015-08-20 12:32:45 +0000499 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 Grosserb241d922015-07-28 18:03:36 +0000503 for (int i = Dims - 1; i >= 0; i--)
Tobias Grosserf10f4632015-08-19 08:03:37 +0000504 if (isl_schedule_node_band_member_get_coincident(Node, i)) {
Tobias Grosserfa57e9b2015-08-24 06:01:47 +0000505 Node = prevectSchedBand(Node, i, PrevectorWidth);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000506 break;
507 }
Tobias Grosserbbb4cec2015-03-22 12:06:39 +0000508
Tobias Grosserf10f4632015-08-19 08:03:37 +0000509 return Node;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000510}
511
Roman Gareev98075fe2017-02-02 14:23:14 +0000512/// Get the position of a dimension with a non-zero coefficient.
Roman Gareev9c3eb592016-05-28 16:17:58 +0000513///
Roman Gareev98075fe2017-02-02 14:23:14 +0000514/// 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.
524static 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.
560static 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.
590static 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.
666static 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.
689static 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.
729static 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 Krusea6d48f52017-06-08 12:06:15 +0000772/// @param Pos The parameter to describe an acceptable true dependence.
Roman Gareev98075fe2017-02-02 14:23:14 +0000773/// 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.
777static bool containsOnlyMatMulDep(__isl_keep isl_map *Schedule,
778 const Dependences *D, int &Pos) {
Roman Gareevb1960552017-02-11 09:59:09 +0000779 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 Gareevafcf0262017-02-11 07:14:37 +0000783 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 Gareevb1960552017-02-11 09:59:09 +0000786 auto *Deltas = isl_map_deltas(isl_union_map_extract_map(Dep, Space));
787 isl_union_map_free(Dep);
Roman Gareev98075fe2017-02-02 14:23:14 +0000788 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 Gareevafcf0262017-02-11 07:14:37 +0000791 Pos = Pos < 0 && isl_val_is_one(Val) ? i : Pos;
Roman Gareev98075fe2017-02-02 14:23:14 +0000792 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 Gareev4eb07e42017-02-16 07:04:41 +0000795 isl_set_free(Deltas);
Roman Gareev98075fe2017-02-02 14:23:14 +0000796 return false;
797 }
798 isl_val_free(Val);
799 }
Roman Gareev4eb07e42017-02-16 07:04:41 +0000800 isl_set_free(Deltas);
Roman Gareevde692932017-02-11 09:48:09 +0000801 if (DeltasDimNum == 0 || Pos < 0)
802 return false;
Roman Gareev98075fe2017-02-02 14:23:14 +0000803 return true;
Roman Gareev9c3eb592016-05-28 16:17:58 +0000804}
805
Tobias Grosserc80d6972016-09-02 06:33:33 +0000806/// Check if the SCoP statement could probably be optimized with analytical
807/// modeling.
Roman Gareev9c3eb592016-05-28 16:17:58 +0000808///
809/// containsMatrMult tries to determine whether the following conditions
810/// are true:
Roman Gareev98075fe2017-02-02 14:23:14 +0000811/// 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 Gareev9c3eb592016-05-28 16:17:58 +0000824///
825/// @param PartialSchedule The PartialSchedule that contains a SCoP statement
826/// to check.
Roman Gareev98075fe2017-02-02 14:23:14 +0000827/// @D The SCoP dependencies.
828/// @MMI Parameters of the matrix multiplication operands.
829static 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 Gareev9c3eb592016-05-28 16:17:58 +0000833 isl_id_free(InputDimsId);
Roman Gareev98075fe2017-02-02 14:23:14 +0000834 if (Stmt->size() <= 1)
Roman Gareev9c3eb592016-05-28 16:17:58 +0000835 return false;
Roman Gareev98075fe2017-02-02 14:23:14 +0000836 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 Gareev9c3eb592016-05-28 16:17:58 +0000841 return false;
Roman Gareev98075fe2017-02-02 14:23:14 +0000842 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 Gareev9c3eb592016-05-28 16:17:58 +0000852
Roman Gareev98075fe2017-02-02 14:23:14 +0000853 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 Gareev9c3eb592016-05-28 16:17:58 +0000862}
863
Tobias Grosserc80d6972016-09-02 06:33:33 +0000864/// Permute two dimensions of the band node.
Roman Gareev3a18a932016-07-25 09:42:53 +0000865///
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.
871static __isl_give isl_schedule_node *
872permuteBandNodeDimensions(__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 Gareev2cb4d132016-07-25 07:27:59 +0000890__isl_give isl_schedule_node *ScheduleTreeOptimizer::createMicroKernel(
891 __isl_take isl_schedule_node *Node, MicroKernelParamsTy MicroKernelParams) {
Roman Gareev8babe1a2016-12-15 11:47:38 +0000892 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 Gareev2cb4d132016-07-25 07:27:59 +0000896}
897
Roman Gareev3a18a932016-07-25 09:42:53 +0000898__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 Gareev98075fe2017-02-02 14:23:14 +0000904 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 Gareev3a18a932016-07-25 09:42:53 +0000910 Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
Roman Gareev98075fe2017-02-02 14:23:14 +0000911 Node = permuteBandNodeDimensions(Node, DimOutNum - 2, DimOutNum - 1);
912 Node = permuteBandNodeDimensions(Node, DimOutNum - 3, DimOutNum - 1);
Roman Gareev3a18a932016-07-25 09:42:53 +0000913 return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
914}
915
Roman Gareev3d4eae32017-02-11 07:00:05 +0000916/// 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.
922static 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.
937static 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 Gareev2cb4d132016-07-25 07:27:59 +0000946/// 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 Gareev3d4eae32017-02-11 07:00:05 +0000955/// @param MMI Parameters of the matrix multiplication operands.
Roman Gareev2cb4d132016-07-25 07:27:59 +0000956/// @return The structure of type MicroKernelParamsTy.
957/// @see MicroKernelParamsTy
958static struct MicroKernelParamsTy
Roman Gareev3d4eae32017-02-11 07:00:05 +0000959getMicroKernelParams(const llvm::TargetTransformInfo *TTI, MatMulInfoTy MMI) {
Roman Gareev42402c92016-06-22 09:52:37 +0000960 assert(TTI && "The target transform info should be provided.");
Roman Gareev2cb4d132016-07-25 07:27:59 +0000961
Roman Gareev42402c92016-06-22 09:52:37 +0000962 // Nvec - Number of double-precision floating-point numbers that can be hold
963 // by a vector register. Use 2 by default.
Tobias Grosser67e94fb2017-01-14 07:14:54 +0000964 long RegisterBitwidth = VectorRegisterBitwidth;
965
966 if (RegisterBitwidth == -1)
967 RegisterBitwidth = TTI->getRegisterBitWidth(true);
Roman Gareev3d4eae32017-02-11 07:00:05 +0000968 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 Gareev42402c92016-06-22 09:52:37 +0000972 if (Nvec == 0)
973 Nvec = 2;
974 int Nr =
Tobias Grosser0791d5f2016-12-23 07:33:39 +0000975 ceil(sqrt(Nvec * LatencyVectorFma * ThroughputVectorFma) / Nvec) * Nvec;
976 int Mr = ceil(Nvec * LatencyVectorFma * ThroughputVectorFma / Nr);
Roman Gareev2cb4d132016-07-25 07:27:59 +0000977 return {Mr, Nr};
978}
979
Roman Gareev3a18a932016-07-25 09:42:53 +0000980/// 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 Gareev3d4eae32017-02-11 07:00:05 +0000990/// @param MMI Parameters of the matrix multiplication operands.
Roman Gareev3a18a932016-07-25 09:42:53 +0000991/// @return The structure of type MacroKernelParamsTy.
992/// @see MacroKernelParamsTy
993/// @see MicroKernelParamsTy
994static struct MacroKernelParamsTy
Roman Gareev3d4eae32017-02-11 07:00:05 +0000995getMacroKernelParams(const MicroKernelParamsTy &MicroKernelParams,
996 MatMulInfoTy MMI) {
Roman Gareev3a18a932016-07-25 09:42:53 +0000997 // 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 Gareev1c2927b2016-12-25 16:32:28 +00001003 FirstCacheLevelSize > 0 && SecondCacheLevelSize > 0 &&
1004 FirstCacheLevelAssociativity > 2 && SecondCacheLevelAssociativity > 2))
Roman Gareev3a18a932016-07-25 09:42:53 +00001005 return {1, 1, 1};
Roman Gareevbe5299a2016-12-21 12:51:12 +00001006 // The quotient should be greater than zero.
1007 if (PollyPatternMatchingNcQuotient <= 0)
1008 return {1, 1, 1};
Roman Gareev15db81e2016-12-15 12:00:57 +00001009 int Car = floor(
Roman Gareev1c2927b2016-12-25 16:32:28 +00001010 (FirstCacheLevelAssociativity - 1) /
Roman Gareev8babe1a2016-12-15 11:47:38 +00001011 (1 + static_cast<double>(MicroKernelParams.Nr) / MicroKernelParams.Mr));
Siddharth Bhat5eeb1dd2017-04-06 08:20:22 +00001012
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 Krusea6d48f52017-06-08 12:06:15 +00001015 // tile sizes to be computed. Prevent division by Cac==0 by early returning
Siddharth Bhat5eeb1dd2017-04-06 08:20:22 +00001016 // if this happens.
1017 if (Car == 0)
1018 return {1, 1, 1};
1019
Roman Gareev3d4eae32017-02-11 07:00:05 +00001020 auto ElementSize = getMatMulAlignTypeSize(MMI);
1021 assert(ElementSize > 0 && "The element size of the matrix multiplication "
1022 "operands should be greater than zero.");
Roman Gareev1c2927b2016-12-25 16:32:28 +00001023 int Kc = (Car * FirstCacheLevelSize) /
Roman Gareev3d4eae32017-02-11 07:00:05 +00001024 (MicroKernelParams.Mr * FirstCacheLevelAssociativity * ElementSize);
1025 double Cac =
1026 static_cast<double>(Kc * ElementSize * SecondCacheLevelAssociativity) /
1027 SecondCacheLevelSize;
Roman Gareev1c2927b2016-12-25 16:32:28 +00001028 int Mc = floor((SecondCacheLevelAssociativity - 2) / Cac);
Roman Gareevbe5299a2016-12-21 12:51:12 +00001029 int Nc = PollyPatternMatchingNcQuotient * MicroKernelParams.Nr;
Siddharth Bhat5eeb1dd2017-04-06 08:20:22 +00001030
1031 assert(Mc > 0 && Nc > 0 && Kc > 0 &&
1032 "Matrix block sizes should be greater than zero");
Roman Gareev3a18a932016-07-25 09:42:53 +00001033 return {Mc, Nc, Kc};
1034}
1035
Tobias Grosserc80d6972016-09-02 06:33:33 +00001036/// Create an access relation that is specific to
Roman Gareev1c892e92016-08-15 12:22:54 +00001037/// the matrix multiplication pattern.
1038///
1039/// Create an access relation of the following form:
Roman Gareev92c44602016-12-21 11:18:42 +00001040/// [O0, O1, O2, O3, O4, O5, O6, O7, O8] -> [OI, O5, OJ]
1041/// where I is @p FirstDim, J is @p SecondDim.
Roman Gareev1c892e92016-08-15 12:22:54 +00001042///
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 Gareev1c892e92016-08-15 12:22:54 +00001059/// @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 Gareev92c44602016-12-21 11:18:42 +00001063 unsigned FirstDim, unsigned SecondDim) {
Roman Gareev1c892e92016-08-15 12:22:54 +00001064 auto *Ctx = isl_map_get_ctx(MapOldIndVar);
Roman Gareev92c44602016-12-21 11:18:42 +00001065 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 Gareev1c892e92016-08-15 12:22:54 +00001070 return isl_map_apply_range(MapOldIndVar, AccessRel);
1071}
1072
Roman Gareevb3224ad2016-09-14 06:26:09 +00001073__isl_give isl_schedule_node *
1074createExtensionNode(__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 Grosserc80d6972016-09-02 06:33:33 +00001081/// Apply the packing transformation.
Roman Gareev1c892e92016-08-15 12:22:54 +00001082///
1083/// The packing transformation can be described as a data-layout
1084/// transformation that requires to introduce a new array, copy data
Roman Gareev7758a2a2017-01-29 10:37:50 +00001085/// 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 Gareev1c892e92016-08-15 12:22:54 +00001104///
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 Gareev98075fe2017-02-02 14:23:14 +00001111/// @param MMI Parameters of the matrix multiplication operands.
Roman Gareev1c892e92016-08-15 12:22:54 +00001112/// @return The optimized schedule node.
Roman Gareevb3224ad2016-09-14 06:26:09 +00001113static __isl_give isl_schedule_node *optimizeDataLayoutMatrMulPattern(
1114 __isl_take isl_schedule_node *Node, __isl_take isl_map *MapOldIndVar,
Roman Gareev98075fe2017-02-02 14:23:14 +00001115 MicroKernelParamsTy MicroParams, MacroKernelParamsTy MacroParams,
1116 MatMulInfoTy &MMI) {
Roman Gareev1c892e92016-08-15 12:22:54 +00001117 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 Gareev2606c482016-12-15 12:35:59 +00001120
1121 // Create a copy statement that corresponds to the memory access to the
1122 // matrix B, the second operand of the matrix multiplication.
Roman Gareevb3224ad2016-09-14 06:26:09 +00001123 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 Gareev92c44602016-12-21 11:18:42 +00001127 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 Gareev1c892e92016-08-15 12:22:54 +00001131 auto *SAI = Stmt->getParent()->createScopArrayInfo(
Roman Gareev98075fe2017-02-02 14:23:14 +00001132 MMI.B->getElementType(), "Packed_B",
Roman Gareev92c44602016-12-21 11:18:42 +00001133 {FirstDimSize, SecondDimSize, ThirdDimSize});
Roman Gareev1c892e92016-08-15 12:22:54 +00001134 AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId());
Roman Gareev98075fe2017-02-02 14:23:14 +00001135 auto *OldAcc = MMI.B->getAccessRelation();
1136 MMI.B->setNewAccessRelation(AccRel);
Roman Gareevb3224ad2016-09-14 06:26:09 +00001137 auto *ExtMap =
Roman Gareev98075fe2017-02-02 14:23:14 +00001138 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 Gareevb3224ad2016-09-14 06:26:09 +00001142 auto *Domain = Stmt->getDomain();
Roman Gareev2606c482016-12-15 12:35:59 +00001143
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 Gareevb3224ad2016-09-14 06:26:09 +00001147 auto *NewStmt = Stmt->getParent()->addScopStmt(
Roman Gareev98075fe2017-02-02 14:23:14 +00001148 OldAcc, MMI.B->getAccessRelation(), isl_set_copy(Domain));
Roman Gareev2606c482016-12-15 12:35:59 +00001149 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 Gareevb3224ad2016-09-14 06:26:09 +00001151 ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId());
1152 Node = createExtensionNode(Node, ExtMap);
Roman Gareev2606c482016-12-15 12:35:59 +00001153
1154 // Create a copy statement that corresponds to the memory access
1155 // to the matrix A, the first operand of the matrix multiplication.
Roman Gareevb3224ad2016-09-14 06:26:09 +00001156 Node = isl_schedule_node_child(Node, 0);
Roman Gareev98075fe2017-02-02 14:23:14 +00001157 AccRel = getMatMulAccRel(isl_map_copy(MapOldIndVar), 4, 6);
Roman Gareev92c44602016-12-21 11:18:42 +00001158 FirstDimSize = MacroParams.Mc / MicroParams.Mr;
1159 ThirdDimSize = MicroParams.Mr;
Roman Gareev1c892e92016-08-15 12:22:54 +00001160 SAI = Stmt->getParent()->createScopArrayInfo(
Roman Gareev98075fe2017-02-02 14:23:14 +00001161 MMI.A->getElementType(), "Packed_A",
Roman Gareev92c44602016-12-21 11:18:42 +00001162 {FirstDimSize, SecondDimSize, ThirdDimSize});
Roman Gareev1c892e92016-08-15 12:22:54 +00001163 AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId());
Roman Gareev98075fe2017-02-02 14:23:14 +00001164 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 Gareev2606c482016-12-15 12:35:59 +00001172
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 Gareevb3224ad2016-09-14 06:26:09 +00001177 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 Gareev1c892e92016-08-15 12:22:54 +00001181}
1182
Tobias Grosserc80d6972016-09-02 06:33:33 +00001183/// Get a relation mapping induction variables produced by schedule
1184/// transformations to the original ones.
Roman Gareev1c892e92016-08-15 12:22:54 +00001185///
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 *
1196getInductionVariablesSubstitution(__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 Gareev99890882017-02-09 07:10:01 +00001210/// 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 Grosser2fb3ed22017-06-19 10:40:12 +00001224static isl::schedule_node
1225isolateAndUnrollMatMulInnerLoops(isl::schedule_node Node,
Roman Gareev99890882017-02-09 07:10:01 +00001226 struct MicroKernelParamsTy MicroKernelParams) {
Tobias Grosser2fb3ed22017-06-19 10:40:12 +00001227 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 Gareev99890882017-02-09 07:10:01 +00001232 Prefix = getPartialTilePrefixes(Prefix, MicroKernelParams.Nr);
1233 Prefix = getPartialTilePrefixes(Prefix, MicroKernelParams.Mr);
Tobias Grosser2fb3ed22017-06-19 10:40:12 +00001234
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 Gareev99890882017-02-09 07:10:01 +00001243 IsolateOption = getIsolateOptions(Prefix, 3);
Tobias Grosser2fb3ed22017-06-19 10:40:12 +00001244 Options = IsolateOption.unite(AtomicOption);
1245 Node = Node.band_set_ast_build_options(Options);
1246 Node = Node.child(0).child(0);
Roman Gareev99890882017-02-09 07:10:01 +00001247 return Node;
1248}
1249
Roman Gareevcdfb57d2017-03-22 14:25:24 +00001250/// 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.
1255static 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 Gareeve0d46632017-04-06 17:09:54 +00001265/// 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.
1273namespace {
1274isl::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 Gareev2cb4d132016-07-25 07:27:59 +00001297__isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeMatMulPattern(
Roman Gareev98075fe2017-02-02 14:23:14 +00001298 __isl_take isl_schedule_node *Node, const llvm::TargetTransformInfo *TTI,
1299 MatMulInfoTy &MMI) {
Roman Gareev2cb4d132016-07-25 07:27:59 +00001300 assert(TTI && "The target transform info should be provided.");
Tobias Grosserf3adab42017-05-10 10:59:58 +00001301 Node = markInterIterationAliasFree(
1302 Node, MMI.WriteToC->getLatestScopArrayInfo()->getBasePtr());
Roman Gareev98075fe2017-02-02 14:23:14 +00001303 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 Gareeve0d46632017-04-06 17:09:54 +00001307 Node = getBandNodeWithOriginDimOrder(isl::manage(Node)).take();
Roman Gareev98075fe2017-02-02 14:23:14 +00001308 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 Gareev9d4d91c2017-04-06 17:25:08 +00001312 NewK = NewK == DimOutNum - 2 ? NewJ : NewK;
Roman Gareev98075fe2017-02-02 14:23:14 +00001313 Node = permuteBandNodeDimensions(Node, NewK, DimOutNum - 1);
Roman Gareev3d4eae32017-02-11 07:00:05 +00001314 auto MicroKernelParams = getMicroKernelParams(TTI, MMI);
1315 auto MacroKernelParams = getMacroKernelParams(MicroKernelParams, MMI);
Roman Gareev3a18a932016-07-25 09:42:53 +00001316 Node = createMacroKernel(Node, MacroKernelParams);
Roman Gareev2cb4d132016-07-25 07:27:59 +00001317 Node = createMicroKernel(Node, MicroKernelParams);
Roman Gareev1c892e92016-08-15 12:22:54 +00001318 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 Grosser2fb3ed22017-06-19 10:40:12 +00001325 Node =
1326 isolateAndUnrollMatMulInnerLoops(give(Node), MicroKernelParams).release();
Roman Gareevb3224ad2016-09-14 06:26:09 +00001327 return optimizeDataLayoutMatrMulPattern(Node, MapOldIndVar, MicroKernelParams,
Roman Gareev98075fe2017-02-02 14:23:14 +00001328 MacroKernelParams, MMI);
Roman Gareev42402c92016-06-22 09:52:37 +00001329}
1330
Roman Gareev9c3eb592016-05-28 16:17:58 +00001331bool ScheduleTreeOptimizer::isMatrMultPattern(
Roman Gareev98075fe2017-02-02 14:23:14 +00001332 __isl_keep isl_schedule_node *Node, const Dependences *D,
1333 MatMulInfoTy &MMI) {
Roman Gareev9c3eb592016-05-28 16:17:58 +00001334 auto *PartialSchedule =
1335 isl_schedule_node_band_get_partial_schedule_union_map(Node);
Roman Gareeve0d46632017-04-06 17:09:54 +00001336 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 Gareev397a34a2016-06-22 12:11:30 +00001342 isl_union_map_n_map(PartialSchedule) != 1) {
1343 isl_union_map_free(PartialSchedule);
Roman Gareev9c3eb592016-05-28 16:17:58 +00001344 return false;
1345 }
Roman Gareev397a34a2016-06-22 12:11:30 +00001346 auto *NewPartialSchedule = isl_map_from_union_map(PartialSchedule);
Roman Gareev98075fe2017-02-02 14:23:14 +00001347 if (containsMatrMult(NewPartialSchedule, D, MMI)) {
Roman Gareev9c3eb592016-05-28 16:17:58 +00001348 isl_map_free(NewPartialSchedule);
1349 return true;
1350 }
1351 isl_map_free(NewPartialSchedule);
1352 return false;
1353}
1354
1355__isl_give isl_schedule_node *
1356ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node,
1357 void *User) {
1358 if (!isTileableBandNode(Node))
1359 return Node;
1360
Roman Gareev98075fe2017-02-02 14:23:14 +00001361 const OptimizerAdditionalInfoTy *OAI =
1362 static_cast<const OptimizerAdditionalInfoTy *>(User);
1363
1364 MatMulInfoTy MMI;
1365 if (PMBasedOpts && User && isMatrMultPattern(Node, OAI->D, MMI)) {
Roman Gareev9c3eb592016-05-28 16:17:58 +00001366 DEBUG(dbgs() << "The matrix multiplication pattern was detected\n");
Roman Gareev772498d2017-02-08 13:29:06 +00001367 return optimizeMatMulPattern(Node, OAI->TTI, MMI);
Roman Gareev42402c92016-06-22 09:52:37 +00001368 }
Roman Gareev9c3eb592016-05-28 16:17:58 +00001369
1370 return standardBandOpts(Node, User);
1371}
1372
Tobias Grosser808cd692015-07-14 09:33:13 +00001373__isl_give isl_schedule *
Roman Gareev42402c92016-06-22 09:52:37 +00001374ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule,
Roman Gareev98075fe2017-02-02 14:23:14 +00001375 const OptimizerAdditionalInfoTy *OAI) {
Tobias Grosserbbb4cec2015-03-22 12:06:39 +00001376 isl_schedule_node *Root = isl_schedule_get_root(Schedule);
Roman Gareev98075fe2017-02-02 14:23:14 +00001377 Root = optimizeScheduleNode(Root, OAI);
Tobias Grosser808cd692015-07-14 09:33:13 +00001378 isl_schedule_free(Schedule);
Tobias Grosser808cd692015-07-14 09:33:13 +00001379 auto S = isl_schedule_node_get_schedule(Root);
Tobias Grosserbbb4cec2015-03-22 12:06:39 +00001380 isl_schedule_node_free(Root);
Tobias Grosser808cd692015-07-14 09:33:13 +00001381 return S;
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001382}
1383
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001384__isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode(
Roman Gareev98075fe2017-02-02 14:23:14 +00001385 __isl_take isl_schedule_node *Node, const OptimizerAdditionalInfoTy *OAI) {
Roman Gareev42402c92016-06-22 09:52:37 +00001386 Node = isl_schedule_node_map_descendant_bottom_up(
Roman Gareev98075fe2017-02-02 14:23:14 +00001387 Node, optimizeBand, const_cast<void *>(static_cast<const void *>(OAI)));
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001388 return Node;
1389}
1390
1391bool ScheduleTreeOptimizer::isProfitableSchedule(
Roman Gareevb3224ad2016-09-14 06:26:09 +00001392 Scop &S, __isl_keep isl_schedule *NewSchedule) {
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001393 // 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 Gareevb3224ad2016-09-14 06:26:09 +00001402 if (S.containsExtensionNode(NewSchedule))
1403 return true;
1404 auto *NewScheduleMap = isl_schedule_get_map(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001405 isl_union_map *OldSchedule = S.getSchedule();
Tobias Grosserff400872017-02-01 10:12:09 +00001406 assert(OldSchedule && "Only IslScheduleOptimizer can insert extension nodes "
1407 "that make Scop::getSchedule() return nullptr.");
Roman Gareevb3224ad2016-09-14 06:26:09 +00001408 bool changed = !isl_union_map_is_equal(OldSchedule, NewScheduleMap);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001409 isl_union_map_free(OldSchedule);
Roman Gareevb3224ad2016-09-14 06:26:09 +00001410 isl_union_map_free(NewScheduleMap);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001411 return changed;
1412}
1413
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001414namespace {
1415class IslScheduleOptimizer : public ScopPass {
1416public:
1417 static char ID;
1418 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
1419
1420 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
1421
Tobias Grosserc80d6972016-09-02 06:33:33 +00001422 /// Optimize the schedule of the SCoP @p S.
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001423 bool runOnScop(Scop &S) override;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001424
Tobias Grosserc80d6972016-09-02 06:33:33 +00001425 /// Print the new schedule for the SCoP @p S.
Johannes Doerfert45be6442015-09-27 15:43:29 +00001426 void printScop(raw_ostream &OS, Scop &S) const override;
1427
Tobias Grosserc80d6972016-09-02 06:33:33 +00001428 /// Register all analyses and transformation required.
Johannes Doerfert45be6442015-09-27 15:43:29 +00001429 void getAnalysisUsage(AnalysisUsage &AU) const override;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001430
Tobias Grosserc80d6972016-09-02 06:33:33 +00001431 /// Release the internal memory.
Johannes Doerfert0f376302015-09-27 15:42:28 +00001432 void releaseMemory() override {
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001433 isl_schedule_free(LastSchedule);
1434 LastSchedule = nullptr;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001435 }
Johannes Doerfert45be6442015-09-27 15:43:29 +00001436
1437private:
1438 isl_schedule *LastSchedule;
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001439};
Tobias Grosser522478d2016-06-23 22:17:27 +00001440} // namespace
Tobias Grosserfa57e9b2015-08-24 06:01:47 +00001441
1442char IslScheduleOptimizer::ID = 0;
1443
Tobias Grosser73600b82011-10-08 00:30:40 +00001444bool IslScheduleOptimizer::runOnScop(Scop &S) {
Johannes Doerfert6f7921f2015-02-14 12:02:24 +00001445
Singapuram Sanjay Srivallabh02ca3462017-06-30 19:42:21 +00001446 // Skip SCoPs in case they're already optimised by PPCGCodeGeneration
1447 if (S.isToBeSkipped())
1448 return false;
1449
Johannes Doerfert6f7921f2015-02-14 12:02:24 +00001450 // Skip empty SCoPs but still allow code generation as it will delete the
1451 // loops present but not needed.
1452 if (S.getSize() == 0) {
1453 S.markAsOptimized();
1454 return false;
1455 }
1456
Hongbin Zheng2a798852016-03-03 08:15:33 +00001457 const Dependences &D =
1458 getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001459
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001460 if (!D.hasValidDependences())
Tobias Grosser38c36ea2014-02-23 15:15:44 +00001461 return false;
1462
Tobias Grosser28781422012-10-16 07:29:19 +00001463 isl_schedule_free(LastSchedule);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001464 LastSchedule = nullptr;
Tobias Grosser28781422012-10-16 07:29:19 +00001465
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001466 // Build input data.
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001467 int ValidityKinds =
1468 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001469 int ProximityKinds;
1470
1471 if (OptimizeDeps == "all")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001472 ProximityKinds =
1473 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001474 else if (OptimizeDeps == "raw")
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001475 ProximityKinds = Dependences::TYPE_RAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001476 else {
1477 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001478 << " Falling back to optimizing all dependences.\n";
Johannes Doerfert7e6424b2015-03-05 00:43:48 +00001479 ProximityKinds =
1480 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
Tobias Grosser1deda292012-02-14 14:02:48 +00001481 }
1482
Tobias Grosser7205f932017-05-21 16:21:33 +00001483 isl::union_set Domain = give(S.getDomains());
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001484
Tobias Grosser98610ee2012-02-13 23:31:39 +00001485 if (!Domain)
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001486 return false;
1487
Tobias Grosser7205f932017-05-21 16:21:33 +00001488 isl::union_map Validity = give(D.getDependences(ValidityKinds));
1489 isl::union_map Proximity = give(D.getDependences(ProximityKinds));
Tobias Grosser8a507022012-03-16 11:51:41 +00001490
Tobias Grossera26db472012-01-30 19:38:43 +00001491 // Simplify the dependences by removing the constraints introduced by the
1492 // domains. This can speed up the scheduling time significantly, as large
1493 // constant coefficients will be removed from the dependences. The
1494 // introduction of some additional dependences reduces the possible
1495 // transformations, but in most cases, such transformation do not seem to be
1496 // interesting anyway. In some cases this option may stop the scheduler to
1497 // find any schedule.
1498 if (SimplifyDeps == "yes") {
Tobias Grosser7205f932017-05-21 16:21:33 +00001499 Validity = Validity.gist_domain(Domain);
1500 Validity = Validity.gist_range(Domain);
1501 Proximity = Proximity.gist_domain(Domain);
1502 Proximity = Proximity.gist_range(Domain);
Tobias Grossera26db472012-01-30 19:38:43 +00001503 } else if (SimplifyDeps != "no") {
1504 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
1505 "or 'no'. Falling back to default: 'yes'\n";
1506 }
1507
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001508 DEBUG(dbgs() << "\n\nCompute schedule from: ");
Tobias Grosser7205f932017-05-21 16:21:33 +00001509 DEBUG(dbgs() << "Domain := " << Domain << ";\n");
1510 DEBUG(dbgs() << "Proximity := " << Proximity << ";\n");
1511 DEBUG(dbgs() << "Validity := " << Validity << ";\n");
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001512
Michael Krusec59f22c2015-06-18 16:45:40 +00001513 unsigned IslSerializeSCCs;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001514
1515 if (FusionStrategy == "max") {
Michael Krusec59f22c2015-06-18 16:45:40 +00001516 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001517 } else if (FusionStrategy == "min") {
Michael Krusec59f22c2015-06-18 16:45:40 +00001518 IslSerializeSCCs = 1;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001519 } else {
1520 errs() << "warning: Unknown fusion strategy. Falling back to maximal "
1521 "fusion.\n";
Michael Krusec59f22c2015-06-18 16:45:40 +00001522 IslSerializeSCCs = 0;
Tobias Grosserb3ad85b2012-01-30 19:38:50 +00001523 }
1524
Tobias Grosser95e860c2012-01-30 19:38:54 +00001525 int IslMaximizeBands;
1526
Tobias Grossera4ea90b2012-01-30 22:43:56 +00001527 if (MaximizeBandDepth == "yes") {
Tobias Grosser95e860c2012-01-30 19:38:54 +00001528 IslMaximizeBands = 1;
Tobias Grossera4ea90b2012-01-30 22:43:56 +00001529 } else if (MaximizeBandDepth == "no") {
Tobias Grosser95e860c2012-01-30 19:38:54 +00001530 IslMaximizeBands = 0;
1531 } else {
1532 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
1533 " or 'no'. Falling back to default: 'yes'\n";
1534 IslMaximizeBands = 1;
1535 }
1536
Michael Kruse315aa322016-05-02 11:35:27 +00001537 int IslOuterCoincidence;
1538
1539 if (OuterCoincidence == "yes") {
1540 IslOuterCoincidence = 1;
1541 } else if (OuterCoincidence == "no") {
1542 IslOuterCoincidence = 0;
1543 } else {
1544 errs() << "warning: Option -polly-opt-outer-coincidence should either be "
1545 "'yes' or 'no'. Falling back to default: 'no'\n";
1546 IslOuterCoincidence = 0;
1547 }
1548
Tobias Grosseraf149932016-06-30 20:42:56 +00001549 isl_ctx *Ctx = S.getIslCtx();
Tobias Grosser42152ff2012-01-30 19:38:47 +00001550
Tobias Grosseraf149932016-06-30 20:42:56 +00001551 isl_options_set_schedule_outer_coincidence(Ctx, IslOuterCoincidence);
1552 isl_options_set_schedule_serialize_sccs(Ctx, IslSerializeSCCs);
1553 isl_options_set_schedule_maximize_band_depth(Ctx, IslMaximizeBands);
1554 isl_options_set_schedule_max_constant_term(Ctx, MaxConstantTerm);
1555 isl_options_set_schedule_max_coefficient(Ctx, MaxCoefficient);
1556 isl_options_set_tile_scale_tile_loops(Ctx, 0);
1557
Tobias Grosser3898a042016-06-30 20:42:58 +00001558 auto OnErrorStatus = isl_options_get_on_error(Ctx);
Tobias Grosseraf149932016-06-30 20:42:56 +00001559 isl_options_set_on_error(Ctx, ISL_ON_ERROR_CONTINUE);
Tobias Grossera38c9242014-01-26 19:36:28 +00001560
Tobias Grosser7205f932017-05-21 16:21:33 +00001561 auto SC = isl::schedule_constraints::on_domain(Domain);
1562 SC = SC.set_proximity(Proximity);
1563 SC = SC.set_validity(Validity);
1564 SC = SC.set_coincidence(Validity);
Tobias Grosser00383a72012-02-14 14:02:44 +00001565 isl_schedule *Schedule;
Tobias Grosser7205f932017-05-21 16:21:33 +00001566 Schedule = SC.compute_schedule().release();
Tobias Grosser3898a042016-06-30 20:42:58 +00001567 isl_options_set_on_error(Ctx, OnErrorStatus);
Tobias Grosser42152ff2012-01-30 19:38:47 +00001568
1569 // In cases the scheduler is not able to optimize the code, we just do not
1570 // touch the schedule.
Tobias Grosser98610ee2012-02-13 23:31:39 +00001571 if (!Schedule)
Tobias Grosser42152ff2012-01-30 19:38:47 +00001572 return false;
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001573
Tobias Grosser97d87452015-05-30 06:46:59 +00001574 DEBUG({
Tobias Grosseraf149932016-06-30 20:42:56 +00001575 auto *P = isl_printer_to_str(Ctx);
Tobias Grosser97d87452015-05-30 06:46:59 +00001576 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
1577 P = isl_printer_print_schedule(P, Schedule);
Michael Kruse79c01732016-12-12 14:51:06 +00001578 auto *str = isl_printer_get_str(P);
1579 dbgs() << "NewScheduleTree: \n" << str << "\n";
1580 free(str);
Tobias Grosser97d87452015-05-30 06:46:59 +00001581 isl_printer_free(P);
1582 });
Tobias Grosser4d63b9d2012-02-20 08:41:21 +00001583
Roman Gareev42402c92016-06-22 09:52:37 +00001584 Function &F = S.getFunction();
1585 auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Roman Gareev98075fe2017-02-02 14:23:14 +00001586 const OptimizerAdditionalInfoTy OAI = {TTI, const_cast<Dependences *>(&D)};
Roman Gareev42402c92016-06-22 09:52:37 +00001587 isl_schedule *NewSchedule =
Roman Gareev98075fe2017-02-02 14:23:14 +00001588 ScheduleTreeOptimizer::optimizeSchedule(Schedule, &OAI);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001589
Roman Gareevb3224ad2016-09-14 06:26:09 +00001590 if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewSchedule)) {
Tobias Grosser808cd692015-07-14 09:33:13 +00001591 isl_schedule_free(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001592 return false;
1593 }
1594
Tobias Grosser808cd692015-07-14 09:33:13 +00001595 S.setScheduleTree(NewSchedule);
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001596 S.markAsOptimized();
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001597
Roman Gareev5f99f862016-08-21 11:20:39 +00001598 if (OptimizedScops)
1599 S.dump();
1600
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001601 return false;
1602}
1603
Johannes Doerfert3fe584d2015-03-01 18:40:25 +00001604void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
Tobias Grosser28781422012-10-16 07:29:19 +00001605 isl_printer *p;
1606 char *ScheduleStr;
1607
1608 OS << "Calculated schedule:\n";
1609
1610 if (!LastSchedule) {
1611 OS << "n/a\n";
1612 return;
1613 }
1614
1615 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
1616 p = isl_printer_print_schedule(p, LastSchedule);
1617 ScheduleStr = isl_printer_get_str(p);
1618 isl_printer_free(p);
1619
1620 OS << ScheduleStr << "\n";
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001621}
1622
Tobias Grosser73600b82011-10-08 00:30:40 +00001623void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001624 ScopPass::getAnalysisUsage(AU);
Johannes Doerfertf6557f92015-03-04 22:43:40 +00001625 AU.addRequired<DependenceInfo>();
Roman Gareev42402c92016-06-22 09:52:37 +00001626 AU.addRequired<TargetTransformInfoWrapperPass>();
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001627}
1628
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001629Pass *polly::createIslScheduleOptimizerPass() {
Tobias Grosser73600b82011-10-08 00:30:40 +00001630 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001631}
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001632
1633INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
1634 "Polly - Optimize schedule of SCoP", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +00001635INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Johannes Doerfert99191c72016-05-31 09:41:04 +00001636INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
Roman Gareev42402c92016-06-22 09:52:37 +00001637INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001638INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
1639 "Polly - Optimize schedule of SCoP", false, false)