Skip to content

Commit d4ba5e1

Browse files
author
Hendrik Greving
committed
Add MachineModuleInfo constructor with external MCContext
Adds a constructor to MachineModuleInfo and MachineModuleInfoWapperPass that takes an external MCContext. If provided, the external context will be used throughout codegen instead of MMI's default one. This enables external drivers to take ownership of data put on the MMI's context during codegen. The internal context is used otherwise and destroyed upon finish. Differential Revision: https://reviews.llvm.org/D91313
1 parent 173bb3c commit d4ba5e1

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class MachineModuleInfo {
8383

8484
/// This is the MCContext used for the entire code generator.
8585
MCContext Context;
86+
// This is an external context, that if assigned, will be used instead of the
87+
// internal context.
88+
MCContext *ExternalContext = nullptr;
8689

8790
/// This is the LLVM Module being worked on.
8891
const Module *TheModule;
@@ -149,6 +152,9 @@ class MachineModuleInfo {
149152
public:
150153
explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
151154

155+
explicit MachineModuleInfo(const LLVMTargetMachine *TM,
156+
MCContext *ExtContext);
157+
152158
MachineModuleInfo(MachineModuleInfo &&MMII);
153159

154160
~MachineModuleInfo();
@@ -158,8 +164,12 @@ class MachineModuleInfo {
158164

159165
const LLVMTargetMachine &getTarget() const { return TM; }
160166

161-
const MCContext &getContext() const { return Context; }
162-
MCContext &getContext() { return Context; }
167+
const MCContext &getContext() const {
168+
return ExternalContext ? *ExternalContext : Context;
169+
}
170+
MCContext &getContext() {
171+
return ExternalContext ? *ExternalContext : Context;
172+
}
163173

164174
const Module *getModule() const { return TheModule; }
165175

@@ -266,6 +276,9 @@ class MachineModuleInfoWrapperPass : public ImmutablePass {
266276
static char ID; // Pass identification, replacement for typeid
267277
explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr);
268278

279+
explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
280+
MCContext *ExtContext);
281+
269282
// Initialization and Finalization
270283
bool doInitialization(Module &) override;
271284
bool doFinalization(Module &) override;

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ void MachineModuleInfo::finalize() {
170170
AddrLabelSymbols = nullptr;
171171

172172
Context.reset();
173+
// We don't clear the ExternalContext.
173174

174175
delete ObjFileMMI;
175176
ObjFileMMI = nullptr;
@@ -187,6 +188,7 @@ MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
187188
HasSplitStack = MMI.HasSplitStack;
188189
HasNosplitStack = MMI.HasNosplitStack;
189190
AddrLabelSymbols = MMI.AddrLabelSymbols;
191+
ExternalContext = MMI.ExternalContext;
190192
TheModule = MMI.TheModule;
191193
}
192194

@@ -196,6 +198,14 @@ MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
196198
initialize();
197199
}
198200

201+
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
202+
MCContext *ExtContext)
203+
: TM(*TM), Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
204+
TM->getObjFileLowering(), nullptr, nullptr, false),
205+
ExternalContext(ExtContext) {
206+
initialize();
207+
}
208+
199209
MachineModuleInfo::~MachineModuleInfo() { finalize(); }
200210

201211
//===- Address of Block Management ----------------------------------------===//
@@ -204,7 +214,7 @@ ArrayRef<MCSymbol *>
204214
MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
205215
// Lazily create AddrLabelSymbols.
206216
if (!AddrLabelSymbols)
207-
AddrLabelSymbols = new MMIAddrLabelMap(Context);
217+
AddrLabelSymbols = new MMIAddrLabelMap(getContext());
208218
return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
209219
}
210220

@@ -296,6 +306,12 @@ MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
296306
initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
297307
}
298308

309+
MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
310+
const LLVMTargetMachine *TM, MCContext *ExtContext)
311+
: ImmutablePass(ID), MMI(TM, ExtContext) {
312+
initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
313+
}
314+
299315
// Handle the Pass registration stuff necessary to use DataLayout's.
300316
INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
301317
"Machine Module Information", false, false)

0 commit comments

Comments
 (0)