Skip to content

Commit c0febca

Browse files
authored
[BOLT][NFC] Refactor BC::createBinaryContext for #81346 (#87172)
1 parent a67b932 commit c0febca

File tree

7 files changed

+48
-58
lines changed

7 files changed

+48
-58
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ class BinaryContext {
265265

266266
public:
267267
static Expected<std::unique_ptr<BinaryContext>>
268-
createBinaryContext(const ObjectFile *File, bool IsPIC,
268+
createBinaryContext(Triple TheTriple, StringRef InputFileName,
269+
SubtargetFeatures *Features, bool IsPIC,
269270
std::unique_ptr<DWARFContext> DwCtx,
270271
JournalingStreams Logger);
271272

bolt/lib/Core/BinaryContext.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,30 @@ BinaryContext::~BinaryContext() {
162162

163163
/// Create BinaryContext for a given architecture \p ArchName and
164164
/// triple \p TripleName.
165-
Expected<std::unique_ptr<BinaryContext>>
166-
BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
167-
std::unique_ptr<DWARFContext> DwCtx,
168-
JournalingStreams Logger) {
165+
Expected<std::unique_ptr<BinaryContext>> BinaryContext::createBinaryContext(
166+
Triple TheTriple, StringRef InputFileName, SubtargetFeatures *Features,
167+
bool IsPIC, std::unique_ptr<DWARFContext> DwCtx, JournalingStreams Logger) {
169168
StringRef ArchName = "";
170169
std::string FeaturesStr = "";
171-
switch (File->getArch()) {
170+
switch (TheTriple.getArch()) {
172171
case llvm::Triple::x86_64:
172+
if (Features)
173+
return createFatalBOLTError(
174+
"x86_64 target does not use SubtargetFeatures");
173175
ArchName = "x86-64";
174176
FeaturesStr = "+nopl";
175177
break;
176178
case llvm::Triple::aarch64:
179+
if (Features)
180+
return createFatalBOLTError(
181+
"AArch64 target does not use SubtargetFeatures");
177182
ArchName = "aarch64";
178183
FeaturesStr = "+all";
179184
break;
180185
case llvm::Triple::riscv64: {
181186
ArchName = "riscv64";
182-
Expected<SubtargetFeatures> Features = File->getFeatures();
183-
184-
if (auto E = Features.takeError())
185-
return std::move(E);
186-
187+
if (!Features)
188+
return createFatalBOLTError("RISCV target needs SubtargetFeatures");
187189
// We rely on relaxation for some transformations (e.g., promoting all calls
188190
// to PseudoCALL and then making JITLink relax them). Since the relax
189191
// feature is not stored in the object file, we manually enable it.
@@ -196,12 +198,11 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
196198
"BOLT-ERROR: Unrecognized machine in ELF file");
197199
}
198200

199-
auto TheTriple = std::make_unique<Triple>(File->makeTriple());
200-
const std::string TripleName = TheTriple->str();
201+
const std::string TripleName = TheTriple.str();
201202

202203
std::string Error;
203204
const Target *TheTarget =
204-
TargetRegistry::lookupTarget(std::string(ArchName), *TheTriple, Error);
205+
TargetRegistry::lookupTarget(std::string(ArchName), TheTriple, Error);
205206
if (!TheTarget)
206207
return createStringError(make_error_code(std::errc::not_supported),
207208
Twine("BOLT-ERROR: ", Error));
@@ -240,13 +241,13 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
240241
Twine("BOLT-ERROR: no instruction info for target ", TripleName));
241242

242243
std::unique_ptr<MCContext> Ctx(
243-
new MCContext(*TheTriple, AsmInfo.get(), MRI.get(), STI.get()));
244+
new MCContext(TheTriple, AsmInfo.get(), MRI.get(), STI.get()));
244245
std::unique_ptr<MCObjectFileInfo> MOFI(
245246
TheTarget->createMCObjectFileInfo(*Ctx, IsPIC));
246247
Ctx->setObjectFileInfo(MOFI.get());
247248
// We do not support X86 Large code model. Change this in the future.
248249
bool Large = false;
249-
if (TheTriple->getArch() == llvm::Triple::aarch64)
250+
if (TheTriple.getArch() == llvm::Triple::aarch64)
250251
Large = true;
251252
unsigned LSDAEncoding =
252253
Large ? dwarf::DW_EH_PE_absptr : dwarf::DW_EH_PE_udata4;
@@ -273,7 +274,7 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
273274

274275
int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
275276
std::unique_ptr<MCInstPrinter> InstructionPrinter(
276-
TheTarget->createMCInstPrinter(*TheTriple, AsmPrinterVariant, *AsmInfo,
277+
TheTarget->createMCInstPrinter(TheTriple, AsmPrinterVariant, *AsmInfo,
277278
*MII, *MRI));
278279
if (!InstructionPrinter)
279280
return createStringError(
@@ -285,8 +286,8 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
285286
TheTarget->createMCCodeEmitter(*MII, *Ctx));
286287

287288
auto BC = std::make_unique<BinaryContext>(
288-
std::move(Ctx), std::move(DwCtx), std::move(TheTriple), TheTarget,
289-
std::string(TripleName), std::move(MCE), std::move(MOFI),
289+
std::move(Ctx), std::move(DwCtx), std::make_unique<Triple>(TheTriple),
290+
TheTarget, std::string(TripleName), std::move(MCE), std::move(MOFI),
290291
std::move(AsmInfo), std::move(MII), std::move(STI),
291292
std::move(InstructionPrinter), std::move(MIA), nullptr, std::move(MRI),
292293
std::move(DisAsm), Logger);
@@ -296,7 +297,7 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
296297
BC->MAB = std::unique_ptr<MCAsmBackend>(
297298
BC->TheTarget->createMCAsmBackend(*BC->STI, *BC->MRI, MCTargetOptions()));
298299

299-
BC->setFilename(File->getFileName());
300+
BC->setFilename(InputFileName);
300301

301302
BC->HasFixedLoadAddress = !IsPIC;
302303

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,7 @@ namespace {
16851685
std::unique_ptr<BinaryContext>
16861686
createDwarfOnlyBC(const object::ObjectFile &File) {
16871687
return cantFail(BinaryContext::createBinaryContext(
1688-
&File, false,
1688+
File.makeTriple(), File.getFileName(), nullptr, false,
16891689
DWARFContext::create(File, DWARFContext::ProcessDebugRelocations::Ignore,
16901690
nullptr, "", WithColor::defaultErrorHandler,
16911691
WithColor::defaultWarningHandler),

bolt/lib/Rewrite/MachORewriteInstance.cpp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "bolt/Rewrite/BinaryPassManager.h"
1919
#include "bolt/Rewrite/ExecutableFileMemoryManager.h"
2020
#include "bolt/Rewrite/JITLinkLinker.h"
21+
#include "bolt/Rewrite/RewriteInstance.h"
2122
#include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h"
2223
#include "bolt/Utils/Utils.h"
2324
#include "llvm/MC/MCObjectStreamer.h"
@@ -54,37 +55,6 @@ extern cl::opt<unsigned> Verbosity;
5455
namespace llvm {
5556
namespace bolt {
5657

57-
extern MCPlusBuilder *createX86MCPlusBuilder(const MCInstrAnalysis *,
58-
const MCInstrInfo *,
59-
const MCRegisterInfo *,
60-
const MCSubtargetInfo *);
61-
extern MCPlusBuilder *createAArch64MCPlusBuilder(const MCInstrAnalysis *,
62-
const MCInstrInfo *,
63-
const MCRegisterInfo *,
64-
const MCSubtargetInfo *);
65-
66-
namespace {
67-
68-
MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
69-
const MCInstrAnalysis *Analysis,
70-
const MCInstrInfo *Info,
71-
const MCRegisterInfo *RegInfo,
72-
const MCSubtargetInfo *STI) {
73-
#ifdef X86_AVAILABLE
74-
if (Arch == Triple::x86_64)
75-
return createX86MCPlusBuilder(Analysis, Info, RegInfo, STI);
76-
#endif
77-
78-
#ifdef AARCH64_AVAILABLE
79-
if (Arch == Triple::aarch64)
80-
return createAArch64MCPlusBuilder(Analysis, Info, RegInfo, STI);
81-
#endif
82-
83-
llvm_unreachable("architecture unsupported by MCPlusBuilder");
84-
}
85-
86-
} // anonymous namespace
87-
8858
#define DEBUG_TYPE "bolt"
8959

9060
Expected<std::unique_ptr<MachORewriteInstance>>
@@ -103,7 +73,8 @@ MachORewriteInstance::MachORewriteInstance(object::MachOObjectFile *InputFile,
10373
: InputFile(InputFile), ToolPath(ToolPath) {
10474
ErrorAsOutParameter EAO(&Err);
10575
auto BCOrErr = BinaryContext::createBinaryContext(
106-
InputFile, /* IsPIC */ true, DWARFContext::create(*InputFile),
76+
InputFile->makeTriple(), InputFile->getFileName(), nullptr,
77+
/* IsPIC */ true, DWARFContext::create(*InputFile),
10778
{llvm::outs(), llvm::errs()});
10879
if (Error E = BCOrErr.takeError()) {
10980
Err = std::move(E);

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ namespace bolt {
268268

269269
extern const char *BoltRevision;
270270

271+
// Weird location for createMCPlusBuilder, but this is here to avoid a
272+
// cyclic dependency of libCore (its natural place) and libTarget. libRewrite
273+
// can depend on libTarget, but not libCore. Since libRewrite is the only
274+
// user of this function, we define it here.
271275
MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
272276
const MCInstrAnalysis *Analysis,
273277
const MCInstrInfo *Info,
@@ -345,8 +349,21 @@ RewriteInstance::RewriteInstance(ELFObjectFileBase *File, const int Argc,
345349
Stderr.SetUnbuffered();
346350
LLVM_DEBUG(dbgs().SetUnbuffered());
347351

352+
// Read RISCV subtarget features from input file
353+
std::unique_ptr<SubtargetFeatures> Features;
354+
Triple TheTriple = File->makeTriple();
355+
if (TheTriple.getArch() == llvm::Triple::riscv64) {
356+
Expected<SubtargetFeatures> FeaturesOrErr = File->getFeatures();
357+
if (auto E = FeaturesOrErr.takeError()) {
358+
Err = std::move(E);
359+
return;
360+
} else {
361+
Features.reset(new SubtargetFeatures(*FeaturesOrErr));
362+
}
363+
}
364+
348365
auto BCOrErr = BinaryContext::createBinaryContext(
349-
File, IsPIC,
366+
TheTriple, File->getFileName(), Features.get(), IsPIC,
350367
DWARFContext::create(*File, DWARFContext::ProcessDebugRelocations::Ignore,
351368
nullptr, opts::DWPPathName,
352369
WithColor::defaultErrorHandler,

bolt/unittests/Core/BinaryContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ struct BinaryContextTester : public testing::TestWithParam<Triple::ArchType> {
4040

4141
void initializeBOLT() {
4242
BC = cantFail(BinaryContext::createBinaryContext(
43-
ObjFile.get(), true, DWARFContext::create(*ObjFile.get()),
44-
{llvm::outs(), llvm::errs()}));
43+
ObjFile->makeTriple(), ObjFile->getFileName(), nullptr, true,
44+
DWARFContext::create(*ObjFile.get()), {llvm::outs(), llvm::errs()}));
4545
ASSERT_FALSE(!BC);
4646
}
4747

bolt/unittests/Core/MCPlusBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ struct MCPlusBuilderTester : public testing::TestWithParam<Triple::ArchType> {
5050

5151
void initializeBolt() {
5252
BC = cantFail(BinaryContext::createBinaryContext(
53-
ObjFile.get(), true, DWARFContext::create(*ObjFile.get()),
54-
{llvm::outs(), llvm::errs()}));
53+
ObjFile->makeTriple(), ObjFile->getFileName(), nullptr, true,
54+
DWARFContext::create(*ObjFile.get()), {llvm::outs(), llvm::errs()}));
5555
ASSERT_FALSE(!BC);
5656
BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(
5757
createMCPlusBuilder(GetParam(), BC->MIA.get(), BC->MII.get(),

0 commit comments

Comments
 (0)