Skip to content

Commit 6782346

Browse files
authored
MC: Add Triple overloads for more MC constructors (#157321)
Avoids more Triple->string->Triple round trip. This is a continuation of f137c3d
1 parent 727e9f5 commit 6782346

File tree

50 files changed

+326
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+326
-269
lines changed

bolt/lib/Core/BinaryContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ Expected<std::unique_ptr<BinaryContext>> BinaryContext::createBinaryContext(
207207
Twine("BOLT-ERROR: ", Error));
208208

209209
std::unique_ptr<const MCRegisterInfo> MRI(
210-
TheTarget->createMCRegInfo(TripleName));
210+
TheTarget->createMCRegInfo(TheTriple));
211211
if (!MRI)
212212
return createStringError(
213213
make_error_code(std::errc::not_supported),
214214
Twine("BOLT-ERROR: no register info for target ", TripleName));
215215

216216
// Set up disassembler.
217217
std::unique_ptr<MCAsmInfo> AsmInfo(
218-
TheTarget->createMCAsmInfo(*MRI, TripleName, MCTargetOptions()));
218+
TheTarget->createMCAsmInfo(*MRI, TheTriple, MCTargetOptions()));
219219
if (!AsmInfo)
220220
return createStringError(
221221
make_error_code(std::errc::not_supported),
@@ -227,7 +227,7 @@ Expected<std::unique_ptr<BinaryContext>> BinaryContext::createBinaryContext(
227227
AsmInfo->setAllowAtInName(true);
228228

229229
std::unique_ptr<const MCSubtargetInfo> STI(
230-
TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr));
230+
TheTarget->createMCSubtargetInfo(TheTriple, "", FeaturesStr));
231231
if (!STI)
232232
return createStringError(
233233
make_error_code(std::errc::not_supported),

clang/lib/Parse/ParseStmtAsm.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
543543
std::string FeaturesStr =
544544
llvm::join(TO.Features.begin(), TO.Features.end(), ",");
545545

546-
std::unique_ptr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
546+
std::unique_ptr<llvm::MCRegisterInfo> MRI(
547+
TheTarget->createMCRegInfo(TheTriple));
547548
if (!MRI) {
548549
Diag(AsmLoc, diag::err_msasm_unable_to_create_target)
549550
<< "target MC unavailable";
@@ -552,11 +553,11 @@ StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
552553
// FIXME: init MCOptions from sanitizer flags here.
553554
llvm::MCTargetOptions MCOptions;
554555
std::unique_ptr<llvm::MCAsmInfo> MAI(
555-
TheTarget->createMCAsmInfo(*MRI, TT, MCOptions));
556+
TheTarget->createMCAsmInfo(*MRI, TheTriple, MCOptions));
556557
// Get the instruction descriptor.
557558
std::unique_ptr<llvm::MCInstrInfo> MII(TheTarget->createMCInstrInfo());
558559
std::unique_ptr<llvm::MCSubtargetInfo> STI(
559-
TheTarget->createMCSubtargetInfo(TT, TO.CPU, FeaturesStr));
560+
TheTarget->createMCSubtargetInfo(TheTriple, TO.CPU, FeaturesStr));
560561
// Target MCTargetDesc may not be linked in clang-based tools.
561562

562563
if (!MAI || !MII || !STI) {
@@ -591,7 +592,7 @@ StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
591592
}
592593

593594
std::unique_ptr<llvm::MCInstPrinter> IP(
594-
TheTarget->createMCInstPrinter(llvm::Triple(TT), 1, *MAI, *MII, *MRI));
595+
TheTarget->createMCInstPrinter(TheTriple, 1, *MAI, *MII, *MRI));
595596

596597
// Change to the Intel dialect.
597598
Parser->setAssemblerDialect(1);

clang/tools/driver/cc1as_main.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ struct AssemblerInvocation {
7171
/// @name Target Options
7272
/// @{
7373

74-
/// The name of the target triple to assemble for.
75-
std::string Triple;
74+
/// The target triple to assemble for.
75+
llvm::Triple Triple;
7676

7777
/// If given, the name of the target CPU to determine which instructions
7878
/// are legal.
@@ -192,9 +192,12 @@ struct AssemblerInvocation {
192192
std::string AsSecureLogFile;
193193
/// @}
194194

195+
void setTriple(llvm::StringRef Str) {
196+
Triple = llvm::Triple(llvm::Triple::normalize(Str));
197+
}
198+
195199
public:
196200
AssemblerInvocation() {
197-
Triple = "";
198201
NoInitialTextSection = 0;
199202
InputFile = "-";
200203
OutputPath = "-";
@@ -261,7 +264,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
261264
// Construct the invocation.
262265

263266
// Target Options
264-
Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
267+
Opts.setTriple(Args.getLastArgValue(OPT_triple));
265268
if (Arg *A = Args.getLastArg(options::OPT_darwin_target_variant_triple))
266269
Opts.DarwinTargetVariantTriple = llvm::Triple(A->getValue());
267270
if (Arg *A = Args.getLastArg(OPT_darwin_target_variant_sdk_version_EQ)) {
@@ -278,7 +281,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
278281

279282
// Use the default target triple if unspecified.
280283
if (Opts.Triple.empty())
281-
Opts.Triple = llvm::sys::getDefaultTargetTriple();
284+
Opts.setTriple(llvm::sys::getDefaultTargetTriple());
282285

283286
// Language Options
284287
Opts.IncludePaths = Args.getAllArgValues(OPT_I);
@@ -419,7 +422,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
419422
std::string Error;
420423
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
421424
if (!TheTarget)
422-
return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
425+
return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple.str();
423426

424427
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
425428
MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true);
@@ -604,7 +607,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
604607
std::unique_ptr<MCTargetAsmParser> TAP(
605608
TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
606609
if (!TAP)
607-
Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
610+
Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple.str();
608611

609612
// Set values for symbols, if any.
610613
for (auto &S : Opts.SymbolDefs) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,12 +1249,15 @@ class InstructionLLVMC : public lldb_private::Instruction {
12491249
};
12501250

12511251
std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>
1252-
DisassemblerLLVMC::MCDisasmInstance::Create(const char *triple, const char *cpu,
1252+
DisassemblerLLVMC::MCDisasmInstance::Create(const char *triple_name,
1253+
const char *cpu,
12531254
const char *features_str,
12541255
unsigned flavor,
12551256
DisassemblerLLVMC &owner) {
12561257
using Instance = std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>;
12571258

1259+
llvm::Triple triple(triple_name);
1260+
12581261
std::string Status;
12591262
const llvm::Target *curr_target =
12601263
llvm::TargetRegistry::lookupTarget(triple, Status);

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,15 @@ EmulateInstructionMIPS::EmulateInstructionMIPS(
147147
if (arch_flags & ArchSpec::eMIPSAse_dspr2)
148148
features += "+dspr2,";
149149

150-
m_reg_info.reset(target->createMCRegInfo(triple.getTriple()));
150+
m_reg_info.reset(target->createMCRegInfo(triple));
151151
assert(m_reg_info.get());
152152

153153
m_insn_info.reset(target->createMCInstrInfo());
154154
assert(m_insn_info.get());
155155

156156
llvm::MCTargetOptions MCOptions;
157-
m_asm_info.reset(
158-
target->createMCAsmInfo(*m_reg_info, triple.getTriple(), MCOptions));
159-
m_subtype_info.reset(
160-
target->createMCSubtargetInfo(triple.getTriple(), cpu, features));
157+
m_asm_info.reset(target->createMCAsmInfo(*m_reg_info, triple, MCOptions));
158+
m_subtype_info.reset(target->createMCSubtargetInfo(triple, cpu, features));
161159
assert(m_asm_info.get() && m_subtype_info.get());
162160

163161
m_context = std::make_unique<llvm::MCContext>(
@@ -174,7 +172,7 @@ EmulateInstructionMIPS::EmulateInstructionMIPS(
174172
features += "+micromips,";
175173

176174
m_alt_subtype_info.reset(
177-
target->createMCSubtargetInfo(triple.getTriple(), cpu, features));
175+
target->createMCSubtargetInfo(triple, cpu, features));
178176
assert(m_alt_subtype_info.get());
179177

180178
m_alt_disasm.reset(
@@ -1005,11 +1003,10 @@ bool EmulateInstructionMIPS::SetInstruction(const Opcode &insn_opcode,
10051003
uint64_t next_inst_addr = (m_addr & (~1ull)) + current_inst_size;
10061004
Address next_addr(next_inst_addr);
10071005

1008-
const size_t bytes_read =
1009-
target->ReadMemory(next_addr, /* Address of next instruction */
1010-
buf, sizeof(uint32_t), error,
1011-
false, /* force_live_memory */
1012-
&load_addr);
1006+
const size_t bytes_read = target->ReadMemory(
1007+
next_addr, /* Address of next instruction */
1008+
buf, sizeof(uint32_t), error, false, /* force_live_memory */
1009+
&load_addr);
10131010

10141011
if (bytes_read == 0)
10151012
return true;

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,15 @@ EmulateInstructionMIPS64::EmulateInstructionMIPS64(
151151
if (arch_flags & ArchSpec::eMIPSAse_micromips)
152152
features += "+micromips,";
153153

154-
m_reg_info.reset(target->createMCRegInfo(triple.getTriple()));
154+
m_reg_info.reset(target->createMCRegInfo(triple));
155155
assert(m_reg_info.get());
156156

157157
m_insn_info.reset(target->createMCInstrInfo());
158158
assert(m_insn_info.get());
159159

160160
llvm::MCTargetOptions MCOptions;
161-
m_asm_info.reset(
162-
target->createMCAsmInfo(*m_reg_info, triple.getTriple(), MCOptions));
163-
m_subtype_info.reset(
164-
target->createMCSubtargetInfo(triple.getTriple(), cpu, features));
161+
m_asm_info.reset(target->createMCAsmInfo(*m_reg_info, triple, MCOptions));
162+
m_subtype_info.reset(target->createMCSubtargetInfo(triple, cpu, features));
165163
assert(m_asm_info.get() && m_subtype_info.get());
166164

167165
m_context = std::make_unique<llvm::MCContext>(

lldb/source/Target/ABI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ bool ABI::GetFallbackRegisterLocation(
232232
}
233233

234234
std::unique_ptr<llvm::MCRegisterInfo> ABI::MakeMCRegisterInfo(const ArchSpec &arch) {
235-
std::string triple = arch.GetTriple().getTriple();
235+
const llvm::Triple &triple = arch.GetTriple();
236236
std::string lookup_error;
237237
const llvm::Target *target =
238238
llvm::TargetRegistry::lookupTarget(triple, lookup_error);
239239
if (!target) {
240240
LLDB_LOG(GetLog(LLDBLog::Process),
241-
"Failed to create an llvm target for {0}: {1}", triple,
241+
"Failed to create an llvm target for {0}: {1}", triple.str(),
242242
lookup_error);
243243
return nullptr;
244244
}

llvm/include/llvm/MC/TargetRegistry.h

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,26 @@ class Target {
389389
/// @name Feature Constructors
390390
/// @{
391391

392-
/// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
392+
[[deprecated("Use overload accepting Triple instead")]]
393+
MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple,
394+
const MCTargetOptions &Options) const {
395+
if (!MCAsmInfoCtorFn)
396+
return nullptr;
397+
return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options);
398+
}
399+
400+
/// Create a MCAsmInfo implementation for the specified
393401
/// target triple.
394402
///
395403
/// \param TheTriple This argument is used to determine the target machine
396404
/// feature set; it should always be provided. Generally this should be
397405
/// either the target triple from the module, or the target triple of the
398406
/// host if that does not exist.
399-
MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple,
407+
MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TheTriple,
400408
const MCTargetOptions &Options) const {
401409
if (!MCAsmInfoCtorFn)
402410
return nullptr;
403-
return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options);
411+
return MCAsmInfoCtorFn(MRI, TheTriple, Options);
404412
}
405413

406414
/// Create a MCObjectFileInfo implementation for the specified target
@@ -432,14 +440,28 @@ class Target {
432440
return MCInstrAnalysisCtorFn(Info);
433441
}
434442

435-
/// createMCRegInfo - Create a MCRegisterInfo implementation.
436-
///
443+
[[deprecated("Use overload accepting Triple instead")]]
437444
MCRegisterInfo *createMCRegInfo(StringRef TT) const {
438445
if (!MCRegInfoCtorFn)
439446
return nullptr;
440447
return MCRegInfoCtorFn(Triple(TT));
441448
}
442449

450+
/// Create a MCRegisterInfo implementation.
451+
MCRegisterInfo *createMCRegInfo(const Triple &TT) const {
452+
if (!MCRegInfoCtorFn)
453+
return nullptr;
454+
return MCRegInfoCtorFn(TT);
455+
}
456+
457+
[[deprecated("Use overload accepting Triple instead")]]
458+
MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
459+
StringRef Features) const {
460+
if (!MCSubtargetInfoCtorFn)
461+
return nullptr;
462+
return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
463+
}
464+
443465
/// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
444466
///
445467
/// \param TheTriple This argument is used to determine the target machine
@@ -449,11 +471,11 @@ class Target {
449471
/// \param CPU This specifies the name of the target CPU.
450472
/// \param Features This specifies the string representation of the
451473
/// additional target features.
452-
MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
474+
MCSubtargetInfo *createMCSubtargetInfo(const Triple &TheTriple, StringRef CPU,
453475
StringRef Features) const {
454476
if (!MCSubtargetInfoCtorFn)
455477
return nullptr;
456-
return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
478+
return MCSubtargetInfoCtorFn(TheTriple, CPU, Features);
457479
}
458480

459481
/// createTargetMachine - Create a target specific machine implementation
@@ -577,15 +599,31 @@ class Target {
577599
return nullptr;
578600
}
579601

602+
[[deprecated("Use overload accepting Triple instead")]]
603+
MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
604+
return createMCRelocationInfo(Triple(TT), Ctx);
605+
}
606+
580607
/// createMCRelocationInfo - Create a target specific MCRelocationInfo.
581608
///
582609
/// \param TT The target triple.
583610
/// \param Ctx The target context.
584-
MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
611+
MCRelocationInfo *createMCRelocationInfo(const Triple &TT,
612+
MCContext &Ctx) const {
585613
MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
586614
? MCRelocationInfoCtorFn
587615
: llvm::createMCRelocationInfo;
588-
return Fn(Triple(TT), Ctx);
616+
return Fn(TT, Ctx);
617+
}
618+
619+
[[deprecated("Use overload accepting Triple instead")]]
620+
MCSymbolizer *
621+
createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
622+
LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
623+
MCContext *Ctx,
624+
std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
625+
return createMCSymbolizer(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
626+
std::move(RelInfo));
589627
}
590628

591629
/// createMCSymbolizer - Create a target specific MCSymbolizer.
@@ -601,14 +639,13 @@ class Target {
601639
/// \param RelInfo The relocation information for this target. Takes
602640
/// ownership.
603641
MCSymbolizer *
604-
createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
642+
createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
605643
LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
606644
MCContext *Ctx,
607645
std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
608646
MCSymbolizerCtorTy Fn =
609647
MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
610-
return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
611-
std::move(RelInfo));
648+
return Fn(TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx, std::move(RelInfo));
612649
}
613650

614651
/// createCustomBehaviour - Create a target specific CustomBehaviour.

llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ static cl::opt<bool> EnableNoTrapAfterNoreturn(
4545
"after noreturn calls, even if --trap-unreachable is set."));
4646

4747
void CodeGenTargetMachineImpl::initAsmInfo() {
48-
MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
48+
MRI.reset(TheTarget.createMCRegInfo(getTargetTriple()));
4949
assert(MRI && "Unable to create reg info");
5050
MII.reset(TheTarget.createMCInstrInfo());
5151
assert(MII && "Unable to create instruction info");
5252
// FIXME: Having an MCSubtargetInfo on the target machine is a hack due
5353
// to some backends having subtarget feature dependent module level
5454
// code generation. This is similar to the hack in the AsmPrinter for
5555
// module level assembly etc.
56-
STI.reset(TheTarget.createMCSubtargetInfo(
57-
getTargetTriple().str(), getTargetCPU(), getTargetFeatureString()));
56+
STI.reset(TheTarget.createMCSubtargetInfo(getTargetTriple(), getTargetCPU(),
57+
getTargetFeatureString()));
5858
assert(STI && "Unable to create subtarget info");
5959

60-
MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
61-
*MRI, getTargetTriple().str(), Options.MCOptions);
60+
MCAsmInfo *TmpAsmInfo =
61+
TheTarget.createMCAsmInfo(*MRI, getTargetTriple(), Options.MCOptions);
6262
// TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
6363
// and if the old one gets included then MCAsmInfo will be NULL and
6464
// we'll crash later.

llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Error DwarfStreamer::init(Triple TheTriple,
5555
TripleName = TheTriple.getTriple();
5656

5757
// Create all the MC Objects.
58-
MRI.reset(TheTarget->createMCRegInfo(TripleName));
58+
MRI.reset(TheTarget->createMCRegInfo(TheTriple));
5959
if (!MRI)
6060
return createStringError(std::errc::invalid_argument,
6161
"no register info for target %s",
@@ -64,12 +64,12 @@ Error DwarfStreamer::init(Triple TheTriple,
6464
MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
6565
MCOptions.AsmVerbose = true;
6666
MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory;
67-
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
67+
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TheTriple, MCOptions));
6868
if (!MAI)
6969
return createStringError(std::errc::invalid_argument,
7070
"no asm info for target %s", TripleName.c_str());
7171

72-
MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
72+
MSTI.reset(TheTarget->createMCSubtargetInfo(TheTriple, "", ""));
7373
if (!MSTI)
7474
return createStringError(std::errc::invalid_argument,
7575
"no subtarget info for target %s",

0 commit comments

Comments
 (0)