From 50697b949ec37cb368c880014dddcd97a449054d Mon Sep 17 00:00:00 2001 From: Lewis Crawford Date: Fri, 18 Oct 2024 14:13:32 +0000 Subject: [PATCH 1/2] [CFGPrinter] Allow CFG dumps with a given filename 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. --- llvm/include/llvm/IR/Function.h | 11 ++++++++++- llvm/lib/Analysis/CFGPrinter.cpp | 13 +++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h index 43bf36d6f1eec..abe574a077f24 100644 --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -942,9 +942,14 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node { /// void viewCFG() const; + /// viewCFG - This function is meant for use from the debugger. It works just + /// like viewCFG(), but generates the dot file with the given filename. + void viewCFG(const char *Name) const; + /// Extended form to print edge weights. void viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI, - const BranchProbabilityInfo *BPI) const; + const BranchProbabilityInfo *BPI, + const char *Name = nullptr) const; /// viewCFGOnly - This function is meant for use from the debugger. It works /// 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 { /// void viewCFGOnly() const; + /// viewCFG - This function is meant for use from the debugger. It works just + /// like viewCFGOnly(), but generates the dot file with the given filename. + void viewCFGOnly(const char *Name) const; + /// Extended form to print edge weights. void viewCFGOnly(const BlockFrequencyInfo *BFI, const BranchProbabilityInfo *BPI) const; diff --git a/llvm/lib/Analysis/CFGPrinter.cpp b/llvm/lib/Analysis/CFGPrinter.cpp index 67a15197058b7..dc07d8239bd88 100644 --- a/llvm/lib/Analysis/CFGPrinter.cpp +++ b/llvm/lib/Analysis/CFGPrinter.cpp @@ -136,12 +136,17 @@ PreservedAnalyses CFGOnlyPrinterPass::run(Function &F, /// void Function::viewCFG() const { viewCFG(false, nullptr, nullptr); } +void Function::viewCFG(const char *Name) const { + viewCFG(false, nullptr, nullptr, Name); +} + void Function::viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI, - const BranchProbabilityInfo *BPI) const { + const BranchProbabilityInfo *BPI, + const char *Name) const { if (!CFGFuncName.empty() && !getName().contains(CFGFuncName)) return; DOTFuncInfo CFGInfo(this, BFI, BPI, BFI ? getMaxFreq(*this, BFI) : 0); - ViewGraph(&CFGInfo, "cfg" + getName(), ViewCFGOnly); + ViewGraph(&CFGInfo, Name ? Name : "cfg" + getName(), ViewCFGOnly); } /// viewCFGOnly - This function is meant for use from the debugger. It works @@ -151,6 +156,10 @@ void Function::viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI, /// void Function::viewCFGOnly() const { viewCFGOnly(nullptr, nullptr); } +void Function::viewCFGOnly(const char *Name) const { + viewCFG(true, nullptr, nullptr, Name); +} + void Function::viewCFGOnly(const BlockFrequencyInfo *BFI, const BranchProbabilityInfo *BPI) const { viewCFG(true, BFI, BPI); From 266e82daa9971f004a75d51e3f8468d06cd7e418 Mon Sep 17 00:00:00 2001 From: Lewis Crawford Date: Mon, 4 Nov 2024 17:03:29 +0000 Subject: [PATCH 2/2] Change variable Name to OutputFileName --- llvm/include/llvm/IR/Function.h | 10 +++++----- llvm/lib/Analysis/CFGPrinter.cpp | 13 +++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h index abe574a077f24..e7afcbd31420c 100644 --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -943,13 +943,13 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node { void viewCFG() const; /// viewCFG - This function is meant for use from the debugger. It works just - /// like viewCFG(), but generates the dot file with the given filename. - void viewCFG(const char *Name) const; + /// like viewCFG(), but generates the dot file with the given file name. + void viewCFG(const char *OutputFileName) const; /// Extended form to print edge weights. void viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI, const BranchProbabilityInfo *BPI, - const char *Name = nullptr) const; + const char *OutputFileName = nullptr) const; /// viewCFGOnly - This function is meant for use from the debugger. It works /// just like viewCFG, but it does not include the contents of basic blocks @@ -959,8 +959,8 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node { void viewCFGOnly() const; /// viewCFG - This function is meant for use from the debugger. It works just - /// like viewCFGOnly(), but generates the dot file with the given filename. - void viewCFGOnly(const char *Name) const; + /// like viewCFGOnly(), but generates the dot file with the given file name. + void viewCFGOnly(const char *OutputFileName) const; /// Extended form to print edge weights. void viewCFGOnly(const BlockFrequencyInfo *BFI, diff --git a/llvm/lib/Analysis/CFGPrinter.cpp b/llvm/lib/Analysis/CFGPrinter.cpp index dc07d8239bd88..af18fb6626e3b 100644 --- a/llvm/lib/Analysis/CFGPrinter.cpp +++ b/llvm/lib/Analysis/CFGPrinter.cpp @@ -136,17 +136,18 @@ PreservedAnalyses CFGOnlyPrinterPass::run(Function &F, /// void Function::viewCFG() const { viewCFG(false, nullptr, nullptr); } -void Function::viewCFG(const char *Name) const { - viewCFG(false, nullptr, nullptr, Name); +void Function::viewCFG(const char *OutputFileName) const { + viewCFG(false, nullptr, nullptr, OutputFileName); } void Function::viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI, const BranchProbabilityInfo *BPI, - const char *Name) const { + const char *OutputFileName) const { if (!CFGFuncName.empty() && !getName().contains(CFGFuncName)) return; DOTFuncInfo CFGInfo(this, BFI, BPI, BFI ? getMaxFreq(*this, BFI) : 0); - ViewGraph(&CFGInfo, Name ? Name : "cfg" + getName(), ViewCFGOnly); + ViewGraph(&CFGInfo, OutputFileName ? OutputFileName : "cfg" + getName(), + ViewCFGOnly); } /// viewCFGOnly - This function is meant for use from the debugger. It works @@ -156,8 +157,8 @@ void Function::viewCFG(bool ViewCFGOnly, const BlockFrequencyInfo *BFI, /// void Function::viewCFGOnly() const { viewCFGOnly(nullptr, nullptr); } -void Function::viewCFGOnly(const char *Name) const { - viewCFG(true, nullptr, nullptr, Name); +void Function::viewCFGOnly(const char *OutputFileName) const { + viewCFG(true, nullptr, nullptr, OutputFileName); } void Function::viewCFGOnly(const BlockFrequencyInfo *BFI,