diff mbox series

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

Message ID 4524bcd7-53ec-b4b7-59ae-19728281d5bc@gmail.com
State New
Headers show
Series [PING,RISCV,PR,target/110201] Fix operand types for various scalar crypto insns | expand

Commit Message

Jeff Law June 25, 2023, 1:13 p.m. UTC
Ping.  With RISCV tag this time.

-------- Forwarded Message --------
Subject: [PR target/110201] Fix operand types for various scalar crypto 
insns
Date: Mon, 19 Jun 2023 16:34:28 -0600
From: Jeff Law <jeffreyalaw@gmail.com>
To: gcc-patches@gcc.gnu.org <gcc-patches@gcc.gnu.org>


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.
diff mbox series

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" } */
+}