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.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// This pass the isl to calculate a schedule that is optimized for parallelism
|
|
|
|
|
// and tileablility. The algorithm used in isl is an optimized version of the
|
|
|
|
|
// algorithm described in following paper:
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
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"
|
|
|
|
|
#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"
|
|
|
|
|
|
2013-06-23 09:29:29 +08:00
|
|
|
|
namespace polly {
|
|
|
|
|
bool DisablePollyTiling;
|
|
|
|
|
}
|
2013-05-07 15:30:56 +08:00
|
|
|
|
static cl::opt<bool, true>
|
2014-07-09 18:50:10 +08:00
|
|
|
|
DisableTiling("polly-no-tiling",
|
|
|
|
|
cl::desc("Disable tiling in the scheduler"),
|
|
|
|
|
cl::location(polly::DisablePollyTiling), cl::init(false),
|
|
|
|
|
cl::ZeroOrMore, cl::cat(PollyCategory));
|
2011-10-24 04:59:26 +08:00
|
|
|
|
|
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
|
|
|
|
|
2014-07-09 18:50:10 +08:00
|
|
|
|
static cl::opt<int> DefaultTileSize(
|
|
|
|
|
"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
|
|
|
|
|
|
|
|
|
static cl::list<int> TileSizes("polly-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));
|
2011-05-15 03:02:06 +08:00
|
|
|
|
namespace {
|
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
|
class IslScheduleOptimizer : public ScopPass {
|
|
|
|
|
public:
|
|
|
|
|
static char ID;
|
2014-04-16 15:33:47 +08:00
|
|
|
|
explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; }
|
2013-03-23 09:05:07 +08:00
|
|
|
|
|
|
|
|
|
~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
|
|
|
|
|
|
2015-03-02 02:42:08 +08:00
|
|
|
|
bool runOnScop(Scop &S) override;
|
|
|
|
|
void printScop(raw_ostream &OS, Scop &S) const override;
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2013-03-23 09:05:07 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
isl_schedule *LastSchedule;
|
|
|
|
|
|
2015-02-12 01:25:09 +08:00
|
|
|
|
/// @brief Decide if the @p NewSchedule is profitable for @p S.
|
|
|
|
|
///
|
|
|
|
|
/// @param S The SCoP we optimize.
|
|
|
|
|
/// @param NewSchedule The new schedule we computed.
|
|
|
|
|
///
|
|
|
|
|
/// @return True, if we believe @p NewSchedule is an improvement for @p S.
|
|
|
|
|
bool isProfitableSchedule(Scop &S, __isl_keep isl_union_map *NewSchedule);
|
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
|
/// @brief Create a map that pre-vectorizes one scheduling dimension.
|
|
|
|
|
///
|
|
|
|
|
/// getPrevectorMap creates a map that maps each input dimension to the same
|
|
|
|
|
/// output dimension, except for the dimension DimToVectorize.
|
|
|
|
|
/// DimToVectorize is strip mined by 'VectorWidth' and the newly created
|
|
|
|
|
/// point loop of DimToVectorize is moved to the innermost level.
|
|
|
|
|
///
|
|
|
|
|
/// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
|
|
|
|
|
///
|
|
|
|
|
/// | Before transformation
|
|
|
|
|
/// |
|
|
|
|
|
/// | A[i,j] -> [i,j]
|
|
|
|
|
/// |
|
|
|
|
|
/// | for (i = 0; i < 128; i++)
|
|
|
|
|
/// | for (j = 0; j < 128; j++)
|
|
|
|
|
/// | A(i,j);
|
|
|
|
|
///
|
|
|
|
|
/// Prevector map:
|
|
|
|
|
/// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
|
|
|
|
|
///
|
|
|
|
|
/// | After transformation:
|
|
|
|
|
/// |
|
|
|
|
|
/// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
|
|
|
|
|
/// |
|
|
|
|
|
/// | for (it = 0; it < 128; it+=4)
|
|
|
|
|
/// | for (j = 0; j < 128; j++)
|
|
|
|
|
/// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
|
|
|
|
|
/// | A(ip,j);
|
|
|
|
|
///
|
|
|
|
|
/// The goal of this transformation is to create a trivially vectorizable
|
|
|
|
|
/// loop. This means a parallel loop at the innermost level that has a
|
|
|
|
|
/// constant number of iterations corresponding to the target vector width.
|
|
|
|
|
///
|
|
|
|
|
/// This transformation creates a loop at the innermost level. The loop has
|
|
|
|
|
/// a constant number of iterations, if the number of loop iterations at
|
|
|
|
|
/// DimToVectorize can be divided by VectorWidth. The default VectorWidth is
|
|
|
|
|
/// currently constant and not yet target specific. This function does not
|
|
|
|
|
/// reason about parallelism.
|
2015-03-19 15:43:35 +08:00
|
|
|
|
static __isl_give isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
|
|
|
|
|
int ScheduleDimensions,
|
|
|
|
|
int VectorWidth = 4);
|
2013-03-23 09:05:07 +08:00
|
|
|
|
|
2015-03-22 20:06:39 +08:00
|
|
|
|
/// @brief Apply additional optimizations on the bands in the schedule tree.
|
|
|
|
|
///
|
|
|
|
|
/// We are looking for an innermost band node and apply the following
|
|
|
|
|
/// transformations:
|
|
|
|
|
///
|
|
|
|
|
/// - Tile the band
|
|
|
|
|
/// - if the band is tileable
|
|
|
|
|
/// - if the band has more than one loop dimension
|
|
|
|
|
///
|
|
|
|
|
/// - Prevectorize the point loop of the tile
|
|
|
|
|
/// - if vectorization is enabled
|
2013-03-23 09:05:07 +08:00
|
|
|
|
///
|
2015-03-22 20:06:39 +08:00
|
|
|
|
/// @param Node The schedule node to (possibly) optimize.
|
|
|
|
|
/// @param User A pointer to forward some use information (currently unused).
|
|
|
|
|
static isl_schedule_node *optimizeBand(isl_schedule_node *Node, void *User);
|
2013-03-23 09:05:07 +08:00
|
|
|
|
|
2015-07-14 17:33:13 +08:00
|
|
|
|
/// @brief Apply post-scheduling transformations.
|
|
|
|
|
///
|
|
|
|
|
/// This function applies a set of additional local transformations on the
|
|
|
|
|
/// schedule tree as it computed by the isl scheduler. Local transformations
|
|
|
|
|
/// applied include:
|
|
|
|
|
///
|
|
|
|
|
/// - Tiling
|
|
|
|
|
/// - Prevectorization
|
|
|
|
|
///
|
|
|
|
|
/// @param Schedule The schedule object post-transformations will be applied
|
|
|
|
|
/// on.
|
|
|
|
|
/// @returns The transformed schedule.
|
|
|
|
|
static __isl_give isl_schedule *
|
|
|
|
|
addPostTransforms(__isl_take isl_schedule *Schedule);
|
2013-03-23 09:05:07 +08:00
|
|
|
|
|
2014-04-12 01:56:49 +08:00
|
|
|
|
using llvm::Pass::doFinalization;
|
|
|
|
|
|
2015-03-02 02:42:08 +08:00
|
|
|
|
virtual bool doFinalization() override {
|
2013-03-23 09:05:07 +08:00
|
|
|
|
isl_schedule_free(LastSchedule);
|
2014-04-16 15:33:47 +08:00
|
|
|
|
LastSchedule = nullptr;
|
2013-03-23 09:05:07 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
|
char IslScheduleOptimizer::ID = 0;
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2015-03-19 15:43:35 +08:00
|
|
|
|
__isl_give isl_map *
|
|
|
|
|
IslScheduleOptimizer::getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
|
|
|
|
|
int ScheduleDimensions, int VectorWidth) {
|
2011-12-07 15:42:57 +08:00
|
|
|
|
isl_space *Space;
|
|
|
|
|
isl_local_space *LocalSpace, *LocalSpaceRange;
|
|
|
|
|
isl_set *Modulo;
|
|
|
|
|
isl_map *TilingMap;
|
2011-07-01 04:29:13 +08:00
|
|
|
|
isl_constraint *c;
|
2011-12-07 15:42:57 +08:00
|
|
|
|
isl_aff *Aff;
|
|
|
|
|
int PointDimension; /* ip */
|
|
|
|
|
int TileDimension; /* it */
|
2013-06-21 14:41:31 +08:00
|
|
|
|
isl_val *VectorWidthMP;
|
2011-12-07 15:42:57 +08:00
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
|
assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
|
2011-12-07 15:42:57 +08:00
|
|
|
|
|
|
|
|
|
Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
|
|
|
|
|
TilingMap = isl_map_universe(isl_space_copy(Space));
|
|
|
|
|
LocalSpace = isl_local_space_from_space(Space);
|
|
|
|
|
PointDimension = ScheduleDimensions;
|
|
|
|
|
TileDimension = DimToVectorize;
|
|
|
|
|
|
|
|
|
|
// Create an identity map for everything except DimToVectorize and map
|
|
|
|
|
// DimToVectorize to the point loop at the innermost dimension.
|
2015-05-22 03:02:44 +08:00
|
|
|
|
for (int i = 0; i < ScheduleDimensions; i++)
|
2011-12-07 15:42:57 +08:00
|
|
|
|
if (i == DimToVectorize)
|
2015-05-22 03:02:44 +08:00
|
|
|
|
TilingMap =
|
|
|
|
|
isl_map_equate(TilingMap, isl_dim_in, i, isl_dim_out, PointDimension);
|
2011-12-07 15:42:57 +08:00
|
|
|
|
else
|
2015-05-22 03:02:44 +08:00
|
|
|
|
TilingMap = isl_map_equate(TilingMap, isl_dim_in, i, isl_dim_out, i);
|
2011-07-01 04:29:13 +08:00
|
|
|
|
|
2011-12-07 15:42:57 +08:00
|
|
|
|
// it % 'VectorWidth' = 0
|
|
|
|
|
LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
|
|
|
|
|
Aff = isl_aff_zero_on_domain(LocalSpaceRange);
|
|
|
|
|
Aff = isl_aff_set_constant_si(Aff, VectorWidth);
|
|
|
|
|
Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
|
2013-06-21 14:41:31 +08:00
|
|
|
|
VectorWidthMP = isl_val_int_from_si(ctx, VectorWidth);
|
|
|
|
|
Aff = isl_aff_mod_val(Aff, VectorWidthMP);
|
2011-12-07 15:42:57 +08:00
|
|
|
|
Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
|
|
|
|
|
TilingMap = isl_map_intersect_range(TilingMap, Modulo);
|
|
|
|
|
|
|
|
|
|
// it <= ip
|
2015-05-22 03:02:44 +08:00
|
|
|
|
TilingMap = isl_map_order_le(TilingMap, isl_dim_out, TileDimension,
|
|
|
|
|
isl_dim_out, PointDimension);
|
2011-07-01 04:29:13 +08:00
|
|
|
|
|
2011-12-07 15:42:57 +08:00
|
|
|
|
// ip <= it + ('VectorWidth' - 1)
|
2011-10-06 08:03:35 +08:00
|
|
|
|
c = isl_inequality_alloc(LocalSpace);
|
2011-12-07 15:42:57 +08:00
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
|
|
|
|
|
isl_constraint_set_constant_si(c, VectorWidth - 1);
|
|
|
|
|
TilingMap = isl_map_add_constraint(TilingMap, c);
|
2011-07-01 04:29:13 +08:00
|
|
|
|
|
2011-12-07 15:42:57 +08:00
|
|
|
|
return TilingMap;
|
2011-07-01 04:29:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 20:06:39 +08:00
|
|
|
|
isl_schedule_node *IslScheduleOptimizer::optimizeBand(isl_schedule_node *Node,
|
|
|
|
|
void *User) {
|
|
|
|
|
if (isl_schedule_node_get_type(Node) != isl_schedule_node_band)
|
|
|
|
|
return Node;
|
|
|
|
|
|
|
|
|
|
if (isl_schedule_node_n_children(Node) != 1)
|
|
|
|
|
return Node;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2015-03-22 20:06:39 +08:00
|
|
|
|
if (!isl_schedule_node_band_get_permutable(Node))
|
|
|
|
|
return Node;
|
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);
|
|
|
|
|
|
|
|
|
|
if (Dims <= 1) {
|
2011-10-06 08:03:35 +08:00
|
|
|
|
isl_space_free(Space);
|
2015-03-22 20:06:39 +08:00
|
|
|
|
return Node;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 20:06:39 +08:00
|
|
|
|
auto Child = isl_schedule_node_get_child(Node, 0);
|
|
|
|
|
auto Type = isl_schedule_node_get_type(Child);
|
|
|
|
|
isl_schedule_node_free(Child);
|
|
|
|
|
|
|
|
|
|
if (Type != isl_schedule_node_leaf) {
|
|
|
|
|
isl_space_free(Space);
|
|
|
|
|
return Node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto Sizes = isl_multi_val_zero(Space);
|
|
|
|
|
auto Ctx = isl_schedule_node_get_ctx(Node);
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Dims; i++) {
|
|
|
|
|
auto tileSize = TileSizes.size() > i ? TileSizes[i] : DefaultTileSize;
|
|
|
|
|
Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize));
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 05:52:21 +08:00
|
|
|
|
isl_schedule_node *Res;
|
|
|
|
|
|
|
|
|
|
if (DisableTiling) {
|
|
|
|
|
isl_multi_val_free(Sizes);
|
|
|
|
|
Res = Node;
|
|
|
|
|
} else {
|
|
|
|
|
Res = isl_schedule_node_band_tile(Node, Sizes);
|
|
|
|
|
}
|
2015-03-22 20:06:39 +08:00
|
|
|
|
|
|
|
|
|
if (PollyVectorizerChoice == VECTORIZER_NONE)
|
|
|
|
|
return Res;
|
|
|
|
|
|
|
|
|
|
Child = isl_schedule_node_get_child(Res, 0);
|
|
|
|
|
auto ChildSchedule = isl_schedule_node_band_get_partial_schedule(Child);
|
|
|
|
|
|
|
|
|
|
for (int i = Dims - 1; i >= 0; i--) {
|
|
|
|
|
if (isl_schedule_node_band_member_get_coincident(Child, i)) {
|
|
|
|
|
auto TileMap = IslScheduleOptimizer::getPrevectorMap(Ctx, i, Dims);
|
|
|
|
|
auto TileUMap = isl_union_map_from_map(TileMap);
|
|
|
|
|
auto ChildSchedule2 = isl_union_map_apply_range(
|
|
|
|
|
isl_union_map_from_multi_union_pw_aff(ChildSchedule), TileUMap);
|
|
|
|
|
ChildSchedule = isl_multi_union_pw_aff_from_union_map(ChildSchedule2);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isl_schedule_node_free(Res);
|
|
|
|
|
Res = isl_schedule_node_delete(Child);
|
|
|
|
|
Res = isl_schedule_node_insert_partial_schedule(Res, ChildSchedule);
|
|
|
|
|
return Res;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 17:33:13 +08:00
|
|
|
|
__isl_give isl_schedule *
|
|
|
|
|
IslScheduleOptimizer::addPostTransforms(__isl_take isl_schedule *Schedule) {
|
2015-03-22 20:06:39 +08:00
|
|
|
|
isl_schedule_node *Root = isl_schedule_get_root(Schedule);
|
2015-07-14 17:33:13 +08:00
|
|
|
|
isl_schedule_free(Schedule);
|
2015-05-28 21:32:11 +08:00
|
|
|
|
Root = isl_schedule_node_map_descendant_bottom_up(
|
2015-03-22 20:06:39 +08:00
|
|
|
|
Root, IslScheduleOptimizer::optimizeBand, NULL);
|
2015-07-14 17:33:13 +08:00
|
|
|
|
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-02-12 01:25:09 +08:00
|
|
|
|
bool IslScheduleOptimizer::isProfitableSchedule(
|
|
|
|
|
Scop &S, __isl_keep isl_union_map *NewSchedule) {
|
|
|
|
|
// 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)
|
|
|
|
|
isl_union_map *OldSchedule = S.getSchedule();
|
|
|
|
|
bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule);
|
|
|
|
|
isl_union_map_free(OldSchedule);
|
|
|
|
|
return changed;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 08:43:48 +08:00
|
|
|
|
const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
|
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
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 22:02:40 +08:00
|
|
|
|
isl_union_set *Domain = 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;
|
|
|
|
|
|
2015-03-05 08:43:48 +08:00
|
|
|
|
isl_union_map *Validity = D.getDependences(ValidityKinds);
|
|
|
|
|
isl_union_map *Proximity = 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") {
|
2012-02-14 22:02:44 +08:00
|
|
|
|
Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain));
|
|
|
|
|
Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain));
|
2013-03-23 09:05:07 +08:00
|
|
|
|
Proximity =
|
|
|
|
|
isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain));
|
2012-02-14 22:02:44 +08:00
|
|
|
|
Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(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: ");
|
2014-10-23 07:16:28 +08:00
|
|
|
|
DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n");
|
|
|
|
|
DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n");
|
|
|
|
|
DEBUG(dbgs() << "Validity := " << stringFromIslObj(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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 00:45:40 +08:00
|
|
|
|
isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs);
|
2012-01-31 03:38:54 +08:00
|
|
|
|
isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands);
|
2012-02-20 16:41:15 +08:00
|
|
|
|
isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm);
|
2012-02-20 16:41:47 +08:00
|
|
|
|
isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient);
|
2015-03-31 15:52:36 +08:00
|
|
|
|
isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0);
|
2012-01-31 03:38:47 +08:00
|
|
|
|
|
|
|
|
|
isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE);
|
2014-01-27 03:36:28 +08:00
|
|
|
|
|
|
|
|
|
isl_schedule_constraints *ScheduleConstraints;
|
|
|
|
|
ScheduleConstraints = isl_schedule_constraints_on_domain(Domain);
|
|
|
|
|
ScheduleConstraints =
|
|
|
|
|
isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity);
|
|
|
|
|
ScheduleConstraints = isl_schedule_constraints_set_validity(
|
|
|
|
|
ScheduleConstraints, isl_union_map_copy(Validity));
|
|
|
|
|
ScheduleConstraints =
|
|
|
|
|
isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity);
|
2012-02-14 22:02:44 +08:00
|
|
|
|
isl_schedule *Schedule;
|
2014-01-27 03:36:28 +08:00
|
|
|
|
Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints);
|
2012-01-31 03:38:47 +08:00
|
|
|
|
isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT);
|
|
|
|
|
|
|
|
|
|
// 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({
|
|
|
|
|
auto *P = isl_printer_to_str(S.getIslCtx());
|
|
|
|
|
P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
|
|
|
|
|
P = isl_printer_print_schedule(P, Schedule);
|
|
|
|
|
dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n";
|
|
|
|
|
isl_printer_free(P);
|
|
|
|
|
});
|
2012-02-20 16:41:21 +08:00
|
|
|
|
|
2015-07-14 17:33:13 +08:00
|
|
|
|
isl_schedule *NewSchedule = addPostTransforms(Schedule);
|
|
|
|
|
isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule);
|
2015-02-12 01:25:09 +08:00
|
|
|
|
|
2015-07-14 17:33:13 +08:00
|
|
|
|
if (!isProfitableSchedule(S, NewScheduleMap)) {
|
|
|
|
|
isl_union_map_free(NewScheduleMap);
|
|
|
|
|
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
|
|
|
|
|
2015-07-14 17:33:13 +08:00
|
|
|
|
isl_union_map_free(NewScheduleMap);
|
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>();
|
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);
|
2013-03-23 09:05:07 +08:00
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopInfo);
|
|
|
|
|
INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
|
|
|
|
|
"Polly - Optimize schedule of SCoP", false, false)
|