1//===- DDGPrinter.cpp - DOT printer for the data dependence graph ----------==//
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//===----------------------------------------------------------------------===//
10//
11// This file defines the `-dot-ddg` analysis pass, which emits DDG in DOT format
12// in a file named `ddg.<graph-name>.dot` for each loop in a function.
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DDGPrinter.h"
16#include "AnalysisOptions.h"
17#include "llvm/Support/GraphWriter.h"
18
19using namespace llvm;
20
21static void writeDDGToDotFile(const AnalysisOptions &Opts,
22 DataDependenceGraph &G);
23
24//===--------------------------------------------------------------------===//
25// Implementation of DDG DOT Printer for a loop
26//===--------------------------------------------------------------------===//
27PreservedAnalyses DDGDotPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
28 LoopStandardAnalysisResults &AR,
29 LPMUpdater &U) {
30 writeDDGToDotFile(Opts: getAnalysisOptions(L), G&: *AM.getResult<DDGAnalysis>(IR&: L, ExtraArgs&: AR));
31 return PreservedAnalyses::all();
32}
33
34static void writeDDGToDotFile(const AnalysisOptions &Opts,
35 DataDependenceGraph &G) {
36 std::string Filename =
37 Twine(Opts.dot_ddg_filename_prefix + "." + G.getName() + ".dot").str();
38 errs() << "Writing '" << Filename << "'...";
39
40 std::error_code EC;
41 raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
42
43 if (!EC)
44 // We only provide the constant verson of the DOTGraphTrait specialization,
45 // hence the conversion to const pointer
46 WriteGraph(O&: File, G: (const DataDependenceGraph *)&G, ShortNames: Opts.dot_ddg_only);
47 else
48 errs() << " error opening file for writing!";
49 errs() << "\n";
50}
51
52//===--------------------------------------------------------------------===//
53// DDG DOT Printer Implementation
54//===--------------------------------------------------------------------===//
55std::string DDGDotGraphTraits::getNodeLabel(const DDGNode *Node,
56 const DataDependenceGraph *Graph) {
57 if (isSimple())
58 return getSimpleNodeLabel(Node, G: Graph);
59 else
60 return getVerboseNodeLabel(Node, G: Graph);
61}
62
63std::string DDGDotGraphTraits::getEdgeAttributes(
64 const DDGNode *Node, GraphTraits<const DDGNode *>::ChildIteratorType I,
65 const DataDependenceGraph *G) {
66 const DDGEdge *E = static_cast<const DDGEdge *>(*I.getCurrent());
67 if (isSimple())
68 return getSimpleEdgeAttributes(Src: Node, Edge: E, G);
69 else
70 return getVerboseEdgeAttributes(Src: Node, Edge: E, G);
71}
72
73bool DDGDotGraphTraits::isNodeHidden(const DDGNode *Node,
74 const DataDependenceGraph *Graph) {
75 if (isSimple() && isa<RootDDGNode>(Val: Node))
76 return true;
77 assert(Graph && "expected a valid graph pointer");
78 return Graph->getPiBlock(N: *Node) != nullptr;
79}
80
81std::string DDGDotGraphTraits::getNodeAttributes(const DDGNode *Node,
82 const DataDependenceGraph *) {
83 if (isa<PiBlockDDGNode>(Val: Node))
84 return "shape=box, style=\"rounded,filled\", fillcolor=lightyellow, "
85 "color=darkorange, penwidth=3";
86 if (isa<SimpleDDGNode>(Val: Node) &&
87 Node->getKind() == DDGNode::NodeKind::MultiInstruction)
88 return "style=filled, fillcolor=lightcyan";
89 return "";
90}
91
92std::string
93DDGDotGraphTraits::getSimpleNodeLabel(const DDGNode *Node,
94 const DataDependenceGraph *G) {
95 std::string Str;
96 raw_string_ostream OS(Str);
97 if (isa<SimpleDDGNode>(Val: Node))
98 for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
99 OS << *II << "\n";
100 else if (isa<PiBlockDDGNode>(Val: Node))
101 OS << "pi-block\nwith\n"
102 << cast<PiBlockDDGNode>(Val: Node)->getNodes().size() << " nodes\n";
103 else if (isa<RootDDGNode>(Val: Node))
104 OS << "root\n";
105 else
106 llvm_unreachable("Unimplemented type of node");
107 return OS.str();
108}
109
110std::string
111DDGDotGraphTraits::getVerboseNodeLabel(const DDGNode *Node,
112 const DataDependenceGraph *G) {
113 std::string Str;
114 raw_string_ostream OS(Str);
115 OS << "<kind:" << Node->getKind() << ">\n";
116 if (isa<SimpleDDGNode>(Val: Node))
117 for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
118 OS << *II << "\n";
119 else if (isa<PiBlockDDGNode>(Val: Node)) {
120 OS << "--- start of nodes in pi-block ---\n";
121 unsigned Count = 0;
122 const auto &PNodes = cast<PiBlockDDGNode>(Val: Node)->getNodes();
123 for (auto *PN : PNodes) {
124 OS << getVerboseNodeLabel(Node: PN, G);
125 if (++Count != PNodes.size())
126 OS << "\n";
127 }
128 OS << "--- end of nodes in pi-block ---\n";
129 } else if (isa<RootDDGNode>(Val: Node))
130 OS << "root\n";
131 else
132 llvm_unreachable("Unimplemented type of node");
133 return OS.str();
134}
135
136/// EdgeKind to DOT color mapping:
137/// - MemoryDependence: red
138/// - RegisterDefUse: blue
139/// - Rooted/Unknown=default (no attribute).
140static void writeEdgeKindColorAttr(raw_ostream &OS, DDGEdge::EdgeKind Kind) {
141 if (Kind == DDGEdge::EdgeKind::MemoryDependence)
142 OS << ", color=red";
143 else if (Kind == DDGEdge::EdgeKind::RegisterDefUse)
144 OS << ", color=blue";
145}
146
147std::string DDGDotGraphTraits::getSimpleEdgeAttributes(
148 const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
149 std::string Str;
150 raw_string_ostream OS(Str);
151 DDGEdge::EdgeKind Kind = Edge->getKind();
152 OS << "label=\"[" << Kind << "]\"";
153 writeEdgeKindColorAttr(OS, Kind);
154 return OS.str();
155}
156
157std::string DDGDotGraphTraits::getVerboseEdgeAttributes(
158 const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
159 std::string Str;
160 raw_string_ostream OS(Str);
161 DDGEdge::EdgeKind Kind = Edge->getKind();
162 OS << "label=\"[";
163 if (Kind == DDGEdge::EdgeKind::MemoryDependence)
164 OS << G->getDependenceString(Src: *Src, Dst: Edge->getTargetNode());
165 else
166 OS << Kind;
167 OS << "]\"";
168 writeEdgeKindColorAttr(OS, Kind);
169 return OS.str();
170}
171