[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:54:42 PDT 2025


https://github.com/knickish created https://github.com/llvm/llvm-project/pull/161979

None

>From 19dc2c27944e41912c3f9981cae083841f516f5d 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 dc00db9daa3b6..49c3f1fc3a12f 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 ece50ed95fc49..99719869e92b6 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -170,8 +170,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"));
@@ -179,7 +183,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
@@ -940,7 +945,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.
   {
@@ -962,7 +967,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;
   {
@@ -986,7 +991,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.
     {
@@ -1040,7 +1046,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.
     {
@@ -1061,7 +1068,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,
@@ -1080,7 +1087,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.
   {
@@ -1103,7 +1110,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.
@@ -1119,7 +1126,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
             CurDAG->dump());
 
   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