2011-05-15 03:02:06 +08:00
|
|
|
|
//===- Schedule.cpp - Calculate an optimized schedule ---------------------===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2016-08-03 13:28:09 +08:00
|
|
|
|
// This pass generates an entirely new schedule tree from the data dependences
|
2015-08-15 17:34:33 +08:00
|
|
|
|
// and iteration domains. The new schedule tree is computed in two steps:
|
|
|
|
|
//
|
|
|
|
|
// 1) The isl scheduling optimizer is run
|
|
|
|
|
//
|
|
|
|
|
// The isl scheduling optimizer creates a new schedule tree that maximizes
|
|
|
|
|
// parallelism and tileability and minimizes data-dependence distances. The
|
|
|
|
|
// algorithm used is a modified version of the ``Pluto'' algorithm:
|
|
|
|
|
//
|
|
|
|
|
// U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan.
|
|
|
|
|
// A Practical Automatic Polyhedral Parallelizer and Locality Optimizer.
|
|
|
|
|
// In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language
|
|
|
|
|
// Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008.
|
|
|
|
|
//
|
|
|
|
|
// 2) A set of post-scheduling transformations is applied on the schedule tree.
|
|
|
|
|
//
|
|
|
|
|
// These optimizations include:
|
|
|
|
|
//
|
|
|
|
|
// - Tiling of the innermost tilable bands
|
2017-03-17 22:52:19 +08:00
|
|
|
|
// - Prevectorization - The choice of a possible outer loop that is strip-mined
|
2015-08-15 17:34:33 +08:00
|
|
|
|
// to the innermost level to enable inner-loop
|
|
|
|
|
// vectorization.
|
|
|
|
|
// - Some optimizations for spatial locality are also planned.
|
|
|
|
|
//
|
|
|
|
|
// For a detailed description of the schedule tree itself please see section 6
|
|
|
|
|
// of:
|
|
|
|
|
//
|
|
|
|
|
// Polyhedral AST generation is more than scanning polyhedra
|
|
|
|
|
// Tobias Grosser, Sven Verdoolaege, Albert Cohen
|
2017-03-17 22:52:19 +08:00
|
|
|
|
// ACM Transactions on Programming Languages and Systems (TOPLAS),
|
2015-08-15 17:34:33 +08:00
|
|
|
|
// 37(4), July 2015
|
|
|
|
|
// http://www.grosser.es/#pub-polyhedral-AST-generation
|
|
|
|
|
//
|
|
|
|
|
// This publication also contains a detailed discussion of the different options
|
|
|
|
|
// for polyhedral loop unrolling, full/partial tile separation and other uses
|
|
|
|
|
// of the schedule tree.
|
|
|
|
|
//
|
2011-05-15 03:02:06 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2011-10-24 04:59:44 +08:00
|
|
|
|
#include "polly/ScheduleOptimizer.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
|
|
|
|
#include "polly/DependenceInfo.h"
|
|
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
|
#include "polly/Options.h"
|
|
|
|
|
#include "polly/ScopInfo.h"
|
|
|
|
|
#include "polly/Support/GICHelper.h"
|
2017-05-22 00:21:33 +08:00
|
|
|
|
#include "polly/Support/ISLOStream.h"
|
2016-06-22 17:52:37 +08:00
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
|
#include "llvm/Support/Debug.h"
|
2011-12-07 15:42:57 +08:00
|
|
|
|
#include "isl/aff.h"
|
2011-07-01 04:01:02 +08:00
|
|
|
|
#include "isl/band.h"
|
2012-01-31 21:26:29 +08:00
|
|
|
|
#include "isl/constraint.h"
|
|
|
|
|
#include "isl/map.h"
|
2012-01-31 03:38:47 +08:00
|
|
|
|
#include "isl/options.h"
|
2015-05-30 14:46:59 +08:00
|
|
|
|
#include "isl/printer.h"
|
2012-01-31 21:26:29 +08:00
|
|
|
|
#include "isl/schedule.h"
|
2015-03-22 20:06:39 +08:00
|
|
|
|
#include "isl/schedule_node.h"
|
2012-01-31 21:26:29 +08:00
|
|
|
|
#include "isl/space.h"
|
2015-05-09 17:36:38 +08:00
|
|
|
|
#include "isl/union_map.h"
|
|
|
|
|
#include "isl/union_set.h"
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace polly;
|
|
|
|
|
|
2014-04-22 11:30:19 +08:00
|
|
|
|
#define DEBUG_TYPE "polly-opt-isl"
|
|
|
|
|
|
2012-02-14 22:02:48 +08:00
|
|
|
|
static cl::opt<std::string>
|
2014-07-09 18:50:10 +08:00
|
|
|
|
OptimizeDeps("polly-opt-optimize-only",
|
|
|
|
|
cl::desc("Only a certain kind of dependences (all/raw)"),
|
|
|
|
|
cl::Hidden, cl::init("all"), cl::ZeroOrMore,
|
|
|
|
|
cl::cat(PollyCategory));
|
2012-02-14 22:02:48 +08:00
|
|
|
|
|
2012-01-31 03:38:43 +08:00
|
|
|
|
static cl::opt<std::string>
|
2014-07-09 18:50:10 +08:00
|
|
|
|
SimplifyDeps("polly-opt-simplify-deps",
|
|
|
|
|
cl::desc("Dependences should be simplified (yes/no)"),
|
|
|
|
|
cl::Hidden, cl::init("yes"), cl::ZeroOrMore,
|
|
|
|
|
cl::cat(PollyCategory));
|
2012-01-31 03:38:43 +08:00
|
|
|
|
|
2014-07-09 18:50:10 +08:00
|
|
|
|
static cl::opt<int> MaxConstantTerm(
|
|
|
|
|
"polly-opt-max-constant-term",
|
|
|
|
|
cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden,
|
|
|
|
|
cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2012-02-20 16:41:15 +08:00
|
|
|
|
|
2014-07-09 18:50:10 +08:00
|
|
|
|
static cl::opt<int> MaxCoefficient(
|
|
|
|
|
"polly-opt-max-coefficient",
|
|
|
|
|
cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden,
|
|
|
|
|
cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2012-02-20 16:41:47 +08:00
|
|
|
|
|
2014-07-09 18:50:10 +08:00
|
|
|
|
static cl::opt<std::string> FusionStrategy(
|
|
|
|
|
"polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"),
|
|
|
|
|
cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2012-01-31 03:38:50 +08:00
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
|
static cl::opt<std::string>
|
2014-07-09 18:50:10 +08:00
|
|
|
|
MaximizeBandDepth("polly-opt-maximize-bands",
|
|
|
|
|
cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
|
|
|
|
|
cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2012-01-31 03:38:54 +08:00
|
|
|
|
|
2016-05-02 19:35:27 +08:00
|
|
|
|
static cl::opt<std::string> OuterCoincidence(
|
|
|
|
|
"polly-opt-outer-coincidence",
|
|
|
|
|
cl::desc("Try to construct schedules where the outer member of each band "
|
|
|
|
|
"satisfies the coincidence constraints (yes/no)"),
|
|
|
|
|
cl::Hidden, cl::init("no"), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
2015-08-19 16:46:11 +08:00
|
|
|
|
static cl::opt<int> PrevectorWidth(
|
|
|
|
|
"polly-prevect-width",
|
|
|
|
|
cl::desc(
|
|
|
|
|
"The number of loop iterations to strip-mine for pre-vectorization"),
|
|
|
|
|
cl::Hidden, cl::init(4), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
2015-08-20 21:45:02 +08:00
|
|
|
|
static cl::opt<bool> FirstLevelTiling("polly-tiling",
|
|
|
|
|
cl::desc("Enable loop tiling"),
|
|
|
|
|
cl::init(true), cl::ZeroOrMore,
|
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
|
2016-06-22 17:52:37 +08:00
|
|
|
|
static cl::opt<int> LatencyVectorFma(
|
|
|
|
|
"polly-target-latency-vector-fma",
|
|
|
|
|
cl::desc("The minimal number of cycles between issuing two "
|
|
|
|
|
"dependent consecutive vector fused multiply-add "
|
|
|
|
|
"instructions."),
|
|
|
|
|
cl::Hidden, cl::init(8), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
2016-12-23 15:33:39 +08:00
|
|
|
|
static cl::opt<int> ThroughputVectorFma(
|
|
|
|
|
"polly-target-throughput-vector-fma",
|
2016-06-22 17:52:37 +08:00
|
|
|
|
cl::desc("A throughput of the processor floating-point arithmetic units "
|
|
|
|
|
"expressed in the number of vector fused multiply-add "
|
|
|
|
|
"instructions per clock cycle."),
|
|
|
|
|
cl::Hidden, cl::init(1), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
2016-12-26 00:32:28 +08:00
|
|
|
|
// This option, along with --polly-target-2nd-cache-level-associativity,
|
|
|
|
|
// --polly-target-1st-cache-level-size, and --polly-target-2st-cache-level-size
|
|
|
|
|
// represent the parameters of the target cache, which do not have typical
|
|
|
|
|
// values that can be used by default. However, to apply the pattern matching
|
|
|
|
|
// optimizations, we use the values of the parameters of Intel Core i7-3820
|
|
|
|
|
// SandyBridge in case the parameters are not specified. Such an approach helps
|
|
|
|
|
// also to attain the high-performance on IBM POWER System S822 and IBM Power
|
|
|
|
|
// 730 Express server.
|
|
|
|
|
static cl::opt<int> FirstCacheLevelAssociativity(
|
|
|
|
|
"polly-target-1st-cache-level-associativity",
|
|
|
|
|
cl::desc("The associativity of the first cache level."), cl::Hidden,
|
|
|
|
|
cl::init(8), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
|
|
|
|
static cl::opt<int> SecondCacheLevelAssociativity(
|
|
|
|
|
"polly-target-2nd-cache-level-associativity",
|
|
|
|
|
cl::desc("The associativity of the second cache level."), cl::Hidden,
|
|
|
|
|
cl::init(8), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
|
|
|
|
static cl::opt<int> FirstCacheLevelSize(
|
|
|
|
|
"polly-target-1st-cache-level-size",
|
|
|
|
|
cl::desc("The size of the first cache level specified in bytes."),
|
|
|
|
|
cl::Hidden, cl::init(32768), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
|
|
|
|
static cl::opt<int> SecondCacheLevelSize(
|
|
|
|
|
"polly-target-2nd-cache-level-size",
|
|
|
|
|
cl::desc("The size of the second level specified in bytes."), cl::Hidden,
|
|
|
|
|
cl::init(262144), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2016-07-25 17:42:53 +08:00
|
|
|
|
|
2017-01-14 15:14:54 +08:00
|
|
|
|
static cl::opt<int> VectorRegisterBitwidth(
|
|
|
|
|
"polly-target-vector-register-bitwidth",
|
|
|
|
|
cl::desc("The size in bits of a vector register (if not set, this "
|
|
|
|
|
"information is taken from LLVM's target information."),
|
|
|
|
|
cl::Hidden, cl::init(-1), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
2015-08-20 21:45:02 +08:00
|
|
|
|
static cl::opt<int> FirstLevelDefaultTileSize(
|
2014-07-09 18:50:10 +08:00
|
|
|
|
"polly-default-tile-size",
|
|
|
|
|
cl::desc("The default tile size (if not enough were provided by"
|
|
|
|
|
" --polly-tile-sizes)"),
|
|
|
|
|
cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2014-05-29 01:21:02 +08:00
|
|
|
|
|
2017-01-16 22:08:10 +08:00
|
|
|
|
static cl::list<int>
|
|
|
|
|
FirstLevelTileSizes("polly-tile-sizes",
|
|
|
|
|
cl::desc("A tile size for each loop dimension, filled "
|
2015-08-20 21:45:02 +08:00
|
|
|
|
"with --polly-default-tile-size"),
|
2017-01-16 22:08:10 +08:00
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
|
|
|
|
|
cl::cat(PollyCategory));
|
2015-08-20 21:45:02 +08:00
|
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
|
SecondLevelTiling("polly-2nd-level-tiling",
|
|
|
|
|
cl::desc("Enable a 2nd level loop of loop tiling"),
|
|
|
|
|
cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
|
|
|
|
static cl::opt<int> SecondLevelDefaultTileSize(
|
|
|
|
|
"polly-2nd-level-default-tile-size",
|
|
|
|
|
cl::desc("The default 2nd-level tile size (if not enough were provided by"
|
|
|
|
|
" --polly-2nd-level-tile-sizes)"),
|
|
|
|
|
cl::Hidden, cl::init(16), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
|
|
|
|
static cl::list<int>
|
|
|
|
|
SecondLevelTileSizes("polly-2nd-level-tile-sizes",
|
|
|
|
|
cl::desc("A tile size for each loop dimension, filled "
|
|
|
|
|
"with --polly-default-tile-size"),
|
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
|
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
|
2015-08-20 21:45:05 +08:00
|
|
|
|
static cl::opt<bool> RegisterTiling("polly-register-tiling",
|
|
|
|
|
cl::desc("Enable register tiling"),
|
|
|
|
|
cl::init(false), cl::ZeroOrMore,
|
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
|
|
|
|
|
static cl::opt<int> RegisterDefaultTileSize(
|
|
|
|
|
"polly-register-tiling-default-tile-size",
|
|
|
|
|
cl::desc("The default register tile size (if not enough were provided by"
|
|
|
|
|
" --polly-register-tile-sizes)"),
|
|
|
|
|
cl::Hidden, cl::init(2), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
Change the determination of parameters of macro-kernel
Typically processor architectures do not include an L3 cache, which means that
Nc, the parameter of the micro-kernel, is, for all practical purposes,
redundant ([1]). However, its small values can cause the redundant packing of
the same elements of the matrix A, the first operand of the matrix
multiplication. At the same time, big values of the parameter Nc can cause
segmentation faults in case the available stack is exceeded.
This patch adds an option to specify the parameter Nc as a multiple of
the parameter of the micro-kernel Nr.
In case of Intel Core i7-3820 SandyBridge and the following options,
clang -O3 gemm.c -I utilities/ utilities/polybench.c -DPOLYBENCH_TIME
-march=native -mllvm -polly -mllvm -polly-pattern-matching-based-opts=true
-DPOLYBENCH_USE_SCALAR_LB -mllvm -polly-target-cache-level-associativity=8,8
-mllvm -polly-target-cache-level-sizes=32768,262144 -mllvm
-polly-target-latency-vector-fma=8
it helps to improve the performance from 11.303 GFlops/sec (39,247% of
theoretical peak) to 17.896 GFlops/sec (62,14% of theoretical peak).
Refs.:
[1] - http://www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf
Reviewed-by: Tobias Grosser <tobias@grosser.es>
Differential Revision: https://reviews.llvm.org/D28019
llvm-svn: 290256
2016-12-21 20:51:12 +08:00
|
|
|
|
static cl::opt<int> PollyPatternMatchingNcQuotient(
|
|
|
|
|
"polly-pattern-matching-nc-quotient",
|
|
|
|
|
cl::desc("Quotient that is obtained by dividing Nc, the parameter of the"
|
|
|
|
|
"macro-kernel, by Nr, the parameter of the micro-kernel"),
|
|
|
|
|
cl::Hidden, cl::init(256), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
2015-08-20 21:45:05 +08:00
|
|
|
|
static cl::list<int>
|
|
|
|
|
RegisterTileSizes("polly-register-tile-sizes",
|
|
|
|
|
cl::desc("A tile size for each loop dimension, filled "
|
|
|
|
|
"with --polly-register-tile-size"),
|
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated,
|
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
|
2016-05-29 00:17:58 +08:00
|
|
|
|
static cl::opt<bool>
|
|
|
|
|
PMBasedOpts("polly-pattern-matching-based-opts",
|
|
|
|
|
cl::desc("Perform optimizations based on pattern matching"),
|
2017-02-23 19:44:12 +08:00
|
|
|
|
cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2016-05-29 00:17:58 +08:00
|
|
|
|
|
2016-08-21 19:20:39 +08:00
|
|
|
|
static cl::opt<bool> OptimizedScops(
|
|
|
|
|
"polly-optimized-scops",
|
|
|
|
|
cl::desc("Polly - Dump polyhedral description of Scops optimized with "
|
|
|
|
|
"the isl scheduling optimizer and the set of post-scheduling "
|
|
|
|
|
"transformations is applied on the schedule tree"),
|
|
|
|
|
cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Create an isl_union_set, which describes the isolate option based on
|
|
|
|
|
/// IsoalteDomain.
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
///
|
2017-02-09 15:10:01 +08:00
|
|
|
|
/// @param IsolateDomain An isl_set whose @p OutDimsNum last dimensions should
|
|
|
|
|
/// belong to the current band node.
|
|
|
|
|
/// @param OutDimsNum A number of dimensions that should belong to
|
|
|
|
|
/// the current band node.
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
static __isl_give isl_union_set *
|
2017-02-09 15:10:01 +08:00
|
|
|
|
getIsolateOptions(__isl_take isl_set *IsolateDomain, unsigned OutDimsNum) {
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
auto Dims = isl_set_dim(IsolateDomain, isl_dim_set);
|
2017-02-09 15:10:01 +08:00
|
|
|
|
assert(OutDimsNum <= Dims &&
|
|
|
|
|
"The isl_set IsolateDomain is used to describe the range of schedule "
|
|
|
|
|
"dimensions values, which should be isolated. Consequently, the "
|
|
|
|
|
"number of its dimensions should be greater than or equal to the "
|
|
|
|
|
"number of the schedule dimensions.");
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
auto *IsolateRelation = isl_map_from_domain(IsolateDomain);
|
2017-02-09 15:10:01 +08:00
|
|
|
|
IsolateRelation =
|
|
|
|
|
isl_map_move_dims(IsolateRelation, isl_dim_out, 0, isl_dim_in,
|
|
|
|
|
Dims - OutDimsNum, OutDimsNum);
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
auto *IsolateOption = isl_map_wrap(IsolateRelation);
|
2016-06-23 00:22:00 +08:00
|
|
|
|
auto *Id = isl_id_alloc(isl_set_get_ctx(IsolateOption), "isolate", nullptr);
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
return isl_union_set_from_set(isl_set_set_tuple_id(IsolateOption, Id));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Create an isl_union_set, which describes the atomic option for the dimension
|
|
|
|
|
/// of the current node.
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
///
|
|
|
|
|
/// It may help to reduce the size of generated code.
|
|
|
|
|
///
|
|
|
|
|
/// @param Ctx An isl_ctx, which is used to create the isl_union_set.
|
2017-02-11 15:14:37 +08:00
|
|
|
|
static __isl_give isl_union_set *getAtomicOptions(isl_ctx *Ctx) {
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
auto *Space = isl_space_set_alloc(Ctx, 0, 1);
|
|
|
|
|
auto *AtomicOption = isl_set_universe(Space);
|
2016-06-23 00:22:00 +08:00
|
|
|
|
auto *Id = isl_id_alloc(Ctx, "atomic", nullptr);
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
return isl_union_set_from_set(isl_set_set_tuple_id(AtomicOption, Id));
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-09 15:10:01 +08:00
|
|
|
|
/// Create an isl_union_set, which describes the option of the form
|
|
|
|
|
/// [isolate[] -> unroll[x]].
|
|
|
|
|
///
|
|
|
|
|
/// @param Ctx An isl_ctx, which is used to create the isl_union_set.
|
|
|
|
|
static __isl_give isl_union_set *getUnrollIsolatedSetOptions(isl_ctx *Ctx) {
|
|
|
|
|
auto *Space = isl_space_alloc(Ctx, 0, 0, 1);
|
|
|
|
|
auto *UnrollIsolatedSetOption = isl_map_universe(Space);
|
|
|
|
|
auto *DimInId = isl_id_alloc(Ctx, "isolate", nullptr);
|
|
|
|
|
auto *DimOutId = isl_id_alloc(Ctx, "unroll", nullptr);
|
|
|
|
|
UnrollIsolatedSetOption =
|
|
|
|
|
isl_map_set_tuple_id(UnrollIsolatedSetOption, isl_dim_in, DimInId);
|
|
|
|
|
UnrollIsolatedSetOption =
|
|
|
|
|
isl_map_set_tuple_id(UnrollIsolatedSetOption, isl_dim_out, DimOutId);
|
|
|
|
|
return isl_union_set_from_set(isl_map_wrap(UnrollIsolatedSetOption));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Make the last dimension of Set to take values from 0 to VectorWidth - 1.
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
///
|
|
|
|
|
/// @param Set A set, which should be modified.
|
|
|
|
|
/// @param VectorWidth A parameter, which determines the constraint.
|
|
|
|
|
static __isl_give isl_set *addExtentConstraints(__isl_take isl_set *Set,
|
|
|
|
|
int VectorWidth) {
|
|
|
|
|
auto Dims = isl_set_dim(Set, isl_dim_set);
|
|
|
|
|
auto Space = isl_set_get_space(Set);
|
|
|
|
|
auto *LocalSpace = isl_local_space_from_space(Space);
|
|
|
|
|
auto *ExtConstr =
|
|
|
|
|
isl_constraint_alloc_inequality(isl_local_space_copy(LocalSpace));
|
|
|
|
|
ExtConstr = isl_constraint_set_constant_si(ExtConstr, 0);
|
|
|
|
|
ExtConstr =
|
|
|
|
|
isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, 1);
|
|
|
|
|
Set = isl_set_add_constraint(Set, ExtConstr);
|
|
|
|
|
ExtConstr = isl_constraint_alloc_inequality(LocalSpace);
|
|
|
|
|
ExtConstr = isl_constraint_set_constant_si(ExtConstr, VectorWidth - 1);
|
|
|
|
|
ExtConstr =
|
|
|
|
|
isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, -1);
|
|
|
|
|
return isl_set_add_constraint(Set, ExtConstr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Build the desired set of partial tile prefixes.
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
///
|
|
|
|
|
/// We build a set of partial tile prefixes, which are prefixes of the vector
|
|
|
|
|
/// loop that have exactly VectorWidth iterations.
|
|
|
|
|
///
|
|
|
|
|
/// 1. Get all prefixes of the vector loop.
|
|
|
|
|
/// 2. Extend it to a set, which has exactly VectorWidth iterations for
|
|
|
|
|
/// any prefix from the set that was built on the previous step.
|
|
|
|
|
/// 3. Subtract loop domain from it, project out the vector loop dimension and
|
2016-05-31 19:22:21 +08:00
|
|
|
|
/// get a set of prefixes, which don't have exactly VectorWidth iterations.
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
/// 4. Subtract it from all prefixes of the vector loop and get the desired
|
|
|
|
|
/// set.
|
|
|
|
|
///
|
|
|
|
|
/// @param ScheduleRange A range of a map, which describes a prefix schedule
|
|
|
|
|
/// relation.
|
|
|
|
|
static __isl_give isl_set *
|
|
|
|
|
getPartialTilePrefixes(__isl_take isl_set *ScheduleRange, int VectorWidth) {
|
|
|
|
|
auto Dims = isl_set_dim(ScheduleRange, isl_dim_set);
|
|
|
|
|
auto *LoopPrefixes = isl_set_project_out(isl_set_copy(ScheduleRange),
|
|
|
|
|
isl_dim_set, Dims - 1, 1);
|
|
|
|
|
auto *ExtentPrefixes =
|
|
|
|
|
isl_set_add_dims(isl_set_copy(LoopPrefixes), isl_dim_set, 1);
|
|
|
|
|
ExtentPrefixes = addExtentConstraints(ExtentPrefixes, VectorWidth);
|
|
|
|
|
auto *BadPrefixes = isl_set_subtract(ExtentPrefixes, ScheduleRange);
|
|
|
|
|
BadPrefixes = isl_set_project_out(BadPrefixes, isl_dim_set, Dims - 1, 1);
|
|
|
|
|
return isl_set_subtract(LoopPrefixes, BadPrefixes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__isl_give isl_schedule_node *ScheduleTreeOptimizer::isolateFullPartialTiles(
|
|
|
|
|
__isl_take isl_schedule_node *Node, int VectorWidth) {
|
|
|
|
|
assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
|
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
|
|
|
|
auto *SchedRelUMap = isl_schedule_node_get_prefix_schedule_relation(Node);
|
|
|
|
|
auto *ScheduleRelation = isl_map_from_union_map(SchedRelUMap);
|
|
|
|
|
auto *ScheduleRange = isl_map_range(ScheduleRelation);
|
|
|
|
|
auto *IsolateDomain = getPartialTilePrefixes(ScheduleRange, VectorWidth);
|
|
|
|
|
auto *AtomicOption = getAtomicOptions(isl_set_get_ctx(IsolateDomain));
|
2017-02-09 15:10:01 +08:00
|
|
|
|
auto *IsolateOption = getIsolateOptions(IsolateDomain, 1);
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
Node = isl_schedule_node_parent(Node);
|
|
|
|
|
Node = isl_schedule_node_parent(Node);
|
|
|
|
|
auto *Options = isl_union_set_union(IsolateOption, AtomicOption);
|
|
|
|
|
Node = isl_schedule_node_band_set_ast_build_options(Node, Options);
|
|
|
|
|
return Node;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 02:03:36 +08:00
|
|
|
|
__isl_give isl_schedule_node *
|
2015-08-24 14:01:47 +08:00
|
|
|
|
ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node,
|
|
|
|
|
unsigned DimToVectorize,
|
|
|
|
|
int VectorWidth) {
|
2015-07-29 02:03:36 +08:00
|
|
|
|
assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
|
|
|
|
|
|
|
|
|
|
auto Space = isl_schedule_node_band_get_space(Node);
|
|
|
|
|
auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set);
|
|
|
|
|
isl_space_free(Space);
|
|
|
|
|
assert(DimToVectorize < ScheduleDimensions);
|
|
|
|
|
|
|
|
|
|
if (DimToVectorize > 0) {
|
|
|
|
|
Node = isl_schedule_node_band_split(Node, DimToVectorize);
|
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
|
|
|
|
}
|
|
|
|
|
if (DimToVectorize < ScheduleDimensions - 1)
|
|
|
|
|
Node = isl_schedule_node_band_split(Node, 1);
|
|
|
|
|
Space = isl_schedule_node_band_get_space(Node);
|
|
|
|
|
auto Sizes = isl_multi_val_zero(Space);
|
|
|
|
|
auto Ctx = isl_schedule_node_get_ctx(Node);
|
|
|
|
|
Sizes =
|
|
|
|
|
isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth));
|
|
|
|
|
Node = isl_schedule_node_band_tile(Node, Sizes);
|
Full/partial tile separation for vectorization
We isolate full tiles from partial tiles to be able to, for example, vectorize
loops with parametric lower and/or upper bounds.
If we use -polly-vectorizer=stripmine, we can see execution-time improvements:
correlation from 1m7361s to 0m5720s (-67.05 %), covariance from 1m5561s to
0m5680s (-63.50 %), ary3 from 2m3201s to 1m2361s (-46.72 %), CrystalMk from
8m5565s to 7m4285s (-13.18 %).
The current full/partial tile separation increases compile-time more than
necessary. As a result, we see in compile time regressions, for example, for 3mm
from 0m6320s to 0m9881s (56.34%). Some of this compile time increase is expected
as we generate more IR and consequently more time is spent in the LLVM backends.
However, a first investiagation has shown that a larger portion of compile time
is unnecessarily spent inside Polly's parallelism detection and could be
eliminated by propagating existing knowledge about vector loop parallelism.
Before enabling -polly-vectorizer=stripmine by default, it is necessary to
address this compile-time issue.
Contributed-by: Roman Gareev <gareevroman@gmail.com>
Reviewers: jdoerfert, grosser
Subscribers: grosser, #polly
Differential Revision: http://reviews.llvm.org/D13779
llvm-svn: 250809
2015-10-20 17:12:21 +08:00
|
|
|
|
Node = isolateFullPartialTiles(Node, VectorWidth);
|
2015-07-29 02:03:36 +08:00
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
2015-08-20 21:45:05 +08:00
|
|
|
|
// Make sure the "trivially vectorizable loop" is not unrolled. Otherwise,
|
|
|
|
|
// we will have troubles to match it in the backend.
|
|
|
|
|
Node = isl_schedule_node_band_set_ast_build_options(
|
2015-08-21 03:08:16 +08:00
|
|
|
|
Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }"));
|
|
|
|
|
Node = isl_schedule_node_band_sink(Node);
|
2015-07-29 02:03:36 +08:00
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
2016-02-23 17:00:13 +08:00
|
|
|
|
if (isl_schedule_node_get_type(Node) == isl_schedule_node_leaf)
|
|
|
|
|
Node = isl_schedule_node_parent(Node);
|
|
|
|
|
isl_id *LoopMarker = isl_id_alloc(Ctx, "SIMD", nullptr);
|
|
|
|
|
Node = isl_schedule_node_insert_mark(Node, LoopMarker);
|
2015-07-29 02:03:36 +08:00
|
|
|
|
return Node;
|
2011-07-01 04:29:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 20:22:37 +08:00
|
|
|
|
__isl_give isl_schedule_node *
|
2015-08-24 14:01:47 +08:00
|
|
|
|
ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node,
|
|
|
|
|
const char *Identifier, ArrayRef<int> TileSizes,
|
|
|
|
|
int DefaultTileSize) {
|
2015-08-20 20:22:37 +08:00
|
|
|
|
auto Ctx = isl_schedule_node_get_ctx(Node);
|
|
|
|
|
auto Space = isl_schedule_node_band_get_space(Node);
|
|
|
|
|
auto Dims = isl_space_dim(Space, isl_dim_set);
|
|
|
|
|
auto Sizes = isl_multi_val_zero(Space);
|
2015-08-23 17:11:00 +08:00
|
|
|
|
std::string IdentifierString(Identifier);
|
2015-08-20 20:22:37 +08:00
|
|
|
|
for (unsigned i = 0; i < Dims; i++) {
|
|
|
|
|
auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize;
|
|
|
|
|
Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
|
|
|
|
|
}
|
2015-08-23 17:11:00 +08:00
|
|
|
|
auto TileLoopMarkerStr = IdentifierString + " - Tiles";
|
|
|
|
|
isl_id *TileLoopMarker =
|
|
|
|
|
isl_id_alloc(Ctx, TileLoopMarkerStr.c_str(), nullptr);
|
|
|
|
|
Node = isl_schedule_node_insert_mark(Node, TileLoopMarker);
|
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
2015-08-20 20:22:37 +08:00
|
|
|
|
Node = isl_schedule_node_band_tile(Node, Sizes);
|
2015-08-23 17:11:00 +08:00
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
|
|
|
|
auto PointLoopMarkerStr = IdentifierString + " - Points";
|
|
|
|
|
isl_id *PointLoopMarker =
|
|
|
|
|
isl_id_alloc(Ctx, PointLoopMarkerStr.c_str(), nullptr);
|
|
|
|
|
Node = isl_schedule_node_insert_mark(Node, PointLoopMarker);
|
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
|
|
|
|
return Node;
|
2015-08-20 20:22:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 01:20:05 +08:00
|
|
|
|
__isl_give isl_schedule_node *
|
|
|
|
|
ScheduleTreeOptimizer::applyRegisterTiling(__isl_take isl_schedule_node *Node,
|
|
|
|
|
llvm::ArrayRef<int> TileSizes,
|
|
|
|
|
int DefaultTileSize) {
|
|
|
|
|
auto *Ctx = isl_schedule_node_get_ctx(Node);
|
|
|
|
|
Node = tileNode(Node, "Register tiling", TileSizes, DefaultTileSize);
|
|
|
|
|
Node = isl_schedule_node_band_set_ast_build_options(
|
|
|
|
|
Node, isl_union_set_read_from_str(Ctx, "{unroll[x]}"));
|
|
|
|
|
return Node;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 03:02:31 +08:00
|
|
|
|
namespace {
|
|
|
|
|
bool isSimpleInnermostBand(const isl::schedule_node &Node) {
|
|
|
|
|
assert(isl_schedule_node_get_type(Node.keep()) == isl_schedule_node_band);
|
|
|
|
|
assert(isl_schedule_node_n_children(Node.keep()) == 1);
|
|
|
|
|
|
|
|
|
|
auto ChildType = isl_schedule_node_get_type(Node.child(0).keep());
|
|
|
|
|
|
|
|
|
|
if (ChildType == isl_schedule_node_leaf)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (ChildType != isl_schedule_node_sequence)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
auto Sequence = Node.child(0);
|
|
|
|
|
|
|
|
|
|
for (int c = 0, nc = isl_schedule_node_n_children(Sequence.keep()); c < nc;
|
|
|
|
|
++c) {
|
|
|
|
|
auto Child = Sequence.child(c);
|
|
|
|
|
if (isl_schedule_node_get_type(Child.keep()) != isl_schedule_node_filter)
|
|
|
|
|
return false;
|
|
|
|
|
if (isl_schedule_node_get_type(Child.child(0).keep()) !=
|
|
|
|
|
isl_schedule_node_leaf)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2015-08-24 14:01:47 +08:00
|
|
|
|
bool ScheduleTreeOptimizer::isTileableBandNode(
|
2015-08-20 20:32:45 +08:00
|
|
|
|
__isl_keep isl_schedule_node *Node) {
|
2015-03-22 20:06:39 +08:00
|
|
|
|
if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
|
2015-08-20 20:32:45 +08:00
|
|
|
|
return false;
|
2015-03-22 20:06:39 +08:00
|
|
|
|
|
|
|
|
|
if (isl_schedule_node_n_children(Node) != 1)
|
2015-08-20 20:32:45 +08:00
|
|
|
|
return false;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2015-03-22 20:06:39 +08:00
|
|
|
|
if (!isl_schedule_node_band_get_permutable(Node))
|
2015-08-20 20:32:45 +08:00
|
|
|
|
return false;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2015-03-22 20:06:39 +08:00
|
|
|
|
auto Space = isl_schedule_node_band_get_space(Node);
|
|
|
|
|
auto Dims = isl_space_dim(Space, isl_dim_set);
|
2015-08-20 20:22:37 +08:00
|
|
|
|
isl_space_free(Space);
|
2015-03-22 20:06:39 +08:00
|
|
|
|
|
2015-08-20 20:22:37 +08:00
|
|
|
|
if (Dims <= 1)
|
2015-08-20 20:32:45 +08:00
|
|
|
|
return false;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2017-03-13 03:02:31 +08:00
|
|
|
|
auto ManagedNode = isl::manage(isl_schedule_node_copy(Node));
|
|
|
|
|
return isSimpleInnermostBand(ManagedNode);
|
2015-08-20 20:32:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__isl_give isl_schedule_node *
|
2016-05-29 00:17:58 +08:00
|
|
|
|
ScheduleTreeOptimizer::standardBandOpts(__isl_take isl_schedule_node *Node,
|
|
|
|
|
void *User) {
|
2015-08-20 21:45:02 +08:00
|
|
|
|
if (FirstLevelTiling)
|
2015-08-23 17:11:00 +08:00
|
|
|
|
Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes,
|
|
|
|
|
FirstLevelDefaultTileSize);
|
2015-08-20 21:45:02 +08:00
|
|
|
|
|
|
|
|
|
if (SecondLevelTiling)
|
2015-08-23 17:11:00 +08:00
|
|
|
|
Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes,
|
|
|
|
|
SecondLevelDefaultTileSize);
|
2015-03-22 20:06:39 +08:00
|
|
|
|
|
2016-06-13 01:20:05 +08:00
|
|
|
|
if (RegisterTiling)
|
|
|
|
|
Node =
|
|
|
|
|
applyRegisterTiling(Node, RegisterTileSizes, RegisterDefaultTileSize);
|
2015-08-20 21:45:05 +08:00
|
|
|
|
|
2015-03-22 20:06:39 +08:00
|
|
|
|
if (PollyVectorizerChoice == VECTORIZER_NONE)
|
2015-08-19 16:03:37 +08:00
|
|
|
|
return Node;
|
2015-03-22 20:06:39 +08:00
|
|
|
|
|
2015-08-20 20:32:45 +08:00
|
|
|
|
auto Space = isl_schedule_node_band_get_space(Node);
|
|
|
|
|
auto Dims = isl_space_dim(Space, isl_dim_set);
|
|
|
|
|
isl_space_free(Space);
|
|
|
|
|
|
2015-07-29 02:03:36 +08:00
|
|
|
|
for (int i = Dims - 1; i >= 0; i--)
|
2015-08-19 16:03:37 +08:00
|
|
|
|
if (isl_schedule_node_band_member_get_coincident(Node, i)) {
|
2015-08-24 14:01:47 +08:00
|
|
|
|
Node = prevectSchedBand(Node, i, PrevectorWidth);
|
2015-03-22 20:06:39 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 16:03:37 +08:00
|
|
|
|
return Node;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 22:23:14 +08:00
|
|
|
|
/// Get the position of a dimension with a non-zero coefficient.
|
2016-05-29 00:17:58 +08:00
|
|
|
|
///
|
2017-02-02 22:23:14 +08:00
|
|
|
|
/// Check that isl constraint @p Constraint has only one non-zero
|
|
|
|
|
/// coefficient for dimensions that have type @p DimType. If this is true,
|
|
|
|
|
/// return the position of the dimension corresponding to the non-zero
|
|
|
|
|
/// coefficient and negative value, otherwise.
|
|
|
|
|
///
|
|
|
|
|
/// @param Constraint The isl constraint to be checked.
|
|
|
|
|
/// @param DimType The type of the dimensions.
|
|
|
|
|
/// @return The position of the dimension in case the isl
|
|
|
|
|
/// constraint satisfies the requirements, a negative
|
|
|
|
|
/// value, otherwise.
|
|
|
|
|
static int getMatMulConstraintDim(__isl_keep isl_constraint *Constraint,
|
|
|
|
|
enum isl_dim_type DimType) {
|
|
|
|
|
int DimPos = -1;
|
|
|
|
|
auto *LocalSpace = isl_constraint_get_local_space(Constraint);
|
|
|
|
|
int LocalSpaceDimNum = isl_local_space_dim(LocalSpace, DimType);
|
|
|
|
|
for (int i = 0; i < LocalSpaceDimNum; i++) {
|
|
|
|
|
auto *Val = isl_constraint_get_coefficient_val(Constraint, DimType, i);
|
|
|
|
|
if (isl_val_is_zero(Val)) {
|
|
|
|
|
isl_val_free(Val);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (DimPos >= 0 || (DimType == isl_dim_out && !isl_val_is_one(Val)) ||
|
|
|
|
|
(DimType == isl_dim_in && !isl_val_is_negone(Val))) {
|
|
|
|
|
isl_val_free(Val);
|
|
|
|
|
isl_local_space_free(LocalSpace);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
DimPos = i;
|
|
|
|
|
isl_val_free(Val);
|
|
|
|
|
}
|
|
|
|
|
isl_local_space_free(LocalSpace);
|
|
|
|
|
return DimPos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check the form of the isl constraint.
|
|
|
|
|
///
|
|
|
|
|
/// Check that the @p DimInPos input dimension of the isl constraint
|
|
|
|
|
/// @p Constraint has a coefficient that is equal to negative one, the @p
|
|
|
|
|
/// DimOutPos has a coefficient that is equal to one and others
|
|
|
|
|
/// have coefficients equal to zero.
|
|
|
|
|
///
|
|
|
|
|
/// @param Constraint The isl constraint to be checked.
|
|
|
|
|
/// @param DimInPos The input dimension of the isl constraint.
|
|
|
|
|
/// @param DimOutPos The output dimension of the isl constraint.
|
|
|
|
|
/// @return isl_stat_ok in case the isl constraint satisfies
|
|
|
|
|
/// the requirements, isl_stat_error otherwise.
|
|
|
|
|
static isl_stat isMatMulOperandConstraint(__isl_keep isl_constraint *Constraint,
|
|
|
|
|
int &DimInPos, int &DimOutPos) {
|
|
|
|
|
auto *Val = isl_constraint_get_constant_val(Constraint);
|
|
|
|
|
if (!isl_constraint_is_equality(Constraint) || !isl_val_is_zero(Val)) {
|
|
|
|
|
isl_val_free(Val);
|
|
|
|
|
return isl_stat_error;
|
|
|
|
|
}
|
|
|
|
|
isl_val_free(Val);
|
|
|
|
|
DimInPos = getMatMulConstraintDim(Constraint, isl_dim_in);
|
|
|
|
|
if (DimInPos < 0)
|
|
|
|
|
return isl_stat_error;
|
|
|
|
|
DimOutPos = getMatMulConstraintDim(Constraint, isl_dim_out);
|
|
|
|
|
if (DimOutPos < 0)
|
|
|
|
|
return isl_stat_error;
|
|
|
|
|
return isl_stat_ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check that the access relation corresponds to a non-constant operand
|
|
|
|
|
/// of the matrix multiplication.
|
|
|
|
|
///
|
|
|
|
|
/// Access relations that correspond to non-constant operands of the matrix
|
|
|
|
|
/// multiplication depend only on two input dimensions and have two output
|
|
|
|
|
/// dimensions. The function checks that the isl basic map @p bmap satisfies
|
|
|
|
|
/// the requirements. The two input dimensions can be specified via @p user
|
|
|
|
|
/// array.
|
|
|
|
|
///
|
|
|
|
|
/// @param bmap The isl basic map to be checked.
|
|
|
|
|
/// @param user The input dimensions of @p bmap.
|
|
|
|
|
/// @return isl_stat_ok in case isl basic map satisfies the requirements,
|
|
|
|
|
/// isl_stat_error otherwise.
|
|
|
|
|
static isl_stat isMatMulOperandBasicMap(__isl_take isl_basic_map *bmap,
|
|
|
|
|
void *user) {
|
|
|
|
|
auto *Constraints = isl_basic_map_get_constraint_list(bmap);
|
|
|
|
|
isl_basic_map_free(bmap);
|
|
|
|
|
if (isl_constraint_list_n_constraint(Constraints) != 2) {
|
|
|
|
|
isl_constraint_list_free(Constraints);
|
|
|
|
|
return isl_stat_error;
|
|
|
|
|
}
|
|
|
|
|
int InPosPair[] = {-1, -1};
|
|
|
|
|
auto DimInPos = user ? static_cast<int *>(user) : InPosPair;
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
|
auto *Constraint = isl_constraint_list_get_constraint(Constraints, i);
|
|
|
|
|
int InPos, OutPos;
|
|
|
|
|
if (isMatMulOperandConstraint(Constraint, InPos, OutPos) ==
|
|
|
|
|
isl_stat_error ||
|
|
|
|
|
OutPos > 1 || (DimInPos[OutPos] >= 0 && DimInPos[OutPos] != InPos)) {
|
|
|
|
|
isl_constraint_free(Constraint);
|
|
|
|
|
isl_constraint_list_free(Constraints);
|
|
|
|
|
return isl_stat_error;
|
|
|
|
|
}
|
|
|
|
|
DimInPos[OutPos] = InPos;
|
|
|
|
|
isl_constraint_free(Constraint);
|
|
|
|
|
}
|
|
|
|
|
isl_constraint_list_free(Constraints);
|
|
|
|
|
return isl_stat_ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Permute the two dimensions of the isl map.
|
|
|
|
|
///
|
|
|
|
|
/// Permute @p DstPos and @p SrcPos dimensions of the isl map @p Map that
|
|
|
|
|
/// have type @p DimType.
|
|
|
|
|
///
|
|
|
|
|
/// @param Map The isl map to be modified.
|
|
|
|
|
/// @param DimType The type of the dimensions.
|
|
|
|
|
/// @param DstPos The first dimension.
|
|
|
|
|
/// @param SrcPos The second dimension.
|
|
|
|
|
/// @return The modified map.
|
|
|
|
|
__isl_give isl_map *permuteDimensions(__isl_take isl_map *Map,
|
|
|
|
|
enum isl_dim_type DimType,
|
|
|
|
|
unsigned DstPos, unsigned SrcPos) {
|
|
|
|
|
assert(DstPos < isl_map_dim(Map, DimType) &&
|
|
|
|
|
SrcPos < isl_map_dim(Map, DimType));
|
|
|
|
|
if (DstPos == SrcPos)
|
|
|
|
|
return Map;
|
|
|
|
|
isl_id *DimId = nullptr;
|
|
|
|
|
if (isl_map_has_tuple_id(Map, DimType))
|
|
|
|
|
DimId = isl_map_get_tuple_id(Map, DimType);
|
|
|
|
|
auto FreeDim = DimType == isl_dim_in ? isl_dim_out : isl_dim_in;
|
|
|
|
|
isl_id *FreeDimId = nullptr;
|
|
|
|
|
if (isl_map_has_tuple_id(Map, FreeDim))
|
|
|
|
|
FreeDimId = isl_map_get_tuple_id(Map, FreeDim);
|
|
|
|
|
auto MaxDim = std::max(DstPos, SrcPos);
|
|
|
|
|
auto MinDim = std::min(DstPos, SrcPos);
|
|
|
|
|
Map = isl_map_move_dims(Map, FreeDim, 0, DimType, MaxDim, 1);
|
|
|
|
|
Map = isl_map_move_dims(Map, FreeDim, 0, DimType, MinDim, 1);
|
|
|
|
|
Map = isl_map_move_dims(Map, DimType, MinDim, FreeDim, 1, 1);
|
|
|
|
|
Map = isl_map_move_dims(Map, DimType, MaxDim, FreeDim, 0, 1);
|
|
|
|
|
if (DimId)
|
|
|
|
|
Map = isl_map_set_tuple_id(Map, DimType, DimId);
|
|
|
|
|
if (FreeDimId)
|
|
|
|
|
Map = isl_map_set_tuple_id(Map, FreeDim, FreeDimId);
|
|
|
|
|
return Map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check the form of the access relation.
|
|
|
|
|
///
|
|
|
|
|
/// Check that the access relation @p AccMap has the form M[i][j], where i
|
|
|
|
|
/// is a @p FirstPos and j is a @p SecondPos.
|
|
|
|
|
///
|
|
|
|
|
/// @param AccMap The access relation to be checked.
|
|
|
|
|
/// @param FirstPos The index of the input dimension that is mapped to
|
|
|
|
|
/// the first output dimension.
|
|
|
|
|
/// @param SecondPos The index of the input dimension that is mapped to the
|
|
|
|
|
/// second output dimension.
|
|
|
|
|
/// @return True in case @p AccMap has the expected form and false,
|
|
|
|
|
/// otherwise.
|
|
|
|
|
static bool isMatMulOperandAcc(__isl_keep isl_map *AccMap, int &FirstPos,
|
|
|
|
|
int &SecondPos) {
|
|
|
|
|
int DimInPos[] = {FirstPos, SecondPos};
|
|
|
|
|
if (isl_map_foreach_basic_map(AccMap, isMatMulOperandBasicMap,
|
|
|
|
|
static_cast<void *>(DimInPos)) != isl_stat_ok ||
|
|
|
|
|
DimInPos[0] < 0 || DimInPos[1] < 0)
|
|
|
|
|
return false;
|
|
|
|
|
FirstPos = DimInPos[0];
|
|
|
|
|
SecondPos = DimInPos[1];
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Does the memory access represent a non-scalar operand of the matrix
|
|
|
|
|
/// multiplication.
|
|
|
|
|
///
|
|
|
|
|
/// Check that the memory access @p MemAccess is the read access to a non-scalar
|
|
|
|
|
/// operand of the matrix multiplication or its result.
|
|
|
|
|
///
|
|
|
|
|
/// @param MemAccess The memory access to be checked.
|
|
|
|
|
/// @param MMI Parameters of the matrix multiplication operands.
|
|
|
|
|
/// @return True in case the memory access represents the read access
|
|
|
|
|
/// to a non-scalar operand of the matrix multiplication and
|
|
|
|
|
/// false, otherwise.
|
|
|
|
|
static bool isMatMulNonScalarReadAccess(MemoryAccess *MemAccess,
|
|
|
|
|
MatMulInfoTy &MMI) {
|
|
|
|
|
if (!MemAccess->isArrayKind() || !MemAccess->isRead())
|
|
|
|
|
return false;
|
|
|
|
|
isl_map *AccMap = MemAccess->getAccessRelation();
|
|
|
|
|
if (isMatMulOperandAcc(AccMap, MMI.i, MMI.j) && !MMI.ReadFromC &&
|
|
|
|
|
isl_map_n_basic_map(AccMap) == 1) {
|
|
|
|
|
MMI.ReadFromC = MemAccess;
|
|
|
|
|
isl_map_free(AccMap);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (isMatMulOperandAcc(AccMap, MMI.i, MMI.k) && !MMI.A &&
|
|
|
|
|
isl_map_n_basic_map(AccMap) == 1) {
|
|
|
|
|
MMI.A = MemAccess;
|
|
|
|
|
isl_map_free(AccMap);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (isMatMulOperandAcc(AccMap, MMI.k, MMI.j) && !MMI.B &&
|
|
|
|
|
isl_map_n_basic_map(AccMap) == 1) {
|
|
|
|
|
MMI.B = MemAccess;
|
|
|
|
|
isl_map_free(AccMap);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
isl_map_free(AccMap);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check accesses to operands of the matrix multiplication.
|
|
|
|
|
///
|
|
|
|
|
/// Check that accesses of the SCoP statement, which corresponds to
|
|
|
|
|
/// the partial schedule @p PartialSchedule, are scalar in terms of loops
|
|
|
|
|
/// containing the matrix multiplication, in case they do not represent
|
|
|
|
|
/// accesses to the non-scalar operands of the matrix multiplication or
|
|
|
|
|
/// its result.
|
|
|
|
|
///
|
|
|
|
|
/// @param PartialSchedule The partial schedule of the SCoP statement.
|
|
|
|
|
/// @param MMI Parameters of the matrix multiplication operands.
|
|
|
|
|
/// @return True in case the corresponding SCoP statement
|
|
|
|
|
/// represents matrix multiplication and false,
|
|
|
|
|
/// otherwise.
|
|
|
|
|
static bool containsOnlyMatrMultAcc(__isl_keep isl_map *PartialSchedule,
|
|
|
|
|
MatMulInfoTy &MMI) {
|
|
|
|
|
auto *InputDimId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in);
|
|
|
|
|
auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimId));
|
|
|
|
|
isl_id_free(InputDimId);
|
|
|
|
|
unsigned OutDimNum = isl_map_dim(PartialSchedule, isl_dim_out);
|
|
|
|
|
assert(OutDimNum > 2 && "In case of the matrix multiplication the loop nest "
|
|
|
|
|
"and, consequently, the corresponding scheduling "
|
|
|
|
|
"functions have at least three dimensions.");
|
|
|
|
|
auto *MapI = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out,
|
|
|
|
|
MMI.i, OutDimNum - 1);
|
|
|
|
|
auto *MapJ = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out,
|
|
|
|
|
MMI.j, OutDimNum - 1);
|
|
|
|
|
auto *MapK = permuteDimensions(isl_map_copy(PartialSchedule), isl_dim_out,
|
|
|
|
|
MMI.k, OutDimNum - 1);
|
|
|
|
|
for (auto *MemA = Stmt->begin(); MemA != Stmt->end() - 1; MemA++) {
|
|
|
|
|
auto *MemAccessPtr = *MemA;
|
|
|
|
|
if (MemAccessPtr->isArrayKind() && MemAccessPtr != MMI.WriteToC &&
|
|
|
|
|
!isMatMulNonScalarReadAccess(MemAccessPtr, MMI) &&
|
|
|
|
|
!(MemAccessPtr->isStrideZero(isl_map_copy(MapI)) &&
|
|
|
|
|
MemAccessPtr->isStrideZero(isl_map_copy(MapJ)) &&
|
|
|
|
|
MemAccessPtr->isStrideZero(isl_map_copy(MapK)))) {
|
|
|
|
|
isl_map_free(MapI);
|
|
|
|
|
isl_map_free(MapJ);
|
|
|
|
|
isl_map_free(MapK);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
isl_map_free(MapI);
|
|
|
|
|
isl_map_free(MapJ);
|
|
|
|
|
isl_map_free(MapK);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check for dependencies corresponding to the matrix multiplication.
|
|
|
|
|
///
|
|
|
|
|
/// Check that there is only true dependence of the form
|
|
|
|
|
/// S(..., k, ...) -> S(..., k + 1, …), where S is the SCoP statement
|
|
|
|
|
/// represented by @p Schedule and k is @p Pos. Such a dependence corresponds
|
|
|
|
|
/// to the dependency produced by the matrix multiplication.
|
|
|
|
|
///
|
|
|
|
|
/// @param Schedule The schedule of the SCoP statement.
|
|
|
|
|
/// @param D The SCoP dependencies.
|
|
|
|
|
/// @param Pos The parameter to desribe an acceptable true dependence.
|
|
|
|
|
/// In case it has a negative value, try to determine its
|
|
|
|
|
/// acceptable value.
|
|
|
|
|
/// @return True in case dependencies correspond to the matrix multiplication
|
|
|
|
|
/// and false, otherwise.
|
|
|
|
|
static bool containsOnlyMatMulDep(__isl_keep isl_map *Schedule,
|
|
|
|
|
const Dependences *D, int &Pos) {
|
2017-02-11 17:59:09 +08:00
|
|
|
|
auto *Dep = D->getDependences(Dependences::TYPE_RAW);
|
|
|
|
|
auto *Red = D->getDependences(Dependences::TYPE_RED);
|
|
|
|
|
if (Red)
|
|
|
|
|
Dep = isl_union_map_union(Dep, Red);
|
2017-02-11 15:14:37 +08:00
|
|
|
|
auto *DomainSpace = isl_space_domain(isl_map_get_space(Schedule));
|
|
|
|
|
auto *Space = isl_space_map_from_domain_and_range(isl_space_copy(DomainSpace),
|
|
|
|
|
DomainSpace);
|
2017-02-11 17:59:09 +08:00
|
|
|
|
auto *Deltas = isl_map_deltas(isl_union_map_extract_map(Dep, Space));
|
|
|
|
|
isl_union_map_free(Dep);
|
2017-02-02 22:23:14 +08:00
|
|
|
|
int DeltasDimNum = isl_set_dim(Deltas, isl_dim_set);
|
|
|
|
|
for (int i = 0; i < DeltasDimNum; i++) {
|
|
|
|
|
auto *Val = isl_set_plain_get_val_if_fixed(Deltas, isl_dim_set, i);
|
2017-02-11 15:14:37 +08:00
|
|
|
|
Pos = Pos < 0 && isl_val_is_one(Val) ? i : Pos;
|
2017-02-02 22:23:14 +08:00
|
|
|
|
if (isl_val_is_nan(Val) ||
|
|
|
|
|
!(isl_val_is_zero(Val) || (i == Pos && isl_val_is_one(Val)))) {
|
|
|
|
|
isl_val_free(Val);
|
2017-02-16 15:04:41 +08:00
|
|
|
|
isl_set_free(Deltas);
|
2017-02-02 22:23:14 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
isl_val_free(Val);
|
|
|
|
|
}
|
2017-02-16 15:04:41 +08:00
|
|
|
|
isl_set_free(Deltas);
|
2017-02-11 17:48:09 +08:00
|
|
|
|
if (DeltasDimNum == 0 || Pos < 0)
|
|
|
|
|
return false;
|
2017-02-02 22:23:14 +08:00
|
|
|
|
return true;
|
2016-05-29 00:17:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Check if the SCoP statement could probably be optimized with analytical
|
|
|
|
|
/// modeling.
|
2016-05-29 00:17:58 +08:00
|
|
|
|
///
|
|
|
|
|
/// containsMatrMult tries to determine whether the following conditions
|
|
|
|
|
/// are true:
|
2017-02-02 22:23:14 +08:00
|
|
|
|
/// 1. The last memory access modeling an array, MA1, represents writing to
|
|
|
|
|
/// memory and has the form S(..., i1, ..., i2, ...) -> M(i1, i2) or
|
|
|
|
|
/// S(..., i2, ..., i1, ...) -> M(i1, i2), where S is the SCoP statement
|
|
|
|
|
/// under consideration.
|
|
|
|
|
/// 2. There is only one loop-carried true dependency, and it has the
|
|
|
|
|
/// form S(..., i3, ...) -> S(..., i3 + 1, ...), and there are no
|
|
|
|
|
/// loop-carried or anti dependencies.
|
|
|
|
|
/// 3. SCoP contains three access relations, MA2, MA3, and MA4 that represent
|
|
|
|
|
/// reading from memory and have the form S(..., i3, ...) -> M(i1, i3),
|
|
|
|
|
/// S(..., i3, ...) -> M(i3, i2), S(...) -> M(i1, i2), respectively,
|
|
|
|
|
/// and all memory accesses of the SCoP that are different from MA1, MA2,
|
|
|
|
|
/// MA3, and MA4 have stride 0, if the innermost loop is exchanged with any
|
|
|
|
|
/// of loops i1, i2 and i3.
|
2016-05-29 00:17:58 +08:00
|
|
|
|
///
|
|
|
|
|
/// @param PartialSchedule The PartialSchedule that contains a SCoP statement
|
|
|
|
|
/// to check.
|
2017-02-02 22:23:14 +08:00
|
|
|
|
/// @D The SCoP dependencies.
|
|
|
|
|
/// @MMI Parameters of the matrix multiplication operands.
|
|
|
|
|
static bool containsMatrMult(__isl_keep isl_map *PartialSchedule,
|
|
|
|
|
const Dependences *D, MatMulInfoTy &MMI) {
|
|
|
|
|
auto *InputDimsId = isl_map_get_tuple_id(PartialSchedule, isl_dim_in);
|
|
|
|
|
auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId));
|
2016-05-29 00:17:58 +08:00
|
|
|
|
isl_id_free(InputDimsId);
|
2017-02-02 22:23:14 +08:00
|
|
|
|
if (Stmt->size() <= 1)
|
2016-05-29 00:17:58 +08:00
|
|
|
|
return false;
|
2017-02-02 22:23:14 +08:00
|
|
|
|
for (auto *MemA = Stmt->end() - 1; MemA != Stmt->begin(); MemA--) {
|
|
|
|
|
auto *MemAccessPtr = *MemA;
|
|
|
|
|
if (!MemAccessPtr->isArrayKind())
|
|
|
|
|
continue;
|
|
|
|
|
if (!MemAccessPtr->isWrite())
|
|
|
|
|
return false;
|
|
|
|
|
auto *AccMap = MemAccessPtr->getAccessRelation();
|
|
|
|
|
if (isl_map_n_basic_map(AccMap) != 1 ||
|
|
|
|
|
!isMatMulOperandAcc(AccMap, MMI.i, MMI.j)) {
|
|
|
|
|
isl_map_free(AccMap);
|
2016-05-29 00:17:58 +08:00
|
|
|
|
return false;
|
2017-02-02 22:23:14 +08:00
|
|
|
|
}
|
|
|
|
|
isl_map_free(AccMap);
|
|
|
|
|
MMI.WriteToC = MemAccessPtr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!containsOnlyMatMulDep(PartialSchedule, D, MMI.k))
|
2016-05-29 00:17:58 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2017-02-02 22:23:14 +08:00
|
|
|
|
if (!MMI.WriteToC || !containsOnlyMatrMultAcc(PartialSchedule, MMI))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!MMI.A || !MMI.B || !MMI.ReadFromC)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
2016-05-29 00:17:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Permute two dimensions of the band node.
|
2016-07-25 17:42:53 +08:00
|
|
|
|
///
|
|
|
|
|
/// Permute FirstDim and SecondDim dimensions of the Node.
|
|
|
|
|
///
|
|
|
|
|
/// @param Node The band node to be modified.
|
|
|
|
|
/// @param FirstDim The first dimension to be permuted.
|
|
|
|
|
/// @param SecondDim The second dimension to be permuted.
|
|
|
|
|
static __isl_give isl_schedule_node *
|
|
|
|
|
permuteBandNodeDimensions(__isl_take isl_schedule_node *Node, unsigned FirstDim,
|
|
|
|
|
unsigned SecondDim) {
|
|
|
|
|
assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band &&
|
|
|
|
|
isl_schedule_node_band_n_member(Node) > std::max(FirstDim, SecondDim));
|
|
|
|
|
auto PartialSchedule = isl_schedule_node_band_get_partial_schedule(Node);
|
|
|
|
|
auto PartialScheduleFirstDim =
|
|
|
|
|
isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, FirstDim);
|
|
|
|
|
auto PartialScheduleSecondDim =
|
|
|
|
|
isl_multi_union_pw_aff_get_union_pw_aff(PartialSchedule, SecondDim);
|
|
|
|
|
PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff(
|
|
|
|
|
PartialSchedule, SecondDim, PartialScheduleFirstDim);
|
|
|
|
|
PartialSchedule = isl_multi_union_pw_aff_set_union_pw_aff(
|
|
|
|
|
PartialSchedule, FirstDim, PartialScheduleSecondDim);
|
|
|
|
|
Node = isl_schedule_node_delete(Node);
|
|
|
|
|
Node = isl_schedule_node_insert_partial_schedule(Node, PartialSchedule);
|
|
|
|
|
return Node;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 15:27:59 +08:00
|
|
|
|
__isl_give isl_schedule_node *ScheduleTreeOptimizer::createMicroKernel(
|
|
|
|
|
__isl_take isl_schedule_node *Node, MicroKernelParamsTy MicroKernelParams) {
|
The order of the loops defines the data reused in the BLIS implementation of
gemm ([1]). In particular, elements of the matrix B, the second operand of
matrix multiplication, are reused between iterations of the innermost loop.
To keep the reused data in cache, only elements of matrix A, the first operand
of matrix multiplication, should be evicted during an iteration of the
innermost loop. To provide such a cache replacement policy, elements of the
matrix A can, in particular, be loaded first and, consequently, be
least-recently-used.
In our case matrices are stored in row-major order instead of column-major
order used in the BLIS implementation ([1]). One of the ways to address it is
to accordingly change the order of the loops of the loop nest. However, it
makes elements of the matrix A to be reused in the innermost loop and,
consequently, requires to load elements of the matrix B first. Since the LLVM
vectorizer always generates loads from the matrix A before loads from the
matrix B and we can not provide it. Consequently, we only change the BLIS micro
kernel and the computation of its parameters instead. In particular, reused
elements of the matrix B are successively multiplied by specific elements of
the matrix A .
Refs.:
[1] - http://www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf
Reviewed-by: Tobias Grosser <tobias@grosser.es>
Differential Revision: https://reviews.llvm.org/D25653
llvm-svn: 289806
2016-12-15 19:47:38 +08:00
|
|
|
|
applyRegisterTiling(Node, {MicroKernelParams.Mr, MicroKernelParams.Nr}, 1);
|
|
|
|
|
Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
|
|
|
|
|
Node = permuteBandNodeDimensions(Node, 0, 1);
|
|
|
|
|
return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
|
2016-07-25 15:27:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 17:42:53 +08:00
|
|
|
|
__isl_give isl_schedule_node *ScheduleTreeOptimizer::createMacroKernel(
|
|
|
|
|
__isl_take isl_schedule_node *Node, MacroKernelParamsTy MacroKernelParams) {
|
|
|
|
|
assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band);
|
|
|
|
|
if (MacroKernelParams.Mc == 1 && MacroKernelParams.Nc == 1 &&
|
|
|
|
|
MacroKernelParams.Kc == 1)
|
|
|
|
|
return Node;
|
2017-02-02 22:23:14 +08:00
|
|
|
|
int DimOutNum = isl_schedule_node_band_n_member(Node);
|
|
|
|
|
std::vector<int> TileSizes(DimOutNum, 1);
|
|
|
|
|
TileSizes[DimOutNum - 3] = MacroKernelParams.Mc;
|
|
|
|
|
TileSizes[DimOutNum - 2] = MacroKernelParams.Nc;
|
|
|
|
|
TileSizes[DimOutNum - 1] = MacroKernelParams.Kc;
|
|
|
|
|
Node = tileNode(Node, "1st level tiling", TileSizes, 1);
|
2016-07-25 17:42:53 +08:00
|
|
|
|
Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
|
2017-02-02 22:23:14 +08:00
|
|
|
|
Node = permuteBandNodeDimensions(Node, DimOutNum - 2, DimOutNum - 1);
|
|
|
|
|
Node = permuteBandNodeDimensions(Node, DimOutNum - 3, DimOutNum - 1);
|
2016-07-25 17:42:53 +08:00
|
|
|
|
return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-11 15:00:05 +08:00
|
|
|
|
/// Get the size of the widest type of the matrix multiplication operands
|
|
|
|
|
/// in bytes, including alignment padding.
|
|
|
|
|
///
|
|
|
|
|
/// @param MMI Parameters of the matrix multiplication operands.
|
|
|
|
|
/// @return The size of the widest type of the matrix multiplication operands
|
|
|
|
|
/// in bytes, including alignment padding.
|
|
|
|
|
static uint64_t getMatMulAlignTypeSize(MatMulInfoTy MMI) {
|
|
|
|
|
auto *S = MMI.A->getStatement()->getParent();
|
|
|
|
|
auto &DL = S->getFunction().getParent()->getDataLayout();
|
|
|
|
|
auto ElementSizeA = DL.getTypeAllocSize(MMI.A->getElementType());
|
|
|
|
|
auto ElementSizeB = DL.getTypeAllocSize(MMI.B->getElementType());
|
|
|
|
|
auto ElementSizeC = DL.getTypeAllocSize(MMI.WriteToC->getElementType());
|
|
|
|
|
return std::max({ElementSizeA, ElementSizeB, ElementSizeC});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the size of the widest type of the matrix multiplication operands
|
|
|
|
|
/// in bits.
|
|
|
|
|
///
|
|
|
|
|
/// @param MMI Parameters of the matrix multiplication operands.
|
|
|
|
|
/// @return The size of the widest type of the matrix multiplication operands
|
|
|
|
|
/// in bits.
|
|
|
|
|
static uint64_t getMatMulTypeSize(MatMulInfoTy MMI) {
|
|
|
|
|
auto *S = MMI.A->getStatement()->getParent();
|
|
|
|
|
auto &DL = S->getFunction().getParent()->getDataLayout();
|
|
|
|
|
auto ElementSizeA = DL.getTypeSizeInBits(MMI.A->getElementType());
|
|
|
|
|
auto ElementSizeB = DL.getTypeSizeInBits(MMI.B->getElementType());
|
|
|
|
|
auto ElementSizeC = DL.getTypeSizeInBits(MMI.WriteToC->getElementType());
|
|
|
|
|
return std::max({ElementSizeA, ElementSizeB, ElementSizeC});
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 15:27:59 +08:00
|
|
|
|
/// Get parameters of the BLIS micro kernel.
|
|
|
|
|
///
|
|
|
|
|
/// We choose the Mr and Nr parameters of the micro kernel to be large enough
|
|
|
|
|
/// such that no stalls caused by the combination of latencies and dependencies
|
|
|
|
|
/// are introduced during the updates of the resulting matrix of the matrix
|
|
|
|
|
/// multiplication. However, they should also be as small as possible to
|
|
|
|
|
/// release more registers for entries of multiplied matrices.
|
|
|
|
|
///
|
|
|
|
|
/// @param TTI Target Transform Info.
|
2017-02-11 15:00:05 +08:00
|
|
|
|
/// @param MMI Parameters of the matrix multiplication operands.
|
2016-07-25 15:27:59 +08:00
|
|
|
|
/// @return The structure of type MicroKernelParamsTy.
|
|
|
|
|
/// @see MicroKernelParamsTy
|
|
|
|
|
static struct MicroKernelParamsTy
|
2017-02-11 15:00:05 +08:00
|
|
|
|
getMicroKernelParams(const llvm::TargetTransformInfo *TTI, MatMulInfoTy MMI) {
|
2016-06-22 17:52:37 +08:00
|
|
|
|
assert(TTI && "The target transform info should be provided.");
|
2016-07-25 15:27:59 +08:00
|
|
|
|
|
2016-06-22 17:52:37 +08:00
|
|
|
|
// Nvec - Number of double-precision floating-point numbers that can be hold
|
|
|
|
|
// by a vector register. Use 2 by default.
|
2017-01-14 15:14:54 +08:00
|
|
|
|
long RegisterBitwidth = VectorRegisterBitwidth;
|
|
|
|
|
|
|
|
|
|
if (RegisterBitwidth == -1)
|
|
|
|
|
RegisterBitwidth = TTI->getRegisterBitWidth(true);
|
2017-02-11 15:00:05 +08:00
|
|
|
|
auto ElementSize = getMatMulTypeSize(MMI);
|
|
|
|
|
assert(ElementSize > 0 && "The element size of the matrix multiplication "
|
|
|
|
|
"operands should be greater than zero.");
|
|
|
|
|
auto Nvec = RegisterBitwidth / ElementSize;
|
2016-06-22 17:52:37 +08:00
|
|
|
|
if (Nvec == 0)
|
|
|
|
|
Nvec = 2;
|
|
|
|
|
int Nr =
|
2016-12-23 15:33:39 +08:00
|
|
|
|
ceil(sqrt(Nvec * LatencyVectorFma * ThroughputVectorFma) / Nvec) * Nvec;
|
|
|
|
|
int Mr = ceil(Nvec * LatencyVectorFma * ThroughputVectorFma / Nr);
|
2016-07-25 15:27:59 +08:00
|
|
|
|
return {Mr, Nr};
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 17:42:53 +08:00
|
|
|
|
/// Get parameters of the BLIS macro kernel.
|
|
|
|
|
///
|
|
|
|
|
/// During the computation of matrix multiplication, blocks of partitioned
|
|
|
|
|
/// matrices are mapped to different layers of the memory hierarchy.
|
|
|
|
|
/// To optimize data reuse, blocks should be ideally kept in cache between
|
|
|
|
|
/// iterations. Since parameters of the macro kernel determine sizes of these
|
|
|
|
|
/// blocks, there are upper and lower bounds on these parameters.
|
|
|
|
|
///
|
|
|
|
|
/// @param MicroKernelParams Parameters of the micro-kernel
|
|
|
|
|
/// to be taken into account.
|
2017-02-11 15:00:05 +08:00
|
|
|
|
/// @param MMI Parameters of the matrix multiplication operands.
|
2016-07-25 17:42:53 +08:00
|
|
|
|
/// @return The structure of type MacroKernelParamsTy.
|
|
|
|
|
/// @see MacroKernelParamsTy
|
|
|
|
|
/// @see MicroKernelParamsTy
|
|
|
|
|
static struct MacroKernelParamsTy
|
2017-02-11 15:00:05 +08:00
|
|
|
|
getMacroKernelParams(const MicroKernelParamsTy &MicroKernelParams,
|
|
|
|
|
MatMulInfoTy MMI) {
|
2016-07-25 17:42:53 +08:00
|
|
|
|
// According to www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf,
|
|
|
|
|
// it requires information about the first two levels of a cache to determine
|
|
|
|
|
// all the parameters of a macro-kernel. It also checks that an associativity
|
|
|
|
|
// degree of a cache level is greater than two. Otherwise, another algorithm
|
|
|
|
|
// for determination of the parameters should be used.
|
|
|
|
|
if (!(MicroKernelParams.Mr > 0 && MicroKernelParams.Nr > 0 &&
|
2016-12-26 00:32:28 +08:00
|
|
|
|
FirstCacheLevelSize > 0 && SecondCacheLevelSize > 0 &&
|
|
|
|
|
FirstCacheLevelAssociativity > 2 && SecondCacheLevelAssociativity > 2))
|
2016-07-25 17:42:53 +08:00
|
|
|
|
return {1, 1, 1};
|
Change the determination of parameters of macro-kernel
Typically processor architectures do not include an L3 cache, which means that
Nc, the parameter of the micro-kernel, is, for all practical purposes,
redundant ([1]). However, its small values can cause the redundant packing of
the same elements of the matrix A, the first operand of the matrix
multiplication. At the same time, big values of the parameter Nc can cause
segmentation faults in case the available stack is exceeded.
This patch adds an option to specify the parameter Nc as a multiple of
the parameter of the micro-kernel Nr.
In case of Intel Core i7-3820 SandyBridge and the following options,
clang -O3 gemm.c -I utilities/ utilities/polybench.c -DPOLYBENCH_TIME
-march=native -mllvm -polly -mllvm -polly-pattern-matching-based-opts=true
-DPOLYBENCH_USE_SCALAR_LB -mllvm -polly-target-cache-level-associativity=8,8
-mllvm -polly-target-cache-level-sizes=32768,262144 -mllvm
-polly-target-latency-vector-fma=8
it helps to improve the performance from 11.303 GFlops/sec (39,247% of
theoretical peak) to 17.896 GFlops/sec (62,14% of theoretical peak).
Refs.:
[1] - http://www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf
Reviewed-by: Tobias Grosser <tobias@grosser.es>
Differential Revision: https://reviews.llvm.org/D28019
llvm-svn: 290256
2016-12-21 20:51:12 +08:00
|
|
|
|
// The quotient should be greater than zero.
|
|
|
|
|
if (PollyPatternMatchingNcQuotient <= 0)
|
|
|
|
|
return {1, 1, 1};
|
2016-12-15 20:00:57 +08:00
|
|
|
|
int Car = floor(
|
2016-12-26 00:32:28 +08:00
|
|
|
|
(FirstCacheLevelAssociativity - 1) /
|
The order of the loops defines the data reused in the BLIS implementation of
gemm ([1]). In particular, elements of the matrix B, the second operand of
matrix multiplication, are reused between iterations of the innermost loop.
To keep the reused data in cache, only elements of matrix A, the first operand
of matrix multiplication, should be evicted during an iteration of the
innermost loop. To provide such a cache replacement policy, elements of the
matrix A can, in particular, be loaded first and, consequently, be
least-recently-used.
In our case matrices are stored in row-major order instead of column-major
order used in the BLIS implementation ([1]). One of the ways to address it is
to accordingly change the order of the loops of the loop nest. However, it
makes elements of the matrix A to be reused in the innermost loop and,
consequently, requires to load elements of the matrix B first. Since the LLVM
vectorizer always generates loads from the matrix A before loads from the
matrix B and we can not provide it. Consequently, we only change the BLIS micro
kernel and the computation of its parameters instead. In particular, reused
elements of the matrix B are successively multiplied by specific elements of
the matrix A .
Refs.:
[1] - http://www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf
Reviewed-by: Tobias Grosser <tobias@grosser.es>
Differential Revision: https://reviews.llvm.org/D25653
llvm-svn: 289806
2016-12-15 19:47:38 +08:00
|
|
|
|
(1 + static_cast<double>(MicroKernelParams.Nr) / MicroKernelParams.Mr));
|
2017-04-06 16:20:22 +08:00
|
|
|
|
|
|
|
|
|
// Car can be computed to be zero since it is floor to int.
|
|
|
|
|
// On Mac OS, division by 0 does not raise a signal. This causes negative
|
|
|
|
|
// tile sizes to be computed. Prevent division by 0 Cac by early returning
|
|
|
|
|
// if this happens.
|
|
|
|
|
if (Car == 0)
|
|
|
|
|
return {1, 1, 1};
|
|
|
|
|
|
2017-02-11 15:00:05 +08:00
|
|
|
|
auto ElementSize = getMatMulAlignTypeSize(MMI);
|
|
|
|
|
assert(ElementSize > 0 && "The element size of the matrix multiplication "
|
|
|
|
|
"operands should be greater than zero.");
|
2016-12-26 00:32:28 +08:00
|
|
|
|
int Kc = (Car * FirstCacheLevelSize) /
|
2017-02-11 15:00:05 +08:00
|
|
|
|
(MicroKernelParams.Mr * FirstCacheLevelAssociativity * ElementSize);
|
|
|
|
|
double Cac =
|
|
|
|
|
static_cast<double>(Kc * ElementSize * SecondCacheLevelAssociativity) /
|
|
|
|
|
SecondCacheLevelSize;
|
2016-12-26 00:32:28 +08:00
|
|
|
|
int Mc = floor((SecondCacheLevelAssociativity - 2) / Cac);
|
Change the determination of parameters of macro-kernel
Typically processor architectures do not include an L3 cache, which means that
Nc, the parameter of the micro-kernel, is, for all practical purposes,
redundant ([1]). However, its small values can cause the redundant packing of
the same elements of the matrix A, the first operand of the matrix
multiplication. At the same time, big values of the parameter Nc can cause
segmentation faults in case the available stack is exceeded.
This patch adds an option to specify the parameter Nc as a multiple of
the parameter of the micro-kernel Nr.
In case of Intel Core i7-3820 SandyBridge and the following options,
clang -O3 gemm.c -I utilities/ utilities/polybench.c -DPOLYBENCH_TIME
-march=native -mllvm -polly -mllvm -polly-pattern-matching-based-opts=true
-DPOLYBENCH_USE_SCALAR_LB -mllvm -polly-target-cache-level-associativity=8,8
-mllvm -polly-target-cache-level-sizes=32768,262144 -mllvm
-polly-target-latency-vector-fma=8
it helps to improve the performance from 11.303 GFlops/sec (39,247% of
theoretical peak) to 17.896 GFlops/sec (62,14% of theoretical peak).
Refs.:
[1] - http://www.cs.utexas.edu/users/flame/pubs/TOMS-BLIS-Analytical.pdf
Reviewed-by: Tobias Grosser <tobias@grosser.es>
Differential Revision: https://reviews.llvm.org/D28019
llvm-svn: 290256
2016-12-21 20:51:12 +08:00
|
|
|
|
int Nc = PollyPatternMatchingNcQuotient * MicroKernelParams.Nr;
|
2017-04-06 16:20:22 +08:00
|
|
|
|
|
|
|
|
|
assert(Mc > 0 && Nc > 0 && Kc > 0 &&
|
|
|
|
|
"Matrix block sizes should be greater than zero");
|
2016-07-25 17:42:53 +08:00
|
|
|
|
return {Mc, Nc, Kc};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Create an access relation that is specific to
|
2016-08-15 20:22:54 +08:00
|
|
|
|
/// the matrix multiplication pattern.
|
|
|
|
|
///
|
|
|
|
|
/// Create an access relation of the following form:
|
2016-12-21 19:18:42 +08:00
|
|
|
|
/// [O0, O1, O2, O3, O4, O5, O6, O7, O8] -> [OI, O5, OJ]
|
|
|
|
|
/// where I is @p FirstDim, J is @p SecondDim.
|
2016-08-15 20:22:54 +08:00
|
|
|
|
///
|
|
|
|
|
/// It can be used, for example, to create relations that helps to consequently
|
|
|
|
|
/// access elements of operands of a matrix multiplication after creation of
|
|
|
|
|
/// the BLIS micro and macro kernels.
|
|
|
|
|
///
|
|
|
|
|
/// @see ScheduleTreeOptimizer::createMicroKernel
|
|
|
|
|
/// @see ScheduleTreeOptimizer::createMacroKernel
|
|
|
|
|
///
|
|
|
|
|
/// Subsequently, the described access relation is applied to the range of
|
|
|
|
|
/// @p MapOldIndVar, that is used to map original induction variables to
|
|
|
|
|
/// the ones, which are produced by schedule transformations. It helps to
|
|
|
|
|
/// define relations using a new space and, at the same time, keep them
|
|
|
|
|
/// in the original one.
|
|
|
|
|
///
|
|
|
|
|
/// @param MapOldIndVar The relation, which maps original induction variables
|
|
|
|
|
/// to the ones, which are produced by schedule
|
|
|
|
|
/// transformations.
|
|
|
|
|
/// @param FirstDim, SecondDim The input dimensions that are used to define
|
|
|
|
|
/// the specified access relation.
|
|
|
|
|
/// @return The specified access relation.
|
|
|
|
|
__isl_give isl_map *getMatMulAccRel(__isl_take isl_map *MapOldIndVar,
|
2016-12-21 19:18:42 +08:00
|
|
|
|
unsigned FirstDim, unsigned SecondDim) {
|
2016-08-15 20:22:54 +08:00
|
|
|
|
auto *Ctx = isl_map_get_ctx(MapOldIndVar);
|
2016-12-21 19:18:42 +08:00
|
|
|
|
auto *AccessRelSpace = isl_space_alloc(Ctx, 0, 9, 3);
|
|
|
|
|
auto *AccessRel = isl_map_universe(AccessRelSpace);
|
|
|
|
|
AccessRel = isl_map_equate(AccessRel, isl_dim_in, FirstDim, isl_dim_out, 0);
|
|
|
|
|
AccessRel = isl_map_equate(AccessRel, isl_dim_in, 5, isl_dim_out, 1);
|
|
|
|
|
AccessRel = isl_map_equate(AccessRel, isl_dim_in, SecondDim, isl_dim_out, 2);
|
2016-08-15 20:22:54 +08:00
|
|
|
|
return isl_map_apply_range(MapOldIndVar, AccessRel);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-14 14:26:09 +08:00
|
|
|
|
__isl_give isl_schedule_node *
|
|
|
|
|
createExtensionNode(__isl_take isl_schedule_node *Node,
|
|
|
|
|
__isl_take isl_map *ExtensionMap) {
|
|
|
|
|
auto *Extension = isl_union_map_from_map(ExtensionMap);
|
|
|
|
|
auto *NewNode = isl_schedule_node_from_extension(Extension);
|
|
|
|
|
return isl_schedule_node_graft_before(Node, NewNode);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Apply the packing transformation.
|
2016-08-15 20:22:54 +08:00
|
|
|
|
///
|
|
|
|
|
/// The packing transformation can be described as a data-layout
|
|
|
|
|
/// transformation that requires to introduce a new array, copy data
|
2017-01-29 18:37:50 +08:00
|
|
|
|
/// to the array, and change memory access locations to reference the array.
|
|
|
|
|
/// It can be used to ensure that elements of the new array are read in-stride
|
|
|
|
|
/// access, aligned to cache lines boundaries, and preloaded into certain cache
|
|
|
|
|
/// levels.
|
|
|
|
|
///
|
|
|
|
|
/// As an example let us consider the packing of the array A that would help
|
|
|
|
|
/// to read its elements with in-stride access. An access to the array A
|
|
|
|
|
/// is represented by an access relation that has the form
|
|
|
|
|
/// S[i, j, k] -> A[i, k]. The scheduling function of the SCoP statement S has
|
|
|
|
|
/// the form S[i,j, k] -> [floor((j mod Nc) / Nr), floor((i mod Mc) / Mr),
|
|
|
|
|
/// k mod Kc, j mod Nr, i mod Mr].
|
|
|
|
|
///
|
|
|
|
|
/// To ensure that elements of the array A are read in-stride access, we add
|
|
|
|
|
/// a new array Packed_A[Mc/Mr][Kc][Mr] to the SCoP, using
|
|
|
|
|
/// Scop::createScopArrayInfo, change the access relation
|
|
|
|
|
/// S[i, j, k] -> A[i, k] to
|
|
|
|
|
/// S[i, j, k] -> Packed_A[floor((i mod Mc) / Mr), k mod Kc, i mod Mr], using
|
|
|
|
|
/// MemoryAccess::setNewAccessRelation, and copy the data to the array, using
|
|
|
|
|
/// the copy statement created by Scop::addScopStmt.
|
2016-08-15 20:22:54 +08:00
|
|
|
|
///
|
|
|
|
|
/// @param Node The schedule node to be optimized.
|
|
|
|
|
/// @param MapOldIndVar The relation, which maps original induction variables
|
|
|
|
|
/// to the ones, which are produced by schedule
|
|
|
|
|
/// transformations.
|
|
|
|
|
/// @param MicroParams, MacroParams Parameters of the BLIS kernel
|
|
|
|
|
/// to be taken into account.
|
2017-02-02 22:23:14 +08:00
|
|
|
|
/// @param MMI Parameters of the matrix multiplication operands.
|
2016-08-15 20:22:54 +08:00
|
|
|
|
/// @return The optimized schedule node.
|
2016-09-14 14:26:09 +08:00
|
|
|
|
static __isl_give isl_schedule_node *optimizeDataLayoutMatrMulPattern(
|
|
|
|
|
__isl_take isl_schedule_node *Node, __isl_take isl_map *MapOldIndVar,
|
2017-02-02 22:23:14 +08:00
|
|
|
|
MicroKernelParamsTy MicroParams, MacroKernelParamsTy MacroParams,
|
|
|
|
|
MatMulInfoTy &MMI) {
|
2016-08-15 20:22:54 +08:00
|
|
|
|
auto InputDimsId = isl_map_get_tuple_id(MapOldIndVar, isl_dim_in);
|
|
|
|
|
auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(InputDimsId));
|
|
|
|
|
isl_id_free(InputDimsId);
|
2016-12-15 20:35:59 +08:00
|
|
|
|
|
|
|
|
|
// Create a copy statement that corresponds to the memory access to the
|
|
|
|
|
// matrix B, the second operand of the matrix multiplication.
|
2016-09-14 14:26:09 +08:00
|
|
|
|
Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
|
|
|
|
|
Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
|
|
|
|
|
Node = isl_schedule_node_parent(Node);
|
|
|
|
|
Node = isl_schedule_node_child(isl_schedule_node_band_split(Node, 2), 0);
|
2016-12-21 19:18:42 +08:00
|
|
|
|
auto *AccRel = getMatMulAccRel(isl_map_copy(MapOldIndVar), 3, 7);
|
|
|
|
|
unsigned FirstDimSize = MacroParams.Nc / MicroParams.Nr;
|
|
|
|
|
unsigned SecondDimSize = MacroParams.Kc;
|
|
|
|
|
unsigned ThirdDimSize = MicroParams.Nr;
|
2016-08-15 20:22:54 +08:00
|
|
|
|
auto *SAI = Stmt->getParent()->createScopArrayInfo(
|
2017-02-02 22:23:14 +08:00
|
|
|
|
MMI.B->getElementType(), "Packed_B",
|
2016-12-21 19:18:42 +08:00
|
|
|
|
{FirstDimSize, SecondDimSize, ThirdDimSize});
|
2016-08-15 20:22:54 +08:00
|
|
|
|
AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId());
|
2017-02-02 22:23:14 +08:00
|
|
|
|
auto *OldAcc = MMI.B->getAccessRelation();
|
|
|
|
|
MMI.B->setNewAccessRelation(AccRel);
|
2016-09-14 14:26:09 +08:00
|
|
|
|
auto *ExtMap =
|
2017-02-02 22:23:14 +08:00
|
|
|
|
isl_map_project_out(isl_map_copy(MapOldIndVar), isl_dim_out, 2,
|
|
|
|
|
isl_map_dim(MapOldIndVar, isl_dim_out) - 2);
|
|
|
|
|
ExtMap = isl_map_reverse(ExtMap);
|
|
|
|
|
ExtMap = isl_map_fix_si(ExtMap, isl_dim_out, MMI.i, 0);
|
2016-09-14 14:26:09 +08:00
|
|
|
|
auto *Domain = Stmt->getDomain();
|
2016-12-15 20:35:59 +08:00
|
|
|
|
|
|
|
|
|
// Restrict the domains of the copy statements to only execute when also its
|
|
|
|
|
// originating statement is executed.
|
|
|
|
|
auto *DomainId = isl_set_get_tuple_id(Domain);
|
2016-09-14 14:26:09 +08:00
|
|
|
|
auto *NewStmt = Stmt->getParent()->addScopStmt(
|
2017-02-02 22:23:14 +08:00
|
|
|
|
OldAcc, MMI.B->getAccessRelation(), isl_set_copy(Domain));
|
2016-12-15 20:35:59 +08:00
|
|
|
|
ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, isl_id_copy(DomainId));
|
|
|
|
|
ExtMap = isl_map_intersect_range(ExtMap, isl_set_copy(Domain));
|
2016-09-14 14:26:09 +08:00
|
|
|
|
ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId());
|
|
|
|
|
Node = createExtensionNode(Node, ExtMap);
|
2016-12-15 20:35:59 +08:00
|
|
|
|
|
|
|
|
|
// Create a copy statement that corresponds to the memory access
|
|
|
|
|
// to the matrix A, the first operand of the matrix multiplication.
|
2016-09-14 14:26:09 +08:00
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
2017-02-02 22:23:14 +08:00
|
|
|
|
AccRel = getMatMulAccRel(isl_map_copy(MapOldIndVar), 4, 6);
|
2016-12-21 19:18:42 +08:00
|
|
|
|
FirstDimSize = MacroParams.Mc / MicroParams.Mr;
|
|
|
|
|
ThirdDimSize = MicroParams.Mr;
|
2016-08-15 20:22:54 +08:00
|
|
|
|
SAI = Stmt->getParent()->createScopArrayInfo(
|
2017-02-02 22:23:14 +08:00
|
|
|
|
MMI.A->getElementType(), "Packed_A",
|
2016-12-21 19:18:42 +08:00
|
|
|
|
{FirstDimSize, SecondDimSize, ThirdDimSize});
|
2016-08-15 20:22:54 +08:00
|
|
|
|
AccRel = isl_map_set_tuple_id(AccRel, isl_dim_out, SAI->getBasePtrId());
|
2017-02-02 22:23:14 +08:00
|
|
|
|
OldAcc = MMI.A->getAccessRelation();
|
|
|
|
|
MMI.A->setNewAccessRelation(AccRel);
|
|
|
|
|
ExtMap = isl_map_project_out(MapOldIndVar, isl_dim_out, 3,
|
|
|
|
|
isl_map_dim(MapOldIndVar, isl_dim_out) - 3);
|
|
|
|
|
ExtMap = isl_map_reverse(ExtMap);
|
|
|
|
|
ExtMap = isl_map_fix_si(ExtMap, isl_dim_out, MMI.j, 0);
|
|
|
|
|
NewStmt = Stmt->getParent()->addScopStmt(OldAcc, MMI.A->getAccessRelation(),
|
|
|
|
|
isl_set_copy(Domain));
|
2016-12-15 20:35:59 +08:00
|
|
|
|
|
|
|
|
|
// Restrict the domains of the copy statements to only execute when also its
|
|
|
|
|
// originating statement is executed.
|
|
|
|
|
ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, DomainId);
|
|
|
|
|
ExtMap = isl_map_intersect_range(ExtMap, Domain);
|
2016-09-14 14:26:09 +08:00
|
|
|
|
ExtMap = isl_map_set_tuple_id(ExtMap, isl_dim_out, NewStmt->getDomainId());
|
|
|
|
|
Node = createExtensionNode(Node, ExtMap);
|
|
|
|
|
Node = isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
|
|
|
|
|
return isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
|
2016-08-15 20:22:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Get a relation mapping induction variables produced by schedule
|
|
|
|
|
/// transformations to the original ones.
|
2016-08-15 20:22:54 +08:00
|
|
|
|
///
|
|
|
|
|
/// @param Node The schedule node produced as the result of creation
|
|
|
|
|
/// of the BLIS kernels.
|
|
|
|
|
/// @param MicroKernelParams, MacroKernelParams Parameters of the BLIS kernel
|
|
|
|
|
/// to be taken into account.
|
|
|
|
|
/// @return The relation mapping original induction variables to the ones
|
|
|
|
|
/// produced by schedule transformation.
|
|
|
|
|
/// @see ScheduleTreeOptimizer::createMicroKernel
|
|
|
|
|
/// @see ScheduleTreeOptimizer::createMacroKernel
|
|
|
|
|
/// @see getMacroKernelParams
|
|
|
|
|
__isl_give isl_map *
|
|
|
|
|
getInductionVariablesSubstitution(__isl_take isl_schedule_node *Node,
|
|
|
|
|
MicroKernelParamsTy MicroKernelParams,
|
|
|
|
|
MacroKernelParamsTy MacroKernelParams) {
|
|
|
|
|
auto *Child = isl_schedule_node_get_child(Node, 0);
|
|
|
|
|
auto *UnMapOldIndVar = isl_schedule_node_get_prefix_schedule_union_map(Child);
|
|
|
|
|
isl_schedule_node_free(Child);
|
|
|
|
|
auto *MapOldIndVar = isl_map_from_union_map(UnMapOldIndVar);
|
|
|
|
|
if (isl_map_dim(MapOldIndVar, isl_dim_out) > 9)
|
|
|
|
|
MapOldIndVar =
|
|
|
|
|
isl_map_project_out(MapOldIndVar, isl_dim_out, 0,
|
|
|
|
|
isl_map_dim(MapOldIndVar, isl_dim_out) - 9);
|
|
|
|
|
return MapOldIndVar;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-09 15:10:01 +08:00
|
|
|
|
/// Isolate a set of partial tile prefixes and unroll the isolated part.
|
|
|
|
|
///
|
|
|
|
|
/// The set should ensure that it contains only partial tile prefixes that have
|
|
|
|
|
/// exactly Mr x Nr iterations of the two innermost loops produced by
|
|
|
|
|
/// the optimization of the matrix multiplication. Mr and Nr are parameters of
|
|
|
|
|
/// the micro-kernel.
|
|
|
|
|
///
|
|
|
|
|
/// In case of parametric bounds, this helps to auto-vectorize the unrolled
|
|
|
|
|
/// innermost loops, using the SLP vectorizer.
|
|
|
|
|
///
|
|
|
|
|
/// @param Node The schedule node to be modified.
|
|
|
|
|
/// @param MicroKernelParams Parameters of the micro-kernel
|
|
|
|
|
/// to be taken into account.
|
|
|
|
|
/// @return The modified isl_schedule_node.
|
|
|
|
|
static __isl_give isl_schedule_node *
|
|
|
|
|
isolateAndUnrollMatMulInnerLoops(__isl_take isl_schedule_node *Node,
|
|
|
|
|
struct MicroKernelParamsTy MicroKernelParams) {
|
|
|
|
|
auto *Child = isl_schedule_node_get_child(Node, 0);
|
|
|
|
|
auto *UnMapOldIndVar = isl_schedule_node_get_prefix_schedule_relation(Child);
|
|
|
|
|
isl_schedule_node_free(Child);
|
|
|
|
|
auto *Prefix = isl_map_range(isl_map_from_union_map(UnMapOldIndVar));
|
|
|
|
|
auto Dims = isl_set_dim(Prefix, isl_dim_set);
|
|
|
|
|
Prefix = isl_set_project_out(Prefix, isl_dim_set, Dims - 1, 1);
|
|
|
|
|
Prefix = getPartialTilePrefixes(Prefix, MicroKernelParams.Nr);
|
|
|
|
|
Prefix = getPartialTilePrefixes(Prefix, MicroKernelParams.Mr);
|
|
|
|
|
auto *IsolateOption = getIsolateOptions(
|
|
|
|
|
isl_set_add_dims(isl_set_copy(Prefix), isl_dim_set, 3), 3);
|
|
|
|
|
auto *Ctx = isl_schedule_node_get_ctx(Node);
|
|
|
|
|
auto *AtomicOption = getAtomicOptions(Ctx);
|
|
|
|
|
auto *Options =
|
|
|
|
|
isl_union_set_union(IsolateOption, isl_union_set_copy(AtomicOption));
|
|
|
|
|
Options = isl_union_set_union(Options, getUnrollIsolatedSetOptions(Ctx));
|
|
|
|
|
Node = isl_schedule_node_band_set_ast_build_options(Node, Options);
|
|
|
|
|
Node = isl_schedule_node_parent(isl_schedule_node_parent(Node));
|
|
|
|
|
IsolateOption = getIsolateOptions(Prefix, 3);
|
|
|
|
|
Options = isl_union_set_union(IsolateOption, AtomicOption);
|
|
|
|
|
Node = isl_schedule_node_band_set_ast_build_options(Node, Options);
|
|
|
|
|
Node = isl_schedule_node_child(isl_schedule_node_child(Node, 0), 0);
|
|
|
|
|
return Node;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 22:25:24 +08:00
|
|
|
|
/// Mark @p BasePtr with "Inter iteration alias-free" mark node.
|
|
|
|
|
///
|
|
|
|
|
/// @param Node The child of the mark node to be inserted.
|
|
|
|
|
/// @param BasePtr The pointer to be marked.
|
|
|
|
|
/// @return The modified isl_schedule_node.
|
|
|
|
|
static isl_schedule_node *markInterIterationAliasFree(isl_schedule_node *Node,
|
|
|
|
|
llvm::Value *BasePtr) {
|
|
|
|
|
if (!BasePtr)
|
|
|
|
|
return Node;
|
|
|
|
|
|
|
|
|
|
auto *Ctx = isl_schedule_node_get_ctx(Node);
|
|
|
|
|
auto *Id = isl_id_alloc(Ctx, "Inter iteration alias-free", BasePtr);
|
|
|
|
|
return isl_schedule_node_child(isl_schedule_node_insert_mark(Node, Id), 0);
|
|
|
|
|
}
|
|
|
|
|
|
Restore the initial ordering of dimensions before applying the pattern matching
Dimensions of band nodes can be implicitly permuted by the algorithm applied
during the schedule generation.
For example, in case of the following matrix-matrix multiplication,
for (i = 0; i < 1024; i++)
for (k = 0; k < 1024; k++)
for (j = 0; j < 1024; j++)
C[i][j] += A[i][k] * B[k][j];
it can produce the following schedule tree
domain: "{ Stmt_for_body6[i0, i1, i2] : 0 <= i0 <= 1023 and 0 <= i1 <= 1023 and
0 <= i2 <= 1023 }"
child:
schedule: "[{ Stmt_for_body6[i0, i1, i2] -> [(i0)] },
{ Stmt_for_body6[i0, i1, i2] -> [(i1)] },
{ Stmt_for_body6[i0, i1, i2] -> [(i2)] }]"
permutable: 1
coincident: [ 1, 1, 0 ]
The current implementation of the pattern matching optimizations relies on the
initial ordering of dimensions. Otherwise, it can produce the miscompilation
(e.g., [1]).
This patch helps to restore the initial ordering of dimensions by recreating
the band node when the corresponding conditions are satisfied.
Refs.:
[1] - https://bugs.llvm.org/show_bug.cgi?id=32500
Reviewed-by: Michael Kruse <llvm@meinersbur.de>
Differential Revision: https://reviews.llvm.org/D31741
llvm-svn: 299662
2017-04-07 01:09:54 +08:00
|
|
|
|
/// Restore the initial ordering of dimensions of the band node
|
|
|
|
|
///
|
|
|
|
|
/// In case the band node represents all the dimensions of the iteration
|
|
|
|
|
/// domain, recreate the band node to restore the initial ordering of the
|
|
|
|
|
/// dimensions.
|
|
|
|
|
///
|
|
|
|
|
/// @param Node The band node to be modified.
|
|
|
|
|
/// @return The modified schedule node.
|
|
|
|
|
namespace {
|
|
|
|
|
isl::schedule_node getBandNodeWithOriginDimOrder(isl::schedule_node Node) {
|
|
|
|
|
assert(isl_schedule_node_get_type(Node.keep()) == isl_schedule_node_band);
|
|
|
|
|
if (isl_schedule_node_get_type(Node.child(0).keep()) !=
|
|
|
|
|
isl_schedule_node_leaf)
|
|
|
|
|
return Node;
|
|
|
|
|
auto Domain = isl::manage(isl_schedule_node_get_universe_domain(Node.keep()));
|
|
|
|
|
assert(isl_union_set_n_set(Domain.keep()) == 1);
|
|
|
|
|
if (isl_schedule_node_get_schedule_depth(Node.keep()) != 0 ||
|
|
|
|
|
(isl::set(isl::manage(Domain.copy())).dim(isl::dim::set) !=
|
|
|
|
|
isl_schedule_node_band_n_member(Node.keep())))
|
|
|
|
|
return Node;
|
|
|
|
|
Node = isl::manage(isl_schedule_node_delete(Node.take()));
|
|
|
|
|
auto PartialSchedulePwAff =
|
|
|
|
|
isl::manage(isl_union_set_identity_union_pw_multi_aff(Domain.take()));
|
|
|
|
|
auto PartialScheduleMultiPwAff =
|
|
|
|
|
isl::multi_union_pw_aff(PartialSchedulePwAff);
|
|
|
|
|
PartialScheduleMultiPwAff = isl::manage(isl_multi_union_pw_aff_reset_tuple_id(
|
|
|
|
|
PartialScheduleMultiPwAff.take(), isl_dim_set));
|
|
|
|
|
return isl::manage(isl_schedule_node_insert_partial_schedule(
|
|
|
|
|
Node.take(), PartialScheduleMultiPwAff.take()));
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2016-07-25 15:27:59 +08:00
|
|
|
|
__isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeMatMulPattern(
|
2017-02-02 22:23:14 +08:00
|
|
|
|
__isl_take isl_schedule_node *Node, const llvm::TargetTransformInfo *TTI,
|
|
|
|
|
MatMulInfoTy &MMI) {
|
2016-07-25 15:27:59 +08:00
|
|
|
|
assert(TTI && "The target transform info should be provided.");
|
2017-05-10 18:59:58 +08:00
|
|
|
|
Node = markInterIterationAliasFree(
|
|
|
|
|
Node, MMI.WriteToC->getLatestScopArrayInfo()->getBasePtr());
|
2017-02-02 22:23:14 +08:00
|
|
|
|
int DimOutNum = isl_schedule_node_band_n_member(Node);
|
|
|
|
|
assert(DimOutNum > 2 && "In case of the matrix multiplication the loop nest "
|
|
|
|
|
"and, consequently, the corresponding scheduling "
|
|
|
|
|
"functions have at least three dimensions.");
|
Restore the initial ordering of dimensions before applying the pattern matching
Dimensions of band nodes can be implicitly permuted by the algorithm applied
during the schedule generation.
For example, in case of the following matrix-matrix multiplication,
for (i = 0; i < 1024; i++)
for (k = 0; k < 1024; k++)
for (j = 0; j < 1024; j++)
C[i][j] += A[i][k] * B[k][j];
it can produce the following schedule tree
domain: "{ Stmt_for_body6[i0, i1, i2] : 0 <= i0 <= 1023 and 0 <= i1 <= 1023 and
0 <= i2 <= 1023 }"
child:
schedule: "[{ Stmt_for_body6[i0, i1, i2] -> [(i0)] },
{ Stmt_for_body6[i0, i1, i2] -> [(i1)] },
{ Stmt_for_body6[i0, i1, i2] -> [(i2)] }]"
permutable: 1
coincident: [ 1, 1, 0 ]
The current implementation of the pattern matching optimizations relies on the
initial ordering of dimensions. Otherwise, it can produce the miscompilation
(e.g., [1]).
This patch helps to restore the initial ordering of dimensions by recreating
the band node when the corresponding conditions are satisfied.
Refs.:
[1] - https://bugs.llvm.org/show_bug.cgi?id=32500
Reviewed-by: Michael Kruse <llvm@meinersbur.de>
Differential Revision: https://reviews.llvm.org/D31741
llvm-svn: 299662
2017-04-07 01:09:54 +08:00
|
|
|
|
Node = getBandNodeWithOriginDimOrder(isl::manage(Node)).take();
|
2017-02-02 22:23:14 +08:00
|
|
|
|
Node = permuteBandNodeDimensions(Node, MMI.i, DimOutNum - 3);
|
|
|
|
|
int NewJ = MMI.j == DimOutNum - 3 ? MMI.i : MMI.j;
|
|
|
|
|
int NewK = MMI.k == DimOutNum - 3 ? MMI.i : MMI.k;
|
|
|
|
|
Node = permuteBandNodeDimensions(Node, NewJ, DimOutNum - 2);
|
2017-04-07 01:25:08 +08:00
|
|
|
|
NewK = NewK == DimOutNum - 2 ? NewJ : NewK;
|
2017-02-02 22:23:14 +08:00
|
|
|
|
Node = permuteBandNodeDimensions(Node, NewK, DimOutNum - 1);
|
2017-02-11 15:00:05 +08:00
|
|
|
|
auto MicroKernelParams = getMicroKernelParams(TTI, MMI);
|
|
|
|
|
auto MacroKernelParams = getMacroKernelParams(MicroKernelParams, MMI);
|
2016-07-25 17:42:53 +08:00
|
|
|
|
Node = createMacroKernel(Node, MacroKernelParams);
|
2016-07-25 15:27:59 +08:00
|
|
|
|
Node = createMicroKernel(Node, MicroKernelParams);
|
2016-08-15 20:22:54 +08:00
|
|
|
|
if (MacroKernelParams.Mc == 1 || MacroKernelParams.Nc == 1 ||
|
|
|
|
|
MacroKernelParams.Kc == 1)
|
|
|
|
|
return Node;
|
|
|
|
|
auto *MapOldIndVar = getInductionVariablesSubstitution(
|
|
|
|
|
Node, MicroKernelParams, MacroKernelParams);
|
|
|
|
|
if (!MapOldIndVar)
|
|
|
|
|
return Node;
|
2017-02-09 15:10:01 +08:00
|
|
|
|
Node = isolateAndUnrollMatMulInnerLoops(Node, MicroKernelParams);
|
2016-09-14 14:26:09 +08:00
|
|
|
|
return optimizeDataLayoutMatrMulPattern(Node, MapOldIndVar, MicroKernelParams,
|
2017-02-02 22:23:14 +08:00
|
|
|
|
MacroKernelParams, MMI);
|
2016-06-22 17:52:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-29 00:17:58 +08:00
|
|
|
|
bool ScheduleTreeOptimizer::isMatrMultPattern(
|
2017-02-02 22:23:14 +08:00
|
|
|
|
__isl_keep isl_schedule_node *Node, const Dependences *D,
|
|
|
|
|
MatMulInfoTy &MMI) {
|
2016-05-29 00:17:58 +08:00
|
|
|
|
auto *PartialSchedule =
|
|
|
|
|
isl_schedule_node_band_get_partial_schedule_union_map(Node);
|
Restore the initial ordering of dimensions before applying the pattern matching
Dimensions of band nodes can be implicitly permuted by the algorithm applied
during the schedule generation.
For example, in case of the following matrix-matrix multiplication,
for (i = 0; i < 1024; i++)
for (k = 0; k < 1024; k++)
for (j = 0; j < 1024; j++)
C[i][j] += A[i][k] * B[k][j];
it can produce the following schedule tree
domain: "{ Stmt_for_body6[i0, i1, i2] : 0 <= i0 <= 1023 and 0 <= i1 <= 1023 and
0 <= i2 <= 1023 }"
child:
schedule: "[{ Stmt_for_body6[i0, i1, i2] -> [(i0)] },
{ Stmt_for_body6[i0, i1, i2] -> [(i1)] },
{ Stmt_for_body6[i0, i1, i2] -> [(i2)] }]"
permutable: 1
coincident: [ 1, 1, 0 ]
The current implementation of the pattern matching optimizations relies on the
initial ordering of dimensions. Otherwise, it can produce the miscompilation
(e.g., [1]).
This patch helps to restore the initial ordering of dimensions by recreating
the band node when the corresponding conditions are satisfied.
Refs.:
[1] - https://bugs.llvm.org/show_bug.cgi?id=32500
Reviewed-by: Michael Kruse <llvm@meinersbur.de>
Differential Revision: https://reviews.llvm.org/D31741
llvm-svn: 299662
2017-04-07 01:09:54 +08:00
|
|
|
|
Node = isl_schedule_node_child(Node, 0);
|
|
|
|
|
auto LeafType = isl_schedule_node_get_type(Node);
|
|
|
|
|
Node = isl_schedule_node_parent(Node);
|
|
|
|
|
if (LeafType != isl_schedule_node_leaf ||
|
|
|
|
|
isl_schedule_node_band_n_member(Node) < 3 ||
|
|
|
|
|
isl_schedule_node_get_schedule_depth(Node) != 0 ||
|
2016-06-22 20:11:30 +08:00
|
|
|
|
isl_union_map_n_map(PartialSchedule) != 1) {
|
|
|
|
|
isl_union_map_free(PartialSchedule);
|
2016-05-29 00:17:58 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-22 20:11:30 +08:00
|
|
|
|
auto *NewPartialSchedule = isl_map_from_union_map(PartialSchedule);
|
2017-02-02 22:23:14 +08:00
|
|
|
|
if (containsMatrMult(NewPartialSchedule, D, MMI)) {
|
2016-05-29 00:17:58 +08:00
|
|
|
|
isl_map_free(NewPartialSchedule);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
isl_map_free(NewPartialSchedule);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__isl_give isl_schedule_node *
|
|
|
|
|
ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node,
|
|
|
|
|
void *User) {
|
|
|
|
|
if (!isTileableBandNode(Node))
|
|
|
|
|
return Node;
|
|
|
|
|
|
2017-02-02 22:23:14 +08:00
|
|
|
|
const OptimizerAdditionalInfoTy *OAI =
|
|
|
|
|
static_cast<const OptimizerAdditionalInfoTy *>(User);
|
|
|
|
|
|
|
|
|
|
MatMulInfoTy MMI;
|
|
|
|
|
if (PMBasedOpts && User && isMatrMultPattern(Node, OAI->D, MMI)) {
|
2016-05-29 00:17:58 +08:00
|
|
|
|
DEBUG(dbgs() << "The matrix multiplication pattern was detected\n");
|
2017-02-08 21:29:06 +08:00
|
|
|
|
return optimizeMatMulPattern(Node, OAI->TTI, MMI);
|
2016-06-22 17:52:37 +08:00
|
|
|
|
}
|
2016-05-29 00:17:58 +08:00
|
|
|
|
|
|
|
|
|
return standardBandOpts(Node, User);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 17:33:13 +08:00
|
|
|
|
__isl_give isl_schedule *
|
2016-06-22 17:52:37 +08:00
|
|
|
|
ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule,
|
2017-02-02 22:23:14 +08:00
|
|
|
|
const OptimizerAdditionalInfoTy *OAI) {
|
2015-03-22 20:06:39 +08:00
|
|
|
|
isl_schedule_node *Root = isl_schedule_get_root(Schedule);
|
2017-02-02 22:23:14 +08:00
|
|
|
|
Root = optimizeScheduleNode(Root, OAI);
|
2015-07-14 17:33:13 +08:00
|
|
|
|
isl_schedule_free(Schedule);
|
|
|
|
|
auto S = isl_schedule_node_get_schedule(Root);
|
2015-03-22 20:06:39 +08:00
|
|
|
|
isl_schedule_node_free(Root);
|
2015-07-14 17:33:13 +08:00
|
|
|
|
return S;
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 14:01:47 +08:00
|
|
|
|
__isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode(
|
2017-02-02 22:23:14 +08:00
|
|
|
|
__isl_take isl_schedule_node *Node, const OptimizerAdditionalInfoTy *OAI) {
|
2016-06-22 17:52:37 +08:00
|
|
|
|
Node = isl_schedule_node_map_descendant_bottom_up(
|
2017-02-02 22:23:14 +08:00
|
|
|
|
Node, optimizeBand, const_cast<void *>(static_cast<const void *>(OAI)));
|
2015-08-24 14:01:47 +08:00
|
|
|
|
return Node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ScheduleTreeOptimizer::isProfitableSchedule(
|
2016-09-14 14:26:09 +08:00
|
|
|
|
Scop &S, __isl_keep isl_schedule *NewSchedule) {
|
2015-02-12 01:25:09 +08:00
|
|
|
|
// To understand if the schedule has been optimized we check if the schedule
|
|
|
|
|
// has changed at all.
|
|
|
|
|
// TODO: We can improve this by tracking if any necessarily beneficial
|
|
|
|
|
// transformations have been performed. This can e.g. be tiling, loop
|
|
|
|
|
// interchange, or ...) We can track this either at the place where the
|
|
|
|
|
// transformation has been performed or, in case of automatic ILP based
|
|
|
|
|
// optimizations, by comparing (yet to be defined) performance metrics
|
|
|
|
|
// before/after the scheduling optimizer
|
|
|
|
|
// (e.g., #stride-one accesses)
|
2016-09-14 14:26:09 +08:00
|
|
|
|
if (S.containsExtensionNode(NewSchedule))
|
|
|
|
|
return true;
|
|
|
|
|
auto *NewScheduleMap = isl_schedule_get_map(NewSchedule);
|
2015-02-12 01:25:09 +08:00
|
|
|
|
isl_union_map *OldSchedule = S.getSchedule();
|
2017-02-01 18:12:09 +08:00
|
|
|
|
assert(OldSchedule && "Only IslScheduleOptimizer can insert extension nodes "
|
|
|
|
|
"that make Scop::getSchedule() return nullptr.");
|
2016-09-14 14:26:09 +08:00
|
|
|
|
bool changed = !isl_union_map_is_equal(OldSchedule, NewScheduleMap);
|
2015-02-12 01:25:09 +08:00
|
|
|
|
isl_union_map_free(OldSchedule);
|
2016-09-14 14:26:09 +08:00
|
|
|
|
isl_union_map_free(NewScheduleMap);
|
2015-02-12 01:25:09 +08:00
|
|
|
|
return changed;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 14:01:47 +08:00
|
|
|
|
namespace {
|
|
|
|
|
class IslScheduleOptimizer : public ScopPass {
|
|
|
|
|
public:
|
|
|
|
|
static char ID;
|
|
|
|
|
explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
|
|
|
|
|
|
|
|
|
|
~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Optimize the schedule of the SCoP @p S.
|
2015-08-24 14:01:47 +08:00
|
|
|
|
bool runOnScop(Scop &S) override;
|
2015-09-27 23:43:29 +08:00
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Print the new schedule for the SCoP @p S.
|
2015-08-24 14:01:47 +08:00
|
|
|
|
void printScop(raw_ostream &OS, Scop &S) const override;
|
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Register all analyses and transformation required.
|
2015-09-27 23:43:29 +08:00
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2015-08-24 14:01:47 +08:00
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
|
/// Release the internal memory.
|
2015-09-27 23:42:28 +08:00
|
|
|
|
void releaseMemory() override {
|
2015-08-24 14:01:47 +08:00
|
|
|
|
isl_schedule_free(LastSchedule);
|
|
|
|
|
LastSchedule = nullptr;
|
|
|
|
|
}
|
2015-09-27 23:43:29 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
isl_schedule *LastSchedule;
|
2015-08-24 14:01:47 +08:00
|
|
|
|
};
|
2016-06-24 06:17:27 +08:00
|
|
|
|
} // namespace
|
2015-08-24 14:01:47 +08:00
|
|
|
|
|
|
|
|
|
char IslScheduleOptimizer::ID = 0;
|
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
|
bool IslScheduleOptimizer::runOnScop(Scop &S) {
|
2015-02-14 20:02:24 +08:00
|
|
|
|
|
|
|
|
|
// Skip empty SCoPs but still allow code generation as it will delete the
|
|
|
|
|
// loops present but not needed.
|
|
|
|
|
if (S.getSize() == 0) {
|
|
|
|
|
S.markAsOptimized();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 16:15:33 +08:00
|
|
|
|
const Dependences &D =
|
|
|
|
|
getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2015-03-05 08:43:48 +08:00
|
|
|
|
if (!D.hasValidDependences())
|
2014-02-23 23:15:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2012-10-16 15:29:19 +08:00
|
|
|
|
isl_schedule_free(LastSchedule);
|
2014-04-16 15:33:47 +08:00
|
|
|
|
LastSchedule = nullptr;
|
2012-10-16 15:29:19 +08:00
|
|
|
|
|
2011-05-15 03:02:06 +08:00
|
|
|
|
// Build input data.
|
2015-03-05 08:43:48 +08:00
|
|
|
|
int ValidityKinds =
|
|
|
|
|
Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
|
2012-02-14 22:02:48 +08:00
|
|
|
|
int ProximityKinds;
|
|
|
|
|
|
|
|
|
|
if (OptimizeDeps == "all")
|
2015-03-05 08:43:48 +08:00
|
|
|
|
ProximityKinds =
|
|
|
|
|
Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
|
2012-02-14 22:02:48 +08:00
|
|
|
|
else if (OptimizeDeps == "raw")
|
2015-03-05 08:43:48 +08:00
|
|
|
|
ProximityKinds = Dependences::TYPE_RAW;
|
2012-02-14 22:02:48 +08:00
|
|
|
|
else {
|
|
|
|
|
errs() << "Do not know how to optimize for '" << OptimizeDeps << "'"
|
2013-03-23 09:05:07 +08:00
|
|
|
|
<< " Falling back to optimizing all dependences.\n";
|
2015-03-05 08:43:48 +08:00
|
|
|
|
ProximityKinds =
|
|
|
|
|
Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
|
2012-02-14 22:02:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-22 00:21:33 +08:00
|
|
|
|
isl::union_set Domain = give(S.getDomains());
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2012-02-14 07:31:39 +08:00
|
|
|
|
if (!Domain)
|
2011-05-15 03:02:06 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2017-05-22 00:21:33 +08:00
|
|
|
|
isl::union_map Validity = give(D.getDependences(ValidityKinds));
|
|
|
|
|
isl::union_map Proximity = give(D.getDependences(ProximityKinds));
|
2012-03-16 19:51:41 +08:00
|
|
|
|
|
2012-01-31 03:38:43 +08:00
|
|
|
|
// Simplify the dependences by removing the constraints introduced by the
|
|
|
|
|
// domains. This can speed up the scheduling time significantly, as large
|
|
|
|
|
// constant coefficients will be removed from the dependences. The
|
|
|
|
|
// introduction of some additional dependences reduces the possible
|
|
|
|
|
// transformations, but in most cases, such transformation do not seem to be
|
|
|
|
|
// interesting anyway. In some cases this option may stop the scheduler to
|
|
|
|
|
// find any schedule.
|
|
|
|
|
if (SimplifyDeps == "yes") {
|
2017-05-22 00:21:33 +08:00
|
|
|
|
Validity = Validity.gist_domain(Domain);
|
|
|
|
|
Validity = Validity.gist_range(Domain);
|
|
|
|
|
Proximity = Proximity.gist_domain(Domain);
|
|
|
|
|
Proximity = Proximity.gist_range(Domain);
|
2012-01-31 03:38:43 +08:00
|
|
|
|
} else if (SimplifyDeps != "no") {
|
|
|
|
|
errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' "
|
|
|
|
|
"or 'no'. Falling back to default: 'yes'\n";
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-15 03:02:06 +08:00
|
|
|
|
DEBUG(dbgs() << "\n\nCompute schedule from: ");
|
2017-05-22 00:21:33 +08:00
|
|
|
|
DEBUG(dbgs() << "Domain := " << Domain << ";\n");
|
|
|
|
|
DEBUG(dbgs() << "Proximity := " << Proximity << ";\n");
|
|
|
|
|
DEBUG(dbgs() << "Validity := " << Validity << ";\n");
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2015-06-19 00:45:40 +08:00
|
|
|
|
unsigned IslSerializeSCCs;
|
2012-01-31 03:38:50 +08:00
|
|
|
|
|
|
|
|
|
if (FusionStrategy == "max") {
|
2015-06-19 00:45:40 +08:00
|
|
|
|
IslSerializeSCCs = 0;
|
2012-01-31 03:38:50 +08:00
|
|
|
|
} else if (FusionStrategy == "min") {
|
2015-06-19 00:45:40 +08:00
|
|
|
|
IslSerializeSCCs = 1;
|
2012-01-31 03:38:50 +08:00
|
|
|
|
} else {
|
|
|
|
|
errs() << "warning: Unknown fusion strategy. Falling back to maximal "
|
|
|
|
|
"fusion.\n";
|
2015-06-19 00:45:40 +08:00
|
|
|
|
IslSerializeSCCs = 0;
|
2012-01-31 03:38:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-31 03:38:54 +08:00
|
|
|
|
int IslMaximizeBands;
|
|
|
|
|
|
2012-01-31 06:43:56 +08:00
|
|
|
|
if (MaximizeBandDepth == "yes") {
|
2012-01-31 03:38:54 +08:00
|
|
|
|
IslMaximizeBands = 1;
|
2012-01-31 06:43:56 +08:00
|
|
|
|
} else if (MaximizeBandDepth == "no") {
|
2012-01-31 03:38:54 +08:00
|
|
|
|
IslMaximizeBands = 0;
|
|
|
|
|
} else {
|
|
|
|
|
errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'"
|
|
|
|
|
" or 'no'. Falling back to default: 'yes'\n";
|
|
|
|
|
IslMaximizeBands = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 19:35:27 +08:00
|
|
|
|
int IslOuterCoincidence;
|
|
|
|
|
|
|
|
|
|
if (OuterCoincidence == "yes") {
|
|
|
|
|
IslOuterCoincidence = 1;
|
|
|
|
|
} else if (OuterCoincidence == "no") {
|
|
|
|
|
IslOuterCoincidence = 0;
|
|
|
|
|
} else {
|
|
|
|
|
errs() << "warning: Option -polly-opt-outer-coincidence should either be "
|
|
|
|
|
"'yes' or 'no'. Falling back to default: 'no'\n";
|
|
|
|
|
IslOuterCoincidence = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-01 04:42:56 +08:00
|
|
|
|
isl_ctx *Ctx = S.getIslCtx();
|
2012-01-31 03:38:47 +08:00
|
|
|
|
|
2016-07-01 04:42:56 +08:00
|
|
|
|
isl_options_set_schedule_outer_coincidence(Ctx, IslOuterCoincidence);
|
|
|
|
|
isl_options_set_schedule_serialize_sccs(Ctx, IslSerializeSCCs);
|
|
|
|
|
isl_options_set_schedule_maximize_band_depth(Ctx, IslMaximizeBands);
|
|
|
|
|
isl_options_set_schedule_max_constant_term(Ctx, MaxConstantTerm);
|
|
|
|
|
isl_options_set_schedule_max_coefficient(Ctx, MaxCoefficient);
|
|
|
|
|
isl_options_set_tile_scale_tile_loops(Ctx, 0);
|
|
|
|
|
|
2016-07-01 04:42:58 +08:00
|
|
|
|
auto OnErrorStatus = isl_options_get_on_error(Ctx);
|
2016-07-01 04:42:56 +08:00
|
|
|
|
isl_options_set_on_error(Ctx, ISL_ON_ERROR_CONTINUE);
|
2014-01-27 03:36:28 +08:00
|
|
|
|
|
2017-05-22 00:21:33 +08:00
|
|
|
|
auto SC = isl::schedule_constraints::on_domain(Domain);
|
|
|
|
|
SC = SC.set_proximity(Proximity);
|
|
|
|
|
SC = SC.set_validity(Validity);
|
|
|
|
|
SC = SC.set_coincidence(Validity);
|
2012-02-14 22:02:44 +08:00
|
|
|
|
isl_schedule *Schedule;
|
2017-05-22 00:21:33 +08:00
|
|
|
|
Schedule = SC.compute_schedule().release();
|
2016-07-01 04:42:58 +08:00
|
|
|
|
isl_options_set_on_error(Ctx, OnErrorStatus);
|
2012-01-31 03:38:47 +08:00
|
|
|
|
|
|
|
|
|
// In cases the scheduler is not able to optimize the code, we just do not
|
|
|
|
|
// touch the schedule.
|
2012-02-14 07:31:39 +08:00
|
|
|
|
if (!Schedule)
|
2012-01-31 03:38:47 +08:00
|
|
|
|
return false;
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2015-05-30 14:46:59 +08:00
|
|
|
|
DEBUG({
|
2016-07-01 04:42:56 +08:00
|
|
|
|
auto *P = isl_printer_to_str(Ctx);
|
2015-05-30 14:46:59 +08:00
|
|
|
|
P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
|
|
|
|
|
P = isl_printer_print_schedule(P, Schedule);
|
2016-12-12 22:51:06 +08:00
|
|
|
|
auto *str = isl_printer_get_str(P);
|
|
|
|
|
dbgs() << "NewScheduleTree: \n" << str << "\n";
|
|
|
|
|
free(str);
|
2015-05-30 14:46:59 +08:00
|
|
|
|
isl_printer_free(P);
|
|
|
|
|
});
|
2012-02-20 16:41:21 +08:00
|
|
|
|
|
2016-06-22 17:52:37 +08:00
|
|
|
|
Function &F = S.getFunction();
|
|
|
|
|
auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
|
2017-02-02 22:23:14 +08:00
|
|
|
|
const OptimizerAdditionalInfoTy OAI = {TTI, const_cast<Dependences *>(&D)};
|
2016-06-22 17:52:37 +08:00
|
|
|
|
isl_schedule *NewSchedule =
|
2017-02-02 22:23:14 +08:00
|
|
|
|
ScheduleTreeOptimizer::optimizeSchedule(Schedule, &OAI);
|
2015-02-12 01:25:09 +08:00
|
|
|
|
|
2016-09-14 14:26:09 +08:00
|
|
|
|
if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewSchedule)) {
|
2015-07-14 17:33:13 +08:00
|
|
|
|
isl_schedule_free(NewSchedule);
|
2015-02-12 01:25:09 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 17:33:13 +08:00
|
|
|
|
S.setScheduleTree(NewSchedule);
|
2015-02-12 01:25:09 +08:00
|
|
|
|
S.markAsOptimized();
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2016-08-21 19:20:39 +08:00
|
|
|
|
if (OptimizedScops)
|
|
|
|
|
S.dump();
|
|
|
|
|
|
2011-05-15 03:02:06 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 02:40:25 +08:00
|
|
|
|
void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const {
|
2012-10-16 15:29:19 +08:00
|
|
|
|
isl_printer *p;
|
|
|
|
|
char *ScheduleStr;
|
|
|
|
|
|
|
|
|
|
OS << "Calculated schedule:\n";
|
|
|
|
|
|
|
|
|
|
if (!LastSchedule) {
|
|
|
|
|
OS << "n/a\n";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
|
|
|
|
|
p = isl_printer_print_schedule(p, LastSchedule);
|
|
|
|
|
ScheduleStr = isl_printer_get_str(p);
|
|
|
|
|
isl_printer_free(p);
|
|
|
|
|
|
|
|
|
|
OS << ScheduleStr << "\n";
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
|
void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
|
2011-05-15 03:02:06 +08:00
|
|
|
|
ScopPass::getAnalysisUsage(AU);
|
2015-03-05 06:43:40 +08:00
|
|
|
|
AU.addRequired<DependenceInfo>();
|
2016-06-22 17:52:37 +08:00
|
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
|
Pass *polly::createIslScheduleOptimizerPass() {
|
2011-10-08 08:30:40 +08:00
|
|
|
|
return new IslScheduleOptimizer();
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
2013-03-23 09:05:07 +08:00
|
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
|
|
|
|
|
"Polly - Optimize schedule of SCoP", false, false);
|
2015-03-05 06:43:40 +08:00
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
|
2016-05-31 17:41:04 +08:00
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
|
2016-06-22 17:52:37 +08:00
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass);
|
2013-03-23 09:05:07 +08:00
|
|
|
|
INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
|
|
|
|
|
"Polly - Optimize schedule of SCoP", false, false)
|