Skip to content

[JITLink][LoongArch] Ignore R_LARCH_RELAX and check R_LARCH_ALIGN #121097

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

Closed

Conversation

zhaoqi5
Copy link
Contributor

@zhaoqi5 zhaoqi5 commented Dec 25, 2024

Linker relaxation is not implemented for jitlink now (maybe implement in the future). But if relaxation is enabled by clang, R_LARCH_RELAX and R_LARCH_ALIGN relocations will be emitted. So we just ignore linker relaxation and check the alignment now.

If not, interpreting C++ code using clang-repl when relaxation is enabled will occur error: JIT session error: Unsupported loongarch relocation:102: R_LARCH_ALIGN.

Similar to: f5b5398.

Linker relaxation is not implemented for jitlink now (maybe
implement in the future). But if relaxation is enabled by clang,
R_LARCH_RELAX and R_LARCH_ALIGN relocations will be emitted.
So we just ignore linker relaxation and check the alignment now.

If not, interpreting C++ code using clang-repl when relaxation
is enabled will occur error: `JIT session error: Unsupported
loongarch relocation:102: R_LARCH_ALIGN`.

Similar to: llvm@f5b5398.
@llvmbot
Copy link
Member

llvmbot commented Dec 25, 2024

@llvm/pr-subscribers-backend-loongarch

Author: ZhaoQi (zhaoqi5)

Changes

Linker relaxation is not implemented for jitlink now (maybe implement in the future). But if relaxation is enabled by clang, R_LARCH_RELAX and R_LARCH_ALIGN relocations will be emitted. So we just ignore linker relaxation and check the alignment now.

If not, interpreting C++ code using clang-repl when relaxation is enabled will occur error: JIT session error: Unsupported loongarch relocation:102: R_LARCH_ALIGN.

Similar to: f5b5398.


Full diff: https://github.com/llvm/llvm-project/pull/121097.diff

1 Files Affected:

  • (modified) llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp (+24-6)
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp
index 56c32aeecf55a8..1f4e34f41ab2b5 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp
@@ -95,6 +95,30 @@ class ELFLinkGraphBuilder_loongarch : public ELFLinkGraphBuilder<ELFT> {
                             Block &BlockToFix) {
     using Base = ELFLinkGraphBuilder<ELFT>;
 
+    uint32_t Type = Rel.getType(false);
+    // We do not implement linker relaxation for jitlink now, except what is
+    // required for alignment (see below).
+    if (Type == ELF::R_LARCH_RELAX)
+      return Error::success();
+
+    int64_t Addend = Rel.r_addend;
+    if (Type == ELF::R_LARCH_ALIGN) {
+      uint64_t Alignment = PowerOf2Ceil(Addend);
+      // FIXME: Implement support for ensuring alignment together with linker
+      // relaxation. Addend is always 28 in the most common case when
+      // interpreting C++ code in clang-repl.
+      if (Alignment > 32)
+        return make_error<JITLinkError>(
+            formatv("Unsupported relocation R_LARCH_ALIGN with alignment {0} "
+                    "larger than 32 (addend: {1})",
+                    Alignment, Addend));
+      return Error::success();
+    }
+
+    Expected<loongarch::EdgeKind_loongarch> Kind = getRelocationKind(Type);
+    if (!Kind)
+      return Kind.takeError();
+
     uint32_t SymbolIndex = Rel.getSymbol(false);
     auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
     if (!ObjSymbol)
@@ -109,12 +133,6 @@ class ELFLinkGraphBuilder_loongarch : public ELFLinkGraphBuilder<ELFT> {
                   Base::GraphSymbols.size()),
           inconvertibleErrorCode());
 
-    uint32_t Type = Rel.getType(false);
-    Expected<loongarch::EdgeKind_loongarch> Kind = getRelocationKind(Type);
-    if (!Kind)
-      return Kind.takeError();
-
-    int64_t Addend = Rel.r_addend;
     auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
     Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
     Edge GE(*Kind, Offset, *GraphSymbol, Addend);

@zhaoqi5 zhaoqi5 marked this pull request as draft December 30, 2024 10:12
@zhaoqi5
Copy link
Contributor Author

zhaoqi5 commented Dec 31, 2024

R_LARCH_ALIGN cannot be simply ignored. Although tests of clang-repl do not meet with error.
But error may occur. For example:

    .text
    .globl main
    .type main,@function
main:
    nop 
    ret 

    .globl br16
br16:

test_br16:
    .p2align 4
    la.pcrel $a0, br16
    .skip (1 << 17 - 16) 
    beq $t1, $t2, %b16(br16)
  • Without relax: llvm-mc --triple=loongarch64 --filetype=obj t.s -o - | llvm-objdump -d -, two nops should be inserted.
0000000000000000 <main>:
       0: 00 00 40 03  	nop
       4: 20 00 00 4c  	ret

0000000000000008 <test_br16>:
       8: 00 00 40 03  	nop
       c: 00 00 40 03  	nop
      10: 04 00 00 1a  	pcalau12i	$a0, 0
      14: 84 00 c0 02  	addi.d	$a0, $a0, 0
		...
   20008: ae 01 00 58  	beq	$t1, $t2, 0 <test_br16+0x20000>
  • But with relax: llvm-mc --triple=loongarch64 --filetype=obj -mattr=+relax t.s -o - | llvm-objdump -d -, three nops are inserted. The linker is expected to remove one nop to align pcalau12i.
0000000000000000 <main>:
       0: 00 00 40 03  	nop
       4: 20 00 00 4c  	ret

0000000000000008 <test_br16>:
       8: 00 00 40 03  	nop
       c: 00 00 40 03  	nop
      10: 00 00 40 03  	nop
      14: 04 00 00 1a  	pcalau12i	$a0, 0
      18: 84 00 c0 02  	addi.d	$a0, $a0, 0
		...
   2000c: ae 01 00 58  	beq	$t1, $t2, 0 <test_br16+0x20004>

If we ignore R_LARCH_ALIGN in jitlink. Nops will not be removed and beq will be out of range while running llvm-jitlink t.o.

llvm-jitlink error: In graph t.o, section .text: relocation target "br16" at address 0x7fffa4000008 is out of range of Branch16PCRel fixup at 0x7fffa402000c (main, 0x7fffa4000000 + 0x2000c)

@zhaoqi5 zhaoqi5 closed this Jan 9, 2025
@zhaoqi5 zhaoqi5 deleted the jitlink-ignore-relax-and-check-align branch January 9, 2025 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants