RISC-V: Compute default ABI from -mcpu or -march

Message ID 20220607025120.1601-1-pc.wang@linux.alibaba.com
State Deferred, archived
Headers
Series RISC-V: Compute default ABI from -mcpu or -march |

Commit Message

pc.wang June 7, 2022, 2:51 a.m. UTC
  If -mcpu or -march is specified and there is no -mabi, we will calculate
default ABI from arch string provided by -march or defined in CPU info.

gcc/ChangeLog:

        * common/config/riscv/riscv-common.cc (compute_default_abi): Implementation
        to calculate -mabi from arch string.
        (riscv_expand_abi_from_arch): New spec function to calcalute -mabi from arch
        string provided by -march option.
        (riscv_expand_abi_from_cpu): New spec function to find CPU info and calculate
        -mabi from arch string defined in CPU info.
        * config/riscv/riscv.h (EXTRA_SPEC_FUNCTIONS): Add above spec functions.
        (OPTION_DEFAULT_SPECS): Use new spec functions to calculate -mabi and -march
        has higher priority than -mcpu.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/mabi-1.c: ilp32e test.
        * gcc.target/riscv/mabi-2.c: ilp32 test.
        * gcc.target/riscv/mabi-3.c: ilp32f test.
        * gcc.target/riscv/mabi-4.c: ilp32d test.
        * gcc.target/riscv/mabi-5.c: lp64 test.
        * gcc.target/riscv/mabi-6.c: lp64f test.
        * gcc.target/riscv/mabi-7.c: lp64d test.
        * gcc.target/riscv/mabi-8.c: -march override -mcpu.
---
 gcc/common/config/riscv/riscv-common.cc | 66 +++++++++++++++++++++++++
 gcc/config/riscv/riscv.h                | 15 ++++--
 gcc/testsuite/gcc.target/riscv/mabi-1.c |  7 +++
 gcc/testsuite/gcc.target/riscv/mabi-2.c |  7 +++
 gcc/testsuite/gcc.target/riscv/mabi-3.c |  7 +++
 gcc/testsuite/gcc.target/riscv/mabi-4.c |  7 +++
 gcc/testsuite/gcc.target/riscv/mabi-5.c |  7 +++
 gcc/testsuite/gcc.target/riscv/mabi-6.c |  7 +++
 gcc/testsuite/gcc.target/riscv/mabi-7.c |  7 +++
 gcc/testsuite/gcc.target/riscv/mabi-8.c |  7 +++
 10 files changed, 134 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-2.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-3.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-4.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-5.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-6.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-7.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-8.c
  

Comments

Palmer Dabbelt June 7, 2022, 6:45 p.m. UTC | #1
On Mon, 06 Jun 2022 19:51:20 PDT (-0700), gcc-patches@gcc.gnu.org wrote:
> If -mcpu or -march is specified and there is no -mabi, we will calculate
> default ABI from arch string provided by -march or defined in CPU info.

IMO this is generally a good idea and we've talked about it before, but 
just setting the ABI from the ISA isn't quite the right way to go.  IIRC 
we came up with something slightly more complicated, like picking the 
closest supported multilib.  That's probably more in line with what 
users are asking for, which IIUC is sort of just "I don't care that much 
about ABI, just make my stuff build".

Whatever we do here, we should document in the RISC-V specs as we'll 
want to make sure LLVM does the same thing.  We probably also want some 
sort of "-mabi=auto" argument, as it's always best to have an argument 
that changes back to the no-argument behavior.


>
> gcc/ChangeLog:
>
>         * common/config/riscv/riscv-common.cc (compute_default_abi): Implementation
>         to calculate -mabi from arch string.
>         (riscv_expand_abi_from_arch): New spec function to calcalute -mabi from arch
>         string provided by -march option.
>         (riscv_expand_abi_from_cpu): New spec function to find CPU info and calculate
>         -mabi from arch string defined in CPU info.
>         * config/riscv/riscv.h (EXTRA_SPEC_FUNCTIONS): Add above spec functions.
>         (OPTION_DEFAULT_SPECS): Use new spec functions to calculate -mabi and -march
>         has higher priority than -mcpu.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/riscv/mabi-1.c: ilp32e test.
>         * gcc.target/riscv/mabi-2.c: ilp32 test.
>         * gcc.target/riscv/mabi-3.c: ilp32f test.
>         * gcc.target/riscv/mabi-4.c: ilp32d test.
>         * gcc.target/riscv/mabi-5.c: lp64 test.
>         * gcc.target/riscv/mabi-6.c: lp64f test.
>         * gcc.target/riscv/mabi-7.c: lp64d test.
>         * gcc.target/riscv/mabi-8.c: -march override -mcpu.
> ---
>  gcc/common/config/riscv/riscv-common.cc | 66 +++++++++++++++++++++++++
>  gcc/config/riscv/riscv.h                | 15 ++++--
>  gcc/testsuite/gcc.target/riscv/mabi-1.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-2.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-3.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-4.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-5.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-6.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-7.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-8.c |  7 +++
>  10 files changed, 134 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-1.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-2.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-3.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-4.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-5.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-6.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-7.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-8.c
>
> diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc
> index 0e5be2ce105..f8e40549d18 100644
> --- a/gcc/common/config/riscv/riscv-common.cc
> +++ b/gcc/common/config/riscv/riscv-common.cc
> @@ -1266,6 +1266,72 @@ riscv_default_mtune (int argc, const char **argv)
>      return default_mtune;
>  }
>
> +/* Compute default -mabi option from arch string.  */
> +
> +static const char *
> +compute_default_abi (const char* arch_str)
> +{
> +  location_t loc = UNKNOWN_LOCATION;
> +
> +  riscv_parse_arch_string (arch_str, NULL, loc);
> +
> +  if (current_subset_list->xlen () == 64)
> +    {
> +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> +					 RISCV_DONT_CARE_VERSION))
> +	return "lp64d";
> +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> +					 RISCV_DONT_CARE_VERSION))
> +	return "lp64f";
> +      return "lp64";
> +    }
> +  else
> +    {
> +      if (current_subset_list->lookup ("e", RISCV_DONT_CARE_VERSION,
> +					 RISCV_DONT_CARE_VERSION))
> +	return "ilp32e";
> +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> +					 RISCV_DONT_CARE_VERSION))
> +	return "ilp32d";
> +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> +					 RISCV_DONT_CARE_VERSION))
> +	return "ilp32f";
> +      return "ilp32";
> +    }
> +}
> +
> +/* Expand default -mabi option from -march option.  */
> +
> +const char *
> +riscv_expand_abi_from_arch (int argc, const char **argv)
> +{
> +  gcc_assert (argc == 1);
> +  return xasprintf ("-mabi=%s", compute_default_abi (argv[0]));
> +}
> +
> +/* Expand default -mabi option from -mcpu option.  */
> +
> +const char *
> +riscv_expand_abi_from_cpu (int argc, const char **argv)
> +{
> +  gcc_assert (argc > 0 && argc <= 2);
> +  const char *default_abi_str = NULL;
> +  if (argc >= 2)
> +    default_abi_str = argv[1];
> +
> +  const riscv_cpu_info *cpu = riscv_find_cpu (argv[0]);
> +
> +  if (cpu == NULL)
> +    {
> +      if (default_abi_str == NULL)
> +	return "";
> +      else
> +	return xasprintf ("-mabi=%s", default_abi_str);
> +    }
> +  else
> +    return xasprintf ("-mabi=%s", compute_default_abi (cpu->arch));
> +}
> +
>  /* Expand arch string with implied extensions from -mcpu option.  */
>
>  const char *
> diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
> index 6f7f4d3fbdc..14cd695f37f 100644
> --- a/gcc/config/riscv/riscv.h
> +++ b/gcc/config/riscv/riscv.h
> @@ -50,15 +50,20 @@ along with GCC; see the file COPYING3.  If not see
>  extern const char *riscv_expand_arch (int argc, const char **argv);
>  extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv);
>  extern const char *riscv_default_mtune (int argc, const char **argv);
> +extern const char *riscv_expand_abi_from_arch (int argc, const char **argv);
> +extern const char *riscv_expand_abi_from_cpu (int argc, const char **argv);
>
>  # define EXTRA_SPEC_FUNCTIONS						\
>    { "riscv_expand_arch", riscv_expand_arch },				\
>    { "riscv_expand_arch_from_cpu", riscv_expand_arch_from_cpu },		\
> -  { "riscv_default_mtune", riscv_default_mtune },
> +  { "riscv_default_mtune", riscv_default_mtune },			\
> +  { "riscv_expand_abi_from_arch", riscv_expand_abi_from_arch },		\
> +  { "riscv_expand_abi_from_cpu", riscv_expand_abi_from_cpu },
>
>  /* Support for a compile-time default CPU, et cetera.  The rules are:
>     --with-arch is ignored if -march or -mcpu is specified.
> -   --with-abi is ignored if -mabi is specified.
> +   --with-abi is ignored if -mabi is specified.  If -mcpu or -march is
> +     specified, suitable -mabi will be selected from arch string.
>     --with-tune is ignored if -mtune or -mcpu is specified.
>     --with-isa-spec is ignored if -misa-spec is specified.
>
> @@ -70,7 +75,11 @@ extern const char *riscv_default_mtune (int argc, const char **argv);
>    {"arch", "%{!march=*:"						\
>  	   "  %{!mcpu=*:-march=%(VALUE)}"				\
>  	   "  %{mcpu=*:%:riscv_expand_arch_from_cpu(%* %(VALUE))}}" },	\
> -  {"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \
> +  {"abi", "%{!mabi=*:"							\
> +	   "  %{!march=*:"						\
> +	   "    %{!mcpu=*:-mabi=%(VALUE)}"				\
> +	   "    %{mcpu=*:%:riscv_expand_abi_from_cpu(%* %(VALUE))}}"	\
> +	   "  %{march=*:%:riscv_expand_abi_from_arch(%*)}}" },		\
>    {"isa_spec", "%{!misa-spec=*:-misa-spec=%(VALUE)}" }, \
>
>  #ifdef IN_LIBGCC2
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-1.c b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> new file mode 100644
> index 00000000000..83bc343f8d8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32e" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_abi_rve))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-2.c b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> new file mode 100644
> index 00000000000..6a97c2ae2a3
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32ima" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_soft))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-3.c b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> new file mode 100644
> index 00000000000..cef04313e19
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32imaf" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_single))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-4.c b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> new file mode 100644
> index 00000000000..d62dcf6ed75
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32imafd" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-5.c b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> new file mode 100644
> index 00000000000..000e5e282c2
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64ima" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_soft))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-6.c b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> new file mode 100644
> index 00000000000..ed1458d4158
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64imaf" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_single))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-7.c b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> new file mode 100644
> index 00000000000..675fe359cfd
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64imafd" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-8.c b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> new file mode 100644
> index 00000000000..8c7e08e4289
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-mcpu=sifive-s21 -march=rv64imafd" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
  
pc.wang June 8, 2022, 9:05 a.m. UTC | #2
Thanks for your opinion! I did these just because LLVM has already done the same thing and I wanted to make GCC with the same behavior of LLVM. The only difference is that LLVM has no handling for ilp32f and lp64f and I have sent a patch to do it (sees https://reviews.llvm.org/D125947).
As for RISC-V specs, there are some descriptions in https://github.com/riscv-non-isa/riscv-toolchain-conventions#specifying-the-target-isa-with--march: 
> A target -march which includes floating point instructions implies a hardfloat calling convention, but can be overridden using the -mabi flag (see the next section).
But I think we can make it clearer.
------------------------------------------------------------------
Sender:Palmer Dabbelt <palmer@dabbelt.com>
Sent At:2022 Jun. 8 (Wed.) 02:45
Recipient:gcc-patches <gcc-patches@gcc.gnu.org>
Cc:pc.wang <pc.wang@linux.alibaba.com>
Subject:Re: [PATCH] RISC-V: Compute default ABI from -mcpu or -march

On Mon, 06 Jun 2022 19:51:20 PDT (-0700), gcc-patches@gcc.gnu.org wrote:
> If -mcpu or -march is specified and there is no -mabi, we will calculate
> default ABI from arch string provided by -march or defined in CPU info.

IMO this is generally a good idea and we've talked about it before, but 
just setting the ABI from the ISA isn't quite the right way to go.  IIRC 
we came up with something slightly more complicated, like picking the 
closest supported multilib.  That's probably more in line with what 
users are asking for, which IIUC is sort of just "I don't care that much 
about ABI, just make my stuff build".

Whatever we do here, we should document in the RISC-V specs as we'll 
want to make sure LLVM does the same thing.  We probably also want some 
sort of "-mabi=auto" argument, as it's always best to have an argument 
that changes back to the no-argument behavior.


>
> gcc/ChangeLog:
>
>         * common/config/riscv/riscv-common.cc (compute_default_abi): Implementation
>         to calculate -mabi from arch string.
>         (riscv_expand_abi_from_arch): New spec function to calcalute -mabi from arch
>         string provided by -march option.
>         (riscv_expand_abi_from_cpu): New spec function to find CPU info and calculate
>         -mabi from arch string defined in CPU info.
>         * config/riscv/riscv.h (EXTRA_SPEC_FUNCTIONS): Add above spec functions.
>         (OPTION_DEFAULT_SPECS): Use new spec functions to calculate -mabi and -march
>         has higher priority than -mcpu.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/riscv/mabi-1.c: ilp32e test.
>         * gcc.target/riscv/mabi-2.c: ilp32 test.
>         * gcc.target/riscv/mabi-3.c: ilp32f test.
>         * gcc.target/riscv/mabi-4.c: ilp32d test.
>         * gcc.target/riscv/mabi-5.c: lp64 test.
>         * gcc.target/riscv/mabi-6.c: lp64f test.
>         * gcc.target/riscv/mabi-7.c: lp64d test.
>         * gcc.target/riscv/mabi-8.c: -march override -mcpu.
> ---
>  gcc/common/config/riscv/riscv-common.cc | 66 +++++++++++++++++++++++++
>  gcc/config/riscv/riscv.h                | 15 ++++--
>  gcc/testsuite/gcc.target/riscv/mabi-1.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-2.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-3.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-4.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-5.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-6.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-7.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-8.c |  7 +++
>  10 files changed, 134 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-1.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-2.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-3.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-4.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-5.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-6.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-7.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-8.c
>
> diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc
> index 0e5be2ce105..f8e40549d18 100644
> --- a/gcc/common/config/riscv/riscv-common.cc
> +++ b/gcc/common/config/riscv/riscv-common.cc
> @@ -1266,6 +1266,72 @@ riscv_default_mtune (int argc, const char **argv)
>      return default_mtune;
>  }
>
> +/* Compute default -mabi option from arch string.  */
> +
> +static const char *
> +compute_default_abi (const char* arch_str)
> +{
> +  location_t loc = UNKNOWN_LOCATION;
> +
> +  riscv_parse_arch_string (arch_str, NULL, loc);
> +
> +  if (current_subset_list->xlen () == 64)
> +    {
> +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> +      RISCV_DONT_CARE_VERSION))
> + return "lp64d";
> +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> +      RISCV_DONT_CARE_VERSION))
> + return "lp64f";
> +      return "lp64";
> +    }
> +  else
> +    {
> +      if (current_subset_list->lookup ("e", RISCV_DONT_CARE_VERSION,
> +      RISCV_DONT_CARE_VERSION))
> + return "ilp32e";
> +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> +      RISCV_DONT_CARE_VERSION))
> + return "ilp32d";
> +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> +      RISCV_DONT_CARE_VERSION))
> + return "ilp32f";
> +      return "ilp32";
> +    }
> +}
> +
> +/* Expand default -mabi option from -march option.  */
> +
> +const char *
> +riscv_expand_abi_from_arch (int argc, const char **argv)
> +{
> +  gcc_assert (argc == 1);
> +  return xasprintf ("-mabi=%s", compute_default_abi (argv[0]));
> +}
> +
> +/* Expand default -mabi option from -mcpu option.  */
> +
> +const char *
> +riscv_expand_abi_from_cpu (int argc, const char **argv)
> +{
> +  gcc_assert (argc > 0 && argc <= 2);
> +  const char *default_abi_str = NULL;
> +  if (argc >= 2)
> +    default_abi_str = argv[1];
> +
> +  const riscv_cpu_info *cpu = riscv_find_cpu (argv[0]);
> +
> +  if (cpu == NULL)
> +    {
> +      if (default_abi_str == NULL)
> + return "";
> +      else
> + return xasprintf ("-mabi=%s", default_abi_str);
> +    }
> +  else
> +    return xasprintf ("-mabi=%s", compute_default_abi (cpu->arch));
> +}
> +
>  /* Expand arch string with implied extensions from -mcpu option.  */
>
>  const char *
> diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
> index 6f7f4d3fbdc..14cd695f37f 100644
> --- a/gcc/config/riscv/riscv.h
> +++ b/gcc/config/riscv/riscv.h
> @@ -50,15 +50,20 @@ along with GCC; see the file COPYING3.  If not see
>  extern const char *riscv_expand_arch (int argc, const char **argv);
>  extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv);
>  extern const char *riscv_default_mtune (int argc, const char **argv);
> +extern const char *riscv_expand_abi_from_arch (int argc, const char **argv);
> +extern const char *riscv_expand_abi_from_cpu (int argc, const char **argv);
>
>  # define EXTRA_SPEC_FUNCTIONS      \
>    { "riscv_expand_arch", riscv_expand_arch },    \
>    { "riscv_expand_arch_from_cpu", riscv_expand_arch_from_cpu },  \
> -  { "riscv_default_mtune", riscv_default_mtune },
> +  { "riscv_default_mtune", riscv_default_mtune },   \
> +  { "riscv_expand_abi_from_arch", riscv_expand_abi_from_arch },  \
> +  { "riscv_expand_abi_from_cpu", riscv_expand_abi_from_cpu },
>
>  /* Support for a compile-time default CPU, et cetera.  The rules are:
>     --with-arch is ignored if -march or -mcpu is specified.
> -   --with-abi is ignored if -mabi is specified.
> +   --with-abi is ignored if -mabi is specified.  If -mcpu or -march is
> +     specified, suitable -mabi will be selected from arch string.
>     --with-tune is ignored if -mtune or -mcpu is specified.
>     --with-isa-spec is ignored if -misa-spec is specified.
>
> @@ -70,7 +75,11 @@ extern const char *riscv_default_mtune (int argc, const char **argv);
>    {"arch", "%{!march=*:"      \
>      "  %{!mcpu=*:-march=%(VALUE)}"    \
>      "  %{mcpu=*:%:riscv_expand_arch_from_cpu(%* %(VALUE))}}" }, \
> -  {"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \
> +  {"abi", "%{!mabi=*:"       \
> +    "  %{!march=*:"      \
> +    "    %{!mcpu=*:-mabi=%(VALUE)}"    \
> +    "    %{mcpu=*:%:riscv_expand_abi_from_cpu(%* %(VALUE))}}" \
> +    "  %{march=*:%:riscv_expand_abi_from_arch(%*)}}" },  \
>    {"isa_spec", "%{!misa-spec=*:-misa-spec=%(VALUE)}" }, \
>
>  #ifdef IN_LIBGCC2
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-1.c b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> new file mode 100644
> index 00000000000..83bc343f8d8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32e" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_abi_rve))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-2.c b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> new file mode 100644
> index 00000000000..6a97c2ae2a3
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32ima" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_soft))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-3.c b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> new file mode 100644
> index 00000000000..cef04313e19
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32imaf" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_single))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-4.c b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> new file mode 100644
> index 00000000000..d62dcf6ed75
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32imafd" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-5.c b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> new file mode 100644
> index 00000000000..000e5e282c2
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64ima" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_soft))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-6.c b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> new file mode 100644
> index 00000000000..ed1458d4158
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64imaf" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_single))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-7.c b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> new file mode 100644
> index 00000000000..675fe359cfd
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64imafd" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-8.c b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> new file mode 100644
> index 00000000000..8c7e08e4289
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-mcpu=sifive-s21 -march=rv64imafd" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
  
Kito Cheng June 8, 2022, 9:48 a.m. UTC | #3
I also prefer adding a -mabi=auto option rather than change existing behavior.

On Wed, Jun 8, 2022 at 5:06 PM pc.wang via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Thanks for your opinion! I did these just because LLVM has already done the same thing and I wanted to make GCC with the same behavior of LLVM. The only difference is that LLVM has no handling for ilp32f and lp64f and I have sent a patch to do it (sees https://reviews.llvm.org/D125947).
> As for RISC-V specs, there are some descriptions in https://github.com/riscv-non-isa/riscv-toolchain-conventions#specifying-the-target-isa-with--march:
> > A target -march which includes floating point instructions implies a hardfloat calling convention, but can be overridden using the -mabi flag (see the next section).
> But I think we can make it clearer.
> ------------------------------------------------------------------
> Sender:Palmer Dabbelt <palmer@dabbelt.com>
> Sent At:2022 Jun. 8 (Wed.) 02:45
> Recipient:gcc-patches <gcc-patches@gcc.gnu.org>
> Cc:pc.wang <pc.wang@linux.alibaba.com>
> Subject:Re: [PATCH] RISC-V: Compute default ABI from -mcpu or -march
>
> On Mon, 06 Jun 2022 19:51:20 PDT (-0700), gcc-patches@gcc.gnu.org wrote:
> > If -mcpu or -march is specified and there is no -mabi, we will calculate
> > default ABI from arch string provided by -march or defined in CPU info.
>
> IMO this is generally a good idea and we've talked about it before, but
> just setting the ABI from the ISA isn't quite the right way to go.  IIRC
> we came up with something slightly more complicated, like picking the
> closest supported multilib.  That's probably more in line with what
> users are asking for, which IIUC is sort of just "I don't care that much
> about ABI, just make my stuff build".
>
> Whatever we do here, we should document in the RISC-V specs as we'll
> want to make sure LLVM does the same thing.  We probably also want some
> sort of "-mabi=auto" argument, as it's always best to have an argument
> that changes back to the no-argument behavior.
>
>
> >
> > gcc/ChangeLog:
> >
> >         * common/config/riscv/riscv-common.cc (compute_default_abi): Implementation
> >         to calculate -mabi from arch string.
> >         (riscv_expand_abi_from_arch): New spec function to calcalute -mabi from arch
> >         string provided by -march option.
> >         (riscv_expand_abi_from_cpu): New spec function to find CPU info and calculate
> >         -mabi from arch string defined in CPU info.
> >         * config/riscv/riscv.h (EXTRA_SPEC_FUNCTIONS): Add above spec functions.
> >         (OPTION_DEFAULT_SPECS): Use new spec functions to calculate -mabi and -march
> >         has higher priority than -mcpu.
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * gcc.target/riscv/mabi-1.c: ilp32e test.
> >         * gcc.target/riscv/mabi-2.c: ilp32 test.
> >         * gcc.target/riscv/mabi-3.c: ilp32f test.
> >         * gcc.target/riscv/mabi-4.c: ilp32d test.
> >         * gcc.target/riscv/mabi-5.c: lp64 test.
> >         * gcc.target/riscv/mabi-6.c: lp64f test.
> >         * gcc.target/riscv/mabi-7.c: lp64d test.
> >         * gcc.target/riscv/mabi-8.c: -march override -mcpu.
> > ---
> >  gcc/common/config/riscv/riscv-common.cc | 66 +++++++++++++++++++++++++
> >  gcc/config/riscv/riscv.h                | 15 ++++--
> >  gcc/testsuite/gcc.target/riscv/mabi-1.c |  7 +++
> >  gcc/testsuite/gcc.target/riscv/mabi-2.c |  7 +++
> >  gcc/testsuite/gcc.target/riscv/mabi-3.c |  7 +++
> >  gcc/testsuite/gcc.target/riscv/mabi-4.c |  7 +++
> >  gcc/testsuite/gcc.target/riscv/mabi-5.c |  7 +++
> >  gcc/testsuite/gcc.target/riscv/mabi-6.c |  7 +++
> >  gcc/testsuite/gcc.target/riscv/mabi-7.c |  7 +++
> >  gcc/testsuite/gcc.target/riscv/mabi-8.c |  7 +++
> >  10 files changed, 134 insertions(+), 3 deletions(-)
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-1.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-2.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-3.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-4.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-5.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-6.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-7.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-8.c
> >
> > diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc
> > index 0e5be2ce105..f8e40549d18 100644
> > --- a/gcc/common/config/riscv/riscv-common.cc
> > +++ b/gcc/common/config/riscv/riscv-common.cc
> > @@ -1266,6 +1266,72 @@ riscv_default_mtune (int argc, const char **argv)
> >      return default_mtune;
> >  }
> >
> > +/* Compute default -mabi option from arch string.  */
> > +
> > +static const char *
> > +compute_default_abi (const char* arch_str)
> > +{
> > +  location_t loc = UNKNOWN_LOCATION;
> > +
> > +  riscv_parse_arch_string (arch_str, NULL, loc);
> > +
> > +  if (current_subset_list->xlen () == 64)
> > +    {
> > +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> > +      RISCV_DONT_CARE_VERSION))
> > + return "lp64d";
> > +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> > +      RISCV_DONT_CARE_VERSION))
> > + return "lp64f";
> > +      return "lp64";
> > +    }
> > +  else
> > +    {
> > +      if (current_subset_list->lookup ("e", RISCV_DONT_CARE_VERSION,
> > +      RISCV_DONT_CARE_VERSION))
> > + return "ilp32e";
> > +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> > +      RISCV_DONT_CARE_VERSION))
> > + return "ilp32d";
> > +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> > +      RISCV_DONT_CARE_VERSION))
> > + return "ilp32f";
> > +      return "ilp32";
> > +    }
> > +}
> > +
> > +/* Expand default -mabi option from -march option.  */
> > +
> > +const char *
> > +riscv_expand_abi_from_arch (int argc, const char **argv)
> > +{
> > +  gcc_assert (argc == 1);
> > +  return xasprintf ("-mabi=%s", compute_default_abi (argv[0]));
> > +}
> > +
> > +/* Expand default -mabi option from -mcpu option.  */
> > +
> > +const char *
> > +riscv_expand_abi_from_cpu (int argc, const char **argv)
> > +{
> > +  gcc_assert (argc > 0 && argc <= 2);
> > +  const char *default_abi_str = NULL;
> > +  if (argc >= 2)
> > +    default_abi_str = argv[1];
> > +
> > +  const riscv_cpu_info *cpu = riscv_find_cpu (argv[0]);
> > +
> > +  if (cpu == NULL)
> > +    {
> > +      if (default_abi_str == NULL)
> > + return "";
> > +      else
> > + return xasprintf ("-mabi=%s", default_abi_str);
> > +    }
> > +  else
> > +    return xasprintf ("-mabi=%s", compute_default_abi (cpu->arch));
> > +}
> > +
> >  /* Expand arch string with implied extensions from -mcpu option.  */
> >
> >  const char *
> > diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
> > index 6f7f4d3fbdc..14cd695f37f 100644
> > --- a/gcc/config/riscv/riscv.h
> > +++ b/gcc/config/riscv/riscv.h
> > @@ -50,15 +50,20 @@ along with GCC; see the file COPYING3.  If not see
> >  extern const char *riscv_expand_arch (int argc, const char **argv);
> >  extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv);
> >  extern const char *riscv_default_mtune (int argc, const char **argv);
> > +extern const char *riscv_expand_abi_from_arch (int argc, const char **argv);
> > +extern const char *riscv_expand_abi_from_cpu (int argc, const char **argv);
> >
> >  # define EXTRA_SPEC_FUNCTIONS      \
> >    { "riscv_expand_arch", riscv_expand_arch },    \
> >    { "riscv_expand_arch_from_cpu", riscv_expand_arch_from_cpu },  \
> > -  { "riscv_default_mtune", riscv_default_mtune },
> > +  { "riscv_default_mtune", riscv_default_mtune },   \
> > +  { "riscv_expand_abi_from_arch", riscv_expand_abi_from_arch },  \
> > +  { "riscv_expand_abi_from_cpu", riscv_expand_abi_from_cpu },
> >
> >  /* Support for a compile-time default CPU, et cetera.  The rules are:
> >     --with-arch is ignored if -march or -mcpu is specified.
> > -   --with-abi is ignored if -mabi is specified.
> > +   --with-abi is ignored if -mabi is specified.  If -mcpu or -march is
> > +     specified, suitable -mabi will be selected from arch string.
> >     --with-tune is ignored if -mtune or -mcpu is specified.
> >     --with-isa-spec is ignored if -misa-spec is specified.
> >
> > @@ -70,7 +75,11 @@ extern const char *riscv_default_mtune (int argc, const char **argv);
> >    {"arch", "%{!march=*:"      \
> >      "  %{!mcpu=*:-march=%(VALUE)}"    \
> >      "  %{mcpu=*:%:riscv_expand_arch_from_cpu(%* %(VALUE))}}" }, \
> > -  {"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \
> > +  {"abi", "%{!mabi=*:"       \
> > +    "  %{!march=*:"      \
> > +    "    %{!mcpu=*:-mabi=%(VALUE)}"    \
> > +    "    %{mcpu=*:%:riscv_expand_abi_from_cpu(%* %(VALUE))}}" \
> > +    "  %{march=*:%:riscv_expand_abi_from_arch(%*)}}" },  \
> >    {"isa_spec", "%{!misa-spec=*:-misa-spec=%(VALUE)}" }, \
> >
> >  #ifdef IN_LIBGCC2
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-1.c b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> > new file mode 100644
> > index 00000000000..83bc343f8d8
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-march=rv32e" } */
> > +
> > +#if !((__riscv_xlen == 32) && defined(__riscv_abi_rve))
> > +#error "unexpected abi"
> > +#endif
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-2.c b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> > new file mode 100644
> > index 00000000000..6a97c2ae2a3
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-march=rv32ima" } */
> > +
> > +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_soft))
> > +#error "unexpected abi"
> > +#endif
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-3.c b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> > new file mode 100644
> > index 00000000000..cef04313e19
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-march=rv32imaf" } */
> > +
> > +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_single))
> > +#error "unexpected abi"
> > +#endif
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-4.c b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> > new file mode 100644
> > index 00000000000..d62dcf6ed75
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-march=rv32imafd" } */
> > +
> > +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_double))
> > +#error "unexpected abi"
> > +#endif
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-5.c b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> > new file mode 100644
> > index 00000000000..000e5e282c2
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-march=rv64ima" } */
> > +
> > +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_soft))
> > +#error "unexpected abi"
> > +#endif
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-6.c b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> > new file mode 100644
> > index 00000000000..ed1458d4158
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-march=rv64imaf" } */
> > +
> > +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_single))
> > +#error "unexpected abi"
> > +#endif
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-7.c b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> > new file mode 100644
> > index 00000000000..675fe359cfd
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-march=rv64imafd" } */
> > +
> > +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> > +#error "unexpected abi"
> > +#endif
> > diff --git a/gcc/testsuite/gcc.target/riscv/mabi-8.c b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> > new file mode 100644
> > index 00000000000..8c7e08e4289
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> > @@ -0,0 +1,7 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> > +/* { dg-options "-mcpu=sifive-s21 -march=rv64imafd" } */
> > +
> > +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> > +#error "unexpected abi"
> > +#endif
  
Andrew Pinski June 8, 2022, 10:59 p.m. UTC | #4
On Mon, Jun 6, 2022 at 7:53 PM wangpc via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> If -mcpu or -march is specified and there is no -mabi, we will calculate
> default ABI from arch string provided by -march or defined in CPU info.

This is 100% wrong and goes against what all other targets do. All
other targets have the following:
a base ABI is selected and then add on to it via -march/-mcpu and only
for the vector ABI (PowerPC backend used not even do that and required
you to supply an option for the vector ABI but that might have changed
in recent years).
Basically the ABI should default to something (via either when
configured with --with-abi= or just a default) and reject invalid
combinations. That is if the ABI specified requires FPU and the
arch/cpu does not have FPU reject it (same with 32bit vs 64bit). This
is how other targets work in GCC and really it should be the same for
RISC-V. Why make it any different and make it harder for people to
convert from one target to another.

Also I think the whole number of extensions of RISC-V makes life much
harder, it would be easier if there was level of support like x86_64
and ARMv8/9 have moved to.
It is past time to require these kind of levels really; otherwise you
end up with so much fragmentation that RISC-V becomes the way of MIPS.

Thanks,
Andrew Pinski


>
> gcc/ChangeLog:
>
>         * common/config/riscv/riscv-common.cc (compute_default_abi): Implementation
>         to calculate -mabi from arch string.
>         (riscv_expand_abi_from_arch): New spec function to calcalute -mabi from arch
>         string provided by -march option.
>         (riscv_expand_abi_from_cpu): New spec function to find CPU info and calculate
>         -mabi from arch string defined in CPU info.
>         * config/riscv/riscv.h (EXTRA_SPEC_FUNCTIONS): Add above spec functions.
>         (OPTION_DEFAULT_SPECS): Use new spec functions to calculate -mabi and -march
>         has higher priority than -mcpu.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/riscv/mabi-1.c: ilp32e test.
>         * gcc.target/riscv/mabi-2.c: ilp32 test.
>         * gcc.target/riscv/mabi-3.c: ilp32f test.
>         * gcc.target/riscv/mabi-4.c: ilp32d test.
>         * gcc.target/riscv/mabi-5.c: lp64 test.
>         * gcc.target/riscv/mabi-6.c: lp64f test.
>         * gcc.target/riscv/mabi-7.c: lp64d test.
>         * gcc.target/riscv/mabi-8.c: -march override -mcpu.
> ---
>  gcc/common/config/riscv/riscv-common.cc | 66 +++++++++++++++++++++++++
>  gcc/config/riscv/riscv.h                | 15 ++++--
>  gcc/testsuite/gcc.target/riscv/mabi-1.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-2.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-3.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-4.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-5.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-6.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-7.c |  7 +++
>  gcc/testsuite/gcc.target/riscv/mabi-8.c |  7 +++
>  10 files changed, 134 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-1.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-2.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-3.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-4.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-5.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-6.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-7.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/mabi-8.c
>
> diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc
> index 0e5be2ce105..f8e40549d18 100644
> --- a/gcc/common/config/riscv/riscv-common.cc
> +++ b/gcc/common/config/riscv/riscv-common.cc
> @@ -1266,6 +1266,72 @@ riscv_default_mtune (int argc, const char **argv)
>      return default_mtune;
>  }
>
> +/* Compute default -mabi option from arch string.  */
> +
> +static const char *
> +compute_default_abi (const char* arch_str)
> +{
> +  location_t loc = UNKNOWN_LOCATION;
> +
> +  riscv_parse_arch_string (arch_str, NULL, loc);
> +
> +  if (current_subset_list->xlen () == 64)
> +    {
> +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> +                                        RISCV_DONT_CARE_VERSION))
> +       return "lp64d";
> +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> +                                        RISCV_DONT_CARE_VERSION))
> +       return "lp64f";
> +      return "lp64";
> +    }
> +  else
> +    {
> +      if (current_subset_list->lookup ("e", RISCV_DONT_CARE_VERSION,
> +                                        RISCV_DONT_CARE_VERSION))
> +       return "ilp32e";
> +      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
> +                                        RISCV_DONT_CARE_VERSION))
> +       return "ilp32d";
> +      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
> +                                        RISCV_DONT_CARE_VERSION))
> +       return "ilp32f";
> +      return "ilp32";
> +    }
> +}
> +
> +/* Expand default -mabi option from -march option.  */
> +
> +const char *
> +riscv_expand_abi_from_arch (int argc, const char **argv)
> +{
> +  gcc_assert (argc == 1);
> +  return xasprintf ("-mabi=%s", compute_default_abi (argv[0]));
> +}
> +
> +/* Expand default -mabi option from -mcpu option.  */
> +
> +const char *
> +riscv_expand_abi_from_cpu (int argc, const char **argv)
> +{
> +  gcc_assert (argc > 0 && argc <= 2);
> +  const char *default_abi_str = NULL;
> +  if (argc >= 2)
> +    default_abi_str = argv[1];
> +
> +  const riscv_cpu_info *cpu = riscv_find_cpu (argv[0]);
> +
> +  if (cpu == NULL)
> +    {
> +      if (default_abi_str == NULL)
> +       return "";
> +      else
> +       return xasprintf ("-mabi=%s", default_abi_str);
> +    }
> +  else
> +    return xasprintf ("-mabi=%s", compute_default_abi (cpu->arch));
> +}
> +
>  /* Expand arch string with implied extensions from -mcpu option.  */
>
>  const char *
> diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
> index 6f7f4d3fbdc..14cd695f37f 100644
> --- a/gcc/config/riscv/riscv.h
> +++ b/gcc/config/riscv/riscv.h
> @@ -50,15 +50,20 @@ along with GCC; see the file COPYING3.  If not see
>  extern const char *riscv_expand_arch (int argc, const char **argv);
>  extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv);
>  extern const char *riscv_default_mtune (int argc, const char **argv);
> +extern const char *riscv_expand_abi_from_arch (int argc, const char **argv);
> +extern const char *riscv_expand_abi_from_cpu (int argc, const char **argv);
>
>  # define EXTRA_SPEC_FUNCTIONS                                          \
>    { "riscv_expand_arch", riscv_expand_arch },                          \
>    { "riscv_expand_arch_from_cpu", riscv_expand_arch_from_cpu },                \
> -  { "riscv_default_mtune", riscv_default_mtune },
> +  { "riscv_default_mtune", riscv_default_mtune },                      \
> +  { "riscv_expand_abi_from_arch", riscv_expand_abi_from_arch },                \
> +  { "riscv_expand_abi_from_cpu", riscv_expand_abi_from_cpu },
>
>  /* Support for a compile-time default CPU, et cetera.  The rules are:
>     --with-arch is ignored if -march or -mcpu is specified.
> -   --with-abi is ignored if -mabi is specified.
> +   --with-abi is ignored if -mabi is specified.  If -mcpu or -march is
> +     specified, suitable -mabi will be selected from arch string.
>     --with-tune is ignored if -mtune or -mcpu is specified.
>     --with-isa-spec is ignored if -misa-spec is specified.
>
> @@ -70,7 +75,11 @@ extern const char *riscv_default_mtune (int argc, const char **argv);
>    {"arch", "%{!march=*:"                                               \
>            "  %{!mcpu=*:-march=%(VALUE)}"                               \
>            "  %{mcpu=*:%:riscv_expand_arch_from_cpu(%* %(VALUE))}}" },  \
> -  {"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \
> +  {"abi", "%{!mabi=*:"                                                 \
> +          "  %{!march=*:"                                              \
> +          "    %{!mcpu=*:-mabi=%(VALUE)}"                              \
> +          "    %{mcpu=*:%:riscv_expand_abi_from_cpu(%* %(VALUE))}}"    \
> +          "  %{march=*:%:riscv_expand_abi_from_arch(%*)}}" },          \
>    {"isa_spec", "%{!misa-spec=*:-misa-spec=%(VALUE)}" }, \
>
>  #ifdef IN_LIBGCC2
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-1.c b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> new file mode 100644
> index 00000000000..83bc343f8d8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-1.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32e" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_abi_rve))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-2.c b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> new file mode 100644
> index 00000000000..6a97c2ae2a3
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-2.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32ima" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_soft))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-3.c b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> new file mode 100644
> index 00000000000..cef04313e19
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-3.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32imaf" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_single))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-4.c b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> new file mode 100644
> index 00000000000..d62dcf6ed75
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-4.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv32imafd" } */
> +
> +#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-5.c b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> new file mode 100644
> index 00000000000..000e5e282c2
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-5.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64ima" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_soft))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-6.c b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> new file mode 100644
> index 00000000000..ed1458d4158
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-6.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64imaf" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_single))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-7.c b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> new file mode 100644
> index 00000000000..675fe359cfd
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-7.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-march=rv64imafd" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
> diff --git a/gcc/testsuite/gcc.target/riscv/mabi-8.c b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> new file mode 100644
> index 00000000000..8c7e08e4289
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/mabi-8.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile } */
> +/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
> +/* { dg-options "-mcpu=sifive-s21 -march=rv64imafd" } */
> +
> +#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
> +#error "unexpected abi"
> +#endif
> --
> 2.34.1.windows.1
>
  

Patch

diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc
index 0e5be2ce105..f8e40549d18 100644
--- a/gcc/common/config/riscv/riscv-common.cc
+++ b/gcc/common/config/riscv/riscv-common.cc
@@ -1266,6 +1266,72 @@  riscv_default_mtune (int argc, const char **argv)
     return default_mtune;
 }
 
+/* Compute default -mabi option from arch string.  */
+
+static const char *
+compute_default_abi (const char* arch_str)
+{
+  location_t loc = UNKNOWN_LOCATION;
+
+  riscv_parse_arch_string (arch_str, NULL, loc);
+
+  if (current_subset_list->xlen () == 64)
+    {
+      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
+					 RISCV_DONT_CARE_VERSION))
+	return "lp64d";
+      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
+					 RISCV_DONT_CARE_VERSION))
+	return "lp64f";
+      return "lp64";
+    }
+  else
+    {
+      if (current_subset_list->lookup ("e", RISCV_DONT_CARE_VERSION,
+					 RISCV_DONT_CARE_VERSION))
+	return "ilp32e";
+      if (current_subset_list->lookup ("d", RISCV_DONT_CARE_VERSION,
+					 RISCV_DONT_CARE_VERSION))
+	return "ilp32d";
+      if (current_subset_list->lookup ("f", RISCV_DONT_CARE_VERSION,
+					 RISCV_DONT_CARE_VERSION))
+	return "ilp32f";
+      return "ilp32";
+    }
+}
+
+/* Expand default -mabi option from -march option.  */
+
+const char *
+riscv_expand_abi_from_arch (int argc, const char **argv)
+{
+  gcc_assert (argc == 1);
+  return xasprintf ("-mabi=%s", compute_default_abi (argv[0]));
+}
+
+/* Expand default -mabi option from -mcpu option.  */
+
+const char *
+riscv_expand_abi_from_cpu (int argc, const char **argv)
+{
+  gcc_assert (argc > 0 && argc <= 2);
+  const char *default_abi_str = NULL;
+  if (argc >= 2)
+    default_abi_str = argv[1];
+
+  const riscv_cpu_info *cpu = riscv_find_cpu (argv[0]);
+
+  if (cpu == NULL)
+    {
+      if (default_abi_str == NULL)
+	return "";
+      else
+	return xasprintf ("-mabi=%s", default_abi_str);
+    }
+  else
+    return xasprintf ("-mabi=%s", compute_default_abi (cpu->arch));
+}
+
 /* Expand arch string with implied extensions from -mcpu option.  */
 
 const char *
diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
index 6f7f4d3fbdc..14cd695f37f 100644
--- a/gcc/config/riscv/riscv.h
+++ b/gcc/config/riscv/riscv.h
@@ -50,15 +50,20 @@  along with GCC; see the file COPYING3.  If not see
 extern const char *riscv_expand_arch (int argc, const char **argv);
 extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv);
 extern const char *riscv_default_mtune (int argc, const char **argv);
+extern const char *riscv_expand_abi_from_arch (int argc, const char **argv);
+extern const char *riscv_expand_abi_from_cpu (int argc, const char **argv);
 
 # define EXTRA_SPEC_FUNCTIONS						\
   { "riscv_expand_arch", riscv_expand_arch },				\
   { "riscv_expand_arch_from_cpu", riscv_expand_arch_from_cpu },		\
-  { "riscv_default_mtune", riscv_default_mtune },
+  { "riscv_default_mtune", riscv_default_mtune },			\
+  { "riscv_expand_abi_from_arch", riscv_expand_abi_from_arch },		\
+  { "riscv_expand_abi_from_cpu", riscv_expand_abi_from_cpu },
 
 /* Support for a compile-time default CPU, et cetera.  The rules are:
    --with-arch is ignored if -march or -mcpu is specified.
-   --with-abi is ignored if -mabi is specified.
+   --with-abi is ignored if -mabi is specified.  If -mcpu or -march is
+     specified, suitable -mabi will be selected from arch string.
    --with-tune is ignored if -mtune or -mcpu is specified.
    --with-isa-spec is ignored if -misa-spec is specified.
 
@@ -70,7 +75,11 @@  extern const char *riscv_default_mtune (int argc, const char **argv);
   {"arch", "%{!march=*:"						\
 	   "  %{!mcpu=*:-march=%(VALUE)}"				\
 	   "  %{mcpu=*:%:riscv_expand_arch_from_cpu(%* %(VALUE))}}" },	\
-  {"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \
+  {"abi", "%{!mabi=*:"							\
+	   "  %{!march=*:"						\
+	   "    %{!mcpu=*:-mabi=%(VALUE)}"				\
+	   "    %{mcpu=*:%:riscv_expand_abi_from_cpu(%* %(VALUE))}}"	\
+	   "  %{march=*:%:riscv_expand_abi_from_arch(%*)}}" },		\
   {"isa_spec", "%{!misa-spec=*:-misa-spec=%(VALUE)}" }, \
 
 #ifdef IN_LIBGCC2
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-1.c b/gcc/testsuite/gcc.target/riscv/mabi-1.c
new file mode 100644
index 00000000000..83bc343f8d8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-1.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-march=rv32e" } */
+
+#if !((__riscv_xlen == 32) && defined(__riscv_abi_rve))
+#error "unexpected abi"
+#endif
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-2.c b/gcc/testsuite/gcc.target/riscv/mabi-2.c
new file mode 100644
index 00000000000..6a97c2ae2a3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-2.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-march=rv32ima" } */
+
+#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_soft))
+#error "unexpected abi"
+#endif
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-3.c b/gcc/testsuite/gcc.target/riscv/mabi-3.c
new file mode 100644
index 00000000000..cef04313e19
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-3.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-march=rv32imaf" } */
+
+#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_single))
+#error "unexpected abi"
+#endif
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-4.c b/gcc/testsuite/gcc.target/riscv/mabi-4.c
new file mode 100644
index 00000000000..d62dcf6ed75
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-4.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-march=rv32imafd" } */
+
+#if !((__riscv_xlen == 32) && defined(__riscv_float_abi_double))
+#error "unexpected abi"
+#endif
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-5.c b/gcc/testsuite/gcc.target/riscv/mabi-5.c
new file mode 100644
index 00000000000..000e5e282c2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-5.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-march=rv64ima" } */
+
+#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_soft))
+#error "unexpected abi"
+#endif
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-6.c b/gcc/testsuite/gcc.target/riscv/mabi-6.c
new file mode 100644
index 00000000000..ed1458d4158
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-6.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-march=rv64imaf" } */
+
+#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_single))
+#error "unexpected abi"
+#endif
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-7.c b/gcc/testsuite/gcc.target/riscv/mabi-7.c
new file mode 100644
index 00000000000..675fe359cfd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-7.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-march=rv64imafd" } */
+
+#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
+#error "unexpected abi"
+#endif
diff --git a/gcc/testsuite/gcc.target/riscv/mabi-8.c b/gcc/testsuite/gcc.target/riscv/mabi-8.c
new file mode 100644
index 00000000000..8c7e08e4289
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mabi-8.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "-mabi given" { *-*-* } { "-mabi=*" } } */
+/* { dg-options "-mcpu=sifive-s21 -march=rv64imafd" } */
+
+#if !((__riscv_xlen == 64) && defined(__riscv_float_abi_double))
+#error "unexpected abi"
+#endif