https://github.com/matinraayai updated https://github.com/llvm/llvm-project/pull/110443
>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:54:50 -0400 Subject: [PATCH 01/19] Made MMIWP not have control over the lifetime of MMI. --- llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++++++------ llvm/lib/CodeGen/MachineModuleInfo.cpp | 21 ++----------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h b/llvm/include/llvm/CodeGen/MachineModuleInfo.h index 310cc4b2abb772..8c81df23c29984 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/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp index c66495969b4e67..8ecc53e66d6d25 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()); } >From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:28:58 -0400 Subject: [PATCH 02/19] Moved the MC pass creation functions from TargetMachine to LLVMTargetMachine. --- llvm/include/llvm/Target/TargetMachine.h | 51 ++++++------------------ llvm/lib/CodeGen/LLVMTargetMachine.cpp | 23 +++++------ 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h index c3e9d41315f617..ed04da6dcd9558 100644 --- a/llvm/include/llvm/Target/TargetMachine.h +++ b/llvm/include/llvm/Target/TargetMachine.h @@ -375,31 +375,6 @@ class TargetMachine { /// with the new pass manager. Only affects the "default" AAManager. 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. - /// 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) { - 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) { - return true; - } - /// True if subtarget inserts the final scheduling pass on its own. /// /// Branch relaxation, which must happen after block placement, can @@ -460,14 +435,14 @@ 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. + virtual bool addPassesToEmitFile(PassManagerBase &PM, MachineModuleInfo &MMI, + raw_pwrite_stream &Out, + raw_pwrite_stream *DwoOut, + CodeGenFileType FileType, + bool DisableVerify = true); virtual Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, @@ -478,12 +453,10 @@ 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, - raw_pwrite_stream &Out, - bool DisableVerify = true) override; + /// the MCJIT. This method returns true if machine code is not supported. + virtual bool addPassesToEmitMC(PassManagerBase &PM, MachineModuleInfo &MMI, + raw_pwrite_stream &Out, + bool DisableVerify = true); /// Returns true if the target is expected to pass all machine verifier /// checks. This is a stopgap measure to fix targets one by one. We will diff --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp b/llvm/lib/CodeGen/LLVMTargetMachine.cpp index ea36fedef93ac6..6a005739d8dae5 100644 --- a/llvm/lib/CodeGen/LLVMTargetMachine.cpp +++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp @@ -209,12 +209,10 @@ Expected<std::unique_ptr<MCStreamer>> 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<MCCodeEmitter> 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<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( - T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out), + T, Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out), std::move(MCE), STI)); // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. >From ed403875e586430d61423dfa2031ad9c865fc76f Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:35:37 -0400 Subject: [PATCH 03/19] Migrated BackendUtil.cpp to the new LLVMTargetMachine code emission API. --- clang/lib/CodeGen/BackendUtil.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 62c6a57e8b7c80..a6abbc492350c3 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<llvm::ToolOutputFile> openOutputFile(StringRef Path) { std::error_code EC; @@ -223,7 +225,7 @@ class EmitAssemblyHelper { BuryPointer(std::move(TM)); } - std::unique_ptr<TargetMachine> TM; + std::unique_ptr<LLVMTargetMachine> TM; // Emit output using the new pass manager for the optimization pipeline. void EmitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS, @@ -607,12 +609,13 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts)) return; - TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, - Options, RM, CM, OptLevel)); + TM.reset(static_cast<LLVMTargetMachine *>(TheTarget->createTargetMachine( + Triple, TargetOpts.CPU, FeaturesStr, Options, RM, CM, OptLevel))); TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold); } 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<MachineModuleInfo> MMI; // Append any output we need to the pass manager. switch (Action) { @@ -1175,7 +1179,8 @@ void EmitAssemblyHelper::RunCodegenPipeline( if (!DwoOS) return; } - if (!AddEmitPasses(CodeGenPasses, Action, *OS, + MMI = std::make_unique<MachineModuleInfo>(TM.get()); + if (!AddEmitPasses(CodeGenPasses, *MMI, Action, *OS, DwoOS ? &DwoOS->os() : nullptr)) // FIXME: Should we handle this error differently? return; >From 75dd38a6e4d80b06cde3ee4c3b5f475e8a2932c4 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:46:11 -0400 Subject: [PATCH 04/19] Migrated DeviceOffload.cpp to the new LLVMTargetMachine code emission API. --- clang/lib/Interpreter/DeviceOffload.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp index 1999d63d1aa047..7b072832717dd3 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<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() { return llvm::make_error<llvm::StringError>(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_); + llvm::LLVMTargetMachine *TargetMachine = + static_cast<llvm::LLVMTargetMachine *>(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<llvm::StringError>( "NVPTX backend cannot produce PTX code.", >From 00be9bf9f645c031ad38b85c1cc2d1ca35ebed0e Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:48:30 -0400 Subject: [PATCH 05/19] Migrated Wasm.cpp to the new LLVMTargetMachine code emission API. --- clang/lib/Interpreter/DeviceOffload.cpp | 2 +- clang/lib/Interpreter/Wasm.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp index 7b072832717dd3..fd4a31ef8135e3 100644 --- a/clang/lib/Interpreter/DeviceOffload.cpp +++ b/clang/lib/Interpreter/DeviceOffload.cpp @@ -83,7 +83,7 @@ llvm::Expected<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() { return llvm::make_error<llvm::StringError>(std::move(Error), std::error_code()); llvm::TargetOptions TO = llvm::TargetOptions(); - llvm::LLVMTargetMachine *TargetMachine = + auto *TargetMachine = static_cast<llvm::LLVMTargetMachine *>(Target->createTargetMachine( PTU.TheModule->getTargetTriple(), TargetOpts.CPU, "", TO, llvm::Reloc::Model::PIC_)); diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp index 1001410aa0f279..863fab9fd6fa3b 100644 --- a/clang/lib/Interpreter/Wasm.cpp +++ b/clang/lib/Interpreter/Wasm.cpp @@ -13,6 +13,7 @@ #include "Wasm.h" #include "IncrementalExecutor.h" +#include <llvm/CodeGen/MachineModuleInfo.h> #include <llvm/IR/LegacyPassManager.h> #include <llvm/IR/Module.h> #include <llvm/MC/TargetRegistry.h> @@ -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<llvm::LLVMTargetMachine *>( + 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<llvm::StringError>( "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode()); >From 7bee39f22e6d44912af88005766c16c55a60494b Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:52:21 -0400 Subject: [PATCH 06/19] Migrated ClangLinkerWrapper.cpp to the new LLVMTargetMachine code emission API. --- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp index 9fea1fdcd5fb46..f1baa98ec5fe94 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<StringRef> compileModule(Module &M, OffloadKind Kind) { codegen::InitTargetOptionsFromCodeGenFlags(Triple(M.getTargetTriple())); StringRef CPU = ""; StringRef Features = ""; - std::unique_ptr<TargetMachine> TM( + std::unique_ptr<LLVMTargetMachine> TM(static_cast<LLVMTargetMachine *>( 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<StringRef> compileModule(Module &M, OffloadKind Kind) { auto OS = std::make_unique<llvm::raw_fd_ostream>(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); >From d9fcf4881c092bc34f614e77960c66af4bcc29dd Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:58:27 -0400 Subject: [PATCH 07/19] Migrated FrontendActions.cpp to the new LLVMTargetMachine code emission API. --- flang/lib/Frontend/FrontendActions.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index 4a52edc436e0ed..eaf42b993aaddb 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" @@ -919,7 +920,7 @@ getOutputStream(CompilerInstance &ci, llvm::StringRef inFile, /// \param [in] codeGenOpts options configuring codegen pipeline /// \param [out] os Output stream to emit the generated code to static void generateMachineCodeOrAssemblyImpl(clang::DiagnosticsEngine &diags, - llvm::TargetMachine &tm, + llvm::LLVMTargetMachine &tm, BackendActionTy act, llvm::Module &llvmModule, const CodeGenOptions &codeGenOpts, @@ -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(&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"); @@ -1369,7 +1371,8 @@ void CodeGenAction::executeAction() { if (action == BackendActionTy::Backend_EmitAssembly || action == BackendActionTy::Backend_EmitObj) { generateMachineCodeOrAssemblyImpl( - diags, targetMachine, action, *llvmModule, codeGenOpts, + diags, *static_cast<llvm::LLVMTargetMachine *>(&targetMachine), action, + *llvmModule, codeGenOpts, ci.isOutputStreamNull() ? *os : ci.getOutputStream()); return; } >From 631cbb219c78aa7d38a5f61a49d27c07560395b5 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:11:28 -0400 Subject: [PATCH 08/19] Migrated MCJIT.cpp to the new LLVMTargetMachine code emission API. --- llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 4cce4a77b343f0..78b7fcedcda618 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<MemoryBuffer> MCJIT::emitObject(Module *M) { // generateCodeForModule. legacy::PassManager PM; + auto * LLVMTM = static_cast<LLVMTargetMachine*>(TM.get()); + MachineModuleInfo MMI(LLVMTM); + Ctx = &MMI.getContext(); // The RuntimeDyld will take ownership of this shortly SmallVector<char, 4096> ObjBufferSV; @@ -164,7 +168,7 @@ std::unique_ptr<MemoryBuffer> 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. >From e8e81db1ee8bd730f6769073c66eb529bdff3972 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:14:28 -0400 Subject: [PATCH 09/19] Migrated CompileUtils.cpp to the new LLVMTargetMachine code emission API. --- llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp index fad7428e1f906c..b696518813bc38 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,9 @@ Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) { raw_svector_ostream ObjStream(ObjBufferSV); legacy::PassManager PM; - MCContext *Ctx; - if (TM.addPassesToEmitMC(PM, Ctx, ObjStream)) + auto *LLVMTM = static_cast<LLVMTargetMachine *>(&TM); + MachineModuleInfo MMI(LLVMTM); + if (LLVMTM->addPassesToEmitMC(PM, MMI, ObjStream)) return make_error<StringError>("Target does not support MC emission", inconvertibleErrorCode()); PM.run(M); >From 4cfc73eb39b076c9a26ebe9a263ef8c5634998a3 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:36:47 -0400 Subject: [PATCH 10/19] Moved the MC emission APIs to TargetMachine again to keep the PR changes to a minimum. --- clang/lib/CodeGen/BackendUtil.cpp | 9 ++--- flang/lib/Frontend/FrontendActions.cpp | 7 ++-- llvm/include/llvm/Target/TargetMachine.h | 34 ++++++++++++++----- llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp | 5 ++- llvm/lib/LTO/LTOBackend.cpp | 1 + 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index a6abbc492350c3..34e58c1f8cd93c 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -225,7 +225,7 @@ class EmitAssemblyHelper { BuryPointer(std::move(TM)); } - std::unique_ptr<LLVMTargetMachine> TM; + std::unique_ptr<TargetMachine> TM; // Emit output using the new pass manager for the optimization pipeline. void EmitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS, @@ -609,8 +609,8 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts)) return; - TM.reset(static_cast<LLVMTargetMachine *>(TheTarget->createTargetMachine( - Triple, TargetOpts.CPU, FeaturesStr, Options, RM, CM, OptLevel))); + TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, + Options, RM, CM, OptLevel)); TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold); } @@ -1179,7 +1179,8 @@ void EmitAssemblyHelper::RunCodegenPipeline( if (!DwoOS) return; } - MMI = std::make_unique<MachineModuleInfo>(TM.get()); + MMI = std::make_unique<MachineModuleInfo>( + static_cast<LLVMTargetMachine *>(TM.get())); if (!AddEmitPasses(CodeGenPasses, *MMI, Action, *OS, DwoOS ? &DwoOS->os() : nullptr)) // FIXME: Should we handle this error differently? diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index eaf42b993aaddb..64aba542b0d1f2 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -920,7 +920,7 @@ getOutputStream(CompilerInstance &ci, llvm::StringRef inFile, /// \param [in] codeGenOpts options configuring codegen pipeline /// \param [out] os Output stream to emit the generated code to static void generateMachineCodeOrAssemblyImpl(clang::DiagnosticsEngine &diags, - llvm::LLVMTargetMachine &tm, + llvm::TargetMachine &tm, BackendActionTy act, llvm::Module &llvmModule, const CodeGenOptions &codeGenOpts, @@ -933,7 +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(&tm); + llvm::MachineModuleInfo mmi(static_cast<llvm::LLVMTargetMachine *>(&tm)); codeGenPasses.add( createTargetTransformInfoWrapperPass(tm.getTargetIRAnalysis())); @@ -1371,8 +1371,7 @@ void CodeGenAction::executeAction() { if (action == BackendActionTy::Backend_EmitAssembly || action == BackendActionTy::Backend_EmitObj) { generateMachineCodeOrAssemblyImpl( - diags, *static_cast<llvm::LLVMTargetMachine *>(&targetMachine), action, - *llvmModule, codeGenOpts, + diags, targetMachine, action, *llvmModule, codeGenOpts, ci.isOutputStreamNull() ? *os : ci.getOutputStream()); return; } diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h index ed04da6dcd9558..267971cd89a5d5 100644 --- a/llvm/include/llvm/Target/TargetMachine.h +++ b/llvm/include/llvm/Target/TargetMachine.h @@ -375,6 +375,25 @@ class TargetMachine { /// with the new pass manager. Only affects the "default" AAManager. 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. + /// This method should return true if emission of this file type is not + /// supported, or false on success. + 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. + virtual bool addPassesToEmitMC(PassManagerBase &, MachineModuleInfo &, + raw_pwrite_stream &, + bool /*DisableVerify*/ = true) { + return true; + } + /// True if subtarget inserts the final scheduling pass on its own. /// /// Branch relaxation, which must happen after block placement, can @@ -438,11 +457,10 @@ class LLVMTargetMachine : public TargetMachine { /// 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. - virtual bool addPassesToEmitFile(PassManagerBase &PM, MachineModuleInfo &MMI, - raw_pwrite_stream &Out, - raw_pwrite_stream *DwoOut, - CodeGenFileType FileType, - bool DisableVerify = true); + 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, @@ -454,9 +472,9 @@ 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. - virtual bool addPassesToEmitMC(PassManagerBase &PM, MachineModuleInfo &MMI, - raw_pwrite_stream &Out, - bool DisableVerify = true); + bool addPassesToEmitMC(PassManagerBase &PM, MachineModuleInfo &MMI, + raw_pwrite_stream &Out, + bool DisableVerify = true) override; /// Returns true if the target is expected to pass all machine verifier /// checks. This is a stopgap measure to fix targets one by one. We will diff --git a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp index b696518813bc38..e65d387b41fd21 100644 --- a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp @@ -47,9 +47,8 @@ Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) { raw_svector_ostream ObjStream(ObjBufferSV); legacy::PassManager PM; - auto *LLVMTM = static_cast<LLVMTargetMachine *>(&TM); - MachineModuleInfo MMI(LLVMTM); - if (LLVMTM->addPassesToEmitMC(PM, MMI, ObjStream)) + MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(&TM)); + if (TM.addPassesToEmitMC(PM, MMI, ObjStream)) return make_error<StringError>("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 4e58cd369c3ac9..d739334c733088 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" >From 4ca0afb9774a3f71c4013aee8b3cf0b84dd42b78 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:38:34 -0400 Subject: [PATCH 11/19] Migrated LTOBackend.cpp to use the new TargetMachine MC emission API. --- llvm/lib/LTO/LTOBackend.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index d739334c733088..15b5daea7f11a6 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -418,13 +418,14 @@ static void codegen(const Config &Conf, TargetMachine *TM, TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName; legacy::PassManager CodeGenPasses; + MachineModuleInfo MMI(static_cast<LLVMTargetMachine*>(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"); >From b33936cc76685d69c2b1e83e344abdb3fb185d24 Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:40:18 -0400 Subject: [PATCH 12/19] Migrated ThinLTOCodeGenerator.cpp to use the new TargetMachine MC emission API. --- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp index 76268c950cf581..4b522a4f3cbf4a 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<MemoryBuffer> codegenModule(Module &TheModule, { raw_svector_ostream OS(OutputBuffer); legacy::PassManager PM; + MachineModuleInfo MMI(static_cast<LLVMTargetMachine*>(&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"); >From e945366963b29958deb9db553589ab40ab2aa80f Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:41:31 -0400 Subject: [PATCH 13/19] Migrated NVPTXTargetMachine.h to use the new TargetMachine MC emission API. --- llvm/lib/Target/NVPTX/NVPTXTargetMachine.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h index 2b88da67a50f95..b6e5f3adb97ed9 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 { >From 737685e4e09c3be8a56566e436a7ae28771e2dcc Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:24:52 -0400 Subject: [PATCH 14/19] Migrated the remaining test files to the new MMIWP constructor. --- llvm/lib/Target/TargetMachineC.cpp | 4 +++- llvm/tools/llc/llc.cpp | 14 +++++++------- llvm/tools/llvm-exegesis/lib/Assembler.cpp | 12 ++++++------ llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp | 4 +++- llvm/unittests/CodeGen/AMDGPUMetadataTest.cpp | 4 +++- llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp | 6 ++++-- llvm/unittests/MI/LiveIntervalTest.cpp | 11 ++++++----- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/llvm/lib/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp index d12fc65047d043..5a2918827a6255 100644 --- a/llvm/lib/Target/TargetMachineC.cpp +++ b/llvm/lib/Target/TargetMachineC.cpp @@ -13,6 +13,7 @@ #include "llvm-c/Core.h" #include "llvm-c/TargetMachine.h" #include "llvm/Analysis/TargetTransformInfo.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" @@ -295,6 +296,7 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, Module* Mod = unwrap(M); legacy::PassManager pass; + MachineModuleInfo MMI(static_cast<LLVMTargetMachine*>(TM)); std::string error; @@ -309,7 +311,7 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, ft = CodeGenFileType::ObjectFile; break; } - if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) { + 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; diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp index 2c1901cdd49d8b..21c492c87e2e87 100644 --- a/llvm/tools/llc/llc.cpp +++ b/llvm/tools/llc/llc.cpp @@ -687,12 +687,13 @@ static int compileModule(char **argv, LLVMContext &Context) { const char *argv0 = argv[0]; LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine &>(*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"; @@ -722,16 +723,15 @@ static int compileModule(char **argv, LLVMContext &Context) { PM.add(createPrintMIRPass(*OS)); PM.add(createFreeMachineFunctionPass()); } else if (Target->addPassesToEmitFile( - PM, *OS, DwoOut ? &DwoOut->os() : nullptr, - codegen::getFileType(), NoVerify, MMIWP)) { + PM, MMI, *OS, DwoOut ? &DwoOut->os() : nullptr, + codegen::getFileType(), NoVerify)) { reportError("target does not support generation of this file type"); } const_cast<TargetLoweringObjectFile *>(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 92ab3a96d91e6b..4472186d6a55b9 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> Module = createModule(Context, TM.createDataLayout()); // TODO: This only works for targets implementing LLVMTargetMachine. const LLVMTargetMachine &LLVMTM = static_cast<const LLVMTargetMachine &>(TM); - auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(&LLVMTM); + MachineModuleInfo MMI(&LLVMTM); MachineFunction &MF = createVoidVoidPtrMachineFunction( - FunctionID, Module.get(), &MMIWP->getMMI()); + 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<LLVMContext>(); std::unique_ptr<Module> Module = createModule(Context, TM->createDataLayout()); - auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(TM.get()); + MachineModuleInfo MMI(TM.get()); MachineFunction &MF = createVoidVoidPtrMachineFunction( - FunctionID, Module.get(), &MMIWP.get()->getMMI()); + 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 742f7b94e116f3..63766f54900296 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<LLVMTargetMachine*>(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 ceea57fa10cce4..c4a8d370a5d3ad 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 1f3d7a55ee8542..7be667b5c76195 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<TestHandler>(*this)); LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&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<Module> M(new Module("TestModule", Context)); @@ -450,7 +451,8 @@ class AsmPrinterDebugHandlerTest : public AsmPrinterFixtureBase { AP->addDebugHandler(std::make_unique<TestDebugHandler>(*this, AP)); LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&AP->TM); legacy::PassManager PM; - PM.add(new MachineModuleInfoWrapperPass(LLVMTM)); + MachineModuleInfo MMI; + PM.add(new MachineModuleInfoWrapperPass(MMI)); PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP LLVMContext Context; std::unique_ptr<Module> M(new Module("TestModule", Context)); diff --git a/llvm/unittests/MI/LiveIntervalTest.cpp b/llvm/unittests/MI/LiveIntervalTest.cpp index f910e8e1f2c8fb..5ebae2ced866bc 100644 --- a/llvm/unittests/MI/LiveIntervalTest.cpp +++ b/llvm/unittests/MI/LiveIntervalTest.cpp @@ -55,7 +55,7 @@ std::unique_ptr<LLVMTargetMachine> createTargetMachine() { } std::unique_ptr<Module> parseMIR(LLVMContext &Context, - legacy::PassManagerBase &PM, + MachineModuleInfo &MMI, std::unique_ptr<MIRParser> &MIR, const LLVMTargetMachine &TM, StringRef MIRCode) { @@ -71,10 +71,8 @@ std::unique_ptr<Module> 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 +210,13 @@ static void doTest(StringRef MIRFunc, return; legacy::PassManager PM; + MachineModuleInfo MMI(TM->get()); std::unique_ptr<MIRParser> MIR; - std::unique_ptr<Module> M = parseMIR(Context, PM, MIR, *TM, MIRFunc); + std::unique_ptr<Module> M = parseMIR(Context, MMI, MIR, *TM, MIRFunc); ASSERT_TRUE(M); + PM.add(new MachineModuleInfoWrapperPass(MMI)); + PM.add(new TestPassT<AnalysisType>(T, ShouldPass)); PM.run(*M); >From 0a6575c62f364be4e46c9fe8bff5c494ab21e60d Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:54:20 -0400 Subject: [PATCH 15/19] clang tidy. --- llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp | 2 +- llvm/lib/LTO/LTOBackend.cpp | 2 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 2 +- llvm/lib/Target/TargetMachineC.cpp | 2 +- llvm/tools/llc/llc.cpp | 9 ++++----- llvm/tools/llvm-exegesis/lib/Assembler.cpp | 8 ++++---- llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp | 2 +- llvm/unittests/MI/LiveIntervalTest.cpp | 3 +-- 8 files changed, 14 insertions(+), 16 deletions(-) diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 78b7fcedcda618..399920f9d347c3 100644 --- a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -158,7 +158,7 @@ std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) { // generateCodeForModule. legacy::PassManager PM; - auto * LLVMTM = static_cast<LLVMTargetMachine*>(TM.get()); + auto *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get()); MachineModuleInfo MMI(LLVMTM); Ctx = &MMI.getContext(); diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index 15b5daea7f11a6..85bba5178850e0 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -418,7 +418,7 @@ static void codegen(const Config &Conf, TargetMachine *TM, TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName; legacy::PassManager CodeGenPasses; - MachineModuleInfo MMI(static_cast<LLVMTargetMachine*>(TM)); + MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(TM)); TargetLibraryInfoImpl TLII(Triple(Mod.getTargetTriple())); CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII)); CodeGenPasses.add( diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp index 4b522a4f3cbf4a..fdc97940649531 100644 --- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp +++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp @@ -334,7 +334,7 @@ std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule, { raw_svector_ostream OS(OutputBuffer); legacy::PassManager PM; - MachineModuleInfo MMI(static_cast<LLVMTargetMachine*>(&TM)); + MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(&TM)); // Setup the codegen now. if (TM.addPassesToEmitFile(PM, MMI, OS, nullptr, diff --git a/llvm/lib/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp index 5a2918827a6255..bae4f3e1da5aba 100644 --- a/llvm/lib/Target/TargetMachineC.cpp +++ b/llvm/lib/Target/TargetMachineC.cpp @@ -296,7 +296,7 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, Module* Mod = unwrap(M); legacy::PassManager pass; - MachineModuleInfo MMI(static_cast<LLVMTargetMachine*>(TM)); + MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(TM)); std::string error; diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp index 21c492c87e2e87..758a6dad0faac2 100644 --- a/llvm/tools/llc/llc.cpp +++ b/llvm/tools/llc/llc.cpp @@ -692,8 +692,7 @@ static int compileModule(char **argv, LLVMContext &Context) { // Construct a custom pass pipeline that starts after instruction // selection. if (!getRunPassNames().empty()) { - auto *MMIWP = - new MachineModuleInfoWrapperPass(MMI); + auto *MMIWP = new MachineModuleInfoWrapperPass(MMI); if (!MIR) { WithColor::warning(errs(), argv[0]) << "run-pass is for .mir file only.\n"; @@ -722,9 +721,9 @@ static int compileModule(char **argv, LLVMContext &Context) { TPC.setInitialized(); PM.add(createPrintMIRPass(*OS)); PM.add(createFreeMachineFunctionPass()); - } else if (Target->addPassesToEmitFile( - PM, MMI, *OS, DwoOut ? &DwoOut->os() : nullptr, - codegen::getFileType(), NoVerify)) { + } else if (Target->addPassesToEmitFile(PM, MMI, *OS, + DwoOut ? &DwoOut->os() : nullptr, + codegen::getFileType(), NoVerify)) { reportError("target does not support generation of this file type"); } diff --git a/llvm/tools/llvm-exegesis/lib/Assembler.cpp b/llvm/tools/llvm-exegesis/lib/Assembler.cpp index 4472186d6a55b9..97307b70078ad8 100644 --- a/llvm/tools/llvm-exegesis/lib/Assembler.cpp +++ b/llvm/tools/llvm-exegesis/lib/Assembler.cpp @@ -235,8 +235,8 @@ BitVector getFunctionReservedRegs(const TargetMachine &TM) { // TODO: This only works for targets implementing LLVMTargetMachine. const LLVMTargetMachine &LLVMTM = static_cast<const LLVMTargetMachine &>(TM); MachineModuleInfo MMI(&LLVMTM); - MachineFunction &MF = createVoidVoidPtrMachineFunction( - FunctionID, Module.get(), &MMI); + MachineFunction &MF = + createVoidVoidPtrMachineFunction(FunctionID, Module.get(), &MMI); // Saving reserved registers for client. return MF.getSubtarget().getRegisterInfo()->getReservedRegs(MF); } @@ -250,8 +250,8 @@ Error assembleToStream(const ExegesisTarget &ET, std::unique_ptr<Module> Module = createModule(Context, TM->createDataLayout()); MachineModuleInfo MMI(TM.get()); - MachineFunction &MF = createVoidVoidPtrMachineFunction( - FunctionID, Module.get(), &MMI); + MachineFunction &MF = + createVoidVoidPtrMachineFunction(FunctionID, Module.get(), &MMI); MF.ensureAlignment(kFunctionAlignment); // We need to instruct the passes that we're done with SSA and virtual diff --git a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp index 63766f54900296..a5e86a1b2fd693 100644 --- a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp +++ b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp @@ -97,7 +97,7 @@ 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<LLVMTargetMachine*>(TM.get())); + MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(TM.get())); TargetLibraryInfoImpl TLII(TM->getTargetTriple()); PM.add(new TargetLibraryInfoWrapperPass(TLII)); raw_null_ostream OS; diff --git a/llvm/unittests/MI/LiveIntervalTest.cpp b/llvm/unittests/MI/LiveIntervalTest.cpp index 5ebae2ced866bc..c1b3ce1c560ae6 100644 --- a/llvm/unittests/MI/LiveIntervalTest.cpp +++ b/llvm/unittests/MI/LiveIntervalTest.cpp @@ -54,8 +54,7 @@ std::unique_ptr<LLVMTargetMachine> createTargetMachine() { std::nullopt, CodeGenOptLevel::Aggressive))); } -std::unique_ptr<Module> parseMIR(LLVMContext &Context, - MachineModuleInfo &MMI, +std::unique_ptr<Module> parseMIR(LLVMContext &Context, MachineModuleInfo &MMI, std::unique_ptr<MIRParser> &MIR, const LLVMTargetMachine &TM, StringRef MIRCode) { >From 753d99a1b9d9df2d9a74384b14834403230a6a4a Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 20:54:07 -0400 Subject: [PATCH 16/19] Fixed compilation issue. --- llvm/examples/Kaleidoscope/Chapter8/toy.cpp | 6 +++++- llvm/include/llvm/Target/TargetMachine.h | 2 +- llvm/unittests/MI/LiveIntervalTest.cpp | 2 +- mlir/lib/Target/LLVM/ModuleToObject.cpp | 6 +++++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/llvm/examples/Kaleidoscope/Chapter8/toy.cpp b/llvm/examples/Kaleidoscope/Chapter8/toy.cpp index ae2f9c7059e5fb..45e7b27ef241b5 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<LLVMTargetMachine *>(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/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h index 267971cd89a5d5..69aff06032502e 100644 --- a/llvm/include/llvm/Target/TargetMachine.h +++ b/llvm/include/llvm/Target/TargetMachine.h @@ -34,7 +34,7 @@ using ModulePassManager = PassManager<Module>; class Function; class GlobalValue; -class MachineModuleInfoWrapperPass; +class MachineModuleInfo; class Mangler; class MCAsmInfo; class MCContext; diff --git a/llvm/unittests/MI/LiveIntervalTest.cpp b/llvm/unittests/MI/LiveIntervalTest.cpp index c1b3ce1c560ae6..de4a2b4ba1b068 100644 --- a/llvm/unittests/MI/LiveIntervalTest.cpp +++ b/llvm/unittests/MI/LiveIntervalTest.cpp @@ -209,7 +209,7 @@ static void doTest(StringRef MIRFunc, return; legacy::PassManager PM; - MachineModuleInfo MMI(TM->get()); + MachineModuleInfo MMI(TM.get()); std::unique_ptr<MIRParser> MIR; std::unique_ptr<Module> M = parseMIR(Context, MMI, MIR, *TM, MIRFunc); ASSERT_TRUE(M); diff --git a/mlir/lib/Target/LLVM/ModuleToObject.cpp b/mlir/lib/Target/LLVM/ModuleToObject.cpp index 77391341adaad2..327268934b6573 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<llvm::LLVMTargetMachine *>(&targetMachine)); - if (targetMachine.addPassesToEmitFile(codegenPasses, pstream, nullptr, + if (targetMachine.addPassesToEmitFile(codegenPasses, machineModuleInfo, + pstream, nullptr, llvm::CodeGenFileType::AssemblyFile)) return std::nullopt; >From 9ba79131028d737be5aadfa17dca3f0795d7ee5c Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Sun, 29 Sep 2024 22:45:57 -0400 Subject: [PATCH 17/19] Fixed default construction of MMI in the AsmPrinterDwarfTest. --- llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp index 7be667b5c76195..0a021ee215e0e7 100644 --- a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp +++ b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp @@ -451,7 +451,7 @@ class AsmPrinterDebugHandlerTest : public AsmPrinterFixtureBase { AP->addDebugHandler(std::make_unique<TestDebugHandler>(*this, AP)); LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&AP->TM); legacy::PassManager PM; - MachineModuleInfo MMI; + MachineModuleInfo MMI(LLVMTM); PM.add(new MachineModuleInfoWrapperPass(MMI)); PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP LLVMContext Context; >From ead138ad0e17cf51334763afb94bc52215d7f70f Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:39:47 -0400 Subject: [PATCH 18/19] Create LLVMTargetMachineC.cpp to implement the MC-emission related things in the C-API + linked any libraries that depends on MC file emission to CodeGen. --- llvm/lib/CodeGen/CMakeLists.txt | 1 + llvm/lib/CodeGen/LLVMTargetMachineC.cpp | 102 ++++++++++++++++++ llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt | 1 + llvm/lib/ExecutionEngine/Orc/CMakeLists.txt | 1 + llvm/lib/Target/TargetMachineC.cpp | 65 ----------- 5 files changed, 105 insertions(+), 65 deletions(-) create mode 100644 llvm/lib/CodeGen/LLVMTargetMachineC.cpp diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt index 5a17944db0ae03..1ef4857af462c9 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/LLVMTargetMachineC.cpp b/llvm/lib/CodeGen/LLVMTargetMachineC.cpp new file mode 100644 index 00000000000000..d4d8bdd4bdc20d --- /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<TargetMachine *>(P); +} + +static Target *unwrap(LLVMTargetRef P) { return reinterpret_cast<Target *>(P); } + +static LLVMTargetMachineRef wrap(const TargetMachine *P) { + return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P)); +} + +static LLVMTargetRef wrap(const Target *P) { + return reinterpret_cast<LLVMTargetRef>(const_cast<Target *>(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<LLVMTargetMachine *>(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; +} \ No newline at end of file diff --git a/llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt b/llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt index f7a4610e0500f3..958cf3138f24fa 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/Orc/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt index 5dfd621781e446..32f7cac0b3d1ec 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/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp index bae4f3e1da5aba..08fad95d18752e 100644 --- a/llvm/lib/Target/TargetMachineC.cpp +++ b/llvm/lib/Target/TargetMachineC.cpp @@ -13,13 +13,11 @@ #include "llvm-c/Core.h" #include "llvm-c/TargetMachine.h" #include "llvm/Analysis/TargetTransformInfo.h" -#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/LegacyPassManager.h" #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" @@ -288,69 +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; - MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(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; -} - char *LLVMGetDefaultTargetTriple(void) { return strdup(sys::getDefaultTargetTriple().c_str()); } >From 68428e3ca4117087a4553efc728f75855e92c93c Mon Sep 17 00:00:00 2001 From: matinraayai <30674652+matinraa...@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:59:04 -0400 Subject: [PATCH 19/19] Added missing line at the end of LLVMTargetMachineC.cpp. --- llvm/lib/CodeGen/LLVMTargetMachineC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/CodeGen/LLVMTargetMachineC.cpp b/llvm/lib/CodeGen/LLVMTargetMachineC.cpp index d4d8bdd4bdc20d..b6b7b74de93bc7 100644 --- a/llvm/lib/CodeGen/LLVMTargetMachineC.cpp +++ b/llvm/lib/CodeGen/LLVMTargetMachineC.cpp @@ -99,4 +99,4 @@ LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); return Result; -} \ No newline at end of file +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits