Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 1 | //===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===// |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open |
| 6 | // Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 10 | // Compilation graph - implementation. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | a673037 | 2008-05-12 16:31:42 +0000 | [diff] [blame] | 14 | #include "Error.h" |
Mikhail Glushenkov | 4a1a77c | 2008-09-22 20:50:40 +0000 | [diff] [blame] | 15 | #include "llvm/CompilerDriver/CompilationGraph.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 16 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DOTGraphTraits.h" |
| 20 | #include "llvm/Support/GraphWriter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 21 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Mikhail Glushenkov | f8c430b | 2009-01-09 16:16:27 +0000 | [diff] [blame^] | 23 | #include <cstring> |
| 24 | #include <iostream> |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 25 | #include <iterator> |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 26 | #include <limits> |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 27 | #include <queue> |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 28 | #include <stdexcept> |
| 29 | |
| 30 | using namespace llvm; |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 31 | using namespace llvmc; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 32 | |
| 33 | extern cl::list<std::string> InputFilenames; |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 34 | extern cl::list<std::string> Languages; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 35 | |
Mikhail Glushenkov | 2e73e85 | 2008-05-30 06:19:52 +0000 | [diff] [blame] | 36 | namespace llvmc { |
Mikhail Glushenkov | 2e73e85 | 2008-05-30 06:19:52 +0000 | [diff] [blame] | 37 | |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 38 | const std::string& LanguageMap::GetLanguage(const sys::Path& File) const { |
| 39 | LanguageMap::const_iterator Lang = this->find(File.getSuffix()); |
| 40 | if (Lang == this->end()) |
Mikhail Glushenkov | 2e73e85 | 2008-05-30 06:19:52 +0000 | [diff] [blame] | 41 | throw std::runtime_error("Unknown suffix: " + File.getSuffix()); |
| 42 | return Lang->second; |
| 43 | } |
| 44 | } |
| 45 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 48 | /// ChooseEdge - Return the edge with the maximum weight. |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 49 | template <class C> |
| 50 | const Edge* ChooseEdge(const C& EdgesContainer, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 51 | const InputLanguagesSet& InLangs, |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 52 | const std::string& NodeName = "root") { |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 53 | const Edge* MaxEdge = 0; |
| 54 | unsigned MaxWeight = 0; |
| 55 | bool SingleMax = true; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 56 | |
| 57 | for (typename C::const_iterator B = EdgesContainer.begin(), |
| 58 | E = EdgesContainer.end(); B != E; ++B) { |
Bill Wendling | 46f7a5e | 2008-10-02 18:39:11 +0000 | [diff] [blame] | 59 | const Edge* e = B->getPtr(); |
| 60 | unsigned EW = e->Weight(InLangs); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 61 | if (EW > MaxWeight) { |
Bill Wendling | 46f7a5e | 2008-10-02 18:39:11 +0000 | [diff] [blame] | 62 | MaxEdge = e; |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 63 | MaxWeight = EW; |
| 64 | SingleMax = true; |
Mikhail Glushenkov | e0ff9ae | 2008-05-06 18:18:58 +0000 | [diff] [blame] | 65 | } else if (EW == MaxWeight) { |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 66 | SingleMax = false; |
| 67 | } |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 70 | if (!SingleMax) |
| 71 | throw std::runtime_error("Node " + NodeName + |
| 72 | ": multiple maximal outward edges found!" |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 73 | " Most probably a specification error."); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 74 | if (!MaxEdge) |
| 75 | throw std::runtime_error("Node " + NodeName + |
| 76 | ": no maximal outward edge found!" |
| 77 | " Most probably a specification error."); |
| 78 | return MaxEdge; |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Mikhail Glushenkov | 7e6d70a | 2008-11-26 22:59:45 +0000 | [diff] [blame] | 83 | void Node::AddEdge(Edge* Edg) { |
| 84 | // If there already was an edge between two nodes, modify it instead |
| 85 | // of adding a new edge. |
| 86 | const std::string& ToolName = Edg->ToolName(); |
| 87 | for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end(); |
| 88 | B != E; ++B) { |
| 89 | if ((*B)->ToolName() == ToolName) { |
| 90 | llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B); |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg)); |
| 95 | } |
| 96 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 97 | CompilationGraph::CompilationGraph() { |
| 98 | NodesMap["root"] = Node(this); |
| 99 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 100 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 101 | Node& CompilationGraph::getNode(const std::string& ToolName) { |
| 102 | nodes_map_type::iterator I = NodesMap.find(ToolName); |
| 103 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 104 | throw std::runtime_error("Node " + ToolName + " is not in the graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 105 | return I->second; |
| 106 | } |
| 107 | |
| 108 | const Node& CompilationGraph::getNode(const std::string& ToolName) const { |
| 109 | nodes_map_type::const_iterator I = NodesMap.find(ToolName); |
| 110 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 111 | throw std::runtime_error("Node " + ToolName + " is not in the graph!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 112 | return I->second; |
| 113 | } |
| 114 | |
Mikhail Glushenkov | be86712 | 2008-05-06 18:16:52 +0000 | [diff] [blame] | 115 | // Find the tools list corresponding to the given language name. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 116 | const CompilationGraph::tools_vector_type& |
| 117 | CompilationGraph::getToolsVector(const std::string& LangName) const |
| 118 | { |
| 119 | tools_map_type::const_iterator I = ToolsMap.find(LangName); |
| 120 | if (I == ToolsMap.end()) |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 121 | throw std::runtime_error("No tool corresponding to the language " |
Mikhail Glushenkov | 35bca41 | 2008-05-30 06:16:59 +0000 | [diff] [blame] | 122 | + LangName + " found"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 123 | return I->second; |
| 124 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 125 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 126 | void CompilationGraph::insertNode(Tool* V) { |
Mikhail Glushenkov | aa4774c | 2008-11-12 00:04:46 +0000 | [diff] [blame] | 127 | if (NodesMap.count(V->Name()) == 0) |
| 128 | NodesMap[V->Name()] = Node(this, V); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 131 | void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) { |
| 132 | Node& B = getNode(Edg->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 133 | if (A == "root") { |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 134 | const char** InLangs = B.ToolPtr->InputLanguages(); |
| 135 | for (;*InLangs; ++InLangs) |
| 136 | ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg)); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 137 | NodesMap["root"].AddEdge(Edg); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 138 | } |
| 139 | else { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 140 | Node& N = getNode(A); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 141 | N.AddEdge(Edg); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 142 | } |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 143 | // Increase the inward edge counter. |
| 144 | B.IncrInEdges(); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 147 | // Pass input file through the chain until we bump into a Join node or |
| 148 | // a node that says that it is the last. |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 149 | void CompilationGraph::PassThroughGraph (const sys::Path& InFile, |
| 150 | const Node* StartNode, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 151 | const InputLanguagesSet& InLangs, |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 152 | const sys::Path& TempDir, |
| 153 | const LanguageMap& LangMap) const { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 154 | sys::Path In = InFile; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 155 | const Node* CurNode = StartNode; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 156 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 157 | while(true) { |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 158 | Tool* CurTool = CurNode->ToolPtr.getPtr(); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 159 | |
| 160 | if (CurTool->IsJoin()) { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 161 | JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool); |
| 162 | JT.AddToJoinList(In); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 166 | Action CurAction = CurTool->GenerateAction(In, CurNode->HasChildren(), |
| 167 | TempDir, InLangs, LangMap); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 168 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 169 | if (int ret = CurAction.Execute()) |
Mikhail Glushenkov | a673037 | 2008-05-12 16:31:42 +0000 | [diff] [blame] | 170 | throw error_code(ret); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 171 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 172 | if (CurAction.StopCompilation()) |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 173 | return; |
| 174 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 175 | CurNode = &getNode(ChooseEdge(CurNode->OutEdges, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 176 | InLangs, |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 177 | CurNode->Name())->ToolName()); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 178 | In = CurAction.OutFile(); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 179 | } |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Mikhail Glushenkov | be86712 | 2008-05-06 18:16:52 +0000 | [diff] [blame] | 182 | // Find the head of the toolchain corresponding to the given file. |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 183 | // Also, insert an input language into InLangs. |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 184 | const Node* CompilationGraph:: |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 185 | FindToolChain(const sys::Path& In, const std::string* ForceLanguage, |
| 186 | InputLanguagesSet& InLangs, const LanguageMap& LangMap) const { |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 187 | |
| 188 | // Determine the input language. |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 189 | const std::string& InLanguage = |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 190 | ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In); |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 191 | |
| 192 | // Add the current input language to the input language set. |
| 193 | InLangs.insert(InLanguage); |
| 194 | |
| 195 | // Find the toolchain for the input language. |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 196 | const tools_vector_type& TV = getToolsVector(InLanguage); |
| 197 | if (TV.empty()) |
Mikhail Glushenkov | a673037 | 2008-05-12 16:31:42 +0000 | [diff] [blame] | 198 | throw std::runtime_error("No toolchain corresponding to language " |
| 199 | + InLanguage + " found"); |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 200 | return &getNode(ChooseEdge(TV, InLangs)->ToolName()); |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Mikhail Glushenkov | 4c11a62 | 2008-05-06 18:15:35 +0000 | [diff] [blame] | 203 | // Helper function used by Build(). |
| 204 | // Traverses initial portions of the toolchains (up to the first Join node). |
| 205 | // This function is also responsible for handling the -x option. |
| 206 | void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs, |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 207 | const sys::Path& TempDir, |
| 208 | const LanguageMap& LangMap) { |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 209 | // This is related to -x option handling. |
| 210 | cl::list<std::string>::const_iterator xIter = Languages.begin(), |
| 211 | xBegin = xIter, xEnd = Languages.end(); |
| 212 | bool xEmpty = true; |
| 213 | const std::string* xLanguage = 0; |
| 214 | unsigned xPos = 0, xPosNext = 0, filePos = 0; |
| 215 | |
| 216 | if (xIter != xEnd) { |
| 217 | xEmpty = false; |
| 218 | xPos = Languages.getPosition(xIter - xBegin); |
| 219 | cl::list<std::string>::const_iterator xNext = llvm::next(xIter); |
| 220 | xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max() |
| 221 | : Languages.getPosition(xNext - xBegin); |
| 222 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 223 | } |
| 224 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 225 | // For each input file: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 226 | for (cl::list<std::string>::const_iterator B = InputFilenames.begin(), |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 227 | CB = B, E = InputFilenames.end(); B != E; ++B) { |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 228 | sys::Path In = sys::Path(*B); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 229 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 230 | // Code for handling the -x option. |
| 231 | // Output: std::string* xLanguage (can be NULL). |
| 232 | if (!xEmpty) { |
| 233 | filePos = InputFilenames.getPosition(B - CB); |
| 234 | |
| 235 | if (xPos < filePos) { |
| 236 | if (filePos < xPosNext) { |
| 237 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 238 | } |
| 239 | else { // filePos >= xPosNext |
| 240 | // Skip xIters while filePos > xPosNext |
| 241 | while (filePos > xPosNext) { |
| 242 | ++xIter; |
| 243 | xPos = xPosNext; |
| 244 | |
| 245 | cl::list<std::string>::const_iterator xNext = llvm::next(xIter); |
| 246 | if (xNext == xEnd) |
| 247 | xPosNext = std::numeric_limits<unsigned>::max(); |
| 248 | else |
| 249 | xPosNext = Languages.getPosition(xNext - xBegin); |
| 250 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 256 | // Find the toolchain corresponding to this file. |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 257 | const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap); |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 258 | // Pass file through the chain starting at head. |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 259 | PassThroughGraph(In, N, InLangs, TempDir, LangMap); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 260 | } |
Mikhail Glushenkov | 4c11a62 | 2008-05-06 18:15:35 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Mikhail Glushenkov | be86712 | 2008-05-06 18:16:52 +0000 | [diff] [blame] | 263 | // Sort the nodes in topological order. |
| 264 | void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) { |
| 265 | std::queue<const Node*> Q; |
| 266 | Q.push(&getNode("root")); |
| 267 | |
| 268 | while (!Q.empty()) { |
| 269 | const Node* A = Q.front(); |
| 270 | Q.pop(); |
| 271 | Out.push_back(A); |
| 272 | for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd(); |
| 273 | EB != EE; ++EB) { |
| 274 | Node* B = &getNode((*EB)->ToolName()); |
| 275 | B->DecrInEdges(); |
| 276 | if (B->HasNoInEdges()) |
| 277 | Q.push(B); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | namespace { |
| 283 | bool NotJoinNode(const Node* N) { |
| 284 | return N->ToolPtr ? !N->ToolPtr->IsJoin() : true; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Call TopologicalSort and filter the resulting list to include |
| 289 | // only Join nodes. |
| 290 | void CompilationGraph:: |
| 291 | TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) { |
| 292 | std::vector<const Node*> TopSorted; |
| 293 | TopologicalSort(TopSorted); |
| 294 | std::remove_copy_if(TopSorted.begin(), TopSorted.end(), |
| 295 | std::back_inserter(Out), NotJoinNode); |
| 296 | } |
| 297 | |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 298 | int CompilationGraph::Build (const sys::Path& TempDir, |
| 299 | const LanguageMap& LangMap) { |
Mikhail Glushenkov | 4c11a62 | 2008-05-06 18:15:35 +0000 | [diff] [blame] | 300 | |
| 301 | InputLanguagesSet InLangs; |
| 302 | |
| 303 | // Traverse initial parts of the toolchains and fill in InLangs. |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 304 | BuildInitial(InLangs, TempDir, LangMap); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 305 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 306 | std::vector<const Node*> JTV; |
| 307 | TopologicalSortFilterJoinNodes(JTV); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 308 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 309 | // For all join nodes in topological order: |
| 310 | for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end(); |
| 311 | B != E; ++B) { |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 312 | |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 313 | const Node* CurNode = *B; |
| 314 | JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr()); |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 315 | |
Mikhail Glushenkov | be86712 | 2008-05-06 18:16:52 +0000 | [diff] [blame] | 316 | // Are there any files in the join list? |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 317 | if (JT->JoinListEmpty()) |
| 318 | continue; |
| 319 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 320 | Action CurAction = JT->GenerateAction(CurNode->HasChildren(), |
| 321 | TempDir, InLangs, LangMap); |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 322 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 323 | if (int ret = CurAction.Execute()) |
Mikhail Glushenkov | a673037 | 2008-05-12 16:31:42 +0000 | [diff] [blame] | 324 | throw error_code(ret); |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 325 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 326 | if (CurAction.StopCompilation()) |
| 327 | return 0; |
| 328 | |
Mikhail Glushenkov | b677df8 | 2008-12-07 16:45:37 +0000 | [diff] [blame] | 329 | const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges, InLangs, |
| 330 | CurNode->Name())->ToolName()); |
| 331 | PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode, |
| 332 | InLangs, TempDir, LangMap); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 333 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 337 | |
Mikhail Glushenkov | f8c430b | 2009-01-09 16:16:27 +0000 | [diff] [blame^] | 338 | int CompilationGraph::CheckLanguageNames() const { |
| 339 | int ret = 0; |
| 340 | // Check that names for output and input languages on all edges do match. |
| 341 | for (const_nodes_iterator B = this->NodesMap.begin(), |
| 342 | E = this->NodesMap.end(); B != E; ++B) { |
| 343 | |
| 344 | const Node & N1 = B->second; |
| 345 | if (N1.ToolPtr) { |
| 346 | for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd(); |
| 347 | EB != EE; ++EB) { |
| 348 | const Node& N2 = this->getNode((*EB)->ToolName()); |
| 349 | |
| 350 | if (!N2.ToolPtr) { |
| 351 | ++ret; |
| 352 | std::cerr << "Error: there is an edge from '" << N1.ToolPtr->Name() |
| 353 | << "' back to the root!\n\n"; |
| 354 | continue; |
| 355 | } |
| 356 | |
| 357 | const char* OutLang = N1.ToolPtr->OutputLanguage(); |
| 358 | const char** InLangs = N2.ToolPtr->InputLanguages(); |
| 359 | bool eq = false; |
| 360 | for (;*InLangs; ++InLangs) { |
| 361 | if (std::strcmp(OutLang, *InLangs) == 0) { |
| 362 | eq = true; |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (!eq) { |
| 368 | ++ret; |
| 369 | std::cerr << "Error: Output->input language mismatch in the edge '" << |
| 370 | N1.ToolPtr->Name() << "' -> '" << N2.ToolPtr->Name() << "'!\n"; |
| 371 | |
| 372 | std::cerr << "Expected one of { "; |
| 373 | |
| 374 | InLangs = N2.ToolPtr->InputLanguages(); |
| 375 | for (;*InLangs; ++InLangs) { |
| 376 | std::cerr << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'"); |
| 377 | } |
| 378 | |
| 379 | std::cerr << " }, but got '" << OutLang << "'!\n\n"; |
| 380 | } |
| 381 | |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | int CompilationGraph::CheckMultipleDefaultEdges() const { |
| 390 | int ret = 0; |
| 391 | InputLanguagesSet Dummy; |
| 392 | |
| 393 | for (const_nodes_iterator B = this->NodesMap.begin(), |
| 394 | E = this->NodesMap.end(); B != E; ++B) { |
| 395 | const Node& N = B->second; |
| 396 | unsigned MaxWeight = 0; |
| 397 | |
| 398 | // Ignore the root node. |
| 399 | if (!N.ToolPtr) |
| 400 | continue; |
| 401 | |
| 402 | for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd(); |
| 403 | EB != EE; ++EB) { |
| 404 | unsigned EdgeWeight = (*EB)->Weight(Dummy); |
| 405 | if (EdgeWeight > MaxWeight) { |
| 406 | MaxWeight = EdgeWeight; |
| 407 | } |
| 408 | else if (EdgeWeight == MaxWeight) { |
| 409 | ++ret; |
| 410 | std::cerr |
| 411 | << "Error: there are multiple maximal edges stemming from the '" |
| 412 | << N.ToolPtr->Name() << "' node!\n\n"; |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | int CompilationGraph::CheckCycles() { |
| 422 | unsigned deleted = 0; |
| 423 | std::queue<Node*> Q; |
| 424 | Q.push(&getNode("root")); |
| 425 | |
| 426 | while (!Q.empty()) { |
| 427 | Node* A = Q.front(); |
| 428 | Q.pop(); |
| 429 | ++deleted; |
| 430 | |
| 431 | for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd(); |
| 432 | EB != EE; ++EB) { |
| 433 | Node* B = &getNode((*EB)->ToolName()); |
| 434 | B->DecrInEdges(); |
| 435 | if (B->HasNoInEdges()) |
| 436 | Q.push(B); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (deleted != NodesMap.size()) { |
| 441 | std::cerr << "Error: there are cycles in the compilation graph!\n" |
| 442 | << "Try inspecting the diagram produced by " |
| 443 | "'llvmc --view-graph'.\n\n"; |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | int CompilationGraph::Check () { |
| 452 | // We try to catch as many errors as we can in one go. |
| 453 | int ret = 0; |
| 454 | |
| 455 | // Check that output/input language names match. |
| 456 | ret += this->CheckLanguageNames(); |
| 457 | |
| 458 | // Check for multiple default edges. |
| 459 | ret += this->CheckMultipleDefaultEdges(); |
| 460 | |
| 461 | // Check for cycles. |
| 462 | ret += this->CheckCycles(); |
| 463 | |
| 464 | return ret; |
| 465 | } |
| 466 | |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 467 | // Code related to graph visualization. |
| 468 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 469 | namespace llvm { |
| 470 | template <> |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 471 | struct DOTGraphTraits<llvmc::CompilationGraph*> |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 472 | : public DefaultDOTGraphTraits |
| 473 | { |
| 474 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 475 | template<typename GraphType> |
| 476 | static std::string getNodeLabel(const Node* N, const GraphType&) |
| 477 | { |
| 478 | if (N->ToolPtr) |
| 479 | if (N->ToolPtr->IsJoin()) |
| 480 | return N->Name() + "\n (join" + |
| 481 | (N->HasChildren() ? ")" |
| 482 | : std::string(": ") + N->ToolPtr->OutputLanguage() + ')'); |
| 483 | else |
| 484 | return N->Name(); |
| 485 | else |
| 486 | return "root"; |
| 487 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 488 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 489 | template<typename EdgeIter> |
| 490 | static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 491 | if (N->ToolPtr) { |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 492 | return N->ToolPtr->OutputLanguage(); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 493 | } |
| 494 | else { |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 495 | const char** InLangs = I->ToolPtr->InputLanguages(); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 496 | std::string ret; |
| 497 | |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 498 | for (; *InLangs; ++InLangs) { |
| 499 | if (*(InLangs + 1)) { |
| 500 | ret += *InLangs; |
| 501 | ret += ", "; |
| 502 | } |
| 503 | else { |
| 504 | ret += *InLangs; |
| 505 | } |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | return ret; |
| 509 | } |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 510 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 511 | }; |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 512 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void CompilationGraph::writeGraph() { |
Mikhail Glushenkov | 35bca41 | 2008-05-30 06:16:59 +0000 | [diff] [blame] | 516 | std::ofstream O("compilation-graph.dot"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 517 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 518 | if (O.good()) { |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 519 | llvm::WriteGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 520 | O.close(); |
| 521 | } |
| 522 | else { |
Mikhail Glushenkov | 35bca41 | 2008-05-30 06:16:59 +0000 | [diff] [blame] | 523 | throw std::runtime_error("Error opening file 'compilation-graph.dot'" |
| 524 | " for writing!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | void CompilationGraph::viewGraph() { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 529 | llvm::ViewGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 530 | } |