-mcpu=native becomes -march=<detected ISA> -mtune=<detected core>, so
cc1 never sees -mcpu. Later patches take per-core settings, such as
errata, from -mcpu, and they would be lost.
Also pass -mcpu=<core> when the driver knows the core. The detected
-march still wins over the core's default ISA.
gcc/ChangeLog:
* config/riscv/driver-riscv.cc (host_detect_local_cpu): Handle
"cpu".
* config/riscv/riscv.h (RISCV_NATIVE_SPECS): Pass -mcpu= for
-mcpu=native.
* doc/invoke.texi (RISC-V Options): Say that -mcpu=native also
selects the detected core.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/mcpu-native-1.c: New test.
* gcc.target/riscv/mcpu-native-1.cpuinfo: New file.
---
gcc/config/riscv/driver-riscv.cc | 13 ++++++++++++-
gcc/config/riscv/riscv.h | 2 +-
gcc/doc/invoke.texi | 7 ++++---
gcc/testsuite/gcc.target/riscv/mcpu-native-1.c | 13 +++++++++++++
.../gcc.target/riscv/mcpu-native-1.cpuinfo | 8 ++++++++
5 files changed, 38 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/riscv/mcpu-native-1.c
create mode 100644 gcc/testsuite/gcc.target/riscv/mcpu-native-1.cpuinfo
diff --git a/gcc/config/riscv/driver-riscv.cc b/gcc/config/riscv/driver-riscv.cc
index 467a959afb5..ea6604dfb36 100644
--- a/gcc/config/riscv/driver-riscv.cc
+++ b/gcc/config/riscv/driver-riscv.cc
@@ -574,7 +574,8 @@ riscv_native_tune (void)
#endif /* __linux__ */
/* Implement the local_cpu_detect spec function. ARGV[0] selects what to
- report: "arch" for an -march= option, "tune" for an -mtune= one. */
+ report: "arch" for an -march= option, "tune" for an -mtune= one, "cpu"
+ for an -mcpu= one. */
const char *
host_detect_local_cpu (int argc, const char **argv)
@@ -602,5 +603,15 @@ host_detect_local_cpu (int argc, const char **argv)
return concat ("-mtune=", core, NULL);
}
+ if (strcmp (argv[0], "cpu") == 0)
+ {
+ const char *core = riscv_native_tune ();
+
+ if (core == NULL)
+ return NULL;
+
+ return concat ("-mcpu=", core, NULL);
+ }
+
return NULL;
}
diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
index 7ba2a726ac4..7dc39764ec8 100644
--- a/gcc/config/riscv/riscv.h
+++ b/gcc/config/riscv/riscv.h
@@ -61,7 +61,7 @@ extern const char *host_detect_local_cpu (int argc, const
char **argv);
# define RISCV_NATIVE_SPECS \
"%{march=native:%<march=native %:local_cpu_detect(arch)} " \
"%{mtune=native:%<mtune=native %:local_cpu_detect(tune)} " \
- "%{mcpu=native:%<mcpu=native " \
+ "%{mcpu=native:%<mcpu=native %:local_cpu_detect(cpu) " \
"%{!march=*|march=unset:%<march=* %:local_cpu_detect(arch)} " \
"%{!mtune=*:%:local_cpu_detect(tune)}} "
#else
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index b17b996677b..8ba2a2baed3 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -31539,9 +31539,10 @@ When @option{-mtune=} is not specified, GCC uses the
setting from
@option{-mtune=native} and @option{-mcpu=native} identify the core the
compiler is running on, subject to the same restrictions and the same
caveat about unlike cores as @option{-march=native} above.
-@option{-mcpu=native} additionally selects the detected architecture. A
-core the compiler has no hardware identification for leaves the tuning at
-its default rather than causing an error.
+@option{-mcpu=native} additionally selects the detected architecture, and
+the detected core as if by @option{-mcpu=}. A core the compiler has no
+hardware identification for leaves the tuning at its default rather than
+causing an error.
The @samp{size} choice is not intended for use by end-users. This is used
when @option{-Os} is specified. It overrides the instruction cost info
diff --git a/gcc/testsuite/gcc.target/riscv/mcpu-native-1.c
b/gcc/testsuite/gcc.target/riscv/mcpu-native-1.c
new file mode 100644
index 00000000000..496082642eb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mcpu-native-1.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target riscv_native_cpu_detect } */
+/* { dg-require-effective-target rv64 } */
+/* { dg-set-compiler-env-var GCC_CPUINFO
"$srcdir/gcc.target/riscv/mcpu-native-1.cpuinfo" } */
+/* { dg-options "-mcpu=native -fverbose-asm" } */
+
+int
+main (void)
+{
+ return 0;
+}
+
+/* { dg-final { scan-assembler {-mcpu=sifive-p870-d} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/mcpu-native-1.cpuinfo
b/gcc/testsuite/gcc.target/riscv/mcpu-native-1.cpuinfo
new file mode 100644
index 00000000000..cacd4d6a9ca
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mcpu-native-1.cpuinfo
@@ -0,0 +1,8 @@
+processor : 0
+hart : 0
+isa : rv64imafdc
+mmu : sv39
+mvendorid : 0x489
+marchid : 0x8000000000000109
+mimpid : 0x14250606
+
--
2.54.0