diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp +++ b/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 DisableBranchRelaxation("riscv-disable-branch-relaxation", + cl::init(false)); + std::optional 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(); diff --git a/llvm/test/MC/RISCV/long-jump-disable-relax.s b/llvm/test/MC/RISCV/long-jump-disable-relax.s new file mode 100644 --- /dev/null +++ b/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,@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