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
91 lines
2.6 KiB
C++
91 lines
2.6 KiB
C++
//===- bolt/Passes/PLTCall.h - PLT call optimization ----------------------===//
|
|
//
|
|
// 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 PLTCall class, which replaces calls to PLT entries
|
|
// with indirect calls against GOT.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "bolt/Passes/PLTCall.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#define DEBUG_TYPE "bolt-plt"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace opts {
|
|
|
|
extern cl::OptionCategory BoltOptCategory;
|
|
|
|
cl::opt<bolt::PLTCall::OptType>
|
|
PLT("plt",
|
|
cl::desc("optimize PLT calls (requires linking with -znow)"),
|
|
cl::init(bolt::PLTCall::OT_NONE),
|
|
cl::values(clEnumValN(bolt::PLTCall::OT_NONE,
|
|
"none",
|
|
"do not optimize PLT calls"),
|
|
clEnumValN(bolt::PLTCall::OT_HOT,
|
|
"hot",
|
|
"optimize executed (hot) PLT calls"),
|
|
clEnumValN(bolt::PLTCall::OT_ALL,
|
|
"all",
|
|
"optimize all PLT calls")),
|
|
cl::ZeroOrMore,
|
|
cl::cat(BoltOptCategory));
|
|
|
|
}
|
|
|
|
namespace llvm {
|
|
namespace bolt {
|
|
|
|
Error PLTCall::runOnFunctions(BinaryContext &BC) {
|
|
if (opts::PLT == OT_NONE)
|
|
return Error::success();
|
|
|
|
uint64_t NumCallsOptimized = 0;
|
|
for (auto &It : BC.getBinaryFunctions()) {
|
|
BinaryFunction &Function = It.second;
|
|
if (!shouldOptimize(Function))
|
|
continue;
|
|
|
|
if (opts::PLT == OT_HOT &&
|
|
Function.getExecutionCount() == BinaryFunction::COUNT_NO_PROFILE)
|
|
continue;
|
|
|
|
for (BinaryBasicBlock &BB : Function) {
|
|
if (opts::PLT == OT_HOT && !BB.getKnownExecutionCount())
|
|
continue;
|
|
|
|
for (MCInst &Instr : BB) {
|
|
if (!BC.MIB->isCall(Instr))
|
|
continue;
|
|
const MCSymbol *CallSymbol = BC.MIB->getTargetSymbol(Instr);
|
|
if (!CallSymbol)
|
|
continue;
|
|
const BinaryFunction *CalleeBF = BC.getFunctionForSymbol(CallSymbol);
|
|
if (!CalleeBF || !CalleeBF->isPLTFunction())
|
|
continue;
|
|
BC.MIB->convertCallToIndirectCall(Instr, CalleeBF->getPLTSymbol(),
|
|
BC.Ctx.get());
|
|
BC.MIB->addAnnotation(Instr, "PLTCall", true);
|
|
++NumCallsOptimized;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (NumCallsOptimized) {
|
|
BC.RequiresZNow = true;
|
|
BC.outs() << "BOLT-INFO: " << NumCallsOptimized
|
|
<< " PLT calls in the binary were optimized.\n";
|
|
}
|
|
return Error::success();
|
|
}
|
|
|
|
} // namespace bolt
|
|
} // namespace llvm
|