[PATCH] D149526: [JITLink][RISCV] Implement linker relaxation

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 29 12:12:54 PDT 2023


jobnoorman created this revision.
jobnoorman added reviewers: lhames, StephenFan, MaskRay.
Herald added subscribers: bd1976llvm, asb, luke, pmatos, VincentWu, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, mgrang, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
jobnoorman requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD.
Herald added a project: LLVM.

This patch is essentially an adaption of LLD's algorithm to JITLink.
Currently, only relaxing R_RISCV_CALL(_PLT) and R_RISCV_ALIGN is
implemented, other relocations can follow later.

>From a high level, the algorithm works as follows. In the first phase
(relaxBlock), we iteratively try to relax all instructions that have a
R_RISCV_RELAX relocation:

- If, based on the current symbol values, an instruction sequence can be relaxed (i.e., replaced by a shorter instruction), we record how many bytes would be removed, the new instruction (Writes), and the new relocation type (EdgeKinds).
- We keep track of the total number of bytes that got removed up to each relocation in the RelocDeltas array. This is the cumulative sum of the number of bytes removed for each relocation.
- Symbol values and sizes are updated based on the number of removed bytes.
- If for any relocation, the current RelocDeltas value doesn't match the one from the previous iteration, something changed and we need to run another iteration as some symbols might now have different values.

In the second phase (finalizeBlockRelax), all code is moved based on
RelocDeltas, the relaxed instructions are rewritten using Writes, and
R_RISCV_ALIGN is handled (moving instructions to ensure alignment and
inserting the correct NOP-sequence if needed). Finally, edge kinds and
offsets are updated and all R_RISCV_RELAX and R_RISCV_ALIGN edges are
removed (they are not needed anymore for the fixup linking stage).

Linker relaxation is implemented as a pass and added to PreFixupPasses
in the default configuration on RISC-V.

Since linker relaxation removes instructions, the memory for blocks
should ideally be reallocated. However, I believe this is currently not
possible in JITLink. Therefore, relaxation directly modifies the memory
of blocks, reducing the number of instructions but not the size of
blocks. I'm not very familiar with JITLink's memory allocators so I
might be overlooking something here, though.

Note on testing: some of the tests rely on the debug output of
llvm-jitlink. The main reason for this is the verification of symbol
sizes (which may change due to relaxation). I don't believe this can be
done using jitlink-check checks alone.

Note that there is a slightly unrelated change that makes
Symbol::setOffset public to be able to update symbol offsets during
relaxation. I felt this change didn't warrant a separate patch but I can
split it off if necessary.

@MaskRay: I've added you as a reviewer since you're the original author
of the LLD algorithm. The algorithm in this patch is mostly the same as
yours but I made some minor modifications. The main one is that I got
rid of the valueDelta map you use in relax() to keep track of of the
deltas of symbols in the previous iteration in order to correctly update
symbol values. I believe this is unnecessary and the same can be
accomplished by using the original symbol value stored in offset field
in its anchor. When making the same change in LLD, all tests still pass.

Depends on D149522 <https://reviews.llvm.org/D149522> and D149523 <https://reviews.llvm.org/D149523> and D149524 <https://reviews.llvm.org/D149524> and D149525 <https://reviews.llvm.org/D149525>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149526

Files:
  llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
  llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
  llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
  llvm/lib/ExecutionEngine/JITLink/riscv.cpp
  llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_align.s
  llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_align_rvc.s
  llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_call.s
  llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_call_boundary.s
  llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_call_rvc.s

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149526.518223.patch
Type: text/x-patch
Size: 37162 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230429/46352909/attachment.bin>


More information about the llvm-commits mailing list