MIPS: improve -march=native arch detection

Message ID 20220802111009.35536-1-yunqiang.su@cipunited.com
State New, archived
Headers
Series MIPS: improve -march=native arch detection |

Commit Message

YunQiang Su Aug. 2, 2022, 11:10 a.m. UTC
  If we cannot get info from options and cpuinfo, we try to get from:
  1. getauxval(AT_BASE_PLATFORM), introduced since Linux 5.7
  2. _MIPS_ARCH from host compiler.

This can fix the wrong loader usage on r5/r6 platform with
 -march=native.

gcc/ChangeLog:
	* config/mips/driver-native.cc (host_detect_local_cpu):
	  try getauxval(AT_BASE_PLATFORM) and _MIPS_ARCH, too.
---
 gcc/config/mips/driver-native.cc | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
  

Comments

YunQiang Su Aug. 14, 2022, 10:36 a.m. UTC | #1
On Tue, Aug 02, 2022 at 11:10:09AM +0000, YunQiang Su wrote:
> If we cannot get info from options and cpuinfo, we try to get from:
>   1. getauxval(AT_BASE_PLATFORM), introduced since Linux 5.7
>   2. _MIPS_ARCH from host compiler.
> 
> This can fix the wrong loader usage on r5/r6 platform with
>  -march=native.
>

ping...

> gcc/ChangeLog:
> 	* config/mips/driver-native.cc (host_detect_local_cpu):
> 	  try getauxval(AT_BASE_PLATFORM) and _MIPS_ARCH, too.
> ---
>  gcc/config/mips/driver-native.cc | 22 +++++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
> 
> diff --git a/gcc/config/mips/driver-native.cc b/gcc/config/mips/driver-native.cc
> index 47627f85ce1..9aa7044c0b8 100644
> --- a/gcc/config/mips/driver-native.cc
> +++ b/gcc/config/mips/driver-native.cc
> @@ -19,6 +19,7 @@ along with GCC; see the file COPYING3.  If not see
>  
>  #define IN_TARGET_CODE 1
>  
> +#include <sys/auxv.h>
>  #include "config.h"
>  #include "system.h"
>  #include "coretypes.h"
> @@ -46,15 +47,15 @@ host_detect_local_cpu (int argc, const char **argv)
>    bool arch;
>  
>    if (argc < 1)
> -    return NULL;
> +    goto fallback_cpu;
>  
>    arch = strcmp (argv[0], "arch") == 0;
>    if (!arch && strcmp (argv[0], "tune"))
> -    return NULL;
> +    goto fallback_cpu;
>  
>    f = fopen ("/proc/cpuinfo", "r");
>    if (f == NULL)
> -    return NULL;
> +    goto fallback_cpu;
>  
>    while (fgets (buf, sizeof (buf), f) != NULL)
>      if (startswith (buf, "cpu model"))
> @@ -84,8 +85,23 @@ host_detect_local_cpu (int argc, const char **argv)
>  
>    fclose (f);
>  
> +fallback_cpu:
> +/*FIXME: how about other OSes, like FreeBSD? */
> +#ifdef __linux__
> +  /*Note: getauxval may return NULL as:
> +   * AT_BASE_PLATFORM is supported since Linux 5.7
> +   * Or from older version of qemu-user
> +   * */
> +  if (cpu == NULL)
> +    cpu = (const char *) getauxval (AT_BASE_PLATFORM);
> +#endif
> +
>    if (cpu == NULL)
> +#if defined (_MIPS_ARCH)
> +    cpu = _MIPS_ARCH;
> +#else
>      return NULL;
> +#endif
>  
>    return concat ("-m", argv[0], "=", cpu, NULL);
>  }
> -- 
> 2.30.2
>
  
YunQiang Su Aug. 24, 2022, 4:14 a.m. UTC | #2
YunQiang Su <yunqiang.su@cipunited.com> 于2022年8月2日周二 19:11写道:
>
> If we cannot get info from options and cpuinfo, we try to get from:
>   1. getauxval(AT_BASE_PLATFORM), introduced since Linux 5.7
>   2. _MIPS_ARCH from host compiler.
>
> This can fix the wrong loader usage on r5/r6 platform with
>  -march=native.
>

Is it treat as minor fixes?

> gcc/ChangeLog:
>         * config/mips/driver-native.cc (host_detect_local_cpu):
>           try getauxval(AT_BASE_PLATFORM) and _MIPS_ARCH, too.
> ---
>  gcc/config/mips/driver-native.cc | 22 +++++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
>
> diff --git a/gcc/config/mips/driver-native.cc b/gcc/config/mips/driver-native.cc
> index 47627f85ce1..9aa7044c0b8 100644
> --- a/gcc/config/mips/driver-native.cc
> +++ b/gcc/config/mips/driver-native.cc
> @@ -19,6 +19,7 @@ along with GCC; see the file COPYING3.  If not see
>
>  #define IN_TARGET_CODE 1
>
> +#include <sys/auxv.h>
>  #include "config.h"
>  #include "system.h"
>  #include "coretypes.h"
> @@ -46,15 +47,15 @@ host_detect_local_cpu (int argc, const char **argv)
>    bool arch;
>
>    if (argc < 1)
> -    return NULL;
> +    goto fallback_cpu;
>
>    arch = strcmp (argv[0], "arch") == 0;
>    if (!arch && strcmp (argv[0], "tune"))
> -    return NULL;
> +    goto fallback_cpu;
>
>    f = fopen ("/proc/cpuinfo", "r");
>    if (f == NULL)
> -    return NULL;
> +    goto fallback_cpu;
>
>    while (fgets (buf, sizeof (buf), f) != NULL)
>      if (startswith (buf, "cpu model"))
> @@ -84,8 +85,23 @@ host_detect_local_cpu (int argc, const char **argv)
>
>    fclose (f);
>
> +fallback_cpu:
> +/*FIXME: how about other OSes, like FreeBSD? */
> +#ifdef __linux__
> +  /*Note: getauxval may return NULL as:
> +   * AT_BASE_PLATFORM is supported since Linux 5.7
> +   * Or from older version of qemu-user
> +   * */
> +  if (cpu == NULL)
> +    cpu = (const char *) getauxval (AT_BASE_PLATFORM);
> +#endif
> +
>    if (cpu == NULL)
> +#if defined (_MIPS_ARCH)
> +    cpu = _MIPS_ARCH;
> +#else
>      return NULL;
> +#endif
>
>    return concat ("-m", argv[0], "=", cpu, NULL);
>  }
> --
> 2.30.2
>
  
Xi Ruoyao Aug. 24, 2022, 5:56 a.m. UTC | #3
On Tue, 2022-08-02 at 11:10 +0000, YunQiang Su wrote:

> If we cannot get info from options and cpuinfo, we try to get from:
>   1. getauxval(AT_BASE_PLATFORM), introduced since Linux 5.7
>   2. _MIPS_ARCH from host compiler.
> 
> This can fix the wrong loader usage on r5/r6 platform with
>  -march=native.

/* snip */

>    if (argc < 1)
> -    return NULL;
> +    goto fallback_cpu;

I don't think this should be changed, if argc < 1 it means the spec
(disambiguation: the thing printed by "gcc -dumpspecs", hard coded in
gnu-user.h) is wrong.  It cannot happen with the built-in spec, but if a
user specifies a bad custom spec with "-spec", we shouldn't be tricked.

>    arch = strcmp (argv[0], "arch") == 0;
>    if (!arch && strcmp (argv[0], "tune"))
> -    return NULL;
> +    goto fallback_cpu;

Likewise.

>    f = fopen ("/proc/cpuinfo", "r");
>    if (f == NULL)
> -    return NULL;
> +    goto fallback_cpu;

OK.

> +fallback_cpu:
> +/*FIXME: how about other OSes, like FreeBSD? */

https://reviews.freebsd.org/D12743 added elf_aux_info as a counterpart
of getauxinfo, but it looks like FreeBSD does not have AT_BASE_PLATFORM.

> +#ifdef __linux__
> +  /*Note: getauxval may return NULL as:
> +   * AT_BASE_PLATFORM is supported since Linux 5.7
> +   * Or from older version of qemu-user
> +   * */
> +  if (cpu == NULL)
> +    cpu = (const char *) getauxval (AT_BASE_PLATFORM);

getauxval is added in Glibc-2.16 so it will fail to build on hosts with
old glibc or other libc implementation.  Check if getauxval and
AT_BASE_PLATFORM are available (in gcc/configure.ac) instead of an
inaccurate "#ifdef __linux__".

> +#endif
> +
>    if (cpu == NULL)
> +#if defined (_MIPS_ARCH)
> +    cpu = _MIPS_ARCH;
> +#else

Ok.

>      return NULL;
> +#endif
>  
>    return concat ("-m", argv[0], "=", cpu, NULL);
>  }
  

Patch

diff --git a/gcc/config/mips/driver-native.cc b/gcc/config/mips/driver-native.cc
index 47627f85ce1..9aa7044c0b8 100644
--- a/gcc/config/mips/driver-native.cc
+++ b/gcc/config/mips/driver-native.cc
@@ -19,6 +19,7 @@  along with GCC; see the file COPYING3.  If not see
 
 #define IN_TARGET_CODE 1
 
+#include <sys/auxv.h>
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
@@ -46,15 +47,15 @@  host_detect_local_cpu (int argc, const char **argv)
   bool arch;
 
   if (argc < 1)
-    return NULL;
+    goto fallback_cpu;
 
   arch = strcmp (argv[0], "arch") == 0;
   if (!arch && strcmp (argv[0], "tune"))
-    return NULL;
+    goto fallback_cpu;
 
   f = fopen ("/proc/cpuinfo", "r");
   if (f == NULL)
-    return NULL;
+    goto fallback_cpu;
 
   while (fgets (buf, sizeof (buf), f) != NULL)
     if (startswith (buf, "cpu model"))
@@ -84,8 +85,23 @@  host_detect_local_cpu (int argc, const char **argv)
 
   fclose (f);
 
+fallback_cpu:
+/*FIXME: how about other OSes, like FreeBSD? */
+#ifdef __linux__
+  /*Note: getauxval may return NULL as:
+   * AT_BASE_PLATFORM is supported since Linux 5.7
+   * Or from older version of qemu-user
+   * */
+  if (cpu == NULL)
+    cpu = (const char *) getauxval (AT_BASE_PLATFORM);
+#endif
+
   if (cpu == NULL)
+#if defined (_MIPS_ARCH)
+    cpu = _MIPS_ARCH;
+#else
     return NULL;
+#endif
 
   return concat ("-m", argv[0], "=", cpu, NULL);
 }