mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-25 10:46:06 +00:00

Make core BOLT functionality more friendly to being used as a library instead of in our standalone driver llvm-bolt. To accomplish this, we augment BinaryContext with journaling streams that are to be used by most BOLT code whenever something needs to be logged to the screen. Users of the library can decide if logs should be printed to a file, no file or to the screen, as before. To illustrate this, this patch adds a new option `--log-file` that allows the user to redirect BOLT logging to a file on disk or completely hide it by using `--log-file=/dev/null`. Future BOLT code should now use `BinaryContext::outs()` for printing important messages instead of `llvm::outs()`. A new test log.test enforces this by verifying that no strings are print to screen once the `--log-file` option is used. In previous patches we also added a new BOLTError class to report common and fatal errors, so code shouldn't call exit(1) now. To easily handle problems as before (by quitting with exit(1)), callers can now use `BinaryContext::logBOLTErrorsAndQuitOnFatal(Error)` whenever code needs to deal with BOLT errors. To test this, we have fatal.s that checks we are correctly quitting and printing a fatal error to the screen. Because this is a significant change by itself, not all code was yet ported. Code from Profiler libs (DataAggregator and friends) still print errors directly to screen. Co-authored-by: Rafael Auler <rafaelauler@fb.com> Test Plan: NFC
146 lines
5.0 KiB
C++
146 lines
5.0 KiB
C++
//===- bolt/Passes/PatchEntries.cpp - Pass for patching function entries --===//
|
|
//
|
|
// 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 PatchEntries class that is used for patching
|
|
// the original function entry points.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "bolt/Passes/PatchEntries.h"
|
|
#include "bolt/Utils/NameResolver.h"
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
namespace opts {
|
|
|
|
extern llvm::cl::OptionCategory BoltCategory;
|
|
|
|
extern llvm::cl::opt<unsigned> Verbosity;
|
|
|
|
llvm::cl::opt<bool>
|
|
ForcePatch("force-patch",
|
|
llvm::cl::desc("force patching of original entry points"),
|
|
llvm::cl::Hidden, llvm::cl::cat(BoltCategory));
|
|
}
|
|
|
|
namespace llvm {
|
|
namespace bolt {
|
|
|
|
Error PatchEntries::runOnFunctions(BinaryContext &BC) {
|
|
if (!opts::ForcePatch) {
|
|
// Mark the binary for patching if we did not create external references
|
|
// for original code in any of functions we are not going to emit.
|
|
bool NeedsPatching = llvm::any_of(
|
|
llvm::make_second_range(BC.getBinaryFunctions()),
|
|
[&](BinaryFunction &BF) {
|
|
return !BC.shouldEmit(BF) && !BF.hasExternalRefRelocations();
|
|
});
|
|
|
|
if (!NeedsPatching)
|
|
return Error::success();
|
|
}
|
|
|
|
if (opts::Verbosity >= 1)
|
|
BC.outs() << "BOLT-INFO: patching entries in original code\n";
|
|
|
|
// Calculate the size of the patch.
|
|
static size_t PatchSize = 0;
|
|
if (!PatchSize) {
|
|
InstructionListType Seq;
|
|
BC.MIB->createLongTailCall(Seq, BC.Ctx->createTempSymbol(), BC.Ctx.get());
|
|
PatchSize = BC.computeCodeSize(Seq.begin(), Seq.end());
|
|
}
|
|
|
|
for (auto &BFI : BC.getBinaryFunctions()) {
|
|
BinaryFunction &Function = BFI.second;
|
|
|
|
// Patch original code only for functions that will be emitted.
|
|
if (!BC.shouldEmit(Function))
|
|
continue;
|
|
|
|
// Check if we can skip patching the function.
|
|
if (!opts::ForcePatch && !Function.hasEHRanges() &&
|
|
Function.getSize() < PatchThreshold)
|
|
continue;
|
|
|
|
// List of patches for function entries. We either successfully patch
|
|
// all entries or, if we cannot patch one or more, do no patch any and
|
|
// mark the function as ignorable.
|
|
std::vector<Patch> PendingPatches;
|
|
|
|
uint64_t NextValidByte = 0; // offset of the byte past the last patch
|
|
bool Success = Function.forEachEntryPoint([&](uint64_t Offset,
|
|
const MCSymbol *Symbol) {
|
|
if (Offset < NextValidByte) {
|
|
if (opts::Verbosity >= 1)
|
|
BC.outs() << "BOLT-INFO: unable to patch entry point in " << Function
|
|
<< " at offset 0x" << Twine::utohexstr(Offset) << '\n';
|
|
return false;
|
|
}
|
|
|
|
PendingPatches.emplace_back(Patch{Symbol, Function.getAddress() + Offset,
|
|
Function.getFileOffset() + Offset,
|
|
Function.getOriginSection()});
|
|
NextValidByte = Offset + PatchSize;
|
|
if (NextValidByte > Function.getMaxSize()) {
|
|
if (opts::Verbosity >= 1)
|
|
BC.outs() << "BOLT-INFO: function " << Function
|
|
<< " too small to patch its entry point\n";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
if (!Success) {
|
|
// We can't change output layout for AArch64 due to LongJmp pass
|
|
if (BC.isAArch64()) {
|
|
if (opts::ForcePatch) {
|
|
BC.errs() << "BOLT-ERROR: unable to patch entries in " << Function
|
|
<< "\n";
|
|
return createFatalBOLTError("");
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
// If the original function entries cannot be patched, then we cannot
|
|
// safely emit new function body.
|
|
BC.errs() << "BOLT-WARNING: failed to patch entries in " << Function
|
|
<< ". The function will not be optimized.\n";
|
|
Function.setIgnored();
|
|
continue;
|
|
}
|
|
|
|
for (Patch &Patch : PendingPatches) {
|
|
BinaryFunction *PatchFunction = BC.createInjectedBinaryFunction(
|
|
NameResolver::append(Patch.Symbol->getName(), ".org.0"));
|
|
// Force the function to be emitted at the given address.
|
|
PatchFunction->setOutputAddress(Patch.Address);
|
|
PatchFunction->setFileOffset(Patch.FileOffset);
|
|
PatchFunction->setOriginSection(Patch.Section);
|
|
|
|
InstructionListType Seq;
|
|
BC.MIB->createLongTailCall(Seq, Patch.Symbol, BC.Ctx.get());
|
|
PatchFunction->addBasicBlock()->addInstructions(Seq);
|
|
|
|
// Verify the size requirements.
|
|
uint64_t HotSize, ColdSize;
|
|
std::tie(HotSize, ColdSize) = BC.calculateEmittedSize(*PatchFunction);
|
|
assert(!ColdSize && "unexpected cold code");
|
|
assert(HotSize <= PatchSize && "max patch size exceeded");
|
|
}
|
|
|
|
Function.setIsPatched(true);
|
|
}
|
|
return Error::success();
|
|
}
|
|
|
|
} // end namespace bolt
|
|
} // end namespace llvm
|