diff options
-rw-r--r-- | polly/include/polly/LinkAllPasses.h | 2 | ||||
-rwxr-xr-x | polly/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | polly/lib/ScheduleOptimizer.cpp | 251 | ||||
-rwxr-xr-x | polly/utils/pollycc | 15 |
4 files changed, 269 insertions, 0 deletions
diff --git a/polly/include/polly/LinkAllPasses.h b/polly/include/polly/LinkAllPasses.h index 8fd2db8cc5c..fe230c33b93 100644 --- a/polly/include/polly/LinkAllPasses.h +++ b/polly/include/polly/LinkAllPasses.h @@ -43,6 +43,7 @@ namespace polly { Pass *createJSONImporterPass(); Pass *createRegionSimplifyPass(); Pass *createScopInfoPass(); + Pass *createScheduleOptimizerPass(); #ifdef OPENSCOP_FOUND Pass *createScopExporterPass(); @@ -87,6 +88,7 @@ namespace { createJSONImporterPass(); createRegionSimplifyPass(); createScopInfoPass(); + createScheduleOptimizerPass(); #ifdef OPENSCOP_FOUND createScopExporterPass(); diff --git a/polly/lib/CMakeLists.txt b/polly/lib/CMakeLists.txt index b5f80219b3e..57c22915ed4 100755 --- a/polly/lib/CMakeLists.txt +++ b/polly/lib/CMakeLists.txt @@ -32,6 +32,7 @@ add_polly_library(LLVMPolly MayAliasSet.cpp Pocc.cpp RegionSimplify.cpp + ScheduleOptimizer.cpp Exchange/JSONExporter.cpp ${POLLY_EXCHANGE_FILES} ${POLLY_SCOPLIB_FILES} diff --git a/polly/lib/ScheduleOptimizer.cpp b/polly/lib/ScheduleOptimizer.cpp new file mode 100644 index 00000000000..2227a00f94e --- /dev/null +++ b/polly/lib/ScheduleOptimizer.cpp @@ -0,0 +1,251 @@ +//===- 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. +//===----------------------------------------------------------------------===// + +#include "polly/Cloog.h" +#include "polly/LinkAllPasses.h" + +#include "polly/Dependences.h" +#include "polly/ScopInfo.h" + +#include "isl/dim.h" +#include "isl/map.h" +#include "isl/constraint.h" +#include "isl/schedule.h" + +#define DEBUG_TYPE "polly-optimize-isl" +#include "llvm/Support/Debug.h" + +using namespace llvm; +using namespace polly; + +namespace { + + class ScheduleOptimizer : public ScopPass { + + public: + static char ID; + explicit ScheduleOptimizer() : ScopPass(ID) {} + + virtual bool runOnScop(Scop &S); + void printScop(llvm::raw_ostream &OS) const; + void getAnalysisUsage(AnalysisUsage &AU) const; + }; + +} + +char ScheduleOptimizer::ID = 0; + +static int getSingleMap(__isl_take isl_map *map, void *user) { + isl_map **singleMap = (isl_map **) user; + *singleMap = map; + + return 0; +} + +void extendScattering(Scop &S, unsigned scatDimensions) { + for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { + ScopStmt *stmt = *SI; + + if (stmt->isFinalRead()) + continue; + + isl_map *scattering = stmt->getScattering(); + isl_dim *dim = isl_dim_alloc(isl_map_get_ctx(scattering), + isl_map_n_param(scattering), + isl_map_n_out(scattering), + scatDimensions); + isl_basic_map *changeScattering = isl_basic_map_universe(isl_dim_copy(dim)); + + for (unsigned i = 0; i < isl_map_n_out(scattering); i++) { + isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim)); + isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1); + isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1); + changeScattering = isl_basic_map_add_constraint(changeScattering, c); + } + + for (unsigned i = isl_map_n_out(scattering); i < scatDimensions; i++) { + isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim)); + isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); + changeScattering = isl_basic_map_add_constraint(changeScattering, c); + } + + isl_map *changeScatteringMap = isl_map_from_basic_map(changeScattering); + + stmt->setScattering(isl_map_apply_range(scattering, changeScatteringMap)); + } +} + +// @brief Tile a band. +// +// This function recieves a map that assigns to the instances of a statement +// an execution time. +// +// [i_0, i_1, i_2] -> [o_0, o_1, o_2, i_0, i_1, i_2]: +// o_0 % 32 = 0 and o_1 % 32 = 0 and o_2 % 32 = 0 +// and o0 <= i0 <= o0 + 32 and o1 <= i1 <= o1 + 32 and o2 <= i2 <= o2 + 32 + +isl_map *tileBand(isl_map *band) { + int dimensions = isl_map_n_out(band); + int tileSize = 32; + + isl_dim *dim = isl_dim_alloc(isl_map_get_ctx(band), isl_map_n_param(band), + dimensions, dimensions * 3); + isl_basic_map *tiledBand = isl_basic_map_universe(isl_dim_copy(dim)); + + for (int i = 0; i < dimensions; i++) { + isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim)); + isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); + isl_constraint_set_coefficient_si(c, isl_dim_out, 2 * dimensions + i, + -tileSize); + tiledBand = isl_basic_map_add_constraint(tiledBand, c); + + + c = isl_equality_alloc(isl_dim_copy(dim)); + isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1); + isl_constraint_set_coefficient_si(c, isl_dim_out, dimensions + i, 1); + tiledBand = isl_basic_map_add_constraint(tiledBand, c); + + c = isl_inequality_alloc(isl_dim_copy(dim)); + isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1); + isl_constraint_set_coefficient_si(c, isl_dim_out, dimensions + i, 1); + tiledBand = isl_basic_map_add_constraint(tiledBand, c); + + c = isl_inequality_alloc(isl_dim_copy(dim)); + isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); + isl_constraint_set_coefficient_si(c, isl_dim_out, dimensions + i, -1); + isl_constraint_set_constant_si(c, tileSize - 1); + tiledBand = isl_basic_map_add_constraint(tiledBand, c); + } + + // Project out auxilary dimensions (introduced to ensure 'ii % tileSize = 0') + // + // The real dimensions are transformed into existentially quantified ones. + // This reduces the number of visible scattering dimensions. Also, Cloog + // produces better code, if auxilary dimensions are existentially quantified. + tiledBand = isl_basic_map_project_out(tiledBand, isl_dim_out, 2 * dimensions, + dimensions); + + return isl_map_apply_range(band, isl_map_from_basic_map(tiledBand)); +} + +bool ScheduleOptimizer::runOnScop(Scop &S) { + Dependences *D = &getAnalysis<Dependences>(); + + // Build input data. + int dependencyKinds = Dependences::TYPE_RAW + | Dependences::TYPE_WAR + | Dependences::TYPE_WAW; + + isl_union_map *validity = D->getDependences(dependencyKinds); + isl_union_map *proximity = D->getDependences(dependencyKinds); + isl_union_set *domain = NULL; + + for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) + if ((*SI)->isFinalRead()) + continue; + else if (!domain) + domain = isl_union_set_from_set((*SI)->getDomain()); + else + domain = isl_union_set_union(domain, + isl_union_set_from_set((*SI)->getDomain())); + + if (!domain) + return false; + + DEBUG(dbgs() << "\n\nCompute schedule from: "); + DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n"); + DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity); + dbgs() << ";\n"); + DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity); + dbgs() << ";\n"); + + isl_schedule *schedule; + + schedule = isl_union_set_compute_schedule(domain, validity, proximity); + + // Get the complete schedule. + isl_union_map *scheduleMap = isl_schedule_get_map(schedule); + + DEBUG(dbgs() << "Computed schedule: "); + DEBUG(isl_union_map_dump(scheduleMap)); + DEBUG(dbgs() << "Individual bands: "); + + // Get individual tileable bands. + for (int i = 0; i < isl_schedule_n_band(schedule); i++) { + isl_union_map *band = isl_schedule_get_band(schedule, i); + + DEBUG(dbgs() << "Band " << i << ": "); + DEBUG(isl_union_map_dump(band)); + + for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { + ScopStmt *stmt = *SI; + + if (stmt->isFinalRead()) + continue; + + isl_set *domain = stmt->getDomain(); + isl_union_map *stmtBand; + stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(band), + isl_union_set_from_set(domain)); + + isl_map *sband; + isl_union_map_foreach_map(stmtBand, getSingleMap, &sband); + + sband = tileBand(sband); + DEBUG(dbgs() << "tiled band: "); + DEBUG(isl_map_dump(sband)); + + if (i == 0) + stmt->setScattering(sband); + else { + isl_map *scattering = stmt->getScattering(); + scattering = isl_map_range_product(scattering, sband); + scattering = isl_map_flatten(scattering); + stmt->setScattering(scattering); + } + } + + } + + unsigned maxScatDims = 0; + + for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) + maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims); + + extendScattering(S, maxScatDims); + isl_schedule_free(schedule); + return false; +} + +void ScheduleOptimizer::printScop(raw_ostream &OS) const { +} + +void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { + ScopPass::getAnalysisUsage(AU); + AU.addRequired<Dependences>(); +} + +static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl", + "Polly - Calculate optimized " + "schedules using the isl schedule " + "calculator"); + +Pass* polly::createScheduleOptimizerPass() { + return new ScheduleOptimizer(); +} diff --git a/polly/utils/pollycc b/polly/utils/pollycc index e061ef3b54e..e9999ae6214 100755 --- a/polly/utils/pollycc +++ b/polly/utils/pollycc @@ -94,6 +94,12 @@ def createAssemblyFile(tmpDir, file, number, args, polly): if args.fpluto: commandLine.append('-polly-optimize') + if args.foptimize: + commandLine.append('-polly-optimize-isl') + + if args.fatLeastOnce: + commandLine.append('-enable-polly-atLeastOnce') + if args.faligned: commandLine.append('-enable-polly-aligned') @@ -232,8 +238,14 @@ def parseArguments(): parser.add_argument('-fpolly', help='enable polly', action='store_true') parser.add_argument('-fpluto', help='enable pluto', action='store_true') parser.add_argument('-fpluto-fuse', dest='pluto_fuse', help='enable pluto') + parser.add_argument('-foptimize', + help='Optimize schedule with isl pluto like algorithm', + action='store_true') parser.add_argument('-faligned', help='Assume aligned vector accesses', action='store_true') + parser.add_argument('-fatLeastOnce', + help='Assume all loops are executed at least once', + action='store_true') parser.add_argument('-fview-scops', dest='view', help='Show the scops with graphviz', action='store_true') @@ -285,6 +297,9 @@ def parseArguments(): if arguments.fpluto: arguments.fpolly = True + if arguments.foptimize: + arguments.fpolly = True + return arguments def createAssemblyFiles(files, args, pollyLib): |