Skip to content

Conversation

@LewisCrawford
Copy link
Contributor

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.

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.
@llvmbot llvmbot added llvm:ir llvm:analysis Includes value tracking, cost tables and constant folding labels Oct 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2024

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-analysis

Author: Lewis Crawford (LewisCrawford)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/112906.diff

2 Files Affected:

  • (modified) llvm/include/llvm/IR/Function.h (+10-1)
  • (modified) llvm/lib/Analysis/CFGPrinter.cpp (+11-2)
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index 43bf36d6f1eec9..abe574a077f24a 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<Function> {
   ///
   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<Function> {
   ///
   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 67a15197058b73..dc07d8239bd889 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);

@LewisCrawford
Copy link
Contributor Author

Requesting review from @WenleiHe and @david-xl , as they reviewed a similar CFGPrinter debugger utils patch a few years ago in https://reviews.llvm.org/D77978

Copy link
Member

@WenleiHe WenleiHe left a comment

Choose a reason for hiding this comment

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

lgtm with a nit. thanks.

@LewisCrawford LewisCrawford merged commit 07b6013 into llvm:main Nov 5, 2024
8 checks passed
PhilippRados pushed a commit to PhilippRados/llvm-project that referenced this pull request Nov 6, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:ir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants