Skip to content

Commit 8715ed6

Browse files
committed
Created owning and non-owning variants of MMIWP.
1 parent 1a29779 commit 8715ed6

File tree

14 files changed

+118
-91
lines changed

14 files changed

+118
-91
lines changed

llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,24 @@ class CodeGenTargetMachineImpl : public TargetMachine {
4343
/// for generating a pipeline of CodeGen passes.
4444
virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
4545

46-
/// Creates a new \c MachineModuleInfo from this \c TargetMachine
47-
std::unique_ptr<MachineModuleInfo> createMachineModuleInfo() const override;
48-
4946
/// Add passes to the specified pass manager to get the specified file
5047
/// emitted. Typically, this will involve several steps of code generation.
5148
/// This method should return true if emission of this file type is not
5249
/// supported, or false on success.
53-
/// \p MMI a \c MachineModuleInfo that holds the generated machine code
54-
/// throughout the code generation process
50+
/// \p MMI an optional, externally-managed \c MachineModuleInfo to
51+
/// hold the generated machine code even after the \p PM is destroyed
5552
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
5653
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
57-
MachineModuleInfo *MMI,
58-
bool DisableVerify = true) override;
54+
bool DisableVerify = true,
55+
MachineModuleInfo *MMI = nullptr) override;
5956

6057
/// Add passes to the specified pass manager to get machine code emitted with
6158
/// the MCJIT. This method returns true if machine code is not supported.
62-
/// \p MMI a \c MachineModuleInfo that holds the generated machine code
63-
/// throughout the code generation process
64-
bool addPassesToEmitMC(PassManagerBase &PM,
65-
raw_pwrite_stream &Out,
66-
MachineModuleInfo *MMI,
67-
bool DisableVerify = true) override;
59+
/// \p MMI an optional, externally-managed \c MachineModuleInfo to
60+
/// hold the generated machine code even after the \p PM is destroyed
61+
bool addPassesToEmitMC(PassManagerBase &PM, raw_pwrite_stream &Out,
62+
bool DisableVerify = true,
63+
MachineModuleInfo *MMI = nullptr) override;
6864

6965
/// Adds an AsmPrinter pass to the pipeline that prints assembly or
7066
/// machine code from the MI representation.

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,51 @@ class MachineModuleInfo {
163163
/// \}
164164
}; // End class MachineModuleInfo
165165

166+
/// \brief Interface for pass that provide access to \c MachineModuleInfo
167+
/// being worked on
166168
class MachineModuleInfoWrapperPass : public ImmutablePass {
167-
MachineModuleInfo &MMI;
168169

169170
public:
170171
static char ID; // Pass identification, replacement for typeid
171-
explicit MachineModuleInfoWrapperPass(MachineModuleInfo *MMI = nullptr);
172+
MachineModuleInfoWrapperPass();
172173

173174
// Initialization and Finalization
174175
bool doInitialization(Module &) override;
175176
bool doFinalization(Module &) override;
176177

177-
MachineModuleInfo &getMMI() { return MMI; }
178-
const MachineModuleInfo &getMMI() const { return MMI; }
178+
virtual MachineModuleInfo &getMMI() = 0;
179+
virtual const MachineModuleInfo &getMMI() const = 0;
180+
};
181+
182+
/// \brief a version of \c MachineModuleInfoWrapperPass that manages the
183+
/// lifetime of its \c MachineModuleInfo
184+
class OwningMachineModuleInfoWrapperPass : public MachineModuleInfoWrapperPass {
185+
MachineModuleInfo MMI;
186+
187+
public:
188+
explicit OwningMachineModuleInfoWrapperPass(const TargetMachine &TM)
189+
: MMI(&TM) {};
190+
191+
OwningMachineModuleInfoWrapperPass(const TargetMachine &TM,
192+
MCContext &ExtContext)
193+
: MMI(&TM, &ExtContext) {};
194+
195+
MachineModuleInfo &getMMI() override { return MMI; }
196+
const MachineModuleInfo &getMMI() const override { return MMI; }
197+
};
198+
199+
/// \brief a version of \c MachineModuleInfoWrapperPass that does not manage the
200+
/// lifetime of its \c MachineModuleInfo
201+
class NonOwningMachineModuleInfoWrapperPass
202+
: public MachineModuleInfoWrapperPass {
203+
MachineModuleInfo &MMI;
204+
205+
public:
206+
explicit NonOwningMachineModuleInfoWrapperPass(MachineModuleInfo &MMI)
207+
: MMI(MMI) {};
208+
209+
MachineModuleInfo &getMMI() override { return MMI; }
210+
const MachineModuleInfo &getMMI() const override { return MMI; }
179211
};
180212

181213
/// An analysis that produces \c MachineModuleInfo for a module.

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -400,38 +400,30 @@ class TargetMachine {
400400
/// with the new pass manager. Only affects the "default" AAManager.
401401
virtual void registerDefaultAliasAnalyses(AAManager &) {}
402402

403-
/// For targets that utilize the target-independent code generator
404-
/// (CodeGen), this method creates a new \c MachineModuleInfo from this
405-
/// \c TargetMachine and returns it; For targets that don't use CodeGen,
406-
/// returns nullptr
407-
virtual std::unique_ptr<MachineModuleInfo> createMachineModuleInfo() const {
408-
return nullptr;
409-
}
410-
411403
/// Add passes to the specified pass manager to get the specified file
412404
/// emitted. Typically, this will involve several steps of code generation.
413405
/// This method should return true if emission of this file type is not
414406
/// supported, or false on success.
415407
/// For targets that utilize the target-independent code generator
416-
/// (CodeGen) to emit the file, \c MachineModuleInfo pointer will hold the
417-
/// generated machine code; For targets that don't use CodeGen, it is set
418-
/// to \c nullptr
408+
/// (CodeGen), an externally-managed \c MachineModuleInfo can be provided
409+
/// to ensure persistence of the generated machine code even after the pass
410+
/// manager is destroyed
419411
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
420412
raw_pwrite_stream *, CodeGenFileType,
421-
MachineModuleInfo *,
422-
bool /*DisableVerify*/ = true) {
413+
bool /*DisableVerify*/ = true,
414+
MachineModuleInfo * = nullptr) {
423415
return true;
424416
}
425417

426418
/// Add passes to the specified pass manager to get machine code emitted with
427419
/// the MCJIT. This method returns true if machine code is not supported.
428420
/// For targets that utilize the target-independent code generator
429-
/// (CodeGen), \c MachineModuleInfo pointer will hold the generated
430-
/// machine code; For targets that don't use CodeGen, it must be set
431-
/// to \c nullptr
421+
/// (CodeGen), an externally-managed \c MachineModuleInfo can be provided
422+
/// to ensure persistence of the generated machine code even after the pass
423+
/// manager is destroyed
432424
virtual bool addPassesToEmitMC(PassManagerBase &, raw_pwrite_stream &,
433-
MachineModuleInfo *,
434-
bool /*DisableVerify*/ = true) {
425+
bool /*DisableVerify*/ = true,
426+
MachineModuleInfo * = nullptr) {
435427
return true;
436428
}
437429

llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,22 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
208208
return std::move(AsmStreamer);
209209
}
210210

211-
std::unique_ptr<MachineModuleInfo>
212-
CodeGenTargetMachineImpl::createMachineModuleInfo() const override {
213-
return std::make_unique<MachineModuleInfo>(this);
214-
}
215-
216211
bool CodeGenTargetMachineImpl::addPassesToEmitFile(
217212
PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
218-
CodeGenFileType FileType, MachineModuleInfo *MMI, bool DisableVerify) {
219-
// Check if MMI is nullptr, or if MMI pointer is valid but the target
220-
// machine of the MMI is not the same as this target machine
221-
if (!MMI || &MMI->getTarget() != this)
222-
return true;
213+
CodeGenFileType FileType, bool DisableVerify, MachineModuleInfo *MMI) {
223214
// Add the wrapper pass for MMI
224-
auto *MMIWP = new MachineModuleInfoWrapperPass(MMI);
215+
MachineModuleInfoWrapperPass *MMIWP;
216+
if (MMI) {
217+
MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI);
218+
} else {
219+
MMIWP = new OwningMachineModuleInfoWrapperPass(*this);
220+
MMI = &MMIWP->getMMI();
221+
}
222+
// Check (for the case of externally-managed MMI) if the TM of MMI is
223+
// the same as this target machine
224+
if (&MMI->getTarget() != this)
225+
return true;
226+
225227
TargetPassConfig *PassConfig =
226228
addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
227229
if (!PassConfig)
@@ -240,21 +242,22 @@ bool CodeGenTargetMachineImpl::addPassesToEmitFile(
240242
return false;
241243
}
242244

243-
/// addPassesToEmitMC - Add passes to the specified pass manager to get
244-
/// machine code emitted with the MCJIT. This method returns true if machine
245-
/// code is not supported. It fills the MCContext Ctx pointer which can be
246-
/// used to build custom MCStreamer.
247-
///
248245
bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
249246
raw_pwrite_stream &Out,
250-
MachineModuleInfo *MMI,
251-
bool DisableVerify) {
252-
// Check if MMI is nullptr, or if MMI pointer is valid but the target
253-
// machine of the MMI is not the same as this target machine
254-
if (!MMI || &MMI->getTarget() != this)
247+
bool DisableVerify,
248+
MachineModuleInfo *MMI) {
249+
// Add the wrapper pass for MMI
250+
MachineModuleInfoWrapperPass *MMIWP;
251+
if (MMI) {
252+
MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI);
253+
} else {
254+
MMIWP = new OwningMachineModuleInfoWrapperPass(*this);
255+
MMI = &MMIWP->getMMI();
256+
}
257+
// Check (for the case of externally-managed MMI) if the TM of MMI is
258+
// the same as this target machine
259+
if (&MMI->getTarget() != this)
255260
return true;
256-
// Add common CodeGen passes.
257-
MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(MMI);
258261
TargetPassConfig *PassConfig =
259262
addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
260263
if (!PassConfig)

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
139139
return new FreeMachineFunction();
140140
}
141141

142-
MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
143-
MachineModuleInfo *MMI)
144-
: ImmutablePass(ID), MMI([&] -> MachineModuleInfo & {
145-
assert(MMI != nullptr, "MMI is nullptr");
146-
return *MMI;
147-
}()) {
142+
MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass()
143+
: ImmutablePass(ID) {
148144
initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
149145
}
150146

@@ -179,6 +175,7 @@ static uint64_t getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
179175
}
180176

181177
bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
178+
MachineModuleInfo &MMI = getMMI();
182179
MMI.initialize();
183180
MMI.TheModule = &M;
184181
LLVMContext &Ctx = M.getContext();
@@ -196,7 +193,7 @@ bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
196193
}
197194

198195
bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
199-
MMI.finalize();
196+
getMMI().finalize();
200197
return false;
201198
}
202199

llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
6666
std::shared_ptr<MCJITMemoryManager> MemMgr,
6767
std::shared_ptr<LegacyJITSymbolResolver> Resolver)
6868
: ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
69-
Ctx(nullptr), MemMgr(std::move(MemMgr)),
70-
Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
71-
ObjCache(nullptr) {
69+
MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)),
70+
Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) {
7271
// FIXME: We are managing our modules, so we do not want the base class
7372
// ExecutionEngine to manage them as well. To avoid double destruction
7473
// of the first (and only) module added in ExecutionEngine constructor
@@ -163,7 +162,7 @@ std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
163162

164163
// Turn the machine code intermediate representation into bytes in memory
165164
// that may be executed.
166-
if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
165+
if (TM->addPassesToEmitMC(PM, ObjStream, !getVerifyModules()))
167166
report_fatal_error("Target does not support MC emission!");
168167

169168
// Initialize passes.

llvm/lib/ExecutionEngine/MCJIT/MCJIT.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class MCJIT : public ExecutionEngine {
168168
};
169169

170170
std::unique_ptr<TargetMachine> TM;
171-
MCContext *Ctx;
172171
std::shared_ptr<MCJITMemoryManager> MemMgr;
173172
LinkingSymbolResolver Resolver;
174173
RuntimeDyld Dyld;

llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) {
4646
raw_svector_ostream ObjStream(ObjBufferSV);
4747

4848
legacy::PassManager PM;
49-
MCContext *Ctx;
50-
if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
49+
if (TM.addPassesToEmitMC(PM, ObjStream))
5150
return make_error<StringError>("Target does not support MC emission",
5251
inconvertibleErrorCode());
5352
PM.run(M);

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,11 @@ static std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
331331
{
332332
raw_svector_ostream OS(OutputBuffer);
333333
legacy::PassManager PM;
334+
std::unique_ptr<MachineModuleInfo> MMI = TM.createMachineModuleInfo();
334335

335-
// Setup the codegen now.
336+
// Setup the codegen pipeline.
336337
if (TM.addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::ObjectFile,
338+
MMI.get(),
337339
/* DisableVerify */ true))
338340
report_fatal_error("Failed to setup codegen");
339341

llvm/lib/Target/DirectX/DirectXTargetMachine.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ void DirectXTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
133133

134134
bool DirectXTargetMachine::addPassesToEmitFile(
135135
PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
136-
CodeGenFileType FileType, bool DisableVerify,
137-
MachineModuleInfoWrapperPass *MMIWP) {
136+
CodeGenFileType FileType, bool DisableVerify, MachineModuleInfo *MMI) {
138137
TargetPassConfig *PassConfig = createPassConfig(PM);
139138
PassConfig->addCodeGenPrepare();
140139

@@ -150,8 +149,17 @@ bool DirectXTargetMachine::addPassesToEmitFile(
150149
// globals don't pollute the DXIL.
151150
PM.add(createDXContainerGlobalsPass());
152151

153-
if (!MMIWP)
154-
MMIWP = new MachineModuleInfoWrapperPass(this);
152+
MachineModuleInfoWrapperPass *MMIWP;
153+
if (MMI) {
154+
MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI);
155+
} else {
156+
MMIWP = new OwningMachineModuleInfoWrapperPass(*this);
157+
MMI = &MMIWP->getMMI();
158+
}
159+
// Check (for the case of externally-managed MMI) if the TM of MMI is
160+
// the same as this target machine
161+
if (&MMI->getTarget() != this)
162+
return true;
155163
PM.add(MMIWP);
156164
if (addAsmPrinter(PM, Out, DwoOut, FileType,
157165
MMIWP->getMMI().getContext()))
@@ -166,9 +174,9 @@ bool DirectXTargetMachine::addPassesToEmitFile(
166174
}
167175

168176
bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
169-
MCContext *&Ctx,
170177
raw_pwrite_stream &Out,
171-
bool DisableVerify) {
178+
bool DisableVerify,
179+
MachineModuleInfo *MMI) {
172180
return true;
173181
}
174182

0 commit comments

Comments
 (0)