Skip to content

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Sep 10, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

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

Author: Craig Topper (topperc)

Changes

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

3 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVInstrInfoZb.td (+4-4)
  • (added) llvm/test/MC/RISCV/rv32zbkb-aliases-valid.s (+20)
  • (added) llvm/test/MC/RISCV/rv64zbkb-aliases-valid.s (+24)
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
index 2abd3e613a037..ca930b822317f 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
@@ -459,15 +459,15 @@ let Predicates = [HasStdExtZba, IsRV64] in {
 def : InstAlias<"zext.w $rd, $rs", (ADD_UW GPR:$rd, GPR:$rs, X0)>;
 } // Predicates = [HasStdExtZba, IsRV64]
 
-let Predicates = [HasStdExtZbb] in {
+let Predicates = [HasStdExtZbbOrZbkb] in {
 def : InstAlias<"ror $rd, $rs1, $shamt",
                 (RORI  GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt), 0>;
-} // Predicates = [HasStdExtZbb]
+} // Predicates = [HasStdExtZbbOrZbkb]
 
-let Predicates = [HasStdExtZbb, IsRV64] in {
+let Predicates = [HasStdExtZbbOrZbkb, IsRV64] in {
 def : InstAlias<"rorw $rd, $rs1, $shamt",
                 (RORIW  GPR:$rd, GPR:$rs1, uimm5:$shamt), 0>;
-} // Predicates = [HasStdExtZbb, IsRV64]
+} // Predicates = [HasStdExtZbbOrZbkb, IsRV64]
 
 let Predicates = [HasStdExtZbs] in {
 def : InstAlias<"bset $rd, $rs1, $shamt",
diff --git a/llvm/test/MC/RISCV/rv32zbkb-aliases-valid.s b/llvm/test/MC/RISCV/rv32zbkb-aliases-valid.s
new file mode 100644
index 0000000000000..c7debf2d04e3d
--- /dev/null
+++ b/llvm/test/MC/RISCV/rv32zbkb-aliases-valid.s
@@ -0,0 +1,20 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zbkb -M no-aliases \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ-NOALIAS %s
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zbkb \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ %s
+# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+zbkb < %s \
+# RUN:     | llvm-objdump --no-print-imm-hex -d -r -M no-aliases --mattr=+zbkb - \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ-NOALIAS %s
+# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+zbkb < %s \
+# RUN:     | llvm-objdump --no-print-imm-hex -d -r --mattr=+zbkb - \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ %s
+
+# The following check prefixes are used in this test:
+# CHECK-S-OBJ            Match both the .s and objdumped object output with
+#                        aliases enabled
+# CHECK-S-OBJ-NOALIAS    Match both the .s and objdumped object output with
+#                        aliases disabled
+
+# CHECK-S-OBJ-NOALIAS: rori t0, t1, 8
+# CHECK-S-OBJ: rori t0, t1, 8
+ror x5, x6, 8
diff --git a/llvm/test/MC/RISCV/rv64zbkb-aliases-valid.s b/llvm/test/MC/RISCV/rv64zbkb-aliases-valid.s
new file mode 100644
index 0000000000000..f98799e9b9963
--- /dev/null
+++ b/llvm/test/MC/RISCV/rv64zbkb-aliases-valid.s
@@ -0,0 +1,24 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+zbkb -M no-aliases \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ-NOALIAS %s
+# RUN: llvm-mc %s  -triple=riscv64 -mattr=+zbkb \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ %s
+# RUN: llvm-mc -filetype=obj -triple riscv64 -mattr=+zbkb < %s \
+# RUN:     | llvm-objdump --no-print-imm-hex -d -r -M no-aliases --mattr=+zbkb - \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ-NOALIAS %s
+# RUN: llvm-mc -filetype=obj -triple riscv64 -mattr=+zbkb < %s \
+# RUN:     | llvm-objdump --no-print-imm-hex -d -r --mattr=+zbkb - \
+# RUN:     | FileCheck -check-prefixes=CHECK-S-OBJ %s
+
+# The following check prefixes are used in this test:
+# CHECK-S-OBJ            Match both the .s and objdumped object output with
+#                        aliases enabled
+# CHECK-S-OBJ-NOALIAS    Match both the .s and objdumped object output with
+#                        aliases disabled
+
+# CHECK-S-OBJ-NOALIAS: rori t0, t1, 8
+# CHECK-S-OBJ: rori t0, t1, 8
+ror x5, x6, 8
+
+# CHECK-S-OBJ-NOALIAS: roriw t0, t1, 8
+# CHECK-S-OBJ: roriw t0, t1, 8
+rorw x5, x6, 8

Copy link
Contributor

@pfusik pfusik left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@preames preames left a comment

Choose a reason for hiding this comment

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

LGTM

Possibly followup: Is there a way in tablegen to derive a predicate from another instruction? That is, say "the predicate for the alias is whatever the predicate for RORI is"?

@topperc topperc merged commit d7e6e72 into llvm:main Sep 10, 2025
9 checks passed
@topperc topperc deleted the pr/ror-aliases branch September 10, 2025 19:20
@topperc
Copy link
Collaborator Author

topperc commented Sep 10, 2025

LGTM

Possibly followup: Is there a way in tablegen to derive a predicate from another instruction? That is, say "the predicate for the alias is whatever the predicate for RORI is"?

Not that I'm aware of.

@lenary
Copy link
Member

lenary commented Sep 10, 2025

Here's some awful tablegen that seems to work (but needs some work on naming)

class SimpleInstAlias<string Asm, dag Result, int Emit = 1, string VariantName = ""> :
  InstAlias<Asm, Result, Emit, VariantName> {

  // Pull the Predicates out the resulting instruction.
  let Predicates = !getdagop<Instruction>(Result).Predicates;
}

The difficulty is how to then "add" predicates to this SimpleInstAlias, if you want any additional ones - but this problem is similar to the case where let Predicates = [...] doesn't nest in a nicely compositional way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants