[PATCH] D140820: [JITLink][RISCV] Homogenize immediate handling

Jonas Hahnfeld via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 1 10:12:59 PST 2023


Hahnfeld created this revision.
Hahnfeld added a reviewer: StephenFan.
Herald added subscribers: sunshaoce, VincentWu, vkmr, frasercrmck, evandro, 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, asb, hiraditya, arichardson.
Herald added a project: All.
Hahnfeld requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD.
Herald added a project: LLVM.

Name the variables based on which part of the immediate value is
extracted, as it was already done for `R_RISCV_JAL`. This makes it
much easier to compare the logic with the spec.
Also only take the lower 12 bits of `RawInstr` for `R_RISCV_JAL`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140820

Files:
  llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp


Index: llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
===================================================================
--- llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
+++ llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
@@ -205,12 +205,13 @@
         return makeTargetOutOfRangeError(G, B, E);
       if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2)))
         return makeAlignmentError(FixupAddress, Value, 2, E);
-      uint32_t Imm31_25 =
-          extractBits(Value, 5, 6) << 25 | extractBits(Value, 12, 1) << 31;
-      uint32_t Imm11_7 =
-          extractBits(Value, 1, 4) << 8 | extractBits(Value, 11, 1) << 7;
+      uint32_t Imm12 = extractBits(Value, 12, 1) << 31;
+      uint32_t Imm10_5 = extractBits(Value, 5, 6) << 25;
+      uint32_t Imm4_1 = extractBits(Value, 1, 4) << 8;
+      uint32_t Imm11 = extractBits(Value, 11, 1) << 7;
       uint32_t RawInstr = *(little32_t *)FixupPtr;
-      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
+      *(little32_t *)FixupPtr =
+          (RawInstr & 0x1FFF07F) | Imm12 | Imm10_5 | Imm4_1 | Imm11;
       break;
     }
     case R_RISCV_JAL: {
@@ -224,7 +225,8 @@
       uint32_t Imm11 = extractBits(Value, 11, 1) << 20;
       uint32_t Imm19_12 = extractBits(Value, 12, 8) << 12;
       uint32_t RawInstr = *(little32_t *)FixupPtr;
-      *(little32_t *)FixupPtr = RawInstr | Imm20 | Imm10_1 | Imm11 | Imm19_12;
+      *(little32_t *)FixupPtr =
+          (RawInstr & 0xFFF) | Imm20 | Imm10_1 | Imm11 | Imm19_12;
       break;
     }
     case R_RISCV_CALL: {
@@ -279,11 +281,11 @@
       int64_t Value = RelHI20->getTarget().getAddress() +
                       RelHI20->getAddend() - E.getTarget().getAddress();
       int64_t Lo = Value & 0xFFF;
-      uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
-      uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
+      uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25;
+      uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7;
       uint32_t RawInstr = *(little32_t *)FixupPtr;
 
-      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
+      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0;
       break;
     }
     case R_RISCV_HI20: {
@@ -311,10 +313,10 @@
       // with current relocation R_RISCV_LO12_S. So here may need a check.
       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
       int64_t Lo = Value & 0xFFF;
-      uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
-      uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
+      uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25;
+      uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7;
       uint32_t RawInstr = *(little32_t *)FixupPtr;
-      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
+      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0;
       break;
     }
     case R_RISCV_ADD8: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140820.485807.patch
Type: text/x-patch
Size: 2893 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230101/9e6f13a1/attachment.bin>


More information about the llvm-commits mailing list