Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[CodeGen] Port MachineCFGPrinter to new pass manager #137570

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

paperchalice
Copy link
Contributor

No description provided.

@paperchalice paperchalice marked this pull request as ready for review April 28, 2025 04:04
@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-backend-amdgpu

Author: None (paperchalice)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/137570.diff

6 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/MachineCFGPrinter.h (+12)
  • (modified) llvm/include/llvm/Passes/MachinePassRegistry.def (+1-1)
  • (modified) llvm/lib/CodeGen/MachineCFGPrinter.cpp (+12)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/test/Analysis/DotMachineCFG/AMDGPU/functions.mir (+2)
  • (modified) llvm/test/Analysis/DotMachineCFG/AMDGPU/irreducible.mir (+4)
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"

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very roundabout way of discarding both stdout and stderr. Should probably check something is printed

if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
return PreservedAnalyses::all();
OS << "Writing Machine CFG for function ";
OS.write_escaped(MF.getName()) << '\n';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't handle anonymous functions correctly, but pre-existing issue

Comment on lines 64 to 65
if (Name.empty())
Name = "(unamed machine function)";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use MF.getFunction().printAsOperand instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found getNameOrAsOperand, but it is only available in debug build. It seems worthwhile to promote it to other build config, standard pass instrumentation could also use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that avoids the intermediate std::string here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants