Skip to content

[RISCV] Add CSRs and an instruction for Smctr and Ssctr extensions. #105148

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 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/test/Driver/print-supported-extensions-riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@
// CHECK-NEXT: zalasr 0.1 'Zalasr' (Load-Acquire and Store-Release Instructions)
// CHECK-NEXT: zvbc32e 0.7 'Zvbc32e' (Vector Carryless Multiplication with 32-bits elements)
// CHECK-NEXT: zvkgs 0.7 'Zvkgs' (Vector-Scalar GCM instructions for Cryptography)
// CHECK-NEXT: smctr 1.0 'Smctr' (Control Transfer Records Machine Level)
// CHECK-NEXT: smmpm 1.0 'Smmpm' (Machine-level Pointer Masking for M-mode)
// CHECK-NEXT: smnpm 1.0 'Smnpm' (Machine-level Pointer Masking for next lower privilege mode)
// CHECK-NEXT: ssctr 1.0 'Ssctr' (Control Transfer Records Supervisor Level)
// CHECK-NEXT: ssnpm 1.0 'Ssnpm' (Supervisor-level Pointer Masking for next lower privilege mode)
// CHECK-NEXT: sspm 1.0 'Sspm' (Indicates Supervisor-mode Pointer Masking)
// CHECK-NEXT: ssqosid 1.0 'Ssqosid' (Quality-of-Service (QoS) Identifiers)
Expand Down
18 changes: 18 additions & 0 deletions clang/test/Preprocessor/riscv-target-features.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@

// Experimental extensions

// CHECK-NOT: __riscv_smctr{{.*$}}
// CHECK-NOT: __riscv_smmpm{{.*$}}
// CHECK-NOT: __riscv_smnpm{{.*$}}
// CHECK-NOT: __riscv_ssctr{{.*$}}
// CHECK-NOT: __riscv_ssnpm{{.*$}}
// CHECK-NOT: __riscv_sspm{{.*$}}
// CHECK-NOT: __riscv_ssqosid{{.*$}}
Expand Down Expand Up @@ -1748,6 +1750,22 @@
// RUN: -o - | FileCheck --check-prefix=CHECK-SSQOSID-EXT %s
// CHECK-SSQOSID-EXT: __riscv_ssqosid 1000000{{$}}

// RUN: %clang --target=riscv32 -menable-experimental-extensions \
// RUN: -march=rv32i_smctr1p0 -E -dM %s \
// RUN: -o - | FileCheck --check-prefix=CHECK-SMCTR-EXT %s
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
// RUN: -march=rv64i_smctr1p0 -E -dM %s \
// RUN: -o - | FileCheck --check-prefix=CHECK-SMCTR-EXT %s
// CHECK-SMCTR-EXT: __riscv_smctr 1000000{{$}}

// RUN: %clang --target=riscv32 -menable-experimental-extensions \
// RUN: -march=rv32i_ssctr1p0 -E -dM %s \
// RUN: -o - | FileCheck --check-prefix=CHECK-SSCTR-EXT %s
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
// RUN: -march=rv64i_ssctr1p0 -E -dM %s \
// RUN: -o - | FileCheck --check-prefix=CHECK-SSCTR-EXT %s
// CHECK-SSCTR-EXT: __riscv_ssctr 1000000{{$}}

// Misaligned

// RUN: %clang --target=riscv32-unknown-linux-gnu -march=rv32i -E -dM %s \
Expand Down
3 changes: 3 additions & 0 deletions llvm/docs/RISCVUsage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ The primary goal of experimental support is to assist in the process of ratifica
``experimental-zvbc32e``, ``experimental-zvkgs``
LLVM implements the `0.7 release specification <https://github.com/user-attachments/files/16450464/riscv-crypto-spec-vector-extra_v0.0.7.pdf>`__.

``experimental-smctr``, ``experimental-ssctr``
LLVM implements the `1.0-rc3 specification <https://github.com/riscv/riscv-control-transfer-records/releases/tag/v1.0_rc3>`__.

To use an experimental extension from `clang`, you must add `-menable-experimental-extensions` to the command line, and specify the exact version of the experimental extension you are using. To use an experimental extension with LLVM's internal developer tools (e.g. `llc`, `llvm-objdump`, `llvm-mc`), you must prefix the extension name with `experimental-`. Note that you don't need to specify the version with internal tools, and shouldn't include the `experimental-` prefix with `clang`.

Vendor Extensions
Expand Down
1 change: 1 addition & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Changes to the RISC-V Backend
means Zve32x and Zve32f will also require Zvl64b. The prior support was
largely untested.
* The ``Zvbc32e`` and ``Zvkgs`` extensions are now supported experimentally.
* Added ``Smctr`` and ``Ssctr`` extensions.

Changes to the WebAssembly Backend
----------------------------------
Expand Down
13 changes: 13 additions & 0 deletions llvm/lib/Target/RISCV/RISCVFeatures.td
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,19 @@ def FeatureStdExtSupm
: RISCVExperimentalExtension<"supm", 1, 0,
"'Supm' (Indicates User-mode Pointer Masking)">;

def FeatureStdExtSmctr
: RISCVExperimentalExtension<"smctr", 1, 0,
"'Smctr' (Control Transfer Records Machine Level)",
[FeatureStdExtSscsrind]>;
def FeatureStdExtSsctr
: RISCVExperimentalExtension<"ssctr" ,1, 0,
"'Ssctr' (Control Transfer Records Supervisor Level)",
[FeatureStdExtSscsrind]>;
def HasStdExtSmctrOrSsctr : Predicate<"Subtarget->hasStdExtSmctrOrSsctr()">,
AssemblerPredicate<(any_of FeatureStdExtSmctr, FeatureStdExtSsctr),
"'Smctr' (Control Transfer Records Machine Level) or "
"'Ssctr' (Control Transfer Records Supervisor Level)">;

//===----------------------------------------------------------------------===//
// Vendor extensions
//===----------------------------------------------------------------------===//
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,14 @@ def HLV_D : HLoad_r<0b0110110, 0b00000, "hlv.d">, Sched<[]>;
def HSV_D : HStore_rr<0b0110111, "hsv.d">, Sched<[]>;
}

let Predicates = [HasStdExtSmctrOrSsctr] in {
def SCTRCLR : Priv<"sctrclr", 0b0001000>, Sched<[]> {
let rd = 0;
let rs1 = 0;
let rs2 = 0b00100;
}
}

//===----------------------------------------------------------------------===//
// Debug instructions
//===----------------------------------------------------------------------===//
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Target/RISCV/RISCVSystemOperands.td
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,12 @@ def : SysReg<"mnscratch", 0x740>;
def : SysReg<"mnepc", 0x741>;
def : SysReg<"mncause", 0x742>;
def : SysReg<"mnstatus", 0x744>;

//===-----------------------------------------------
// Control Transfer Records CSRs
//===-----------------------------------------------
def : SysReg<"sctrctl", 0x14e>;
def : SysReg<"sctrstatus", 0x14f>;
def : SysReg<"sctrdepth", 0x15f>;
def : SysReg<"vsctrctl", 0x24e>;
def : SysReg<"mctrctl", 0x34e>;
8 changes: 8 additions & 0 deletions llvm/test/CodeGen/RISCV/attributes.ll
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
; RUN: llc -mtriple=riscv32 -mattr=+experimental-sspm %s -o - | FileCheck --check-prefix=RV32SSPM %s
; RUN: llc -mtriple=riscv32 -mattr=+experimental-supm %s -o - | FileCheck --check-prefix=RV32SUPM %s
; RUN: llc -mtriple=riscv32 -mattr=+experimental-ssqosid %s -o - | FileCheck --check-prefix=RV32SSQOSID %s
; RUN: llc -mtriple=riscv32 -mattr=+experimental-smctr %s -o - | FileCheck --check-prefix=RV32SMCTR %s
; RUN: llc -mtriple=riscv32 -mattr=+experimental-ssctr %s -o - | FileCheck --check-prefix=RV32SSCTR %s

; RUN: llc -mtriple=riscv64 %s -o - | FileCheck %s
; RUN: llc -mtriple=riscv64 -mattr=+m %s -o - | FileCheck --check-prefixes=CHECK,RV64M %s
Expand Down Expand Up @@ -273,6 +275,8 @@
; RUN: llc -mtriple=riscv64 -mattr=+experimental-sspm %s -o - | FileCheck --check-prefix=RV64SSPM %s
; RUN: llc -mtriple=riscv64 -mattr=+experimental-supm %s -o - | FileCheck --check-prefix=RV64SUPM %s
; RUN: llc -mtriple=riscv64 -mattr=+experimental-ssqosid %s -o - | FileCheck --check-prefix=RV64SSQOSID %s
; RUN: llc -mtriple=riscv64 -mattr=+experimental-smctr %s -o - | FileCheck --check-prefix=RV64SMCTR %s
; RUN: llc -mtriple=riscv64 -mattr=+experimental-ssctr %s -o - | FileCheck --check-prefix=RV64SSCTR %s

; Tests for profile features.
; RUN: llc -mtriple=riscv32 -mattr=+rvi20u32 %s -o - | FileCheck --check-prefix=RVI20U32 %s
Expand Down Expand Up @@ -421,6 +425,8 @@
; RV32SSPM: .attribute 5, "rv32i2p1_sspm1p0"
; RV32SUPM: .attribute 5, "rv32i2p1_supm1p0"
; RV32SSQOSID: .attribute 5, "rv32i2p1_ssqosid1p0"
; RV32SMCTR: .attribute 5, "rv32i2p1_smctr1p0_sscsrind1p0"
; RV32SSCTR: .attribute 5, "rv32i2p1_sscsrind1p0_ssctr1p0"

; RV64M: .attribute 5, "rv64i2p1_m2p0_zmmul1p0"
; RV64ZMMUL: .attribute 5, "rv64i2p1_zmmul1p0"
Expand Down Expand Up @@ -559,6 +565,8 @@
; RV64SSPM: .attribute 5, "rv64i2p1_sspm1p0"
; RV64SUPM: .attribute 5, "rv64i2p1_supm1p0"
; RV64SSQOSID: .attribute 5, "rv64i2p1_ssqosid1p0"
; RV64SMCTR: .attribute 5, "rv64i2p1_smctr1p0_sscsrind1p0"
; RV64SSCTR: .attribute 5, "rv64i2p1_sscsrind1p0_ssctr1p0"

; RVI20U32: .attribute 5, "rv32i2p1"
; RVI20U64: .attribute 5, "rv64i2p1"
Expand Down
6 changes: 6 additions & 0 deletions llvm/test/MC/RISCV/attribute-arch.s
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,9 @@

.attribute arch, "rv64i_supm1p0"
# CHECK: attribute 5, "rv64i2p1_supm1p0"

.attribute arch, "rv32i_smctr1p0"
# CHECK: attribute 5, "rv32i2p1_smctr1p0_sscsrind1p0"

.attribute arch, "rv32i_ssctr1p0"
# CHECK: attribute 5, "rv32i2p1_sscsrind1p0_ssctr1p0"
17 changes: 17 additions & 0 deletions llvm/test/MC/RISCV/hypervisor-csr-names.s
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,20 @@ csrrs t2, 0x25C, zero
csrrs t1, vstopi, zero
# uimm12
csrrs t2, 0xEB0, zero

##################################
# Control Transfer Records
##################################

# vsctrctl
# name
# CHECK-INST: csrrs t1, vsctrctl, zero
# CHECK-ENC: encoding: [0x73,0x23,0xe0,0x24]
# CHECK-INST-ALIAS: csrr t1, vsctrctl
# uimm12
# CHECK-INST: csrrs t2, vsctrctl, zero
# CHECK-ENC: encoding: [0xf3,0x23,0xe0,0x24]
# CHECK-INST-ALIAS: csrr t2, vsctrctl
csrrs t1, vsctrctl, zero
# uimm12
csrrs t2, 0x24E, zero
17 changes: 17 additions & 0 deletions llvm/test/MC/RISCV/machine-csr-names.s
Original file line number Diff line number Diff line change
Expand Up @@ -2568,3 +2568,20 @@ csrrs t2, 0x308, zero
csrrs t1, mvip, zero
# uimm12
csrrs t2, 0x309, zero

##################################
# Control Transfer Records
##################################

# mctrctl
# name
# CHECK-INST: csrrs t1, mctrctl, zero
# CHECK-ENC: encoding: [0x73,0x23,0xe0,0x34]
# CHECK-INST-ALIAS: csrr t1, mctrctl
# uimm12
# CHECK-INST: csrrs t2, mctrctl, zero
# CHECK-ENC: encoding: [0xf3,0x23,0xe0,0x34]
# CHECK-INST-ALIAS: csrr t2, mctrctl
csrrs t1, mctrctl, zero
# uimm12
csrrs t2, 0x34E, zero
30 changes: 30 additions & 0 deletions llvm/test/MC/RISCV/smctr-ssctr-valid.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-smctr -riscv-no-aliases -show-encoding \
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-smctr -riscv-no-aliases -show-encoding \
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-ssctr -riscv-no-aliases -show-encoding \
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-ssctr -riscv-no-aliases -show-encoding \
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-smctr < %s \
# RUN: | llvm-objdump --mattr=+experimental-smctr -M no-aliases -d - \
# RUN: | FileCheck -check-prefix=CHECK-INST %s
# RUN: llvm-mc -filetype=obj -triple riscv64 -mattr=+experimental-smctr < %s \
# RUN: | llvm-objdump --mattr=+experimental-smctr -M no-aliases -d - \
# RUN: | FileCheck -check-prefix=CHECK-INST %s
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-ssctr < %s \
# RUN: | llvm-objdump --mattr=+experimental-ssctr -M no-aliases -d - \
# RUN: | FileCheck -check-prefix=CHECK-INST %s
# RUN: llvm-mc -filetype=obj -triple riscv64 -mattr=+experimental-ssctr < %s \
# RUN: | llvm-objdump --mattr=+experimental-ssctr -M no-aliases -d - \
# RUN: | FileCheck -check-prefix=CHECK-INST %s

# RUN: not llvm-mc -triple riscv32 -riscv-no-aliases -show-encoding < %s 2>&1 \
# RUN: | FileCheck -check-prefixes=CHECK-NO-EXT %s
# RUN: not llvm-mc -triple riscv64 -defsym=RV64=1 -riscv-no-aliases -show-encoding < %s 2>&1 \
# RUN: | FileCheck -check-prefixes=CHECK-NO-EXT %s

# CHECK-INST: sctrclr
# CHECK: encoding: [0x73,0x00,0x40,0x10]
# CHECK-NO-EXT: error: instruction requires the following: 'Smctr' (Control Transfer Records Machine Level) or 'Ssctr' (Control Transfer Records Supervisor Level){{$}}
sctrclr
43 changes: 43 additions & 0 deletions llvm/test/MC/RISCV/supervisor-csr-names.s
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,46 @@ csrrs t2, 0xDB0, zero
csrrs t1, scountinhibit, zero
# uimm12
csrrs t2, 0x120, zero

##################################
# Control Transfer Records
##################################

# sctrctl
# name
# CHECK-INST: csrrs t1, sctrctl, zero
# CHECK-ENC: encoding: [0x73,0x23,0xe0,0x14]
# CHECK-INST-ALIAS: csrr t1, sctrctl
# uimm12
# CHECK-INST: csrrs t2, sctrctl, zero
# CHECK-ENC: encoding: [0xf3,0x23,0xe0,0x14]
# CHECK-INST-ALIAS: csrr t2, sctrctl
csrrs t1, sctrctl, zero
# uimm12
csrrs t2, 0x14E, zero

# sctrstatus
# name
# CHECK-INST: csrrs t1, sctrstatus, zero
# CHECK-ENC: encoding: [0x73,0x23,0xf0,0x14]
# CHECK-INST-ALIAS: csrr t1, sctrstatus
# uimm12
# CHECK-INST: csrrs t2, sctrstatus, zero
# CHECK-ENC: encoding: [0xf3,0x23,0xf0,0x14]
# CHECK-INST-ALIAS: csrr t2, sctrstatus
csrrs t1, sctrstatus, zero
# uimm12
csrrs t2, 0x14F, zero

# sctrdepth
# name
# CHECK-INST: csrrs t1, sctrdepth, zero
# CHECK-ENC: encoding: [0x73,0x23,0xf0,0x15]
# CHECK-INST-ALIAS: csrr t1, sctrdepth
# uimm12
# CHECK-INST: csrrs t2, sctrdepth, zero
# CHECK-ENC: encoding: [0xf3,0x23,0xf0,0x15]
# CHECK-INST-ALIAS: csrr t2, sctrdepth
csrrs t1, sctrdepth, zero
# uimm12
csrrs t2, 0x15F, zero
2 changes: 2 additions & 0 deletions llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,8 +1119,10 @@ Experimental extensions
zalasr 0.1
zvbc32e 0.7
zvkgs 0.7
smctr 1.0
smmpm 1.0
smnpm 1.0
ssctr 1.0
ssnpm 1.0
sspm 1.0
ssqosid 1.0
Expand Down
Loading