[llvm] [JITLink][LoongArch] Ignore R_LARCH_RELAX and check R_LARCH_ALIGN (PR #121097)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 25 01:32:11 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-loongarch

Author: ZhaoQi (zhaoqi5)

<details>
<summary>Changes</summary>

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: https://github.com/llvm/llvm-project/commit/f5b5398ebf7da1ab54f6793331bca8fbc3340f14.

---
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
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);

``````````

</details>


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


More information about the llvm-commits mailing list