[PATCH] D159089: [BOLT][RISCV] Add support for linker relaxation

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 29 06:17:45 PDT 2023


jobnoorman created this revision.
jobnoorman added reviewers: rafauler, maksfb, yota9, Amir.
Herald added subscribers: asb, luke, treapster, sunshaoce, pmatos, ayermolo, VincentWu, vkmr, frasercrmck, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, arichardson.
Herald added a project: All.
jobnoorman requested review of this revision.
Herald added subscribers: llvm-commits, wangpc, eopXD, MaskRay.
Herald added a project: LLVM.

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 <https://reviews.llvm.org/D159082>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159089

Files:
  bolt/include/bolt/Core/MCPlusBuilder.h
  bolt/include/bolt/Passes/FixRISCVCallsPass.h
  bolt/lib/Core/BinaryContext.cpp
  bolt/lib/Passes/FixRISCVCallsPass.cpp
  bolt/lib/Rewrite/JITLinkLinker.cpp
  bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp
  bolt/test/RISCV/internal-func-reloc.s
  bolt/test/RISCV/lit.local.cfg
  bolt/test/RISCV/relax.s
  bolt/test/RISCV/reloc-call.s

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159089.554289.patch
Type: text/x-patch
Size: 10276 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230829/a4e47056/attachment.bin>


More information about the llvm-commits mailing list