https://gcc.gnu.org/g:72f8433690e893410155a14c22d72dc435b9f9e2

commit r17-6379-g72f8433690e893410155a14c22d72dc435b9f9e2
Author: Kito Cheng <[email protected]>
Date:   Mon Nov 16 21:02:48 2026 +0800

    RISC-V: Pass the detected core with -mcpu=native
    
    -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.

Diff:
---
 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/testsuite/gcc.target/riscv/mcpu-native-1.cpuinfo |  8 ++++++++
 5 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/gcc/config/riscv/driver-riscv.cc b/gcc/config/riscv/driver-riscv.cc
index 467a959afb5a..ea6604dfb36c 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 7ba2a726ac40..7dc39764ec88 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 5284996177d0..c0e97db9a012 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -31554,9 +31554,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 000000000000..496082642eb5
--- /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 000000000000..cacd4d6a9cad
--- /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
+

Reply via email to