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

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 28 18:04:50 PDT 2025


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

>From d5691286eb77503a6fb586807e38e8969e86dacb 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/6] [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 8c22a28eba277..c0679913c9b8b 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -141,6 +141,7 @@ MACHINE_FUNCTION_PASS("block-placement-stats", MachineBlockPlacementStatsPass())
 MACHINE_FUNCTION_PASS("branch-relaxation", BranchRelaxationPass())
 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())
@@ -288,7 +289,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 e7057d9a6b625..a9e74c7acf518 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -118,6 +118,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 e5433e768a2d2b4502ecbd49e85ab2df1b8d0a80 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/6] 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 6afd1fad62e9b4514925215ed8295d928be9bc9a 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/6] 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 9e881d3a4e81ddd6bcb4d8527ca75fe898e20523 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/6] 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();
 }

>From 97f43c4f32e9067d50212d2bb3f6b1a0f80e606e Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 28 Apr 2025 16:35:06 +0800
Subject: [PATCH 5/6] use underlying string directly

---
 llvm/lib/CodeGen/MachineCFGPrinter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/MachineCFGPrinter.cpp b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
index 8e1c7ac705901..312c58fceb086 100644
--- a/llvm/lib/CodeGen/MachineCFGPrinter.cpp
+++ b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
@@ -62,7 +62,7 @@ MachineCFGPrinterPass::run(MachineFunction &MF,
   std::string Name;
   raw_string_ostream SS(Name);
   MF.getFunction().printAsOperand(SS, /*PrintType=*/false);
-  OS << "Writing Machine CFG for function " << SS.str();
+  OS << "Writing Machine CFG for function " << Name;
   writeMCFGToDotFile(MF);
   return PreservedAnalyses::all();
 }

>From 73f15dc92ff5349e37bc3a87121267e28bffb9e4 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Tue, 29 Apr 2025 09:04:33 +0800
Subject: [PATCH 6/6] use getNameOrAsOperand

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

diff --git a/llvm/lib/CodeGen/MachineCFGPrinter.cpp b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
index 312c58fceb086..4df41380f7509 100644
--- a/llvm/lib/CodeGen/MachineCFGPrinter.cpp
+++ b/llvm/lib/CodeGen/MachineCFGPrinter.cpp
@@ -59,10 +59,8 @@ MachineCFGPrinterPass::run(MachineFunction &MF,
                            MachineFunctionAnalysisManager &MFAM) {
   if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
     return PreservedAnalyses::all();
-  std::string Name;
-  raw_string_ostream SS(Name);
-  MF.getFunction().printAsOperand(SS, /*PrintType=*/false);
-  OS << "Writing Machine CFG for function " << Name;
+  OS << "Writing Machine CFG for function "
+     << MF.getFunction().getNameOrAsOperand();
   writeMCFGToDotFile(MF);
   return PreservedAnalyses::all();
 }



More information about the llvm-commits mailing list