-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[CFGPrinter] Added command line option to change DOT font #148403
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
base: main
Are you sure you want to change the base?
Conversation
This patch introduces the ability to change the fontname used in the DOT graphs of the view-cfg and dot-cfg passes.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-analysis Author: PysKa Ratzinger (PysKa-Ratzinger) ChangesThis patch introduces the ability to change the fontname used in the DOT graphs of the view-cfg and dot-cfg passes. This was tested by compiling the bitcode for a simple main.cpp with int main() { return 0; } And generating 2 CFG graphs, one without the new argument (to make sure the original behavior is maintained), and another with the new argument, which resulted in the following: # Generated with opt -p dot-cfg ./main.bc
digraph "CFG for 'main' function" {
label="CFG for 'main' function";
Node0x557a6e235010 [shape=record,color="#b70d28ff", style=filled, fillcolor="#b70d2870" fontname="Courier",label="{entry:\l| %retval = alloca i32, align 4\l store i32 0, ptr %retval, align 4\l ret i32 0\l}"];
} # Generated with opt -p dot-cfg ./main.bc -cfg-fontname=Arial
digraph "CFG for 'main' function" {
label="CFG for 'main' function";
Node0x55c9e8286010 [shape=record,color="#b70d28ff", style=filled, fillcolor="#b70d2870" fontname="Arial",label="{entry:\l| %retval = alloca i32, align 4\l store i32 0, ptr %retval, align 4\l ret i32 0\l}"];
} Full diff: https://github.com/llvm/llvm-project/pull/148403.diff 2 Files Affected:
diff --git a/llvm/include/llvm/Analysis/CFGPrinter.h b/llvm/include/llvm/Analysis/CFGPrinter.h
index ec26da87eb916..8955ada815ed0 100644
--- a/llvm/include/llvm/Analysis/CFGPrinter.h
+++ b/llvm/include/llvm/Analysis/CFGPrinter.h
@@ -27,11 +27,14 @@
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ProfDataUtils.h"
+#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DOTGraphTraits.h"
#include "llvm/Support/FormatVariadic.h"
namespace llvm {
+LLVM_ABI extern cl::opt<std::string> CFGFontName;
+
class ModuleSlotTracker;
template <class GraphType> struct GraphTraits;
@@ -321,9 +324,14 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
? (getHeatColor(0))
: (getHeatColor(1));
+ std::string FontName = "Courier";
+ if (!CFGFontName.empty()) {
+ FontName = CFGFontName;
+ }
+
std::string Attrs = "color=\"" + EdgeColor + "ff\", style=filled," +
- " fillcolor=\"" + Color + "70\"" +
- " fontname=\"Courier\"";
+ " fillcolor=\"" + Color + "70\"" + " fontname=\"" +
+ FontName + "\"";
return Attrs;
}
LLVM_ABI bool isNodeHidden(const BasicBlock *Node,
diff --git a/llvm/lib/Analysis/CFGPrinter.cpp b/llvm/lib/Analysis/CFGPrinter.cpp
index 38aad849755be..d7f411ddbf9a4 100644
--- a/llvm/lib/Analysis/CFGPrinter.cpp
+++ b/llvm/lib/Analysis/CFGPrinter.cpp
@@ -26,6 +26,11 @@
using namespace llvm;
+cl::opt<std::string>
+ llvm::CFGFontName("cfg-fontname", cl::Hidden,
+ cl::desc("The name of a font to use for the DOT"
+ " file (defaults to Courier)"));
+
static cl::opt<std::string>
CFGFuncName("cfg-func-name", cl::Hidden,
cl::desc("The name of a function (or its substring)"
|
As an aside, the motivation for doing this patch is because "opt view-cfg" by default opens KGraphViewer, which is fine, but zooming into the individual blocks, or zooming out, makes the font very ugly. I'm assuming the font is bitmap on my system? I could make a script to run "opt dot-cfg" first, edit the file to use a ttf font, and then open the file again with KGraphViewer, but there is a secondary problem that I'm looking at heavily templated code, which sometimes explodes the names of the functions and prevents me from using the "dot-cfg" pass, since the name of the file would exceed the max filename allowed on Linux. So I feel like this was really the only good option. |
This patch introduces the ability to change the fontname used in the DOT graphs of the view-cfg and dot-cfg passes.
This was tested by compiling the bitcode for a simple main.cpp with
clang++ -O0 -emit-llvm ./main.cpp -c -o main.bc
And generating 2 CFG graphs, one without the new argument (to make sure the original behavior is maintained), and another with the new argument, which resulted in the following: