Skip to content

Commit 1a29779

Browse files
committed
Changed TargetMachine and CodeGenTargetMachineImpl interfaces to use externally-initialized MMIs.
1 parent ed5c332 commit 1a29779

File tree

3 files changed

+65
-39
lines changed

3 files changed

+65
-39
lines changed

llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,27 @@ 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+
4649
/// Add passes to the specified pass manager to get the specified file
47-
/// emitted. Typically this will involve several steps of code generation.
48-
/// \p MMIWP is an optional parameter that, if set to non-nullptr,
49-
/// will be used to set the MachineModuloInfo for this PM.
50-
bool
51-
addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
52-
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
53-
bool DisableVerify = true,
54-
MachineModuleInfoWrapperPass *MMIWP = nullptr) override;
50+
/// emitted. Typically, this will involve several steps of code generation.
51+
/// This method should return true if emission of this file type is not
52+
/// supported, or false on success.
53+
/// \p MMI a \c MachineModuleInfo that holds the generated machine code
54+
/// throughout the code generation process
55+
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
56+
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
57+
MachineModuleInfo *MMI,
58+
bool DisableVerify = true) override;
5559

5660
/// Add passes to the specified pass manager to get machine code emitted with
57-
/// the MCJIT. This method returns true if machine code is not supported. It
58-
/// fills the MCContext Ctx pointer which can be used to build custom
59-
/// MCStreamer.
60-
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
61+
/// 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,
6165
raw_pwrite_stream &Out,
66+
MachineModuleInfo *MMI,
6267
bool DisableVerify = true) override;
6368

6469
/// Adds an AsmPrinter pass to the pipeline that prints assembly or

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ using ModulePassManager = PassManager<Module>;
3838

3939
class Function;
4040
class GlobalValue;
41-
class MachineModuleInfoWrapperPass;
41+
class MachineModuleInfo;
4242
struct MachineSchedContext;
4343
class Mangler;
4444
class MCAsmInfo;
@@ -195,7 +195,7 @@ class TargetMachine {
195195
/// TargetSubtargetInfo. In debug builds, it verifies that the object being
196196
/// returned is of the correct type.
197197
template <typename STC> const STC &getSubtarget(const Function &F) const {
198-
return *static_cast<const STC*>(getSubtargetImpl(F));
198+
return *static_cast<const STC *>(getSubtargetImpl(F));
199199
}
200200

201201
/// Create a DataLayout.
@@ -400,27 +400,37 @@ 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+
403411
/// Add passes to the specified pass manager to get the specified file
404-
/// emitted. Typically this will involve several steps of code generation.
412+
/// emitted. Typically, this will involve several steps of code generation.
405413
/// This method should return true if emission of this file type is not
406414
/// supported, or false on success.
407-
/// \p MMIWP is an optional parameter that, if set to non-nullptr,
408-
/// will be used to set the MachineModuloInfo for this PM.
409-
virtual bool
410-
addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
411-
raw_pwrite_stream *, CodeGenFileType,
412-
bool /*DisableVerify*/ = true,
413-
MachineModuleInfoWrapperPass *MMIWP = nullptr) {
415+
/// 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
419+
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
420+
raw_pwrite_stream *, CodeGenFileType,
421+
MachineModuleInfo *,
422+
bool /*DisableVerify*/ = true) {
414423
return true;
415424
}
416425

417426
/// Add passes to the specified pass manager to get machine code emitted with
418-
/// the MCJIT. This method returns true if machine code is not supported. It
419-
/// fills the MCContext Ctx pointer which can be used to build custom
420-
/// MCStreamer.
421-
///
422-
virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
423-
raw_pwrite_stream &,
427+
/// the MCJIT. This method returns true if machine code is not supported.
428+
/// 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
432+
virtual bool addPassesToEmitMC(PassManagerBase &, raw_pwrite_stream &,
433+
MachineModuleInfo *,
424434
bool /*DisableVerify*/ = true) {
425435
return true;
426436
}

llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,27 @@ 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+
211216
bool CodeGenTargetMachineImpl::addPassesToEmitFile(
212217
PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
213-
CodeGenFileType FileType, bool DisableVerify,
214-
MachineModuleInfoWrapperPass *MMIWP) {
215-
// Add common CodeGen passes.
216-
if (!MMIWP)
217-
MMIWP = new MachineModuleInfoWrapperPass(this);
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;
223+
// Add the wrapper pass for MMI
224+
auto *MMIWP = new MachineModuleInfoWrapperPass(MMI);
218225
TargetPassConfig *PassConfig =
219226
addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
220227
if (!PassConfig)
221228
return true;
222229

223230
if (TargetPassConfig::willCompleteCodeGenPipeline()) {
224-
if (addAsmPrinter(PM, Out, DwoOut, FileType, MMIWP->getMMI().getContext()))
231+
if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
225232
return true;
226233
} else {
227234
// MIR printing is redundant with -filetype=null.
@@ -239,19 +246,23 @@ bool CodeGenTargetMachineImpl::addPassesToEmitFile(
239246
/// used to build custom MCStreamer.
240247
///
241248
bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
242-
MCContext *&Ctx,
243249
raw_pwrite_stream &Out,
250+
MachineModuleInfo *MMI,
244251
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)
255+
return true;
245256
// Add common CodeGen passes.
246-
MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this);
257+
MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(MMI);
247258
TargetPassConfig *PassConfig =
248259
addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
249260
if (!PassConfig)
250261
return true;
251262
assert(TargetPassConfig::willCompleteCodeGenPipeline() &&
252263
"Cannot emit MC with limited codegen pipeline");
253264

254-
Ctx = &MMIWP->getMMI().getContext();
265+
MCContext &Ctx = MMI->getContext();
255266
// libunwind is unable to load compact unwind dynamically, so we must generate
256267
// DWARF unwind info for the JIT.
257268
Options.MCOptions.EmitDwarfUnwind = EmitDwarfUnwindType::Always;
@@ -261,7 +272,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
261272
const MCSubtargetInfo &STI = *getMCSubtargetInfo();
262273
const MCRegisterInfo &MRI = *getMCRegisterInfo();
263274
std::unique_ptr<MCCodeEmitter> MCE(
264-
getTarget().createMCCodeEmitter(*getMCInstrInfo(), *Ctx));
275+
getTarget().createMCCodeEmitter(*getMCInstrInfo(), Ctx));
265276
if (!MCE)
266277
return true;
267278
MCAsmBackend *MAB =
@@ -271,7 +282,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
271282

272283
const Triple &T = getTargetTriple();
273284
std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
274-
T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
285+
T, Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
275286
std::move(MCE), STI));
276287

277288
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.

0 commit comments

Comments
 (0)