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

Print out callgraph of a specific entry point #6354

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
153 changes: 108 additions & 45 deletions src/passes/PrintCallGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <iomanip>
#include <memory>
#include <sstream>

#include "ir/element-utils.h"
#include "ir/module-utils.h"
Expand All @@ -34,72 +35,134 @@ struct PrintCallGraph : public Pass {
bool modifiesBinaryenIR() override { return false; }

void run(Module* module) override {
std::ostream& o = std::cout;
o << "digraph call {\n"
" rankdir = LR;\n"
" subgraph cluster_key {\n"
" node [shape=box, fontname=courier, fontsize=10];\n"
" edge [fontname=courier, fontsize=10];\n"
" label = \"Key\";\n"
" \"Import\" [style=\"filled\", fillcolor=\"turquoise\"];\n"
" \"Export\" [style=\"filled\", fillcolor=\"gray\"];\n"
" \"Indirect Target\" [style=\"filled, rounded\", "
"fillcolor=\"white\"];\n"
" \"A\" -> \"B\" [style=\"filled, rounded\", label = \"Direct "
"Call\"];\n"
" }\n\n"
" node [shape=box, fontname=courier, fontsize=10];\n";

// Defined functions
ModuleUtils::iterDefinedFunctions(*module, [&](Function* curr) {
std::cout << " \"" << curr->name
<< "\" [style=\"filled\", fillcolor=\"white\"];\n";
});

// Imported functions
ModuleUtils::iterImportedFunctions(*module, [&](Function* curr) {
o << " \"" << curr->name
<< "\" [style=\"filled\", fillcolor=\"turquoise\"];\n";
});

// Exports
for (auto& curr : module->exports) {
if (curr->kind == ExternalKind::Function) {
Function* func = module->getFunction(curr->value);
o << " \"" << func->name
<< "\" [style=\"filled\", fillcolor=\"gray\"];\n";
}
}
Name caller = getPassOptions().getArgumentOrDefault(
"callgraph-entry",
"");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this flag in the top comment (line 20 or so).


struct CallPrinter : public PostWalker<CallPrinter> {
struct GraphCreator : public PostWalker<GraphCreator> {
Module* module;
Function* currFunction;
std::set<Name> visitedTargets; // Used to avoid printing duplicate edges.
std::vector<Function*> allIndirectTargets;
CallPrinter(Module* module) : module(module) {
std::multimap<Name, Name> fullGraph; // First param is the caller/parent, second is the callee/child
std::stringstream callGraphStream;

GraphCreator(Module* module, Name caller) : module(module) {
// Walk function bodies.
ModuleUtils::iterDefinedFunctions(*module, [&](Function* curr) {
currFunction = curr;
visitedTargets.clear();
walk(curr->body);
});

constructCallgraph(caller);
}

void constructCallgraph(Name caller) {
if (caller.toString().empty()) {
// construct callgraph for the entire module
for (const auto& graph : fullGraph) {
callGraphStream << " \"" << graph.first << "\" -> \"" << graph.second
<< "\"; // call\n";
}
} else {
// construct callgraph for a given entry point
std::set<Name> next{caller};
visitedTargets.clear();

while (!next.empty()) {
std::set<Name> new_next;
for (auto curFunc : next) {
if (visitedTargets.count(curFunc)) {
continue;
}
visitedTargets.insert(curFunc);

auto count = fullGraph.count(curFunc);
auto range = fullGraph.equal_range(curFunc);
for (auto iter = range.first; iter != range.second; ++iter) {
Name callee = iter->second;
new_next.insert(callee);

callGraphStream << " \"" << curFunc.toString() << "\" -> \"" << callee.toString()
<< "\"; // call\n";
}
}
next = std::move(new_next);
}
}
}

void visitCall(Call* curr) {
auto* target = module->getFunction(curr->target);
if (!visitedTargets.emplace(target->name).second) {
return;
}
std::cout << " \"" << currFunction->name << "\" -> \"" << target->name
<< "\"; // call\n";
fullGraph.emplace(currFunction->name, target->name);
}
};
CallPrinter printer(module);
GraphCreator graphCreator(module, caller);

std::ostream& o = std::cout;
o << "digraph call {\n"
" rankdir = LR;\n"
" subgraph cluster_key {\n"
" node [shape=box, fontname=courier, fontsize=10];\n"
" edge [fontname=courier, fontsize=10];\n"
" label = \"Key\";\n"
" \"Import\" [style=\"filled\", fillcolor=\"turquoise\"];\n"
" \"Export\" [style=\"filled\", fillcolor=\"gray\"];\n"
" \"Indirect Target\" [style=\"filled, rounded\", "
"fillcolor=\"white\"];\n"
" \"A\" -> \"B\" [style=\"filled, rounded\", label = \"Direct "
"Call\"];\n"
" }\n\n"
" node [shape=box, fontname=courier, fontsize=10];\n";

// Defined functions
ModuleUtils::iterDefinedFunctions(*module, [&](Function* curr) {
if (!caller.toString().empty() &&
graphCreator.visitedTargets.count(curr->name) == 0) {
return;
}
std::cout << " \"" << curr->name
<< "\" [style=\"filled\", fillcolor=\"white\"];\n";
});

// Imported functions
ModuleUtils::iterImportedFunctions(*module, [&](Function* curr) {
if (!caller.toString().empty() &&
graphCreator.visitedTargets.count(curr->name) == 0) {
return;
}
o << " \"" << curr->name
<< "\" [style=\"filled\", fillcolor=\"turquoise\"];\n";
});

// Exports
for (auto& curr : module->exports) {
if (curr->kind == ExternalKind::Function) {
Function* func = module->getFunction(curr->value);
if (!caller.toString().empty() &&
graphCreator.visitedTargets.count(func->name) == 0) {
continue;
}
o << " \"" << func->name
<< "\" [style=\"filled\", fillcolor=\"gray\"];\n";
}
}

// Indirect Targets
ElementUtils::iterAllElementFunctionNames(module, [&](Name& name) {
auto* func = module->getFunction(name);
o << " \"" << func->name << "\" [style=\"filled, rounded\"];\n";
});
ElementUtils::iterAllElementFunctionNames(module, [&](Name& name) {
auto* func = module->getFunction(name);
if (!caller.toString().empty() &&
graphCreator.visitedTargets.count(func->name) == 0) {
return;
}
o << " \"" << func->name << "\" [style=\"filled, rounded\"];\n";
});

o << graphCreator.callGraphStream.rdbuf();

o << "}\n";
}
Expand Down