2012-10-03 03:50:38 +08:00
|
|
|
//===- IslAst.cpp - isl code generator interface --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-08-15 17:34:33 +08:00
|
|
|
// The isl code generator interface takes a Scop and generates an isl_ast. This
|
2012-10-03 03:50:38 +08:00
|
|
|
// ist_ast can either be returned directly or it can be pretty printed to
|
|
|
|
// stdout.
|
|
|
|
//
|
|
|
|
// A typical isl_ast output looks like this:
|
|
|
|
//
|
|
|
|
// for (c2 = max(0, ceild(n + m, 2); c2 <= min(511, floord(5 * n, 3)); c2++) {
|
|
|
|
// bb2(c2);
|
|
|
|
// }
|
|
|
|
//
|
2015-08-15 17:34:33 +08:00
|
|
|
// An in-depth discussion of our AST generation approach can be found in:
|
|
|
|
//
|
|
|
|
// Polyhedral AST generation is more than scanning polyhedra
|
|
|
|
// Tobias Grosser, Sven Verdoolaege, Albert Cohen
|
2017-06-08 20:06:15 +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
|
|
|
|
//
|
2012-10-03 03:50:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/CodeGen/IslAst.h"
|
2015-12-21 20:38:56 +08:00
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
2015-03-05 06:43:40 +08:00
|
|
|
#include "polly/DependenceInfo.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "polly/LinkAllPasses.h"
|
2013-05-07 15:31:10 +08:00
|
|
|
#include "polly/Options.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "polly/ScopDetection.h"
|
2012-10-03 03:50:38 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "polly/ScopPass.h"
|
2014-10-23 07:16:28 +08:00
|
|
|
#include "polly/Support/GICHelper.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-10-03 03:50:38 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/aff.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "isl/ast.h"
|
2012-10-03 03:50:38 +08:00
|
|
|
#include "isl/ast_build.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "isl/id.h"
|
|
|
|
#include "isl/isl-noexceptions.h"
|
2012-10-03 03:50:38 +08:00
|
|
|
#include "isl/map.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "isl/printer.h"
|
|
|
|
#include "isl/schedule.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/set.h"
|
|
|
|
#include "isl/union_map.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "isl/val.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2017-07-11 04:13:06 +08:00
|
|
|
#include <utility>
|
|
|
|
|
2014-07-24 04:17:28 +08:00
|
|
|
#define DEBUG_TYPE "polly-ast"
|
|
|
|
|
2012-10-03 03:50:38 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2014-07-24 04:17:28 +08:00
|
|
|
using IslAstUserPayload = IslAstInfo::IslAstUserPayload;
|
2014-04-22 11:30:19 +08:00
|
|
|
|
2014-11-07 03:35:21 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
PollyParallel("polly-parallel",
|
2014-11-07 05:25:54 +08:00
|
|
|
cl::desc("Generate thread parallel code (isl codegen only)"),
|
|
|
|
cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2014-11-07 03:35:21 +08:00
|
|
|
|
2017-07-11 04:13:06 +08:00
|
|
|
static cl::opt<bool> PrintAccesses("polly-ast-print-accesses",
|
|
|
|
cl::desc("Print memory access functions"),
|
|
|
|
cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
2014-11-16 22:24:53 +08:00
|
|
|
static cl::opt<bool> PollyParallelForce(
|
|
|
|
"polly-parallel-force",
|
2014-11-17 05:03:32 +08:00
|
|
|
cl::desc(
|
|
|
|
"Force generation of thread parallel code ignoring any cost model"),
|
2014-11-16 22:24:53 +08:00
|
|
|
cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
static cl::opt<bool> UseContext("polly-ast-use-context",
|
|
|
|
cl::desc("Use context"), cl::Hidden,
|
2016-11-10 17:39:58 +08:00
|
|
|
cl::init(true), cl::ZeroOrMore,
|
2013-05-07 15:31:10 +08:00
|
|
|
cl::cat(PollyCategory));
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
static cl::opt<bool> DetectParallel("polly-ast-detect-parallel",
|
|
|
|
cl::desc("Detect parallelism"), cl::Hidden,
|
2013-05-07 15:31:10 +08:00
|
|
|
cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
2012-12-13 14:24:06 +08:00
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
STATISTIC(ScopsProcessed, "Number of SCoPs processed");
|
|
|
|
STATISTIC(ScopsBeneficial, "Number of beneficial SCoPs");
|
|
|
|
STATISTIC(BeneficialAffineLoops, "Number of beneficial affine loops");
|
|
|
|
STATISTIC(BeneficialBoxedLoops, "Number of beneficial boxed loops");
|
|
|
|
|
|
|
|
STATISTIC(NumForLoops, "Number of for-loops");
|
|
|
|
STATISTIC(NumParallel, "Number of parallel for-loops");
|
|
|
|
STATISTIC(NumInnermostParallel, "Number of innermost parallel for-loops");
|
|
|
|
STATISTIC(NumOutermostParallel, "Number of outermost parallel for-loops");
|
|
|
|
STATISTIC(NumReductionParallel, "Number of reduction-parallel for-loops");
|
|
|
|
STATISTIC(NumExecutedInParallel, "Number of for-loops executed in parallel");
|
|
|
|
STATISTIC(NumIfConditions, "Number of if-conditions");
|
|
|
|
|
2016-03-24 21:20:52 +08:00
|
|
|
namespace polly {
|
2017-08-25 05:22:41 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Temporary information used when building the ast.
|
2012-12-13 14:24:06 +08:00
|
|
|
struct AstBuildUserInfo {
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Construct and initialize the helper struct for AST creation.
|
2017-08-25 05:22:41 +08:00
|
|
|
AstBuildUserInfo() = default;
|
2014-07-24 23:59:06 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// The dependence information used for the parallelism check.
|
2017-08-25 05:22:41 +08:00
|
|
|
const Dependences *Deps = nullptr;
|
2012-12-13 14:24:06 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Flag to indicate that we are inside a parallel for node.
|
2017-08-25 05:22:41 +08:00
|
|
|
bool InParallelFor = false;
|
2014-07-24 23:59:06 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// The last iterator id created for the current SCoP.
|
2017-08-25 05:22:41 +08:00
|
|
|
isl_id *LastForNodeId = nullptr;
|
2012-12-13 14:24:06 +08:00
|
|
|
};
|
2016-06-24 06:17:27 +08:00
|
|
|
} // namespace polly
|
2016-03-24 21:20:52 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Free an IslAstUserPayload object pointed to by @p Ptr.
|
2016-03-24 21:20:52 +08:00
|
|
|
static void freeIslAstUserPayload(void *Ptr) {
|
|
|
|
delete ((IslAstInfo::IslAstUserPayload *)Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
IslAstInfo::IslAstUserPayload::~IslAstUserPayload() {
|
|
|
|
isl_ast_build_free(Build);
|
|
|
|
isl_pw_aff_free(MinimalDependenceDistance);
|
|
|
|
}
|
2012-12-13 14:24:06 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Print a string @p str in a single line using @p Printer.
|
2014-08-01 05:33:49 +08:00
|
|
|
static isl_printer *printLine(__isl_take isl_printer *Printer,
|
2014-09-14 01:34:11 +08:00
|
|
|
const std::string &str,
|
|
|
|
__isl_keep isl_pw_aff *PWA = nullptr) {
|
2014-08-01 05:33:49 +08:00
|
|
|
Printer = isl_printer_start_line(Printer);
|
|
|
|
Printer = isl_printer_print_str(Printer, str.c_str());
|
2014-09-14 01:34:11 +08:00
|
|
|
if (PWA)
|
|
|
|
Printer = isl_printer_print_pw_aff(Printer, PWA);
|
2014-08-01 05:33:49 +08:00
|
|
|
return isl_printer_end_line(Printer);
|
2012-12-13 14:24:06 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Return all broken reductions as a string of clauses (OpenMP style).
|
2014-08-01 16:17:19 +08:00
|
|
|
static const std::string getBrokenReductionsStr(__isl_keep isl_ast_node *Node) {
|
|
|
|
IslAstInfo::MemoryAccessSet *BrokenReductions;
|
|
|
|
std::string str;
|
|
|
|
|
|
|
|
BrokenReductions = IslAstInfo::getBrokenReductions(Node);
|
|
|
|
if (!BrokenReductions || BrokenReductions->empty())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// Map each type of reduction to a comma separated list of the base addresses.
|
|
|
|
std::map<MemoryAccess::ReductionType, std::string> Clauses;
|
|
|
|
for (MemoryAccess *MA : *BrokenReductions)
|
|
|
|
if (MA->isWrite())
|
|
|
|
Clauses[MA->getReductionType()] +=
|
2017-02-09 16:06:15 +08:00
|
|
|
", " + MA->getScopArrayInfo()->getName();
|
2014-08-01 16:17:19 +08:00
|
|
|
|
|
|
|
// Now print the reductions sorted by type. Each type will cause a clause
|
|
|
|
// like: reduction (+ : sum0, sum1, sum2)
|
|
|
|
for (const auto &ReductionClause : Clauses) {
|
|
|
|
str += " reduction (";
|
|
|
|
str += MemoryAccess::getReductionOperatorStr(ReductionClause.first);
|
|
|
|
// Remove the first two symbols (", ") to make the output look pretty.
|
|
|
|
str += " : " + ReductionClause.second.substr(2) + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Callback executed for each for node in the ast in order to print it.
|
2014-08-01 05:33:49 +08:00
|
|
|
static isl_printer *cbPrintFor(__isl_take isl_printer *Printer,
|
|
|
|
__isl_take isl_ast_print_options *Options,
|
|
|
|
__isl_keep isl_ast_node *Node, void *) {
|
2014-09-14 01:34:11 +08:00
|
|
|
isl_pw_aff *DD = IslAstInfo::getMinimalDependenceDistance(Node);
|
2014-08-01 16:17:19 +08:00
|
|
|
const std::string BrokenReductionsStr = getBrokenReductionsStr(Node);
|
2014-11-07 03:35:21 +08:00
|
|
|
const std::string KnownParallelStr = "#pragma known-parallel";
|
2014-09-14 01:34:11 +08:00
|
|
|
const std::string DepDisPragmaStr = "#pragma minimal dependence distance: ";
|
2014-08-01 16:17:19 +08:00
|
|
|
const std::string SimdPragmaStr = "#pragma simd";
|
|
|
|
const std::string OmpPragmaStr = "#pragma omp parallel for";
|
2014-08-01 05:33:49 +08:00
|
|
|
|
2014-09-14 01:34:11 +08:00
|
|
|
if (DD)
|
|
|
|
Printer = printLine(Printer, DepDisPragmaStr, DD);
|
|
|
|
|
2014-08-01 16:17:19 +08:00
|
|
|
if (IslAstInfo::isInnermostParallel(Node))
|
|
|
|
Printer = printLine(Printer, SimdPragmaStr + BrokenReductionsStr);
|
2014-08-01 05:33:49 +08:00
|
|
|
|
2014-11-07 03:35:21 +08:00
|
|
|
if (IslAstInfo::isExecutedInParallel(Node))
|
|
|
|
Printer = printLine(Printer, OmpPragmaStr);
|
|
|
|
else if (IslAstInfo::isOutermostParallel(Node))
|
|
|
|
Printer = printLine(Printer, KnownParallelStr + BrokenReductionsStr);
|
2014-08-01 05:33:49 +08:00
|
|
|
|
2014-09-14 01:34:11 +08:00
|
|
|
isl_pw_aff_free(DD);
|
2014-08-01 05:33:49 +08:00
|
|
|
return isl_ast_node_for_print(Node, Printer, Options);
|
2012-12-13 14:24:06 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Check if the current scheduling dimension is parallel.
|
2014-07-15 08:00:35 +08:00
|
|
|
///
|
|
|
|
/// In case the dimension is parallel we also check if any reduction
|
|
|
|
/// dependences is broken when we exploit this parallelism. If so,
|
|
|
|
/// @p IsReductionParallel will be set to true. The reduction dependences we use
|
|
|
|
/// to check are actually the union of the transitive closure of the initial
|
2017-06-08 20:06:15 +08:00
|
|
|
/// reduction dependences together with their reversal. Even though these
|
2014-07-15 08:00:35 +08:00
|
|
|
/// dependences connect all iterations with each other (thus they are cyclic)
|
|
|
|
/// we can perform the parallelism check as we are only interested in a zero
|
|
|
|
/// (or non-zero) dependence distance on the dimension in question.
|
|
|
|
static bool astScheduleDimIsParallel(__isl_keep isl_ast_build *Build,
|
2015-03-05 08:43:48 +08:00
|
|
|
const Dependences *D,
|
2014-08-01 16:17:19 +08:00
|
|
|
IslAstUserPayload *NodeInfo) {
|
2014-07-15 08:00:35 +08:00
|
|
|
if (!D->hasValidDependences())
|
|
|
|
return false;
|
|
|
|
|
2014-07-28 11:46:28 +08:00
|
|
|
isl_union_map *Schedule = isl_ast_build_get_schedule(Build);
|
2015-03-05 08:43:48 +08:00
|
|
|
isl_union_map *Deps = D->getDependences(
|
|
|
|
Dependences::TYPE_RAW | Dependences::TYPE_WAW | Dependences::TYPE_WAR);
|
2014-09-14 01:34:11 +08:00
|
|
|
|
|
|
|
if (!D->isParallel(Schedule, Deps, &NodeInfo->MinimalDependenceDistance) &&
|
|
|
|
!isl_union_map_free(Schedule))
|
2014-07-15 08:00:35 +08:00
|
|
|
return false;
|
|
|
|
|
2015-03-05 08:43:48 +08:00
|
|
|
isl_union_map *RedDeps = D->getDependences(Dependences::TYPE_TC_RED);
|
2014-07-28 11:46:28 +08:00
|
|
|
if (!D->isParallel(Schedule, RedDeps))
|
2014-08-01 16:17:19 +08:00
|
|
|
NodeInfo->IsReductionParallel = true;
|
|
|
|
|
|
|
|
if (!NodeInfo->IsReductionParallel && !isl_union_map_free(Schedule))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Annotate reduction parallel nodes with the memory accesses which caused the
|
|
|
|
// reduction dependences parallel execution of the node conflicts with.
|
|
|
|
for (const auto &MaRedPair : D->getReductionDependences()) {
|
|
|
|
if (!MaRedPair.second)
|
|
|
|
continue;
|
|
|
|
RedDeps = isl_union_map_from_map(isl_map_copy(MaRedPair.second));
|
|
|
|
if (!D->isParallel(Schedule, RedDeps))
|
|
|
|
NodeInfo->BrokenReductions.insert(MaRedPair.first);
|
|
|
|
}
|
2014-07-15 08:00:35 +08:00
|
|
|
|
2014-07-28 11:46:28 +08:00
|
|
|
isl_union_map_free(Schedule);
|
2014-07-15 08:00:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-13 14:24:06 +08:00
|
|
|
// This method is executed before the construction of a for node. It creates
|
|
|
|
// an isl_id that is used to annotate the subsequently generated ast for nodes.
|
|
|
|
//
|
|
|
|
// In this function we also run the following analyses:
|
|
|
|
//
|
|
|
|
// - Detection of openmp parallel loops
|
|
|
|
//
|
2013-05-07 15:30:56 +08:00
|
|
|
static __isl_give isl_id *astBuildBeforeFor(__isl_keep isl_ast_build *Build,
|
|
|
|
void *User) {
|
2014-07-24 04:17:28 +08:00
|
|
|
AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
|
2014-08-01 05:34:32 +08:00
|
|
|
IslAstUserPayload *Payload = new IslAstUserPayload();
|
|
|
|
isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
|
2014-07-24 04:17:28 +08:00
|
|
|
Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
|
2014-07-24 23:59:06 +08:00
|
|
|
BuildInfo->LastForNodeId = Id;
|
2012-12-13 14:24:06 +08:00
|
|
|
|
2014-08-01 05:34:32 +08:00
|
|
|
// Test for parallelism only if we are not already inside a parallel loop
|
|
|
|
if (!BuildInfo->InParallelFor)
|
|
|
|
BuildInfo->InParallelFor = Payload->IsOutermostParallel =
|
2014-08-01 16:17:19 +08:00
|
|
|
astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload);
|
2012-12-13 14:24:06 +08:00
|
|
|
|
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method is executed after the construction of a for node.
|
|
|
|
//
|
|
|
|
// It performs the following actions:
|
|
|
|
//
|
|
|
|
// - Reset the 'InParallelFor' flag, as soon as we leave a for node,
|
|
|
|
// that is marked as openmp parallel.
|
|
|
|
//
|
|
|
|
static __isl_give isl_ast_node *
|
2013-02-06 02:01:29 +08:00
|
|
|
astBuildAfterFor(__isl_take isl_ast_node *Node, __isl_keep isl_ast_build *Build,
|
|
|
|
void *User) {
|
2012-12-14 00:52:41 +08:00
|
|
|
isl_id *Id = isl_ast_node_get_annotation(Node);
|
2014-08-01 05:34:32 +08:00
|
|
|
assert(Id && "Post order visit assumes annotated for nodes");
|
|
|
|
IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
|
|
|
|
assert(Payload && "Post order visit assumes annotated for nodes");
|
2014-08-01 05:33:49 +08:00
|
|
|
|
2014-08-01 05:34:32 +08:00
|
|
|
AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
|
|
|
|
assert(!Payload->Build && "Build environment already set");
|
|
|
|
Payload->Build = isl_ast_build_copy(Build);
|
|
|
|
Payload->IsInnermost = (Id == BuildInfo->LastForNodeId);
|
|
|
|
|
|
|
|
// Innermost loops that are surrounded by parallel loops have not yet been
|
|
|
|
// tested for parallelism. Test them here to ensure we check all innermost
|
|
|
|
// loops for parallelism.
|
2014-08-01 16:17:19 +08:00
|
|
|
if (Payload->IsInnermost && BuildInfo->InParallelFor) {
|
2016-02-23 17:00:13 +08:00
|
|
|
if (Payload->IsOutermostParallel) {
|
2014-08-01 05:34:32 +08:00
|
|
|
Payload->IsInnermostParallel = true;
|
2016-02-23 17:00:13 +08:00
|
|
|
} else {
|
|
|
|
if (PollyVectorizerChoice == VECTORIZER_NONE)
|
|
|
|
Payload->IsInnermostParallel =
|
|
|
|
astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload);
|
|
|
|
}
|
2014-09-10 01:03:54 +08:00
|
|
|
}
|
|
|
|
if (Payload->IsOutermostParallel)
|
2014-08-01 05:34:32 +08:00
|
|
|
BuildInfo->InParallelFor = false;
|
2012-12-13 14:24:06 +08:00
|
|
|
|
2012-12-18 15:46:13 +08:00
|
|
|
isl_id_free(Id);
|
2012-12-13 14:24:06 +08:00
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:00:13 +08:00
|
|
|
static isl_stat astBuildBeforeMark(__isl_keep isl_id *MarkId,
|
|
|
|
__isl_keep isl_ast_build *Build,
|
|
|
|
void *User) {
|
|
|
|
if (!MarkId)
|
|
|
|
return isl_stat_error;
|
|
|
|
|
|
|
|
AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
|
2017-08-25 05:22:41 +08:00
|
|
|
if (strcmp(isl_id_get_name(MarkId), "SIMD") == 0)
|
2016-02-23 17:00:13 +08:00
|
|
|
BuildInfo->InParallelFor = true;
|
|
|
|
|
|
|
|
return isl_stat_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __isl_give isl_ast_node *
|
|
|
|
astBuildAfterMark(__isl_take isl_ast_node *Node,
|
|
|
|
__isl_keep isl_ast_build *Build, void *User) {
|
|
|
|
assert(isl_ast_node_get_type(Node) == isl_ast_node_mark);
|
|
|
|
AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
|
|
|
|
auto *Id = isl_ast_node_mark_get_id(Node);
|
2017-08-25 05:22:41 +08:00
|
|
|
if (strcmp(isl_id_get_name(Id), "SIMD") == 0)
|
2016-02-23 17:00:13 +08:00
|
|
|
BuildInfo->InParallelFor = false;
|
|
|
|
isl_id_free(Id);
|
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
static __isl_give isl_ast_node *AtEachDomain(__isl_take isl_ast_node *Node,
|
2014-07-24 04:17:28 +08:00
|
|
|
__isl_keep isl_ast_build *Build,
|
2013-05-07 15:30:56 +08:00
|
|
|
void *User) {
|
2014-07-29 16:59:56 +08:00
|
|
|
assert(!isl_ast_node_get_annotation(Node) && "Node already annotated");
|
2014-08-01 05:34:32 +08:00
|
|
|
|
|
|
|
IslAstUserPayload *Payload = new IslAstUserPayload();
|
|
|
|
isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
|
2014-07-29 16:59:56 +08:00
|
|
|
Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2014-08-01 05:34:32 +08:00
|
|
|
Payload->Build = isl_ast_build_copy(Build);
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2012-12-18 15:46:13 +08:00
|
|
|
return isl_ast_node_set_annotation(Node, Id);
|
2012-10-03 03:50:38 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 01:04:54 +08:00
|
|
|
// Build alias check condition given a pair of minimal/maximal access.
|
2017-09-04 03:47:19 +08:00
|
|
|
static isl::ast_expr buildCondition(Scop &S, isl::ast_build Build,
|
2017-09-04 02:31:44 +08:00
|
|
|
const Scop::MinMaxAccessTy *It0,
|
|
|
|
const Scop::MinMaxAccessTy *It1) {
|
|
|
|
|
2017-11-20 06:13:34 +08:00
|
|
|
isl::pw_multi_aff AFirst = It0->first;
|
|
|
|
isl::pw_multi_aff ASecond = It0->second;
|
|
|
|
isl::pw_multi_aff BFirst = It1->first;
|
|
|
|
isl::pw_multi_aff BSecond = It1->second;
|
2017-09-04 02:31:44 +08:00
|
|
|
|
|
|
|
isl::id Left = AFirst.get_tuple_id(isl::dim::set);
|
|
|
|
isl::id Right = BFirst.get_tuple_id(isl::dim::set);
|
2017-08-29 04:17:02 +08:00
|
|
|
|
2017-09-04 03:47:19 +08:00
|
|
|
isl::ast_expr True =
|
|
|
|
isl::ast_expr::from_val(isl::val::int_from_ui(Build.get_ctx(), 1));
|
|
|
|
isl::ast_expr False =
|
|
|
|
isl::ast_expr::from_val(isl::val::int_from_ui(Build.get_ctx(), 0));
|
|
|
|
|
2017-08-29 04:17:02 +08:00
|
|
|
const ScopArrayInfo *BaseLeft =
|
|
|
|
ScopArrayInfo::getFromId(Left)->getBasePtrOriginSAI();
|
|
|
|
const ScopArrayInfo *BaseRight =
|
|
|
|
ScopArrayInfo::getFromId(Right)->getBasePtrOriginSAI();
|
2017-09-04 02:31:44 +08:00
|
|
|
if (BaseLeft && BaseLeft == BaseRight)
|
2017-09-04 03:47:19 +08:00
|
|
|
return True;
|
|
|
|
|
|
|
|
isl::set Params = S.getContext();
|
2017-09-04 02:31:44 +08:00
|
|
|
|
|
|
|
isl::ast_expr NonAliasGroup, MinExpr, MaxExpr;
|
2017-09-04 03:47:19 +08:00
|
|
|
|
|
|
|
// In the following, we first check if any accesses will be empty under
|
|
|
|
// the execution context of the scop and do not code generate them if this
|
|
|
|
// is the case as isl will fail to derive valid AST expressions for such
|
|
|
|
// accesses.
|
|
|
|
|
|
|
|
if (!AFirst.intersect_params(Params).domain().is_empty() &&
|
|
|
|
!BSecond.intersect_params(Params).domain().is_empty()) {
|
|
|
|
MinExpr = Build.access_from(AFirst).address_of();
|
|
|
|
MaxExpr = Build.access_from(BSecond).address_of();
|
|
|
|
NonAliasGroup = MaxExpr.le(MinExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BFirst.intersect_params(Params).domain().is_empty() &&
|
|
|
|
!ASecond.intersect_params(Params).domain().is_empty()) {
|
|
|
|
MinExpr = Build.access_from(BFirst).address_of();
|
|
|
|
MaxExpr = Build.access_from(ASecond).address_of();
|
|
|
|
|
|
|
|
isl::ast_expr Result = MaxExpr.le(MinExpr);
|
|
|
|
if (!NonAliasGroup.is_null())
|
|
|
|
NonAliasGroup = isl::manage(
|
|
|
|
isl_ast_expr_or(NonAliasGroup.release(), Result.release()));
|
|
|
|
else
|
|
|
|
NonAliasGroup = Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NonAliasGroup.is_null())
|
|
|
|
NonAliasGroup = True;
|
2015-07-25 20:31:03 +08:00
|
|
|
|
|
|
|
return NonAliasGroup;
|
2015-07-24 01:04:54 +08:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:17:15 +08:00
|
|
|
__isl_give isl_ast_expr *
|
2017-05-23 18:12:56 +08:00
|
|
|
IslAst::buildRunCondition(Scop &S, __isl_keep isl_ast_build *Build) {
|
2016-02-26 20:59:38 +08:00
|
|
|
isl_ast_expr *RunCondition;
|
|
|
|
|
2013-10-31 19:50:52 +08:00
|
|
|
// The conditions that need to be checked at run-time for this scop are
|
2015-08-20 13:58:56 +08:00
|
|
|
// available as an isl_set in the runtime check context from which we can
|
|
|
|
// directly derive a run-time condition.
|
2017-08-07 05:42:09 +08:00
|
|
|
auto *PosCond =
|
|
|
|
isl_ast_build_expr_from_set(Build, S.getAssumedContext().release());
|
2017-05-23 18:12:56 +08:00
|
|
|
if (S.hasTrivialInvalidContext()) {
|
2016-03-01 21:06:28 +08:00
|
|
|
RunCondition = PosCond;
|
|
|
|
} else {
|
|
|
|
auto *ZeroV = isl_val_zero(isl_ast_build_get_ctx(Build));
|
2017-08-07 05:42:16 +08:00
|
|
|
auto *NegCond =
|
|
|
|
isl_ast_build_expr_from_set(Build, S.getInvalidContext().release());
|
2016-03-01 21:06:28 +08:00
|
|
|
auto *NotNegCond = isl_ast_expr_eq(isl_ast_expr_from_val(ZeroV), NegCond);
|
|
|
|
RunCondition = isl_ast_expr_and(PosCond, NotNegCond);
|
|
|
|
}
|
2014-09-18 19:17:17 +08:00
|
|
|
|
|
|
|
// Create the alias checks from the minimal/maximal accesses in each alias
|
2015-07-24 01:04:54 +08:00
|
|
|
// group which consists of read only and non read only (read write) accesses.
|
|
|
|
// This operation is by construction quadratic in the read-write pointers and
|
2017-05-02 22:54:49 +08:00
|
|
|
// linear in the read only pointers in each alias group.
|
2017-05-23 18:12:56 +08:00
|
|
|
for (const Scop::MinMaxVectorPairTy &MinMaxAccessPair : S.getAliasGroups()) {
|
2015-07-26 21:14:38 +08:00
|
|
|
auto &MinMaxReadWrite = MinMaxAccessPair.first;
|
|
|
|
auto &MinMaxReadOnly = MinMaxAccessPair.second;
|
|
|
|
auto RWAccEnd = MinMaxReadWrite.end();
|
2015-07-24 01:04:54 +08:00
|
|
|
|
2015-07-26 21:14:38 +08:00
|
|
|
for (auto RWAccIt0 = MinMaxReadWrite.begin(); RWAccIt0 != RWAccEnd;
|
2015-07-24 01:04:54 +08:00
|
|
|
++RWAccIt0) {
|
|
|
|
for (auto RWAccIt1 = RWAccIt0 + 1; RWAccIt1 != RWAccEnd; ++RWAccIt1)
|
2015-07-25 20:31:03 +08:00
|
|
|
RunCondition = isl_ast_expr_and(
|
2017-09-04 03:47:19 +08:00
|
|
|
RunCondition,
|
2018-02-20 15:26:58 +08:00
|
|
|
buildCondition(S, isl::manage_copy(Build), RWAccIt0, RWAccIt1)
|
2017-09-04 03:47:19 +08:00
|
|
|
.release());
|
2015-07-26 21:14:38 +08:00
|
|
|
for (const Scop::MinMaxAccessTy &ROAccIt : MinMaxReadOnly)
|
2015-07-25 20:31:03 +08:00
|
|
|
RunCondition = isl_ast_expr_and(
|
2017-09-04 03:47:19 +08:00
|
|
|
RunCondition,
|
2018-02-20 15:26:58 +08:00
|
|
|
buildCondition(S, isl::manage_copy(Build), RWAccIt0, &ROAccIt)
|
2017-09-04 03:47:19 +08:00
|
|
|
.release());
|
2014-09-18 19:17:17 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-26 20:59:38 +08:00
|
|
|
|
|
|
|
return RunCondition;
|
2013-10-30 05:05:49 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Simple cost analysis for a given SCoP.
|
2015-02-12 01:25:09 +08:00
|
|
|
///
|
|
|
|
/// TODO: Improve this analysis and extract it to make it usable in other
|
|
|
|
/// places too.
|
|
|
|
/// In order to improve the cost model we could either keep track of
|
|
|
|
/// performed optimizations (e.g., tiling) or compute properties on the
|
|
|
|
/// original as well as optimized SCoP (e.g., #stride-one-accesses).
|
2017-05-23 18:12:56 +08:00
|
|
|
static bool benefitsFromPolly(Scop &Scop, bool PerformParallelTest) {
|
2015-10-07 00:10:29 +08:00
|
|
|
if (PollyProcessUnprofitable)
|
2015-02-12 01:25:09 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check if nothing interesting happened.
|
2017-05-23 18:12:56 +08:00
|
|
|
if (!PerformParallelTest && !Scop.isOptimized() &&
|
|
|
|
Scop.getAliasGroups().empty())
|
2015-02-12 01:25:09 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// The default assumption is that Polly improves the code.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
/// Collect statistics for the syntax tree rooted at @p Ast.
|
|
|
|
static void walkAstForStatistics(__isl_keep isl_ast_node *Ast) {
|
|
|
|
assert(Ast);
|
|
|
|
isl_ast_node_foreach_descendant_top_down(
|
|
|
|
Ast,
|
|
|
|
[](__isl_keep isl_ast_node *Node, void *User) -> isl_bool {
|
|
|
|
switch (isl_ast_node_get_type(Node)) {
|
|
|
|
case isl_ast_node_for:
|
|
|
|
NumForLoops++;
|
|
|
|
if (IslAstInfo::isParallel(Node))
|
|
|
|
NumParallel++;
|
|
|
|
if (IslAstInfo::isInnermostParallel(Node))
|
|
|
|
NumInnermostParallel++;
|
|
|
|
if (IslAstInfo::isOutermostParallel(Node))
|
|
|
|
NumOutermostParallel++;
|
|
|
|
if (IslAstInfo::isReductionParallel(Node))
|
|
|
|
NumReductionParallel++;
|
|
|
|
if (IslAstInfo::isExecutedInParallel(Node))
|
|
|
|
NumExecutedInParallel++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case isl_ast_node_if:
|
|
|
|
NumIfConditions++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue traversing subtrees.
|
|
|
|
return isl_bool_true;
|
|
|
|
},
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
2017-08-25 05:22:41 +08:00
|
|
|
IslAst::IslAst(Scop &Scop) : S(Scop), Ctx(Scop.getSharedIslCtx()) {}
|
|
|
|
|
|
|
|
IslAst::IslAst(IslAst &&O)
|
|
|
|
: S(O.S), Root(O.Root), RunCondition(O.RunCondition), Ctx(O.Ctx) {
|
|
|
|
O.Root = nullptr;
|
|
|
|
O.RunCondition = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IslAst::~IslAst() {
|
|
|
|
isl_ast_node_free(Root);
|
|
|
|
isl_ast_expr_free(RunCondition);
|
|
|
|
}
|
2015-02-12 01:25:09 +08:00
|
|
|
|
2015-07-31 03:27:04 +08:00
|
|
|
void IslAst::init(const Dependences &D) {
|
2015-02-12 01:25:09 +08:00
|
|
|
bool PerformParallelTest = PollyParallel || DetectParallel ||
|
|
|
|
PollyVectorizerChoice != VECTORIZER_NONE;
|
|
|
|
|
2017-02-27 16:03:11 +08:00
|
|
|
// We can not perform the dependence analysis and, consequently,
|
|
|
|
// the parallel code generation in case the schedule tree contains
|
|
|
|
// extension nodes.
|
2017-11-20 06:13:34 +08:00
|
|
|
auto ScheduleTree = S.getScheduleTree();
|
2017-02-27 16:03:11 +08:00
|
|
|
PerformParallelTest =
|
2017-05-23 18:12:56 +08:00
|
|
|
PerformParallelTest && !S.containsExtensionNode(ScheduleTree);
|
2017-02-27 16:03:11 +08:00
|
|
|
|
2015-02-12 01:25:09 +08:00
|
|
|
// Skip AST and code generation if there was no benefit achieved.
|
2015-07-31 03:27:04 +08:00
|
|
|
if (!benefitsFromPolly(S, PerformParallelTest))
|
2015-02-12 01:25:09 +08:00
|
|
|
return;
|
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
auto ScopStats = S.getStatistics();
|
|
|
|
ScopsBeneficial++;
|
|
|
|
BeneficialAffineLoops += ScopStats.NumAffineLoops;
|
|
|
|
BeneficialBoxedLoops += ScopStats.NumBoxedLoops;
|
|
|
|
|
2017-11-20 06:13:34 +08:00
|
|
|
auto Ctx = S.getIslCtx();
|
|
|
|
isl_options_set_ast_build_atomic_upper_bound(Ctx.get(), true);
|
|
|
|
isl_options_set_ast_build_detect_min_max(Ctx.get(), true);
|
2016-06-12 03:17:15 +08:00
|
|
|
isl_ast_build *Build;
|
2014-07-24 04:17:28 +08:00
|
|
|
AstBuildUserInfo BuildInfo;
|
2012-10-03 03:50:38 +08:00
|
|
|
|
|
|
|
if (UseContext)
|
2017-08-07 03:52:38 +08:00
|
|
|
Build = isl_ast_build_from_context(S.getContext().release());
|
2012-10-03 03:50:38 +08:00
|
|
|
else
|
2017-08-07 04:11:59 +08:00
|
|
|
Build = isl_ast_build_from_context(
|
|
|
|
isl_set_universe(S.getParamSpace().release()));
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2014-07-24 04:17:28 +08:00
|
|
|
Build = isl_ast_build_set_at_each_domain(Build, AtEachDomain, nullptr);
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2015-02-12 01:25:09 +08:00
|
|
|
if (PerformParallelTest) {
|
2012-12-13 14:24:06 +08:00
|
|
|
BuildInfo.Deps = &D;
|
2017-08-25 05:22:41 +08:00
|
|
|
BuildInfo.InParallelFor = false;
|
2012-12-13 14:24:06 +08:00
|
|
|
|
2014-07-24 04:17:28 +08:00
|
|
|
Build = isl_ast_build_set_before_each_for(Build, &astBuildBeforeFor,
|
|
|
|
&BuildInfo);
|
|
|
|
Build =
|
|
|
|
isl_ast_build_set_after_each_for(Build, &astBuildAfterFor, &BuildInfo);
|
2016-02-23 17:00:13 +08:00
|
|
|
|
|
|
|
Build = isl_ast_build_set_before_each_mark(Build, &astBuildBeforeMark,
|
|
|
|
&BuildInfo);
|
|
|
|
|
|
|
|
Build = isl_ast_build_set_after_each_mark(Build, &astBuildAfterMark,
|
|
|
|
&BuildInfo);
|
2012-12-13 14:24:06 +08:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:17:15 +08:00
|
|
|
RunCondition = buildRunCondition(S, Build);
|
|
|
|
|
2017-08-07 05:42:38 +08:00
|
|
|
Root = isl_ast_build_node_from_schedule(Build, S.getScheduleTree().release());
|
2017-08-23 21:50:30 +08:00
|
|
|
walkAstForStatistics(Root);
|
2016-06-12 03:17:15 +08:00
|
|
|
|
|
|
|
isl_ast_build_free(Build);
|
2012-10-03 03:50:38 +08:00
|
|
|
}
|
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
IslAst IslAst::create(Scop &Scop, const Dependences &D) {
|
|
|
|
IslAst Ast{Scop};
|
|
|
|
Ast.init(D);
|
2015-07-31 03:27:04 +08:00
|
|
|
return Ast;
|
|
|
|
}
|
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
__isl_give isl_ast_node *IslAst::getAst() { return isl_ast_node_copy(Root); }
|
2016-06-12 03:17:15 +08:00
|
|
|
__isl_give isl_ast_expr *IslAst::getRunCondition() {
|
|
|
|
return isl_ast_expr_copy(RunCondition);
|
|
|
|
}
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
__isl_give isl_ast_node *IslAstInfo::getAst() { return Ast.getAst(); }
|
|
|
|
__isl_give isl_ast_expr *IslAstInfo::getRunCondition() {
|
|
|
|
return Ast.getRunCondition();
|
2013-11-17 11:18:25 +08:00
|
|
|
}
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2014-07-18 00:11:28 +08:00
|
|
|
IslAstUserPayload *IslAstInfo::getNodePayload(__isl_keep isl_ast_node *Node) {
|
|
|
|
isl_id *Id = isl_ast_node_get_annotation(Node);
|
|
|
|
if (!Id)
|
|
|
|
return nullptr;
|
|
|
|
IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
|
|
|
|
isl_id_free(Id);
|
|
|
|
return Payload;
|
|
|
|
}
|
|
|
|
|
2014-08-01 05:33:49 +08:00
|
|
|
bool IslAstInfo::isInnermost(__isl_keep isl_ast_node *Node) {
|
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
|
|
|
return Payload && Payload->IsInnermost;
|
|
|
|
}
|
|
|
|
|
2014-07-18 00:11:28 +08:00
|
|
|
bool IslAstInfo::isParallel(__isl_keep isl_ast_node *Node) {
|
2014-08-01 16:14:28 +08:00
|
|
|
return IslAstInfo::isInnermostParallel(Node) ||
|
|
|
|
IslAstInfo::isOutermostParallel(Node);
|
2014-07-18 00:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IslAstInfo::isInnermostParallel(__isl_keep isl_ast_node *Node) {
|
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
2014-08-01 16:14:28 +08:00
|
|
|
return Payload && Payload->IsInnermostParallel;
|
2014-07-18 00:11:28 +08:00
|
|
|
}
|
|
|
|
|
2014-08-01 05:34:32 +08:00
|
|
|
bool IslAstInfo::isOutermostParallel(__isl_keep isl_ast_node *Node) {
|
2014-07-18 00:11:28 +08:00
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
2014-08-01 16:14:28 +08:00
|
|
|
return Payload && Payload->IsOutermostParallel;
|
2014-07-18 00:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IslAstInfo::isReductionParallel(__isl_keep isl_ast_node *Node) {
|
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
|
|
|
return Payload && Payload->IsReductionParallel;
|
|
|
|
}
|
|
|
|
|
2014-11-07 03:35:21 +08:00
|
|
|
bool IslAstInfo::isExecutedInParallel(__isl_keep isl_ast_node *Node) {
|
2014-11-16 22:24:53 +08:00
|
|
|
if (!PollyParallel)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do not parallelize innermost loops.
|
|
|
|
//
|
|
|
|
// Parallelizing innermost loops is often not profitable, especially if
|
|
|
|
// they have a low number of iterations.
|
|
|
|
//
|
|
|
|
// TODO: Decide this based on the number of loop iterations that will be
|
|
|
|
// executed. This can possibly require run-time checks, which again
|
|
|
|
// raises the question of both run-time check overhead and code size
|
|
|
|
// costs.
|
|
|
|
if (!PollyParallelForce && isInnermost(Node))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return isOutermostParallel(Node) && !isReductionParallel(Node);
|
2014-11-07 03:35:21 +08:00
|
|
|
}
|
|
|
|
|
2016-04-10 05:55:23 +08:00
|
|
|
__isl_give isl_union_map *
|
|
|
|
IslAstInfo::getSchedule(__isl_keep isl_ast_node *Node) {
|
2014-07-24 04:17:28 +08:00
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
|
|
|
return Payload ? isl_ast_build_get_schedule(Payload->Build) : nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-10 05:55:23 +08:00
|
|
|
__isl_give isl_pw_aff *
|
2014-09-14 01:34:11 +08:00
|
|
|
IslAstInfo::getMinimalDependenceDistance(__isl_keep isl_ast_node *Node) {
|
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
|
|
|
return Payload ? isl_pw_aff_copy(Payload->MinimalDependenceDistance)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-01 16:17:19 +08:00
|
|
|
IslAstInfo::MemoryAccessSet *
|
|
|
|
IslAstInfo::getBrokenReductions(__isl_keep isl_ast_node *Node) {
|
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
|
|
|
return Payload ? &Payload->BrokenReductions : nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-03 09:51:59 +08:00
|
|
|
isl_ast_build *IslAstInfo::getBuild(__isl_keep isl_ast_node *Node) {
|
|
|
|
IslAstUserPayload *Payload = getNodePayload(Node);
|
|
|
|
return Payload ? Payload->Build : nullptr;
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
IslAstInfo IslAstAnalysis::run(Scop &S, ScopAnalysisManager &SAM,
|
|
|
|
ScopStandardAnalysisResults &SAR) {
|
|
|
|
return {S, SAM.getResult<DependenceAnalysis>(S, SAR).getDependences(
|
2017-11-22 03:25:26 +08:00
|
|
|
Dependences::AL_Statement)};
|
2017-05-23 18:12:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-11 04:13:06 +08:00
|
|
|
static __isl_give isl_printer *cbPrintUser(__isl_take isl_printer *P,
|
|
|
|
__isl_take isl_ast_print_options *O,
|
|
|
|
__isl_keep isl_ast_node *Node,
|
|
|
|
void *User) {
|
2018-02-20 15:26:58 +08:00
|
|
|
isl::ast_node AstNode = isl::manage_copy(Node);
|
2017-07-11 04:13:06 +08:00
|
|
|
isl::ast_expr NodeExpr = AstNode.user_get_expr();
|
|
|
|
isl::ast_expr CallExpr = NodeExpr.get_op_arg(0);
|
|
|
|
isl::id CallExprId = CallExpr.get_id();
|
|
|
|
ScopStmt *AccessStmt = (ScopStmt *)CallExprId.get_user();
|
|
|
|
|
|
|
|
P = isl_printer_start_line(P);
|
|
|
|
P = isl_printer_print_str(P, AccessStmt->getBaseName());
|
|
|
|
P = isl_printer_print_str(P, "(");
|
|
|
|
P = isl_printer_end_line(P);
|
|
|
|
P = isl_printer_indent(P, 2);
|
|
|
|
|
|
|
|
for (MemoryAccess *MemAcc : *AccessStmt) {
|
|
|
|
P = isl_printer_start_line(P);
|
|
|
|
|
|
|
|
if (MemAcc->isRead())
|
|
|
|
P = isl_printer_print_str(P, "/* read */ &");
|
|
|
|
else
|
|
|
|
P = isl_printer_print_str(P, "/* write */ ");
|
|
|
|
|
2018-02-20 15:26:58 +08:00
|
|
|
isl::ast_build Build = isl::manage_copy(IslAstInfo::getBuild(Node));
|
2017-07-11 04:13:06 +08:00
|
|
|
if (MemAcc->isAffine()) {
|
|
|
|
isl_pw_multi_aff *PwmaPtr =
|
2017-07-23 12:08:52 +08:00
|
|
|
MemAcc->applyScheduleToAccessRelation(Build.get_schedule()).release();
|
2017-07-11 04:13:06 +08:00
|
|
|
isl::pw_multi_aff Pwma = isl::manage(PwmaPtr);
|
|
|
|
isl::ast_expr AccessExpr = Build.access_from(Pwma);
|
|
|
|
P = isl_printer_print_ast_expr(P, AccessExpr.get());
|
|
|
|
} else {
|
|
|
|
P = isl_printer_print_str(
|
|
|
|
P, MemAcc->getLatestScopArrayInfo()->getName().c_str());
|
|
|
|
P = isl_printer_print_str(P, "[*]");
|
|
|
|
}
|
|
|
|
P = isl_printer_end_line(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
P = isl_printer_indent(P, -2);
|
|
|
|
P = isl_printer_start_line(P);
|
|
|
|
P = isl_printer_print_str(P, ");");
|
|
|
|
P = isl_printer_end_line(P);
|
|
|
|
|
|
|
|
isl_ast_print_options_free(O);
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
void IslAstInfo::print(raw_ostream &OS) {
|
2014-07-24 02:14:43 +08:00
|
|
|
isl_ast_print_options *Options;
|
2017-05-23 18:12:56 +08:00
|
|
|
isl_ast_node *RootNode = Ast.getAst();
|
2016-05-23 20:38:05 +08:00
|
|
|
Function &F = S.getFunction();
|
2015-02-12 01:25:09 +08:00
|
|
|
|
2016-05-23 20:38:05 +08:00
|
|
|
OS << ":: isl ast :: " << F.getName() << " :: " << S.getNameStr() << "\n";
|
2015-02-12 01:25:09 +08:00
|
|
|
|
|
|
|
if (!RootNode) {
|
|
|
|
OS << ":: isl ast generation and code generation was skipped!\n\n";
|
2015-10-07 00:10:46 +08:00
|
|
|
OS << ":: This is either because no useful optimizations could be applied "
|
|
|
|
"(use -polly-process-unprofitable to enforce code generation) or "
|
|
|
|
"because earlier passes such as dependence analysis timed out (use "
|
|
|
|
"-polly-dependences-computeout=0 to set dependence analysis timeout "
|
|
|
|
"to infinity)\n\n";
|
2015-02-12 01:25:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
isl_ast_expr *RunCondition = Ast.getRunCondition();
|
2014-07-24 02:14:43 +08:00
|
|
|
char *RtCStr, *AstStr;
|
|
|
|
|
2017-11-20 06:13:34 +08:00
|
|
|
Options = isl_ast_print_options_alloc(S.getIslCtx().get());
|
2017-07-11 04:13:06 +08:00
|
|
|
|
|
|
|
if (PrintAccesses)
|
|
|
|
Options =
|
|
|
|
isl_ast_print_options_set_print_user(Options, cbPrintUser, nullptr);
|
2014-08-01 05:33:49 +08:00
|
|
|
Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr);
|
2014-07-24 02:14:43 +08:00
|
|
|
|
2017-11-20 06:13:34 +08:00
|
|
|
isl_printer *P = isl_printer_to_str(S.getIslCtx().get());
|
2016-07-06 17:11:00 +08:00
|
|
|
P = isl_printer_set_output_format(P, ISL_FORMAT_C);
|
2014-07-24 02:14:43 +08:00
|
|
|
P = isl_printer_print_ast_expr(P, RunCondition);
|
|
|
|
RtCStr = isl_printer_get_str(P);
|
|
|
|
P = isl_printer_flush(P);
|
|
|
|
P = isl_printer_indent(P, 4);
|
|
|
|
P = isl_ast_node_print(RootNode, P, Options);
|
|
|
|
AstStr = isl_printer_get_str(P);
|
|
|
|
|
2017-08-07 05:42:38 +08:00
|
|
|
auto *Schedule = S.getScheduleTree().release();
|
2014-07-24 02:14:43 +08:00
|
|
|
|
2014-10-23 07:16:28 +08:00
|
|
|
DEBUG({
|
|
|
|
dbgs() << S.getContextStr() << "\n";
|
|
|
|
dbgs() << stringFromIslObj(Schedule);
|
|
|
|
});
|
2014-07-24 02:14:43 +08:00
|
|
|
OS << "\nif (" << RtCStr << ")\n\n";
|
|
|
|
OS << AstStr << "\n";
|
|
|
|
OS << "else\n";
|
|
|
|
OS << " { /* original code */ }\n\n";
|
|
|
|
|
2015-06-05 13:31:46 +08:00
|
|
|
free(RtCStr);
|
|
|
|
free(AstStr);
|
|
|
|
|
2014-07-24 02:14:43 +08:00
|
|
|
isl_ast_expr_free(RunCondition);
|
2016-09-14 14:26:09 +08:00
|
|
|
isl_schedule_free(Schedule);
|
2014-07-24 02:14:43 +08:00
|
|
|
isl_ast_node_free(RootNode);
|
|
|
|
isl_printer_free(P);
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
AnalysisKey IslAstAnalysis::Key;
|
|
|
|
PreservedAnalyses IslAstPrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
|
|
|
|
ScopStandardAnalysisResults &SAR,
|
|
|
|
SPMUpdater &U) {
|
|
|
|
auto &Ast = SAM.getResult<IslAstAnalysis>(S, SAR);
|
2017-08-25 05:22:41 +08:00
|
|
|
Ast.print(OS);
|
2017-05-23 18:12:56 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslAstInfoWrapperPass::releaseMemory() { Ast.reset(); }
|
|
|
|
|
|
|
|
bool IslAstInfoWrapperPass::runOnScop(Scop &Scop) {
|
2017-07-01 03:42:21 +08:00
|
|
|
// Skip SCoPs in case they're already handled by PPCGCodeGeneration.
|
|
|
|
if (Scop.isToBeSkipped())
|
|
|
|
return false;
|
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
ScopsProcessed++;
|
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
const Dependences &D =
|
|
|
|
getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
|
|
|
|
|
2017-09-21 08:01:13 +08:00
|
|
|
if (D.getSharedIslCtx() != Scop.getSharedIslCtx()) {
|
|
|
|
DEBUG(dbgs() << "Got dependence analysis for different SCoP/isl_ctx\n");
|
|
|
|
Ast.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
Ast.reset(new IslAstInfo(Scop, D));
|
|
|
|
|
|
|
|
DEBUG(printScop(dbgs(), Scop));
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-25 05:22:41 +08:00
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
void IslAstInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
2012-10-03 03:50:38 +08:00
|
|
|
// Get the Common analysis usage of ScopPasses.
|
|
|
|
ScopPass::getAnalysisUsage(AU);
|
2017-09-21 08:01:13 +08:00
|
|
|
AU.addRequiredTransitive<ScopInfoRegionPass>();
|
2015-03-05 06:43:40 +08:00
|
|
|
AU.addRequired<DependenceInfo>();
|
2017-08-28 22:07:33 +08:00
|
|
|
|
|
|
|
AU.addPreserved<DependenceInfo>();
|
2012-10-03 03:50:38 +08:00
|
|
|
}
|
2013-02-22 16:07:06 +08:00
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
void IslAstInfoWrapperPass::printScop(raw_ostream &OS, Scop &S) const {
|
|
|
|
if (Ast)
|
|
|
|
Ast->print(OS);
|
|
|
|
}
|
2012-10-03 03:50:38 +08:00
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
char IslAstInfoWrapperPass::ID = 0;
|
|
|
|
|
|
|
|
Pass *polly::createIslAstInfoWrapperPassPass() {
|
|
|
|
return new IslAstInfoWrapperPass();
|
|
|
|
}
|
2013-02-22 16:07:06 +08:00
|
|
|
|
2017-05-23 18:12:56 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(IslAstInfoWrapperPass, "polly-ast",
|
2013-03-23 09:05:07 +08:00
|
|
|
"Polly - Generate an AST of the SCoP (isl)", false,
|
|
|
|
false);
|
2016-05-31 17:41:04 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
|
2015-03-05 06:43:40 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
|
2017-05-23 18:12:56 +08:00
|
|
|
INITIALIZE_PASS_END(IslAstInfoWrapperPass, "polly-ast",
|
2013-03-23 09:05:07 +08:00
|
|
|
"Polly - Generate an AST from the SCoP (isl)", false, false)
|