Skip to content

Commit 0ca77f6

Browse files
authored
[RISCV] Add CSRs and an instruction for Smctr and Ssctr extensions. (llvm#105148)
https://github.com/riscv/riscv-control-transfer-records/releases/tag/v1.0_rc3
1 parent 7854b16 commit 0ca77f6

14 files changed

+177
-0
lines changed

clang/test/Driver/print-supported-extensions-riscv.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@
175175
// CHECK-NEXT: zalasr 0.1 'Zalasr' (Load-Acquire and Store-Release Instructions)
176176
// CHECK-NEXT: zvbc32e 0.7 'Zvbc32e' (Vector Carryless Multiplication with 32-bits elements)
177177
// CHECK-NEXT: zvkgs 0.7 'Zvkgs' (Vector-Scalar GCM instructions for Cryptography)
178+
// CHECK-NEXT: smctr 1.0 'Smctr' (Control Transfer Records Machine Level)
178179
// CHECK-NEXT: smmpm 1.0 'Smmpm' (Machine-level Pointer Masking for M-mode)
179180
// CHECK-NEXT: smnpm 1.0 'Smnpm' (Machine-level Pointer Masking for next lower privilege mode)
181+
// CHECK-NEXT: ssctr 1.0 'Ssctr' (Control Transfer Records Supervisor Level)
180182
// CHECK-NEXT: ssnpm 1.0 'Ssnpm' (Supervisor-level Pointer Masking for next lower privilege mode)
181183
// CHECK-NEXT: sspm 1.0 'Sspm' (Indicates Supervisor-mode Pointer Masking)
182184
// CHECK-NEXT: supm 1.0 'Supm' (Indicates User-mode Pointer Masking)

clang/test/Preprocessor/riscv-target-features.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@
176176

177177
// Experimental extensions
178178

179+
// CHECK-NOT: __riscv_smctr{{.*$}}
179180
// CHECK-NOT: __riscv_smmpm{{.*$}}
180181
// CHECK-NOT: __riscv_smnpm{{.*$}}
182+
// CHECK-NOT: __riscv_ssctr{{.*$}}
181183
// CHECK-NOT: __riscv_ssnpm{{.*$}}
182184
// CHECK-NOT: __riscv_sspm{{.*$}}
183185
// CHECK-NOT: __riscv_supm{{.*$}}
@@ -1748,6 +1750,22 @@
17481750
// RUN: -o - | FileCheck --check-prefix=CHECK-SUPM-EXT %s
17491751
// CHECK-SUPM-EXT: __riscv_supm 1000000{{$}}
17501752

1753+
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
1754+
// RUN: -march=rv32i_smctr1p0 -E -dM %s \
1755+
// RUN: -o - | FileCheck --check-prefix=CHECK-SMCTR-EXT %s
1756+
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
1757+
// RUN: -march=rv64i_smctr1p0 -E -dM %s \
1758+
// RUN: -o - | FileCheck --check-prefix=CHECK-SMCTR-EXT %s
1759+
// CHECK-SMCTR-EXT: __riscv_smctr 1000000{{$}}
1760+
1761+
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
1762+
// RUN: -march=rv32i_ssctr1p0 -E -dM %s \
1763+
// RUN: -o - | FileCheck --check-prefix=CHECK-SSCTR-EXT %s
1764+
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
1765+
// RUN: -march=rv64i_ssctr1p0 -E -dM %s \
1766+
// RUN: -o - | FileCheck --check-prefix=CHECK-SSCTR-EXT %s
1767+
// CHECK-SSCTR-EXT: __riscv_ssctr 1000000{{$}}
1768+
17511769
// Misaligned
17521770

17531771
// RUN: %clang --target=riscv32-unknown-linux-gnu -march=rv32i -E -dM %s \

llvm/docs/RISCVUsage.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ The primary goal of experimental support is to assist in the process of ratifica
303303
``experimental-zvbc32e``, ``experimental-zvkgs``
304304
LLVM implements the `0.7 release specification <https://github.com/user-attachments/files/16450464/riscv-crypto-spec-vector-extra_v0.0.7.pdf>`__.
305305

306+
``experimental-smctr``, ``experimental-ssctr``
307+
LLVM implements the `1.0-rc3 specification <https://github.com/riscv/riscv-control-transfer-records/releases/tag/v1.0_rc3>`__.
308+
306309
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`.
307310

308311
Vendor Extensions

llvm/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Changes to the RISC-V Backend
114114
means Zve32x and Zve32f will also require Zvl64b. The prior support was
115115
largely untested.
116116
* The ``Zvbc32e`` and ``Zvkgs`` extensions are now supported experimentally.
117+
* Added ``Smctr`` and ``Ssctr`` extensions.
117118

118119
Changes to the WebAssembly Backend
119120
----------------------------------

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,19 @@ def FeatureStdExtSupm
10541054
: RISCVExperimentalExtension<"supm", 1, 0,
10551055
"'Supm' (Indicates User-mode Pointer Masking)">;
10561056

1057+
def FeatureStdExtSmctr
1058+
: RISCVExperimentalExtension<"smctr", 1, 0,
1059+
"'Smctr' (Control Transfer Records Machine Level)",
1060+
[FeatureStdExtSscsrind]>;
1061+
def FeatureStdExtSsctr
1062+
: RISCVExperimentalExtension<"ssctr" ,1, 0,
1063+
"'Ssctr' (Control Transfer Records Supervisor Level)",
1064+
[FeatureStdExtSscsrind]>;
1065+
def HasStdExtSmctrOrSsctr : Predicate<"Subtarget->hasStdExtSmctrOrSsctr()">,
1066+
AssemblerPredicate<(any_of FeatureStdExtSmctr, FeatureStdExtSsctr),
1067+
"'Smctr' (Control Transfer Records Machine Level) or "
1068+
"'Ssctr' (Control Transfer Records Supervisor Level)">;
1069+
10571070
//===----------------------------------------------------------------------===//
10581071
// Vendor extensions
10591072
//===----------------------------------------------------------------------===//

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,14 @@ def HLV_D : HLoad_r<0b0110110, 0b00000, "hlv.d">, Sched<[]>;
839839
def HSV_D : HStore_rr<0b0110111, "hsv.d">, Sched<[]>;
840840
}
841841

842+
let Predicates = [HasStdExtSmctrOrSsctr] in {
843+
def SCTRCLR : Priv<"sctrclr", 0b0001000>, Sched<[]> {
844+
let rd = 0;
845+
let rs1 = 0;
846+
let rs2 = 0b00100;
847+
}
848+
}
849+
842850
//===----------------------------------------------------------------------===//
843851
// Debug instructions
844852
//===----------------------------------------------------------------------===//

llvm/lib/Target/RISCV/RISCVSystemOperands.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,12 @@ def : SysReg<"mnscratch", 0x740>;
455455
def : SysReg<"mnepc", 0x741>;
456456
def : SysReg<"mncause", 0x742>;
457457
def : SysReg<"mnstatus", 0x744>;
458+
459+
//===-----------------------------------------------
460+
// Control Transfer Records CSRs
461+
//===-----------------------------------------------
462+
def : SysReg<"sctrctl", 0x14e>;
463+
def : SysReg<"sctrstatus", 0x14f>;
464+
def : SysReg<"sctrdepth", 0x15f>;
465+
def : SysReg<"vsctrctl", 0x24e>;
466+
def : SysReg<"mctrctl", 0x34e>;

llvm/test/CodeGen/RISCV/attributes.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
; RUN: llc -mtriple=riscv32 -mattr=+experimental-smmpm %s -o - | FileCheck --check-prefix=RV32SMMPM %s
134134
; RUN: llc -mtriple=riscv32 -mattr=+experimental-sspm %s -o - | FileCheck --check-prefix=RV32SSPM %s
135135
; RUN: llc -mtriple=riscv32 -mattr=+experimental-supm %s -o - | FileCheck --check-prefix=RV32SUPM %s
136+
; RUN: llc -mtriple=riscv32 -mattr=+experimental-smctr %s -o - | FileCheck --check-prefix=RV32SMCTR %s
137+
; RUN: llc -mtriple=riscv32 -mattr=+experimental-ssctr %s -o - | FileCheck --check-prefix=RV32SSCTR %s
136138

137139
; RUN: llc -mtriple=riscv64 %s -o - | FileCheck %s
138140
; RUN: llc -mtriple=riscv64 -mattr=+m %s -o - | FileCheck --check-prefixes=CHECK,RV64M %s
@@ -273,6 +275,8 @@
273275
; RUN: llc -mtriple=riscv64 -mattr=+experimental-smmpm %s -o - | FileCheck --check-prefix=RV64SMMPM %s
274276
; RUN: llc -mtriple=riscv64 -mattr=+experimental-sspm %s -o - | FileCheck --check-prefix=RV64SSPM %s
275277
; RUN: llc -mtriple=riscv64 -mattr=+experimental-supm %s -o - | FileCheck --check-prefix=RV64SUPM %s
278+
; RUN: llc -mtriple=riscv64 -mattr=+experimental-smctr %s -o - | FileCheck --check-prefix=RV64SMCTR %s
279+
; RUN: llc -mtriple=riscv64 -mattr=+experimental-ssctr %s -o - | FileCheck --check-prefix=RV64SSCTR %s
276280

277281
; Tests for profile features.
278282
; RUN: llc -mtriple=riscv32 -mattr=+rvi20u32 %s -o - | FileCheck --check-prefix=RVI20U32 %s
@@ -421,6 +425,8 @@
421425
; RV32SMMPM: .attribute 5, "rv32i2p1_smmpm1p0"
422426
; RV32SSPM: .attribute 5, "rv32i2p1_sspm1p0"
423427
; RV32SUPM: .attribute 5, "rv32i2p1_supm1p0"
428+
; RV32SMCTR: .attribute 5, "rv32i2p1_smctr1p0_sscsrind1p0"
429+
; RV32SSCTR: .attribute 5, "rv32i2p1_sscsrind1p0_ssctr1p0"
424430

425431
; RV64M: .attribute 5, "rv64i2p1_m2p0_zmmul1p0"
426432
; RV64ZMMUL: .attribute 5, "rv64i2p1_zmmul1p0"
@@ -559,6 +565,8 @@
559565
; RV64SMMPM: .attribute 5, "rv64i2p1_smmpm1p0"
560566
; RV64SSPM: .attribute 5, "rv64i2p1_sspm1p0"
561567
; RV64SUPM: .attribute 5, "rv64i2p1_supm1p0"
568+
; RV64SMCTR: .attribute 5, "rv64i2p1_smctr1p0_sscsrind1p0"
569+
; RV64SSCTR: .attribute 5, "rv64i2p1_sscsrind1p0_ssctr1p0"
562570

563571
; RVI20U32: .attribute 5, "rv32i2p1"
564572
; RVI20U64: .attribute 5, "rv64i2p1"

llvm/test/MC/RISCV/attribute-arch.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,9 @@
446446

447447
.attribute arch, "rv64i_supm1p0"
448448
# CHECK: attribute 5, "rv64i2p1_supm1p0"
449+
450+
.attribute arch, "rv32i_smctr1p0"
451+
# CHECK: attribute 5, "rv32i2p1_smctr1p0_sscsrind1p0"
452+
453+
.attribute arch, "rv32i_ssctr1p0"
454+
# CHECK: attribute 5, "rv32i2p1_sscsrind1p0_ssctr1p0"

llvm/test/MC/RISCV/hypervisor-csr-names.s

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,20 @@ csrrs t2, 0x25C, zero
633633
csrrs t1, vstopi, zero
634634
# uimm12
635635
csrrs t2, 0xEB0, zero
636+
637+
##################################
638+
# Control Transfer Records
639+
##################################
640+
641+
# vsctrctl
642+
# name
643+
# CHECK-INST: csrrs t1, vsctrctl, zero
644+
# CHECK-ENC: encoding: [0x73,0x23,0xe0,0x24]
645+
# CHECK-INST-ALIAS: csrr t1, vsctrctl
646+
# uimm12
647+
# CHECK-INST: csrrs t2, vsctrctl, zero
648+
# CHECK-ENC: encoding: [0xf3,0x23,0xe0,0x24]
649+
# CHECK-INST-ALIAS: csrr t2, vsctrctl
650+
csrrs t1, vsctrctl, zero
651+
# uimm12
652+
csrrs t2, 0x24E, zero

llvm/test/MC/RISCV/machine-csr-names.s

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,3 +2568,20 @@ csrrs t2, 0x308, zero
25682568
csrrs t1, mvip, zero
25692569
# uimm12
25702570
csrrs t2, 0x309, zero
2571+
2572+
##################################
2573+
# Control Transfer Records
2574+
##################################
2575+
2576+
# mctrctl
2577+
# name
2578+
# CHECK-INST: csrrs t1, mctrctl, zero
2579+
# CHECK-ENC: encoding: [0x73,0x23,0xe0,0x34]
2580+
# CHECK-INST-ALIAS: csrr t1, mctrctl
2581+
# uimm12
2582+
# CHECK-INST: csrrs t2, mctrctl, zero
2583+
# CHECK-ENC: encoding: [0xf3,0x23,0xe0,0x34]
2584+
# CHECK-INST-ALIAS: csrr t2, mctrctl
2585+
csrrs t1, mctrctl, zero
2586+
# uimm12
2587+
csrrs t2, 0x34E, zero
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-smctr -riscv-no-aliases -show-encoding \
2+
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
3+
# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-smctr -riscv-no-aliases -show-encoding \
4+
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
5+
# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-ssctr -riscv-no-aliases -show-encoding \
6+
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
7+
# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-ssctr -riscv-no-aliases -show-encoding \
8+
# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
9+
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-smctr < %s \
10+
# RUN: | llvm-objdump --mattr=+experimental-smctr -M no-aliases -d - \
11+
# RUN: | FileCheck -check-prefix=CHECK-INST %s
12+
# RUN: llvm-mc -filetype=obj -triple riscv64 -mattr=+experimental-smctr < %s \
13+
# RUN: | llvm-objdump --mattr=+experimental-smctr -M no-aliases -d - \
14+
# RUN: | FileCheck -check-prefix=CHECK-INST %s
15+
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-ssctr < %s \
16+
# RUN: | llvm-objdump --mattr=+experimental-ssctr -M no-aliases -d - \
17+
# RUN: | FileCheck -check-prefix=CHECK-INST %s
18+
# RUN: llvm-mc -filetype=obj -triple riscv64 -mattr=+experimental-ssctr < %s \
19+
# RUN: | llvm-objdump --mattr=+experimental-ssctr -M no-aliases -d - \
20+
# RUN: | FileCheck -check-prefix=CHECK-INST %s
21+
22+
# RUN: not llvm-mc -triple riscv32 -riscv-no-aliases -show-encoding < %s 2>&1 \
23+
# RUN: | FileCheck -check-prefixes=CHECK-NO-EXT %s
24+
# RUN: not llvm-mc -triple riscv64 -defsym=RV64=1 -riscv-no-aliases -show-encoding < %s 2>&1 \
25+
# RUN: | FileCheck -check-prefixes=CHECK-NO-EXT %s
26+
27+
# CHECK-INST: sctrclr
28+
# CHECK: encoding: [0x73,0x00,0x40,0x10]
29+
# CHECK-NO-EXT: error: instruction requires the following: 'Smctr' (Control Transfer Records Machine Level) or 'Ssctr' (Control Transfer Records Supervisor Level){{$}}
30+
sctrclr

llvm/test/MC/RISCV/supervisor-csr-names.s

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,46 @@ csrrs t2, 0xDB0, zero
457457
csrrs t1, scountinhibit, zero
458458
# uimm12
459459
csrrs t2, 0x120, zero
460+
461+
##################################
462+
# Control Transfer Records
463+
##################################
464+
465+
# sctrctl
466+
# name
467+
# CHECK-INST: csrrs t1, sctrctl, zero
468+
# CHECK-ENC: encoding: [0x73,0x23,0xe0,0x14]
469+
# CHECK-INST-ALIAS: csrr t1, sctrctl
470+
# uimm12
471+
# CHECK-INST: csrrs t2, sctrctl, zero
472+
# CHECK-ENC: encoding: [0xf3,0x23,0xe0,0x14]
473+
# CHECK-INST-ALIAS: csrr t2, sctrctl
474+
csrrs t1, sctrctl, zero
475+
# uimm12
476+
csrrs t2, 0x14E, zero
477+
478+
# sctrstatus
479+
# name
480+
# CHECK-INST: csrrs t1, sctrstatus, zero
481+
# CHECK-ENC: encoding: [0x73,0x23,0xf0,0x14]
482+
# CHECK-INST-ALIAS: csrr t1, sctrstatus
483+
# uimm12
484+
# CHECK-INST: csrrs t2, sctrstatus, zero
485+
# CHECK-ENC: encoding: [0xf3,0x23,0xf0,0x14]
486+
# CHECK-INST-ALIAS: csrr t2, sctrstatus
487+
csrrs t1, sctrstatus, zero
488+
# uimm12
489+
csrrs t2, 0x14F, zero
490+
491+
# sctrdepth
492+
# name
493+
# CHECK-INST: csrrs t1, sctrdepth, zero
494+
# CHECK-ENC: encoding: [0x73,0x23,0xf0,0x15]
495+
# CHECK-INST-ALIAS: csrr t1, sctrdepth
496+
# uimm12
497+
# CHECK-INST: csrrs t2, sctrdepth, zero
498+
# CHECK-ENC: encoding: [0xf3,0x23,0xf0,0x15]
499+
# CHECK-INST-ALIAS: csrr t2, sctrdepth
500+
csrrs t1, sctrdepth, zero
501+
# uimm12
502+
csrrs t2, 0x15F, zero

llvm/unittests/TargetParser/RISCVISAInfoTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,10 @@ Experimental extensions
11201120
zalasr 0.1
11211121
zvbc32e 0.7
11221122
zvkgs 0.7
1123+
smctr 1.0
11231124
smmpm 1.0
11241125
smnpm 1.0
1126+
ssctr 1.0
11251127
ssnpm 1.0
11261128
sspm 1.0
11271129
supm 1.0

0 commit comments

Comments
 (0)