Skip to content

Commit a7e20a8

Browse files
committed
[CallPrinter] Port CallPrinter passes to new pass manager
Port the legacy CallGraphViewer and CallGraphDOTPrinter to work with the new pass manager. Addresses issue #54323 Adds back related tests that were removed in commits d53a4e7 and 9e9d9ab Reviewed By: aeubanks Differential Revision: https://reviews.llvm.org/D122989
1 parent 17f6cba commit a7e20a8

File tree

6 files changed

+106
-28
lines changed

6 files changed

+106
-28
lines changed

llvm/include/llvm/Analysis/CallPrinter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@
1414
#ifndef LLVM_ANALYSIS_CALLPRINTER_H
1515
#define LLVM_ANALYSIS_CALLPRINTER_H
1616

17+
#include "llvm/IR/PassManager.h"
18+
1719
namespace llvm {
1820

1921
class ModulePass;
2022

23+
/// Pass for printing the call graph to a dot file
24+
class CallGraphDOTPrinterPass : public PassInfoMixin<CallGraphDOTPrinterPass> {
25+
public:
26+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
27+
};
28+
29+
/// Pass for viewing the call graph
30+
class CallGraphViewerPass : public PassInfoMixin<CallGraphViewerPass> {
31+
public:
32+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
33+
};
34+
2135
ModulePass *createCallGraphViewerPass();
2236
ModulePass *createCallGraphDOTPrinterPass();
2337

llvm/lib/Analysis/CallPrinter.cpp

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,71 @@ struct DOTGraphTraits<CallGraphDOTInfo *> : public DefaultDOTGraphTraits {
217217

218218
} // end llvm namespace
219219

220+
namespace {
221+
void doCallGraphDOTPrinting(
222+
Module &M, function_ref<BlockFrequencyInfo *(Function &)> LookupBFI) {
223+
std::string Filename;
224+
if (!CallGraphDotFilenamePrefix.empty())
225+
Filename = (CallGraphDotFilenamePrefix + ".callgraph.dot");
226+
else
227+
Filename = (std::string(M.getModuleIdentifier()) + ".callgraph.dot");
228+
errs() << "Writing '" << Filename << "'...";
229+
230+
std::error_code EC;
231+
raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
232+
233+
CallGraph CG(M);
234+
CallGraphDOTInfo CFGInfo(&M, &CG, LookupBFI);
235+
236+
if (!EC)
237+
WriteGraph(File, &CFGInfo);
238+
else
239+
errs() << " error opening file for writing!";
240+
errs() << "\n";
241+
}
242+
243+
void viewCallGraph(Module &M,
244+
function_ref<BlockFrequencyInfo *(Function &)> LookupBFI) {
245+
CallGraph CG(M);
246+
CallGraphDOTInfo CFGInfo(&M, &CG, LookupBFI);
247+
248+
std::string Title =
249+
DOTGraphTraits<CallGraphDOTInfo *>::getGraphName(&CFGInfo);
250+
ViewGraph(&CFGInfo, "callgraph", true, Title);
251+
}
252+
} // namespace
253+
254+
namespace llvm {
255+
PreservedAnalyses CallGraphDOTPrinterPass::run(Module &M,
256+
ModuleAnalysisManager &AM) {
257+
FunctionAnalysisManager &FAM =
258+
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
259+
260+
auto LookupBFI = [&FAM](Function &F) {
261+
return &FAM.getResult<BlockFrequencyAnalysis>(F);
262+
};
263+
264+
doCallGraphDOTPrinting(M, LookupBFI);
265+
266+
return PreservedAnalyses::all();
267+
}
268+
269+
PreservedAnalyses CallGraphViewerPass::run(Module &M,
270+
ModuleAnalysisManager &AM) {
271+
272+
FunctionAnalysisManager &FAM =
273+
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
274+
275+
auto LookupBFI = [&FAM](Function &F) {
276+
return &FAM.getResult<BlockFrequencyAnalysis>(F);
277+
};
278+
279+
viewCallGraph(M, LookupBFI);
280+
281+
return PreservedAnalyses::all();
282+
}
283+
} // namespace llvm
284+
220285
namespace {
221286
// Viewer
222287
class CallGraphViewer : public ModulePass {
@@ -239,12 +304,7 @@ bool CallGraphViewer::runOnModule(Module &M) {
239304
return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
240305
};
241306

242-
CallGraph CG(M);
243-
CallGraphDOTInfo CFGInfo(&M, &CG, LookupBFI);
244-
245-
std::string Title =
246-
DOTGraphTraits<CallGraphDOTInfo *>::getGraphName(&CFGInfo);
247-
ViewGraph(&CFGInfo, "callgraph", true, Title);
307+
viewCallGraph(M, LookupBFI);
248308

249309
return false;
250310
}
@@ -271,24 +331,7 @@ bool CallGraphDOTPrinter::runOnModule(Module &M) {
271331
return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
272332
};
273333

274-
std::string Filename;
275-
if (!CallGraphDotFilenamePrefix.empty())
276-
Filename = (CallGraphDotFilenamePrefix + ".callgraph.dot");
277-
else
278-
Filename = (std::string(M.getModuleIdentifier()) + ".callgraph.dot");
279-
errs() << "Writing '" << Filename << "'...";
280-
281-
std::error_code EC;
282-
raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
283-
284-
CallGraph CG(M);
285-
CallGraphDOTInfo CFGInfo(&M, &CG, LookupBFI);
286-
287-
if (!EC)
288-
WriteGraph(File, &CFGInfo);
289-
else
290-
errs() << " error opening file for writing!";
291-
errs() << "\n";
334+
doCallGraphDOTPrinting(M, LookupBFI);
292335

293336
return false;
294337
}

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
2828
#include "llvm/Analysis/CGSCCPassManager.h"
2929
#include "llvm/Analysis/CallGraph.h"
30+
#include "llvm/Analysis/CallPrinter.h"
3031
#include "llvm/Analysis/CostModel.h"
3132
#include "llvm/Analysis/CycleAnalysis.h"
3233
#include "llvm/Analysis/DDG.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ MODULE_PASS("constmerge", ConstantMergePass())
5353
MODULE_PASS("cross-dso-cfi", CrossDSOCFIPass())
5454
MODULE_PASS("deadargelim", DeadArgumentEliminationPass())
5555
MODULE_PASS("debugify", NewPMDebugifyPass())
56+
MODULE_PASS("dot-callgraph", CallGraphDOTPrinterPass())
5657
MODULE_PASS("elim-avail-extern", EliminateAvailableExternallyPass())
5758
MODULE_PASS("extract-blocks", BlockExtractorPass())
5859
MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
@@ -112,6 +113,7 @@ MODULE_PASS("strip-nondebug", StripNonDebugSymbolsPass())
112113
MODULE_PASS("strip-nonlinetable-debuginfo", StripNonLineTableDebugInfoPass())
113114
MODULE_PASS("synthetic-counts-propagation", SyntheticCountsPropagation())
114115
MODULE_PASS("verify", VerifierPass())
116+
MODULE_PASS("view-callgraph", CallGraphViewerPass())
115117
MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass())
116118
MODULE_PASS("dfsan", DataFlowSanitizerPass())
117119
MODULE_PASS("msan-module", ModuleMemorySanitizerPass({}))

llvm/test/Other/heat-colors-graphs.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
; RUN: opt %s -dot-cfg -cfg-heat-colors -cfg-dot-filename-prefix=%t -disable-output
2-
; RUN: FileCheck %s -input-file=%t.f.dot
2+
; RUN: FileCheck %s -input-file=%t.f.dot --check-prefixes=CHECK-CFG,CHECK-BOTH
3+
; RUN: opt %s -dot-callgraph -callgraph-heat-colors -callgraph-dot-filename-prefix=%t -disable-output
4+
; RUN: FileCheck %s -input-file=%t.callgraph.dot --check-prefix=CHECK-BOTH
35

4-
; CHECK: color="#[[#%x,]]", style={{[a-z]+}}, fillcolor="#[[#%x,]]"
5-
; CHECK: color="#[[#%x,]]", style={{[a-z]+}}, fillcolor="#[[#%x,]]"
6-
; CHECK: color="#[[#%x,]]", style={{[a-z]+}}, fillcolor="#[[#%x,]]"
6+
; CHECK-BOTH: color="#[[#%x,]]", style={{[a-z]+}}, fillcolor="#[[#%x,]]"
7+
; CHECK-CFG: color="#[[#%x,]]", style={{[a-z]+}}, fillcolor="#[[#%x,]]"
8+
; CHECK-CFG: color="#[[#%x,]]", style={{[a-z]+}}, fillcolor="#[[#%x,]]"
79

810
define void @f(i32) {
911
entry:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: opt %s -dot-callgraph -callgraph-multigraph -callgraph-dot-filename-prefix=%t -disable-output
2+
; RUN: FileCheck %s -input-file=%t.callgraph.dot --check-prefix=CHECK-MULTIGRAPH
3+
; RUN: opt %s -dot-callgraph -callgraph-dot-filename-prefix=%t -disable-output
4+
; RUN: FileCheck %s -input-file=%t.callgraph.dot --check-prefix=CHECK
5+
6+
; CHECK-MULTIGRAPH: {external caller}
7+
; CHECK-NOT: {external caller}
8+
9+
define void @bar() {
10+
ret void
11+
}
12+
13+
define void @foo() {
14+
call void @bar()
15+
ret void
16+
}

0 commit comments

Comments
 (0)