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
>

Reply via email to