From df7a8b162ea0048a6d66891f381062eddcfdc106 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Sun, 23 May 2021 14:15:23 -0700 Subject: [PATCH] [MC] Refactor MCObjectFileInfo initialization and allow targets to create MCObjectFileInfo This makes it possible for targets to define their own MCObjectFileInfo. This MCObjectFileInfo is then used to determine things like section alignment. This is a follow up to D101462 and prepares for the RISCV backend defining the text section alignment depending on the enabled extensions. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D101921 --- include/llvm/MC/MCContext.h | 5 +- include/llvm/Support/TargetRegistry.h | 66 +++++++++++++++++++ lib/CodeGen/MachineModuleInfo.cpp | 15 +++-- lib/DWARFLinker/DWARFStreamer.cpp | 7 +- lib/MC/MCContext.cpp | 10 +-- lib/MC/MCDisassembler/Disassembler.cpp | 4 +- lib/Object/ModuleSymbolTable.cpp | 9 +-- tools/llvm-cfi-verify/lib/FileAnalysis.cpp | 2 +- tools/llvm-cfi-verify/lib/FileAnalysis.h | 1 - tools/llvm-dwp/llvm-dwp.cpp | 7 +- tools/llvm-exegesis/lib/Analysis.cpp | 6 +- tools/llvm-exegesis/lib/Analysis.h | 1 - tools/llvm-exegesis/lib/LlvmState.cpp | 3 +- tools/llvm-exegesis/lib/SnippetFile.cpp | 8 +-- tools/llvm-jitlink/llvm-jitlink.cpp | 3 +- .../llvm-mc-assemble-fuzzer.cpp | 8 +-- tools/llvm-mc/llvm-mc.cpp | 7 +- tools/llvm-mca/llvm-mca.cpp | 8 +-- tools/llvm-ml/Disassembler.cpp | 3 +- tools/llvm-ml/llvm-ml.cpp | 8 +-- tools/llvm-objdump/MachODump.cpp | 6 +- tools/llvm-objdump/llvm-objdump.cpp | 7 +- tools/llvm-profgen/ProfiledBinary.cpp | 7 +- tools/llvm-rtdyld/llvm-rtdyld.cpp | 3 +- tools/sancov/sancov.cpp | 3 +- unittests/CodeGen/MachineInstrTest.cpp | 2 +- unittests/CodeGen/MachineOperandTest.cpp | 2 +- unittests/CodeGen/TestAsmPrinter.cpp | 3 +- unittests/DebugInfo/DWARF/DwarfGenerator.cpp | 3 +- unittests/MC/DwarfLineTables.cpp | 2 +- unittests/MC/SystemZ/SystemZAsmLexerTest.cpp | 10 +-- 31 files changed, 149 insertions(+), 80 deletions(-) diff --git a/include/llvm/MC/MCContext.h b/include/llvm/MC/MCContext.h index 100befc48ec..1bd567d9dea 100644 --- a/include/llvm/MC/MCContext.h +++ b/include/llvm/MC/MCContext.h @@ -393,8 +393,7 @@ namespace llvm { public: explicit MCContext(const Triple &TheTriple, const MCAsmInfo *MAI, - const MCRegisterInfo *MRI, const MCObjectFileInfo *MOFI, - const MCSubtargetInfo *MSTI, + const MCRegisterInfo *MRI, const MCSubtargetInfo *MSTI, const SourceMgr *Mgr = nullptr, MCTargetOptions const *TargetOpts = nullptr, bool DoAutoReset = true); @@ -416,6 +415,8 @@ namespace llvm { this->DiagHandler = DiagHandler; } + void setObjectFileInfo(const MCObjectFileInfo *Mofi) { MOFI = Mofi; } + const MCAsmInfo *getAsmInfo() const { return MAI; } const MCRegisterInfo *getRegisterInfo() const { return MRI; } diff --git a/include/llvm/Support/TargetRegistry.h b/include/llvm/Support/TargetRegistry.h index 9d21babd82e..e661ae26cb4 100644 --- a/include/llvm/Support/TargetRegistry.h +++ b/include/llvm/Support/TargetRegistry.h @@ -23,6 +23,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" #include "llvm/ADT/iterator_range.h" +#include "llvm/MC/MCObjectFileInfo.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FormattedStream.h" @@ -130,6 +131,9 @@ public: using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI, const Triple &TT, const MCTargetOptions &Options); + using MCObjectFileInfoCtorFnTy = MCObjectFileInfo *(*)(MCContext &Ctx, + bool PIC, + bool LargeCodeModel); using MCInstrInfoCtorFnTy = MCInstrInfo *(*)(); using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info); using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT); @@ -227,6 +231,9 @@ private: /// registered. MCAsmInfoCtorFnTy MCAsmInfoCtorFn; + /// Constructor function for this target's MCObjectFileInfo, if registered. + MCObjectFileInfoCtorFnTy MCObjectFileInfoCtorFn; + /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo, /// if registered. MCInstrInfoCtorFnTy MCInstrInfoCtorFn; @@ -350,6 +357,19 @@ public: return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options); } + /// Create a MCObjectFileInfo implementation for the specified target + /// triple. + /// + MCObjectFileInfo *createMCObjectFileInfo(MCContext &Ctx, bool PIC, + bool LargeCodeModel = false) const { + if (!MCObjectFileInfoCtorFn) { + MCObjectFileInfo *MOFI = new MCObjectFileInfo(); + MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel); + return MOFI; + } + return MCObjectFileInfoCtorFn(Ctx, PIC, LargeCodeModel); + } + /// createMCInstrInfo - Create a MCInstrInfo implementation. /// MCInstrInfo *createMCInstrInfo() const { @@ -724,6 +744,19 @@ struct TargetRegistry { T.MCAsmInfoCtorFn = Fn; } + /// Register a MCObjectFileInfo implementation for the given target. + /// + /// Clients are responsible for ensuring that registration doesn't occur + /// while another thread is attempting to access the registry. Typically + /// this is done by initializing all targets at program startup. + /// + /// @param T - The target being registered. + /// @param Fn - A function to construct a MCObjectFileInfo for the target. + static void RegisterMCObjectFileInfo(Target &T, + Target::MCObjectFileInfoCtorFnTy Fn) { + T.MCObjectFileInfoCtorFn = Fn; + } + /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the /// given target. /// @@ -991,6 +1024,39 @@ struct RegisterMCAsmInfoFn { } }; +/// Helper template for registering a target object file info implementation. +/// This invokes the static "Create" method on the class to actually do the +/// construction. Usage: +/// +/// extern "C" void LLVMInitializeFooTarget() { +/// extern Target TheFooTarget; +/// RegisterMCObjectFileInfo X(TheFooTarget); +/// } +template struct RegisterMCObjectFileInfo { + RegisterMCObjectFileInfo(Target &T) { + TargetRegistry::RegisterMCObjectFileInfo(T, &Allocator); + } + +private: + static MCObjectFileInfo *Allocator(MCContext &Ctx, bool PIC, + bool LargeCodeModel = false) { + return new MCObjectFileInfoImpl(Ctx, PIC, LargeCodeModel); + } +}; + +/// Helper template for registering a target object file info implementation. +/// This invokes the specified function to do the construction. Usage: +/// +/// extern "C" void LLVMInitializeFooTarget() { +/// extern Target TheFooTarget; +/// RegisterMCObjectFileInfoFn X(TheFooTarget, TheFunction); +/// } +struct RegisterMCObjectFileInfoFn { + RegisterMCObjectFileInfoFn(Target &T, Target::MCObjectFileInfoCtorFnTy Fn) { + TargetRegistry::RegisterMCObjectFileInfo(T, Fn); + } +}; + /// RegisterMCInstrInfo - Helper template for registering a target instruction /// info implementation. This invokes the static "Create" method on the class /// to actually do the construction. Usage: diff --git a/lib/CodeGen/MachineModuleInfo.cpp b/lib/CodeGen/MachineModuleInfo.cpp index 2dddb88b27d..50cbb14e926 100644 --- a/lib/CodeGen/MachineModuleInfo.cpp +++ b/lib/CodeGen/MachineModuleInfo.cpp @@ -218,9 +218,10 @@ void MachineModuleInfo::finalize() { MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI) : TM(std::move(MMI.TM)), Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(), - MMI.TM.getMCRegisterInfo(), MMI.TM.getObjFileLowering(), - MMI.TM.getMCSubtargetInfo(), nullptr, nullptr, false), + MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr, + nullptr, false), MachineFunctions(std::move(MMI.MachineFunctions)) { + Context.setObjectFileInfo(MMI.TM.getObjFileLowering()); ObjFileMMI = MMI.ObjFileMMI; CurCallSite = MMI.CurCallSite; UsesMSVCFloatingPoint = MMI.UsesMSVCFloatingPoint; @@ -234,17 +235,19 @@ MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI) MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM) : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(), - TM->getMCRegisterInfo(), TM->getObjFileLowering(), - TM->getMCSubtargetInfo(), nullptr, nullptr, false) { + TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(), + nullptr, nullptr, false) { + Context.setObjectFileInfo(TM->getObjFileLowering()); initialize(); } MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM, MCContext *ExtContext) : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(), - TM->getMCRegisterInfo(), TM->getObjFileLowering(), - TM->getMCSubtargetInfo(), nullptr, nullptr, false), + TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(), + nullptr, nullptr, false), ExternalContext(ExtContext) { + Context.setObjectFileInfo(TM->getObjFileLowering()); initialize(); } diff --git a/lib/DWARFLinker/DWARFStreamer.cpp b/lib/DWARFLinker/DWARFStreamer.cpp index f452514b74c..3a9f79e4701 100644 --- a/lib/DWARFLinker/DWARFStreamer.cpp +++ b/lib/DWARFLinker/DWARFStreamer.cpp @@ -54,10 +54,9 @@ bool DwarfStreamer::init(Triple TheTriple) { if (!MSTI) return error("no subtarget info for target " + TripleName, Context), false; - MOFI.reset(new MCObjectFileInfo); - MC.reset( - new MCContext(TheTriple, MAI.get(), MRI.get(), MOFI.get(), MSTI.get())); - MOFI->initMCObjectFileInfo(*MC, /*PIC=*/false); + MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get())); + MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, /*PIC=*/false)); + MC->setObjectFileInfo(MOFI.get()); MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions); if (!MAB) diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index 6a0b7b94ead..72171488e93 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -63,12 +63,12 @@ static void defaultDiagHandler(const SMDiagnostic &SMD, bool, const SourceMgr &, } MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai, - const MCRegisterInfo *mri, const MCObjectFileInfo *mofi, - const MCSubtargetInfo *msti, const SourceMgr *mgr, - MCTargetOptions const *TargetOpts, bool DoAutoReset) + const MCRegisterInfo *mri, const MCSubtargetInfo *msti, + const SourceMgr *mgr, MCTargetOptions const *TargetOpts, + bool DoAutoReset) : TT(TheTriple), SrcMgr(mgr), InlineSrcMgr(nullptr), - DiagHandler(defaultDiagHandler), MAI(mai), MRI(mri), MOFI(mofi), - MSTI(msti), Symbols(Allocator), UsedNames(Allocator), + DiagHandler(defaultDiagHandler), MAI(mai), MRI(mri), MSTI(msti), + Symbols(Allocator), UsedNames(Allocator), InlineAsmUsedLabelNames(Allocator), CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), AutoReset(DoAutoReset), TargetOptions(TargetOpts) { diff --git a/lib/MC/MCDisassembler/Disassembler.cpp b/lib/MC/MCDisassembler/Disassembler.cpp index 4cc28d10ebd..52ab0b41f53 100644 --- a/lib/MC/MCDisassembler/Disassembler.cpp +++ b/lib/MC/MCDisassembler/Disassembler.cpp @@ -74,8 +74,8 @@ LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU, return nullptr; // Set up the MCContext for creating symbols and MCExpr's. - std::unique_ptr Ctx(new MCContext(Triple(TT), MAI.get(), MRI.get(), - /*MOFI=*/nullptr, STI.get())); + std::unique_ptr Ctx( + new MCContext(Triple(TT), MAI.get(), MRI.get(), STI.get())); if (!Ctx) return nullptr; diff --git a/lib/Object/ModuleSymbolTable.cpp b/lib/Object/ModuleSymbolTable.cpp index a00fea3bbf0..9cdc2813f42 100644 --- a/lib/Object/ModuleSymbolTable.cpp +++ b/lib/Object/ModuleSymbolTable.cpp @@ -99,10 +99,11 @@ initializeRecordStreamer(const Module &M, if (!MCII) return; - MCObjectFileInfo MOFI; - MCContext MCCtx(TT, MAI.get(), MRI.get(), &MOFI, STI.get()); - MOFI.initMCObjectFileInfo(MCCtx, /*PIC=*/false); - MOFI.setSDKVersion(M.getSDKVersion()); + MCContext MCCtx(TT, MAI.get(), MRI.get(), STI.get()); + std::unique_ptr MOFI( + T->createMCObjectFileInfo(MCCtx, /*PIC=*/false)); + MOFI->setSDKVersion(M.getSDKVersion()); + MCCtx.setObjectFileInfo(MOFI.get()); RecordStreamer Streamer(MCCtx, M); T->createNullTargetStreamer(Streamer); diff --git a/tools/llvm-cfi-verify/lib/FileAnalysis.cpp b/tools/llvm-cfi-verify/lib/FileAnalysis.cpp index 695a16256df..e1bca0dc51b 100644 --- a/tools/llvm-cfi-verify/lib/FileAnalysis.cpp +++ b/tools/llvm-cfi-verify/lib/FileAnalysis.cpp @@ -408,7 +408,7 @@ Error FileAnalysis::initialiseDisassemblyMembers() { return make_error("Failed to initialise MII."); Context.reset(new MCContext(Triple(TripleName), AsmInfo.get(), - RegisterInfo.get(), &MOFI, SubtargetInfo.get())); + RegisterInfo.get(), SubtargetInfo.get())); Disassembler.reset( ObjectTarget->createMCDisassembler(*SubtargetInfo, *Context)); diff --git a/tools/llvm-cfi-verify/lib/FileAnalysis.h b/tools/llvm-cfi-verify/lib/FileAnalysis.h index 27135c0debb..b256f7ebd39 100644 --- a/tools/llvm-cfi-verify/lib/FileAnalysis.h +++ b/tools/llvm-cfi-verify/lib/FileAnalysis.h @@ -207,7 +207,6 @@ private: std::unique_ptr AsmInfo; std::unique_ptr SubtargetInfo; std::unique_ptr MII; - MCObjectFileInfo MOFI; std::unique_ptr Context; std::unique_ptr Disassembler; std::unique_ptr MIA; diff --git a/tools/llvm-dwp/llvm-dwp.cpp b/tools/llvm-dwp/llvm-dwp.cpp index 1be8e4733c9..c7cb1fc23a4 100644 --- a/tools/llvm-dwp/llvm-dwp.cpp +++ b/tools/llvm-dwp/llvm-dwp.cpp @@ -880,9 +880,10 @@ int main(int argc, char **argv) { if (!MSTI) return error("no subtarget info for target " + TripleName, Context); - MCObjectFileInfo MOFI; - MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), &MOFI, MSTI.get()); - MOFI.initMCObjectFileInfo(MC, /*PIC=*/false); + MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get()); + std::unique_ptr MOFI( + TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false)); + MC.setObjectFileInfo(MOFI.get()); MCTargetOptions Options; auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options); diff --git a/tools/llvm-exegesis/lib/Analysis.cpp b/tools/llvm-exegesis/lib/Analysis.cpp index 079fcd85b56..519b54512da 100644 --- a/tools/llvm-exegesis/lib/Analysis.cpp +++ b/tools/llvm-exegesis/lib/Analysis.cpp @@ -176,9 +176,9 @@ Analysis::Analysis(const Target &Target, std::unique_ptr InstrInfo, Triple(FirstPoint.LLVMTriple), 0 /*default variant*/, *AsmInfo_, *InstrInfo_, *RegInfo_)); - Context_ = std::make_unique( - Triple(FirstPoint.LLVMTriple), AsmInfo_.get(), RegInfo_.get(), - &ObjectFileInfo_, SubtargetInfo_.get()); + Context_ = + std::make_unique(Triple(FirstPoint.LLVMTriple), AsmInfo_.get(), + RegInfo_.get(), SubtargetInfo_.get()); Disasm_.reset(Target.createMCDisassembler(*SubtargetInfo_, *Context_)); assert(Disasm_ && "cannot create MCDisassembler. missing call to " "InitializeXXXTargetDisassembler ?"); diff --git a/tools/llvm-exegesis/lib/Analysis.h b/tools/llvm-exegesis/lib/Analysis.h index 0f8209c6e2f..9d8b04c9990 100644 --- a/tools/llvm-exegesis/lib/Analysis.h +++ b/tools/llvm-exegesis/lib/Analysis.h @@ -112,7 +112,6 @@ private: const char *Separator) const; const InstructionBenchmarkClustering &Clustering_; - MCObjectFileInfo ObjectFileInfo_; std::unique_ptr Context_; std::unique_ptr SubtargetInfo_; std::unique_ptr InstrInfo_; diff --git a/tools/llvm-exegesis/lib/LlvmState.cpp b/tools/llvm-exegesis/lib/LlvmState.cpp index 51e5f863424..eb492a59113 100644 --- a/tools/llvm-exegesis/lib/LlvmState.cpp +++ b/tools/llvm-exegesis/lib/LlvmState.cpp @@ -61,10 +61,9 @@ std::unique_ptr LLVMState::createTargetMachine() const { } bool LLVMState::canAssemble(const MCInst &Inst) const { - MCObjectFileInfo ObjectFileInfo; MCContext Context(TheTargetMachine->getTargetTriple(), TheTargetMachine->getMCAsmInfo(), - TheTargetMachine->getMCRegisterInfo(), &ObjectFileInfo, + TheTargetMachine->getMCRegisterInfo(), TheTargetMachine->getMCSubtargetInfo()); std::unique_ptr CodeEmitter( TheTargetMachine->getTarget().createMCCodeEmitter( diff --git a/tools/llvm-exegesis/lib/SnippetFile.cpp b/tools/llvm-exegesis/lib/SnippetFile.cpp index ec5ce5bf951..f674bebf05d 100644 --- a/tools/llvm-exegesis/lib/SnippetFile.cpp +++ b/tools/llvm-exegesis/lib/SnippetFile.cpp @@ -130,13 +130,13 @@ Expected> readSnippets(const LLVMState &State, BenchmarkCode Result; - MCObjectFileInfo ObjectFileInfo; const TargetMachine &TM = State.getTargetMachine(); MCContext Context(TM.getTargetTriple(), TM.getMCAsmInfo(), - TM.getMCRegisterInfo(), &ObjectFileInfo, - TM.getMCSubtargetInfo()); + TM.getMCRegisterInfo(), TM.getMCSubtargetInfo()); + std::unique_ptr ObjectFileInfo( + TM.getTarget().createMCObjectFileInfo(Context, /*PIC=*/false)); + Context.setObjectFileInfo(ObjectFileInfo.get()); Context.initInlineSourceManager(); - ObjectFileInfo.initMCObjectFileInfo(Context, /*PIC=*/false); BenchmarkCodeStreamer Streamer(&Context, TM.getMCRegisterInfo(), &Result); std::string Error; diff --git a/tools/llvm-jitlink/llvm-jitlink.cpp b/tools/llvm-jitlink/llvm-jitlink.cpp index 2d33676f2fb..95e5cba58f7 100644 --- a/tools/llvm-jitlink/llvm-jitlink.cpp +++ b/tools/llvm-jitlink/llvm-jitlink.cpp @@ -1256,8 +1256,7 @@ static Error runChecks(Session &S) { TripleName, inconvertibleErrorCode())); - MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), /*MOFI=*/nullptr, - STI.get()); + MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), STI.get()); std::unique_ptr Disassembler( TheTarget->createMCDisassembler(*STI, Ctx)); diff --git a/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp b/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp index 6dd26e1f83e..74051666a6e 100644 --- a/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp +++ b/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp @@ -173,10 +173,10 @@ int AssembleOneInput(const uint8_t *Data, size_t Size) { std::unique_ptr STI( TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); - MCObjectFileInfo MOFI; - MCContext Ctx(TheTriple, MAI.get(), MRI.get(), &MOFI, STI.get(), &SrcMgr); - static const bool UsePIC = false; - MOFI.initMCObjectFileInfo(Ctx, UsePIC); + MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr); + std::unique_ptr MOFI( + TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false)); + Ctx.setObjectFileInfo(MOFI.get()); const unsigned OutputAsmVariant = 0; std::unique_ptr MCII(TheTarget->createMCInstrInfo()); diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index 1c087401369..73a3a05c5a0 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -394,10 +394,11 @@ int main(int argc, char **argv) { // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and // MCObjectFileInfo needs a MCContext reference in order to initialize itself. - MCObjectFileInfo MOFI; - MCContext Ctx(TheTriple, MAI.get(), MRI.get(), &MOFI, STI.get(), &SrcMgr, + MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr, &MCOptions); - MOFI.initMCObjectFileInfo(Ctx, PIC, LargeCodeModel); + std::unique_ptr MOFI( + TheTarget->createMCObjectFileInfo(Ctx, PIC, LargeCodeModel)); + Ctx.setObjectFileInfo(MOFI.get()); if (SaveTempLabels) Ctx.setAllowTemporaryLabels(false); diff --git a/tools/llvm-mca/llvm-mca.cpp b/tools/llvm-mca/llvm-mca.cpp index 4a4230ce452..3bb2f10cd94 100644 --- a/tools/llvm-mca/llvm-mca.cpp +++ b/tools/llvm-mca/llvm-mca.cpp @@ -371,15 +371,15 @@ int main(int argc, char **argv) { TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); assert(MAI && "Unable to create target asm info!"); - MCObjectFileInfo MOFI; SourceMgr SrcMgr; // Tell SrcMgr about this buffer, which is what the parser will pick up. SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); - MCContext Ctx(TheTriple, MAI.get(), MRI.get(), &MOFI, STI.get(), &SrcMgr); - - MOFI.initMCObjectFileInfo(Ctx, /*PIC=*/false); + MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr); + std::unique_ptr MOFI( + TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false)); + Ctx.setObjectFileInfo(MOFI.get()); std::unique_ptr BOS; diff --git a/tools/llvm-ml/Disassembler.cpp b/tools/llvm-ml/Disassembler.cpp index 67d94518f0d..793128c0231 100644 --- a/tools/llvm-ml/Disassembler.cpp +++ b/tools/llvm-ml/Disassembler.cpp @@ -142,8 +142,7 @@ int Disassembler::disassemble(const Target &T, const std::string &TripleName, } // Set up the MCContext for creating symbols and MCExpr's. - MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), /*MOFI=*/nullptr, - &STI); + MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), &STI); std::unique_ptr DisAsm( T.createMCDisassembler(STI, Ctx)); diff --git a/tools/llvm-ml/llvm-ml.cpp b/tools/llvm-ml/llvm-ml.cpp index c61cacc4dbb..14f75d0d814 100644 --- a/tools/llvm-ml/llvm-ml.cpp +++ b/tools/llvm-ml/llvm-ml.cpp @@ -281,10 +281,10 @@ int main(int Argc, char **Argv) { // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and // MCObjectFileInfo needs a MCContext reference in order to initialize itself. - MCObjectFileInfo MOFI; - MCContext Ctx(TheTriple, MAI.get(), MRI.get(), &MOFI, STI.get(), &SrcMgr); - MOFI.initMCObjectFileInfo(Ctx, /*PIC=*/false, - /*LargeCodeModel=*/true); + MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr); + std::unique_ptr MOFI(TheTarget->createMCObjectFileInfo( + Ctx, /*PIC=*/false, /*LargeCodeModel=*/true)); + Ctx.setObjectFileInfo(MOFI.get()); if (InputArgs.hasArg(OPT_save_temp_labels)) Ctx.setAllowTemporaryLabels(false); diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 74fb685ddbf..552cf077453 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -7228,8 +7228,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, std::unique_ptr STI( TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr)); CHECK_TARGET_INFO_CREATION(STI); - MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), /*MOFI=*/nullptr, - STI.get()); + MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get()); std::unique_ptr DisAsm( TheTarget->createMCDisassembler(*STI, Ctx)); CHECK_TARGET_INFO_CREATION(DisAsm); @@ -7280,8 +7279,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, FeaturesStr)); CHECK_THUMB_TARGET_INFO_CREATION(ThumbSTI); ThumbCtx.reset(new MCContext(Triple(ThumbTripleName), ThumbAsmInfo.get(), - ThumbMRI.get(), /*MOFI=*/nullptr, - ThumbSTI.get())); + ThumbMRI.get(), ThumbSTI.get())); ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); CHECK_THUMB_TARGET_INFO_CREATION(ThumbDisAsm); MCContext *PtrThumbCtx = ThumbCtx.get(); diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 3689dd46caa..084c5ed4066 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -1581,10 +1581,11 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { if (!MII) reportError(Obj->getFileName(), "no instruction info for target " + TripleName); - MCObjectFileInfo MOFI; - MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), &MOFI, STI.get()); + MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get()); // FIXME: for now initialize MCObjectFileInfo with default values - MOFI.initMCObjectFileInfo(Ctx, /*PIC=*/false); + std::unique_ptr MOFI( + TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false)); + Ctx.setObjectFileInfo(MOFI.get()); std::unique_ptr DisAsm( TheTarget->createMCDisassembler(*STI, Ctx)); diff --git a/tools/llvm-profgen/ProfiledBinary.cpp b/tools/llvm-profgen/ProfiledBinary.cpp index 645f3cdff5b..464616f6059 100644 --- a/tools/llvm-profgen/ProfiledBinary.cpp +++ b/tools/llvm-profgen/ProfiledBinary.cpp @@ -332,9 +332,10 @@ void ProfiledBinary::setUpDisassembler(const ELFObjectFileBase *Obj) { if (!MII) exitWithError("no instruction info for target " + TripleName, FileName); - MCObjectFileInfo MOFI; - MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), &MOFI, STI.get()); - MOFI.initMCObjectFileInfo(Ctx, /*PIC=*/false); + MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get()); + std::unique_ptr MOFI( + TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false)); + Ctx.setObjectFileInfo(MOFI.get()); DisAsm.reset(TheTarget->createMCDisassembler(*STI, Ctx)); if (!DisAsm) exitWithError("no disassembler for target " + TripleName, FileName); diff --git a/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index a84c0233dc7..7b2e363bbbf 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -757,8 +757,7 @@ static int linkAndVerify() { if (!MAI) ErrorAndExit("Unable to create target asm info!"); - MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), /*MOFI=*/nullptr, - STI.get()); + MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), STI.get()); std::unique_ptr Disassembler( TheTarget->createMCDisassembler(*STI, Ctx)); diff --git a/tools/sancov/sancov.cpp b/tools/sancov/sancov.cpp index 850e2329224..b274310f43e 100644 --- a/tools/sancov/sancov.cpp +++ b/tools/sancov/sancov.cpp @@ -726,8 +726,7 @@ static void getObjectCoveragePoints(const object::ObjectFile &O, TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); failIfEmpty(AsmInfo, "no asm info for target " + TripleName); - std::unique_ptr MOFI(new MCObjectFileInfo); - MCContext Ctx(TheTriple, AsmInfo.get(), MRI.get(), MOFI.get(), STI.get()); + MCContext Ctx(TheTriple, AsmInfo.get(), MRI.get(), STI.get()); std::unique_ptr DisAsm( TheTarget->createMCDisassembler(*STI, Ctx)); failIfEmpty(DisAsm, "no disassembler info for target " + TripleName); diff --git a/unittests/CodeGen/MachineInstrTest.cpp b/unittests/CodeGen/MachineInstrTest.cpp index 1616311495a..82be17bac57 100644 --- a/unittests/CodeGen/MachineInstrTest.cpp +++ b/unittests/CodeGen/MachineInstrTest.cpp @@ -37,7 +37,7 @@ std::unique_ptr createMCContext(MCAsmInfo *AsmInfo) { Triple TheTriple(/*ArchStr=*/"", /*VendorStr=*/"", /*OSStr=*/"", /*EnvironmentStr=*/"elf"); return std::make_unique(TheTriple, AsmInfo, nullptr, nullptr, - nullptr, nullptr, nullptr, false); + nullptr, nullptr, false); } // This test makes sure that MachineInstr::isIdenticalTo handles Defs correctly diff --git a/unittests/CodeGen/MachineOperandTest.cpp b/unittests/CodeGen/MachineOperandTest.cpp index 78f6619fa16..d6808f94be8 100644 --- a/unittests/CodeGen/MachineOperandTest.cpp +++ b/unittests/CodeGen/MachineOperandTest.cpp @@ -319,7 +319,7 @@ TEST(MachineOperandTest, PrintMetadata) { TEST(MachineOperandTest, PrintMCSymbol) { MCAsmInfo MAI; Triple T = Triple("unknown-unknown-unknown"); - MCContext Ctx(T, &MAI, /*MRI=*/nullptr, /*MOFI=*/nullptr, /*MSTI=*/nullptr); + MCContext Ctx(T, &MAI, /*MRI=*/nullptr, /*MSTI=*/nullptr); MCSymbol *Sym = Ctx.getOrCreateSymbol("foo"); // Create a MachineOperand with a metadata and print it. diff --git a/unittests/CodeGen/TestAsmPrinter.cpp b/unittests/CodeGen/TestAsmPrinter.cpp index 82d3e6fb4d6..09154b36446 100644 --- a/unittests/CodeGen/TestAsmPrinter.cpp +++ b/unittests/CodeGen/TestAsmPrinter.cpp @@ -57,8 +57,9 @@ llvm::Error TestAsmPrinter::init(const Target *TheTarget, StringRef TripleName, Triple TheTriple(TripleName); MC.reset(new MCContext(TheTriple, TM->getMCAsmInfo(), TM->getMCRegisterInfo(), - TM->getObjFileLowering(), TM->getMCSubtargetInfo())); + TM->getMCSubtargetInfo())); TM->getObjFileLowering()->Initialize(*MC, *TM); + MC->setObjectFileInfo(TM->getObjFileLowering()); MS = new StrictMock(MC.get()); diff --git a/unittests/DebugInfo/DWARF/DwarfGenerator.cpp b/unittests/DebugInfo/DWARF/DwarfGenerator.cpp index cccde548044..8fe3d2d74d1 100644 --- a/unittests/DebugInfo/DWARF/DwarfGenerator.cpp +++ b/unittests/DebugInfo/DWARF/DwarfGenerator.cpp @@ -464,9 +464,10 @@ llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) { return make_error("no target machine for target " + TripleName, inconvertibleErrorCode()); + MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get())); TLOF = TM->getObjFileLowering(); - MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), TLOF, MSTI.get())); TLOF->Initialize(*MC, *TM); + MC->setObjectFileInfo(TLOF); MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC); if (!MCE) diff --git a/unittests/MC/DwarfLineTables.cpp b/unittests/MC/DwarfLineTables.cpp index 758fe31e870..8206bc55c6a 100644 --- a/unittests/MC/DwarfLineTables.cpp +++ b/unittests/MC/DwarfLineTables.cpp @@ -41,7 +41,7 @@ struct Context { MCTargetOptions MCOptions; MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); Ctx = std::make_unique(Triple(TripleName), MAI.get(), MRI.get(), - /*MOFI=*/nullptr, /*MSTI=*/nullptr); + /*MSTI=*/nullptr); } operator bool() { return Ctx.get(); } diff --git a/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp b/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp index 973c0b8c72d..b90e051b6f2 100644 --- a/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp +++ b/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp @@ -62,6 +62,7 @@ protected: std::unique_ptr MRI; std::unique_ptr MUPMAI; std::unique_ptr MII; + std::unique_ptr MOFI; std::unique_ptr Str; std::unique_ptr Parser; std::unique_ptr Ctx; @@ -74,7 +75,6 @@ protected: const Target *TheTarget; const MCTargetOptions MCOptions; - MCObjectFileInfo MOFI; SystemZAsmLexerTest() { // We will use the SystemZ triple, because of missing @@ -112,9 +112,11 @@ protected: SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc()); EXPECT_EQ(Buffer, nullptr); - Ctx.reset(new MCContext(Triple, MUPMAI.get(), MRI.get(), &MOFI, STI.get(), - &SrcMgr, &MCOptions)); - MOFI.initMCObjectFileInfo(*Ctx, /*PIC=*/false, /*LargeCodeModel=*/false); + Ctx.reset(new MCContext(Triple, MUPMAI.get(), MRI.get(), STI.get(), &SrcMgr, + &MCOptions)); + MOFI.reset(TheTarget->createMCObjectFileInfo(*Ctx, /*PIC=*/false, + /*LargeCodeModel=*/false)); + Ctx->setObjectFileInfo(MOFI.get()); Str.reset(TheTarget->createNullStreamer(*Ctx));