17 #include "llvm/ADT/PointerUnion.h" 18 #include "llvm/ADT/SCCIterator.h" 19 #include "llvm/Support/raw_ostream.h" 33 assert(!
isExternal() &&
"the external node has no callable region");
34 return callableRegion;
40 assert(
isExternal() &&
"abstract edges are only valid on external nodes");
41 addEdge(node, Edge::Kind::Abstract);
46 addEdge(node, Edge::Kind::Call);
51 addEdge(child, Edge::Kind::Child);
56 return llvm::any_of(edges, [](
const Edge &edge) {
return edge.
isChild(); });
60 void CallGraphNode::addEdge(
CallGraphNode *node, Edge::Kind kind) {
61 edges.insert({node, kind});
73 if (CallOpInterface call = dyn_cast<CallOpInterface>(op)) {
77 if (resolveCalls && parentNode)
83 if (CallableOpInterface callable = dyn_cast<CallableOpInterface>(op)) {
84 if (
auto *callableRegion = callable.getCallableRegion())
85 parentNode = cg.
getOrAddNode(callableRegion, parentNode);
109 assert(region && isa<CallableOpInterface>(region->
getParentOp()) &&
110 "expected parent operation to be callable");
111 std::unique_ptr<CallGraphNode> &node = nodes[region];
134 auto it = nodes.find(region);
135 return it == nodes.end() ? nullptr : it->second.get();
143 Operation *callable = call.resolveCallable(&symbolTable);
144 if (
auto callableOp = dyn_cast_or_null<CallableOpInterface>(callable))
145 if (
auto *node =
lookupNode(callableOp.getCallableRegion()))
161 for (
auto &it : nodes) {
175 os <<
"// ---- CallGraph ----\n";
180 os <<
"<External-Node>";
187 << callableRegion->getRegionNumber();
188 auto attrs = parentOp->getAttrDictionary();
190 os <<
" : " << attrs;
193 for (
auto &nodeIt : nodes) {
197 os <<
"// - Node : ";
202 for (
auto &edge : *node) {
206 else if (edge.isChild())
210 emitNodeName(edge.getTarget());
216 os <<
"// -- SCCs --\n";
218 for (
auto &scc : make_range(llvm::scc_begin(
this), llvm::scc_end(
this))) {
219 os <<
"// - SCC : \n";
220 for (
auto &node : scc) {
221 os <<
"// -- Node :";
228 os <<
"// -------------------\n";
Include the generated interface declarations.
void addChildEdge(CallGraphNode *child)
Adds a reference edge to the given child node.
This class contains a list of basic blocks and a link to the parent operation it is attached to...
CallGraphNode * lookupNode(Region *region) const
Lookup a call graph node for the given region, or nullptr if none is registered.
bool isChild() const
Returns true if this edge represents a Child edge.
Operation is a basic unit of execution within MLIR.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
CallGraphNode * getExternalNode() const
Return the callgraph node representing the indirect-external callee.
CallGraphNode * resolveCallable(CallOpInterface call, SymbolTableCollection &symbolTable) const
Resolve the callable for given callee to a node in the callgraph, or the external node if a valid nod...
This class represents a collection of SymbolTables.
CallGraphNode * getOrAddNode(Region *region, CallGraphNode *parentNode)
Get or add a call graph node for the given region.
CallGraphNode * getTarget() const
Returns the target node for this edge.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
void addCallEdge(CallGraphNode *node)
Add an outgoing call edge from this node.
Operation * getParentOp()
Return the parent operation this region is attached to.
This class represents a single callable in the callgraph.
bool isExternal() const
Returns true if this node is an external node.
void addAbstractEdge(CallGraphNode *node)
Adds an abstract reference edge to the given node.
void print(raw_ostream &os) const
This class represents a directed edge between two nodes in the callgraph.
OperationName getName()
The name of an operation is the key identifier for it.
void eraseNode(CallGraphNode *node)
Erase the given node from the callgraph.
static void computeCallGraph(Operation *op, CallGraph &cg, SymbolTableCollection &symbolTable, CallGraphNode *parentNode, bool resolveCalls)
Recursively compute the callgraph edges for the given operation.
Region * getCallableRegion() const
Returns the callable region this node represents.
void dump() const
Dump the graph in a human readable format.
bool hasChildren() const
Returns true if this node has any child edges.