diff --git a/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h b/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h index 7bb4420e555fb..1491eb3d42964 100644 --- a/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h +++ b/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h @@ -44,22 +44,23 @@ class CodeGenTargetMachineImpl : public TargetMachine { virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) override; /// Add passes to the specified pass manager to get the specified file - /// emitted. Typically this will involve several steps of code generation. - /// \p MMIWP is an optional parameter that, if set to non-nullptr, - /// will be used to set the MachineModuloInfo for this PM. - bool - addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, - raw_pwrite_stream *DwoOut, CodeGenFileType FileType, - bool DisableVerify = true, - MachineModuleInfoWrapperPass *MMIWP = nullptr) override; + /// emitted. Typically, this will involve several steps of code generation. + /// This method should return true if emission of this file type is not + /// supported, or false on success. + /// \p MMI an optional, externally-managed \c MachineModuleInfo to + /// hold the generated machine code even after the \p PM is destroyed + bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, + raw_pwrite_stream *DwoOut, CodeGenFileType FileType, + bool DisableVerify = true, + MachineModuleInfo *MMI = nullptr) override; /// Add passes to the specified pass manager to get machine code emitted with - /// the MCJIT. This method returns true if machine code is not supported. It - /// fills the MCContext Ctx pointer which can be used to build custom - /// MCStreamer. - bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, - raw_pwrite_stream &Out, - bool DisableVerify = true) override; + /// the MCJIT. This method returns true if machine code is not supported. + /// \p MMI an optional, externally-managed \c MachineModuleInfo to + /// hold the generated machine code even after the \p PM is destroyed + bool addPassesToEmitMC(PassManagerBase &PM, raw_pwrite_stream &Out, + bool DisableVerify = true, + MachineModuleInfo *MMI = nullptr) override; /// Adds an AsmPrinter pass to the pipeline that prints assembly or /// machine code from the MI representation. diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h b/llvm/include/llvm/CodeGen/MachineModuleInfo.h index bec500dc609f3..4e4582fa0021a 100644 --- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h +++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h @@ -106,15 +106,11 @@ class MachineModuleInfo { const Function *LastRequest = nullptr; ///< Used for shortcut/cache. MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache. - MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete; - public: explicit MachineModuleInfo(const TargetMachine *TM = nullptr); explicit MachineModuleInfo(const TargetMachine *TM, MCContext *ExtContext); - MachineModuleInfo(MachineModuleInfo &&MMII); - ~MachineModuleInfo(); void initialize(); @@ -167,22 +163,51 @@ class MachineModuleInfo { /// \} }; // End class MachineModuleInfo +/// \brief Interface for pass that provide access to \c MachineModuleInfo +/// being worked on class MachineModuleInfoWrapperPass : public ImmutablePass { - MachineModuleInfo MMI; public: static char ID; // Pass identification, replacement for typeid - explicit MachineModuleInfoWrapperPass(const TargetMachine *TM = nullptr); - - explicit MachineModuleInfoWrapperPass(const TargetMachine *TM, - MCContext *ExtContext); + MachineModuleInfoWrapperPass(); // Initialization and Finalization bool doInitialization(Module &) override; bool doFinalization(Module &) override; - MachineModuleInfo &getMMI() { return MMI; } - const MachineModuleInfo &getMMI() const { return MMI; } + virtual MachineModuleInfo &getMMI() = 0; + virtual const MachineModuleInfo &getMMI() const = 0; +}; + +/// \brief a version of \c MachineModuleInfoWrapperPass that manages the +/// lifetime of its \c MachineModuleInfo +class OwningMachineModuleInfoWrapperPass : public MachineModuleInfoWrapperPass { + MachineModuleInfo MMI; + +public: + explicit OwningMachineModuleInfoWrapperPass(const TargetMachine &TM) + : MMI(&TM) {}; + + OwningMachineModuleInfoWrapperPass(const TargetMachine &TM, + MCContext &ExtContext) + : MMI(&TM, &ExtContext) {}; + + MachineModuleInfo &getMMI() override { return MMI; } + const MachineModuleInfo &getMMI() const override { return MMI; } +}; + +/// \brief a version of \c MachineModuleInfoWrapperPass that does not manage the +/// lifetime of its \c MachineModuleInfo +class NonOwningMachineModuleInfoWrapperPass + : public MachineModuleInfoWrapperPass { + MachineModuleInfo &MMI; + +public: + explicit NonOwningMachineModuleInfoWrapperPass(MachineModuleInfo &MMI) + : MMI(MMI) {}; + + MachineModuleInfo &getMMI() override { return MMI; } + const MachineModuleInfo &getMMI() const override { return MMI; } }; /// An analysis that produces \c MachineModuleInfo for a module. diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h index 27eeb415ed644..3d8c6694297c9 100644 --- a/llvm/include/llvm/Target/TargetMachine.h +++ b/llvm/include/llvm/Target/TargetMachine.h @@ -38,7 +38,7 @@ using ModulePassManager = PassManager; class Function; class GlobalValue; -class MachineModuleInfoWrapperPass; +class MachineModuleInfo; struct MachineSchedContext; class Mangler; class MCAsmInfo; @@ -401,27 +401,29 @@ class TargetMachine { virtual void registerDefaultAliasAnalyses(AAManager &) {} /// Add passes to the specified pass manager to get the specified file - /// emitted. Typically this will involve several steps of code generation. + /// emitted. Typically, this will involve several steps of code generation. /// This method should return true if emission of this file type is not /// supported, or false on success. - /// \p MMIWP is an optional parameter that, if set to non-nullptr, - /// will be used to set the MachineModuloInfo for this PM. - virtual bool - addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, - raw_pwrite_stream *, CodeGenFileType, - bool /*DisableVerify*/ = true, - MachineModuleInfoWrapperPass *MMIWP = nullptr) { + /// For targets that utilize the target-independent code generator + /// (CodeGen), an externally-managed \c MachineModuleInfo can be provided + /// to ensure persistence of the generated machine code even after the pass + /// manager is destroyed + virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, + raw_pwrite_stream *, CodeGenFileType, + bool /*DisableVerify*/ = true, + MachineModuleInfo * = nullptr) { return true; } /// Add passes to the specified pass manager to get machine code emitted with - /// the MCJIT. This method returns true if machine code is not supported. It - /// fills the MCContext Ctx pointer which can be used to build custom - /// MCStreamer. - /// - virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&, - raw_pwrite_stream &, - bool /*DisableVerify*/ = true) { + /// the MCJIT. This method returns true if machine code is not supported. + /// For targets that utilize the target-independent code generator + /// (CodeGen), an externally-managed \c MachineModuleInfo can be provided + /// to ensure persistence of the generated machine code even after the pass + /// manager is destroyed + virtual bool addPassesToEmitMC(PassManagerBase &, raw_pwrite_stream &, + bool /*DisableVerify*/ = true, + MachineModuleInfo * = nullptr) { return true; } diff --git a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp index 5757ca1b3adf8..3495a45859054 100644 --- a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp +++ b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp @@ -210,18 +210,27 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out, bool CodeGenTargetMachineImpl::addPassesToEmitFile( PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, - CodeGenFileType FileType, bool DisableVerify, - MachineModuleInfoWrapperPass *MMIWP) { - // Add common CodeGen passes. - if (!MMIWP) - MMIWP = new MachineModuleInfoWrapperPass(this); + CodeGenFileType FileType, bool DisableVerify, MachineModuleInfo *MMI) { + // Add the wrapper pass for MMI + MachineModuleInfoWrapperPass *MMIWP; + if (MMI) { + MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI); + } else { + MMIWP = new OwningMachineModuleInfoWrapperPass(*this); + MMI = &MMIWP->getMMI(); + } + // Check (for the case of externally-managed MMI) if the TM of MMI is + // the same as this target machine + if (&MMI->getTarget() != this) + return true; + TargetPassConfig *PassConfig = addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); if (!PassConfig) return true; if (TargetPassConfig::willCompleteCodeGenPipeline()) { - if (addAsmPrinter(PM, Out, DwoOut, FileType, MMIWP->getMMI().getContext())) + if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext())) return true; } else { // MIR printing is redundant with -filetype=null. @@ -233,17 +242,22 @@ bool CodeGenTargetMachineImpl::addPassesToEmitFile( return false; } -/// addPassesToEmitMC - Add passes to the specified pass manager to get -/// machine code emitted with the MCJIT. This method returns true if machine -/// code is not supported. It fills the MCContext Ctx pointer which can be -/// used to build custom MCStreamer. -/// bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM, - MCContext *&Ctx, raw_pwrite_stream &Out, - bool DisableVerify) { - // Add common CodeGen passes. - MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this); + bool DisableVerify, + MachineModuleInfo *MMI) { + // Add the wrapper pass for MMI + MachineModuleInfoWrapperPass *MMIWP; + if (MMI) { + MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI); + } else { + MMIWP = new OwningMachineModuleInfoWrapperPass(*this); + MMI = &MMIWP->getMMI(); + } + // Check (for the case of externally-managed MMI) if the TM of MMI is + // the same as this target machine + if (&MMI->getTarget() != this) + return true; TargetPassConfig *PassConfig = addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); if (!PassConfig) @@ -251,7 +265,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM, assert(TargetPassConfig::willCompleteCodeGenPipeline() && "Cannot emit MC with limited codegen pipeline"); - Ctx = &MMIWP->getMMI().getContext(); + MCContext &Ctx = MMI->getContext(); // libunwind is unable to load compact unwind dynamically, so we must generate // DWARF unwind info for the JIT. Options.MCOptions.EmitDwarfUnwind = EmitDwarfUnwindType::Always; @@ -261,7 +275,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM, const MCSubtargetInfo &STI = *getMCSubtargetInfo(); const MCRegisterInfo &MRI = *getMCRegisterInfo(); std::unique_ptr MCE( - getTarget().createMCCodeEmitter(*getMCInstrInfo(), *Ctx)); + getTarget().createMCCodeEmitter(*getMCInstrInfo(), Ctx)); if (!MCE) return true; MCAsmBackend *MAB = @@ -271,7 +285,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM, const Triple &T = getTargetTriple(); std::unique_ptr AsmStreamer(getTarget().createMCObjectStreamer( - T, *Ctx, std::unique_ptr(MAB), MAB->createObjectWriter(Out), + T, Ctx, std::unique_ptr(MAB), MAB->createObjectWriter(Out), std::move(MCE), STI)); // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp index 6167e99aecf03..76841d4116ef4 100644 --- a/llvm/lib/CodeGen/MachineModuleInfo.cpp +++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp @@ -37,17 +37,6 @@ void MachineModuleInfo::finalize() { ObjFileMMI = nullptr; } -MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI) - : TM(std::move(MMI.TM)), - Context(TM.getTargetTriple(), TM.getMCAsmInfo(), TM.getMCRegisterInfo(), - TM.getMCSubtargetInfo(), nullptr, &TM.Options.MCOptions, false), - MachineFunctions(std::move(MMI.MachineFunctions)) { - Context.setObjectFileInfo(TM.getObjFileLowering()); - ObjFileMMI = MMI.ObjFileMMI; - ExternalContext = MMI.ExternalContext; - TheModule = MMI.TheModule; -} - MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM) : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(), TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(), @@ -150,15 +139,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() { return new FreeMachineFunction(); } -MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass( - const TargetMachine *TM) - : ImmutablePass(ID), MMI(TM) { - initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); -} - -MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass( - const TargetMachine *TM, MCContext *ExtContext) - : ImmutablePass(ID), MMI(TM, ExtContext) { +MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass() + : ImmutablePass(ID) { initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); } @@ -193,6 +175,7 @@ static uint64_t getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr, } bool MachineModuleInfoWrapperPass::doInitialization(Module &M) { + MachineModuleInfo &MMI = getMMI(); MMI.initialize(); MMI.TheModule = &M; LLVMContext &Ctx = M.getContext(); @@ -210,7 +193,7 @@ bool MachineModuleInfoWrapperPass::doInitialization(Module &M) { } bool MachineModuleInfoWrapperPass::doFinalization(Module &M) { - MMI.finalize(); + getMMI().finalize(); return false; } diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 5f3067b2a97ea..e73db347e4c01 100644 --- a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -66,9 +66,8 @@ MCJIT::MCJIT(std::unique_ptr M, std::unique_ptr TM, std::shared_ptr MemMgr, std::shared_ptr Resolver) : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)), - Ctx(nullptr), MemMgr(std::move(MemMgr)), - Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver), - ObjCache(nullptr) { + MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)), + Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) { // FIXME: We are managing our modules, so we do not want the base class // ExecutionEngine to manage them as well. To avoid double destruction // of the first (and only) module added in ExecutionEngine constructor @@ -163,7 +162,7 @@ std::unique_ptr MCJIT::emitObject(Module *M) { // Turn the machine code intermediate representation into bytes in memory // that may be executed. - if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules())) + if (TM->addPassesToEmitMC(PM, ObjStream, !getVerifyModules())) report_fatal_error("Target does not support MC emission!"); // Initialize passes. diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h index fbe64fabd3240..596b83dbb8a05 100644 --- a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h +++ b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h @@ -168,7 +168,6 @@ class MCJIT : public ExecutionEngine { }; std::unique_ptr TM; - MCContext *Ctx; std::shared_ptr MemMgr; LinkingSymbolResolver Resolver; RuntimeDyld Dyld; diff --git a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp index c4d65af1b57f8..b09cc738996a5 100644 --- a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp @@ -46,8 +46,7 @@ Expected SimpleCompiler::operator()(Module &M) { raw_svector_ostream ObjStream(ObjBufferSV); legacy::PassManager PM; - MCContext *Ctx; - if (TM.addPassesToEmitMC(PM, Ctx, ObjStream)) + if (TM.addPassesToEmitMC(PM, ObjStream)) return make_error("Target does not support MC emission", inconvertibleErrorCode()); PM.run(M); diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp index ce408b4034f83..d7a05ebf1f738 100644 --- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp +++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp @@ -133,8 +133,7 @@ void DirectXTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { bool DirectXTargetMachine::addPassesToEmitFile( PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, - CodeGenFileType FileType, bool DisableVerify, - MachineModuleInfoWrapperPass *MMIWP) { + CodeGenFileType FileType, bool DisableVerify, MachineModuleInfo *MMI) { TargetPassConfig *PassConfig = createPassConfig(PM); PassConfig->addCodeGenPrepare(); @@ -150,8 +149,17 @@ bool DirectXTargetMachine::addPassesToEmitFile( // globals don't pollute the DXIL. PM.add(createDXContainerGlobalsPass()); - if (!MMIWP) - MMIWP = new MachineModuleInfoWrapperPass(this); + MachineModuleInfoWrapperPass *MMIWP; + if (MMI) { + MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI); + } else { + MMIWP = new OwningMachineModuleInfoWrapperPass(*this); + MMI = &MMIWP->getMMI(); + } + // Check (for the case of externally-managed MMI) if the TM of MMI is + // the same as this target machine + if (&MMI->getTarget() != this) + return true; PM.add(MMIWP); if (addAsmPrinter(PM, Out, DwoOut, FileType, MMIWP->getMMI().getContext())) @@ -166,9 +174,9 @@ bool DirectXTargetMachine::addPassesToEmitFile( } bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM, - MCContext *&Ctx, raw_pwrite_stream &Out, - bool DisableVerify) { + bool DisableVerify, + MachineModuleInfo *MMI) { return true; } diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.h b/llvm/lib/Target/DirectX/DirectXTargetMachine.h index 7ba80d2ba5de1..21c50d4c996a9 100644 --- a/llvm/lib/Target/DirectX/DirectXTargetMachine.h +++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.h @@ -33,10 +33,10 @@ class DirectXTargetMachine : public CodeGenTargetMachineImpl { bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, bool DisableVerify, - MachineModuleInfoWrapperPass *MMIWP) override; + MachineModuleInfo *MMIWP) override; - bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, - raw_pwrite_stream &Out, bool DisableVerify) override; + bool addPassesToEmitMC(PassManagerBase &PM, raw_pwrite_stream &Out, + bool DisableVerify, MachineModuleInfo *MMIWP) override; const DirectXSubtarget *getSubtargetImpl(const Function &) const override; diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h index 34d841cd28404..159f31fa6daa1 100644 --- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h +++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h @@ -52,8 +52,8 @@ class NVPTXTargetMachine : public CodeGenTargetMachineImpl { TargetPassConfig *createPassConfig(PassManagerBase &PM) override; // Emission of machine code through MCJIT is not supported. - bool addPassesToEmitMC(PassManagerBase &, MCContext *&, raw_pwrite_stream &, - bool = true) override { + bool addPassesToEmitMC(PassManagerBase &, raw_pwrite_stream &, bool = true, + MachineModuleInfo * = nullptr) override { return true; } TargetLoweringObjectFile *getObjFileLowering() const override { diff --git a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp index 145285a31dc10..e3159c83c4eb2 100644 --- a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp @@ -121,15 +121,14 @@ SPIRVTranslate(Module *M, std::string &SpirvObj, std::string &ErrMsg, TargetLibraryInfoImpl TLII(M->getTargetTriple()); legacy::PassManager PM; PM.add(new TargetLibraryInfoWrapperPass(TLII)); - std::unique_ptr MMIWP( - new MachineModuleInfoWrapperPass(Target.get())); - const_cast(Target->getObjFileLowering()) - ->Initialize(MMIWP.get()->getMMI().getContext(), *Target); + + MachineModuleInfo MMI(Target.get()); + Target->getObjFileLowering()->Initialize(MMI.getContext(), *Target); SmallString<4096> OutBuffer; raw_svector_ostream OutStream(OutBuffer); if (Target->addPassesToEmitFile(PM, OutStream, nullptr, - CodeGenFileType::ObjectFile)) { + CodeGenFileType::ObjectFile, true, &MMI)) { ErrMsg = "Target machine cannot emit a file of this type"; return false; } diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp index a97d07dec722b..27ac543327f4e 100644 --- a/llvm/tools/llc/llc.cpp +++ b/llvm/tools/llc/llc.cpp @@ -671,8 +671,7 @@ static int compileModule(char **argv, LLVMContext &Context) { } const char *argv0 = argv[0]; - MachineModuleInfoWrapperPass *MMIWP = - new MachineModuleInfoWrapperPass(Target.get()); + MachineModuleInfo MMI(Target.get()); // Construct a custom pass pipeline that starts after instruction // selection. @@ -680,7 +679,6 @@ static int compileModule(char **argv, LLVMContext &Context) { if (!MIR) { WithColor::error(errs(), argv[0]) << "run-pass is for .mir file only.\n"; - delete MMIWP; return 1; } TargetPassConfig *PTPC = Target->createPassConfig(PM); @@ -690,13 +688,12 @@ static int compileModule(char **argv, LLVMContext &Context) { << "run-pass cannot be used with " << TPC.getLimitedCodeGenPipelineReason() << ".\n"; delete PTPC; - delete MMIWP; return 1; } TPC.setDisableVerify(NoVerify); PM.add(&TPC); - PM.add(MMIWP); + PM.add(new NonOwningMachineModuleInfoWrapperPass(MMI)); TPC.printAndVerify(""); for (const std::string &RunPassName : getRunPassNames()) { if (addPass(PM, argv0, RunPassName, TPC)) @@ -707,15 +704,14 @@ static int compileModule(char **argv, LLVMContext &Context) { PM.add(createFreeMachineFunctionPass()); } else if (Target->addPassesToEmitFile( PM, *OS, DwoOut ? &DwoOut->os() : nullptr, - codegen::getFileType(), NoVerify, MMIWP)) { + codegen::getFileType(), NoVerify, &MMI)) { reportError("target does not support generation of this file type"); } const_cast(Target->getObjFileLowering()) - ->Initialize(MMIWP->getMMI().getContext(), *Target); + ->Initialize(MMI.getContext(), *Target); if (MIR) { - assert(MMIWP && "Forgot to create MMIWP?"); - if (MIR->parseMachineFunctions(*M, MMIWP->getMMI())) + if (MIR->parseMachineFunctions(*M, MMI)) return 1; } diff --git a/llvm/tools/llvm-exegesis/lib/Assembler.cpp b/llvm/tools/llvm-exegesis/lib/Assembler.cpp index f638478e0c51d..729c829b106e8 100644 --- a/llvm/tools/llvm-exegesis/lib/Assembler.cpp +++ b/llvm/tools/llvm-exegesis/lib/Assembler.cpp @@ -233,9 +233,9 @@ createModule(const std::unique_ptr &Context, const DataLayout &DL) BitVector getFunctionReservedRegs(const TargetMachine &TM) { std::unique_ptr Context = std::make_unique(); std::unique_ptr Module = createModule(Context, TM.createDataLayout()); - auto MMIWP = std::make_unique(&TM); - MachineFunction &MF = createVoidVoidPtrMachineFunction( - FunctionID, Module.get(), &MMIWP->getMMI()); + auto MMI = std::make_unique(&TM); + MachineFunction &MF = + createVoidVoidPtrMachineFunction(FunctionID, Module.get(), MMI.get()); // Saving reserved registers for client. return MF.getSubtarget().getRegisterInfo()->getReservedRegs(MF); } @@ -248,7 +248,7 @@ Error assembleToStream(const ExegesisTarget &ET, auto Context = std::make_unique(); std::unique_ptr Module = createModule(Context, TM->createDataLayout()); - auto MMIWP = std::make_unique(TM.get()); + auto MMIWP = std::make_unique(*TM); MachineFunction &MF = createVoidVoidPtrMachineFunction( FunctionID, Module.get(), &MMIWP.get()->getMMI()); MF.ensureAlignment(kFunctionAlignment); diff --git a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp index 6c08173f78622..ca01b2a4fed4e 100644 --- a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp +++ b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp @@ -403,7 +403,7 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase { AP->addAsmPrinterHandler(std::make_unique(*this)); TargetMachine *TM = &AP->TM; legacy::PassManager PM; - PM.add(new MachineModuleInfoWrapperPass(TM)); + PM.add(new OwningMachineModuleInfoWrapperPass(*TM)); PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP LLVMContext Context; std::unique_ptr M(new Module("TestModule", Context)); diff --git a/llvm/unittests/CodeGen/X86MCInstLowerTest.cpp b/llvm/unittests/CodeGen/X86MCInstLowerTest.cpp index 72cc79a8dfd8c..629019a1717ad 100644 --- a/llvm/unittests/CodeGen/X86MCInstLowerTest.cpp +++ b/llvm/unittests/CodeGen/X86MCInstLowerTest.cpp @@ -149,7 +149,7 @@ class X86MCInstLowerTest : public testing::Test { TEST_F(X86MCInstLowerTest, moExternalSymbol_MCSYMBOL) { MachineModuleInfoWrapperPass *MMIWP = - new MachineModuleInfoWrapperPass(TM.get(), &*MCFoo); + new OwningMachineModuleInfoWrapperPass(*TM, *MCFoo); SmallString<1024> Buf; llvm::raw_svector_ostream OS(Buf); diff --git a/llvm/unittests/MI/LiveIntervalTest.cpp b/llvm/unittests/MI/LiveIntervalTest.cpp index 85a5085ece5fd..2adb894c32d70 100644 --- a/llvm/unittests/MI/LiveIntervalTest.cpp +++ b/llvm/unittests/MI/LiveIntervalTest.cpp @@ -70,7 +70,8 @@ std::unique_ptr parseMIR(LLVMContext &Context, M->setDataLayout(TM.createDataLayout()); - MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(&TM); + MachineModuleInfoWrapperPass *MMIWP = + new OwningMachineModuleInfoWrapperPass(TM); if (MIR->parseMachineFunctions(*M, MMIWP->getMMI())) return nullptr; PM.add(MMIWP); diff --git a/offload/plugins-nextgen/common/src/JIT.cpp b/offload/plugins-nextgen/common/src/JIT.cpp index affedb1a33687..7cc4c7510959e 100644 --- a/offload/plugins-nextgen/common/src/JIT.cpp +++ b/offload/plugins-nextgen/common/src/JIT.cpp @@ -175,11 +175,11 @@ void JITEngine::codegen(TargetMachine *TM, TargetLibraryInfoImpl *TLII, Module &M, raw_pwrite_stream &OS) { legacy::PassManager PM; PM.add(new TargetLibraryInfoWrapperPass(*TLII)); - MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(TM); + MachineModuleInfo MMI(TM); TM->addPassesToEmitFile(PM, OS, nullptr, TT.isNVPTX() ? CodeGenFileType::AssemblyFile : CodeGenFileType::ObjectFile, - /*DisableVerify=*/false, MMIWP); + /*DisableVerify=*/false, &MMI); PM.run(M); }