2011-04-29 14:27:02 +08:00
|
|
|
//===- Pocc.cpp - Pocc interface ----------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Pocc[1] interface.
|
|
|
|
//
|
|
|
|
// Pocc, the polyhedral compilation collection is a collection of polyhedral
|
|
|
|
// tools. It is used as an optimizer in polly
|
|
|
|
//
|
|
|
|
// [1] http://www-roc.inria.fr/~pouchet/software/pocc/
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
|
|
|
|
#ifdef SCOPLIB_FOUND
|
2012-08-24 21:54:36 +08:00
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
2012-08-24 21:54:40 +08:00
|
|
|
#include "polly/Dependences.h"
|
2013-05-07 15:31:10 +08:00
|
|
|
#include "polly/Options.h"
|
2011-10-24 04:59:44 +08:00
|
|
|
#include "polly/ScheduleOptimizer.h"
|
2012-08-24 21:54:40 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-08-30 19:49:38 +08:00
|
|
|
#define DEBUG_TYPE "polly-opt-pocc"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2013-06-14 14:26:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
#include "polly/ScopLib.h"
|
|
|
|
|
2011-10-06 08:03:35 +08:00
|
|
|
#include "isl/space.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "isl/map.h"
|
|
|
|
#include "isl/constraint.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
static cl::opt<std::string> PlutoFuse("pluto-fuse", cl::desc(""), cl::Hidden,
|
|
|
|
cl::value_desc("Set fuse mode of Pluto"),
|
2013-05-07 15:31:10 +08:00
|
|
|
cl::init("maxfuse"),
|
|
|
|
cl::cat(PollyCategory));
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
class Pocc : public ScopPass {
|
2013-06-14 14:26:33 +08:00
|
|
|
SmallString<128> PlutoStderr;
|
|
|
|
SmallString<128> PlutoStdout;
|
2013-03-23 09:05:07 +08:00
|
|
|
std::vector<const char *> arguments;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
explicit Pocc() : ScopPass(ID) {}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
std::string getFileName(Region *R) const;
|
|
|
|
virtual bool runOnScop(Scop &S);
|
|
|
|
void printScop(llvm::raw_ostream &OS) const;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
2012-08-30 19:49:38 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
private:
|
|
|
|
bool runTransform(Scop &S);
|
|
|
|
};
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char Pocc::ID = 0;
|
2012-08-30 19:49:38 +08:00
|
|
|
bool Pocc::runTransform(Scop &S) {
|
2011-04-29 14:27:02 +08:00
|
|
|
Dependences *D = &getAnalysis<Dependences>();
|
|
|
|
|
|
|
|
// Create the scop file.
|
2013-06-14 14:26:33 +08:00
|
|
|
SmallString<128> TempDir;
|
|
|
|
SmallString<128> ScopFile;
|
2013-07-03 00:13:07 +08:00
|
|
|
llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/true, TempDir);
|
2013-06-14 14:26:33 +08:00
|
|
|
ScopFile = TempDir;
|
|
|
|
llvm::sys::path::append(ScopFile, "polly.scop");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-06-14 14:26:33 +08:00
|
|
|
FILE *F = fopen(ScopFile.c_str(), "w");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
arguments.clear();
|
|
|
|
|
|
|
|
if (!F) {
|
2013-06-14 14:26:33 +08:00
|
|
|
errs() << "Cannot open file: " << TempDir.c_str() << "\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
errs() << "Skipping export.\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopLib scoplib(&S);
|
|
|
|
scoplib.print(F);
|
|
|
|
fclose(F);
|
|
|
|
|
|
|
|
// Execute pocc
|
2013-06-14 14:26:33 +08:00
|
|
|
std::string pocc = sys::FindProgramByName("pocc");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
arguments.push_back("pocc");
|
|
|
|
arguments.push_back("--read-scop");
|
2013-06-14 14:26:33 +08:00
|
|
|
arguments.push_back(ScopFile.c_str());
|
2011-04-29 14:27:02 +08:00
|
|
|
arguments.push_back("--pluto-tile-scat");
|
|
|
|
arguments.push_back("--candl-dep-isl-simp");
|
|
|
|
arguments.push_back("--cloogify-scheds");
|
|
|
|
arguments.push_back("--output-scop");
|
|
|
|
arguments.push_back("--pluto");
|
|
|
|
arguments.push_back("--pluto-bounds");
|
|
|
|
arguments.push_back("10");
|
|
|
|
arguments.push_back("--pluto-fuse");
|
|
|
|
|
|
|
|
arguments.push_back(PlutoFuse.c_str());
|
|
|
|
|
2011-10-24 04:59:44 +08:00
|
|
|
if (!DisablePollyTiling)
|
2011-04-29 14:27:02 +08:00
|
|
|
arguments.push_back("--pluto-tile");
|
|
|
|
|
2012-05-06 18:22:19 +08:00
|
|
|
if (PollyVectorizerChoice != VECTORIZER_NONE)
|
2011-04-29 14:27:02 +08:00
|
|
|
arguments.push_back("--pluto-prevector");
|
|
|
|
|
|
|
|
arguments.push_back(0);
|
|
|
|
|
2013-06-14 14:26:33 +08:00
|
|
|
PlutoStdout = TempDir;
|
|
|
|
llvm::sys::path::append(PlutoStdout, "pluto.stdout");
|
|
|
|
PlutoStderr = TempDir;
|
|
|
|
llvm::sys::path::append(PlutoStderr, "pluto.stderr");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-06-14 14:26:33 +08:00
|
|
|
std::vector<llvm::StringRef> Redirect;
|
|
|
|
Redirect.push_back(0);
|
|
|
|
Redirect.push_back(PlutoStdout.c_str());
|
|
|
|
Redirect.push_back(PlutoStderr.c_str());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-06-14 14:26:33 +08:00
|
|
|
sys::ExecuteAndWait(pocc, &arguments[0], 0,
|
|
|
|
(const llvm::StringRef **)&Redirect[0]);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Read the created scop file
|
2013-06-14 14:26:33 +08:00
|
|
|
SmallString<128> NewScopFile;
|
|
|
|
NewScopFile = TempDir;
|
|
|
|
llvm::sys::path::append(NewScopFile, "polly.pocc.c.scop");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-06-14 14:26:33 +08:00
|
|
|
FILE *poccFile = fopen(NewScopFile.c_str(), "r");
|
2011-04-29 14:27:02 +08:00
|
|
|
ScopLib newScoplib(&S, poccFile, D);
|
|
|
|
|
|
|
|
if (!newScoplib.updateScattering()) {
|
|
|
|
errs() << "Failure when calculating the optimization with "
|
|
|
|
"the following command: ";
|
2013-03-23 09:05:07 +08:00
|
|
|
for (std::vector<const char *>::const_iterator AI = arguments.begin(),
|
|
|
|
AE = arguments.end();
|
|
|
|
AI != AE; ++AI)
|
2011-04-29 14:27:02 +08:00
|
|
|
if (*AI)
|
|
|
|
errs() << " " << *AI;
|
|
|
|
errs() << "\n";
|
|
|
|
return false;
|
|
|
|
} else
|
|
|
|
fclose(poccFile);
|
|
|
|
|
2012-08-24 21:56:56 +08:00
|
|
|
if (PollyVectorizerChoice == VECTORIZER_NONE)
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the innermost dimension that is not a constant dimension. This
|
|
|
|
// dimension will be vectorized.
|
|
|
|
unsigned scatterDims = S.getScatterDim();
|
|
|
|
int lastLoop = scatterDims - 1;
|
|
|
|
|
|
|
|
while (lastLoop) {
|
|
|
|
bool isSingleValued = true;
|
|
|
|
|
|
|
|
for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
|
2011-10-06 08:04:05 +08:00
|
|
|
isl_map *scat = (*SI)->getScattering();
|
2011-04-29 14:27:02 +08:00
|
|
|
isl_map *projected = isl_map_project_out(scat, isl_dim_out, lastLoop,
|
|
|
|
scatterDims - lastLoop);
|
|
|
|
|
|
|
|
if (!isl_map_is_bijective(projected)) {
|
|
|
|
isSingleValued = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isSingleValued)
|
|
|
|
break;
|
|
|
|
|
|
|
|
lastLoop--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip mine the innermost loop.
|
|
|
|
for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
|
2011-10-24 04:59:17 +08:00
|
|
|
isl_map *scat = (*SI)->getScattering();
|
2011-10-06 08:04:05 +08:00
|
|
|
int scatDims = (*SI)->getNumScattering();
|
2011-10-24 04:59:17 +08:00
|
|
|
isl_space *Space = isl_space_alloc(S.getIslCtx(), S.getNumParams(),
|
|
|
|
scatDims, scatDims + 1);
|
2011-10-06 08:03:35 +08:00
|
|
|
isl_basic_map *map = isl_basic_map_universe(isl_space_copy(Space));
|
2011-10-24 04:59:17 +08:00
|
|
|
isl_local_space *LSpace = isl_local_space_from_space(Space);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
for (int i = 0; i <= lastLoop - 1; i++) {
|
2011-10-24 04:59:17 +08:00
|
|
|
isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
|
|
|
|
|
|
|
|
map = isl_basic_map_add_constraint(map, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = lastLoop; i < scatDims; i++) {
|
2011-10-24 04:59:17 +08:00
|
|
|
isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, i + 1, -1);
|
|
|
|
|
|
|
|
map = isl_basic_map_add_constraint(map, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_constraint *c;
|
|
|
|
|
|
|
|
int vectorWidth = 4;
|
2011-10-24 04:59:17 +08:00
|
|
|
c = isl_inequality_alloc(isl_local_space_copy(LSpace));
|
2011-04-29 14:27:02 +08:00
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, lastLoop, -vectorWidth);
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, lastLoop + 1, 1);
|
|
|
|
map = isl_basic_map_add_constraint(map, c);
|
|
|
|
|
2011-10-24 04:59:17 +08:00
|
|
|
c = isl_inequality_alloc(LSpace);
|
2011-04-29 14:27:02 +08:00
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, lastLoop, vectorWidth);
|
|
|
|
isl_constraint_set_coefficient_si(c, isl_dim_out, lastLoop + 1, -1);
|
|
|
|
isl_constraint_set_constant_si(c, vectorWidth - 1);
|
|
|
|
map = isl_basic_map_add_constraint(map, c);
|
|
|
|
|
|
|
|
isl_map *transform = isl_map_from_basic_map(map);
|
|
|
|
transform = isl_map_set_tuple_name(transform, isl_dim_out, "scattering");
|
|
|
|
transform = isl_map_set_tuple_name(transform, isl_dim_in, "scattering");
|
|
|
|
|
|
|
|
scat = isl_map_apply_range(scat, isl_map_copy(transform));
|
|
|
|
(*SI)->setScattering(scat);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-30 19:49:38 +08:00
|
|
|
bool Pocc::runOnScop(Scop &S) {
|
|
|
|
bool Result = runTransform(S);
|
|
|
|
DEBUG(printScop(dbgs()));
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void Pocc::printScop(raw_ostream &OS) const {
|
|
|
|
OwningPtr<MemoryBuffer> stdoutBuffer;
|
|
|
|
OwningPtr<MemoryBuffer> stderrBuffer;
|
|
|
|
|
|
|
|
OS << "Command line: ";
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
for (std::vector<const char *>::const_iterator AI = arguments.begin(),
|
|
|
|
AE = arguments.end();
|
|
|
|
AI != AE; ++AI)
|
2011-04-29 14:27:02 +08:00
|
|
|
if (*AI)
|
|
|
|
OS << " " << *AI;
|
|
|
|
|
|
|
|
OS << "\n";
|
|
|
|
|
2013-06-14 14:26:33 +08:00
|
|
|
if (error_code ec = MemoryBuffer::getFile(PlutoStdout, stdoutBuffer))
|
2012-08-30 19:49:38 +08:00
|
|
|
OS << "Could not open pocc stdout file: " + ec.message() << "\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
else {
|
|
|
|
OS << "pocc stdout: " << stdoutBuffer->getBufferIdentifier() << "\n";
|
|
|
|
OS << stdoutBuffer->getBuffer() << "\n";
|
|
|
|
}
|
|
|
|
|
2013-06-14 14:26:33 +08:00
|
|
|
if (error_code ec = MemoryBuffer::getFile(PlutoStderr, stderrBuffer))
|
2012-08-30 19:49:38 +08:00
|
|
|
OS << "Could not open pocc stderr file: " + ec.message() << "\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
else {
|
2013-06-14 14:26:33 +08:00
|
|
|
OS << "pocc stderr: " << PlutoStderr << "\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
OS << stderrBuffer->getBuffer() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pocc::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
ScopPass::getAnalysisUsage(AU);
|
|
|
|
AU.addRequired<Dependences>();
|
|
|
|
}
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Pass *polly::createPoccPass() { return new Pocc(); }
|
|
|
|
|
2011-11-23 03:40:19 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(Pocc, "polly-opt-pocc",
|
2013-03-23 09:05:07 +08:00
|
|
|
"Polly - Optimize the scop using pocc", false, false);
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(Dependences);
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopInfo);
|
2011-11-23 03:40:19 +08:00
|
|
|
INITIALIZE_PASS_END(Pocc, "polly-opt-pocc",
|
2011-10-08 08:30:40 +08:00
|
|
|
"Polly - Optimize the scop using pocc", false, false)
|
2011-04-29 14:27:02 +08:00
|
|
|
#endif /* SCOPLIB_FOUND */
|