[PATCH] D155953: [RISCV][MC] Add CLI option to disable branch relaxation

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 21 06:57:10 PDT 2023


jobnoorman created this revision.
jobnoorman added reviewers: asb, jrtc27, craig.topper, kito-cheng, MaskRay, reames.
Herald added subscribers: luke, pmatos, VincentWu, vkmr, frasercrmck, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, shiva0217, 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.

D154958 <https://reviews.llvm.org/D154958> enables branch relaxation for unresolved symbols. This has an
interesting consequence for some LLD tests: branch relocations are
tested by using branches to undefined symbols and defining them, with
different values, on the LLD command line. These tests broke and there
doesn't seem to be an easy workaround: as far as I can tell, there is no
way to convince llvm-mc to emit a branch relocation to an undefined
symbol without branch relaxation kicking in.

This patch proposes to add a flag, `--riscv-disable-branch-relaxation`,
to do just that. The main purpose for this flag is for testing but it
might be seen as a first step to some kind of "strict" or WYSIWYG mode
(i.e., what you give to the assembler is exactly what comes out). The
need for this has been mentioned in, for example, D108961 <https://reviews.llvm.org/D108961>. However, I
suspect there will be a lot of discussion around what exactly such a
strict mode would look like. Therefore, I gated this feature behind a
CLI flag instead of adding a new target feature.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155953

Files:
  llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
  llvm/test/MC/RISCV/long-jump-disable-relax.s


Index: llvm/test/MC/RISCV/long-jump-disable-relax.s
===================================================================
--- /dev/null
+++ llvm/test/MC/RISCV/long-jump-disable-relax.s
@@ -0,0 +1,17 @@
+# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+c \
+# RUN:       --riscv-disable-branch-relaxation %s \
+# RUN:     | llvm-objdump -dr -M no-aliases - \
+# RUN:     | FileCheck %s
+
+       .text
+       .type   test, at function
+test:
+# CHECK:      bne a0, a1, {{.*}}
+# CHECK-NEXT: R_RISCV_BRANCH foo
+   bne a0, a1, foo
+# CHECK:      c.beqz a0, {{.*}}
+# CHECK-NEXT: R_RISCV_RVC_BRANCH foo
+   c.beqz a0, foo
+# CHECK:      c.j {{.*}}
+# CHECK-NEXT: R_RISCV_RVC_JUMP foo
+   c.j foo
Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
===================================================================
--- llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -19,6 +19,7 @@
 #include "llvm/MC/MCObjectWriter.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/MC/MCValue.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/EndianStream.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -27,6 +28,9 @@
 
 using namespace llvm;
 
+static cl::opt<bool> DisableBranchRelaxation("riscv-disable-branch-relaxation",
+                                             cl::init(false));
+
 std::optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const {
   if (STI.getTargetTriple().isOSBinFormatELF()) {
     unsigned Type;
@@ -144,6 +148,9 @@
                                                    const MCRelaxableFragment *DF,
                                                    const MCAsmLayout &Layout,
                                                    const bool WasForced) const {
+  if (DisableBranchRelaxation)
+    return false;
+
   int64_t Offset = int64_t(Value);
   unsigned Kind = Fixup.getTargetKind();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155953.542895.patch
Type: text/x-patch
Size: 1953 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230721/890564a9/attachment.bin>


More information about the llvm-commits mailing list