2011-04-29 14:27:02 +08:00
|
|
|
//===- Cloog.cpp - Cloog interface ----------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Cloog[1] interface.
|
|
|
|
//
|
|
|
|
// The Cloog interface takes a Scop and generates a Cloog AST (clast). This
|
|
|
|
// clast can either be returned directly or it can be pretty printed to stdout.
|
|
|
|
//
|
|
|
|
// A typical clast output looks like this:
|
|
|
|
//
|
|
|
|
// for (c2 = max(0, ceild(n + m, 2); c2 <= min(511, floord(5 * n, 3)); c2++) {
|
|
|
|
// bb2(c2);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// [1] http://www.cloog.org/ - The Chunky Loop Generator
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-05-29 17:11:46 +08:00
|
|
|
#include "polly/CodeGen/Cloog.h"
|
2012-05-08 00:35:11 +08:00
|
|
|
#ifdef CLOOG_FOUND
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
#include "polly/ScopInfo.h"
|
|
|
|
|
2011-10-23 18:57:44 +08:00
|
|
|
#define DEBUG_TYPE "polly-cloog"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2013-01-02 19:47:44 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-10-23 18:57:44 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
#include "cloog/isl/domain.h"
|
2011-08-20 19:11:25 +08:00
|
|
|
#include "cloog/isl/cloog.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-12-04 18:44:14 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
|
|
|
namespace polly {
|
|
|
|
class Cloog {
|
|
|
|
Scop *S;
|
|
|
|
CloogOptions *Options;
|
|
|
|
CloogState *State;
|
|
|
|
clast_stmt *ClastRoot;
|
|
|
|
|
|
|
|
void buildCloogOptions();
|
|
|
|
CloogUnionDomain *buildCloogUnionDomain();
|
|
|
|
CloogInput *buildCloogInput();
|
|
|
|
|
|
|
|
public:
|
|
|
|
Cloog(Scop *Scop);
|
|
|
|
|
|
|
|
~Cloog();
|
|
|
|
|
|
|
|
/// Write a .cloog input file
|
|
|
|
void dump(FILE *F);
|
|
|
|
|
|
|
|
/// Print a source code representation of the program.
|
|
|
|
void pprint(llvm::raw_ostream &OS);
|
|
|
|
|
|
|
|
/// Create the Cloog AST from this program.
|
2011-05-15 03:02:29 +08:00
|
|
|
struct clast_root *getClast();
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Cloog::Cloog(Scop *Scop) : S(Scop) {
|
2011-10-06 08:03:54 +08:00
|
|
|
State = cloog_isl_state_malloc(Scop->getIslCtx());
|
2011-04-29 14:27:02 +08:00
|
|
|
buildCloogOptions();
|
|
|
|
ClastRoot = cloog_clast_create_from_input(buildCloogInput(), Options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cloog::~Cloog() {
|
|
|
|
cloog_clast_free(ClastRoot);
|
2011-08-20 19:11:25 +08:00
|
|
|
cloog_options_free(Options);
|
2011-04-29 14:27:02 +08:00
|
|
|
cloog_state_free(State);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a FILE* write stream and get the output to it written
|
|
|
|
// to a std::string.
|
|
|
|
class FileToString {
|
|
|
|
int FD[2];
|
|
|
|
FILE *input;
|
|
|
|
static const int BUFFERSIZE = 20;
|
|
|
|
|
|
|
|
char buf[BUFFERSIZE + 1];
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileToString() {
|
|
|
|
pipe(FD);
|
|
|
|
input = fdopen(FD[1], "w");
|
|
|
|
}
|
|
|
|
~FileToString() {
|
|
|
|
close(FD[0]);
|
2013-06-23 09:29:29 +08:00
|
|
|
// close(FD[1]);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
FILE *getInputFile() { return input; }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void closeInput() {
|
|
|
|
fclose(input);
|
|
|
|
close(FD[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getOutput() {
|
|
|
|
std::string output;
|
|
|
|
int readSize;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
readSize = read(FD[0], &buf, BUFFERSIZE);
|
|
|
|
|
|
|
|
if (readSize <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
output += std::string(buf, readSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Write .cloog input file.
|
|
|
|
void Cloog::dump(FILE *F) {
|
|
|
|
CloogInput *Input = buildCloogInput();
|
|
|
|
cloog_input_dump_cloog(F, Input, Options);
|
|
|
|
cloog_input_free(Input);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print a source code representation of the program.
|
|
|
|
void Cloog::pprint(raw_ostream &OS) {
|
|
|
|
FileToString *Output = new FileToString();
|
|
|
|
clast_pprint(Output->getInputFile(), ClastRoot, 0, Options);
|
|
|
|
Output->closeInput();
|
|
|
|
OS << Output->getOutput();
|
|
|
|
delete (Output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the Cloog AST from this program.
|
2013-02-06 02:01:29 +08:00
|
|
|
struct clast_root *Cloog::getClast() { return (clast_root *)ClastRoot; }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void Cloog::buildCloogOptions() {
|
|
|
|
Options = cloog_options_malloc(State);
|
|
|
|
Options->quiet = 1;
|
|
|
|
Options->strides = 1;
|
|
|
|
Options->save_domains = 1;
|
|
|
|
Options->noscalars = 1;
|
2011-12-07 19:03:48 +08:00
|
|
|
|
|
|
|
// The last loop depth to optimize should be the last scattering dimension.
|
|
|
|
// CLooG by default will continue to split the loops even after the last
|
|
|
|
// scattering dimension. This splitting is problematic for the schedules
|
|
|
|
// calculated by the PoCC/isl/Pluto optimizer. Such schedules contain may
|
|
|
|
// not be fully defined, but statements without dependences may be mapped
|
|
|
|
// to the same exeuction time. For such schedules, continuing to split
|
|
|
|
// may lead to a larger set of if-conditions in the innermost loop.
|
|
|
|
Options->l = 0;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CloogUnionDomain *Cloog::buildCloogUnionDomain() {
|
|
|
|
CloogUnionDomain *DU = cloog_union_domain_alloc(S->getNumParams());
|
|
|
|
|
|
|
|
for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) {
|
|
|
|
ScopStmt *Stmt = *SI;
|
2011-10-06 08:04:05 +08:00
|
|
|
CloogScattering *Scattering;
|
|
|
|
CloogDomain *Domain;
|
|
|
|
|
|
|
|
Scattering = cloog_scattering_from_isl_map(Stmt->getScattering());
|
2012-12-30 07:47:38 +08:00
|
|
|
Domain = cloog_domain_from_isl_set(Stmt->getDomain());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
std::string entryName = Stmt->getBaseName();
|
|
|
|
|
2011-08-20 19:11:25 +08:00
|
|
|
DU = cloog_union_domain_add_domain(DU, entryName.c_str(), Domain,
|
|
|
|
Scattering, Stmt);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return DU;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloogInput *Cloog::buildCloogInput() {
|
2012-06-06 03:31:08 +08:00
|
|
|
// XXX: We do not copy the context of the scop, but use an unconstrained
|
|
|
|
// context. This 'hack' is necessary as the context may contain bounds
|
|
|
|
// on parameters such as [n] -> {:0 <= n < 2^32}. Those large
|
|
|
|
// integers will cause CLooG to construct a clast that contains
|
|
|
|
// expressions that include these large integers. Such expressions can
|
|
|
|
// possibly not be evaluated correctly with i64 types. The cloog
|
|
|
|
// based code generation backend, however, can not derive types
|
|
|
|
// automatically and just assumes i64 types. Hence, it will break or
|
|
|
|
// generate incorrect code.
|
|
|
|
// This hack does not remove all possibilities of incorrectly generated
|
|
|
|
// code, but it is ensures that for most problems the problems do not
|
|
|
|
// show up. The correct solution, will be to automatically derive the
|
|
|
|
// minimal types for each expression. This could be added to CLooG and it
|
|
|
|
// will be available in the isl based code generation.
|
|
|
|
isl_set *EmptyContext = isl_set_universe(S->getParamSpace());
|
|
|
|
CloogDomain *Context = cloog_domain_from_isl_set(EmptyContext);
|
2011-04-29 14:27:02 +08:00
|
|
|
CloogUnionDomain *Statements = buildCloogUnionDomain();
|
2011-11-15 19:38:47 +08:00
|
|
|
|
|
|
|
isl_set *ScopContext = S->getContext();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < isl_set_dim(ScopContext, isl_dim_param); i++) {
|
|
|
|
isl_id *id = isl_set_get_dim_id(ScopContext, isl_dim_param, i);
|
|
|
|
Statements = cloog_union_domain_set_name(Statements, CLOOG_PARAM, i,
|
|
|
|
isl_id_get_name(id));
|
|
|
|
isl_id_free(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_set_free(ScopContext);
|
|
|
|
|
2011-10-06 08:03:59 +08:00
|
|
|
CloogInput *Input = cloog_input_alloc(Context, Statements);
|
2011-04-29 14:27:02 +08:00
|
|
|
return Input;
|
|
|
|
}
|
2012-11-01 13:34:48 +08:00
|
|
|
|
|
|
|
void ClastVisitor::visit(const clast_stmt *stmt) {
|
2013-02-06 02:01:29 +08:00
|
|
|
if (CLAST_STMT_IS_A(stmt, stmt_root))
|
2012-11-01 13:34:48 +08:00
|
|
|
assert(false && "No second root statement expected");
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_ass))
|
|
|
|
return visitAssignment((const clast_assignment *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_user))
|
|
|
|
return visitUser((const clast_user_stmt *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_block))
|
|
|
|
return visitBlock((const clast_block *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_for))
|
|
|
|
return visitFor((const clast_for *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_guard))
|
|
|
|
return visitGuard((const clast_guard *)stmt);
|
|
|
|
|
|
|
|
if (stmt->next)
|
|
|
|
visit(stmt->next);
|
|
|
|
}
|
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
void ClastVisitor::visitAssignment(const clast_assignment *stmt) {}
|
2012-11-01 13:34:48 +08:00
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
void ClastVisitor::visitBlock(const clast_block *stmt) { visit(stmt->body); }
|
2012-11-01 13:34:48 +08:00
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
void ClastVisitor::visitFor(const clast_for *stmt) { visit(stmt->body); }
|
2012-11-01 13:34:48 +08:00
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
void ClastVisitor::visitGuard(const clast_guard *stmt) { visit(stmt->then); }
|
2012-11-01 13:34:48 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
} // End namespace polly.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct CloogExporter : public ScopPass {
|
|
|
|
static char ID;
|
|
|
|
Scop *S;
|
|
|
|
explicit CloogExporter() : ScopPass(ID) {}
|
|
|
|
|
|
|
|
std::string getFileName(Region *R) const;
|
|
|
|
virtual bool runOnScop(Scop &S);
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
std::string CloogExporter::getFileName(Region *R) const {
|
2011-11-17 22:52:36 +08:00
|
|
|
std::string FunctionName = R->getEntry()->getParent()->getName();
|
2011-04-29 14:27:02 +08:00
|
|
|
std::string ExitName, EntryName;
|
|
|
|
|
|
|
|
raw_string_ostream ExitStr(ExitName);
|
|
|
|
raw_string_ostream EntryStr(EntryName);
|
|
|
|
|
|
|
|
WriteAsOperand(EntryStr, R->getEntry(), false);
|
|
|
|
EntryStr.str();
|
|
|
|
|
|
|
|
if (R->getExit()) {
|
|
|
|
WriteAsOperand(ExitStr, R->getExit(), false);
|
|
|
|
ExitStr.str();
|
|
|
|
} else
|
|
|
|
ExitName = "FunctionExit";
|
|
|
|
|
|
|
|
std::string RegionName = EntryName + "---" + ExitName;
|
|
|
|
std::string FileName = FunctionName + "___" + RegionName + ".cloog";
|
|
|
|
|
|
|
|
return FileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
char CloogExporter::ID = 0;
|
|
|
|
bool CloogExporter::runOnScop(Scop &S) {
|
|
|
|
Region &R = S.getRegion();
|
|
|
|
CloogInfo &C = getAnalysis<CloogInfo>();
|
|
|
|
|
2011-11-17 22:52:36 +08:00
|
|
|
std::string FunctionName = R.getEntry()->getParent()->getName();
|
2011-04-29 14:27:02 +08:00
|
|
|
std::string Filename = getFileName(&R);
|
|
|
|
|
|
|
|
errs() << "Writing Scop '" << R.getNameStr() << "' in function '"
|
2013-02-06 02:01:29 +08:00
|
|
|
<< FunctionName << "' to '" << Filename << "'...\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
FILE *F = fopen(Filename.c_str(), "w");
|
|
|
|
C.dump(F);
|
|
|
|
fclose(F);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloogExporter::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
// Get the Common analysis usage of ScopPasses.
|
|
|
|
ScopPass::getAnalysisUsage(AU);
|
|
|
|
AU.addRequired<CloogInfo>();
|
|
|
|
}
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
static RegisterPass<CloogExporter> A("polly-export-cloog",
|
|
|
|
"Polly - Export the Cloog input file"
|
|
|
|
" (Writes a .cloog file for each Scop)");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
llvm::Pass *polly::createCloogExporterPass() { return new CloogExporter(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
/// Write a .cloog input file
|
2013-02-06 02:01:29 +08:00
|
|
|
void CloogInfo::dump(FILE *F) { C->dump(F); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
/// Print a source code representation of the program.
|
2013-02-06 02:01:29 +08:00
|
|
|
void CloogInfo::pprint(llvm::raw_ostream &OS) { C->pprint(OS); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
/// Create the Cloog AST from this program.
|
2013-02-06 02:01:29 +08:00
|
|
|
const struct clast_root *CloogInfo::getClast() { return C->getClast(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-08-20 19:11:25 +08:00
|
|
|
void CloogInfo::releaseMemory() {
|
|
|
|
if (C) {
|
|
|
|
delete C;
|
|
|
|
C = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool CloogInfo::runOnScop(Scop &S) {
|
|
|
|
if (C)
|
|
|
|
delete C;
|
|
|
|
|
|
|
|
scop = &S;
|
|
|
|
|
|
|
|
C = new Cloog(&S);
|
|
|
|
|
2011-10-23 18:57:44 +08:00
|
|
|
Function *F = S.getRegion().getEntry()->getParent();
|
2013-06-23 09:29:29 +08:00
|
|
|
(void)F;
|
2011-10-23 18:57:44 +08:00
|
|
|
|
2011-11-17 22:52:36 +08:00
|
|
|
DEBUG(dbgs() << ":: " << F->getName());
|
2012-12-30 07:47:38 +08:00
|
|
|
DEBUG(dbgs() << " : " << S.getRegion().getNameStr() << "\n");
|
2011-10-23 18:57:44 +08:00
|
|
|
DEBUG(C->pprint(dbgs()));
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloogInfo::printScop(raw_ostream &OS) const {
|
|
|
|
Function *function = scop->getRegion().getEntry()->getParent();
|
|
|
|
|
2011-11-17 22:52:36 +08:00
|
|
|
OS << function->getName() << "():\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
C->pprint(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloogInfo::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
// Get the Common analysis usage of ScopPasses.
|
|
|
|
ScopPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
char CloogInfo::ID = 0;
|
|
|
|
|
2013-02-22 16:07:06 +08:00
|
|
|
Pass *polly::createCloogInfoPass() { return new CloogInfo(); }
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(CloogInfo, "polly-cloog", "Execute Cloog code generation",
|
|
|
|
false, false);
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopInfo);
|
|
|
|
INITIALIZE_PASS_END(CloogInfo, "polly-cloog", "Execute Cloog code generation",
|
|
|
|
false, false)
|
2012-05-08 00:35:11 +08:00
|
|
|
|
|
|
|
#endif // CLOOG_FOUND
|