2008-05-06 18:34:12 +02:00
|
|
|
//===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
|
2008-03-23 09:57:20 +01: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 18:34:12 +02:00
|
|
|
// Compilation graph - implementation.
|
2008-03-23 09:57:20 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-25 20:20:10 +02:00
|
|
|
#include "llvm/CompilerDriver/BuiltinOptions.h"
|
2008-09-22 22:50:40 +02:00
|
|
|
#include "llvm/CompilerDriver/CompilationGraph.h"
|
2009-03-02 10:01:14 +01:00
|
|
|
#include "llvm/CompilerDriver/Error.h"
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-06 20:10:53 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-05-06 18:35:25 +02:00
|
|
|
#include "llvm/Support/DOTGraphTraits.h"
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2009-06-30 06:07:12 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-06 20:07:14 +02:00
|
|
|
#include <algorithm>
|
2009-01-09 17:16:27 +01:00
|
|
|
#include <cstring>
|
2008-05-06 20:07:14 +02:00
|
|
|
#include <iterator>
|
2008-05-06 20:10:53 +02:00
|
|
|
#include <limits>
|
2008-05-06 20:07:14 +02:00
|
|
|
#include <queue>
|
2008-03-23 09:57:20 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace llvm;
|
2008-05-06 20:08:59 +02:00
|
|
|
using namespace llvmc;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-30 08:19:52 +02:00
|
|
|
namespace llvmc {
|
|
|
|
|
2008-09-22 22:47:46 +02:00
|
|
|
const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
|
|
|
|
LanguageMap::const_iterator Lang = this->find(File.getSuffix());
|
|
|
|
if (Lang == this->end())
|
2009-12-17 22:02:39 +01:00
|
|
|
throw std::runtime_error(("Unknown suffix: " + File.getSuffix()).str());
|
2008-05-30 08:19:52 +02:00
|
|
|
return Lang->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 19:28:03 +02:00
|
|
|
namespace {
|
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
/// ChooseEdge - Return the edge with the maximum weight.
|
2008-05-06 19:28:03 +02:00
|
|
|
template <class C>
|
|
|
|
const Edge* ChooseEdge(const C& EdgesContainer,
|
2008-05-06 20:15:12 +02:00
|
|
|
const InputLanguagesSet& InLangs,
|
2008-05-06 19:28:03 +02:00
|
|
|
const std::string& NodeName = "root") {
|
2008-05-06 20:14:24 +02:00
|
|
|
const Edge* MaxEdge = 0;
|
|
|
|
unsigned MaxWeight = 0;
|
|
|
|
bool SingleMax = true;
|
2008-05-06 19:28:03 +02:00
|
|
|
|
|
|
|
for (typename C::const_iterator B = EdgesContainer.begin(),
|
|
|
|
E = EdgesContainer.end(); B != E; ++B) {
|
2008-10-02 20:39:11 +02:00
|
|
|
const Edge* e = B->getPtr();
|
|
|
|
unsigned EW = e->Weight(InLangs);
|
2008-05-06 20:14:24 +02:00
|
|
|
if (EW > MaxWeight) {
|
2008-10-02 20:39:11 +02:00
|
|
|
MaxEdge = e;
|
2008-05-06 20:14:24 +02:00
|
|
|
MaxWeight = EW;
|
|
|
|
SingleMax = true;
|
2008-05-06 20:18:58 +02:00
|
|
|
} else if (EW == MaxWeight) {
|
2008-05-06 20:14:24 +02:00
|
|
|
SingleMax = false;
|
|
|
|
}
|
2008-05-06 19:28:03 +02:00
|
|
|
}
|
|
|
|
|
2008-05-06 20:14:24 +02:00
|
|
|
if (!SingleMax)
|
|
|
|
throw std::runtime_error("Node " + NodeName +
|
|
|
|
": multiple maximal outward edges found!"
|
|
|
|
" Most probably a specification error.");
|
|
|
|
if (!MaxEdge)
|
|
|
|
throw std::runtime_error("Node " + NodeName +
|
|
|
|
": no maximal outward edge found!"
|
2008-05-06 20:10:53 +02:00
|
|
|
" Most probably a specification error.");
|
2008-05-06 20:14:24 +02:00
|
|
|
return MaxEdge;
|
2008-05-06 19:23:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-11-26 23:59:45 +01:00
|
|
|
void Node::AddEdge(Edge* Edg) {
|
|
|
|
// If there already was an edge between two nodes, modify it instead
|
|
|
|
// of adding a new edge.
|
|
|
|
const std::string& ToolName = Edg->ToolName();
|
|
|
|
for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
if ((*B)->ToolName() == ToolName) {
|
|
|
|
llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:35:25 +02: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 18:36:50 +02:00
|
|
|
throw std::runtime_error("Node " + ToolName + " is not in the graph");
|
2008-05-06 18:35:25 +02: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 18:36:50 +02:00
|
|
|
throw std::runtime_error("Node " + ToolName + " is not in the graph!");
|
2008-05-06 18:35:25 +02:00
|
|
|
return I->second;
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-06 20:16:52 +02:00
|
|
|
// Find the tools list corresponding to the given language name.
|
2008-05-06 18:35:25 +02:00
|
|
|
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 20:10:53 +02:00
|
|
|
throw std::runtime_error("No tool corresponding to the language "
|
2008-05-30 08:16:59 +02:00
|
|
|
+ LangName + " found");
|
2008-05-06 18:35:25 +02:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:36:06 +02:00
|
|
|
void CompilationGraph::insertNode(Tool* V) {
|
2008-11-12 01:04:46 +01:00
|
|
|
if (NodesMap.count(V->Name()) == 0)
|
|
|
|
NodesMap[V->Name()] = Node(this, V);
|
2008-05-06 18:35:25 +02:00
|
|
|
}
|
|
|
|
|
2008-05-30 08:18:16 +02:00
|
|
|
void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
|
|
|
|
Node& B = getNode(Edg->ToolName());
|
2008-05-06 18:35:25 +02:00
|
|
|
if (A == "root") {
|
2008-05-30 08:24:49 +02:00
|
|
|
const char** InLangs = B.ToolPtr->InputLanguages();
|
|
|
|
for (;*InLangs; ++InLangs)
|
|
|
|
ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
|
2008-05-30 08:18:16 +02:00
|
|
|
NodesMap["root"].AddEdge(Edg);
|
2008-05-06 18:35:25 +02:00
|
|
|
}
|
|
|
|
else {
|
2008-05-06 18:36:06 +02:00
|
|
|
Node& N = getNode(A);
|
2008-05-30 08:18:16 +02:00
|
|
|
N.AddEdge(Edg);
|
2008-05-06 18:35:25 +02:00
|
|
|
}
|
2008-05-06 19:26:53 +02:00
|
|
|
// Increase the inward edge counter.
|
|
|
|
B.IncrInEdges();
|
2008-05-06 18:35:25 +02:00
|
|
|
}
|
|
|
|
|
2008-05-06 19:25:51 +02: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 20:10:20 +02:00
|
|
|
void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
|
|
|
|
const Node* StartNode,
|
2008-05-06 20:15:12 +02:00
|
|
|
const InputLanguagesSet& InLangs,
|
2008-09-22 22:47:46 +02:00
|
|
|
const sys::Path& TempDir,
|
|
|
|
const LanguageMap& LangMap) const {
|
2008-05-06 20:10:20 +02:00
|
|
|
sys::Path In = InFile;
|
2008-05-06 19:28:03 +02:00
|
|
|
const Node* CurNode = StartNode;
|
2008-05-06 19:25:51 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
while(true) {
|
2008-05-06 19:28:03 +02:00
|
|
|
Tool* CurTool = CurNode->ToolPtr.getPtr();
|
2008-05-06 19:25:51 +02:00
|
|
|
|
|
|
|
if (CurTool->IsJoin()) {
|
2008-05-06 20:10:20 +02:00
|
|
|
JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
|
|
|
|
JT.AddToJoinList(In);
|
2008-05-06 19:25:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
Action CurAction = CurTool->GenerateAction(In, CurNode->HasChildren(),
|
|
|
|
TempDir, InLangs, LangMap);
|
2008-05-06 19:25:51 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (int ret = CurAction.Execute())
|
2008-05-12 18:31:42 +02:00
|
|
|
throw error_code(ret);
|
2008-05-06 19:25:51 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (CurAction.StopCompilation())
|
2008-05-06 20:10:53 +02:00
|
|
|
return;
|
|
|
|
|
2008-05-06 19:28:03 +02:00
|
|
|
CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
|
2008-05-06 20:15:12 +02:00
|
|
|
InLangs,
|
2008-05-06 19:28:03 +02:00
|
|
|
CurNode->Name())->ToolName());
|
2008-12-07 17:41:11 +01:00
|
|
|
In = CurAction.OutFile();
|
2008-05-06 19:25:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 20:16:52 +02:00
|
|
|
// Find the head of the toolchain corresponding to the given file.
|
2008-05-06 20:15:12 +02:00
|
|
|
// Also, insert an input language into InLangs.
|
2008-05-06 20:10:53 +02:00
|
|
|
const Node* CompilationGraph::
|
2008-09-22 22:47:46 +02:00
|
|
|
FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
|
|
|
|
InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
|
2008-05-06 20:15:12 +02:00
|
|
|
|
|
|
|
// Determine the input language.
|
2008-05-06 20:10:53 +02:00
|
|
|
const std::string& InLanguage =
|
2008-09-22 22:47:46 +02:00
|
|
|
ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
|
2008-05-06 20:15:12 +02:00
|
|
|
|
|
|
|
// Add the current input language to the input language set.
|
|
|
|
InLangs.insert(InLanguage);
|
|
|
|
|
|
|
|
// Find the toolchain for the input language.
|
2008-05-06 20:07:14 +02:00
|
|
|
const tools_vector_type& TV = getToolsVector(InLanguage);
|
|
|
|
if (TV.empty())
|
2008-05-12 18:31:42 +02:00
|
|
|
throw std::runtime_error("No toolchain corresponding to language "
|
|
|
|
+ InLanguage + " found");
|
2008-05-06 20:15:12 +02:00
|
|
|
return &getNode(ChooseEdge(TV, InLangs)->ToolName());
|
2008-05-06 20:07:14 +02:00
|
|
|
}
|
|
|
|
|
2008-05-06 20:15:35 +02:00
|
|
|
// Helper function used by Build().
|
|
|
|
// Traverses initial portions of the toolchains (up to the first Join node).
|
|
|
|
// This function is also responsible for handling the -x option.
|
|
|
|
void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
|
2008-09-22 22:47:46 +02:00
|
|
|
const sys::Path& TempDir,
|
|
|
|
const LanguageMap& LangMap) {
|
2008-05-06 20:10:53 +02: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 19:28:03 +02:00
|
|
|
// For each input file:
|
2008-03-23 09:57:20 +01:00
|
|
|
for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
|
2008-05-06 20:10:53 +02:00
|
|
|
CB = B, E = InputFilenames.end(); B != E; ++B) {
|
2008-05-06 19:27:37 +02:00
|
|
|
sys::Path In = sys::Path(*B);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-06 20:10:53 +02: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 20:07:14 +02:00
|
|
|
// Find the toolchain corresponding to this file.
|
2008-09-22 22:47:46 +02:00
|
|
|
const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
|
2008-05-06 20:07:14 +02:00
|
|
|
// Pass file through the chain starting at head.
|
2008-09-22 22:47:46 +02:00
|
|
|
PassThroughGraph(In, N, InLangs, TempDir, LangMap);
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
2008-05-06 20:15:35 +02:00
|
|
|
}
|
|
|
|
|
2008-05-06 20:16:52 +02: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);
|
|
|
|
}
|
|
|
|
|
2008-09-22 22:47:46 +02:00
|
|
|
int CompilationGraph::Build (const sys::Path& TempDir,
|
|
|
|
const LanguageMap& LangMap) {
|
2008-05-06 20:15:35 +02:00
|
|
|
|
|
|
|
InputLanguagesSet InLangs;
|
|
|
|
|
|
|
|
// Traverse initial parts of the toolchains and fill in InLangs.
|
2008-09-22 22:47:46 +02:00
|
|
|
BuildInitial(InLangs, TempDir, LangMap);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-06 20:07:14 +02:00
|
|
|
std::vector<const Node*> JTV;
|
|
|
|
TopologicalSortFilterJoinNodes(JTV);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-06 20:07:14 +02: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 20:08:59 +02:00
|
|
|
|
2008-05-06 20:07:48 +02:00
|
|
|
const Node* CurNode = *B;
|
|
|
|
JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
|
|
|
|
|
2008-05-06 20:16:52 +02:00
|
|
|
// Are there any files in the join list?
|
2008-05-06 20:07:48 +02:00
|
|
|
if (JT->JoinListEmpty())
|
|
|
|
continue;
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
Action CurAction = JT->GenerateAction(CurNode->HasChildren(),
|
|
|
|
TempDir, InLangs, LangMap);
|
2008-05-06 20:07:48 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (int ret = CurAction.Execute())
|
2008-05-12 18:31:42 +02:00
|
|
|
throw error_code(ret);
|
2008-05-06 20:07:48 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (CurAction.StopCompilation())
|
|
|
|
return 0;
|
|
|
|
|
2008-12-07 17:45:37 +01:00
|
|
|
const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
|
|
|
|
CurNode->Name())->ToolName());
|
|
|
|
PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
|
|
|
|
InLangs, TempDir, LangMap);
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
2008-05-06 18:35:25 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-09 17:16:27 +01:00
|
|
|
int CompilationGraph::CheckLanguageNames() const {
|
|
|
|
int ret = 0;
|
|
|
|
// Check that names for output and input languages on all edges do match.
|
|
|
|
for (const_nodes_iterator B = this->NodesMap.begin(),
|
|
|
|
E = this->NodesMap.end(); B != E; ++B) {
|
|
|
|
|
|
|
|
const Node & N1 = B->second;
|
|
|
|
if (N1.ToolPtr) {
|
|
|
|
for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
|
|
|
|
EB != EE; ++EB) {
|
|
|
|
const Node& N2 = this->getNode((*EB)->ToolName());
|
|
|
|
|
|
|
|
if (!N2.ToolPtr) {
|
|
|
|
++ret;
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
|
|
|
|
<< "' back to the root!\n\n";
|
2009-01-09 17:16:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* OutLang = N1.ToolPtr->OutputLanguage();
|
|
|
|
const char** InLangs = N2.ToolPtr->InputLanguages();
|
|
|
|
bool eq = false;
|
|
|
|
for (;*InLangs; ++InLangs) {
|
|
|
|
if (std::strcmp(OutLang, *InLangs) == 0) {
|
|
|
|
eq = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eq) {
|
|
|
|
++ret;
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << "Error: Output->input language mismatch in the edge '"
|
|
|
|
<< N1.ToolPtr->Name() << "' -> '" << N2.ToolPtr->Name()
|
|
|
|
<< "'!\n"
|
|
|
|
<< "Expected one of { ";
|
2009-01-09 17:16:27 +01:00
|
|
|
|
|
|
|
InLangs = N2.ToolPtr->InputLanguages();
|
|
|
|
for (;*InLangs; ++InLangs) {
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
|
2009-01-09 17:16:27 +01:00
|
|
|
}
|
|
|
|
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << " }, but got '" << OutLang << "'!\n\n";
|
2009-01-09 17:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CompilationGraph::CheckMultipleDefaultEdges() const {
|
|
|
|
int ret = 0;
|
|
|
|
InputLanguagesSet Dummy;
|
|
|
|
|
2009-01-30 03:12:57 +01:00
|
|
|
// For all nodes, just iterate over the outgoing edges and check if there is
|
|
|
|
// more than one edge with maximum weight.
|
2009-01-09 17:16:27 +01:00
|
|
|
for (const_nodes_iterator B = this->NodesMap.begin(),
|
|
|
|
E = this->NodesMap.end(); B != E; ++B) {
|
|
|
|
const Node& N = B->second;
|
|
|
|
unsigned MaxWeight = 0;
|
|
|
|
|
|
|
|
// Ignore the root node.
|
|
|
|
if (!N.ToolPtr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
|
|
|
|
EB != EE; ++EB) {
|
|
|
|
unsigned EdgeWeight = (*EB)->Weight(Dummy);
|
|
|
|
if (EdgeWeight > MaxWeight) {
|
|
|
|
MaxWeight = EdgeWeight;
|
|
|
|
}
|
|
|
|
else if (EdgeWeight == MaxWeight) {
|
|
|
|
++ret;
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << "Error: there are multiple maximal edges stemming from the '"
|
|
|
|
<< N.ToolPtr->Name() << "' node!\n\n";
|
2009-01-09 17:16:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CompilationGraph::CheckCycles() {
|
|
|
|
unsigned deleted = 0;
|
|
|
|
std::queue<Node*> Q;
|
|
|
|
Q.push(&getNode("root"));
|
|
|
|
|
2009-01-30 03:12:57 +01:00
|
|
|
// Try to delete all nodes that have no ingoing edges, starting from the
|
|
|
|
// root. If there are any nodes left after this operation, then we have a
|
|
|
|
// cycle. This relies on '--check-graph' not performing the topological sort.
|
2009-01-09 17:16:27 +01:00
|
|
|
while (!Q.empty()) {
|
|
|
|
Node* A = Q.front();
|
|
|
|
Q.pop();
|
|
|
|
++deleted;
|
|
|
|
|
|
|
|
for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
|
|
|
|
EB != EE; ++EB) {
|
|
|
|
Node* B = &getNode((*EB)->ToolName());
|
|
|
|
B->DecrInEdges();
|
|
|
|
if (B->HasNoInEdges())
|
|
|
|
Q.push(B);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deleted != NodesMap.size()) {
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << "Error: there are cycles in the compilation graph!\n"
|
|
|
|
<< "Try inspecting the diagram produced by "
|
|
|
|
<< "'llvmc --view-graph'.\n\n";
|
2009-01-09 17:16:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CompilationGraph::Check () {
|
|
|
|
// We try to catch as many errors as we can in one go.
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
// Check that output/input language names match.
|
|
|
|
ret += this->CheckLanguageNames();
|
|
|
|
|
|
|
|
// Check for multiple default edges.
|
|
|
|
ret += this->CheckMultipleDefaultEdges();
|
|
|
|
|
|
|
|
// Check for cycles.
|
|
|
|
ret += this->CheckCycles();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-05-06 19:27:37 +02:00
|
|
|
// Code related to graph visualization.
|
|
|
|
|
2008-05-06 18:35:25 +02:00
|
|
|
namespace llvm {
|
|
|
|
template <>
|
2008-05-06 20:08:59 +02:00
|
|
|
struct DOTGraphTraits<llvmc::CompilationGraph*>
|
2008-05-06 18:35:25 +02:00
|
|
|
: public DefaultDOTGraphTraits
|
|
|
|
{
|
2009-11-30 14:34:51 +01:00
|
|
|
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
|
2008-05-06 18:35:25 +02:00
|
|
|
|
2008-05-06 19:26:14 +02:00
|
|
|
template<typename GraphType>
|
2009-11-30 14:14:13 +01:00
|
|
|
static std::string getNodeLabel(const Node* N, const GraphType&)
|
2008-05-06 19:26:14 +02:00
|
|
|
{
|
|
|
|
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 09:57:20 +01:00
|
|
|
|
2008-05-06 19:26:14 +02:00
|
|
|
template<typename EdgeIter>
|
|
|
|
static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
|
2008-05-30 08:18:16 +02:00
|
|
|
if (N->ToolPtr) {
|
2008-05-06 19:26:14 +02:00
|
|
|
return N->ToolPtr->OutputLanguage();
|
2008-05-30 08:18:16 +02:00
|
|
|
}
|
|
|
|
else {
|
2008-05-30 08:24:49 +02:00
|
|
|
const char** InLangs = I->ToolPtr->InputLanguages();
|
2008-05-30 08:18:16 +02:00
|
|
|
std::string ret;
|
|
|
|
|
2008-05-30 08:24:49 +02:00
|
|
|
for (; *InLangs; ++InLangs) {
|
|
|
|
if (*(InLangs + 1)) {
|
|
|
|
ret += *InLangs;
|
|
|
|
ret += ", ";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret += *InLangs;
|
|
|
|
}
|
2008-05-30 08:18:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2008-05-06 19:26:14 +02:00
|
|
|
}
|
2008-05-06 18:35:25 +02:00
|
|
|
};
|
2008-05-06 19:26:14 +02:00
|
|
|
|
2008-05-06 18:35:25 +02:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-03-27 13:57:14 +01:00
|
|
|
void CompilationGraph::writeGraph(const std::string& OutputFilename) {
|
2009-08-23 09:19:13 +02:00
|
|
|
std::string ErrorInfo;
|
|
|
|
raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
|
2008-05-06 18:35:25 +02:00
|
|
|
|
2009-08-23 09:19:13 +02:00
|
|
|
if (ErrorInfo.empty()) {
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << "Writing '"<< OutputFilename << "' file...";
|
2009-03-26 22:23:48 +01:00
|
|
|
llvm::WriteGraph(O, this);
|
2009-06-30 06:07:12 +02:00
|
|
|
errs() << "done.\n";
|
2008-05-06 18:35:25 +02:00
|
|
|
}
|
|
|
|
else {
|
2009-03-27 13:57:14 +01:00
|
|
|
throw std::runtime_error("Error opening file '" + OutputFilename
|
|
|
|
+ "' for writing!");
|
2008-05-06 18:35:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilationGraph::viewGraph() {
|
2008-05-06 18:36:50 +02:00
|
|
|
llvm::ViewGraph(this, "compilation-graph");
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|