Calls on RISC-V are typically compiled to `auipc`/`jalr` pairs to allow a maximum target range (32-bit pc-relative). In order to optimize calls to near targets, linker relaxation may replace those pairs with, for example, single `jal` instructions. To allow BOLT to freely reassign function addresses in relaxed binaries, this patch proposes the following approach: - Expand all relaxed calls back to `auipc`/`jalr`; - Rely on JITLink to relax those back to shorter forms where possible. This is implemented by detecting all possible call instructions and replacing them with `PseudoCALL` (or `PseudoTAIL`) instructions. The RISC-V backend then expands those and adds the necessary relocations for relaxation. Since BOLT generally ignores pseudo instruction, this patch makes `MCPlusBuilder::isPseudo` virtual so that `RISCVMCPlusBuilder` can override it to exclude `PseudoCALL` and `PseudoTAIL`. To ensure JITLink knows about the correct section addresses while relaxing, reassignment of addresses has been moved to a post-allocation pass. Note that this is probably the time it had to be done in the first place since in `notifyResolved` (where it was done before), all symbols are supposed to be resolved already. Depends on D159082 Reviewed By: maksfb Differential Revision: https://reviews.llvm.org/D159089
68 lines
1.5 KiB
C++
68 lines
1.5 KiB
C++
#include "bolt/Passes/FixRISCVCallsPass.h"
|
|
#include "bolt/Core/ParallelUtilities.h"
|
|
|
|
#include <iterator>
|
|
|
|
using namespace llvm;
|
|
|
|
namespace llvm {
|
|
namespace bolt {
|
|
|
|
void FixRISCVCallsPass::runOnFunction(BinaryFunction &BF) {
|
|
auto &BC = BF.getBinaryContext();
|
|
auto &MIB = BC.MIB;
|
|
auto *Ctx = BC.Ctx.get();
|
|
|
|
for (auto &BB : BF) {
|
|
for (auto II = BB.begin(); II != BB.end();) {
|
|
if (MIB->isCall(*II) && !MIB->isIndirectCall(*II)) {
|
|
auto *Target = MIB->getTargetSymbol(*II);
|
|
assert(Target && "Cannot find call target");
|
|
|
|
auto L = BC.scopeLock();
|
|
|
|
if (MIB->isTailCall(*II))
|
|
MIB->createTailCall(*II, Target, Ctx);
|
|
else
|
|
MIB->createCall(*II, Target, Ctx);
|
|
|
|
++II;
|
|
continue;
|
|
}
|
|
|
|
auto NextII = std::next(II);
|
|
|
|
if (NextII == BB.end())
|
|
break;
|
|
|
|
if (MIB->isRISCVCall(*II, *NextII)) {
|
|
auto *Target = MIB->getTargetSymbol(*II);
|
|
assert(Target && "Cannot find call target");
|
|
|
|
auto L = BC.scopeLock();
|
|
MIB->createCall(*II, Target, Ctx);
|
|
II = BB.eraseInstruction(NextII);
|
|
continue;
|
|
}
|
|
|
|
++II;
|
|
}
|
|
}
|
|
}
|
|
|
|
void FixRISCVCallsPass::runOnFunctions(BinaryContext &BC) {
|
|
if (!BC.isRISCV() || !BC.HasRelocations)
|
|
return;
|
|
|
|
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
|
|
runOnFunction(BF);
|
|
};
|
|
|
|
ParallelUtilities::runOnEachFunction(
|
|
BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun, nullptr,
|
|
"FixRISCVCalls");
|
|
}
|
|
|
|
} // namespace bolt
|
|
} // namespace llvm
|