Skip to content

Commit 310473c

Browse files
committed
[JITLink][RISCV] Implement linker relaxation
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. Reviewed By: lhames Differential Revision: https://reviews.llvm.org/D149526
1 parent 348d0a6 commit 310473c

File tree

9 files changed

+973
-20
lines changed

9 files changed

+973
-20
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ class Symbol {
563563
/// Returns the offset for this symbol within the underlying addressable.
564564
orc::ExecutorAddrDiff getOffset() const { return Offset; }
565565

566+
void setOffset(orc::ExecutorAddrDiff NewOffset) {
567+
assert(NewOffset < getBlock().getSize() && "Offset out of range");
568+
Offset = NewOffset;
569+
}
570+
566571
/// Returns the address of this symbol.
567572
orc::ExecutorAddr getAddress() const { return Base->getAddress() + Offset; }
568573

@@ -661,11 +666,6 @@ class Symbol {
661666

662667
void setBlock(Block &B) { Base = &B; }
663668

664-
void setOffset(orc::ExecutorAddrDiff NewOffset) {
665-
assert(NewOffset <= MaxOffset && "Offset out of range");
666-
Offset = NewOffset;
667-
}
668-
669669
static constexpr uint64_t MaxOffset = (1ULL << 59) - 1;
670670

671671
// FIXME: A char* or SymbolStringPtr may pack better.

llvm/include/llvm/ExecutionEngine/JITLink/riscv.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ enum EdgeKind_riscv : Edge::Kind {
202202
/// Fixup expression:
203203
/// Fixup <- (Target - Fixup + Addend)
204204
R_RISCV_32_PCREL,
205+
206+
/// An auipc/jalr pair eligible for linker relaxation.
207+
///
208+
/// Linker relaxation will replace this with R_RISCV_RVC_JUMP or R_RISCV_JAL
209+
/// if it succeeds, or with R_RISCV_CALL_PLT if it fails.
210+
CallRelaxable,
211+
212+
/// Alignment requirement used by linker relaxation.
213+
///
214+
/// Linker relaxation will use this to ensure all code sequences are properly
215+
/// aligned and then remove these edges from the graph.
216+
AlignRelaxable,
205217
};
206218

207219
/// Returns a string name for the given riscv edge. For debugging purposes

0 commit comments

Comments
 (0)