[llvm] [CodeGen] Port MachineCFGPrinter to new pass manager (PR #137570)

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 28 01:32:19 PDT 2025


https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/137570

>From 8a4b11c1d150030ec14fb2006c61eab4df6cc43f Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 28 Apr 2025 10:34:55 +0800
Subject: [PATCH 1/4] [CodeGen] Port MachineCFGPrinter to new pass manager

---
 llvm/include/llvm/CodeGen/MachineCFGPrinter.h        | 12 ++++++++++++
 llvm/include/llvm/Passes/MachinePassRegistry.def     |  2 +-
 llvm/lib/CodeGen/MachineCFGPrinter.cpp               | 12 ++++++++++++
 llvm/lib/Passes/PassBuilder.cpp                      |  1 +
 .../test/Analysis/DotMachineCFG/AMDGPU/functions.mir |  2 ++
 .../Analysis/DotMachineCFG/AMDGPU/irreducible.mir    |  4 ++++
 6 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/CodeGen/MachineCFGPrinter.h b/llvm/include/llvm/CodeGen/MachineCFGPrinter.h
index ea3ff5a5c828b..f91a7caa5d0ca 100644
--- a/llvm/include/llvm/CodeGen/MachineCFGPrinter.h
+++ b/llvm/include/llvm/CodeGen/MachineCFGPrinter.h
@@ -12,6 +12,7 @@
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/Support/DOTGraphTraits.h"
 
 namespace llvm {
@@ -89,4 +90,15 @@ struct DOTGraphTraits<DOTMachineFuncInfo *> : public DefaultDOTGraphTraits {
            "' function";
   }
 };
+
+class MachineCFGPrinterPass : public PassInfoMixin<MachineCFGPrinterPass> {
+  raw_ostream &OS;
+
+public:
+  explicit MachineCFGPrinterPass(raw_ostream &OS) : OS(OS) {}
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+  static bool isRequired() { return true; }
+};
+
 } // namespace llvm
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 3e9e788662900..ccb3dbb76351f 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -139,6 +139,7 @@ MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
 #endif
 MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
 MACHINE_FUNCTION_PASS("detect-dead-lanes", DetectDeadLanesPass())
+MACHINE_FUNCTION_PASS("dot-machine-cfg", MachineCFGPrinterPass(errs()))
 MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass())
 MACHINE_FUNCTION_PASS("early-machinelicm", EarlyMachineLICMPass())
 MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass())
@@ -281,7 +282,6 @@ DUMMY_MACHINE_FUNCTION_PASS("break-false-deps", BreakFalseDepsPass)
 DUMMY_MACHINE_FUNCTION_PASS("cfguard-longjmp", CFGuardLongjmpPass)
 DUMMY_MACHINE_FUNCTION_PASS("cfi-fixup", CFIFixupPass)
 DUMMY_MACHINE_FUNCTION_PASS("cfi-instr-inserter", CFIInstrInserterPass)
-DUMMY_MACHINE_FUNCTION_PASS("dot-machine-cfg", MachineCFGPrinter)
 DUMMY_MACHINE_FUNCTION_PASS("fs-profile-loader", MIRProfileLoaderNewPass)
 DUMMY_MACHINE_FUNCTION_PASS("funclet-layout", FuncletLayoutPass)
 DUMMY_MACHINE_FUNCTION_PASS("gc-empty-basic-blocks", GCEmptyBasicBlocksPass)
diff --git a/llvm/lib/CodeGen/MachineCFGPrinter.cpp b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
index 7bfb817713808..ed6521bd41135 100644
--- a/llvm/lib/CodeGen/MachineCFGPrinter.cpp
+++ b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
@@ -54,6 +54,18 @@ static void writeMCFGToDotFile(MachineFunction &MF) {
   errs() << '\n';
 }
 
+PreservedAnalyses
+MachineCFGPrinterPass::run(MachineFunction &MF,
+                           MachineFunctionAnalysisManager &MFAM) {
+  if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
+    return PreservedAnalyses::all();
+  OS << "Writing Machine CFG for function ";
+  OS.write_escaped(MF.getName()) << '\n';
+
+  writeMCFGToDotFile(MF);
+  return PreservedAnalyses::all();
+}
+
 namespace {
 
 class MachineCFGPrinter : public MachineFunctionPass {
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5cda1517e127d..ddef41a66def8 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -117,6 +117,7 @@
 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
 #include "llvm/CodeGen/MachineBlockPlacement.h"
 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
+#include "llvm/CodeGen/MachineCFGPrinter.h"
 #include "llvm/CodeGen/MachineCSE.h"
 #include "llvm/CodeGen/MachineCopyPropagation.h"
 #include "llvm/CodeGen/MachineCycleAnalysis.h"
diff --git a/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir b/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir
index b34493aea8668..7550b25f9e357 100644
--- a/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir
+++ b/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir
@@ -1,5 +1,7 @@
 # RUN: llc -mtriple=amdgcn-- -run-pass=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -mcfg-func-name=func2 -o -  %s 2>&1 > /dev/null
 # RUN: FileCheck %s -input-file=%t.func2.dot --check-prefix=MCFG
+# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -mcfg-func-name=func2 -o -  %s 2>&1 > /dev/null
+# RUN: FileCheck %s -input-file=%t.func2.dot --check-prefix=MCFG
 
 # MCFG-NOT: digraph "Machine CFG for 'func1' function"
 name: func1
diff --git a/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir b/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir
index 56ea4b528ba8f..d15dfc5407c96 100644
--- a/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir
+++ b/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir
@@ -2,6 +2,10 @@
 # RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG
 # RUN: llc -mtriple=amdgcn-- -run-pass=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -dot-mcfg-only -o -  %s 2>&1 > /dev/null
 # RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG-ONLY
+# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -o -  %s 2>&1 > /dev/null
+# RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG
+# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -dot-mcfg-only -o -  %s 2>&1 > /dev/null
+# RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG-ONLY
 
 # MCFG: digraph "Machine CFG for 'irreducible' function"
 # MCFG-NEXT: label="Machine CFG for 'irreducible' function"

>From 499fefa1debe4e0d41d5b12a3580670eab3c5295 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 28 Apr 2025 16:03:14 +0800
Subject: [PATCH 2/4] handle unamed machine function

---
 llvm/lib/CodeGen/MachineCFGPrinter.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineCFGPrinter.cpp b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
index ed6521bd41135..aa4a2e3749b74 100644
--- a/llvm/lib/CodeGen/MachineCFGPrinter.cpp
+++ b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
@@ -60,8 +60,10 @@ MachineCFGPrinterPass::run(MachineFunction &MF,
   if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
     return PreservedAnalyses::all();
   OS << "Writing Machine CFG for function ";
-  OS.write_escaped(MF.getName()) << '\n';
-
+  StringRef Name = MF.getName();
+  if (Name.empty())
+    Name = "(unamed machine function)";
+  OS.write_escaped(Name) << '\n';
   writeMCFGToDotFile(MF);
   return PreservedAnalyses::all();
 }

>From 76a86cb8885dd3d837b46986675332069465cfeb Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 28 Apr 2025 16:03:30 +0800
Subject: [PATCH 3/4] use `-filetype=null`

---
 llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir   | 2 +-
 llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir b/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir
index 7550b25f9e357..970729f4cc50b 100644
--- a/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir
+++ b/llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir
@@ -1,6 +1,6 @@
 # RUN: llc -mtriple=amdgcn-- -run-pass=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -mcfg-func-name=func2 -o -  %s 2>&1 > /dev/null
 # RUN: FileCheck %s -input-file=%t.func2.dot --check-prefix=MCFG
-# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -mcfg-func-name=func2 -o -  %s 2>&1 > /dev/null
+# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -mcfg-func-name=func2 -filetype=null %s
 # RUN: FileCheck %s -input-file=%t.func2.dot --check-prefix=MCFG
 
 # MCFG-NOT: digraph "Machine CFG for 'func1' function"
diff --git a/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir b/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir
index d15dfc5407c96..3662789fb4499 100644
--- a/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir
+++ b/llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir
@@ -2,9 +2,9 @@
 # RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG
 # RUN: llc -mtriple=amdgcn-- -run-pass=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -dot-mcfg-only -o -  %s 2>&1 > /dev/null
 # RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG-ONLY
-# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -o -  %s 2>&1 > /dev/null
+# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -filetype=null %s
 # RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG
-# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -dot-mcfg-only -o -  %s 2>&1 > /dev/null
+# RUN: llc -mtriple=amdgcn-- -passes=dot-machine-cfg  -mcfg-dot-filename-prefix=%t -dot-mcfg-only -filetype=null %s
 # RUN: FileCheck %s -input-file=%t.irreducible.dot --check-prefix=MCFG-ONLY
 
 # MCFG: digraph "Machine CFG for 'irreducible' function"

>From a536b25b6b7a327e601974fbe4f57ad860ee50db Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 28 Apr 2025 16:31:51 +0800
Subject: [PATCH 4/4] use `printAsOperand`

---
 llvm/lib/CodeGen/MachineCFGPrinter.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineCFGPrinter.cpp b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
index aa4a2e3749b74..8e1c7ac705901 100644
--- a/llvm/lib/CodeGen/MachineCFGPrinter.cpp
+++ b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
@@ -59,11 +59,10 @@ MachineCFGPrinterPass::run(MachineFunction &MF,
                            MachineFunctionAnalysisManager &MFAM) {
   if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
     return PreservedAnalyses::all();
-  OS << "Writing Machine CFG for function ";
-  StringRef Name = MF.getName();
-  if (Name.empty())
-    Name = "(unamed machine function)";
-  OS.write_escaped(Name) << '\n';
+  std::string Name;
+  raw_string_ostream SS(Name);
+  MF.getFunction().printAsOperand(SS, /*PrintType=*/false);
+  OS << "Writing Machine CFG for function " << SS.str();
   writeMCFGToDotFile(MF);
   return PreservedAnalyses::all();
 }



More information about the llvm-commits mailing list