[RFC] RISC-V: Add support for vector crypto extensions

Message ID 20221221183103.3800844-1-christoph.muellner@vrull.eu
State Accepted
Headers
Series [RFC] RISC-V: Add support for vector crypto extensions |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Christoph Müllner Dec. 21, 2022, 6:31 p.m. UTC
  From: Christoph Müllner <christoph.muellner@vrull.eu>

This series adds basic support for the vector crypto extensions:
* Zvkb
* Zvkg
* Zvkh[a,b]
* Zvkn
* Zvksed
* Zvksh

The implementation follows the version 20221220 of the specification,
which can be found here:
  https://github.com/riscv/riscv-crypto/releases/tag/v20221220

Note, that this specification is not frozen yet, meaning that
incompatible changes are possible.
Therefore, this patchset is marked as RFC and should not be considered
for upstream inclusion.

All extensions come with (passing) tests for the feature test macros.

A Binutils patch series for vector crypto support can be found here:
  https://sourceware.org/pipermail/binutils/2022-December/125272.html

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 gcc/common/config/riscv/riscv-common.cc | 16 ++++++++++++++++
 gcc/config/riscv/riscv-opts.h           | 16 ++++++++++++++++
 gcc/config/riscv/riscv.opt              |  3 +++
 gcc/testsuite/gcc.target/riscv/zvkb.c   | 13 +++++++++++++
 gcc/testsuite/gcc.target/riscv/zvkg.c   | 13 +++++++++++++
 gcc/testsuite/gcc.target/riscv/zvkha.c  | 13 +++++++++++++
 gcc/testsuite/gcc.target/riscv/zvkhb.c  | 13 +++++++++++++
 gcc/testsuite/gcc.target/riscv/zvkn.c   | 13 +++++++++++++
 gcc/testsuite/gcc.target/riscv/zvksed.c | 13 +++++++++++++
 gcc/testsuite/gcc.target/riscv/zvksh.c  | 13 +++++++++++++
 10 files changed, 126 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/zvkb.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/zvkg.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/zvkha.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/zvkhb.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/zvkn.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/zvksed.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/zvksh.c
  

Comments

Jeff Law Dec. 27, 2022, 5:35 p.m. UTC | #1
On 12/21/22 11:31, Christoph Muellner wrote:
> From: Christoph Müllner <christoph.muellner@vrull.eu>
> 
> This series adds basic support for the vector crypto extensions:
> * Zvkb
> * Zvkg
> * Zvkh[a,b]
> * Zvkn
> * Zvksed
> * Zvksh
> 
> The implementation follows the version 20221220 of the specification,
> which can be found here:
>    https://github.com/riscv/riscv-crypto/releases/tag/v20221220
> 
> Note, that this specification is not frozen yet, meaning that
> incompatible changes are possible.
> Therefore, this patchset is marked as RFC and should not be considered
> for upstream inclusion.
> 
> All extensions come with (passing) tests for the feature test macros.
> 
> A Binutils patch series for vector crypto support can be found here:
>    https://sourceware.org/pipermail/binutils/2022-December/125272.html
> 
> Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
> ---
>   gcc/common/config/riscv/riscv-common.cc | 16 ++++++++++++++++
>   gcc/config/riscv/riscv-opts.h           | 16 ++++++++++++++++
>   gcc/config/riscv/riscv.opt              |  3 +++
>   gcc/testsuite/gcc.target/riscv/zvkb.c   | 13 +++++++++++++
>   gcc/testsuite/gcc.target/riscv/zvkg.c   | 13 +++++++++++++
>   gcc/testsuite/gcc.target/riscv/zvkha.c  | 13 +++++++++++++
>   gcc/testsuite/gcc.target/riscv/zvkhb.c  | 13 +++++++++++++
>   gcc/testsuite/gcc.target/riscv/zvkn.c   | 13 +++++++++++++
>   gcc/testsuite/gcc.target/riscv/zvksed.c | 13 +++++++++++++
>   gcc/testsuite/gcc.target/riscv/zvksh.c  | 13 +++++++++++++
>   10 files changed, 126 insertions(+)
>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkb.c
>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkg.c
>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkha.c
>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkhb.c
>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkn.c
>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvksed.c
>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvksh.c
I don't see anything objectionable in here.  I'd guess that most (but 
perhaps not all) of these will wire up as builtins at some point in the 
not too distant future.


Jeff
  
Palmer Dabbelt Dec. 27, 2022, 6:58 p.m. UTC | #2
On Tue, 27 Dec 2022 09:35:55 PST (-0800), gcc-patches@gcc.gnu.org wrote:
>
>
> On 12/21/22 11:31, Christoph Muellner wrote:
>> From: Christoph Müllner <christoph.muellner@vrull.eu>
>>
>> This series adds basic support for the vector crypto extensions:
>> * Zvkb
>> * Zvkg
>> * Zvkh[a,b]
>> * Zvkn
>> * Zvksed
>> * Zvksh
>>
>> The implementation follows the version 20221220 of the specification,
>> which can be found here:
>>    https://github.com/riscv/riscv-crypto/releases/tag/v20221220
>>
>> Note, that this specification is not frozen yet, meaning that
>> incompatible changes are possible.
>> Therefore, this patchset is marked as RFC and should not be considered
>> for upstream inclusion.
>>
>> All extensions come with (passing) tests for the feature test macros.
>>
>> A Binutils patch series for vector crypto support can be found here:
>>    https://sourceware.org/pipermail/binutils/2022-December/125272.html
>>
>> Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
>> ---
>>   gcc/common/config/riscv/riscv-common.cc | 16 ++++++++++++++++
>>   gcc/config/riscv/riscv-opts.h           | 16 ++++++++++++++++
>>   gcc/config/riscv/riscv.opt              |  3 +++
>>   gcc/testsuite/gcc.target/riscv/zvkb.c   | 13 +++++++++++++
>>   gcc/testsuite/gcc.target/riscv/zvkg.c   | 13 +++++++++++++
>>   gcc/testsuite/gcc.target/riscv/zvkha.c  | 13 +++++++++++++
>>   gcc/testsuite/gcc.target/riscv/zvkhb.c  | 13 +++++++++++++
>>   gcc/testsuite/gcc.target/riscv/zvkn.c   | 13 +++++++++++++
>>   gcc/testsuite/gcc.target/riscv/zvksed.c | 13 +++++++++++++
>>   gcc/testsuite/gcc.target/riscv/zvksh.c  | 13 +++++++++++++
>>   10 files changed, 126 insertions(+)
>>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkb.c
>>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkg.c
>>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkha.c
>>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkhb.c
>>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkn.c
>>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvksed.c
>>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvksh.c
> I don't see anything objectionable in here.  I'd guess that most (but
> perhaps not all) of these will wire up as builtins at some point in the
> not too distant future.

These allow things like `-march=rv64gc_zvksh`, it's not really clear 
what the indented behavior is there -- specifically, does that 
implicitly enable some base vector extension?

I've just skimmed the ISA manual here, but all I can find is a bit 
ambiguous

    With the exception of Zvknhb, each of these Vector Crypto Extensions 
    can be build on any base Vector Extension, embedded (Zve*) or 
    application ("V"). Zvknhb requires ELEN=64 and therefore cannot be 
    implemented on a Zve32* base.

I doubt it really matters which way we pick, but it is something we're 
going to need to keep consistent moving forwards as otherwise users 
might get some surprising behavior.  This has come up a bunch of times, 
but there's slightly different wording each time in the specs and I'm 
never really sure what to read of it.

I don't think that alone would be enough to delay this for gcc-14, but 
as far as I can tell binutils is branching very soon for a target 
release in the middle of January.  I'm guessing these extensions will 
not be frozen by then, which would be a blocker.

I'm not sure if anyone has a pressing need for these?  If not, I think 
it's best to delay them until binutils-2.41 (and presumably then 
gcc-14).
  
Philipp Tomsich Dec. 27, 2022, 7:47 p.m. UTC | #3
On Tue, 27 Dec 2022 at 19:58, Palmer Dabbelt <palmer@dabbelt.com> wrote:
>
> On Tue, 27 Dec 2022 09:35:55 PST (-0800), gcc-patches@gcc.gnu.org wrote:
> >
> >
> > On 12/21/22 11:31, Christoph Muellner wrote:
> >> From: Christoph Müllner <christoph.muellner@vrull.eu>
> >>
> >> This series adds basic support for the vector crypto extensions:
> >> * Zvkb
> >> * Zvkg
> >> * Zvkh[a,b]
> >> * Zvkn
> >> * Zvksed
> >> * Zvksh
> >>
> >> The implementation follows the version 20221220 of the specification,
> >> which can be found here:
> >>    https://github.com/riscv/riscv-crypto/releases/tag/v20221220
> >>
> >> Note, that this specification is not frozen yet, meaning that
> >> incompatible changes are possible.
> >> Therefore, this patchset is marked as RFC and should not be considered
> >> for upstream inclusion.
> >>
> >> All extensions come with (passing) tests for the feature test macros.
> >>
> >> A Binutils patch series for vector crypto support can be found here:
> >>    https://sourceware.org/pipermail/binutils/2022-December/125272.html
> >>
> >> Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
> >> ---
> >>   gcc/common/config/riscv/riscv-common.cc | 16 ++++++++++++++++
> >>   gcc/config/riscv/riscv-opts.h           | 16 ++++++++++++++++
> >>   gcc/config/riscv/riscv.opt              |  3 +++
> >>   gcc/testsuite/gcc.target/riscv/zvkb.c   | 13 +++++++++++++
> >>   gcc/testsuite/gcc.target/riscv/zvkg.c   | 13 +++++++++++++
> >>   gcc/testsuite/gcc.target/riscv/zvkha.c  | 13 +++++++++++++
> >>   gcc/testsuite/gcc.target/riscv/zvkhb.c  | 13 +++++++++++++
> >>   gcc/testsuite/gcc.target/riscv/zvkn.c   | 13 +++++++++++++
> >>   gcc/testsuite/gcc.target/riscv/zvksed.c | 13 +++++++++++++
> >>   gcc/testsuite/gcc.target/riscv/zvksh.c  | 13 +++++++++++++
> >>   10 files changed, 126 insertions(+)
> >>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkb.c
> >>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkg.c
> >>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkha.c
> >>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkhb.c
> >>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvkn.c
> >>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvksed.c
> >>   create mode 100644 gcc/testsuite/gcc.target/riscv/zvksh.c
> > I don't see anything objectionable in here.  I'd guess that most (but
> > perhaps not all) of these will wire up as builtins at some point in the
> > not too distant future.
>
> These allow things like `-march=rv64gc_zvksh`, it's not really clear
> what the indented behavior is there -- specifically, does that
> implicitly enable some base vector extension?
>
> I've just skimmed the ISA manual here, but all I can find is a bit
> ambiguous
>
>     With the exception of Zvknhb, each of these Vector Crypto Extensions
>     can be build on any base Vector Extension, embedded (Zve*) or
>     application ("V"). Zvknhb requires ELEN=64 and therefore cannot be
>     implemented on a Zve32* base.
>
> I doubt it really matters which way we pick, but it is something we're
> going to need to keep consistent moving forwards as otherwise users
> might get some surprising behavior.  This has come up a bunch of times,
> but there's slightly different wording each time in the specs and I'm
> never really sure what to read of it.
>
> I don't think that alone would be enough to delay this for gcc-14, but
> as far as I can tell binutils is branching very soon for a target
> release in the middle of January.  I'm guessing these extensions will
> not be frozen by then, which would be a blocker.
>
> I'm not sure if anyone has a pressing need for these?  If not, I think
> it's best to delay them until binutils-2.41 (and presumably then
> gcc-14).

Given that the encodings last changed on Dec 21st, I would also prefer
if we could off until after the binutils-2.40 has been released.

Philipp.
  

Patch

diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc
index 4b7f777c103..dfd654eea24 100644
--- a/gcc/common/config/riscv/riscv-common.cc
+++ b/gcc/common/config/riscv/riscv-common.cc
@@ -201,6 +201,14 @@  static const struct riscv_ext_version riscv_ext_version_table[] =
   {"zve64f", ISA_SPEC_CLASS_NONE, 1, 0},
   {"zve64d", ISA_SPEC_CLASS_NONE, 1, 0},
 
+  {"zvkb", ISA_SPEC_CLASS_NONE, 1, 0},
+  {"zvkg", ISA_SPEC_CLASS_NONE, 1, 0},
+  {"zvkha", ISA_SPEC_CLASS_NONE, 1, 0},
+  {"zvkhb", ISA_SPEC_CLASS_NONE, 1, 0},
+  {"zvkn", ISA_SPEC_CLASS_NONE, 1, 0},
+  {"zvksed", ISA_SPEC_CLASS_NONE, 1, 0},
+  {"zvksh", ISA_SPEC_CLASS_NONE, 1, 0},
+
   {"zvl32b", ISA_SPEC_CLASS_NONE, 1, 0},
   {"zvl64b", ISA_SPEC_CLASS_NONE, 1, 0},
   {"zvl128b", ISA_SPEC_CLASS_NONE, 1, 0},
@@ -1226,6 +1234,14 @@  static const riscv_ext_flag_table_t riscv_ext_flag_table[] =
   {"zve64f",   &gcc_options::x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_32},
   {"zve64d",   &gcc_options::x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_64},
 
+  {"zvkb",     &gcc_options::x_riscv_zvk_subext, MASK_ZVKB},
+  {"zvkg",     &gcc_options::x_riscv_zvk_subext, MASK_ZVKG},
+  {"zvkha",    &gcc_options::x_riscv_zvk_subext, MASK_ZVKHA},
+  {"zvkhb",    &gcc_options::x_riscv_zvk_subext, MASK_ZVKHB},
+  {"zvkn",     &gcc_options::x_riscv_zvk_subext, MASK_ZVKN},
+  {"zvksed",   &gcc_options::x_riscv_zvk_subext, MASK_ZVKSED},
+  {"zvksh",    &gcc_options::x_riscv_zvk_subext, MASK_ZVKSH},
+
   {"zvl32b",    &gcc_options::x_riscv_zvl_flags, MASK_ZVL32B},
   {"zvl64b",    &gcc_options::x_riscv_zvl_flags, MASK_ZVL64B},
   {"zvl128b",   &gcc_options::x_riscv_zvl_flags, MASK_ZVL128B},
diff --git a/gcc/config/riscv/riscv-opts.h b/gcc/config/riscv/riscv-opts.h
index 25fd85b09b1..5b367bd194c 100644
--- a/gcc/config/riscv/riscv-opts.h
+++ b/gcc/config/riscv/riscv-opts.h
@@ -132,6 +132,22 @@  enum stack_protector_guard {
 #define TARGET_VECTOR_ELEN_FP_64 \
   ((riscv_vector_elen_flags & MASK_VECTOR_ELEN_FP_64) != 0)
 
+#define MASK_ZVKB      (1 << 0)
+#define MASK_ZVKG      (1 << 1)
+#define MASK_ZVKHA     (1 << 2)
+#define MASK_ZVKHB     (1 << 3)
+#define MASK_ZVKN      (1 << 4)
+#define MASK_ZVKSED    (1 << 5)
+#define MASK_ZVKSH     (1 << 6)
+
+#define TARGET_ZVKB    ((riscv_zvk_subext & MASK_ZVKB) != 0)
+#define TARGET_ZVKG    ((riscv_zvk_subext & MASK_ZVKG) != 0)
+#define TARGET_ZVKHA   ((riscv_zvk_subext & MASK_ZVKHA) != 0)
+#define TARGET_ZVKHB   ((riscv_zvk_subext & MASK_ZVKHB) != 0)
+#define TARGET_ZVKN    ((riscv_zvk_subext & MASK_ZVKN) != 0)
+#define TARGET_ZVKSED  ((riscv_zvk_subext & MASK_ZVKSED) != 0)
+#define TARGET_ZVKSH   ((riscv_zvk_subext & MASK_ZVKSH) != 0)
+
 #define MASK_ZVL32B    (1 <<  0)
 #define MASK_ZVL64B    (1 <<  1)
 #define MASK_ZVL128B   (1 <<  2)
diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt
index 7c3ca48d1cc..ea24a80d734 100644
--- a/gcc/config/riscv/riscv.opt
+++ b/gcc/config/riscv/riscv.opt
@@ -218,6 +218,9 @@  int riscv_zk_subext
 TargetVariable
 int riscv_vector_elen_flags
 
+TargetVariable
+int riscv_zvk_subext
+
 TargetVariable
 int riscv_zvl_flags
 
diff --git a/gcc/testsuite/gcc.target/riscv/zvkb.c b/gcc/testsuite/gcc.target/riscv/zvkb.c
new file mode 100644
index 00000000000..7116e98921c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zvkb.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zvkb" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zvkb" { target { rv32 } } } */
+
+#ifndef __riscv_zvkb
+#error Feature macro not defined
+#endif
+
+int
+foo (int a)
+{
+  return a;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zvkg.c b/gcc/testsuite/gcc.target/riscv/zvkg.c
new file mode 100644
index 00000000000..1e2a05aa1d8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zvkg.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zvkg" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zvkg" { target { rv32 } } } */
+
+#ifndef __riscv_zvkg
+#error Feature macro not defined
+#endif
+
+int
+foo (int a)
+{
+  return a;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zvkha.c b/gcc/testsuite/gcc.target/riscv/zvkha.c
new file mode 100644
index 00000000000..ca4e8d4c561
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zvkha.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zvkha" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zvkha" { target { rv32 } } } */
+
+#ifndef __riscv_zvkha
+#error Feature macro not defined
+#endif
+
+int
+foo (int a)
+{
+  return a;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zvkhb.c b/gcc/testsuite/gcc.target/riscv/zvkhb.c
new file mode 100644
index 00000000000..8fa25ff52ee
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zvkhb.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zvkhb" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zvkhb" { target { rv32 } } } */
+
+#ifndef __riscv_zvkhb
+#error Feature macro not defined
+#endif
+
+int
+foo (int a)
+{
+  return a;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zvkn.c b/gcc/testsuite/gcc.target/riscv/zvkn.c
new file mode 100644
index 00000000000..3647936d9e9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zvkn.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zvkn" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zvkn" { target { rv32 } } } */
+
+#ifndef __riscv_zvkn
+#error Feature macro not defined
+#endif
+
+int
+foo (int a)
+{
+  return a;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zvksed.c b/gcc/testsuite/gcc.target/riscv/zvksed.c
new file mode 100644
index 00000000000..439b546bb8a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zvksed.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zvksed" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zvksed" { target { rv32 } } } */
+
+#ifndef __riscv_zvksed
+#error Feature macro not defined
+#endif
+
+int
+foo (int a)
+{
+  return a;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/zvksh.c b/gcc/testsuite/gcc.target/riscv/zvksh.c
new file mode 100644
index 00000000000..5359ca50281
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zvksh.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zvksh" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zvksh" { target { rv32 } } } */
+
+#ifndef __riscv_zvksh
+#error Feature macro not defined
+#endif
+
+int
+foo (int a)
+{
+  return a;
+}