Skip to content

[RISCV] Update evaluateBranch for Xqci #141718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025

Conversation

lenary
Copy link
Member

@lenary lenary commented May 28, 2025

This implements RISCVMCInstrAnalysis::evaluateBranch for the two Xqci pc-relative jumps, which were otherwise not being displayed correctly when disassembling.

This also adds tests for all Xqci branches, and also the Core-V branches as they work, but did not have coverage.

This implements RISCVMCInstrAnalysis::evaluateBranch for the two Xqci
jumps, which were otherwise not being displayed correctly when
disassembling.

This also adds tests for all Xqci branches, and also the Core-V branches
as they work, but did not have coverage.
@lenary lenary requested review from simonpcook, mtvec and svs-quic May 28, 2025 05:35
@llvmbot llvmbot added backend:RISC-V mc Machine (object) code labels May 28, 2025
@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-backend-risc-v

@llvm/pr-subscribers-mc

Author: Sam Elliott (lenary)

Changes

This implements RISCVMCInstrAnalysis::evaluateBranch for the two Xqci pc-relative jumps, which were otherwise not being displayed correctly when disassembling.

This also adds tests for all Xqci branches, and also the Core-V branches as they work, but did not have coverage.


Full diff: https://github.com/llvm/llvm-project/pull/141718.diff

3 Files Affected:

  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp (+8-8)
  • (added) llvm/test/MC/Disassembler/RISCV/branch-targets-xcv.txt (+25)
  • (added) llvm/test/MC/Disassembler/RISCV/branch-targets-xqci.txt (+36)
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
index d1bc6aa9a1e33..f3b93f032588c 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
@@ -212,24 +212,24 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
       return true;
     }
 
-    if (Inst.getOpcode() == RISCV::C_JAL || Inst.getOpcode() == RISCV::C_J) {
+    switch (Inst.getOpcode()) {
+    case RISCV::C_J:
+    case RISCV::C_JAL:
+    case RISCV::QC_E_J:
+    case RISCV::QC_E_JAL:
       Target = Addr + Inst.getOperand(0).getImm();
       return true;
-    }
-
-    if (Inst.getOpcode() == RISCV::JAL) {
+    case RISCV::JAL:
       Target = Addr + Inst.getOperand(1).getImm();
       return true;
-    }
-
-    if (Inst.getOpcode() == RISCV::JALR) {
+    case RISCV::JALR: {
       if (auto TargetRegState = getGPRState(Inst.getOperand(1).getReg())) {
         Target = *TargetRegState + Inst.getOperand(2).getImm();
         return true;
       }
-
       return false;
     }
+    }
 
     return false;
   }
diff --git a/llvm/test/MC/Disassembler/RISCV/branch-targets-xcv.txt b/llvm/test/MC/Disassembler/RISCV/branch-targets-xcv.txt
new file mode 100644
index 0000000000000..64ac7b7bf8e6b
--- /dev/null
+++ b/llvm/test/MC/Disassembler/RISCV/branch-targets-xcv.txt
@@ -0,0 +1,25 @@
+# RUN: llvm-mc -assemble -triple riscv32 \
+# RUN:   -mattr=+xcvbi \
+# RUN:   %s -filetype=obj -o - \
+# RUN:   | llvm-objdump -d -M no-aliases - \
+# RUN:   --mattr=+xcvbi \
+# RUN:   | FileCheck %s
+
+.option exact
+
+# CHECK-LABEL: <label1>:
+label1:
+
+# CHECK-NEXT: cv.beqimm a0, 0x1, 0x0 <label1>
+  cv.beqimm a0, 1, label1
+# CHECK-NEXT: cv.beqimm a0, 0x1, 0x10 <label2>
+  cv.beqimm a0, 1, label2
+
+# CHECK-NEXT: cv.bneimm a0, 0x2, 0x0 <label1>
+  cv.bneimm a0, 2, label1
+# CHECK-NEXT: cv.bneimm a0, 0x2, 0x10 <label2>
+  cv.bneimm a0, 2, label2
+
+
+label2:
+
diff --git a/llvm/test/MC/Disassembler/RISCV/branch-targets-xqci.txt b/llvm/test/MC/Disassembler/RISCV/branch-targets-xqci.txt
new file mode 100644
index 0000000000000..fbbabe8bda832
--- /dev/null
+++ b/llvm/test/MC/Disassembler/RISCV/branch-targets-xqci.txt
@@ -0,0 +1,36 @@
+# RUN: llvm-mc -assemble -triple riscv32 \
+# RUN:   -mattr=+experimental-xqcilb,+experimental-xqcibi \
+# RUN:   %s -filetype=obj -o - \
+# RUN:   | llvm-objdump -d -M no-aliases - \
+# RUN:   --mattr=+experimental-xqcilb,+experimental-xqcibi \
+# RUN:   | FileCheck %s
+
+.option exact
+
+# CHECK-LABEL: <label1>:
+label1:
+
+# CHECK-NEXT: qc.e.j 0x0 <label1>
+  qc.e.j label1
+# CHECK-NEXT: qc.e.j 0x2c <label2>
+  qc.e.j label2
+
+# CHECK-NEXT: qc.e.jal 0x0 <label1>
+  qc.e.jal label1
+# CHECK-NEXT: qc.e.jal 0x2c <label2>
+  qc.e.jal label2
+
+# CHECK-NEXT: qc.beqi a0, 0x1, 0x0 <label1>
+  qc.beqi a0, 1, label1
+
+# CHECK-NEXT: qc.bnei a0, 0x1, 0x2c <label2>
+  qc.bnei a0, 1, label2
+
+# CHECK-NEXT: qc.e.beqi a0, 0x2, 0x0 <label1>
+  qc.e.beqi a0, 2, label1
+
+# CHECK-NEXT: qc.e.bnei a0, 0x2, 0x2c <label2>
+  qc.e.bnei a0, 2, label2
+
+label2:
+

Copy link
Contributor

@svs-quic svs-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@lenary lenary merged commit 31f47dd into llvm:main May 28, 2025
14 checks passed
@lenary lenary deleted the pr/riscv-xqci-branch-targets branch May 28, 2025 17:59
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
This implements RISCVMCInstrAnalysis::evaluateBranch for the two Xqci
pc-relative jumps, which were otherwise not being displayed correctly
when disassembling.

This also adds tests for all Xqci branches, and also the Core-V branches
as they work, but did not have coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:RISC-V mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants