1 //===- CallGraph.cpp - CallGraph analysis for MLIR ------------------------===// 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 contains interfaces and analyses for defining a nested callgraph. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/Analysis/CallGraph.h" 14 #include "mlir/IR/Operation.h" 15 #include "mlir/IR/SymbolTable.h" 16 #include "mlir/Interfaces/CallInterfaces.h" 17 #include "llvm/ADT/PointerUnion.h" 18 #include "llvm/ADT/SCCIterator.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 using namespace mlir; 22 23 //===----------------------------------------------------------------------===// 24 // CallGraphNode 25 //===----------------------------------------------------------------------===// 26 27 /// Returns if this node refers to the indirect/external node. 28 bool CallGraphNode::isExternal() const { return !callableRegion; } 29 30 /// Return the callable region this node represents. This can only be called 31 /// on non-external nodes. 32 Region *CallGraphNode::getCallableRegion() const { 33 assert(!isExternal() && "the external node has no callable region"); 34 return callableRegion; 35 } 36 37 /// Adds an reference edge to the given node. This is only valid on the 38 /// external node. 39 void CallGraphNode::addAbstractEdge(CallGraphNode *node) { 40 assert(isExternal() && "abstract edges are only valid on external nodes"); 41 addEdge(node, Edge::Kind::Abstract); 42 } 43 44 /// Add an outgoing call edge from this node. 45 void CallGraphNode::addCallEdge(CallGraphNode *node) { 46 addEdge(node, Edge::Kind::Call); 47 } 48 49 /// Adds a reference edge to the given child node. 50 void CallGraphNode::addChildEdge(CallGraphNode *child) { 51 addEdge(child, Edge::Kind::Child); 52 } 53 54 /// Returns true if this node has any child edges. 55 bool CallGraphNode::hasChildren() const { 56 return llvm::any_of(edges, [](const Edge &edge) { return edge.isChild(); }); 57 } 58 59 /// Add an edge to 'node' with the given kind. 60 void CallGraphNode::addEdge(CallGraphNode *node, Edge::Kind kind) { 61 edges.insert({node, kind}); 62 } 63 64 //===----------------------------------------------------------------------===// 65 // CallGraph 66 //===----------------------------------------------------------------------===// 67 68 /// Recursively compute the callgraph edges for the given operation. Computed 69 /// edges are placed into the given callgraph object. 70 static void computeCallGraph(Operation *op, CallGraph &cg, 71 CallGraphNode *parentNode, bool resolveCalls) { 72 if (CallOpInterface call = dyn_cast<CallOpInterface>(op)) { 73 // If there is no parent node, we ignore this operation. Even if this 74 // operation was a call, there would be no callgraph node to attribute it 75 // to. 76 if (resolveCalls && parentNode) 77 parentNode->addCallEdge(cg.resolveCallable(call)); 78 return; 79 } 80 81 // Compute the callgraph nodes and edges for each of the nested operations. 82 if (CallableOpInterface callable = dyn_cast<CallableOpInterface>(op)) { 83 if (auto *callableRegion = callable.getCallableRegion()) 84 parentNode = cg.getOrAddNode(callableRegion, parentNode); 85 else 86 return; 87 } 88 89 for (Region ®ion : op->getRegions()) 90 for (Block &block : region) 91 for (Operation &nested : block) 92 computeCallGraph(&nested, cg, parentNode, resolveCalls); 93 } 94 95 CallGraph::CallGraph(Operation *op) : externalNode(/*callableRegion=*/nullptr) { 96 // Make two passes over the graph, one to compute the callables and one to 97 // resolve the calls. We split these up as we may have nested callable objects 98 // that need to be reserved before the calls. 99 computeCallGraph(op, *this, /*parentNode=*/nullptr, /*resolveCalls=*/false); 100 computeCallGraph(op, *this, /*parentNode=*/nullptr, /*resolveCalls=*/true); 101 } 102 103 /// Get or add a call graph node for the given region. 104 CallGraphNode *CallGraph::getOrAddNode(Region *region, 105 CallGraphNode *parentNode) { 106 assert(region && isa<CallableOpInterface>(region->getParentOp()) && 107 "expected parent operation to be callable"); 108 std::unique_ptr<CallGraphNode> &node = nodes[region]; 109 if (!node) { 110 node.reset(new CallGraphNode(region)); 111 112 // Add this node to the given parent node if necessary. 113 if (parentNode) 114 parentNode->addChildEdge(node.get()); 115 else 116 // Otherwise, connect all callable nodes to the external node, this allows 117 // for conservatively including all callable nodes within the graph. 118 // FIXME(riverriddle) This isn't correct, this is only necessary for 119 // callable nodes that *could* be called from external sources. This 120 // requires extending the interface for callables to check if they may be 121 // referenced externally. 122 externalNode.addAbstractEdge(node.get()); 123 } 124 return node.get(); 125 } 126 127 /// Lookup a call graph node for the given region, or nullptr if none is 128 /// registered. 129 CallGraphNode *CallGraph::lookupNode(Region *region) const { 130 auto it = nodes.find(region); 131 return it == nodes.end() ? nullptr : it->second.get(); 132 } 133 134 /// Resolve the callable for given callee to a node in the callgraph, or the 135 /// external node if a valid node was not resolved. 136 CallGraphNode *CallGraph::resolveCallable(CallOpInterface call) const { 137 Operation *callable = call.resolveCallable(); 138 if (auto callableOp = dyn_cast_or_null<CallableOpInterface>(callable)) 139 if (auto *node = lookupNode(callableOp.getCallableRegion())) 140 return node; 141 142 // If we don't have a valid direct region, this is an external call. 143 return getExternalNode(); 144 } 145 146 /// Erase the given node from the callgraph. 147 void CallGraph::eraseNode(CallGraphNode *node) { 148 // Erase any children of this node first. 149 if (node->hasChildren()) { 150 for (const CallGraphNode::Edge &edge : llvm::make_early_inc_range(*node)) 151 if (edge.isChild()) 152 eraseNode(edge.getTarget()); 153 } 154 // Erase any edges to this node from any other nodes. 155 for (auto &it : nodes) { 156 it.second->edges.remove_if([node](const CallGraphNode::Edge &edge) { 157 return edge.getTarget() == node; 158 }); 159 } 160 nodes.erase(node->getCallableRegion()); 161 } 162 163 //===----------------------------------------------------------------------===// 164 // Printing 165 166 /// Dump the graph in a human readable format. 167 void CallGraph::dump() const { print(llvm::errs()); } 168 void CallGraph::print(raw_ostream &os) const { 169 os << "// ---- CallGraph ----\n"; 170 171 // Functor used to output the name for the given node. 172 auto emitNodeName = [&](const CallGraphNode *node) { 173 if (node->isExternal()) { 174 os << "<External-Node>"; 175 return; 176 } 177 178 auto *callableRegion = node->getCallableRegion(); 179 auto *parentOp = callableRegion->getParentOp(); 180 os << "'" << callableRegion->getParentOp()->getName() << "' - Region #" 181 << callableRegion->getRegionNumber(); 182 if (auto attrs = parentOp->getAttrList().getDictionary()) 183 os << " : " << attrs; 184 }; 185 186 for (auto &nodeIt : nodes) { 187 const CallGraphNode *node = nodeIt.second.get(); 188 189 // Dump the header for this node. 190 os << "// - Node : "; 191 emitNodeName(node); 192 os << "\n"; 193 194 // Emit each of the edges. 195 for (auto &edge : *node) { 196 os << "// -- "; 197 if (edge.isCall()) 198 os << "Call"; 199 else if (edge.isChild()) 200 os << "Child"; 201 202 os << "-Edge : "; 203 emitNodeName(edge.getTarget()); 204 os << "\n"; 205 } 206 os << "//\n"; 207 } 208 209 os << "// -- SCCs --\n"; 210 211 for (auto &scc : make_range(llvm::scc_begin(this), llvm::scc_end(this))) { 212 os << "// - SCC : \n"; 213 for (auto &node : scc) { 214 os << "// -- Node :"; 215 emitNodeName(node); 216 os << "\n"; 217 } 218 os << "\n"; 219 } 220 221 os << "// -------------------\n"; 222 } 223