[llvm] [SelectionDAG] add cli option to write SelectionDAG graphviz to file (PR #161979)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 4 14:59:27 PDT 2025
https://github.com/knickish updated https://github.com/llvm/llvm-project/pull/161979
>From baaa051d2857bafd03d5635f5d3d67504df2314c Mon Sep 17 00:00:00 2001
From: kirk <knickish at gmail.com>
Date: Sat, 4 Oct 2025 21:21:36 +0000
Subject: [PATCH] [SelectionDAG] add a cli option that will only write DAG
vizualization to file instead of opening the viewer
---
llvm/include/llvm/CodeGen/SelectionDAG.h | 3 ++
.../CodeGen/SelectionDAG/SelectionDAGISel.cpp | 29 ++++++++++++-------
.../SelectionDAG/SelectionDAGPrinter.cpp | 17 +++++++++++
3 files changed, 38 insertions(+), 11 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/SelectionDAG.h b/llvm/include/llvm/CodeGen/SelectionDAG.h
index 62c0806dada2e..24113778d52a8 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAG.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAG.h
@@ -530,6 +530,9 @@ class SelectionDAG {
/// Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
LLVM_ABI void viewGraph(const std::string &Title);
+ /// Pop up a GraphViz/gv window with the DAG rendered using 'dot', or write
+ /// the file out instead.
+ LLVM_ABI void viewGraph(const std::string &Title, const bool WriteFile);
LLVM_ABI void viewGraph();
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index c35f29dc2548c..aa3adef235dce 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -175,8 +175,12 @@ static cl::opt<bool>
ViewISelDAGs("view-isel-dags", cl::Hidden,
cl::desc("Pop up a window to show isel dags as they are selected"));
static cl::opt<bool>
-ViewSchedDAGs("view-sched-dags", cl::Hidden,
- cl::desc("Pop up a window to show sched dags as they are processed"));
+ WriteDAGsToFile("write-dags-to-file", cl::Hidden,
+ cl::desc("Write dags as they are selected to DOT files "
+ "instead of opening viewer"));
+static cl::opt<bool> ViewSchedDAGs(
+ "view-sched-dags", cl::Hidden,
+ cl::desc("Pop up a window to show sched dags as they are processed"));
static cl::opt<bool>
ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
cl::desc("Pop up a window to show SUnit dags after they are processed"));
@@ -184,7 +188,8 @@ ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
static const bool ViewDAGCombine1 = false, ViewLegalizeTypesDAGs = false,
ViewDAGCombineLT = false, ViewLegalizeDAGs = false,
ViewDAGCombine2 = false, ViewISelDAGs = false,
- ViewSchedDAGs = false, ViewSUnitDAGs = false;
+ WriteDAGsToFile = false, ViewSchedDAGs = false,
+ ViewSUnitDAGs = false;
#endif
#ifndef NDEBUG
@@ -945,7 +950,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
#endif
if (ViewDAGCombine1 && MatchFilterBB)
- CurDAG->viewGraph("dag-combine1 input for " + BlockName);
+ CurDAG->viewGraph("dag-combine1 input for " + BlockName, WriteDAGsToFile);
// Run the DAG combiner in pre-legalize mode.
{
@@ -967,7 +972,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
// Second step, hack on the DAG until it only uses operations and types that
// the target supports.
if (ViewLegalizeTypesDAGs && MatchFilterBB)
- CurDAG->viewGraph("legalize-types input for " + BlockName);
+ CurDAG->viewGraph("legalize-types input for " + BlockName, WriteDAGsToFile);
bool Changed;
{
@@ -991,7 +996,8 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
if (Changed) {
if (ViewDAGCombineLT && MatchFilterBB)
- CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
+ CurDAG->viewGraph("dag-combine-lt input for " + BlockName,
+ WriteDAGsToFile);
// Run the DAG combiner in post-type-legalize mode.
{
@@ -1045,7 +1051,8 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
#endif
if (ViewDAGCombineLT && MatchFilterBB)
- CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
+ CurDAG->viewGraph("dag-combine-lv input for " + BlockName,
+ WriteDAGsToFile);
// Run the DAG combiner in post-type-legalize mode.
{
@@ -1066,7 +1073,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
}
if (ViewLegalizeDAGs && MatchFilterBB)
- CurDAG->viewGraph("legalize input for " + BlockName);
+ CurDAG->viewGraph("legalize input for " + BlockName, WriteDAGsToFile);
{
NamedRegionTimer T("legalize", "DAG Legalization", GroupName,
@@ -1085,7 +1092,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
#endif
if (ViewDAGCombine2 && MatchFilterBB)
- CurDAG->viewGraph("dag-combine2 input for " + BlockName);
+ CurDAG->viewGraph("dag-combine2 input for " + BlockName, WriteDAGsToFile);
// Run the DAG combiner in post-legalize mode.
{
@@ -1108,7 +1115,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ComputeLiveOutVRegInfo();
if (ViewISelDAGs && MatchFilterBB)
- CurDAG->viewGraph("isel input for " + BlockName);
+ CurDAG->viewGraph("isel input for " + BlockName, WriteDAGsToFile);
// Third, instruction select all of the operations to machine code, adding the
// code to the MachineBasicBlock.
@@ -1124,7 +1131,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
CurDAG->dump(DumpSortedDAG));
if (ViewSchedDAGs && MatchFilterBB)
- CurDAG->viewGraph("scheduler input for " + BlockName);
+ CurDAG->viewGraph("scheduler input for " + BlockName, WriteDAGsToFile);
// Schedule machine code.
ScheduleDAGSDNodes *Scheduler = CreateScheduler();
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
index ac28f62894788..0c8157cc7447e 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
@@ -154,6 +154,23 @@ void SelectionDAG::viewGraph(const std::string &Title) {
#endif // NDEBUG
}
+/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
+/// rendered using 'dot' if not writing to file. Otherwise, just write it out.
+///
+void SelectionDAG::viewGraph(const std::string &Title, const bool WriteFile) {
+// This code is only for debugging!
+#ifndef NDEBUG
+ std::string GraphName = "dag." + getMachineFunction().getName().str();
+ if (WriteFile)
+ WriteGraph(this, Twine(GraphName), false, Title);
+ else
+ ViewGraph(this, Twine(GraphName), false, Title);
+#else
+ errs() << "SelectionDAG::viewGraph is only available in debug builds on "
+ << "systems with Graphviz or gv!\n";
+#endif // NDEBUG
+}
+
// This overload is defined out-of-line here instead of just using a
// default parameter because this is easiest for gdb to call.
void SelectionDAG::viewGraph() {
More information about the llvm-commits
mailing list