Skip to content

Commit 07b6013

Browse files
[CFGPrinter] Allow CFG dumps with a given filename (#112906)
Add functions to print the CFG via the debugger using a specified filename. This is useful when comparing CFGs for the same function before vs after a change, or when handling functions with names that are too long to be file names.
1 parent f3025c8 commit 07b6013

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

llvm/include/llvm/IR/Function.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,14 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
942942
///
943943
void viewCFG() const;
944944

945+
/// viewCFG - This function is meant for use from the debugger. It works just
946+
/// like viewCFG(), but generates the dot file with the given file name.
947+
void viewCFG(const char *OutputFileName) const;
948+
945949
/// Extended form to print edge weights.
946950
void viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI,
947-
const BranchProbabilityInfo *BPI) const;
951+
const BranchProbabilityInfo *BPI,
952+
const char *OutputFileName = nullptr) const;
948953

949954
/// viewCFGOnly - This function is meant for use from the debugger. It works
950955
/// just like viewCFG, but it does not include the contents of basic blocks
@@ -953,6 +958,10 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
953958
///
954959
void viewCFGOnly() const;
955960

961+
/// viewCFG - This function is meant for use from the debugger. It works just
962+
/// like viewCFGOnly(), but generates the dot file with the given file name.
963+
void viewCFGOnly(const char *OutputFileName) const;
964+
956965
/// Extended form to print edge weights.
957966
void viewCFGOnly(const BlockFrequencyInfo *BFI,
958967
const BranchProbabilityInfo *BPI) const;

llvm/lib/Analysis/CFGPrinter.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,18 @@ PreservedAnalyses CFGOnlyPrinterPass::run(Function &F,
136136
///
137137
void Function::viewCFG() const { viewCFG(false, nullptr, nullptr); }
138138

139+
void Function::viewCFG(const char *OutputFileName) const {
140+
viewCFG(false, nullptr, nullptr, OutputFileName);
141+
}
142+
139143
void Function::viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI,
140-
const BranchProbabilityInfo *BPI) const {
144+
const BranchProbabilityInfo *BPI,
145+
const char *OutputFileName) const {
141146
if (!CFGFuncName.empty() && !getName().contains(CFGFuncName))
142147
return;
143148
DOTFuncInfo CFGInfo(this, BFI, BPI, BFI ? getMaxFreq(*this, BFI) : 0);
144-
ViewGraph(&CFGInfo, "cfg" + getName(), ViewCFGOnly);
149+
ViewGraph(&CFGInfo, OutputFileName ? OutputFileName : "cfg" + getName(),
150+
ViewCFGOnly);
145151
}
146152

147153
/// viewCFGOnly - This function is meant for use from the debugger. It works
@@ -151,6 +157,10 @@ void Function::viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI,
151157
///
152158
void Function::viewCFGOnly() const { viewCFGOnly(nullptr, nullptr); }
153159

160+
void Function::viewCFGOnly(const char *OutputFileName) const {
161+
viewCFG(true, nullptr, nullptr, OutputFileName);
162+
}
163+
154164
void Function::viewCFGOnly(const BlockFrequencyInfo *BFI,
155165
const BranchProbabilityInfo *BPI) const {
156166
viewCFG(true, BFI, BPI);

0 commit comments

Comments
 (0)