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"
|
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"
|
2012-01-31 21:26:29 +08:00
|
|
|
|
#include "isl/schedule.h"
|
|
|
|
|
#include "isl/space.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
|
|
|
|
#include "polly/Dependences.h"
|
|
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
|
#include "polly/Options.h"
|
|
|
|
|
#include "polly/ScopInfo.h"
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-11-23 03:40:19 +08:00
|
|
|
|
#define DEBUG_TYPE "polly-opt-isl"
|
2011-05-15 03:02:06 +08:00
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace polly;
|
|
|
|
|
|
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>
|
|
|
|
|
DisableTiling("polly-no-tiling", cl::desc("Disable tiling in the scheduler"),
|
2013-05-07 15:31:10 +08:00
|
|
|
|
cl::location(polly::DisablePollyTiling), cl::init(false),
|
|
|
|
|
cl::cat(PollyCategory));
|
2011-10-24 04:59:26 +08:00
|
|
|
|
|
2012-02-14 22:02:48 +08:00
|
|
|
|
static cl::opt<std::string>
|
|
|
|
|
OptimizeDeps("polly-opt-optimize-only",
|
|
|
|
|
cl::desc("Only a certain kind of dependences (all/raw)"),
|
2013-05-07 15:31:10 +08:00
|
|
|
|
cl::Hidden, cl::init("all"), cl::cat(PollyCategory));
|
2012-02-14 22:02:48 +08:00
|
|
|
|
|
2012-01-31 03:38:43 +08:00
|
|
|
|
static cl::opt<std::string>
|
|
|
|
|
SimplifyDeps("polly-opt-simplify-deps",
|
2013-03-23 09:05:07 +08:00
|
|
|
|
cl::desc("Dependences should be simplified (yes/no)"), cl::Hidden,
|
2013-05-07 15:31:10 +08:00
|
|
|
|
cl::init("yes"), cl::cat(PollyCategory));
|
2012-01-31 03:38:43 +08:00
|
|
|
|
|
2012-02-20 16:41:15 +08:00
|
|
|
|
static cl::opt<int>
|
|
|
|
|
MaxConstantTerm("polly-opt-max-constant-term",
|
|
|
|
|
cl::desc("The maximal constant term allowed (-1 is unlimited)"),
|
2013-05-07 15:31:10 +08:00
|
|
|
|
cl::Hidden, cl::init(20), cl::cat(PollyCategory));
|
2012-02-20 16:41:15 +08:00
|
|
|
|
|
2012-02-20 16:41:47 +08:00
|
|
|
|
static cl::opt<int>
|
|
|
|
|
MaxCoefficient("polly-opt-max-coefficient",
|
|
|
|
|
cl::desc("The maximal coefficient allowed (-1 is unlimited)"),
|
2013-05-07 15:31:10 +08:00
|
|
|
|
cl::Hidden, cl::init(20), cl::cat(PollyCategory));
|
2012-02-20 16:41:47 +08:00
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
|
static cl::opt<std::string>
|
|
|
|
|
FusionStrategy("polly-opt-fusion",
|
|
|
|
|
cl::desc("The fusion strategy to choose (min/max)"), cl::Hidden,
|
2013-05-07 15:31:10 +08:00
|
|
|
|
cl::init("min"), cl::cat(PollyCategory));
|
2012-01-31 03:38:50 +08:00
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
|
static cl::opt<std::string>
|
|
|
|
|
MaximizeBandDepth("polly-opt-maximize-bands",
|
|
|
|
|
cl::desc("Maximize the band depth (yes/no)"), cl::Hidden,
|
2013-05-07 15:31:10 +08:00
|
|
|
|
cl::init("yes"), cl::cat(PollyCategory));
|
2012-01-31 03:38:54 +08:00
|
|
|
|
|
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;
|
|
|
|
|
explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = NULL; }
|
|
|
|
|
|
|
|
|
|
~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); }
|
|
|
|
|
|
|
|
|
|
virtual bool runOnScop(Scop &S);
|
|
|
|
|
void printScop(llvm::raw_ostream &OS) const;
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
isl_schedule *LastSchedule;
|
|
|
|
|
|
|
|
|
|
static void extendScattering(Scop &S, unsigned NewDimensions);
|
|
|
|
|
|
|
|
|
|
/// @brief Create a map that describes a n-dimensonal tiling.
|
|
|
|
|
///
|
|
|
|
|
/// getTileMap creates a map from a n-dimensional scattering space into an
|
|
|
|
|
/// 2*n-dimensional scattering space. The map describes a rectangular
|
|
|
|
|
/// tiling.
|
|
|
|
|
///
|
|
|
|
|
/// Example:
|
|
|
|
|
/// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
|
|
|
|
|
///
|
|
|
|
|
/// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
|
|
|
|
|
/// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
|
|
|
|
|
/// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
|
|
|
|
|
///
|
|
|
|
|
/// Before tiling:
|
|
|
|
|
///
|
|
|
|
|
/// for (i = 0; i < N; i++)
|
|
|
|
|
/// for (j = 0; j < M; j++)
|
|
|
|
|
/// S(i,j)
|
|
|
|
|
///
|
|
|
|
|
/// After tiling:
|
|
|
|
|
///
|
|
|
|
|
/// for (t_i = 0; t_i < N; i+=32)
|
|
|
|
|
/// for (t_j = 0; t_j < M; j+=32)
|
|
|
|
|
/// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
|
|
|
|
|
/// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
|
|
|
|
|
/// S(i,j)
|
|
|
|
|
///
|
|
|
|
|
static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
|
|
|
|
|
isl_space *SpaceModel, int tileSize = 32);
|
|
|
|
|
|
|
|
|
|
/// @brief Get the schedule for this band.
|
|
|
|
|
///
|
|
|
|
|
/// Polly applies transformations like tiling on top of the isl calculated
|
|
|
|
|
/// value. This can influence the number of scheduling dimension. The
|
|
|
|
|
/// number of schedule dimensions is returned in the parameter 'Dimension'.
|
|
|
|
|
static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions);
|
|
|
|
|
|
|
|
|
|
/// @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.
|
|
|
|
|
static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
|
|
|
|
|
int ScheduleDimensions, int VectorWidth = 4);
|
|
|
|
|
|
|
|
|
|
/// @brief Get the scheduling map for a list of bands.
|
|
|
|
|
///
|
|
|
|
|
/// Walk recursively the forest of bands to combine the schedules of the
|
|
|
|
|
/// individual bands to the overall schedule. In case tiling is requested,
|
|
|
|
|
/// the individual bands are tiled.
|
|
|
|
|
static isl_union_map *getScheduleForBandList(isl_band_list *BandList);
|
|
|
|
|
|
|
|
|
|
static isl_union_map *getScheduleMap(isl_schedule *Schedule);
|
|
|
|
|
|
|
|
|
|
bool doFinalization() {
|
|
|
|
|
isl_schedule_free(LastSchedule);
|
|
|
|
|
LastSchedule = NULL;
|
|
|
|
|
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
|
|
|
|
|
2012-04-25 21:22:43 +08:00
|
|
|
|
void IslScheduleOptimizer::extendScattering(Scop &S, unsigned NewDimensions) {
|
2011-05-15 03:02:06 +08:00
|
|
|
|
for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
|
2011-10-06 08:04:05 +08:00
|
|
|
|
ScopStmt *Stmt = *SI;
|
|
|
|
|
unsigned OldDimensions = Stmt->getNumScattering();
|
|
|
|
|
isl_space *Space;
|
2012-05-22 18:47:31 +08:00
|
|
|
|
isl_map *Map, *New;
|
2011-10-06 08:04:05 +08:00
|
|
|
|
|
|
|
|
|
Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
|
2012-05-22 18:47:31 +08:00
|
|
|
|
Map = isl_map_universe(Space);
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < OldDimensions; i++)
|
|
|
|
|
Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
|
|
|
|
|
|
|
|
|
|
for (unsigned i = OldDimensions; i < NewDimensions; i++)
|
|
|
|
|
Map = isl_map_fix_si(Map, isl_dim_out, i, 0);
|
|
|
|
|
|
|
|
|
|
Map = isl_map_align_params(Map, S.getParamSpace());
|
|
|
|
|
New = isl_map_apply_range(Stmt->getScattering(), Map);
|
|
|
|
|
Stmt->setScattering(New);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
|
isl_basic_map *IslScheduleOptimizer::getTileMap(isl_ctx *ctx,
|
|
|
|
|
int scheduleDimensions,
|
|
|
|
|
isl_space *SpaceModel,
|
|
|
|
|
int tileSize) {
|
2011-07-01 04:01:02 +08:00
|
|
|
|
// We construct
|
|
|
|
|
//
|
|
|
|
|
// tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
|
|
|
|
|
// s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
|
|
|
|
|
// s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
|
|
|
|
|
//
|
|
|
|
|
// and project out the auxilary dimensions a0 and a1.
|
2013-03-23 09:05:07 +08:00
|
|
|
|
isl_space *Space =
|
|
|
|
|
isl_space_alloc(ctx, 0, scheduleDimensions, scheduleDimensions * 3);
|
2011-10-06 08:03:35 +08:00
|
|
|
|
isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
|
|
|
|
|
|
|
|
|
|
isl_local_space *LocalSpace = isl_local_space_from_space(Space);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
for (int x = 0; x < scheduleDimensions; x++) {
|
|
|
|
|
int sX = x;
|
|
|
|
|
int tX = x;
|
|
|
|
|
int pX = scheduleDimensions + x;
|
|
|
|
|
int aX = 2 * scheduleDimensions + x;
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
isl_constraint *c;
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
// sX = aX * tileSize;
|
2011-10-06 08:03:35 +08:00
|
|
|
|
c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
|
2011-07-01 04:01:02 +08:00
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
|
|
|
|
|
tileMap = isl_basic_map_add_constraint(tileMap, c);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
// pX = sX;
|
2011-10-06 08:03:35 +08:00
|
|
|
|
c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
|
2011-07-01 04:01:02 +08:00
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
|
|
|
|
|
tileMap = isl_basic_map_add_constraint(tileMap, c);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
// tX <= pX
|
2011-10-06 08:03:35 +08:00
|
|
|
|
c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
|
2011-07-01 04:01:02 +08:00
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
|
|
|
|
|
tileMap = isl_basic_map_add_constraint(tileMap, c);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
// pX <= tX + (tileSize - 1)
|
2011-10-06 08:03:35 +08:00
|
|
|
|
c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
|
2011-07-01 04:01:02 +08:00
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
isl_constraint_set_constant_si(c, tileSize - 1);
|
2011-07-01 04:01:02 +08:00
|
|
|
|
tileMap = isl_basic_map_add_constraint(tileMap, c);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
// Project out auxilary dimensions.
|
2011-05-15 03:02:06 +08:00
|
|
|
|
//
|
2011-07-01 04:01:02 +08:00
|
|
|
|
// The auxilary dimensions are transformed into existentially quantified ones.
|
|
|
|
|
// This reduces the number of visible scattering dimensions and allows Cloog
|
|
|
|
|
// to produces better code.
|
2013-03-23 09:05:07 +08:00
|
|
|
|
tileMap = isl_basic_map_project_out(
|
|
|
|
|
tileMap, isl_dim_out, 2 * scheduleDimensions, scheduleDimensions);
|
2011-10-06 08:03:35 +08:00
|
|
|
|
isl_local_space_free(LocalSpace);
|
2011-07-01 04:01:02 +08:00
|
|
|
|
return tileMap;
|
|
|
|
|
}
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
|
isl_union_map *IslScheduleOptimizer::getScheduleForBand(isl_band *Band,
|
|
|
|
|
int *Dimensions) {
|
2011-11-17 20:56:03 +08:00
|
|
|
|
isl_union_map *PartialSchedule;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
isl_ctx *ctx;
|
2011-10-06 08:03:35 +08:00
|
|
|
|
isl_space *Space;
|
2011-11-17 20:56:03 +08:00
|
|
|
|
isl_basic_map *TileMap;
|
|
|
|
|
isl_union_map *TileUMap;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2011-11-17 20:56:03 +08:00
|
|
|
|
PartialSchedule = isl_band_get_partial_schedule(Band);
|
2011-12-08 21:02:58 +08:00
|
|
|
|
*Dimensions = isl_band_n_member(Band);
|
2011-10-24 04:59:26 +08:00
|
|
|
|
|
2011-11-17 20:56:00 +08:00
|
|
|
|
if (DisableTiling)
|
2011-11-17 20:56:03 +08:00
|
|
|
|
return PartialSchedule;
|
2011-11-17 20:56:00 +08:00
|
|
|
|
|
2011-12-08 21:02:58 +08:00
|
|
|
|
// It does not make any sense to tile a band with just one dimension.
|
|
|
|
|
if (*Dimensions == 1)
|
|
|
|
|
return PartialSchedule;
|
|
|
|
|
|
2011-11-17 20:56:03 +08:00
|
|
|
|
ctx = isl_union_map_get_ctx(PartialSchedule);
|
|
|
|
|
Space = isl_union_map_get_space(PartialSchedule);
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2011-12-08 21:02:58 +08:00
|
|
|
|
TileMap = getTileMap(ctx, *Dimensions, Space);
|
2011-11-17 20:56:03 +08:00
|
|
|
|
TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
|
|
|
|
|
TileUMap = isl_union_map_align_params(TileUMap, Space);
|
2011-12-08 21:02:58 +08:00
|
|
|
|
*Dimensions = 2 * *Dimensions;
|
|
|
|
|
|
2011-11-17 20:56:03 +08:00
|
|
|
|
return isl_union_map_apply_range(PartialSchedule, TileUMap);
|
2011-07-01 04:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
|
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.
|
|
|
|
|
for (int i = 0; i < ScheduleDimensions; i++) {
|
2011-10-06 08:03:35 +08:00
|
|
|
|
c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
|
2011-07-01 04:29:13 +08:00
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
|
|
|
|
|
|
2011-12-07 15:42:57 +08:00
|
|
|
|
if (i == DimToVectorize)
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
|
|
|
|
|
else
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
|
2011-07-01 04:29:13 +08:00
|
|
|
|
|
2011-12-07 15:42:57 +08:00
|
|
|
|
TilingMap = isl_map_add_constraint(TilingMap, c);
|
|
|
|
|
}
|
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
|
2011-10-06 08:03:35 +08:00
|
|
|
|
c = isl_inequality_alloc(isl_local_space_copy(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);
|
|
|
|
|
TilingMap = isl_map_add_constraint(TilingMap, c);
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
|
isl_union_map *
|
|
|
|
|
IslScheduleOptimizer::getScheduleForBandList(isl_band_list *BandList) {
|
2011-11-17 20:56:03 +08:00
|
|
|
|
int NumBands;
|
|
|
|
|
isl_union_map *Schedule;
|
|
|
|
|
isl_ctx *ctx;
|
|
|
|
|
|
|
|
|
|
ctx = isl_band_list_get_ctx(BandList);
|
|
|
|
|
NumBands = isl_band_list_n_band(BandList);
|
2011-11-17 20:56:04 +08:00
|
|
|
|
Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
|
2011-11-17 20:56:03 +08:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < NumBands; i++) {
|
|
|
|
|
isl_band *Band;
|
|
|
|
|
isl_union_map *PartialSchedule;
|
|
|
|
|
int ScheduleDimensions;
|
|
|
|
|
isl_space *Space;
|
|
|
|
|
|
|
|
|
|
Band = isl_band_list_get_band(BandList, i);
|
2011-12-08 21:02:58 +08:00
|
|
|
|
PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions);
|
2011-11-17 20:56:03 +08:00
|
|
|
|
Space = isl_union_map_get_space(PartialSchedule);
|
|
|
|
|
|
|
|
|
|
if (isl_band_has_children(Band)) {
|
|
|
|
|
isl_band_list *Children;
|
|
|
|
|
isl_union_map *SuffixSchedule;
|
|
|
|
|
|
|
|
|
|
Children = isl_band_get_children(Band);
|
|
|
|
|
SuffixSchedule = getScheduleForBandList(Children);
|
2013-03-23 09:05:07 +08:00
|
|
|
|
PartialSchedule =
|
|
|
|
|
isl_union_map_flat_range_product(PartialSchedule, SuffixSchedule);
|
2011-11-17 20:56:03 +08:00
|
|
|
|
isl_band_list_free(Children);
|
2012-05-06 18:22:19 +08:00
|
|
|
|
} else if (PollyVectorizerChoice != VECTORIZER_NONE) {
|
2013-03-23 09:05:07 +08:00
|
|
|
|
for (int j = 0; j < isl_band_n_member(Band); j++) {
|
2014-01-27 03:36:28 +08:00
|
|
|
|
if (isl_band_member_is_coincident(Band, j)) {
|
2011-11-17 20:56:03 +08:00
|
|
|
|
isl_map *TileMap;
|
|
|
|
|
isl_union_map *TileUMap;
|
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
|
TileMap = getPrevectorMap(ctx, ScheduleDimensions - j - 1,
|
2012-04-16 19:06:06 +08:00
|
|
|
|
ScheduleDimensions);
|
2013-03-23 09:05:07 +08:00
|
|
|
|
TileUMap = isl_union_map_from_map(TileMap);
|
|
|
|
|
TileUMap =
|
|
|
|
|
isl_union_map_align_params(TileUMap, isl_space_copy(Space));
|
|
|
|
|
PartialSchedule =
|
|
|
|
|
isl_union_map_apply_range(PartialSchedule, TileUMap);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-07-01 04:29:20 +08:00
|
|
|
|
}
|
2011-07-01 04:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 20:56:04 +08:00
|
|
|
|
Schedule = isl_union_map_union(Schedule, PartialSchedule);
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2011-11-17 20:56:03 +08:00
|
|
|
|
isl_band_free(Band);
|
2011-10-06 08:03:35 +08:00
|
|
|
|
isl_space_free(Space);
|
2011-07-01 04:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 20:56:03 +08:00
|
|
|
|
return Schedule;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-25 21:22:43 +08:00
|
|
|
|
isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) {
|
2011-11-17 20:56:03 +08:00
|
|
|
|
isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
|
|
|
|
|
isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
|
|
|
|
|
isl_band_list_free(BandList);
|
|
|
|
|
return ScheduleMap;
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
|
bool IslScheduleOptimizer::runOnScop(Scop &S) {
|
2011-05-15 03:02:06 +08:00
|
|
|
|
Dependences *D = &getAnalysis<Dependences>();
|
|
|
|
|
|
2014-02-23 23:15:44 +08:00
|
|
|
|
if (!D->hasValidDependences())
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-10-16 15:29:19 +08:00
|
|
|
|
isl_schedule_free(LastSchedule);
|
|
|
|
|
LastSchedule = NULL;
|
|
|
|
|
|
2011-05-15 03:02:06 +08:00
|
|
|
|
// Build input data.
|
2013-03-23 09:05:07 +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")
|
2013-03-23 09:05:07 +08:00
|
|
|
|
ProximityKinds =
|
|
|
|
|
Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW;
|
2012-02-14 22:02:48 +08:00
|
|
|
|
else if (OptimizeDeps == "raw")
|
2012-02-15 17:58:42 +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";
|
|
|
|
|
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;
|
|
|
|
|
|
2012-03-16 19:51:41 +08:00
|
|
|
|
isl_union_map *Validity = D->getDependences(ValidityKinds);
|
|
|
|
|
isl_union_map *Proximity = D->getDependences(ProximityKinds);
|
|
|
|
|
|
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: ");
|
2012-02-14 07:31:39 +08:00
|
|
|
|
DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n");
|
|
|
|
|
DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
dbgs() << ";\n");
|
2012-02-14 07:31:39 +08:00
|
|
|
|
DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
dbgs() << ";\n");
|
|
|
|
|
|
2012-01-31 03:38:50 +08:00
|
|
|
|
int IslFusionStrategy;
|
|
|
|
|
|
|
|
|
|
if (FusionStrategy == "max") {
|
|
|
|
|
IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
|
|
|
|
|
} else if (FusionStrategy == "min") {
|
|
|
|
|
IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN;
|
|
|
|
|
} else {
|
|
|
|
|
errs() << "warning: Unknown fusion strategy. Falling back to maximal "
|
|
|
|
|
"fusion.\n";
|
|
|
|
|
IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-31 03:38:50 +08:00
|
|
|
|
isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy);
|
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);
|
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
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
|
DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule); dbgs() << ";\n");
|
2012-02-20 16:41:21 +08:00
|
|
|
|
|
2012-02-14 07:31:39 +08:00
|
|
|
|
isl_union_map *ScheduleMap = getScheduleMap(Schedule);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2011-07-01 04:01:02 +08:00
|
|
|
|
for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
|
2012-02-14 07:31:39 +08:00
|
|
|
|
ScopStmt *Stmt = *SI;
|
2013-04-11 13:55:13 +08:00
|
|
|
|
isl_map *StmtSchedule;
|
2012-02-14 07:31:39 +08:00
|
|
|
|
isl_set *Domain = Stmt->getDomain();
|
|
|
|
|
isl_union_map *StmtBand;
|
|
|
|
|
StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
|
2013-03-23 09:05:07 +08:00
|
|
|
|
isl_union_set_from_set(Domain));
|
2013-04-11 13:55:13 +08:00
|
|
|
|
if (isl_union_map_is_empty(StmtBand)) {
|
2014-02-22 04:51:36 +08:00
|
|
|
|
StmtSchedule = isl_map_from_domain(isl_set_empty(Stmt->getDomainSpace()));
|
2013-04-11 13:55:13 +08:00
|
|
|
|
isl_union_map_free(StmtBand);
|
|
|
|
|
} else {
|
|
|
|
|
assert(isl_union_map_n_map(StmtBand) == 1);
|
|
|
|
|
StmtSchedule = isl_map_from_union_map(StmtBand);
|
2013-04-11 06:48:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 07:31:39 +08:00
|
|
|
|
Stmt->setScattering(StmtSchedule);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 20:56:03 +08:00
|
|
|
|
isl_union_map_free(ScheduleMap);
|
2012-10-16 15:29:19 +08:00
|
|
|
|
LastSchedule = Schedule;
|
2011-07-01 04:01:02 +08:00
|
|
|
|
|
2012-02-14 07:31:39 +08:00
|
|
|
|
unsigned MaxScatDims = 0;
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
|
|
|
|
for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
|
2012-02-14 07:31:39 +08:00
|
|
|
|
MaxScatDims = std::max((*SI)->getNumScattering(), MaxScatDims);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
|
2012-02-14 07:31:39 +08:00
|
|
|
|
extendScattering(S, MaxScatDims);
|
2011-05-15 03:02:06 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
|
void IslScheduleOptimizer::printScop(raw_ostream &OS) 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);
|
|
|
|
|
AU.addRequired<Dependences>();
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(Dependences);
|
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopInfo);
|
|
|
|
|
INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
|
|
|
|
|
"Polly - Optimize schedule of SCoP", false, false)
|