[llvm] [NewPM][CodeGen] Let `ModuleToMachineFunctionPassAdaptor` own `MachineModuleInfo` (PR #87226)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 1 04:25:00 PDT 2024
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/87226
>From 090bc31b36358b4f29fa20c5331f17dd2bf364b7 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 1 Apr 2024 18:46:24 +0800
Subject: [PATCH] [NewPM] Let `ModuleToMachineFunctionPassAdaptor` own
`MachineModuleInfo` Thus `MachineFunction`s are no longer analysis results,
but still keep `MachineModuleAnalysis` so "machine module pass" can work.
---
.../include/llvm/CodeGen/MachinePassManager.h | 36 ++++++++++++++++---
llvm/include/llvm/Passes/CodeGenPassBuilder.h | 27 +++++++++++---
llvm/include/llvm/Passes/PassBuilder.h | 5 +++
llvm/lib/CodeGen/MachinePassManager.cpp | 2 +-
llvm/lib/Passes/PassBuilder.cpp | 16 +++++++--
llvm/lib/Passes/PassRegistry.def | 1 +
llvm/tools/llc/NewPMDriver.cpp | 6 ++--
llvm/unittests/CodeGen/PassManagerTest.cpp | 6 ++--
.../MIR/PassBuilderCallbacksTest.cpp | 6 ++--
9 files changed, 83 insertions(+), 22 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/MachinePassManager.h b/llvm/include/llvm/CodeGen/MachinePassManager.h
index 8689fd19030f90..ac24bac65f9143 100644
--- a/llvm/include/llvm/CodeGen/MachinePassManager.h
+++ b/llvm/include/llvm/CodeGen/MachinePassManager.h
@@ -24,6 +24,7 @@
#include "llvm/ADT/FunctionExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/PassManagerInternal.h"
#include "llvm/Support/Error.h"
@@ -217,30 +218,57 @@ class ModuleToMachineFunctionPassAdaptor
detail::PassConcept<MachineFunction, MachineFunctionAnalysisManager>;
explicit ModuleToMachineFunctionPassAdaptor(
- std::unique_ptr<PassConceptT> Pass)
- : Pass(std::move(Pass)) {}
+ std::unique_ptr<PassConceptT> Pass,
+ std::unique_ptr<MachineModuleInfo> MMI)
+ : Pass(std::move(Pass)), OwnedMMI(std::move(MMI)) {}
+
+ explicit ModuleToMachineFunctionPassAdaptor(
+ std::unique_ptr<PassConceptT> Pass, MachineModuleInfo &MMI)
+ : Pass(std::move(Pass)), MMIPtr(&MMI) {}
/// Runs the function pass across every function in the module.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
void printPipeline(raw_ostream &OS,
function_ref<StringRef(StringRef)> MapClassName2PassName);
+ MachineModuleInfo &getMachineModuleInfo() {
+ assert((OwnedMMI || MMIPtr) && "Need MachineModuleInfo!");
+ return OwnedMMI ? *OwnedMMI : *MMIPtr;
+ }
+
static bool isRequired() { return true; }
private:
std::unique_ptr<PassConceptT> Pass;
+ std::unique_ptr<MachineModuleInfo> OwnedMMI;
+ MachineModuleInfo *MMIPtr = nullptr;
};
+template <typename MachineFunctionPassT>
+ModuleToMachineFunctionPassAdaptor createModuleToMachineFunctionPassAdaptor(
+ MachineFunctionPassT &&Pass, std::unique_ptr<MachineModuleInfo> &&MMI) {
+ using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
+ MachineFunctionAnalysisManager>;
+ // Do not use make_unique, it causes too many template instantiations,
+ // causing terrible compile times.
+ return ModuleToMachineFunctionPassAdaptor(
+ std::unique_ptr<ModuleToMachineFunctionPassAdaptor::PassConceptT>(
+ new PassModelT(std::forward<MachineFunctionPassT>(Pass))),
+ std::move(MMI));
+}
+
template <typename MachineFunctionPassT>
ModuleToMachineFunctionPassAdaptor
-createModuleToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
+createModuleToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass,
+ MachineModuleInfo &MMI) {
using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
MachineFunctionAnalysisManager>;
// Do not use make_unique, it causes too many template instantiations,
// causing terrible compile times.
return ModuleToMachineFunctionPassAdaptor(
std::unique_ptr<ModuleToMachineFunctionPassAdaptor::PassConceptT>(
- new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
+ new PassModelT(std::forward<MachineFunctionPassT>(Pass))),
+ MMI);
}
template <>
diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index 00eb9b096a9356..d5a1a1e50065ee 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -114,7 +114,8 @@ template <typename DerivedT> class CodeGenPassBuilder {
explicit CodeGenPassBuilder(LLVMTargetMachine &TM,
const CGPassBuilderOption &Opts,
PassInstrumentationCallbacks *PIC)
- : TM(TM), Opt(Opts), PIC(PIC) {
+ : TM(TM), Opt(Opts), PIC(PIC), MMI(new MachineModuleInfo(&TM)),
+ MMIRefPtr(MMI.get()) {
// Target could set CGPassBuilderOption::MISchedPostRA to true to achieve
// substitutePass(&PostRASchedulerID, &PostMachineSchedulerID)
@@ -205,8 +206,16 @@ template <typename DerivedT> class CodeGenPassBuilder {
AddMachinePass(ModulePassManager &MPM, const DerivedT &PB)
: MPM(MPM), PB(PB) {}
~AddMachinePass() {
- if (!MFPM.isEmpty())
- MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
+ if (!MFPM.isEmpty()) {
+ if (PB.MMI) {
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(
+ std::move(MFPM), std::move(PB.MMI)));
+ PB.MMI = nullptr;
+ } else {
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM),
+ *PB.MMIRefPtr));
+ }
+ }
}
template <typename PassT>
@@ -228,8 +237,14 @@ template <typename DerivedT> class CodeGenPassBuilder {
} else {
// Add Module Pass
if (!MFPM.isEmpty()) {
- MPM.addPass(
- createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
+ if (PB.MMI) {
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(
+ std::move(MFPM), std::move(PB.MMI)));
+ PB.MMI = nullptr;
+ } else {
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(
+ std::move(MFPM), *PB.MMIRefPtr));
+ }
MFPM = MachineFunctionPassManager();
}
@@ -249,6 +264,8 @@ template <typename DerivedT> class CodeGenPassBuilder {
LLVMTargetMachine &TM;
CGPassBuilderOption Opt;
PassInstrumentationCallbacks *PIC;
+ mutable std::unique_ptr<MachineModuleInfo> MMI;
+ mutable MachineModuleInfo *MMIRefPtr;
template <typename TMC> TMC &getTM() const { return static_cast<TMC &>(TM); }
CodeGenOptLevel getOptLevel() const { return TM.getOptLevel(); }
diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h
index d1232124d5d819..a6056424c5d986 100644
--- a/llvm/include/llvm/Passes/PassBuilder.h
+++ b/llvm/include/llvm/Passes/PassBuilder.h
@@ -33,6 +33,7 @@ class StringRef;
class AAManager;
class TargetMachine;
class ModuleSummaryIndex;
+class MachineModuleInfo;
template <typename T> class IntrusiveRefCntPtr;
namespace vfs {
class FileSystem;
@@ -106,6 +107,8 @@ class PassBuilder {
PipelineTuningOptions PTO;
std::optional<PGOOptions> PGOOpt;
PassInstrumentationCallbacks *PIC;
+ std::unique_ptr<MachineModuleInfo> MMI;
+ MachineModuleInfo *MMIRefPtr; // As reference to MMI
public:
/// A struct to capture parsed pass pipeline names.
@@ -626,6 +629,8 @@ class PassBuilder {
void invokePipelineEarlySimplificationEPCallbacks(ModulePassManager &MPM,
OptimizationLevel Level);
+ MachineModuleInfo &getMachineModuleInfo() { return *MMIRefPtr; }
+
static bool checkParametrizedPassName(StringRef Name, StringRef PassName) {
if (!Name.consume_front(PassName))
return false;
diff --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp
index 2763193b2c306b..21449dd6d86f97 100644
--- a/llvm/lib/CodeGen/MachinePassManager.cpp
+++ b/llvm/lib/CodeGen/MachinePassManager.cpp
@@ -71,7 +71,7 @@ bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
PreservedAnalyses
ModuleToMachineFunctionPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) {
- auto &MMI = AM.getResult<MachineModuleAnalysis>(M).getMMI();
+ auto &MMI = getMachineModuleInfo();
MachineFunctionAnalysisManager &MFAM =
AM.getResult<MachineFunctionAnalysisManagerModuleProxy>(M).getManager();
PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M);
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 57975e34d4265b..5c7751d10592e4 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -92,6 +92,7 @@
#include "llvm/CodeGen/JMCInstrumenter.h"
#include "llvm/CodeGen/LowerEmuTLS.h"
#include "llvm/CodeGen/MIRPrinter.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SelectOptimize.h"
@@ -397,7 +398,9 @@ class RequireAllMachineFunctionPropertiesPass
PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
std::optional<PGOOptions> PGOOpt,
PassInstrumentationCallbacks *PIC)
- : TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) {
+ : TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC),
+ MMI(new MachineModuleInfo(static_cast<LLVMTargetMachine *>(TM))),
+ MMIRefPtr(MMI.get()) {
bool ShouldPopulateClassToPassNames = PIC && shouldPopulateClassToPassNames();
if (TM)
TM->registerPassBuilderCallbacks(*this, ShouldPopulateClassToPassNames);
@@ -1412,7 +1415,16 @@ Error PassBuilder::parseModulePass(ModulePassManager &MPM,
MachineFunctionPassManager MFPM;
if (auto Err = parseMachinePassPipeline(MFPM, InnerPipeline))
return Err;
- MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
+
+ if (MMI) {
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM),
+ std::move(MMI)));
+ MMI = nullptr;
+ } else {
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM),
+ *MMIRefPtr));
+ }
+
return Error::success();
}
if (auto Params = parseFunctionPipelineName(Name)) {
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 41f16d0915bf23..a685893868fa4b 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -23,6 +23,7 @@ MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis())
MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
+MODULE_ANALYSIS("machine-module", MachineModuleAnalysis(*MMIRefPtr))
MODULE_ANALYSIS("module-summary", ModuleSummaryIndexAnalysis())
MODULE_ANALYSIS("no-op-module", NoOpModuleAnalysis())
MODULE_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
diff --git a/llvm/tools/llc/NewPMDriver.cpp b/llvm/tools/llc/NewPMDriver.cpp
index 6ae1b8db5e115c..985dfce7800c21 100644
--- a/llvm/tools/llc/NewPMDriver.cpp
+++ b/llvm/tools/llc/NewPMDriver.cpp
@@ -113,8 +113,6 @@ int llvm::compileModuleWithNewPM(
Opt.DebugPM = DebugPM;
Opt.RegAlloc = RegAlloc;
- MachineModuleInfo MMI(&LLVMTM);
-
PassInstrumentationCallbacks PIC;
StandardInstrumentations SI(Context, Opt.DebugPM);
SI.registerCallbacks(PIC);
@@ -134,9 +132,9 @@ int llvm::compileModuleWithNewPM(
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
- MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
ModulePassManager MPM;
+ auto &MMI = PB.getMachineModuleInfo();
if (!PassPipeline.empty()) {
// Construct a custom pass pipeline that starts after instruction
@@ -153,7 +151,7 @@ int llvm::compileModuleWithNewPM(
MachineFunctionPassManager MFPM;
MFPM.addPass(PrintMIRPass(*OS));
MFPM.addPass(FreeMachineFunctionPass());
- MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM), MMI));
if (MIR->parseMachineFunctions(*M, MMI))
return 1;
diff --git a/llvm/unittests/CodeGen/PassManagerTest.cpp b/llvm/unittests/CodeGen/PassManagerTest.cpp
index 4283eb01a9c8f2..41b03eb456d95e 100644
--- a/llvm/unittests/CodeGen/PassManagerTest.cpp
+++ b/llvm/unittests/CodeGen/PassManagerTest.cpp
@@ -189,7 +189,6 @@ TEST_F(PassManagerTest, Basic) {
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
FAM.registerPass([&] { return TestFunctionAnalysis(); });
- MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
MFAM.registerPass([&] { return TestMachineFunctionAnalysis(); });
int Count = 0;
@@ -197,12 +196,13 @@ TEST_F(PassManagerTest, Basic) {
ModulePassManager MPM;
MachineFunctionPassManager MFPM;
+ auto &MMI = PB.getMachineModuleInfo();
MPM.addPass(TestMachineModulePass(Count, Counts));
MPM.addPass(createModuleToMachineFunctionPassAdaptor(
- TestMachineFunctionPass(Count, Counts)));
+ TestMachineFunctionPass(Count, Counts), MMI));
MPM.addPass(TestMachineModulePass(Count, Counts));
MFPM.addPass(TestMachineFunctionPass(Count, Counts));
- MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM), MMI));
MPM.run(*M, MAM);
diff --git a/llvm/unittests/MIR/PassBuilderCallbacksTest.cpp b/llvm/unittests/MIR/PassBuilderCallbacksTest.cpp
index 8e3738dc919209..be37aa7802c2c3 100644
--- a/llvm/unittests/MIR/PassBuilderCallbacksTest.cpp
+++ b/llvm/unittests/MIR/PassBuilderCallbacksTest.cpp
@@ -528,8 +528,8 @@ TEST_F(MachineFunctionCallbacksTest, InstrumentedFreeMFPass) {
runAfterPass(HasNameRegex("FreeMachineFunctionPass"), _, _))
.Times(0);
- MPM.addPass(
- createModuleToMachineFunctionPassAdaptor(FreeMachineFunctionPass()));
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(
+ FreeMachineFunctionPass(), *MMI));
MPM.run(*M, MAM);
}
@@ -568,7 +568,7 @@ TEST_F(MachineFunctionCallbacksTest, InstrumentedFreeMFPass2) {
MachineFunctionPassManager MFPM;
MFPM.addPass(FreeMachineFunctionPass());
- MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
+ MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM), *MMI));
MPM.run(*M, MAM);
}
More information about the llvm-commits
mailing list