[llvm] [CodeGen] Port PrintMIR to new pass manager (PR #79440)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 31 20:00:33 PST 2024
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/79440
>From 6ae4866d0204aed07e84e63f4a27d1c02b5f3353 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Thu, 25 Jan 2024 19:57:12 +0800
Subject: [PATCH 1/2] [CodeGen] Port PrintMIR to new pass manager
---
llvm/include/llvm/CodeGen/MIRPrinter.h | 19 +++++++++++++++++++
.../llvm/Passes/MachinePassRegistry.def | 3 +--
llvm/lib/CodeGen/MIRPrintingPass.cpp | 11 +++++++++++
llvm/lib/Passes/PassBuilder.cpp | 3 ++-
llvm/tools/llc/NewPMDriver.cpp | 12 ++++++------
5 files changed, 39 insertions(+), 9 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/MIRPrinter.h b/llvm/include/llvm/CodeGen/MIRPrinter.h
index 5e94418d5fe06..c0bd884b62d10 100644
--- a/llvm/include/llvm/CodeGen/MIRPrinter.h
+++ b/llvm/include/llvm/CodeGen/MIRPrinter.h
@@ -14,6 +14,8 @@
#ifndef LLVM_CODEGEN_MIRPRINTER_H
#define LLVM_CODEGEN_MIRPRINTER_H
+#include "llvm/CodeGen/MachinePassManager.h"
+
namespace llvm {
class MachineBasicBlock;
@@ -22,6 +24,23 @@ class Module;
class raw_ostream;
template <typename T> class SmallVectorImpl;
+class PrintMIRPreparePass : public MachinePassInfoMixin<PrintMIRPreparePass> {
+ raw_ostream &OS;
+
+public:
+ PrintMIRPreparePass(raw_ostream &OS) : OS(OS) {}
+ PreservedAnalyses run(Module &M, ModuleAnalysisManager &MFAM);
+};
+
+class PrintMIRPass : public MachinePassInfoMixin<PrintMIRPass> {
+ raw_ostream &OS;
+
+public:
+ PrintMIRPass(raw_ostream &OS) : OS(OS) {}
+ PreservedAnalyses run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &MFAM);
+};
+
/// Print LLVM IR using the MIR serialization format to the given output stream.
void printMIR(raw_ostream &OS, const Module &M);
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 9df3bae107b17..d6071d9961bbd 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -126,8 +126,8 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis,
#define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)
#endif
// MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass, ())
-// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass, ())
+MACHINE_FUNCTION_PASS("print", PrintMIRPass, (dbgs()))
#undef MACHINE_FUNCTION_PASS
// After a pass is converted to new pass manager, its entry should be moved from
@@ -205,7 +205,6 @@ DUMMY_MACHINE_FUNCTION_PASS("machineinstr-printer", MachineFunctionPrinterPass,
(OS, Banner))
DUMMY_MACHINE_FUNCTION_PASS("machinelicm", MachineLICMPass, ())
DUMMY_MACHINE_FUNCTION_PASS("machineverifier", MachineVerifierPass, (Banner))
-DUMMY_MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
DUMMY_MACHINE_FUNCTION_PASS("mirfs-discriminators", MIRAddFSDiscriminatorsPass,
(P))
DUMMY_MACHINE_FUNCTION_PASS("opt-phis", OptimizePHIsPass, ())
diff --git a/llvm/lib/CodeGen/MIRPrintingPass.cpp b/llvm/lib/CodeGen/MIRPrintingPass.cpp
index 1b5a9ade0871e..19566597e8c9a 100644
--- a/llvm/lib/CodeGen/MIRPrintingPass.cpp
+++ b/llvm/lib/CodeGen/MIRPrintingPass.cpp
@@ -20,6 +20,17 @@
using namespace llvm;
+PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {
+ printMIR(OS, M);
+ return PreservedAnalyses::all();
+}
+
+PreservedAnalyses PrintMIRPass::run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &) {
+ printMIR(OS, MF);
+ return PreservedAnalyses::all();
+}
+
namespace {
/// This pass prints out the LLVM IR to an output stream using the MIR
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index c934ec42f6eb1..2a6f3e4d0b12a 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -89,6 +89,7 @@
#include "llvm/CodeGen/InterleavedLoadCombine.h"
#include "llvm/CodeGen/JMCInstrumenter.h"
#include "llvm/CodeGen/LowerEmuTLS.h"
+#include "llvm/CodeGen/MIRPrinter.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SelectOptimize.h"
#include "llvm/CodeGen/ShadowStackGCLowering.h"
@@ -1866,7 +1867,7 @@ Error PassBuilder::parseMachinePass(MachineFunctionPassManager &MFPM,
}
#define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
if (Name == NAME) { \
- MFPM.addPass(PASS_NAME()); \
+ MFPM.addPass(PASS_NAME CONSTRUCTOR); \
return Error::success(); \
}
#include "llvm/Passes/MachinePassRegistry.def"
diff --git a/llvm/tools/llc/NewPMDriver.cpp b/llvm/tools/llc/NewPMDriver.cpp
index 8b1c263391b9e..17ce77d6034de 100644
--- a/llvm/tools/llc/NewPMDriver.cpp
+++ b/llvm/tools/llc/NewPMDriver.cpp
@@ -168,6 +168,9 @@ int llvm::compileModuleWithNewPM(
MachineFunctionAnalysisManager MFAM(FAM, MAM);
+ ModulePassManager MPM;
+ MachineFunctionPassManager MFPM;
+
if (!PassPipeline.empty()) {
// Construct a custom pass pipeline that starts after instruction
// selection.
@@ -177,8 +180,8 @@ int llvm::compileModuleWithNewPM(
return 1;
}
- MachineFunctionPassManager MFPM;
ExitOnErr(PB.parsePassPipeline(MFPM, PassPipeline));
+ MPM.addPass(PrintMIRPreparePass(*OS));
MFPM.addPass(PrintMIRPass(*OS));
MFPM.addPass(FreeMachineFunctionPass());
@@ -186,12 +189,9 @@ int llvm::compileModuleWithNewPM(
if (MIR->parseMachineFunctions(*M, MMI))
return 1;
- RunPasses(BOS.get(), Out.get(), M.get(), Context, Buffer, nullptr, nullptr,
- MFPM, MFAM);
+ RunPasses(BOS.get(), Out.get(), M.get(), Context, Buffer, &MPM, &MAM, MFPM,
+ MFAM);
} else {
- ModulePassManager MPM;
- MachineFunctionPassManager MFPM;
-
ExitOnErr(LLVMTM.buildCodeGenPipeline(MPM, MFPM, MFAM, *OS,
DwoOut ? &DwoOut->os() : nullptr,
FileType, Opt, &PIC));
>From 4346fad9945d8f3ba12aeb19608f04100cde6b2d Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Thu, 1 Feb 2024 11:59:01 +0800
Subject: [PATCH 2/2] Address comments
---
llvm/include/llvm/CodeGen/MIRPrinter.h | 4 ++--
llvm/lib/CodeGen/MIRPrintingPass.cpp | 2 --
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/MIRPrinter.h b/llvm/include/llvm/CodeGen/MIRPrinter.h
index c0bd884b62d10..9b49f36f137aa 100644
--- a/llvm/include/llvm/CodeGen/MIRPrinter.h
+++ b/llvm/include/llvm/CodeGen/MIRPrinter.h
@@ -15,20 +15,20 @@
#define LLVM_CODEGEN_MIRPRINTER_H
#include "llvm/CodeGen/MachinePassManager.h"
+#include "llvm/Support/Debug.h"
namespace llvm {
class MachineBasicBlock;
class MachineFunction;
class Module;
-class raw_ostream;
template <typename T> class SmallVectorImpl;
class PrintMIRPreparePass : public MachinePassInfoMixin<PrintMIRPreparePass> {
raw_ostream &OS;
public:
- PrintMIRPreparePass(raw_ostream &OS) : OS(OS) {}
+ PrintMIRPreparePass(raw_ostream &OS = dbgs()) : OS(OS) {}
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MFAM);
};
diff --git a/llvm/lib/CodeGen/MIRPrintingPass.cpp b/llvm/lib/CodeGen/MIRPrintingPass.cpp
index 19566597e8c9a..0aed1297cd3ea 100644
--- a/llvm/lib/CodeGen/MIRPrintingPass.cpp
+++ b/llvm/lib/CodeGen/MIRPrintingPass.cpp
@@ -15,8 +15,6 @@
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/InitializePasses.h"
-#include "llvm/Support/Debug.h"
-#include "llvm/Support/raw_ostream.h"
using namespace llvm;
More information about the llvm-commits
mailing list