Skip to content

Commit c2f819a

Browse files
flip1995MaskRay
authored andcommitted
[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
1 parent cb15116 commit c2f819a

File tree

37 files changed

+173
-102
lines changed

37 files changed

+173
-102
lines changed

clang/lib/Parse/ParseStmtAsm.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,20 +577,22 @@ StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
577577
TheTarget->createMCAsmInfo(*MRI, TT, MCOptions));
578578
// Get the instruction descriptor.
579579
std::unique_ptr<llvm::MCInstrInfo> MII(TheTarget->createMCInstrInfo());
580-
std::unique_ptr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo());
581580
std::unique_ptr<llvm::MCSubtargetInfo> STI(
582581
TheTarget->createMCSubtargetInfo(TT, TO.CPU, FeaturesStr));
583582
// Target MCTargetDesc may not be linked in clang-based tools.
584-
if (!MAI || !MII || !MOFI || !STI) {
583+
584+
if (!MAI || !MII || !STI) {
585585
Diag(AsmLoc, diag::err_msasm_unable_to_create_target)
586586
<< "target MC unavailable";
587587
return EmptyStmt();
588588
}
589589

590590
llvm::SourceMgr TempSrcMgr;
591-
llvm::MCContext Ctx(TheTriple, MAI.get(), MRI.get(), MOFI.get(), STI.get(),
592-
&TempSrcMgr);
593-
MOFI->initMCObjectFileInfo(Ctx, /*PIC=*/false);
591+
llvm::MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &TempSrcMgr);
592+
std::unique_ptr<llvm::MCObjectFileInfo> MOFI(
593+
TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false));
594+
Ctx.setObjectFileInfo(MOFI.get());
595+
594596
std::unique_ptr<llvm::MemoryBuffer> Buffer =
595597
llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>");
596598

clang/tools/driver/cc1as_main.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,15 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
383383
if (!Opts.SplitDwarfOutput.empty())
384384
DwoOS = getOutputStream(Opts.SplitDwarfOutput, Diags, IsBinary);
385385

386-
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
387-
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
388-
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
389-
390386
// Build up the feature string from the target feature list.
391387
std::string FS = llvm::join(Opts.Features, ",");
392388

393389
std::unique_ptr<MCSubtargetInfo> STI(
394390
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
395391
assert(STI && "Unable to create subtarget info!");
396392

397-
MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), MOFI.get(),
398-
STI.get(), &SrcMgr, &MCOptions);
393+
MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr,
394+
&MCOptions);
399395

400396
bool PIC = false;
401397
if (Opts.RelocationModel == "static") {
@@ -408,7 +404,12 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
408404
PIC = false;
409405
}
410406

411-
MOFI->initMCObjectFileInfo(Ctx, PIC);
407+
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
408+
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
409+
std::unique_ptr<MCObjectFileInfo> MOFI(
410+
TheTarget->createMCObjectFileInfo(Ctx, PIC));
411+
Ctx.setObjectFileInfo(MOFI.get());
412+
412413
if (Opts.SaveTemporaryLabels)
413414
Ctx.setAllowTemporaryLabels(false);
414415
if (Opts.GenDwarfForAssembly)

lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,9 @@ DisassemblerLLVMC::MCDisasmInstance::Create(const char *triple, const char *cpu,
904904
if (!asm_info_up)
905905
return Instance();
906906

907-
std::unique_ptr<llvm::MCContext> context_up(new llvm::MCContext(
908-
llvm::Triple(triple), asm_info_up.get(), reg_info_up.get(),
909-
/*MOFI=*/nullptr, subtarget_info_up.get()));
907+
std::unique_ptr<llvm::MCContext> context_up(
908+
new llvm::MCContext(llvm::Triple(triple), asm_info_up.get(),
909+
reg_info_up.get(), subtarget_info_up.get()));
910910
if (!context_up)
911911
return Instance();
912912

lldb/source/Plugins/Instruction/MIPS/EmulateInstructionMIPS.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ EmulateInstructionMIPS::EmulateInstructionMIPS(
160160
assert(m_asm_info.get() && m_subtype_info.get());
161161

162162
m_context = std::make_unique<llvm::MCContext>(
163-
triple, m_asm_info.get(), m_reg_info.get(), /*MOFI=*/nullptr,
164-
m_subtype_info.get());
163+
triple, m_asm_info.get(), m_reg_info.get(), m_subtype_info.get());
165164
assert(m_context.get());
166165

167166
m_disasm.reset(target->createMCDisassembler(*m_subtype_info, *m_context));

lldb/source/Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ EmulateInstructionMIPS64::EmulateInstructionMIPS64(
164164
assert(m_asm_info.get() && m_subtype_info.get());
165165

166166
m_context = std::make_unique<llvm::MCContext>(
167-
triple, m_asm_info.get(), m_reg_info.get(), /*MOFI=*/nullptr,
168-
m_subtype_info.get());
167+
triple, m_asm_info.get(), m_reg_info.get(), m_subtype_info.get());
169168
assert(m_context.get());
170169

171170
m_disasm.reset(target->createMCDisassembler(*m_subtype_info, *m_context));

llvm/include/llvm/MC/MCContext.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,7 @@ namespace llvm {
393393

394394
public:
395395
explicit MCContext(const Triple &TheTriple, const MCAsmInfo *MAI,
396-
const MCRegisterInfo *MRI, const MCObjectFileInfo *MOFI,
397-
const MCSubtargetInfo *MSTI,
396+
const MCRegisterInfo *MRI, const MCSubtargetInfo *MSTI,
398397
const SourceMgr *Mgr = nullptr,
399398
MCTargetOptions const *TargetOpts = nullptr,
400399
bool DoAutoReset = true);
@@ -416,6 +415,8 @@ namespace llvm {
416415
this->DiagHandler = DiagHandler;
417416
}
418417

418+
void setObjectFileInfo(const MCObjectFileInfo *Mofi) { MOFI = Mofi; }
419+
419420
const MCAsmInfo *getAsmInfo() const { return MAI; }
420421

421422
const MCRegisterInfo *getRegisterInfo() const { return MRI; }

llvm/include/llvm/Support/TargetRegistry.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/StringRef.h"
2424
#include "llvm/ADT/Triple.h"
2525
#include "llvm/ADT/iterator_range.h"
26+
#include "llvm/MC/MCObjectFileInfo.h"
2627
#include "llvm/Support/CodeGen.h"
2728
#include "llvm/Support/ErrorHandling.h"
2829
#include "llvm/Support/FormattedStream.h"
@@ -130,6 +131,9 @@ class Target {
130131
using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
131132
const Triple &TT,
132133
const MCTargetOptions &Options);
134+
using MCObjectFileInfoCtorFnTy = MCObjectFileInfo *(*)(MCContext &Ctx,
135+
bool PIC,
136+
bool LargeCodeModel);
133137
using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
134138
using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
135139
using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
@@ -227,6 +231,9 @@ class Target {
227231
/// registered.
228232
MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
229233

234+
/// Constructor function for this target's MCObjectFileInfo, if registered.
235+
MCObjectFileInfoCtorFnTy MCObjectFileInfoCtorFn;
236+
230237
/// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
231238
/// if registered.
232239
MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
@@ -350,6 +357,19 @@ class Target {
350357
return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options);
351358
}
352359

360+
/// Create a MCObjectFileInfo implementation for the specified target
361+
/// triple.
362+
///
363+
MCObjectFileInfo *createMCObjectFileInfo(MCContext &Ctx, bool PIC,
364+
bool LargeCodeModel = false) const {
365+
if (!MCObjectFileInfoCtorFn) {
366+
MCObjectFileInfo *MOFI = new MCObjectFileInfo();
367+
MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel);
368+
return MOFI;
369+
}
370+
return MCObjectFileInfoCtorFn(Ctx, PIC, LargeCodeModel);
371+
}
372+
353373
/// createMCInstrInfo - Create a MCInstrInfo implementation.
354374
///
355375
MCInstrInfo *createMCInstrInfo() const {
@@ -724,6 +744,19 @@ struct TargetRegistry {
724744
T.MCAsmInfoCtorFn = Fn;
725745
}
726746

747+
/// Register a MCObjectFileInfo implementation for the given target.
748+
///
749+
/// Clients are responsible for ensuring that registration doesn't occur
750+
/// while another thread is attempting to access the registry. Typically
751+
/// this is done by initializing all targets at program startup.
752+
///
753+
/// @param T - The target being registered.
754+
/// @param Fn - A function to construct a MCObjectFileInfo for the target.
755+
static void RegisterMCObjectFileInfo(Target &T,
756+
Target::MCObjectFileInfoCtorFnTy Fn) {
757+
T.MCObjectFileInfoCtorFn = Fn;
758+
}
759+
727760
/// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
728761
/// given target.
729762
///
@@ -991,6 +1024,39 @@ struct RegisterMCAsmInfoFn {
9911024
}
9921025
};
9931026

1027+
/// Helper template for registering a target object file info implementation.
1028+
/// This invokes the static "Create" method on the class to actually do the
1029+
/// construction. Usage:
1030+
///
1031+
/// extern "C" void LLVMInitializeFooTarget() {
1032+
/// extern Target TheFooTarget;
1033+
/// RegisterMCObjectFileInfo<FooMCObjectFileInfo> X(TheFooTarget);
1034+
/// }
1035+
template <class MCObjectFileInfoImpl> struct RegisterMCObjectFileInfo {
1036+
RegisterMCObjectFileInfo(Target &T) {
1037+
TargetRegistry::RegisterMCObjectFileInfo(T, &Allocator);
1038+
}
1039+
1040+
private:
1041+
static MCObjectFileInfo *Allocator(MCContext &Ctx, bool PIC,
1042+
bool LargeCodeModel = false) {
1043+
return new MCObjectFileInfoImpl(Ctx, PIC, LargeCodeModel);
1044+
}
1045+
};
1046+
1047+
/// Helper template for registering a target object file info implementation.
1048+
/// This invokes the specified function to do the construction. Usage:
1049+
///
1050+
/// extern "C" void LLVMInitializeFooTarget() {
1051+
/// extern Target TheFooTarget;
1052+
/// RegisterMCObjectFileInfoFn X(TheFooTarget, TheFunction);
1053+
/// }
1054+
struct RegisterMCObjectFileInfoFn {
1055+
RegisterMCObjectFileInfoFn(Target &T, Target::MCObjectFileInfoCtorFnTy Fn) {
1056+
TargetRegistry::RegisterMCObjectFileInfo(T, Fn);
1057+
}
1058+
};
1059+
9941060
/// RegisterMCInstrInfo - Helper template for registering a target instruction
9951061
/// info implementation. This invokes the static "Create" method on the class
9961062
/// to actually do the construction. Usage:

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ void MachineModuleInfo::finalize() {
218218
MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
219219
: TM(std::move(MMI.TM)),
220220
Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(),
221-
MMI.TM.getMCRegisterInfo(), MMI.TM.getObjFileLowering(),
222-
MMI.TM.getMCSubtargetInfo(), nullptr, nullptr, false),
221+
MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr,
222+
nullptr, false),
223223
MachineFunctions(std::move(MMI.MachineFunctions)) {
224+
Context.setObjectFileInfo(MMI.TM.getObjFileLowering());
224225
ObjFileMMI = MMI.ObjFileMMI;
225226
CurCallSite = MMI.CurCallSite;
226227
UsesMSVCFloatingPoint = MMI.UsesMSVCFloatingPoint;
@@ -234,17 +235,19 @@ MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
234235

235236
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
236237
: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
237-
TM->getMCRegisterInfo(), TM->getObjFileLowering(),
238-
TM->getMCSubtargetInfo(), nullptr, nullptr, false) {
238+
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
239+
nullptr, nullptr, false) {
240+
Context.setObjectFileInfo(TM->getObjFileLowering());
239241
initialize();
240242
}
241243

242244
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
243245
MCContext *ExtContext)
244246
: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
245-
TM->getMCRegisterInfo(), TM->getObjFileLowering(),
246-
TM->getMCSubtargetInfo(), nullptr, nullptr, false),
247+
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
248+
nullptr, nullptr, false),
247249
ExternalContext(ExtContext) {
250+
Context.setObjectFileInfo(TM->getObjFileLowering());
248251
initialize();
249252
}
250253

llvm/lib/DWARFLinker/DWARFStreamer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ bool DwarfStreamer::init(Triple TheTriple) {
5454
if (!MSTI)
5555
return error("no subtarget info for target " + TripleName, Context), false;
5656

57-
MOFI.reset(new MCObjectFileInfo);
58-
MC.reset(
59-
new MCContext(TheTriple, MAI.get(), MRI.get(), MOFI.get(), MSTI.get()));
60-
MOFI->initMCObjectFileInfo(*MC, /*PIC=*/false);
57+
MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get()));
58+
MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, /*PIC=*/false));
59+
MC->setObjectFileInfo(MOFI.get());
6160

6261
MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
6362
if (!MAB)

llvm/lib/MC/MCContext.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ static void defaultDiagHandler(const SMDiagnostic &SMD, bool, const SourceMgr &,
6363
}
6464

6565
MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
66-
const MCRegisterInfo *mri, const MCObjectFileInfo *mofi,
67-
const MCSubtargetInfo *msti, const SourceMgr *mgr,
68-
MCTargetOptions const *TargetOpts, bool DoAutoReset)
66+
const MCRegisterInfo *mri, const MCSubtargetInfo *msti,
67+
const SourceMgr *mgr, MCTargetOptions const *TargetOpts,
68+
bool DoAutoReset)
6969
: TT(TheTriple), SrcMgr(mgr), InlineSrcMgr(nullptr),
70-
DiagHandler(defaultDiagHandler), MAI(mai), MRI(mri), MOFI(mofi),
71-
MSTI(msti), Symbols(Allocator), UsedNames(Allocator),
70+
DiagHandler(defaultDiagHandler), MAI(mai), MRI(mri), MSTI(msti),
71+
Symbols(Allocator), UsedNames(Allocator),
7272
InlineAsmUsedLabelNames(Allocator),
7373
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
7474
AutoReset(DoAutoReset), TargetOptions(TargetOpts) {

0 commit comments

Comments
 (0)