Skip to content

Commit aa1ae52

Browse files
committed
RISC-V: Support -march=unset
This patch introduces a new `-march=unset` option for RISC-V GCC that allows users to explicitly ignore previous `-march` options and derive the architecture string from the `-mcpu` option instead. This feature is particularly useful for build systems and toolchain configurations where you want to ensure the architecture is always derived from the CPU specification rather than relying on potentially conflicting `-march` options. gcc/ChangeLog: * common/config/riscv/riscv-common.cc (riscv_expand_arch): Ignore `unset`. * config/riscv/riscv.h (OPTION_DEFAULT_SPECS): Handle `-march=unset`. (ARCH_UNSET_CLEANUP_SPECS): New. (DRIVER_SELF_SPECS): Handle -march=unset. * doc/invoke.texi (RISC-V Options): Update documentation for `-march=unset`. gcc/testsuite/ChangeLog: * gcc.target/riscv/arch-unset-1.c: New test. * gcc.target/riscv/arch-unset-2.c: New test. * gcc.target/riscv/arch-unset-3.c: New test. * gcc.target/riscv/arch-unset-4.c: New test. * gcc.target/riscv/arch-unset-5.c: New test.
1 parent 8726262 commit aa1ae52

File tree

8 files changed

+55
-3
lines changed

8 files changed

+55
-3
lines changed

gcc/common/config/riscv/riscv-common.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,11 @@ riscv_expand_arch (int argc,
17581758
{
17591759
gcc_assert (argc == 1);
17601760
location_t loc = UNKNOWN_LOCATION;
1761+
1762+
/* Filter out -march=unset, it will expand from -mcpu later. */
1763+
if (strcmp (argv[0], "unset") == 0)
1764+
return "";
1765+
17611766
/* Try to interpret the arch as CPU first. */
17621767
const char *arch_str = riscv_expand_arch_from_cpu (argc, argv);
17631768
if (!strlen (arch_str))

gcc/config/riscv/riscv.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extern const char *riscv_arch_help (int argc, const char **argv);
7171
{"tune", "%{!mtune=*:" \
7272
" %{!mcpu=*:-mtune=%(VALUE)}" \
7373
" %{mcpu=*:-mtune=%:riscv_default_mtune(%* %(VALUE))}}" }, \
74-
{"arch", "%{!march=*:" \
74+
{"arch", "%{!march=*|march=unset:" \
7575
" %{!mcpu=*:-march=%(VALUE)}" \
7676
" %{mcpu=*:%:riscv_expand_arch_from_cpu(%* %(VALUE))}}" }, \
7777
{"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \
@@ -111,13 +111,19 @@ extern const char *riscv_arch_help (int argc, const char **argv);
111111
%(subtarget_asm_spec)" \
112112
ASM_MISA_SPEC
113113

114+
/* Drop all -march=* options before -march=unset. */
115+
#define ARCH_UNSET_CLEANUP_SPECS \
116+
"%{march=unset:%<march=*} " \
117+
114118
#undef DRIVER_SELF_SPECS
115119
#define DRIVER_SELF_SPECS \
120+
ARCH_UNSET_CLEANUP_SPECS \
116121
"%{march=help:%:riscv_arch_help()} " \
117122
"%{print-supported-extensions:%:riscv_arch_help()} " \
118123
"%{-print-supported-extensions:%:riscv_arch_help()} " \
119124
"%{march=*:%:riscv_expand_arch(%*)} " \
120-
"%{!march=*:%{mcpu=*:%:riscv_expand_arch_from_cpu(%*)}} "
125+
"%{!march=*|march=unset:%{mcpu=*:%:riscv_expand_arch_from_cpu(%*)}} " \
126+
"%{march=unset:%{!mcpu=*:%eAt least one valid -mcpu option must be given after -march=unset}} "
121127

122128
#define LOCAL_LABEL_PREFIX "."
123129
#define USER_LABEL_PREFIX ""

gcc/doc/invoke.texi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31336,7 +31336,7 @@ The default is @option{-misa-spec=20191213} unless GCC has been configured
3133631336
with @option{--with-isa-spec=} specifying a different default version.
3133731337

3133831338
@opindex march
31339-
@item -march=@var{ISA-string|Profiles|Profile_ISA-string}
31339+
@item -march=@var{ISA-string|Profiles|Profile_ISA-string|help|unset}
3134031340
Generate code for given RISC-V ISA or Profiles or a combination of them
3134131341
(e.g.@: @samp{rv64im} @samp{rvi20u64} @samp{rvi20u64_zbb}). ISA strings and
3134231342
Profiles must be lower-case. Examples include @samp{rv64i}, @samp{rv32g},
@@ -31347,6 +31347,12 @@ at the beginning of the option, then use underline connect ISA-string (e.g.@:
3134731347
@option{help} (@option{-march=help}) is accepted to list all supported
3134831348
extensions.
3134931349

31350+
@samp{-march=unset} causes the compiler to ignore any @samp{-march=@dots{}} options
31351+
that appear earlier on the command line, behaving as if the option was never
31352+
passed. This is useful for ensuring that the architecture is taken from the
31353+
@samp{-mcpu} option, and it will result in an error if no @samp{-mcpu} option
31354+
is given when @samp{-march=unset} is used.
31355+
3135031356
The syntax of the ISA string is defined as follows:
3135131357

3135231358
@table @code
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-march=rv64i -march=unset -mcpu=sifive-x280 -mabi=lp64 -misa-spec=20191213" } */
3+
int foo()
4+
{
5+
}
6+
7+
/* { dg-final { scan-assembler "\.attribute arch, \"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_v1p0_zicsr2p0_zmmul1p0_zaamo1p0_zalrsc1p0_zfh1p0_zfhmin1p0_zca1p0_zcd1p0_zba1p0_zbb1p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvfh1p0_zvl128b1p0_zvl256b1p0_zvl32b1p0_zvl512b1p0_zvl64b1p0\"" } } */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-march=rv64i -march=unset -mcpu=sifive-x280 -march=rv64i -mabi=lp64 -misa-spec=20191213" } */
3+
int foo()
4+
{
5+
}
6+
7+
/* { dg-final { scan-assembler "\.attribute arch, \"rv64i2p1\"" } } */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-march=rv64i -march=unset -mcpu=sifive-x280 -march=rv64i -march=unset -mabi=lp64 -misa-spec=20191213" } */
3+
int foo()
4+
{
5+
}
6+
7+
/* { dg-final { scan-assembler "\.attribute arch, \"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_v1p0_zicsr2p0_zmmul1p0_zaamo1p0_zalrsc1p0_zfh1p0_zfhmin1p0_zca1p0_zcd1p0_zba1p0_zbb1p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvfh1p0_zvl128b1p0_zvl256b1p0_zvl32b1p0_zvl512b1p0_zvl64b1p0\"" } } */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-march=rv64i -march=unset -mcpu=sifive-x280 -march=unset -march=rv64i -march=unset -march=rv64i -mabi=lp64 -misa-spec=20191213" } */
3+
int foo()
4+
{
5+
}
6+
7+
/* { dg-final { scan-assembler "\.attribute arch, \"rv64i2p1\"" } } */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-march=rv64i -march=unset -mabi=lp64 -misa-spec=20191213" } */
3+
int foo()
4+
{
5+
}
6+
7+
/* { dg-error "At least one valid -mcpu option must be given after -march=unset" "" { target { "riscv*-*-*" } } 0 } */

0 commit comments

Comments
 (0)