[PATCH] D153515: [JITLink][RISCV] Adjust offsets of non-relaxable edges

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 22 03:15:26 PDT 2023


jobnoorman created this revision.
jobnoorman added reviewers: lhames, StephenFan, MaskRay.
Herald added subscribers: asb, luke, pmatos, VincentWu, vkmr, frasercrmck, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, 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, wangpc, eopXD.
Herald added a project: LLVM.

The relaxation algorithm used to only update offsets of relaxable edges.
This caused non-relaxable edges that appear after a relaxed instruction
to have an incorrect offset and be applied at the wrong location. This
patch fixes this by updating the offsets of all edges.

Note that this bug was caused by an incorrect translation of LLD's
relaxation algorithm. LLD always uses all edges during relaxation while
I decided to filter-out relaxable edges to prevent having to iterate
non-relaxable edges at each step. However, this had the side-effect of
only updating offsets of relaxable edges. This patch leaves the
filtering of relaxable edges as-is but iterates all edges when updating
offsets.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153515

Files:
  llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
  llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_nonrelaxable.s


Index: llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_nonrelaxable.s
===================================================================
--- /dev/null
+++ llvm/test/ExecutionEngine/JITLink/RISCV/ELF_relax_nonrelaxable.s
@@ -0,0 +1,35 @@
+## Test that non-relaxable edges have their offset adjusted by relaxation
+
+# RUN: rm -rf %t && mkdir %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+relax %s -o %t.rv32
+# RUN: llvm-jitlink -noexec \
+# RUN:     -slab-allocate 100Kb -slab-address 0x0 -slab-page-size 4096 \
+# RUN:     -check %s %t.rv32
+
+# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+relax %s -o %t.rv64
+# RUN: llvm-jitlink -noexec \
+# RUN:     -slab-allocate 100Kb -slab-address 0x0 -slab-page-size 4096 \
+# RUN:     -check %s %t.rv64
+
+    .globl main,nonrelaxable,nonrelaxable_target
+    .size nonrelaxable, 4
+    .size nonrelaxable_target, 4
+main:
+    call f
+nonrelaxable:
+    ## Non-relaxable R_RISCV_BRANCH edge after a relaxable R_RISCV_CALL edge.
+    ## Even though this edge isn't relaxable, its offset should still be
+    ## adjusted.
+    beq zero, zero, nonrelaxable_target
+nonrelaxable_target:
+    ret
+    .size main, .-main
+
+    .globl f
+f:
+    ret
+    .size f, .-f
+
+# jitlink-check: decode_operand(nonrelaxable, 2) = (nonrelaxable_target - nonrelaxable)
+
Index: llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
===================================================================
--- llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
+++ llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
@@ -723,13 +723,17 @@
 
   // Fixup edge offsets and kinds.
   Delta = 0;
-  for (auto [I, E] : llvm::enumerate(Aux.RelaxEdges)) {
-    E->setOffset(E->getOffset() - Delta);
+  size_t I = 0;
+  for (auto &E : Block.edges()) {
+    E.setOffset(E.getOffset() - Delta);
 
-    if (Aux.EdgeKinds[I] != Edge::Invalid)
-      E->setKind(Aux.EdgeKinds[I]);
+    if (I < Aux.RelaxEdges.size() && Aux.RelaxEdges[I] == &E) {
+      if (Aux.EdgeKinds[I] != Edge::Invalid)
+        E.setKind(Aux.EdgeKinds[I]);
 
-    Delta = Aux.RelocDeltas[I];
+      Delta = Aux.RelocDeltas[I];
+      ++I;
+    }
   }
 
   // Remove AlignRelaxable edges: all other relaxable edges got modified and


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153515.533524.patch
Type: text/x-patch
Size: 2229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230622/5f02967f/attachment.bin>


More information about the llvm-commits mailing list