| 1 | //===- llvm/Support/GraphWriter.h - Write graph to a .dot file --*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines a simple interface that can be used to print out generic |
| 10 | // LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T |
| 11 | // graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can |
| 12 | // be used to turn the files output by this interface into a variety of |
| 13 | // different graphics formats. |
| 14 | // |
| 15 | // Graphs do not need to implement any interface past what is already required |
| 16 | // by the GraphTraits template, but they can choose to implement specializations |
| 17 | // of the DOTGraphTraits template if they want to customize the graphs output in |
| 18 | // any way. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #ifndef LLVM_SUPPORT_GRAPHWRITER_H |
| 23 | #define LLVM_SUPPORT_GRAPHWRITER_H |
| 24 | |
| 25 | #include "llvm/ADT/GraphTraits.h" |
| 26 | #include "llvm/ADT/StringRef.h" |
| 27 | #include "llvm/ADT/Twine.h" |
| 28 | #include "llvm/Support/Compiler.h" |
| 29 | #include "llvm/Support/DOTGraphTraits.h" |
| 30 | #include "llvm/Support/FileSystem.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include <iterator> |
| 33 | #include <string> |
| 34 | #include <type_traits> |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace llvm { |
| 38 | |
| 39 | namespace DOT { // Private functions... |
| 40 | |
| 41 | LLVM_ABI std::string EscapeString(const std::string &Label); |
| 42 | |
| 43 | /// Get a color string for this node number. Simply round-robin selects |
| 44 | /// from a reasonable number of colors. |
| 45 | LLVM_ABI StringRef getColorString(unsigned NodeNumber); |
| 46 | |
| 47 | } // end namespace DOT |
| 48 | |
| 49 | namespace GraphProgram { |
| 50 | |
| 51 | enum Name { |
| 52 | DOT, |
| 53 | FDP, |
| 54 | NEATO, |
| 55 | TWOPI, |
| 56 | CIRCO |
| 57 | }; |
| 58 | |
| 59 | } // end namespace GraphProgram |
| 60 | |
| 61 | LLVM_ABI bool DisplayGraph(StringRef Filename, bool wait = true, |
| 62 | GraphProgram::Name program = GraphProgram::DOT); |
| 63 | |
| 64 | template <typename GraphType, typename Derived> class GraphWriterBase { |
| 65 | protected: |
| 66 | raw_ostream &O; |
| 67 | const GraphType &G; |
| 68 | bool RenderUsingHTML = false; |
| 69 | |
| 70 | using DOTTraits = DOTGraphTraits<GraphType>; |
| 71 | using GTraits = GraphTraits<GraphType>; |
| 72 | using NodeRef = typename GTraits::NodeRef; |
| 73 | using node_iterator = typename GTraits::nodes_iterator; |
| 74 | using child_iterator = typename GTraits::ChildIteratorType; |
| 75 | DOTTraits DTraits; |
| 76 | |
| 77 | static_assert(std::is_pointer_v<NodeRef>, |
| 78 | "FIXME: Currently GraphWriterBase requires the NodeRef type to " |
| 79 | "be a pointer.\nThe pointer usage should be moved to " |
| 80 | "DOTGraphTraits, and removed from GraphWriterBase itself." ); |
| 81 | |
| 82 | // Cast the 'this' pointer to the derived type and return a reference. |
| 83 | Derived &getDerived() { return *static_cast<Derived *>(this); } |
| 84 | const Derived &getDerived() const { |
| 85 | return *static_cast<const Derived *>(this); |
| 86 | } |
| 87 | |
| 88 | // Writes the edge labels of the node to O and returns true if there are any |
| 89 | // edge labels not equal to the empty string "". |
| 90 | bool getEdgeSourceLabels(raw_ostream &O, NodeRef Node) { |
| 91 | child_iterator EI = GTraits::child_begin(Node); |
| 92 | child_iterator EE = GTraits::child_end(Node); |
| 93 | bool hasEdgeSourceLabels = false; |
| 94 | |
| 95 | if (RenderUsingHTML) |
| 96 | O << "</tr><tr>" ; |
| 97 | |
| 98 | for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) { |
| 99 | std::string label = DTraits.getEdgeSourceLabel(Node, EI); |
| 100 | |
| 101 | if (label.empty()) |
| 102 | continue; |
| 103 | |
| 104 | hasEdgeSourceLabels = true; |
| 105 | |
| 106 | if (RenderUsingHTML) |
| 107 | O << "<td colspan=\"1\" port=\"s" << i << "\">" << label << "</td>" ; |
| 108 | else { |
| 109 | if (i) |
| 110 | O << "|" ; |
| 111 | |
| 112 | O << "<s" << i << ">" << DOT::EscapeString(Label: label); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (EI != EE && hasEdgeSourceLabels) { |
| 117 | if (RenderUsingHTML) |
| 118 | O << "<td colspan=\"1\" port=\"s64\">truncated...</td>" ; |
| 119 | else |
| 120 | O << "|<s64>truncated..." ; |
| 121 | } |
| 122 | |
| 123 | return hasEdgeSourceLabels; |
| 124 | } |
| 125 | |
| 126 | public: |
| 127 | GraphWriterBase(raw_ostream &o, const GraphType &g, bool SN) : O(o), G(g) { |
| 128 | DTraits = DOTTraits(SN); |
| 129 | RenderUsingHTML = DTraits.renderNodesUsingHTML(); |
| 130 | } |
| 131 | virtual ~GraphWriterBase() = default; |
| 132 | |
| 133 | void writeGraph(const std::string &Title = "" ) { |
| 134 | // Output the header for the graph... |
| 135 | getDerived().writeHeader(Title); |
| 136 | |
| 137 | // Emit all of the nodes in the graph... |
| 138 | getDerived().writeNodes(); |
| 139 | |
| 140 | // Output any customizations on the graph |
| 141 | DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, getDerived()); |
| 142 | |
| 143 | // Output the end of the graph |
| 144 | getDerived().writeFooter(); |
| 145 | } |
| 146 | |
| 147 | void (const std::string &Title) { |
| 148 | std::string GraphName(DTraits.getGraphName(G)); |
| 149 | |
| 150 | if (!Title.empty()) |
| 151 | O << "digraph \"" << DOT::EscapeString(Label: Title) << "\" {\n" ; |
| 152 | else if (!GraphName.empty()) |
| 153 | O << "digraph \"" << DOT::EscapeString(Label: GraphName) << "\" {\n" ; |
| 154 | else |
| 155 | O << "digraph unnamed {\n" ; |
| 156 | |
| 157 | if (DTraits.renderGraphFromBottomUp()) |
| 158 | O << "\trankdir=\"BT\";\n" ; |
| 159 | |
| 160 | if (!Title.empty()) |
| 161 | O << "\tlabel=\"" << DOT::EscapeString(Label: Title) << "\";\n" ; |
| 162 | else if (!GraphName.empty()) |
| 163 | O << "\tlabel=\"" << DOT::EscapeString(Label: GraphName) << "\";\n" ; |
| 164 | O << DTraits.getGraphProperties(G); |
| 165 | O << "\n" ; |
| 166 | } |
| 167 | |
| 168 | void () { |
| 169 | // Finish off the graph |
| 170 | O << "}\n" ; |
| 171 | } |
| 172 | |
| 173 | void writeNodes() { |
| 174 | // Loop over the graph, printing it out... |
| 175 | for (const auto Node : nodes<GraphType>(G)) |
| 176 | if (!getDerived().isNodeHidden(Node)) |
| 177 | getDerived().writeNode(Node); |
| 178 | } |
| 179 | |
| 180 | bool isNodeHidden(NodeRef Node) { return DTraits.isNodeHidden(Node, G); } |
| 181 | |
| 182 | void writeNode(NodeRef Node) { |
| 183 | std::string NodeAttributes = DTraits.getNodeAttributes(Node, G); |
| 184 | |
| 185 | O << "\tNode" << static_cast<const void *>(Node) << " [shape=" ; |
| 186 | if (RenderUsingHTML) |
| 187 | O << "none," ; |
| 188 | else |
| 189 | O << "record," ; |
| 190 | |
| 191 | if (!NodeAttributes.empty()) O << NodeAttributes << "," ; |
| 192 | O << "label=" ; |
| 193 | |
| 194 | if (RenderUsingHTML) { |
| 195 | // Count the numbewr of edges out of the node to determine how |
| 196 | // many columns to span (max 64) |
| 197 | unsigned ColSpan = 0; |
| 198 | child_iterator EI = GTraits::child_begin(Node); |
| 199 | child_iterator EE = GTraits::child_end(Node); |
| 200 | for (; EI != EE && ColSpan != 64; ++EI, ++ColSpan) |
| 201 | ; |
| 202 | if (ColSpan == 0) |
| 203 | ColSpan = 1; |
| 204 | // Include truncated messages when counting. |
| 205 | if (EI != EE) |
| 206 | ++ColSpan; |
| 207 | O << "<<table border=\"0\" cellborder=\"1\" cellspacing=\"0\"" |
| 208 | << " cellpadding=\"0\"><tr><td align=\"text\" colspan=\"" << ColSpan |
| 209 | << "\">" ; |
| 210 | } else { |
| 211 | O << "\"{" ; |
| 212 | } |
| 213 | |
| 214 | if (!DTraits.renderGraphFromBottomUp()) { |
| 215 | if (RenderUsingHTML) |
| 216 | O << DTraits.getNodeLabel(Node, G) << "</td>" ; |
| 217 | else |
| 218 | O << DOT::EscapeString(Label: DTraits.getNodeLabel(Node, G)); |
| 219 | |
| 220 | // If we should include the address of the node in the label, do so now. |
| 221 | std::string Id = DTraits.getNodeIdentifierLabel(Node, G); |
| 222 | if (!Id.empty()) |
| 223 | O << "|" << DOT::EscapeString(Label: Id); |
| 224 | |
| 225 | std::string NodeDesc = DTraits.getNodeDescription(Node, G); |
| 226 | if (!NodeDesc.empty()) |
| 227 | O << "|" << DOT::EscapeString(Label: NodeDesc); |
| 228 | } |
| 229 | |
| 230 | std::string edgeSourceLabels; |
| 231 | raw_string_ostream EdgeSourceLabels(edgeSourceLabels); |
| 232 | bool hasEdgeSourceLabels = getEdgeSourceLabels(O&: EdgeSourceLabels, Node); |
| 233 | |
| 234 | if (hasEdgeSourceLabels) { |
| 235 | if (!DTraits.renderGraphFromBottomUp()) |
| 236 | if (!RenderUsingHTML) |
| 237 | O << "|" ; |
| 238 | |
| 239 | if (RenderUsingHTML) |
| 240 | O << edgeSourceLabels; |
| 241 | else |
| 242 | O << "{" << edgeSourceLabels << "}" ; |
| 243 | |
| 244 | if (DTraits.renderGraphFromBottomUp()) |
| 245 | if (!RenderUsingHTML) |
| 246 | O << "|" ; |
| 247 | } |
| 248 | |
| 249 | if (DTraits.renderGraphFromBottomUp()) { |
| 250 | if (RenderUsingHTML) |
| 251 | O << DTraits.getNodeLabel(Node, G); |
| 252 | else |
| 253 | O << DOT::EscapeString(Label: DTraits.getNodeLabel(Node, G)); |
| 254 | |
| 255 | // If we should include the address of the node in the label, do so now. |
| 256 | std::string Id = DTraits.getNodeIdentifierLabel(Node, G); |
| 257 | if (!Id.empty()) |
| 258 | O << "|" << DOT::EscapeString(Label: Id); |
| 259 | |
| 260 | std::string NodeDesc = DTraits.getNodeDescription(Node, G); |
| 261 | if (!NodeDesc.empty()) |
| 262 | O << "|" << DOT::EscapeString(Label: NodeDesc); |
| 263 | } |
| 264 | |
| 265 | if (DTraits.hasEdgeDestLabels()) { |
| 266 | O << "|{" ; |
| 267 | |
| 268 | unsigned i = 0, e = DTraits.numEdgeDestLabels(Node); |
| 269 | for (; i != e && i != 64; ++i) { |
| 270 | if (i) O << "|" ; |
| 271 | O << "<d" << i << ">" |
| 272 | << DOT::EscapeString(Label: DTraits.getEdgeDestLabel(Node, i)); |
| 273 | } |
| 274 | |
| 275 | if (i != e) |
| 276 | O << "|<d64>truncated..." ; |
| 277 | O << "}" ; |
| 278 | } |
| 279 | |
| 280 | if (RenderUsingHTML) |
| 281 | O << "</tr></table>>" ; |
| 282 | else |
| 283 | O << "}\"" ; |
| 284 | O << "];\n" ; // Finish printing the "node" line |
| 285 | |
| 286 | // Output all of the edges now |
| 287 | child_iterator EI = GTraits::child_begin(Node); |
| 288 | child_iterator EE = GTraits::child_end(Node); |
| 289 | for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) |
| 290 | if (!DTraits.isNodeHidden(*EI, G)) |
| 291 | writeEdge(Node, edgeidx: i, EI); |
| 292 | for (; EI != EE; ++EI) |
| 293 | if (!DTraits.isNodeHidden(*EI, G)) |
| 294 | writeEdge(Node, edgeidx: 64, EI); |
| 295 | } |
| 296 | |
| 297 | void writeEdge(NodeRef Node, unsigned edgeidx, child_iterator EI) { |
| 298 | if (NodeRef TargetNode = *EI) { |
| 299 | int DestPort = -1; |
| 300 | if (DTraits.edgeTargetsEdgeSource(Node, EI)) { |
| 301 | child_iterator TargetIt = DTraits.getEdgeTarget(Node, EI); |
| 302 | |
| 303 | // Figure out which edge this targets... |
| 304 | unsigned Offset = |
| 305 | (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt); |
| 306 | DestPort = static_cast<int>(Offset); |
| 307 | } |
| 308 | |
| 309 | if (DTraits.getEdgeSourceLabel(Node, EI).empty()) |
| 310 | edgeidx = -1; |
| 311 | |
| 312 | getDerived().emitEdge(static_cast<const void *>(Node), edgeidx, |
| 313 | static_cast<const void *>(TargetNode), DestPort, |
| 314 | DTraits.getEdgeAttributes(Node, EI, G)); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /// emitSimpleNode - Outputs a simple (non-record) node |
| 319 | void emitSimpleNode(const void *ID, const std::string &Attr, |
| 320 | const std::string &Label, unsigned NumEdgeSources = 0, |
| 321 | const std::vector<std::string> *EdgeSourceLabels = nullptr) { |
| 322 | O << "\tNode" << ID << "[ " ; |
| 323 | if (!Attr.empty()) |
| 324 | O << Attr << "," ; |
| 325 | O << " label =\"" ; |
| 326 | if (NumEdgeSources) O << "{" ; |
| 327 | O << DOT::EscapeString(Label); |
| 328 | if (NumEdgeSources) { |
| 329 | O << "|{" ; |
| 330 | |
| 331 | for (unsigned i = 0; i != NumEdgeSources; ++i) { |
| 332 | if (i) O << "|" ; |
| 333 | O << "<s" << i << ">" ; |
| 334 | if (EdgeSourceLabels) O << DOT::EscapeString(Label: (*EdgeSourceLabels)[i]); |
| 335 | } |
| 336 | O << "}}" ; |
| 337 | } |
| 338 | O << "\"];\n" ; |
| 339 | } |
| 340 | |
| 341 | /// emitEdge - Output an edge from a simple node into the graph... |
| 342 | void emitEdge(const void *SrcNodeID, int SrcNodePort, |
| 343 | const void *DestNodeID, int DestNodePort, |
| 344 | const std::string &Attrs) { |
| 345 | if (SrcNodePort > 64) return; // Eminating from truncated part? |
| 346 | DestNodePort = std::min(a: DestNodePort, b: 64); // Targeting the truncated part? |
| 347 | |
| 348 | O << "\tNode" << SrcNodeID; |
| 349 | if (SrcNodePort >= 0) |
| 350 | O << ":s" << SrcNodePort; |
| 351 | O << " -> Node" << DestNodeID; |
| 352 | if (DestNodePort >= 0 && DTraits.hasEdgeDestLabels()) |
| 353 | O << ":d" << DestNodePort; |
| 354 | |
| 355 | if (!Attrs.empty()) |
| 356 | O << "[" << Attrs << "]" ; |
| 357 | O << ";\n" ; |
| 358 | } |
| 359 | |
| 360 | /// getOStream - Get the raw output stream into the graph file. Useful to |
| 361 | /// write fancy things using addCustomGraphFeatures(). |
| 362 | raw_ostream &getOStream() { |
| 363 | return O; |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | template <typename GraphType> |
| 368 | class GraphWriter : public GraphWriterBase<GraphType, GraphWriter<GraphType>> { |
| 369 | public: |
| 370 | GraphWriter(raw_ostream &o, const GraphType &g, bool SN) |
| 371 | : GraphWriterBase<GraphType, GraphWriter<GraphType>>(o, g, SN) {} |
| 372 | ~GraphWriter() override = default; |
| 373 | }; |
| 374 | |
| 375 | template <typename GraphType> |
| 376 | raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G, |
| 377 | bool ShortNames = false, const Twine &Title = "" ) { |
| 378 | // Start the graph emission process... |
| 379 | GraphWriter<GraphType> W(O, G, ShortNames); |
| 380 | |
| 381 | // Emit the graph. |
| 382 | W.writeGraph(Title.str()); |
| 383 | |
| 384 | return O; |
| 385 | } |
| 386 | |
| 387 | LLVM_ABI std::string createGraphFilename(const Twine &Name, int &FD); |
| 388 | |
| 389 | /// Writes graph into a provided @c Filename. |
| 390 | /// If @c Filename is empty, generates a random one. |
| 391 | /// \return The resulting filename, or an empty string if writing |
| 392 | /// failed. |
| 393 | template <typename GraphType> |
| 394 | std::string WriteGraph(const GraphType &G, const Twine &Name, |
| 395 | bool ShortNames = false, |
| 396 | const Twine &Title = "" , |
| 397 | std::string Filename = "" ) { |
| 398 | int FD; |
| 399 | if (Filename.empty()) { |
| 400 | Filename = createGraphFilename(Name: Name.str(), FD); |
| 401 | } else { |
| 402 | std::error_code EC = sys::fs::openFileForWrite( |
| 403 | Name: Filename, ResultFD&: FD, Disp: sys::fs::CD_CreateAlways, Flags: sys::fs::OF_Text); |
| 404 | |
| 405 | // Writing over an existing file is not considered an error. |
| 406 | if (EC == std::errc::file_exists) { |
| 407 | errs() << "file exists, overwriting" << "\n" ; |
| 408 | } else if (EC) { |
| 409 | errs() << "error writing into file" << "\n" ; |
| 410 | return "" ; |
| 411 | } else { |
| 412 | errs() << "writing to the newly created file " << Filename << "\n" ; |
| 413 | } |
| 414 | } |
| 415 | raw_fd_ostream O(FD, /*shouldClose=*/ true); |
| 416 | |
| 417 | if (FD == -1) { |
| 418 | errs() << "error opening file '" << Filename << "' for writing!\n" ; |
| 419 | return "" ; |
| 420 | } |
| 421 | |
| 422 | llvm::WriteGraph(O, G, ShortNames, Title); |
| 423 | errs() << " done. \n" ; |
| 424 | |
| 425 | return Filename; |
| 426 | } |
| 427 | |
| 428 | /// DumpDotGraph - Just dump a dot graph to the user-provided file name. |
| 429 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 430 | template <typename GraphType> |
| 431 | LLVM_DUMP_METHOD void |
| 432 | dumpDotGraphToFile(const GraphType &G, const Twine &FileName, |
| 433 | const Twine &Title, bool ShortNames = false, |
| 434 | const Twine &Name = "" ) { |
| 435 | llvm::WriteGraph(G, Name, ShortNames, Title, FileName.str()); |
| 436 | } |
| 437 | #endif |
| 438 | |
| 439 | /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, |
| 440 | /// then cleanup. For use from the debugger. |
| 441 | /// |
| 442 | template<typename GraphType> |
| 443 | void ViewGraph(const GraphType &G, const Twine &Name, |
| 444 | bool ShortNames = false, const Twine &Title = "" , |
| 445 | GraphProgram::Name Program = GraphProgram::DOT) { |
| 446 | std::string Filename = llvm::WriteGraph(G, Name, ShortNames, Title); |
| 447 | |
| 448 | if (Filename.empty()) |
| 449 | return; |
| 450 | |
| 451 | DisplayGraph(Filename, wait: false, program: Program); |
| 452 | } |
| 453 | |
| 454 | } // end namespace llvm |
| 455 | |
| 456 | #endif // LLVM_SUPPORT_GRAPHWRITER_H |
| 457 | |