[llvm] [RISCV][XCV] Relax long `cv.beqimm`/`cv.bneimm` branches (PR #205096)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 22 05:44:23 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: vitbur (VittorioBurani)
<details>
<summary>Changes</summary>
`cv.beqimm` and `cv.bneimm` encode their target as a 13-bit signed PC-relative offset (+/-4094 bytes). Branches beyond that range were silently truncated by MC fixup application, producing wrong code with no diagnostic. Add `PseudoLongCV_BEQIMM`/`PseudoLongCV_BNEIMM` and the MC-layer relaxation flow (inverted short branch + JAL trampoline), mirroring the standard B-type and Qualcomm Xqcibi vendor branches.
**Tests:** `xcvbi-branch-relax.ll` (uses `-filetype=obj | llvm-objdump`, since MC-layer relaxation is only observable on object emission, not on textual asm).
Split out of #<!-- -->204879 at review request (one fix per PR).
Part of a CORE-V (XCV) series; see RFC: https://discourse.llvm.org/t/rfc-core-v-xcv-support-for-cv32e40p-clang-builtins-xcvsimd-intrinsics-and-generic-auto-selection/91111
---
Full diff: https://github.com/llvm/llvm-project/pull/205096.diff
4 Files Affected:
- (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp (+6)
- (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp (+6)
- (modified) llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td (+7)
- (added) llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll (+48)
``````````diff
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index 8e4dab0a59c98..315d129aeb5fb 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -235,6 +235,10 @@ static unsigned getRelaxedOpcode(unsigned Opcode, ArrayRef<MCOperand> Operands,
return RISCV::PseudoLongQC_E_BLTUI;
case RISCV::QC_E_BGEUI:
return RISCV::PseudoLongQC_E_BGEUI;
+ case RISCV::CV_BEQIMM:
+ return RISCV::PseudoLongCV_BEQIMM;
+ case RISCV::CV_BNEIMM:
+ return RISCV::PseudoLongCV_BNEIMM;
}
// Returning the original opcode means we cannot relax the instruction.
@@ -310,6 +314,8 @@ void RISCVAsmBackend::relaxInstruction(MCInst &Inst,
case RISCV::QC_E_BGEI:
case RISCV::QC_E_BLTUI:
case RISCV::QC_E_BGEUI:
+ case RISCV::CV_BEQIMM:
+ case RISCV::CV_BNEIMM:
Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
Res.addOperand(Inst.getOperand(0));
Res.addOperand(Inst.getOperand(1));
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
index 31205fda3e9d2..23cbbf0954cd7 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
@@ -365,6 +365,10 @@ static unsigned getInvertedBranchOp(unsigned BrOp) {
return RISCV::QC_E_BGEUI;
case RISCV::PseudoLongQC_E_BGEUI:
return RISCV::QC_E_BLTUI;
+ case RISCV::PseudoLongCV_BEQIMM:
+ return RISCV::CV_BNEIMM;
+ case RISCV::PseudoLongCV_BNEIMM:
+ return RISCV::CV_BEQIMM;
}
}
@@ -602,6 +606,8 @@ void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI,
case RISCV::PseudoLongQC_BGEI:
case RISCV::PseudoLongQC_BLTUI:
case RISCV::PseudoLongQC_BGEUI:
+ case RISCV::PseudoLongCV_BEQIMM:
+ case RISCV::PseudoLongCV_BNEIMM:
expandQCLongCondBrImm(MI, CB, Fixups, STI, 4);
MCNumEmitted += 2;
return;
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td b/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
index ec93f60366e1e..806c2a5337ee4 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
@@ -594,6 +594,13 @@ let Predicates = [HasVendorXCVbi, IsRV32] in {
def CV_BNEIMM : CVInstImmBranch<0b111, (outs),
(ins GPR:$rs1, simm5:$imm5, bare_simm13_lsb0:$imm12),
"cv.bneimm", "$rs1, $imm5, $imm12">, Sched<[]>;
+ // Long-branch relaxation pseudos for cv.beqimm / cv.bneimm. These are emitted
+ // by the RISCVAsmBackend when the target of a CV_BEQIMM / CV_BNEIMM is
+ // further than +/-4 KiB away. Each expands to an inverted short cv.{n,b}eimm
+ // (4 bytes) plus an unconditional JAL trampoline (4 bytes). Reuses the
+ // LongBcciPseudo class from RISCVInstrInfo.td (size = 8 bytes, simm5 imm).
+ def PseudoLongCV_BEQIMM : LongBcciPseudo<simm5, 8>;
+ def PseudoLongCV_BNEIMM : LongBcciPseudo<simm5, 8>;
}
let Predicates = [HasVendorXCVmem, IsRV32] in {
diff --git a/llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll b/llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll
new file mode 100644
index 0000000000000..db125c8c30923
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll
@@ -0,0 +1,48 @@
+; NOTE: Do not use update_llc_test_checks.py on this file.
+; NOTE: Branch relaxation for RISC-V happens at the MC layer in RISCVAsmBackend, not in a MIR pass.
+; It is therefore only observable on object emission, not on textual asm. Use -filetype=obj + llvm-objdump.
+
+; RUN: llc -mtriple=riscv32 -mattr=+xcvbi -filetype=obj -verify-machineinstrs < %s \
+; RUN: | llvm-objdump -d -M no-aliases --no-show-raw-insn - | FileCheck %s
+
+; A short forward branch (target ~8 KiB away, well past the +/-4094 range
+; of cv.beqimm / cv.bneimm) must be relaxed by the MC layer into:
+; <inverted short branch> rs1, imm5, .Lskip
+; jal zero, target
+; .Lskip:
+; Without relaxation, the assembler would silently truncate the 13-bit
+; branch immediate and produce wrong code.
+
+define void @test_far_beqimm(i32 %x) {
+; CHECK-LABEL: <test_far_beqimm>:
+; CHECK: cv.bneimm a0, 0x3, 0x{{[0-9a-f]+}}
+; CHECK-NEXT: jal zero, 0x{{[0-9a-f]+}}
+entry:
+ %cmp = icmp eq i32 %x, 3
+ br i1 %cmp, label %target, label %filler
+
+filler:
+ ; ~8 KiB of dead bytes via inline asm, to force the branch out of range.
+ call void asm sideeffect ".zero 8192", ""()
+ br label %target
+
+target:
+ ret void
+}
+
+define void @test_far_bneimm(i32 %x) {
+; CHECK-LABEL: <test_far_bneimm>:
+; CHECK: cv.beqimm a0, 0x3, 0x{{[0-9a-f]+}}
+; CHECK-NEXT: jal zero, 0x{{[0-9a-f]+}}
+entry:
+ %cmp = icmp ne i32 %x, 3
+ br i1 %cmp, label %target, label %filler
+
+filler:
+ ; ~8 KiB of dead bytes via inline asm, to force the branch out of range.
+ call void asm sideeffect ".zero 8192", ""()
+ br label %target
+
+target:
+ ret void
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/205096
More information about the llvm-commits
mailing list