Skip to content

[JITLink][LoongArch] Add R_LARCH_{B16,B21} relocations support #121096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions llvm/include/llvm/ExecutionEngine/JITLink/loongarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,50 @@ enum EdgeKind_loongarch : Edge::Kind {
///
Pointer32,

/// A 16-bit PC-relative branch.
///
/// Represents a PC-relative branch to a target within +/-128Kb. The target
/// must be 4-byte aligned.
///
/// Fixup expression:
/// Fixup <- (Target - Fixup + Addend) >> 2 : int16
///
/// Notes:
/// The '16' in the name refers to the number operand bits and follows the
/// naming convention used by the corresponding ELF relocations. Since the low
/// two bits must be zero (because of the 4-byte alignment of the target) the
/// operand is effectively a signed 18-bit number.
///
/// Errors:
/// - The result of the unshifted part of the fixup expression must be
/// 4-byte aligned otherwise an alignment error will be returned.
/// - The result of the fixup expression must fit into an int16 otherwise an
/// out-of-range error will be returned.
///
Branch16PCRel,

/// A 21-bit PC-relative branch.
///
/// Represents a PC-relative branch to a target within +/-4Mb. The Target must
/// be 4-byte aligned.
///
/// Fixup expression:
/// Fixup <- (Target - Fixup + Addend) >> 2 : int21
///
/// Notes:
/// The '21' in the name refers to the number operand bits and follows the
/// naming convention used by the corresponding ELF relocations. Since the low
/// two bits must be zero (because of the 4-byte alignment of the target) the
/// operand is effectively a signed 23-bit number.
///
/// Errors:
/// - The result of the unshifted part of the fixup expression must be
/// 4-byte aligned otherwise an alignment error will be returned.
/// - The result of the fixup expression must fit into an int21 otherwise an
/// out-of-range error will be returned.
///
Branch21PCRel,

/// A 26-bit PC-relative branch.
///
/// Represents a PC-relative call or branch to a target within +/-128Mb. The
Expand Down Expand Up @@ -213,6 +257,37 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E) {
*(ulittle32_t *)FixupPtr = Value;
break;
}
case Branch16PCRel: {
int64_t Value = TargetAddress - FixupAddress + Addend;

if (!isInt<18>(Value))
return makeTargetOutOfRangeError(G, B, E);

if (!isShiftedInt<16, 2>(Value))
return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);

uint32_t RawInstr = *(little32_t *)FixupPtr;
uint32_t Imm = static_cast<uint32_t>(Value >> 2);
uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
*(little32_t *)FixupPtr = RawInstr | Imm15_0;
break;
}
case Branch21PCRel: {
int64_t Value = TargetAddress - FixupAddress + Addend;

if (!isInt<23>(Value))
return makeTargetOutOfRangeError(G, B, E);

if (!isShiftedInt<21, 2>(Value))
return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);

uint32_t RawInstr = *(little32_t *)FixupPtr;
uint32_t Imm = static_cast<uint32_t>(Value >> 2);
uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
uint32_t Imm20_16 = extractBits(Imm, /*Hi=*/20, /*Lo=*/16);
*(little32_t *)FixupPtr = RawInstr | Imm15_0 | Imm20_16;
break;
}
case Branch26PCRel: {
int64_t Value = TargetAddress - FixupAddress + Addend;

Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class ELFLinkGraphBuilder_loongarch : public ELFLinkGraphBuilder<ELFT> {
return Pointer32;
case ELF::R_LARCH_32_PCREL:
return Delta32;
case ELF::R_LARCH_B16:
return Branch16PCRel;
case ELF::R_LARCH_B21:
return Branch21PCRel;
case ELF::R_LARCH_B26:
return Branch26PCRel;
case ELF::R_LARCH_PCALA_HI20:
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/ExecutionEngine/JITLink/loongarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const char *getEdgeKindName(Edge::Kind K) {
KIND_NAME_CASE(Delta32)
KIND_NAME_CASE(NegDelta32)
KIND_NAME_CASE(Delta64)
KIND_NAME_CASE(Branch16PCRel)
KIND_NAME_CASE(Branch21PCRel)
KIND_NAME_CASE(Branch26PCRel)
KIND_NAME_CASE(Page20)
KIND_NAME_CASE(PageOffset12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ test_gotoffset12_external:
ld.w $a0, $a0, %got_pc_lo12(external_data)
.size test_gotoffset12_external, .-test_gotoffset12_external

## Check R_LARCH_B16 relocation for compare and branch instructions.

# jitlink-check: decode_operand(test_br16, 2)[17:0] = \
# jitlink-check: (test_br16_target - test_br16)[17:0]
.globl test_br16, test_br16_target
.p2align 2
test_br16:
beq $t1, $t2, %b16(test_br16_target)
.skip (1 << 16)
test_br16_target:
.size test_br16, .-test_br16

## Check R_LARCH_B21 relocation for compare and branch instructions.

# jitlink-check: decode_operand(test_br21, 1)[22:0] = \
# jitlink-check: (test_br21_target - test_br21)[22:0]
.globl test_br21, test_br21_target
.p2align 2
test_br21:
beqz $t1, %b21(test_br21_target)
.skip (1 << 21)
test_br21_target:
.size test_br21, .-test_br21


.globl named_data
.p2align 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ test_gotoffset12_external:
ld.d $a0, $a0, %got_pc_lo12(external_data)
.size test_gotoffset12_external, .-test_gotoffset12_external


## Check R_LARCH_CALL36 relocation of a local function call.

# jitlink-check: decode_operand(local_func_call36, 1)[19:0] = \
Expand All @@ -130,6 +129,31 @@ local_func_call36:
jirl $ra, $ra, 0
.size local_func_call36, .-local_func_call36

## Check R_LARCH_B16 relocation for compare and branch instructions.

# jitlink-check: decode_operand(test_br16, 2)[17:0] = \
# jitlink-check: (test_br16_target - test_br16)[17:0]
.globl test_br16, test_br16_target
.p2align 2
test_br16:
beq $t1, $t2, %b16(test_br16_target)
.skip (1 << 16)
test_br16_target:
.size test_br16, .-test_br16

## Check R_LARCH_B21 relocation for compare and branch instructions.

# jitlink-check: decode_operand(test_br21, 1)[22:0] = \
# jitlink-check: (test_br21_target - test_br21)[22:0]
.globl test_br21, test_br21_target
.p2align 2
test_br21:
beqz $t1, %b21(test_br21_target)
.skip (1 << 21)
test_br21_target:
.size test_br21, .-test_br21


.globl named_data
.p2align 4
.type named_data,@object
Expand Down
Loading