2008-05-06 16:34:12 +00:00
|
|
|
//===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open
|
|
|
|
// Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-05-06 16:34:12 +00:00
|
|
|
// Compilation graph - implementation.
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-06 16:34:12 +00:00
|
|
|
#include "CompilationGraph.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 18:10:53 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-05-06 16:35:25 +00:00
|
|
|
#include "llvm/Support/DOTGraphTraits.h"
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2008-05-06 18:10:53 +00:00
|
|
|
#include <limits>
|
2008-05-06 18:07:14 +00:00
|
|
|
#include <queue>
|
2008-03-23 08:57:20 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace llvm;
|
2008-05-06 18:08:59 +00:00
|
|
|
using namespace llvmc;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
extern cl::list<std::string> InputFilenames;
|
|
|
|
extern cl::opt<std::string> OutputFilename;
|
2008-05-06 18:10:53 +00:00
|
|
|
extern cl::list<std::string> Languages;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 17:28:03 +00:00
|
|
|
namespace {
|
|
|
|
|
2008-05-06 18:10:53 +00:00
|
|
|
// Go through the list C and find the edge that isEnabled(); if
|
|
|
|
// there is no such edge, return the default edge; if there is no
|
|
|
|
// default edge, throw an exception.
|
2008-05-06 17:28:03 +00:00
|
|
|
template <class C>
|
|
|
|
const Edge* ChooseEdge(const C& EdgesContainer,
|
|
|
|
const std::string& NodeName = "root") {
|
|
|
|
const Edge* DefaultEdge = 0;
|
|
|
|
|
|
|
|
for (typename C::const_iterator B = EdgesContainer.begin(),
|
|
|
|
E = EdgesContainer.end(); B != E; ++B) {
|
|
|
|
const Edge* E = B->getPtr();
|
|
|
|
|
|
|
|
if (E->isDefault())
|
|
|
|
if (!DefaultEdge)
|
|
|
|
DefaultEdge = E;
|
|
|
|
else
|
|
|
|
throw std::runtime_error("Node " + NodeName
|
|
|
|
+ ": multiple default outward edges found!"
|
2008-05-06 18:10:53 +00:00
|
|
|
" Most probably a specification error.");
|
2008-05-06 17:28:03 +00:00
|
|
|
if (E->isEnabled())
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DefaultEdge)
|
|
|
|
return DefaultEdge;
|
|
|
|
else
|
|
|
|
throw std::runtime_error("Node " + NodeName
|
|
|
|
+ ": no default outward edge found!"
|
2008-05-06 18:10:53 +00:00
|
|
|
" Most probably a specification error.");
|
2008-05-06 17:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
CompilationGraph::CompilationGraph() {
|
|
|
|
NodesMap["root"] = Node(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node& CompilationGraph::getNode(const std::string& ToolName) {
|
|
|
|
nodes_map_type::iterator I = NodesMap.find(ToolName);
|
|
|
|
if (I == NodesMap.end())
|
2008-05-06 16:36:50 +00:00
|
|
|
throw std::runtime_error("Node " + ToolName + " is not in the graph");
|
2008-05-06 16:35:25 +00:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Node& CompilationGraph::getNode(const std::string& ToolName) const {
|
|
|
|
nodes_map_type::const_iterator I = NodesMap.find(ToolName);
|
|
|
|
if (I == NodesMap.end())
|
2008-05-06 16:36:50 +00:00
|
|
|
throw std::runtime_error("Node " + ToolName + " is not in the graph!");
|
2008-05-06 16:35:25 +00:00
|
|
|
return I->second;
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
|
|
|
|
LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
|
2008-03-23 08:57:20 +00:00
|
|
|
if (Lang == ExtsToLangs.end())
|
2008-05-06 16:35:25 +00:00
|
|
|
throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
|
|
|
|
return Lang->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CompilationGraph::tools_vector_type&
|
|
|
|
CompilationGraph::getToolsVector(const std::string& LangName) const
|
|
|
|
{
|
|
|
|
tools_map_type::const_iterator I = ToolsMap.find(LangName);
|
|
|
|
if (I == ToolsMap.end())
|
2008-05-06 18:10:53 +00:00
|
|
|
throw std::runtime_error("No tool corresponding to the language "
|
|
|
|
+ LangName + "found!");
|
2008-05-06 16:35:25 +00:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
void CompilationGraph::insertNode(Tool* V) {
|
2008-05-06 17:26:53 +00:00
|
|
|
if (NodesMap.count(V->Name()) == 0) {
|
2008-05-06 16:35:25 +00:00
|
|
|
Node N;
|
|
|
|
N.OwningGraph = this;
|
|
|
|
N.ToolPtr = V;
|
|
|
|
NodesMap[V->Name()] = N;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 16:36:50 +00:00
|
|
|
void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
|
2008-05-06 17:26:53 +00:00
|
|
|
Node& B = getNode(E->ToolName());
|
2008-05-06 16:35:25 +00:00
|
|
|
if (A == "root") {
|
2008-05-06 17:26:53 +00:00
|
|
|
const std::string& InputLanguage = B.ToolPtr->InputLanguage();
|
2008-05-06 17:28:03 +00:00
|
|
|
ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
|
2008-05-06 16:36:50 +00:00
|
|
|
NodesMap["root"].AddEdge(E);
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-05-06 16:36:06 +00:00
|
|
|
Node& N = getNode(A);
|
2008-05-06 16:36:50 +00:00
|
|
|
N.AddEdge(E);
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
2008-05-06 17:26:53 +00:00
|
|
|
// Increase the inward edge counter.
|
|
|
|
B.IncrInEdges();
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 18:10:53 +00:00
|
|
|
namespace {
|
|
|
|
sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
|
|
|
|
const std::string& Suffix) {
|
|
|
|
sys::Path Out = TempDir;
|
|
|
|
Out.appendComponent(BaseName);
|
|
|
|
Out.appendSuffix(Suffix);
|
|
|
|
Out.makeUnique(true, NULL);
|
|
|
|
return Out;
|
|
|
|
}
|
2008-05-06 18:10:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 17:25:51 +00:00
|
|
|
// Pass input file through the chain until we bump into a Join node or
|
|
|
|
// a node that says that it is the last.
|
2008-05-06 18:10:20 +00:00
|
|
|
void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
|
|
|
|
const Node* StartNode,
|
|
|
|
const sys::Path& TempDir) const {
|
2008-05-06 17:25:51 +00:00
|
|
|
bool Last = false;
|
2008-05-06 18:10:20 +00:00
|
|
|
sys::Path In = InFile;
|
2008-05-06 17:28:03 +00:00
|
|
|
const Node* CurNode = StartNode;
|
2008-05-06 17:25:51 +00:00
|
|
|
|
|
|
|
while(!Last) {
|
2008-05-06 17:27:37 +00:00
|
|
|
sys::Path Out;
|
2008-05-06 17:28:03 +00:00
|
|
|
Tool* CurTool = CurNode->ToolPtr.getPtr();
|
2008-05-06 17:25:51 +00:00
|
|
|
|
|
|
|
if (CurTool->IsJoin()) {
|
2008-05-06 18:10:20 +00:00
|
|
|
JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
|
|
|
|
JT.AddToJoinList(In);
|
2008-05-06 17:25:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:10:20 +00:00
|
|
|
// Since toolchains do not have to end with a Join node, we should
|
|
|
|
// check if this Node is the last.
|
2008-05-06 17:28:03 +00:00
|
|
|
if (!CurNode->HasChildren() || CurTool->IsLast()) {
|
2008-05-06 18:10:20 +00:00
|
|
|
if (!OutputFilename.empty()) {
|
|
|
|
Out.set(OutputFilename);
|
|
|
|
}
|
|
|
|
else {
|
2008-05-06 17:25:51 +00:00
|
|
|
Out.set(In.getBasename());
|
2008-05-06 18:10:20 +00:00
|
|
|
Out.appendSuffix(CurTool->OutputSuffix());
|
|
|
|
}
|
2008-05-06 17:25:51 +00:00
|
|
|
Last = true;
|
|
|
|
}
|
|
|
|
else {
|
2008-05-06 18:10:20 +00:00
|
|
|
Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
|
2008-05-06 17:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CurTool->GenerateAction(In, Out).Execute() != 0)
|
|
|
|
throw std::runtime_error("Tool returned error code!");
|
|
|
|
|
2008-05-06 18:10:53 +00:00
|
|
|
if (Last)
|
|
|
|
return;
|
|
|
|
|
2008-05-06 17:28:03 +00:00
|
|
|
CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
|
|
|
|
CurNode->Name())->ToolName());
|
2008-05-06 17:25:51 +00:00
|
|
|
In = Out; Out.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
// Sort the nodes in topological order.
|
|
|
|
void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
|
|
|
|
std::queue<const Node*> Q;
|
|
|
|
Q.push(&getNode("root"));
|
|
|
|
|
|
|
|
while (!Q.empty()) {
|
|
|
|
const Node* A = Q.front();
|
|
|
|
Q.pop();
|
|
|
|
Out.push_back(A);
|
|
|
|
for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
|
|
|
|
EB != EE; ++EB) {
|
|
|
|
Node* B = &getNode((*EB)->ToolName());
|
|
|
|
B->DecrInEdges();
|
|
|
|
if (B->HasNoInEdges())
|
|
|
|
Q.push(B);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool NotJoinNode(const Node* N) {
|
|
|
|
return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call TopologicalSort and filter the resulting list to include
|
|
|
|
// only Join nodes.
|
|
|
|
void CompilationGraph::
|
|
|
|
TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
|
|
|
|
std::vector<const Node*> TopSorted;
|
|
|
|
TopologicalSort(TopSorted);
|
|
|
|
std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
|
|
|
|
std::back_inserter(Out), NotJoinNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find head of the toolchain corresponding to the given file.
|
2008-05-06 18:10:53 +00:00
|
|
|
const Node* CompilationGraph::
|
|
|
|
FindToolChain(const sys::Path& In, const std::string* forceLanguage) const {
|
|
|
|
const std::string& InLanguage =
|
|
|
|
forceLanguage ? *forceLanguage : getLanguage(In);
|
2008-05-06 18:07:14 +00:00
|
|
|
const tools_vector_type& TV = getToolsVector(InLanguage);
|
|
|
|
if (TV.empty())
|
|
|
|
throw std::runtime_error("No toolchain corresponding to language"
|
|
|
|
+ InLanguage + " found!");
|
|
|
|
return &getNode(ChooseEdge(TV)->ToolName());
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:08:59 +00:00
|
|
|
// Build the targets. Command-line options are passed through
|
|
|
|
// temporary variables.
|
2008-05-06 18:07:14 +00:00
|
|
|
int CompilationGraph::Build (const sys::Path& TempDir) {
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 18:10:53 +00:00
|
|
|
// This is related to -x option handling.
|
|
|
|
cl::list<std::string>::const_iterator xIter = Languages.begin(),
|
|
|
|
xBegin = xIter, xEnd = Languages.end();
|
|
|
|
bool xEmpty = true;
|
|
|
|
const std::string* xLanguage = 0;
|
|
|
|
unsigned xPos = 0, xPosNext = 0, filePos = 0;
|
|
|
|
|
|
|
|
if (xIter != xEnd) {
|
|
|
|
xEmpty = false;
|
|
|
|
xPos = Languages.getPosition(xIter - xBegin);
|
|
|
|
cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
|
|
|
|
xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
|
|
|
|
: Languages.getPosition(xNext - xBegin);
|
|
|
|
xLanguage = (*xIter == "none") ? 0 : &(*xIter);
|
|
|
|
}
|
|
|
|
|
2008-05-06 17:28:03 +00:00
|
|
|
// For each input file:
|
2008-03-23 08:57:20 +00:00
|
|
|
for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
|
2008-05-06 18:10:53 +00:00
|
|
|
CB = B, E = InputFilenames.end(); B != E; ++B) {
|
2008-05-06 17:27:37 +00:00
|
|
|
sys::Path In = sys::Path(*B);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 18:10:53 +00:00
|
|
|
// Code for handling the -x option.
|
|
|
|
// Output: std::string* xLanguage (can be NULL).
|
|
|
|
if (!xEmpty) {
|
|
|
|
filePos = InputFilenames.getPosition(B - CB);
|
|
|
|
|
|
|
|
if (xPos < filePos) {
|
|
|
|
if (filePos < xPosNext) {
|
|
|
|
xLanguage = (*xIter == "none") ? 0 : &(*xIter);
|
|
|
|
}
|
|
|
|
else { // filePos >= xPosNext
|
|
|
|
// Skip xIters while filePos > xPosNext
|
|
|
|
while (filePos > xPosNext) {
|
|
|
|
++xIter;
|
|
|
|
xPos = xPosNext;
|
|
|
|
|
|
|
|
cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
|
|
|
|
if (xNext == xEnd)
|
|
|
|
xPosNext = std::numeric_limits<unsigned>::max();
|
|
|
|
else
|
|
|
|
xPosNext = Languages.getPosition(xNext - xBegin);
|
|
|
|
xLanguage = (*xIter == "none") ? 0 : &(*xIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
// Find the toolchain corresponding to this file.
|
2008-05-06 18:10:53 +00:00
|
|
|
const Node* N = FindToolChain(In, xLanguage);
|
2008-05-06 18:07:14 +00:00
|
|
|
// Pass file through the chain starting at head.
|
|
|
|
PassThroughGraph(In, N, TempDir);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
std::vector<const Node*> JTV;
|
|
|
|
TopologicalSortFilterJoinNodes(JTV);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
// For all join nodes in topological order:
|
|
|
|
for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
|
|
|
|
B != E; ++B) {
|
2008-05-06 18:08:59 +00:00
|
|
|
|
2008-05-06 18:07:48 +00:00
|
|
|
sys::Path Out;
|
|
|
|
const Node* CurNode = *B;
|
|
|
|
JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
|
|
|
|
bool IsLast = false;
|
|
|
|
|
2008-05-06 18:10:20 +00:00
|
|
|
// Are there any files to be joined?
|
2008-05-06 18:07:48 +00:00
|
|
|
if (JT->JoinListEmpty())
|
|
|
|
continue;
|
|
|
|
|
2008-05-06 18:10:20 +00:00
|
|
|
// Is this the last tool in the chain?
|
|
|
|
// NOTE: we can process several chains in parallel.
|
2008-05-06 18:07:48 +00:00
|
|
|
if (!CurNode->HasChildren() || JT->IsLast()) {
|
|
|
|
if (OutputFilename.empty()) {
|
|
|
|
Out.set("a");
|
|
|
|
Out.appendSuffix(JT->OutputSuffix());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Out.set(OutputFilename);
|
|
|
|
IsLast = true;
|
|
|
|
}
|
|
|
|
else {
|
2008-05-06 18:10:20 +00:00
|
|
|
Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
|
2008-05-06 18:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (JT->GenerateAction(Out).Execute() != 0)
|
|
|
|
throw std::runtime_error("Tool returned error code!");
|
|
|
|
|
|
|
|
if (!IsLast) {
|
|
|
|
const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges,
|
|
|
|
CurNode->Name())->ToolName());
|
|
|
|
PassThroughGraph(Out, NextNode, TempDir);
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-06 17:27:37 +00:00
|
|
|
// Code related to graph visualization.
|
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
namespace llvm {
|
|
|
|
template <>
|
2008-05-06 18:08:59 +00:00
|
|
|
struct DOTGraphTraits<llvmc::CompilationGraph*>
|
2008-05-06 16:35:25 +00:00
|
|
|
: public DefaultDOTGraphTraits
|
|
|
|
{
|
|
|
|
|
2008-05-06 17:26:14 +00:00
|
|
|
template<typename GraphType>
|
|
|
|
static std::string getNodeLabel(const Node* N, const GraphType&)
|
|
|
|
{
|
|
|
|
if (N->ToolPtr)
|
|
|
|
if (N->ToolPtr->IsJoin())
|
|
|
|
return N->Name() + "\n (join" +
|
|
|
|
(N->HasChildren() ? ")"
|
|
|
|
: std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
|
|
|
|
else
|
|
|
|
return N->Name();
|
|
|
|
else
|
|
|
|
return "root";
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 17:26:14 +00:00
|
|
|
template<typename EdgeIter>
|
|
|
|
static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
|
|
|
|
if (N->ToolPtr)
|
|
|
|
return N->ToolPtr->OutputLanguage();
|
|
|
|
else
|
|
|
|
return I->ToolPtr->InputLanguage();
|
|
|
|
}
|
2008-05-06 16:35:25 +00:00
|
|
|
};
|
2008-05-06 17:26:14 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
void CompilationGraph::writeGraph() {
|
|
|
|
std::ofstream O("CompilationGraph.dot");
|
|
|
|
|
2008-05-06 16:37:33 +00:00
|
|
|
if (O.good()) {
|
2008-05-06 18:07:48 +00:00
|
|
|
llvm::WriteGraph(this, "compilation-graph");
|
2008-05-06 16:35:25 +00:00
|
|
|
O.close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw std::runtime_error("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilationGraph::viewGraph() {
|
2008-05-06 16:36:50 +00:00
|
|
|
llvm::ViewGraph(this, "compilation-graph");
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|