[PR,target/110201] Fix operand types for various scalar crypto insns

Message ID f7505008-3132-20e0-da7b-00ec1ee66cc3@gmail.com
State New
Headers
Series [PR,target/110201] Fix operand types for various scalar crypto insns |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Testing passed

Commit Message

Jeff Law June 19, 2023, 10:34 p.m. UTC
  A handful of the scalar crypto instructions are supposed to take a 
constant integer argument 0..3 inclusive.  A suitable constraint was 
created and used for this purpose (D03), but the operand's predicate is 
"register_operand".  That's just wrong.

This patch adds a new predicate "const_0_3_operand" and fixes the 
relevant insns to use it.  One could argue the constraint is redundant 
now (and you'd be correct).  I wouldn't lose sleep if someone wanted 
that removed, in which case I'll spin up a V2.

The testsuite was broken in a way that made it consistent with the 
compiler, so the tests passed, when they really should have been issuing 
errors all along.

This patch adjusts the existing tests so that they all expect a 
diagnostic on the invalid operand usage (including out of range 
constants).  It adds new tests with proper constants, testing the 
extremes of valid values.

OK for the trunk, or should we remove the D03 constraint?

Jeff
PR target/110201
gcc/
	* config/riscv/predicates.md (const_0_3_operand): New predicate.
	* config/riscv/crypto.md (riscv_aes32dsi): Use new predicate.
	(riscv_aes32dsmi, riscv_aes32esi, riscvaes32esmi): Likewise.
	(riscv_sm4ed_<mode>, riscv_sm4ks_<mode): Likewise.

gcc/testsuite
	* testsuite/gcc.target/riscv/zknd32.c: Verify diagnostics are issued
	for invalid builtin arguments.
	* testsuite/gcc.target/riscv/zkne32.c: Likewise.
	* testsuite/gcc.target/riscv/zksed32.c: Likewise.
	* testsuite/gcc.target/riscv/zksed64.c: Likewise.
	* testsuite/gcc.target/riscv/zknd32-2.c: New test.
	* testsuite/gcc.target/riscv/zkne32-2.c: Likewise.
	* testsuite/gcc.target/riscv/zksed32-2.c: Likewise.
	* testsuite/gcc.target/riscv/zksed64-2.c: Likewise.
  

Comments

Christoph Müllner Dec. 14, 2023, 9:46 a.m. UTC | #1
On Tue, Jun 20, 2023 at 12:34 AM Jeff Law via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
>
> A handful of the scalar crypto instructions are supposed to take a
> constant integer argument 0..3 inclusive.  A suitable constraint was
> created and used for this purpose (D03), but the operand's predicate is
> "register_operand".  That's just wrong.
>
> This patch adds a new predicate "const_0_3_operand" and fixes the
> relevant insns to use it.  One could argue the constraint is redundant
> now (and you'd be correct).  I wouldn't lose sleep if someone wanted
> that removed, in which case I'll spin up a V2.
>
> The testsuite was broken in a way that made it consistent with the
> compiler, so the tests passed, when they really should have been issuing
> errors all along.
>
> This patch adjusts the existing tests so that they all expect a
> diagnostic on the invalid operand usage (including out of range
> constants).  It adds new tests with proper constants, testing the
> extremes of valid values.
>
> OK for the trunk, or should we remove the D03 constraint?

Reviewed-by: Christoph Muellner <christoph.muellner@vrull.eu>

The patch does not apply cleanly anymore, because there were some
small changes in crypto.md.
  
Jeff Law Dec. 14, 2023, 11:36 p.m. UTC | #2
On 12/14/23 02:46, Christoph Müllner wrote:
> On Tue, Jun 20, 2023 at 12:34 AM Jeff Law via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>>
>>
>> A handful of the scalar crypto instructions are supposed to take a
>> constant integer argument 0..3 inclusive.  A suitable constraint was
>> created and used for this purpose (D03), but the operand's predicate is
>> "register_operand".  That's just wrong.
>>
>> This patch adds a new predicate "const_0_3_operand" and fixes the
>> relevant insns to use it.  One could argue the constraint is redundant
>> now (and you'd be correct).  I wouldn't lose sleep if someone wanted
>> that removed, in which case I'll spin up a V2.
>>
>> The testsuite was broken in a way that made it consistent with the
>> compiler, so the tests passed, when they really should have been issuing
>> errors all along.
>>
>> This patch adjusts the existing tests so that they all expect a
>> diagnostic on the invalid operand usage (including out of range
>> constants).  It adds new tests with proper constants, testing the
>> extremes of valid values.
>>
>> OK for the trunk, or should we remove the D03 constraint?
> 
> Reviewed-by: Christoph Muellner <christoph.muellner@vrull.eu>
> 
> The patch does not apply cleanly anymore, because there were some
> small changes in crypto.md.
Here's an update to that old patch that also takes care of the pattern 
where we allow 0..10 inclusive, but not registers.

Regression tested on rv64gc without new failures.  It'll need a 
ChangeLog when approved, but that's easy to adjust.

jeff
diff --git a/gcc/config/riscv/constraints.md b/gcc/config/riscv/constraints.md
index 9836fd34460..d0f95b2b594 100644
--- a/gcc/config/riscv/constraints.md
+++ b/gcc/config/riscv/constraints.md
@@ -103,14 +103,6 @@ (define_constraint "DnS"
   (and (match_code "const_int")
        (match_test "SINGLE_BIT_MASK_OPERAND (~ival)")))
 
-(define_constraint "D03"
-  "0, 1, 2 or 3 immediate"
-  (match_test "IN_RANGE (ival, 0, 3)"))
-
-(define_constraint "DsA"
-  "0 - 10 immediate"
-  (match_test "IN_RANGE (ival, 0, 10)"))
-
 ;; Floating-point constant +0.0, used for FCVT-based moves when FMV is
 ;; not available in RV32.
 (define_constraint "G"
diff --git a/gcc/config/riscv/crypto.md b/gcc/config/riscv/crypto.md
index 03a1d03397d..2b65fadeb15 100644
--- a/gcc/config/riscv/crypto.md
+++ b/gcc/config/riscv/crypto.md
@@ -148,7 +148,7 @@ (define_insn "riscv_aes32dsi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "")]
                    UNSPEC_AES_DSI))]
   "TARGET_ZKND && !TARGET_64BIT"
   "aes32dsi\t%0,%1,%2,%3"
@@ -158,7 +158,7 @@ (define_insn "riscv_aes32dsmi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "")]
                    UNSPEC_AES_DSMI))]
   "TARGET_ZKND && !TARGET_64BIT"
   "aes32dsmi\t%0,%1,%2,%3"
@@ -193,7 +193,7 @@ (define_insn "riscv_aes64im"
 (define_insn "riscv_aes64ks1i"
   [(set (match_operand:DI 0 "register_operand" "=r")
         (unspec:DI [(match_operand:DI 1 "register_operand" "r")
-                   (match_operand:SI 2 "register_operand" "DsA")]
+                   (match_operand:SI 2 "const_0_10_operand" "")]
                    UNSPEC_AES_KS1I))]
   "(TARGET_ZKND || TARGET_ZKNE) && TARGET_64BIT"
   "aes64ks1i\t%0,%1,%2"
@@ -214,7 +214,7 @@ (define_insn "riscv_aes32esi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "")]
                    UNSPEC_AES_ESI))]
   "TARGET_ZKNE && !TARGET_64BIT"
   "aes32esi\t%0,%1,%2,%3"
@@ -224,7 +224,7 @@ (define_insn "riscv_aes32esmi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "")]
                    UNSPEC_AES_ESMI))]
   "TARGET_ZKNE && !TARGET_64BIT"
   "aes32esmi\t%0,%1,%2,%3"
@@ -431,7 +431,7 @@ (define_insn "*riscv_<sm4_op>_si"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "")]
                    SM4_OP))]
   "TARGET_ZKSED && !TARGET_64BIT"
   "<sm4_op>\t%0,%1,%2,%3"
@@ -442,7 +442,7 @@ (define_insn "riscv_<sm4_op>_di_extended"
         (sign_extend:DI
              (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                         (match_operand:SI 2 "register_operand" "r")
-                        (match_operand:SI 3 "register_operand" "D03")]
+                        (match_operand:SI 3 "const_0_3_operand" "")]
                         SM4_OP)))]
   "TARGET_ZKSED && TARGET_64BIT"
   "<sm4_op>\t%0,%1,%2,%3"
@@ -452,7 +452,7 @@ (define_expand "riscv_<sm4_op>_si"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "")]
                    SM4_OP))]
   "TARGET_ZKSED"
   {
diff --git a/gcc/config/riscv/predicates.md b/gcc/config/riscv/predicates.md
index 525455f6db6..6bf6e186641 100644
--- a/gcc/config/riscv/predicates.md
+++ b/gcc/config/riscv/predicates.md
@@ -51,6 +51,14 @@ (define_predicate "const_csr_operand"
   (and (match_code "const_int")
        (match_test "IN_RANGE (INTVAL (op), 0, 31)")))
 
+(define_predicate "const_0_3_operand"
+  (and (match_code "const_int")
+       (match_test "IN_RANGE (INTVAL (op), 0, 3)")))
+
+(define_predicate "const_0_10_operand"
+  (and (match_code "const_int")
+       (match_test "IN_RANGE (INTVAL (op), 0, 10)")))
+
 (define_predicate "csr_operand"
   (ior (match_operand 0 "const_csr_operand")
        (match_operand 0 "register_operand")))
diff --git a/gcc/testsuite/gcc.target/riscv/zknd32-2.c b/gcc/testsuite/gcc.target/riscv/zknd32-2.c
new file mode 100644
index 00000000000..f3549e786c7
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zknd32-2.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zknd -mabi=ilp32d" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+uint32_t foo1(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,0);
+}
+
+uint32_t foo2(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,0);
+}
+
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,3);
+}
+
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,3);
+}
+
+/* { dg-final { scan-assembler-times "aes32dsi" 2 } } */
+/* { dg-final { scan-assembler-times "aes32dsmi" 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zknd32.c b/gcc/testsuite/gcc.target/riscv/zknd32.c
index e60c027e091..31d475a8d83 100644
--- a/gcc/testsuite/gcc.target/riscv/zknd32.c
+++ b/gcc/testsuite/gcc.target/riscv/zknd32.c
@@ -6,13 +6,30 @@
 
 uint32_t foo1(uint32_t rs1, uint32_t rs2, int bs)
 {
-    return __builtin_riscv_aes32dsi(rs1,rs2,bs);
+    return __builtin_riscv_aes32dsi(rs1,rs2,bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
 uint32_t foo2(uint32_t rs1, uint32_t rs2, int bs)
 {
-    return __builtin_riscv_aes32dsmi(rs1,rs2,bs);
+    return __builtin_riscv_aes32dsmi(rs1,rs2,bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
-/* { dg-final { scan-assembler-times "aes32dsi" 1 } } */
-/* { dg-final { scan-assembler-times "aes32dsmi" 1 } } */
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,-1);	/* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,-1);	/* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo5(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,4);		/* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo6(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,4);	/* { dg-error "invalid argument to built-in function" } */
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zknd64-2.c b/gcc/testsuite/gcc.target/riscv/zknd64-2.c
new file mode 100644
index 00000000000..cd0e79d60f5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zknd64-2.c
@@ -0,0 +1,42 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zknd -mabi=lp64" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+uint64_t foo1(uint64_t rs1, uint64_t rs2)
+{
+    return __builtin_riscv_aes64ds(rs1,rs2);
+}
+
+uint64_t foo2(uint64_t rs1, uint64_t rs2)
+{
+    return __builtin_riscv_aes64dsm(rs1,rs2);
+}
+
+uint64_t foo3(uint64_t rs1, unsigned rnum)
+{
+    return __builtin_riscv_aes64ks1i(rs1,0);
+}
+
+uint64_t foo3a(uint64_t rs1, unsigned rnum)
+{
+    return __builtin_riscv_aes64ks1i(rs1,10);
+}
+
+uint64_t foo4(uint64_t rs1, uint64_t rs2)
+{
+    return __builtin_riscv_aes64ks2(rs1,rs2);
+}
+
+uint64_t foo5(uint64_t rs1)
+{
+    return __builtin_riscv_aes64im(rs1);
+}
+
+/* { dg-final { scan-assembler-times "aes64ds\t" 1 } } */
+/* { dg-final { scan-assembler-times "aes64dsm" 1 } } */
+/* { dg-final { scan-assembler-times "aes64ks1i" 2 } } */
+/* { dg-final { scan-assembler-times "aes64ks2" 1 } } */
+/* { dg-final { scan-assembler-times {\maes64im} 1 } } */
+
diff --git a/gcc/testsuite/gcc.target/riscv/zknd64.c b/gcc/testsuite/gcc.target/riscv/zknd64.c
index 707418cd51e..e6a7b88813e 100644
--- a/gcc/testsuite/gcc.target/riscv/zknd64.c
+++ b/gcc/testsuite/gcc.target/riscv/zknd64.c
@@ -4,33 +4,17 @@
 
 #include <stdint-gcc.h>
 
-uint64_t foo1(uint64_t rs1, uint64_t rs2)
-{
-    return __builtin_riscv_aes64ds(rs1,rs2);
-}
-
-uint64_t foo2(uint64_t rs1, uint64_t rs2)
-{
-    return __builtin_riscv_aes64dsm(rs1,rs2);
-}
-
 uint64_t foo3(uint64_t rs1, unsigned rnum)
 {
-    return __builtin_riscv_aes64ks1i(rs1,rnum);
+    return __builtin_riscv_aes64ks1i(rs1,rnum);	/* { dg-error "invalid argument to built-in function" } */
 }
 
-uint64_t foo4(uint64_t rs1, uint64_t rs2)
+uint64_t foo3a(uint64_t rs1, unsigned rnum)
 {
-    return __builtin_riscv_aes64ks2(rs1,rs2);
+    return __builtin_riscv_aes64ks1i(rs1,-1);	/* { dg-error "invalid argument to built-in function" } */
 }
 
-uint64_t foo5(uint64_t rs1)
+uint64_t foo3b(uint64_t rs1, unsigned rnum)
 {
-    return __builtin_riscv_aes64im(rs1);
+    return __builtin_riscv_aes64ks1i(rs1,11);	/* { dg-error "invalid argument to built-in function" } */
 }
-
-/* { dg-final { scan-assembler-times "aes64ds\t" 1 } } */
-/* { dg-final { scan-assembler-times "aes64dsm" 1 } } */
-/* { dg-final { scan-assembler-times "aes64ks1i" 1 } } */
-/* { dg-final { scan-assembler-times "aes64ks2" 1 } } */
-/* { dg-final { scan-assembler-times {\maes64im} 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zkne32-2.c b/gcc/testsuite/gcc.target/riscv/zkne32-2.c
new file mode 100644
index 00000000000..4ad1cdcdd80
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zkne32-2.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zkne -mabi=ilp32d" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+uint32_t foo1(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, 0);
+}
+
+uint32_t foo2(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, 0);
+}
+
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, 3);
+}
+
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, 3);
+}
+
+/* { dg-final { scan-assembler-times "aes32esi" 2 } } */
+/* { dg-final { scan-assembler-times "aes32esmi" 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zkne32.c b/gcc/testsuite/gcc.target/riscv/zkne32.c
index 252e9ffa43b..68dad613dad 100644
--- a/gcc/testsuite/gcc.target/riscv/zkne32.c
+++ b/gcc/testsuite/gcc.target/riscv/zkne32.c
@@ -6,13 +6,30 @@
 
 uint32_t foo1(uint32_t rs1, uint32_t rs2, unsigned bs)
 {
-    return __builtin_riscv_aes32esi(rs1, rs2, bs);
+    return __builtin_riscv_aes32esi(rs1, rs2, bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
 uint32_t foo2(uint32_t rs1, uint32_t rs2, unsigned bs)
 {
-    return __builtin_riscv_aes32esmi(rs1, rs2, bs);
+    return __builtin_riscv_aes32esmi(rs1, rs2, bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
-/* { dg-final { scan-assembler-times "aes32esi" 1 } } */
-/* { dg-final { scan-assembler-times "aes32esmi" 1 } } */
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, -1);	/* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, -1);	/* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo5(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, 4);	/* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo6(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, 4);	/* { dg-error "invalid argument to built-in function" } */
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zkne64-2.c b/gcc/testsuite/gcc.target/riscv/zkne64-2.c
new file mode 100644
index 00000000000..144c394365b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zkne64-2.c
@@ -0,0 +1,34 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zkne -mabi=lp64" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+uint64_t foo1(uint64_t rs1, uint64_t rs2)
+{
+    return __builtin_riscv_aes64es(rs1,rs2);
+}
+
+uint64_t foo2(uint64_t rs1, uint64_t rs2)
+{
+    return __builtin_riscv_aes64esm(rs1,rs2);
+}
+
+uint64_t foo3(uint64_t rs1, unsigned rnum)
+{
+    return __builtin_riscv_aes64ks1i(rs1,0);
+}
+
+uint64_t foo3a(uint64_t rs1, unsigned rnum)
+{
+    return __builtin_riscv_aes64ks1i(rs1,10);
+}
+uint64_t foo4(uint64_t rs1, uint64_t rs2)
+{
+    return __builtin_riscv_aes64ks2(rs1,rs2);
+}
+
+/* { dg-final { scan-assembler-times "aes64es\t" 1 } } */
+/* { dg-final { scan-assembler-times "aes64esm" 1 } } */
+/* { dg-final { scan-assembler-times "aes64ks1i" 2 } } */
+/* { dg-final { scan-assembler-times "aes64ks2" 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zkne64.c b/gcc/testsuite/gcc.target/riscv/zkne64.c
index b25f6b5c29a..88a89f8762c 100644
--- a/gcc/testsuite/gcc.target/riscv/zkne64.c
+++ b/gcc/testsuite/gcc.target/riscv/zkne64.c
@@ -4,27 +4,18 @@
 
 #include <stdint-gcc.h>
 
-uint64_t foo1(uint64_t rs1, uint64_t rs2)
+uint64_t foo1(uint64_t rs1, unsigned rnum)
 {
-    return __builtin_riscv_aes64es(rs1,rs2);
+    return __builtin_riscv_aes64ks1i(rs1,-1);	/* { dg-error "invalid argument to built-in function" } */
 }
 
-uint64_t foo2(uint64_t rs1, uint64_t rs2)
+uint64_t foo2(uint64_t rs1, unsigned rnum)
 {
-    return __builtin_riscv_aes64esm(rs1,rs2);
+    return __builtin_riscv_aes64ks1i(rs1,11);	/* { dg-error "invalid argument to built-in function" } */
 }
 
 uint64_t foo3(uint64_t rs1, unsigned rnum)
 {
-    return __builtin_riscv_aes64ks1i(rs1,rnum);
+    return __builtin_riscv_aes64ks1i(rs1,rnum);	/* { dg-error "invalid argument to built-in function" } */
 }
 
-uint64_t foo4(uint64_t rs1, uint64_t rs2)
-{
-    return __builtin_riscv_aes64ks2(rs1,rs2);
-}
-
-/* { dg-final { scan-assembler-times "aes64es\t" 1 } } */
-/* { dg-final { scan-assembler-times "aes64esm" 1 } } */
-/* { dg-final { scan-assembler-times "aes64ks1i" 1 } } */
-/* { dg-final { scan-assembler-times "aes64ks2" 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zksed32-2.c b/gcc/testsuite/gcc.target/riscv/zksed32-2.c
new file mode 100644
index 00000000000..cee8cc217a9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zksed32-2.c
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zksed -mabi=ilp32" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+uint32_t foo1(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,0);
+}
+
+uint32_t foo2(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,0);
+}
+
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,3);
+}
+
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,3);
+}
+
+
+/* { dg-final { scan-assembler-times {\msm4ks} 2 } } */
+/* { dg-final { scan-assembler-times {\msm4ed} 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zksed32.c b/gcc/testsuite/gcc.target/riscv/zksed32.c
index 0e8f01cd548..cb178a2daeb 100644
--- a/gcc/testsuite/gcc.target/riscv/zksed32.c
+++ b/gcc/testsuite/gcc.target/riscv/zksed32.c
@@ -6,14 +6,30 @@
 
 uint32_t foo1(uint32_t rs1, uint32_t rs2, unsigned bs)
 {
-    return __builtin_riscv_sm4ks(rs1,rs2,bs);
+    return __builtin_riscv_sm4ks(rs1,rs2,bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
 uint32_t foo2(uint32_t rs1, uint32_t rs2, unsigned bs)
 {
-    return __builtin_riscv_sm4ed(rs1,rs2,bs);
+    return __builtin_riscv_sm4ed(rs1,rs2,bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,-1);	/* { dg-error "invalid argument to built-in function" } */
+}
 
-/* { dg-final { scan-assembler-times {\msm4ks} 1 } } */
-/* { dg-final { scan-assembler-times {\msm4ed} 1 } } */
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,-1);	/* { dg-error "invalid argument to built-in function" } */
+}
+ 
+uint32_t foo5(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,4);	/* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo6(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,4);	/* { dg-error "invalid argument to built-in function" } */
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zksed64-2.c b/gcc/testsuite/gcc.target/riscv/zksed64-2.c
new file mode 100644
index 00000000000..ee20aa1535e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zksed64-2.c
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zksed -mabi=lp64" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+uint32_t foo1(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,0);
+}
+
+uint32_t foo2(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,0);
+}
+
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,3);
+}
+
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,3);
+}
+
+
+/* { dg-final { scan-assembler-times {\msm4ks} 2 } } */
+/* { dg-final { scan-assembler-times {\msm4ed} 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zksed64.c b/gcc/testsuite/gcc.target/riscv/zksed64.c
index 9e4d1961419..d38ed1caf97 100644
--- a/gcc/testsuite/gcc.target/riscv/zksed64.c
+++ b/gcc/testsuite/gcc.target/riscv/zksed64.c
@@ -6,14 +6,30 @@
 
 uint32_t foo1(uint32_t rs1, uint32_t rs2, unsigned bs)
 {
-    return __builtin_riscv_sm4ks(rs1,rs2,bs);
+    return __builtin_riscv_sm4ks(rs1,rs2,bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
 uint32_t foo2(uint32_t rs1, uint32_t rs2, unsigned bs)
 {
-    return __builtin_riscv_sm4ed(rs1,rs2,bs);
+    return __builtin_riscv_sm4ed(rs1,rs2,bs);	/* { dg-error "invalid argument to built-in function" } */
 }
 
+uint32_t foo3(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,-1); /* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo4(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,-1); /* { dg-error "invalid argument to built-in function" } */
+}
 
-/* { dg-final { scan-assembler-times {\msm4ks} 1 } } */
-/* { dg-final { scan-assembler-times {\msm4ed} 1 } } */
+uint32_t foo5(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,4); /* { dg-error "invalid argument to built-in function" } */
+}
+
+uint32_t foo6(uint32_t rs1, uint32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,4); /* { dg-error "invalid argument to built-in function" } */
+}
  
Christoph Müllner Dec. 15, 2023, 12:14 a.m. UTC | #3
On Fri, Dec 15, 2023 at 12:36 AM Jeff Law <jeffreyalaw@gmail.com> wrote:
>
>
>
> On 12/14/23 02:46, Christoph Müllner wrote:
> > On Tue, Jun 20, 2023 at 12:34 AM Jeff Law via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> >>
> >>
> >> A handful of the scalar crypto instructions are supposed to take a
> >> constant integer argument 0..3 inclusive.  A suitable constraint was
> >> created and used for this purpose (D03), but the operand's predicate is
> >> "register_operand".  That's just wrong.
> >>
> >> This patch adds a new predicate "const_0_3_operand" and fixes the
> >> relevant insns to use it.  One could argue the constraint is redundant
> >> now (and you'd be correct).  I wouldn't lose sleep if someone wanted
> >> that removed, in which case I'll spin up a V2.
> >>
> >> The testsuite was broken in a way that made it consistent with the
> >> compiler, so the tests passed, when they really should have been issuing
> >> errors all along.
> >>
> >> This patch adjusts the existing tests so that they all expect a
> >> diagnostic on the invalid operand usage (including out of range
> >> constants).  It adds new tests with proper constants, testing the
> >> extremes of valid values.
> >>
> >> OK for the trunk, or should we remove the D03 constraint?
> >
> > Reviewed-by: Christoph Muellner <christoph.muellner@vrull.eu>
> >
> > The patch does not apply cleanly anymore, because there were some
> > small changes in crypto.md.
> Here's an update to that old patch that also takes care of the pattern
> where we allow 0..10 inclusive, but not registers.
>
> Regression tested on rv64gc without new failures.  It'll need a
> ChangeLog when approved, but that's easy to adjust.

Looks good and tests pass for rv64gc and rv32gc.

Reviewed-by: Christoph Muellner <christoph.muellner@vrull.eu>
Tested-by: Christoph Muellner <christoph.muellner@vrull.eu>
  
Jeff Law Dec. 15, 2023, 9:29 p.m. UTC | #4
On 12/14/23 17:14, Christoph Müllner wrote:
> On Fri, Dec 15, 2023 at 12:36 AM Jeff Law <jeffreyalaw@gmail.com> wrote:
>>
>>
>>
>> On 12/14/23 02:46, Christoph Müllner wrote:
>>> On Tue, Jun 20, 2023 at 12:34 AM Jeff Law via Gcc-patches
>>> <gcc-patches@gcc.gnu.org> wrote:
>>>>
>>>>
>>>> A handful of the scalar crypto instructions are supposed to take a
>>>> constant integer argument 0..3 inclusive.  A suitable constraint was
>>>> created and used for this purpose (D03), but the operand's predicate is
>>>> "register_operand".  That's just wrong.
>>>>
>>>> This patch adds a new predicate "const_0_3_operand" and fixes the
>>>> relevant insns to use it.  One could argue the constraint is redundant
>>>> now (and you'd be correct).  I wouldn't lose sleep if someone wanted
>>>> that removed, in which case I'll spin up a V2.
>>>>
>>>> The testsuite was broken in a way that made it consistent with the
>>>> compiler, so the tests passed, when they really should have been issuing
>>>> errors all along.
>>>>
>>>> This patch adjusts the existing tests so that they all expect a
>>>> diagnostic on the invalid operand usage (including out of range
>>>> constants).  It adds new tests with proper constants, testing the
>>>> extremes of valid values.
>>>>
>>>> OK for the trunk, or should we remove the D03 constraint?
>>>
>>> Reviewed-by: Christoph Muellner <christoph.muellner@vrull.eu>
>>>
>>> The patch does not apply cleanly anymore, because there were some
>>> small changes in crypto.md.
>> Here's an update to that old patch that also takes care of the pattern
>> where we allow 0..10 inclusive, but not registers.
>>
>> Regression tested on rv64gc without new failures.  It'll need a
>> ChangeLog when approved, but that's easy to adjust.
> 
> Looks good and tests pass for rv64gc and rv32gc.
> 
> Reviewed-by: Christoph Muellner <christoph.muellner@vrull.eu>
> Tested-by: Christoph Muellner <christoph.muellner@vrull.eu>
I've pushed this to the trunk with Liao listed as a co-author.

jeff
  

Patch

diff --git a/gcc/config/riscv/crypto.md b/gcc/config/riscv/crypto.md
index e4b7f0190df..2173cabfcd0 100644
--- a/gcc/config/riscv/crypto.md
+++ b/gcc/config/riscv/crypto.md
@@ -148,7 +148,7 @@  (define_insn "riscv_aes32dsi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "D03")]
                    UNSPEC_AES_DSI))]
   "TARGET_ZKND && !TARGET_64BIT"
   "aes32dsi\t%0,%1,%2,%3"
@@ -158,7 +158,7 @@  (define_insn "riscv_aes32dsmi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "D03")]
                    UNSPEC_AES_DSMI))]
   "TARGET_ZKND && !TARGET_64BIT"
   "aes32dsmi\t%0,%1,%2,%3"
@@ -214,7 +214,7 @@  (define_insn "riscv_aes32esi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "D03")]
                    UNSPEC_AES_ESI))]
   "TARGET_ZKNE && !TARGET_64BIT"
   "aes32esi\t%0,%1,%2,%3"
@@ -224,7 +224,7 @@  (define_insn "riscv_aes32esmi"
   [(set (match_operand:SI 0 "register_operand" "=r")
         (unspec:SI [(match_operand:SI 1 "register_operand" "r")
                    (match_operand:SI 2 "register_operand" "r")
-                   (match_operand:SI 3 "register_operand" "D03")]
+                   (match_operand:SI 3 "const_0_3_operand" "D03")]
                    UNSPEC_AES_ESMI))]
   "TARGET_ZKNE && !TARGET_64BIT"
   "aes32esmi\t%0,%1,%2,%3"
@@ -394,7 +394,7 @@  (define_insn "riscv_sm4ed_<mode>"
   [(set (match_operand:X 0 "register_operand" "=r")
         (unspec:X [(match_operand:X 1 "register_operand" "r")
                   (match_operand:X 2 "register_operand" "r")
-                  (match_operand:SI 3 "register_operand" "D03")]
+                  (match_operand:SI 3 "const_0_3_operand" "D03")]
                   UNSPEC_SM4_ED))]
   "TARGET_ZKSED"
   "sm4ed\t%0,%1,%2,%3"
@@ -404,7 +404,7 @@  (define_insn "riscv_sm4ks_<mode>"
   [(set (match_operand:X 0 "register_operand" "=r")
         (unspec:X [(match_operand:X 1 "register_operand" "r")
                   (match_operand:X 2 "register_operand" "r")
-                  (match_operand:SI 3 "register_operand" "D03")]
+                  (match_operand:SI 3 "const_0_3_operand" "D03")]
                   UNSPEC_SM4_KS))]
   "TARGET_ZKSED"
   "sm4ks\t%0,%1,%2,%3"
diff --git a/gcc/config/riscv/predicates.md b/gcc/config/riscv/predicates.md
index 04ca6ceabc7..7aed71b5123 100644
--- a/gcc/config/riscv/predicates.md
+++ b/gcc/config/riscv/predicates.md
@@ -45,6 +45,10 @@  (define_predicate "const_csr_operand"
   (and (match_code "const_int")
        (match_test "IN_RANGE (INTVAL (op), 0, 31)")))
 
+(define_predicate "const_0_3_operand"
+  (and (match_code "const_int")
+       (match_test "IN_RANGE (INTVAL (op), 0, 3)")))
+
 (define_predicate "csr_operand"
   (ior (match_operand 0 "const_csr_operand")
        (match_operand 0 "register_operand")))
diff --git a/gcc/testsuite/gcc.target/riscv/zknd32-2.c b/gcc/testsuite/gcc.target/riscv/zknd32-2.c
new file mode 100644
index 00000000000..f8e68c6e56b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zknd32-2.c
@@ -0,0 +1,28 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zknd -mabi=ilp32d" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+int32_t foo1(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,0);
+}
+
+int32_t foo2(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,0);
+}
+
+int32_t foo3(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,3);
+}
+
+int32_t foo4(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,3);
+}
+
+/* { dg-final { scan-assembler-times "aes32dsi" 2 } } */
+/* { dg-final { scan-assembler-times "aes32dsmi" 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zknd32.c b/gcc/testsuite/gcc.target/riscv/zknd32.c
index 5fcc66da901..7370a2c1812 100644
--- a/gcc/testsuite/gcc.target/riscv/zknd32.c
+++ b/gcc/testsuite/gcc.target/riscv/zknd32.c
@@ -6,13 +6,30 @@ 
 
 int32_t foo1(int32_t rs1, int32_t rs2, int bs)
 {
-    return __builtin_riscv_aes32dsi(rs1,rs2,bs);
+    return __builtin_riscv_aes32dsi(rs1,rs2,bs); /* { dg-error "invalid argument to built-in function" } */
 }
 
 int32_t foo2(int32_t rs1, int32_t rs2, int bs)
 {
-    return __builtin_riscv_aes32dsmi(rs1,rs2,bs);
+    return __builtin_riscv_aes32dsmi(rs1,rs2,bs); /* { dg-error "invalid argument to built-in function" } */
 }
 
-/* { dg-final { scan-assembler-times "aes32dsi" 1 } } */
-/* { dg-final { scan-assembler-times "aes32dsmi" 1 } } */
+int32_t foo3(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,-1); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo4(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,-1); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo5(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsi(rs1,rs2,4); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo6(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32dsmi(rs1,rs2,4); /* { dg-error "invalid argument to built-in function" } */
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zkne32-2.c b/gcc/testsuite/gcc.target/riscv/zkne32-2.c
new file mode 100644
index 00000000000..57a9b3f7d2e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zkne32-2.c
@@ -0,0 +1,28 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zkne -mabi=ilp32d" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+int32_t foo1(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, 0);
+}
+
+int32_t foo2(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, 0);
+}
+
+int32_t foo3(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, 3);
+}
+
+int32_t foo4(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, 3);
+}
+
+/* { dg-final { scan-assembler-times "aes32esi" 2 } } */
+/* { dg-final { scan-assembler-times "aes32esmi" 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zkne32.c b/gcc/testsuite/gcc.target/riscv/zkne32.c
index c131c9a6bbb..c7a0d0d02b8 100644
--- a/gcc/testsuite/gcc.target/riscv/zkne32.c
+++ b/gcc/testsuite/gcc.target/riscv/zkne32.c
@@ -6,13 +6,31 @@ 
 
 int32_t foo1(int32_t rs1, int32_t rs2, int bs)
 {
-    return __builtin_riscv_aes32esi(rs1, rs2, bs);
+    return __builtin_riscv_aes32esi(rs1, rs2, bs); /* { dg-error "invalid argument to built-in function" } */
 }
 
 int32_t foo2(int32_t rs1, int32_t rs2, int bs)
 {
-    return __builtin_riscv_aes32esmi(rs1, rs2, bs);
+    return __builtin_riscv_aes32esmi(rs1, rs2, bs); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo3(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, -1); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo4(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, -1); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo5(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esi(rs1, rs2, 4); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo6(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_aes32esmi(rs1, rs2, 4); /* { dg-error "invalid argument to built-in function" } */
 }
 
-/* { dg-final { scan-assembler-times "aes32esi" 1 } } */
-/* { dg-final { scan-assembler-times "aes32esmi" 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zksed32-2.c b/gcc/testsuite/gcc.target/riscv/zksed32-2.c
new file mode 100644
index 00000000000..6cb5ac35d75
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zksed32-2.c
@@ -0,0 +1,29 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zksed -mabi=ilp32" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+int32_t foo1(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,0);
+}
+
+int32_t foo2(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,0);
+}
+
+int32_t foo3(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,3);
+}
+
+int32_t foo4(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,3);
+}
+
+
+/* { dg-final { scan-assembler-times "sm4ks" 2 } } */
+/* { dg-final { scan-assembler-times "sm4ed" 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zksed32.c b/gcc/testsuite/gcc.target/riscv/zksed32.c
index 9548d007cb2..ee296dc7d8f 100644
--- a/gcc/testsuite/gcc.target/riscv/zksed32.c
+++ b/gcc/testsuite/gcc.target/riscv/zksed32.c
@@ -6,14 +6,31 @@ 
 
 int32_t foo1(int32_t rs1, int32_t rs2, int bs)
 {
-    return __builtin_riscv_sm4ks(rs1,rs2,bs);
+    return __builtin_riscv_sm4ks(rs1,rs2,bs); /* { dg-error "invalid argument to built-in function" } */
 }
 
 int32_t foo2(int32_t rs1, int32_t rs2, int bs)
 {
-    return __builtin_riscv_sm4ed(rs1,rs2,bs);
+    return __builtin_riscv_sm4ed(rs1,rs2,bs);/* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo3(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,-1); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo4(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,-1);/* { dg-error "invalid argument to built-in function" } */
 }
 
 
-/* { dg-final { scan-assembler-times "sm4ks" 1 } } */
-/* { dg-final { scan-assembler-times "sm4ed" 1 } } */
+int32_t foo5(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,4); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int32_t foo6(int32_t rs1, int32_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,4);/* { dg-error "invalid argument to built-in function" } */
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zksed64-2.c b/gcc/testsuite/gcc.target/riscv/zksed64-2.c
new file mode 100644
index 00000000000..5fb0bfd69f8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zksed64-2.c
@@ -0,0 +1,29 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zksed -mabi=lp64" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto"} } */
+
+#include <stdint-gcc.h>
+
+int64_t foo1(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,0);
+}
+
+int64_t foo2(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,0);
+}
+
+int64_t foo3(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,3);
+}
+
+int64_t foo4(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,3);
+}
+
+
+/* { dg-final { scan-assembler-times "sm4ks" 2 } } */
+/* { dg-final { scan-assembler-times "sm4ed" 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zksed64.c b/gcc/testsuite/gcc.target/riscv/zksed64.c
index 190a654151d..f7bf9f0c79a 100644
--- a/gcc/testsuite/gcc.target/riscv/zksed64.c
+++ b/gcc/testsuite/gcc.target/riscv/zksed64.c
@@ -6,14 +6,30 @@ 
 
 int64_t foo1(int64_t rs1, int64_t rs2, int bs)
 {
-    return __builtin_riscv_sm4ks(rs1,rs2,bs);
+    return __builtin_riscv_sm4ks(rs1,rs2,bs); /* { dg-error "invalid argument to built-in function" } */
 }
 
 int64_t foo2(int64_t rs1, int64_t rs2, int bs)
 {
-    return __builtin_riscv_sm4ed(rs1,rs2,bs);
+    return __builtin_riscv_sm4ed(rs1,rs2,bs); /* { dg-error "invalid argument to built-in function" } */
 }
 
+int64_t foo3(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,-1); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int64_t foo4(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,-1); /* { dg-error "invalid argument to built-in function" } */
+}
 
-/* { dg-final { scan-assembler-times "sm4ks" 1 } } */
-/* { dg-final { scan-assembler-times "sm4ed" 1 } } */
+int64_t foo5(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ks(rs1,rs2,4); /* { dg-error "invalid argument to built-in function" } */
+}
+
+int64_t foo6(int64_t rs1, int64_t rs2)
+{
+    return __builtin_riscv_sm4ed(rs1,rs2,4); /* { dg-error "invalid argument to built-in function" } */
+}