diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 62c6a57e8b7c8..34e58c1f8cd93 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -26,6 +26,7 @@ #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/Bitcode/BitcodeWriter.h" #include "llvm/Bitcode/BitcodeWriterPass.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/RegAllocRegistry.h" #include "llvm/CodeGen/SchedulerRegistry.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" @@ -167,8 +168,9 @@ class EmitAssemblyHelper { /// Add passes necessary to emit assembly or LLVM IR. /// /// \return True on success. - bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action, - raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS); + bool AddEmitPasses(legacy::PassManager &CodeGenPasses, MachineModuleInfo &MMI, + BackendAction Action, raw_pwrite_stream &OS, + raw_pwrite_stream *DwoOS); std::unique_ptr openOutputFile(StringRef Path) { std::error_code EC; @@ -613,6 +615,7 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { } bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses, + MachineModuleInfo &MMI, BackendAction Action, raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS) { @@ -625,7 +628,7 @@ bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses, // this also adds codegenerator level optimization passes. CodeGenFileType CGFT = getCodeGenFileType(Action); - if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT, + if (TM->addPassesToEmitFile(CodeGenPasses, MMI, OS, DwoOS, CGFT, /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { Diags.Report(diag::err_fe_unable_to_interface_with_target); return false; @@ -1162,6 +1165,7 @@ void EmitAssemblyHelper::RunCodegenPipeline( // does not work with the codegen pipeline. // FIXME: make the new PM work with the codegen pipeline. legacy::PassManager CodeGenPasses; + std::unique_ptr MMI; // Append any output we need to the pass manager. switch (Action) { @@ -1175,7 +1179,9 @@ void EmitAssemblyHelper::RunCodegenPipeline( if (!DwoOS) return; } - if (!AddEmitPasses(CodeGenPasses, Action, *OS, + MMI = std::make_unique( + static_cast(TM.get())); + if (!AddEmitPasses(CodeGenPasses, *MMI, Action, *OS, DwoOS ? &DwoOS->os() : nullptr)) // FIXME: Should we handle this error differently? return; diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp index 1999d63d1aa04..fd4a31ef8135e 100644 --- a/clang/lib/Interpreter/DeviceOffload.cpp +++ b/clang/lib/Interpreter/DeviceOffload.cpp @@ -17,6 +17,7 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Interpreter/PartialTranslationUnit.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/MC/TargetRegistry.h" @@ -82,16 +83,18 @@ llvm::Expected IncrementalCUDADeviceParser::GeneratePTX() { return llvm::make_error(std::move(Error), std::error_code()); llvm::TargetOptions TO = llvm::TargetOptions(); - llvm::TargetMachine *TargetMachine = Target->createTargetMachine( - PTU.TheModule->getTargetTriple(), TargetOpts.CPU, "", TO, - llvm::Reloc::Model::PIC_); + auto *TargetMachine = + static_cast(Target->createTargetMachine( + PTU.TheModule->getTargetTriple(), TargetOpts.CPU, "", TO, + llvm::Reloc::Model::PIC_)); PTU.TheModule->setDataLayout(TargetMachine->createDataLayout()); PTXCode.clear(); llvm::raw_svector_ostream dest(PTXCode); llvm::legacy::PassManager PM; - if (TargetMachine->addPassesToEmitFile(PM, dest, nullptr, + llvm::MachineModuleInfo MMI(TargetMachine); + if (TargetMachine->addPassesToEmitFile(PM, MMI, dest, nullptr, llvm::CodeGenFileType::AssemblyFile)) { return llvm::make_error( "NVPTX backend cannot produce PTX code.", diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp index 1001410aa0f27..863fab9fd6fa3 100644 --- a/clang/lib/Interpreter/Wasm.cpp +++ b/clang/lib/Interpreter/Wasm.cpp @@ -13,6 +13,7 @@ #include "Wasm.h" #include "IncrementalExecutor.h" +#include #include #include #include @@ -48,8 +49,9 @@ llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) { } llvm::TargetOptions TO = llvm::TargetOptions(); - llvm::TargetMachine *TargetMachine = Target->createTargetMachine( - PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_); + auto *TargetMachine = static_cast( + Target->createTargetMachine(PTU.TheModule->getTargetTriple(), "", "", TO, + llvm::Reloc::Model::PIC_)); PTU.TheModule->setDataLayout(TargetMachine->createDataLayout()); std::string OutputFileName = PTU.TheModule->getName().str() + ".wasm"; @@ -57,7 +59,8 @@ llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) { llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error); llvm::legacy::PassManager PM; - if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr, + llvm::MachineModuleInfo MMI(TargetMachine); + if (TargetMachine->addPassesToEmitFile(PM, MMI, OutputFile, nullptr, llvm::CodeGenFileType::ObjectFile)) { return llvm::make_error( "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode()); diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp index 9fea1fdcd5fb4..f1baa98ec5fe9 100644 --- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp +++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp @@ -20,6 +20,7 @@ #include "llvm/BinaryFormat/Magic.h" #include "llvm/Bitcode/BitcodeWriter.h" #include "llvm/CodeGen/CommandFlags.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/Frontend/Offloading/OffloadWrapper.h" #include "llvm/Frontend/Offloading/Utility.h" #include "llvm/IR/Constants.h" @@ -1031,9 +1032,9 @@ Expected compileModule(Module &M, OffloadKind Kind) { codegen::InitTargetOptionsFromCodeGenFlags(Triple(M.getTargetTriple())); StringRef CPU = ""; StringRef Features = ""; - std::unique_ptr TM( + std::unique_ptr TM(static_cast( T->createTargetMachine(M.getTargetTriple(), CPU, Features, Options, - Reloc::PIC_, M.getCodeModel())); + Reloc::PIC_, M.getCodeModel()))); if (M.getDataLayout().isDefault()) M.setDataLayout(TM->createDataLayout()); @@ -1051,9 +1052,10 @@ Expected compileModule(Module &M, OffloadKind Kind) { auto OS = std::make_unique(FD, true); legacy::PassManager CodeGenPasses; + MachineModuleInfo MMI(TM.get()); TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple())); CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII)); - if (TM->addPassesToEmitFile(CodeGenPasses, *OS, nullptr, + if (TM->addPassesToEmitFile(CodeGenPasses, MMI, *OS, nullptr, CodeGenFileType::ObjectFile)) return createStringError("Failed to execute host backend"); CodeGenPasses.run(M); diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index 4a52edc436e0e..64aba542b0d1f 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -52,6 +52,7 @@ #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Bitcode/BitcodeWriterPass.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" #include "llvm/IR/LLVMRemarkStreamer.h" #include "llvm/IR/LegacyPassManager.h" @@ -932,6 +933,7 @@ static void generateMachineCodeOrAssemblyImpl(clang::DiagnosticsEngine &diags, // Currently only the legacy pass manager is supported. // TODO: Switch to the new PM once it's available in the backend. llvm::legacy::PassManager codeGenPasses; + llvm::MachineModuleInfo mmi(static_cast(&tm)); codeGenPasses.add( createTargetTransformInfoWrapperPass(tm.getTargetIRAnalysis())); @@ -943,7 +945,7 @@ static void generateMachineCodeOrAssemblyImpl(clang::DiagnosticsEngine &diags, llvm::CodeGenFileType cgft = (act == BackendActionTy::Backend_EmitAssembly) ? llvm::CodeGenFileType::AssemblyFile : llvm::CodeGenFileType::ObjectFile; - if (tm.addPassesToEmitFile(codeGenPasses, os, nullptr, cgft)) { + if (tm.addPassesToEmitFile(codeGenPasses, mmi, os, nullptr, cgft)) { unsigned diagID = diags.getCustomDiagID(clang::DiagnosticsEngine::Error, "emission of this file type is not supported"); diff --git a/llvm/examples/Kaleidoscope/Chapter8/toy.cpp b/llvm/examples/Kaleidoscope/Chapter8/toy.cpp index ae2f9c7059e5f..45e7b27ef241b 100644 --- a/llvm/examples/Kaleidoscope/Chapter8/toy.cpp +++ b/llvm/examples/Kaleidoscope/Chapter8/toy.cpp @@ -1,5 +1,6 @@ #include "llvm/ADT/APFloat.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" @@ -1257,9 +1258,12 @@ int main() { } legacy::PassManager pass; + llvm::MachineModuleInfo MMI( + static_cast(TheTargetMachine)); auto FileType = CodeGenFileType::ObjectFile; - if (TheTargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType)) { + if (TheTargetMachine->addPassesToEmitFile(pass, MMI, dest, nullptr, + FileType)) { errs() << "TheTargetMachine can't emit a file of this type"; return 1; } diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h b/llvm/include/llvm/CodeGen/MachineModuleInfo.h index 310cc4b2abb77..8c81df23c2998 100644 --- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h +++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h @@ -106,7 +106,11 @@ class MachineModuleInfo { const Function *LastRequest = nullptr; ///< Used for shortcut/cache. MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache. - MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete; + /// Deleted copy constructor + MachineModuleInfo(MachineModuleInfo &MMI) = delete; + + /// Deleted copy assignment operator + MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete; public: explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr); @@ -114,8 +118,6 @@ class MachineModuleInfo { explicit MachineModuleInfo(const LLVMTargetMachine *TM, MCContext *ExtContext); - MachineModuleInfo(MachineModuleInfo &&MMII); - ~MachineModuleInfo(); void initialize(); @@ -169,14 +171,11 @@ class MachineModuleInfo { }; // End class MachineModuleInfo class MachineModuleInfoWrapperPass : public ImmutablePass { - MachineModuleInfo MMI; + MachineModuleInfo &MMI; public: static char ID; // Pass identification, replacement for typeid - explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr); - - explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM, - MCContext *ExtContext); + explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI); // Initialization and Finalization bool doInitialization(Module &) override; diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h index c3e9d41315f61..69aff06032502 100644 --- a/llvm/include/llvm/Target/TargetMachine.h +++ b/llvm/include/llvm/Target/TargetMachine.h @@ -34,7 +34,7 @@ using ModulePassManager = PassManager; class Function; class GlobalValue; -class MachineModuleInfoWrapperPass; +class MachineModuleInfo; class Mangler; class MCAsmInfo; class MCContext; @@ -376,25 +376,19 @@ 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) { + virtual bool addPassesToEmitFile(PassManagerBase &, MachineModuleInfo &, + raw_pwrite_stream &, raw_pwrite_stream *, + CodeGenFileType, + bool /*DisableVerify*/ = true) { 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 *&, + /// the MCJIT. This method returns true if machine code is not supported. + virtual bool addPassesToEmitMC(PassManagerBase &, MachineModuleInfo &, raw_pwrite_stream &, bool /*DisableVerify*/ = true) { return true; @@ -460,14 +454,13 @@ class LLVMTargetMachine : public TargetMachine { virtual TargetPassConfig *createPassConfig(PassManagerBase &PM); /// 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. + bool addPassesToEmitFile(PassManagerBase &PM, MachineModuleInfo &MMI, + raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, + CodeGenFileType FileType, + bool DisableVerify = true) override; virtual Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, @@ -478,10 +471,8 @@ class LLVMTargetMachine : public TargetMachine { } /// 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, + /// the MCJIT. This method returns true if machine code is not supported. + bool addPassesToEmitMC(PassManagerBase &PM, MachineModuleInfo &MMI, raw_pwrite_stream &Out, bool DisableVerify = true) override; diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt index 5a17944db0ae0..1ef4857af462c 100644 --- a/llvm/lib/CodeGen/CMakeLists.txt +++ b/llvm/lib/CodeGen/CMakeLists.txt @@ -100,6 +100,7 @@ add_llvm_component_library(LLVMCodeGen LiveStacks.cpp LiveVariables.cpp LLVMTargetMachine.cpp + LLVMTargetMachineC.cpp LocalStackSlotAllocation.cpp LoopTraversal.cpp LowLevelTypeUtils.cpp diff --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp b/llvm/lib/CodeGen/LLVMTargetMachine.cpp index ea36fedef93ac..6a005739d8dae 100644 --- a/llvm/lib/CodeGen/LLVMTargetMachine.cpp +++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp @@ -209,12 +209,10 @@ Expected> LLVMTargetMachine::createMCStreamer( } bool LLVMTargetMachine::addPassesToEmitFile( - PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, - CodeGenFileType FileType, bool DisableVerify, - MachineModuleInfoWrapperPass *MMIWP) { + PassManagerBase &PM, MachineModuleInfo &MMI, raw_pwrite_stream &Out, + raw_pwrite_stream *DwoOut, CodeGenFileType FileType, bool DisableVerify) { // Add common CodeGen passes. - if (!MMIWP) - MMIWP = new MachineModuleInfoWrapperPass(this); + auto *MMIWP = new MachineModuleInfoWrapperPass(MMI); TargetPassConfig *PassConfig = addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); if (!PassConfig) @@ -235,14 +233,13 @@ bool LLVMTargetMachine::addPassesToEmitFile( /// 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 LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, +/// code is not supported. +bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, + MachineModuleInfo &MMI, raw_pwrite_stream &Out, bool DisableVerify) { // Add common CodeGen passes. - MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this); + MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(MMI); TargetPassConfig *PassConfig = addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); if (!PassConfig) @@ -250,7 +247,7 @@ bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, assert(TargetPassConfig::willCompleteCodeGenPipeline() && "Cannot emit MC with limited codegen pipeline"); - Ctx = &MMIWP->getMMI().getContext(); + auto &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; @@ -260,7 +257,7 @@ bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 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 = @@ -270,7 +267,7 @@ bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 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/LLVMTargetMachineC.cpp b/llvm/lib/CodeGen/LLVMTargetMachineC.cpp new file mode 100644 index 0000000000000..b6b7b74de93bc --- /dev/null +++ b/llvm/lib/CodeGen/LLVMTargetMachineC.cpp @@ -0,0 +1,102 @@ +//===-- LLVMTargetMachineC.cpp --------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements the LLVM-C part of TargetMachine.h that directly +// depends on the CodeGen library. +// +//===----------------------------------------------------------------------===// + +#include "llvm-c/Core.h" +#include "llvm-c/TargetMachine.h" +#include "llvm/CodeGen/MachineModuleInfo.h" +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" + +using namespace llvm; + +static TargetMachine *unwrap(LLVMTargetMachineRef P) { + return reinterpret_cast(P); +} + +static Target *unwrap(LLVMTargetRef P) { return reinterpret_cast(P); } + +static LLVMTargetMachineRef wrap(const TargetMachine *P) { + return reinterpret_cast(const_cast(P)); +} + +static LLVMTargetRef wrap(const Target *P) { + return reinterpret_cast(const_cast(P)); +} + +static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, + raw_pwrite_stream &OS, + LLVMCodeGenFileType codegen, + char **ErrorMessage) { + TargetMachine *TM = unwrap(T); + Module *Mod = unwrap(M); + + legacy::PassManager pass; + MachineModuleInfo MMI(static_cast(TM)); + + std::string error; + + Mod->setDataLayout(TM->createDataLayout()); + + CodeGenFileType ft; + switch (codegen) { + case LLVMAssemblyFile: + ft = CodeGenFileType::AssemblyFile; + break; + default: + ft = CodeGenFileType::ObjectFile; + break; + } + if (TM->addPassesToEmitFile(pass, MMI, OS, nullptr, ft)) { + error = "TargetMachine can't emit a file of this type"; + *ErrorMessage = strdup(error.c_str()); + return true; + } + + pass.run(*Mod); + + OS.flush(); + return false; +} + +LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, + const char *Filename, + LLVMCodeGenFileType codegen, + char **ErrorMessage) { + std::error_code EC; + raw_fd_ostream dest(Filename, EC, sys::fs::OF_None); + if (EC) { + *ErrorMessage = strdup(EC.message().c_str()); + return true; + } + bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage); + dest.flush(); + return Result; +} + +LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, + LLVMModuleRef M, + LLVMCodeGenFileType codegen, + char **ErrorMessage, + LLVMMemoryBufferRef *OutMemBuf) { + SmallString<0> CodeString; + raw_svector_ostream OStream(CodeString); + bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage); + + StringRef Data = OStream.str(); + *OutMemBuf = + LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); + return Result; +} diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp index c66495969b4e6..8ecc53e66d6d2 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 LLVMTargetMachine *TM) : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(), TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(), @@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() { } MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass( - const LLVMTargetMachine *TM) - : ImmutablePass(ID), MMI(TM) { - initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); -} - -MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass( - const LLVMTargetMachine *TM, MCContext *ExtContext) - : ImmutablePass(ID), MMI(TM, ExtContext) { + MachineModuleInfo &MMI) + : ImmutablePass(ID), MMI(MMI) { initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); } diff --git a/llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt b/llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt index f7a4610e0500f..958cf3138f24f 100644 --- a/llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt +++ b/llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt @@ -5,6 +5,7 @@ add_llvm_component_library(LLVMMCJIT intrinsics_gen LINK_COMPONENTS + CodeGen Core ExecutionEngine Object diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 4cce4a77b343f..399920f9d347c 100644 --- a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -8,6 +8,7 @@ #include "MCJIT.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/ExecutionEngine/GenericValue.h" #include "llvm/ExecutionEngine/JITEventListener.h" #include "llvm/ExecutionEngine/MCJIT.h" @@ -157,6 +158,9 @@ std::unique_ptr MCJIT::emitObject(Module *M) { // generateCodeForModule. legacy::PassManager PM; + auto *LLVMTM = static_cast(TM.get()); + MachineModuleInfo MMI(LLVMTM); + Ctx = &MMI.getContext(); // The RuntimeDyld will take ownership of this shortly SmallVector ObjBufferSV; @@ -164,7 +168,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 (LLVMTM->addPassesToEmitMC(PM, MMI, ObjStream, !getVerifyModules())) report_fatal_error("Target does not support MC emission!"); // Initialize passes. diff --git a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt index 5dfd621781e44..32f7cac0b3d1e 100644 --- a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt +++ b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt @@ -63,6 +63,7 @@ add_llvm_component_library(LLVMOrcJIT LINK_COMPONENTS BinaryFormat + CodeGen Core ExecutionEngine JITLink diff --git a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp index fad7428e1f906..e65d387b41fd2 100644 --- a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp @@ -9,6 +9,7 @@ #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/ExecutionEngine/ObjectCache.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" @@ -46,8 +47,8 @@ Expected SimpleCompiler::operator()(Module &M) { raw_svector_ostream ObjStream(ObjBufferSV); legacy::PassManager PM; - MCContext *Ctx; - if (TM.addPassesToEmitMC(PM, Ctx, ObjStream)) + MachineModuleInfo MMI(static_cast(&TM)); + if (TM.addPassesToEmitMC(PM, MMI, ObjStream)) return make_error("Target does not support MC emission", inconvertibleErrorCode()); PM.run(M); diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index 4e58cd369c3ac..85bba5178850e 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -20,6 +20,7 @@ #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/LLVMRemarkStreamer.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/PassManager.h" @@ -417,13 +418,14 @@ static void codegen(const Config &Conf, TargetMachine *TM, TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName; legacy::PassManager CodeGenPasses; + MachineModuleInfo MMI(static_cast(TM)); TargetLibraryInfoImpl TLII(Triple(Mod.getTargetTriple())); CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII)); CodeGenPasses.add( createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex)); if (Conf.PreCodeGenPassesHook) Conf.PreCodeGenPassesHook(CodeGenPasses); - if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, + if (TM->addPassesToEmitFile(CodeGenPasses, MMI, *Stream->OS, DwoOut ? &DwoOut->os() : nullptr, Conf.CGFileType)) report_fatal_error("Failed to setup codegen"); diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp index 76268c950cf58..fdc9794064953 100644 --- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp +++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp @@ -24,6 +24,7 @@ #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/Bitcode/BitcodeWriter.h" #include "llvm/Bitcode/BitcodeWriterPass.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/Config/llvm-config.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/DiagnosticPrinter.h" @@ -333,9 +334,11 @@ std::unique_ptr codegenModule(Module &TheModule, { raw_svector_ostream OS(OutputBuffer); legacy::PassManager PM; + MachineModuleInfo MMI(static_cast(&TM)); // Setup the codegen now. - if (TM.addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::ObjectFile, + if (TM.addPassesToEmitFile(PM, MMI, OS, nullptr, + CodeGenFileType::ObjectFile, /* DisableVerify */ true)) report_fatal_error("Failed to setup codegen"); diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h index 2b88da67a50f9..b6e5f3adb97ed 100644 --- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h +++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h @@ -52,8 +52,8 @@ class NVPTXTargetMachine : public LLVMTargetMachine { 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 &, MachineModuleInfo &, + raw_pwrite_stream &, bool = true) override { return true; } TargetLoweringObjectFile *getObjFileLowering() const override { diff --git a/llvm/lib/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp index d12fc65047d04..08fad95d18752 100644 --- a/llvm/lib/Target/TargetMachineC.cpp +++ b/llvm/lib/Target/TargetMachineC.cpp @@ -18,7 +18,6 @@ #include "llvm/IR/Module.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Support/CBindingWrapping.h" -#include "llvm/Support/FileSystem.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/CodeGenCWrappers.h" #include "llvm/Target/TargetMachine.h" @@ -287,68 +286,6 @@ LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) { return wrap(new DataLayout(unwrap(T)->createDataLayout())); } -static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, - raw_pwrite_stream &OS, - LLVMCodeGenFileType codegen, - char **ErrorMessage) { - TargetMachine* TM = unwrap(T); - Module* Mod = unwrap(M); - - legacy::PassManager pass; - - std::string error; - - Mod->setDataLayout(TM->createDataLayout()); - - CodeGenFileType ft; - switch (codegen) { - case LLVMAssemblyFile: - ft = CodeGenFileType::AssemblyFile; - break; - default: - ft = CodeGenFileType::ObjectFile; - break; - } - if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) { - error = "TargetMachine can't emit a file of this type"; - *ErrorMessage = strdup(error.c_str()); - return true; - } - - pass.run(*Mod); - - OS.flush(); - return false; -} - -LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, - const char *Filename, - LLVMCodeGenFileType codegen, - char **ErrorMessage) { - std::error_code EC; - raw_fd_ostream dest(Filename, EC, sys::fs::OF_None); - if (EC) { - *ErrorMessage = strdup(EC.message().c_str()); - return true; - } - bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage); - dest.flush(); - return Result; -} - -LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, - LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage, - LLVMMemoryBufferRef *OutMemBuf) { - SmallString<0> CodeString; - raw_svector_ostream OStream(CodeString); - bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage); - - StringRef Data = OStream.str(); - *OutMemBuf = - LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); - return Result; -} - char *LLVMGetDefaultTargetTriple(void) { return strdup(sys::getDefaultTargetTriple().c_str()); } diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp index 2c1901cdd49d8..758a6dad0faac 100644 --- a/llvm/tools/llc/llc.cpp +++ b/llvm/tools/llc/llc.cpp @@ -687,12 +687,12 @@ static int compileModule(char **argv, LLVMContext &Context) { const char *argv0 = argv[0]; LLVMTargetMachine &LLVMTM = static_cast(*Target); - MachineModuleInfoWrapperPass *MMIWP = - new MachineModuleInfoWrapperPass(&LLVMTM); + MachineModuleInfo MMI(&LLVMTM); // Construct a custom pass pipeline that starts after instruction // selection. if (!getRunPassNames().empty()) { + auto *MMIWP = new MachineModuleInfoWrapperPass(MMI); if (!MIR) { WithColor::warning(errs(), argv[0]) << "run-pass is for .mir file only.\n"; @@ -721,17 +721,16 @@ static int compileModule(char **argv, LLVMContext &Context) { TPC.setInitialized(); PM.add(createPrintMIRPass(*OS)); PM.add(createFreeMachineFunctionPass()); - } else if (Target->addPassesToEmitFile( - PM, *OS, DwoOut ? &DwoOut->os() : nullptr, - codegen::getFileType(), NoVerify, MMIWP)) { + } else if (Target->addPassesToEmitFile(PM, MMI, *OS, + DwoOut ? &DwoOut->os() : nullptr, + codegen::getFileType(), NoVerify)) { reportError("target does not support generation of this file type"); } const_cast(LLVMTM.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 92ab3a96d91e6..97307b70078ad 100644 --- a/llvm/tools/llvm-exegesis/lib/Assembler.cpp +++ b/llvm/tools/llvm-exegesis/lib/Assembler.cpp @@ -234,9 +234,9 @@ BitVector getFunctionReservedRegs(const TargetMachine &TM) { std::unique_ptr Module = createModule(Context, TM.createDataLayout()); // TODO: This only works for targets implementing LLVMTargetMachine. const LLVMTargetMachine &LLVMTM = static_cast(TM); - auto MMIWP = std::make_unique(&LLVMTM); - MachineFunction &MF = createVoidVoidPtrMachineFunction( - FunctionID, Module.get(), &MMIWP->getMMI()); + MachineModuleInfo MMI(&LLVMTM); + MachineFunction &MF = + createVoidVoidPtrMachineFunction(FunctionID, Module.get(), &MMI); // Saving reserved registers for client. return MF.getSubtarget().getRegisterInfo()->getReservedRegs(MF); } @@ -249,9 +249,9 @@ 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()); - MachineFunction &MF = createVoidVoidPtrMachineFunction( - FunctionID, Module.get(), &MMIWP.get()->getMMI()); + MachineModuleInfo MMI(TM.get()); + MachineFunction &MF = + createVoidVoidPtrMachineFunction(FunctionID, Module.get(), &MMI); MF.ensureAlignment(kFunctionAlignment); // We need to instruct the passes that we're done with SSA and virtual @@ -308,7 +308,7 @@ Error assembleToStream(const ExegesisTarget &ET, MF.getRegInfo().freezeReservedRegs(); // We create the pass manager, run the passes to populate AsmBuffer. - MCContext &MCContext = MMIWP->getMMI().getContext(); + MCContext &MCContext = MMI.getContext(); legacy::PassManager PM; TargetLibraryInfoImpl TLII(Triple(Module->getTargetTriple())); @@ -316,7 +316,7 @@ Error assembleToStream(const ExegesisTarget &ET, TargetPassConfig *TPC = TM->createPassConfig(PM); PM.add(TPC); - PM.add(MMIWP.release()); + PM.add(new llvm::MachineModuleInfoWrapperPass(MMI)); TPC->printAndVerify("MachineFunctionGenerator::assemble"); // Add target-specific passes. ET.addTargetSpecificPasses(PM); diff --git a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp index 742f7b94e116f..a5e86a1b2fd69 100644 --- a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp +++ b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp @@ -15,6 +15,7 @@ #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/Bitcode/BitcodeWriter.h" #include "llvm/CodeGen/CommandFlags.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/FuzzMutate/FuzzerCLI.h" #include "llvm/FuzzMutate/IRMutator.h" #include "llvm/FuzzMutate/Operations.h" @@ -96,10 +97,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { // Build up a PM to do instruction selection. legacy::PassManager PM; + MachineModuleInfo MMI(static_cast(TM.get())); TargetLibraryInfoImpl TLII(TM->getTargetTriple()); PM.add(new TargetLibraryInfoWrapperPass(TLII)); raw_null_ostream OS; - TM->addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::Null); + TM->addPassesToEmitFile(PM, MMI, OS, nullptr, CodeGenFileType::Null); PM.run(*M); return 0; diff --git a/llvm/unittests/CodeGen/AMDGPUMetadataTest.cpp b/llvm/unittests/CodeGen/AMDGPUMetadataTest.cpp index ceea57fa10cce..c4a8d370a5d3a 100644 --- a/llvm/unittests/CodeGen/AMDGPUMetadataTest.cpp +++ b/llvm/unittests/CodeGen/AMDGPUMetadataTest.cpp @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/MC/TargetRegistry.h" @@ -68,9 +69,10 @@ class AMDGPUSelectionDAGTest : public testing::Test { M->setDataLayout(TM->createDataLayout()); legacy::PassManager PM; + MachineModuleInfo MMI(TM.get()); PM.add(new AddMetadataPass(PalMDString)); raw_svector_ostream OutStream(Elf); - if (TM->addPassesToEmitFile(PM, OutStream, nullptr, + if (TM->addPassesToEmitFile(PM, MMI, OutStream, nullptr, CodeGenFileType::ObjectFile)) report_fatal_error("Target machine cannot emit a file of this type"); diff --git a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp index 1f3d7a55ee854..0a021ee215e0e 100644 --- a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp +++ b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp @@ -400,7 +400,8 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase { AP->addAsmPrinterHandler(std::make_unique(*this)); LLVMTargetMachine *LLVMTM = static_cast(&AP->TM); legacy::PassManager PM; - PM.add(new MachineModuleInfoWrapperPass(LLVMTM)); + MachineModuleInfo MMI(LLVMTM); + PM.add(new MachineModuleInfoWrapperPass(MMI)); PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP LLVMContext Context; std::unique_ptr M(new Module("TestModule", Context)); @@ -450,7 +451,8 @@ class AsmPrinterDebugHandlerTest : public AsmPrinterFixtureBase { AP->addDebugHandler(std::make_unique(*this, AP)); LLVMTargetMachine *LLVMTM = static_cast(&AP->TM); legacy::PassManager PM; - PM.add(new MachineModuleInfoWrapperPass(LLVMTM)); + MachineModuleInfo MMI(LLVMTM); + PM.add(new MachineModuleInfoWrapperPass(MMI)); PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP LLVMContext Context; std::unique_ptr M(new Module("TestModule", Context)); diff --git a/llvm/unittests/MI/LiveIntervalTest.cpp b/llvm/unittests/MI/LiveIntervalTest.cpp index f910e8e1f2c8f..de4a2b4ba1b06 100644 --- a/llvm/unittests/MI/LiveIntervalTest.cpp +++ b/llvm/unittests/MI/LiveIntervalTest.cpp @@ -54,8 +54,7 @@ std::unique_ptr createTargetMachine() { std::nullopt, CodeGenOptLevel::Aggressive))); } -std::unique_ptr parseMIR(LLVMContext &Context, - legacy::PassManagerBase &PM, +std::unique_ptr parseMIR(LLVMContext &Context, MachineModuleInfo &MMI, std::unique_ptr &MIR, const LLVMTargetMachine &TM, StringRef MIRCode) { @@ -71,10 +70,8 @@ std::unique_ptr parseMIR(LLVMContext &Context, M->setDataLayout(TM.createDataLayout()); - MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(&TM); - if (MIR->parseMachineFunctions(*M, MMIWP->getMMI())) + if (MIR->parseMachineFunctions(*M, MMI)) return nullptr; - PM.add(MMIWP); return M; } @@ -212,10 +209,13 @@ static void doTest(StringRef MIRFunc, return; legacy::PassManager PM; + MachineModuleInfo MMI(TM.get()); std::unique_ptr MIR; - std::unique_ptr M = parseMIR(Context, PM, MIR, *TM, MIRFunc); + std::unique_ptr M = parseMIR(Context, MMI, MIR, *TM, MIRFunc); ASSERT_TRUE(M); + PM.add(new MachineModuleInfoWrapperPass(MMI)); + PM.add(new TestPassT(T, ShouldPass)); PM.run(*M); diff --git a/mlir/lib/Target/LLVM/ModuleToObject.cpp b/mlir/lib/Target/LLVM/ModuleToObject.cpp index 77391341adaad..327268934b657 100644 --- a/mlir/lib/Target/LLVM/ModuleToObject.cpp +++ b/mlir/lib/Target/LLVM/ModuleToObject.cpp @@ -20,6 +20,7 @@ #include "mlir/Target/LLVMIR/ModuleTranslation.h" #include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IRReader/IRReader.h" #include "llvm/Linker/Linker.h" @@ -175,8 +176,11 @@ ModuleToObject::translateToISA(llvm::Module &llvmModule, { // Drop pstream after this to prevent the ISA from being stuck buffering llvm::buffer_ostream pstream(stream); llvm::legacy::PassManager codegenPasses; + llvm::MachineModuleInfo machineModuleInfo( + static_cast(&targetMachine)); - if (targetMachine.addPassesToEmitFile(codegenPasses, pstream, nullptr, + if (targetMachine.addPassesToEmitFile(codegenPasses, machineModuleInfo, + pstream, nullptr, llvm::CodeGenFileType::AssemblyFile)) return std::nullopt;