Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][CallGraph] Fix abstract edge connected to external node #116177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions mlir/lib/Analysis/CallGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ CallGraph::CallGraph(Operation *op)
/// Get or add a call graph node for the given region.
CallGraphNode *CallGraph::getOrAddNode(Region *region,
CallGraphNode *parentNode) {
assert(region && isa<CallableOpInterface>(region->getParentOp()) &&
Operation *parentOp = region->getParentOp();
assert(region && isa<CallableOpInterface>(parentOp) &&
"expected parent operation to be callable");
std::unique_ptr<CallGraphNode> &node = nodes[region];
if (!node) {
Expand All @@ -122,13 +123,12 @@ CallGraphNode *CallGraph::getOrAddNode(Region *region,
if (parentNode) {
parentNode->addChildEdge(node.get());
} else {
// Otherwise, connect all callable nodes to the external node, this allows
// for conservatively including all callable nodes within the graph.
// FIXME This isn't correct, this is only necessary for callable nodes
// that *could* be called from external sources. This requires extending
// the interface for callables to check if they may be referenced
// externally.
externalCallerNode.addAbstractEdge(node.get());
// Otherwise, connect all symbol nodes with public visibility
// to the external node, which is a set including callable nodes
// may be referenced externally.
if (isa<SymbolOpInterface>(parentOp) &&
cast<SymbolOpInterface>(parentOp).isPublic())
externalCallerNode.addAbstractEdge(node.get());
}
}
return node.get();
Expand Down Expand Up @@ -199,9 +199,8 @@ void CallGraph::print(raw_ostream &os) const {
os << " : " << attrs;
};

for (auto &nodeIt : nodes) {
const CallGraphNode *node = nodeIt.second.get();

// Functor used to emit the given node and edges.
auto emitNodeAndEdge = [&](const CallGraphNode *node) {
// Dump the header for this node.
os << "// - Node : ";
emitNodeName(node);
Expand All @@ -220,7 +219,13 @@ void CallGraph::print(raw_ostream &os) const {
os << "\n";
}
os << "//\n";
}
};

// Emit all graph nodes including ExternalCallerNode and UnknownCalleeNode.
for (auto &nodeIt : nodes)
emitNodeAndEdge(nodeIt.second.get());
emitNodeAndEdge(getExternalCallerNode());
emitNodeAndEdge(getUnknownCalleeNode());

os << "// -- SCCs --\n";

Expand Down
29 changes: 23 additions & 6 deletions mlir/test/Analysis/test-callgraph.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ module attributes {test.name = "simple"} {
return
}

// CHECK-NOT: Node{{.*}}func_b
func.func private @func_b()

// CHECK: Node{{.*}}func_c
// CHECK: Node{{.*}}func_c{{.*}}private
// CHECK-NEXT: Call-Edge{{.*}}Unknown-Callee-Node
func.func @func_c() {
func.func private @func_c() {
call @func_b() : () -> ()
return
}

// CHECK: Node{{.*}}func_d
// CHECK-NEXT: Call-Edge{{.*}}func_c
// CHECK-NEXT: Call-Edge{{.*}}func_c{{.*}}private
func.func @func_d() {
call @func_c() : () -> ()
return
}

// CHECK: Node{{.*}}func_e
// CHECK-DAG: Call-Edge{{.*}}func_c
// CHECK-DAG: Call-Edge{{.*}}func_c{{.*}}private
// CHECK-DAG: Call-Edge{{.*}}func_d
// CHECK-DAG: Call-Edge{{.*}}func_e
func.func @func_e() {
Expand All @@ -49,6 +50,16 @@ module attributes {test.name = "simple"} {
call_indirect %fn() : () -> ()
return
}

// CHECK: Node{{.*}}External-Caller-Node
// CHECK: Edge{{.*}}func_a
// CHECK-NOT: Edge{{.*}}func_b
// CHECK-NOT: Edge{{.*}}func_c
// CHECK: Edge{{.*}}func_d
// CHECK: Edge{{.*}}func_e
// CHECK: Edge{{.*}}func_f

// CHECK: Node{{.*}}Unknown-Callee-Node
}

// -----
Expand All @@ -57,17 +68,23 @@ module attributes {test.name = "simple"} {
module attributes {test.name = "nested"} {
module @nested_module {
// CHECK: Node{{.*}}func_a
func.func @func_a() {
func.func nested @func_a() {
return
}
}

// CHECK: Node{{.*}}func_b
// CHECK: Call-Edge{{.*}}func_a
// CHECK: Call-Edge{{.*}}func_a{{.*}}nested
func.func @func_b() {
"test.conversion_call_op"() { callee = @nested_module::@func_a } : () -> ()
return
}

// CHECK: Node{{.*}}External-Caller-Node
// CHECK: Edge{{.*}}func_b
// CHECK-NOT: Edge{{.*}}func_a

// CHECK: Node{{.*}}Unknown-Callee-Node
}

// -----
Expand Down
Loading