Skip to content

Commit bb531c9

Browse files
authored
[NewPM/Codegen] Move MachineModuleInfo ownership outside of analysis (#80937)
With the legacy pass manager, MachineModuleInfoWrapperPass owned the MachineModuleInfo used in the codegen pipeline. It can do this since it's an ImmutablePass that doesn't get invalidated. However, with the new pass manager, it is legal for the ModuleAnalysisManager to clear all of its analyses, regardless of if the analysis does not want to be invalidated. So we must move ownership of the MachineModuleInfo outside of the analysis (this is similar to PassInstrumentation). For now, make the PassBuilder user register a MachineModuleAnalysis that returns a reference to a MachineModuleInfo that the user owns. Perhaps we can find a better place to own the MachineModuleInfo to make using the codegen pass manager less cumbersome in the future.
1 parent 65bf93d commit bb531c9

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,31 @@ class MachineModuleInfoWrapperPass : public ImmutablePass {
218218
const MachineModuleInfo &getMMI() const { return MMI; }
219219
};
220220

221-
/// An analysis that produces \c MachineInfo for a module.
221+
/// An analysis that produces \c MachineModuleInfo for a module.
222+
/// This does not produce its own MachineModuleInfo because we need a consistent
223+
/// MachineModuleInfo to keep ownership of MachineFunctions regardless of
224+
/// analysis invalidation/clearing. So something outside the analysis
225+
/// infrastructure must own the MachineModuleInfo.
222226
class MachineModuleAnalysis : public AnalysisInfoMixin<MachineModuleAnalysis> {
223227
friend AnalysisInfoMixin<MachineModuleAnalysis>;
224228
static AnalysisKey Key;
225229

226-
const LLVMTargetMachine *TM;
230+
MachineModuleInfo &MMI;
227231

228232
public:
229-
/// Provide the result type for this analysis pass.
230-
using Result = MachineModuleInfo;
233+
class Result {
234+
MachineModuleInfo &MMI;
235+
Result(MachineModuleInfo &MMI) : MMI(MMI) {}
236+
friend class MachineModuleAnalysis;
231237

232-
MachineModuleAnalysis(const LLVMTargetMachine *TM) : TM(TM) {}
238+
public:
239+
MachineModuleInfo &getMMI() { return MMI; }
240+
};
241+
242+
MachineModuleAnalysis(MachineModuleInfo &MMI) : MMI(MMI) {}
233243

234244
/// Run the analysis pass and produce machine module information.
235-
MachineModuleInfo run(Module &M, ModuleAnalysisManager &);
245+
Result run(Module &M, ModuleAnalysisManager &);
236246
};
237247

238248
} // end namespace llvm

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,10 @@ bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
237237

238238
AnalysisKey MachineModuleAnalysis::Key;
239239

240-
MachineModuleInfo MachineModuleAnalysis::run(Module &M,
241-
ModuleAnalysisManager &) {
242-
MachineModuleInfo MMI(TM);
240+
MachineModuleAnalysis::Result
241+
MachineModuleAnalysis::run(Module &M, ModuleAnalysisManager &) {
243242
MMI.TheModule = &M;
244-
MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
245-
!M.debug_compile_units().empty();
246-
return MMI;
243+
MMI.DbgInfoAvailable =
244+
!DisableDebugInfoPrinting && !M.debug_compile_units().empty();
245+
return Result(MMI);
247246
}

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Error MachineFunctionPassManager::run(Module &M,
2929
// because we don't run any module pass in codegen pipeline. This is very
3030
// important because the codegen state is stored in MMI which is the analysis
3131
// result of MachineModuleAnalysis. MMI should not be recomputed.
32-
auto &MMI = MFAM.getResult<MachineModuleAnalysis>(M);
32+
auto &MMI = MFAM.getResult<MachineModuleAnalysis>(M).getMMI();
3333

3434
(void)RequireCodeGenSCCOrder;
3535
assert(!RequireCodeGenSCCOrder && "not implemented");

llvm/tools/llc/NewPMDriver.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ int llvm::compileModuleWithNewPM(
147147
Opt.DebugPM = DebugPM;
148148
Opt.RegAlloc = RegAlloc;
149149

150+
MachineModuleInfo MMI(&LLVMTM);
151+
150152
PassInstrumentationCallbacks PIC;
151153
StandardInstrumentations SI(Context, Opt.DebugPM);
152154
SI.registerCallbacks(PIC);
@@ -164,7 +166,7 @@ int llvm::compileModuleWithNewPM(
164166
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
165167

166168
FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
167-
MAM.registerPass([&] { return MachineModuleAnalysis(&LLVMTM); });
169+
MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
168170

169171
MachineFunctionAnalysisManager MFAM(FAM, MAM);
170172

@@ -185,7 +187,7 @@ int llvm::compileModuleWithNewPM(
185187
MFPM.addPass(PrintMIRPass(*OS));
186188
MFPM.addPass(FreeMachineFunctionPass());
187189

188-
auto &MMI = MFAM.getResult<MachineModuleAnalysis>(*M);
190+
auto &MMI = MFAM.getResult<MachineModuleAnalysis>(*M).getMMI();
189191
if (MIR->parseMachineFunctions(*M, MMI))
190192
return 1;
191193

llvm/unittests/CodeGen/PassManagerTest.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ struct TestMachineFunctionPass : public PassInfoMixin<TestMachineFunctionPass> {
118118

119119
// Query module analysis result.
120120
MachineModuleInfo &MMI =
121-
MFAM.getResult<MachineModuleAnalysis>(*MF.getFunction().getParent());
121+
MFAM.getResult<MachineModuleAnalysis>(*MF.getFunction().getParent())
122+
.getMMI();
122123
// 1 + 1 + 1 = 3
123124
Count += (MMI.getModule() == MF.getFunction().getParent());
124125

@@ -144,7 +145,7 @@ struct TestMachineModulePass : public PassInfoMixin<TestMachineModulePass> {
144145
: Count(Count), MachineModulePassCount(MachineModulePassCount) {}
145146

146147
Error run(Module &M, MachineFunctionAnalysisManager &MFAM) {
147-
MachineModuleInfo &MMI = MFAM.getResult<MachineModuleAnalysis>(M);
148+
MachineModuleInfo &MMI = MFAM.getResult<MachineModuleAnalysis>(M).getMMI();
148149
// + 1
149150
Count += (MMI.getModule() == &M);
150151
MachineModulePassCount.push_back(Count);
@@ -209,6 +210,7 @@ TEST_F(PassManagerTest, Basic) {
209210
LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get());
210211
M->setDataLayout(TM->createDataLayout());
211212

213+
MachineModuleInfo MMI(LLVMTM);
212214
LoopAnalysisManager LAM;
213215
FunctionAnalysisManager FAM;
214216
CGSCCAnalysisManager CGAM;
@@ -220,7 +222,7 @@ TEST_F(PassManagerTest, Basic) {
220222

221223
FAM.registerPass([&] { return TestFunctionAnalysis(); });
222224
FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
223-
MAM.registerPass([&] { return MachineModuleAnalysis(LLVMTM); });
225+
MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
224226
MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
225227

226228
MachineFunctionAnalysisManager MFAM;

llvm/unittests/MIR/PassBuilderCallbacksTest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class MachineFunctionCallbacksTest : public testing::Test {
298298
}
299299

300300
std::unique_ptr<LLVMTargetMachine> TM;
301+
std::unique_ptr<MachineModuleInfo> MMI;
301302

302303
LLVMContext Context;
303304
std::unique_ptr<Module> M;
@@ -355,9 +356,9 @@ class MachineFunctionCallbacksTest : public testing::Test {
355356
TripleName, "", "", TargetOptions(), std::nullopt)));
356357
if (!TM)
357358
GTEST_SKIP();
358-
MachineModuleInfo MMI(TM.get());
359-
M = parseMIR(*TM, MIRString, MMI);
360-
AM.registerPass([&] { return MachineModuleAnalysis(TM.get()); });
359+
MMI = std::make_unique<MachineModuleInfo>(TM.get());
360+
M = parseMIR(*TM, MIRString, *MMI);
361+
AM.registerPass([&] { return MachineModuleAnalysis(*MMI); });
361362
}
362363

363364
MachineFunctionCallbacksTest()

0 commit comments

Comments
 (0)