public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] MIPS: improve -march=native arch detection
@ 2022-08-02 11:10 YunQiang Su
  2022-08-14 10:36 ` YunQiang Su
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: YunQiang Su @ 2022-08-02 11:10 UTC (permalink / raw)
  To: gcc-patches; +Cc: macro, YunQiang Su

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(-)

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] MIPS: improve -march=native arch detection
  2022-08-02 11:10 [PATCH] MIPS: improve -march=native arch detection YunQiang Su
@ 2022-08-14 10:36 ` YunQiang Su
  2022-08-24  4:14 ` YunQiang Su
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: YunQiang Su @ 2022-08-14 10:36 UTC (permalink / raw)
  To: gcc-patches

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
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] MIPS: improve -march=native arch detection
  2022-08-02 11:10 [PATCH] MIPS: improve -march=native arch detection YunQiang Su
  2022-08-14 10:36 ` YunQiang Su
@ 2022-08-24  4:14 ` YunQiang Su
  2022-08-24  5:56 ` Xi Ruoyao
  2022-09-16 12:23 ` [PATCH v2] " YunQiang Su
  3 siblings, 0 replies; 5+ messages in thread
From: YunQiang Su @ 2022-08-24  4:14 UTC (permalink / raw)
  To: YunQiang Su; +Cc: apinski--- via Gcc-patches, Maciej W. Rozycki

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
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] MIPS: improve -march=native arch detection
  2022-08-02 11:10 [PATCH] MIPS: improve -march=native arch detection YunQiang Su
  2022-08-14 10:36 ` YunQiang Su
  2022-08-24  4:14 ` YunQiang Su
@ 2022-08-24  5:56 ` Xi Ruoyao
  2022-09-16 12:23 ` [PATCH v2] " YunQiang Su
  3 siblings, 0 replies; 5+ messages in thread
From: Xi Ruoyao @ 2022-08-24  5:56 UTC (permalink / raw)
  To: YunQiang Su, gcc-patches; +Cc: macro

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);
>  }

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] MIPS: improve -march=native arch detection
  2022-08-02 11:10 [PATCH] MIPS: improve -march=native arch detection YunQiang Su
                   ` (2 preceding siblings ...)
  2022-08-24  5:56 ` Xi Ruoyao
@ 2022-09-16 12:23 ` YunQiang Su
  3 siblings, 0 replies; 5+ messages in thread
From: YunQiang Su @ 2022-09-16 12:23 UTC (permalink / raw)
  To: gcc-patches; +Cc: macro, xry111, YunQiang Su

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.

mnan=2008 option is also used if __mips_nan2008__ is used.
This can fix the wrong loader usage on r5/r6 platform with
 -march=native.

gcc/ChangeLog:
	* config.gcc: set with_arch to default_mips_arch if no defined.
	* config/mips/driver-native.cc (host_detect_local_cpu):
	  try getauxval(AT_BASE_PLATFORM) and _MIPS_ARCH, too.
	  pass -mnan=2008 if __mips_nan2008__ is defined.
	* config.in: define HAVE_SYS_AUXV_H and HAVE_GETAUXVAL.
	* configure.ac: detect sys/auxv.h and getauxval.
	* configure: regenerated.
---
 gcc/config.gcc                   |  2 ++
 gcc/config.in                    | 10 ++++++++++
 gcc/config/mips/driver-native.cc | 25 ++++++++++++++++++++++---
 gcc/configure                    |  4 ++--
 gcc/configure.ac                 |  4 ++--
 5 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index f4e757bd853..181a062825d 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -5590,6 +5590,8 @@ case ${target} in
 		esac
 		if test x$with_arch != x; then
 			default_mips_arch=$with_arch
+		else
+			with_arch=$default_mips_arch
 		fi
 		if test x$with_abi != x; then
 			default_mips_abi=$with_abi
diff --git a/gcc/config.in b/gcc/config.in
index 6ac17be189e..cc217b94e0c 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -1939,6 +1939,12 @@
 #endif
 
 
+/* Define to 1 if you have the <sys/auxv.h> header file. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_SYS_AUXV_H
+#endif
+
+
 /* Define to 1 if you have the <sys/file.h> header file. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_SYS_FILE_H
@@ -2672,3 +2678,7 @@
 #undef vfork
 #endif
 
+/* Define to 1 if you have the `getauxval' function. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_GETAUXVAL
+#endif
diff --git a/gcc/config/mips/driver-native.cc b/gcc/config/mips/driver-native.cc
index 47627f85ce1..327ad255c3e 100644
--- a/gcc/config/mips/driver-native.cc
+++ b/gcc/config/mips/driver-native.cc
@@ -23,6 +23,9 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
+#ifdef HAVE_SYS_AUXV_H
+#include <sys/auxv.h>
+#endif
 
 /* This will be called by the spec parser in gcc.cc when it sees
    a %:local_cpu_detect(args) construct.  Currently it will be called
@@ -41,6 +44,7 @@ const char *
 host_detect_local_cpu (int argc, const char **argv)
 {
   const char *cpu = NULL;
+  char *ret = NULL;
   char buf[128];
   FILE *f;
   bool arch;
@@ -54,7 +58,7 @@ host_detect_local_cpu (int argc, const char **argv)
 
   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 +88,23 @@ host_detect_local_cpu (int argc, const char **argv)
 
   fclose (f);
 
+fallback_cpu:
+#if defined (__mips_nan2008)
+  ret = reconcat (ret, " -mnan=2008 ", NULL);
+#endif
+
+#ifdef HAVE_GETAUXVAL
   if (cpu == NULL)
-    return NULL;
+    cpu = (const char *) getauxval (AT_BASE_PLATFORM);
+#endif
+
+#if defined (_MIPS_ARCH)
+  if (cpu == NULL)
+    cpu = _MIPS_ARCH;
+#endif
+
+  if (cpu)
+    ret = reconcat (ret, ret, "-m", argv[0], "=", cpu, NULL);
 
-  return concat ("-m", argv[0], "=", cpu, NULL);
+  return ret;
 }
diff --git a/gcc/configure b/gcc/configure
index 817d765568e..a419ac66576 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -9327,7 +9327,7 @@ $as_echo "#define GWINSZ_IN_SYS_IOCTL 1" >>confdefs.h
 fi
 
 for ac_header in limits.h stddef.h string.h strings.h stdlib.h time.h iconv.h \
-		 fcntl.h ftw.h unistd.h sys/file.h sys/time.h sys/mman.h \
+		 fcntl.h ftw.h unistd.h sys/auxv.h sys/file.h sys/time.h sys/mman.h \
 		 sys/resource.h sys/param.h sys/times.h sys/stat.h sys/locking.h \
 		 direct.h malloc.h langinfo.h ldfcn.h locale.h wchar.h
 do :
@@ -10622,7 +10622,7 @@ fi
 for ac_func in times clock kill getrlimit setrlimit atoq \
 	popen sysconf strsignal getrusage nl_langinfo \
 	gettimeofday mbstowcs wcswidth mmap posix_fallocate setlocale \
-	clearerr_unlocked feof_unlocked   ferror_unlocked fflush_unlocked fgetc_unlocked fgets_unlocked   fileno_unlocked fprintf_unlocked fputc_unlocked fputs_unlocked   fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked   putchar_unlocked putc_unlocked madvise mallinfo mallinfo2 fstatat
+	clearerr_unlocked feof_unlocked   ferror_unlocked fflush_unlocked fgetc_unlocked fgets_unlocked   fileno_unlocked fprintf_unlocked fputc_unlocked fputs_unlocked   fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked   putchar_unlocked putc_unlocked madvise mallinfo mallinfo2 fstatat getauxval
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_cxx_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 59f205a1781..b9cc0dda571 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1307,7 +1307,7 @@ ACX_HEADER_STRING
 AC_HEADER_SYS_WAIT
 AC_HEADER_TIOCGWINSZ
 AC_CHECK_HEADERS(limits.h stddef.h string.h strings.h stdlib.h time.h iconv.h \
-		 fcntl.h ftw.h unistd.h sys/file.h sys/time.h sys/mman.h \
+		 fcntl.h ftw.h unistd.h sys/auxv.h sys/file.h sys/time.h sys/mman.h \
 		 sys/resource.h sys/param.h sys/times.h sys/stat.h sys/locking.h \
 		 direct.h malloc.h langinfo.h ldfcn.h locale.h wchar.h)
 
@@ -1525,7 +1525,7 @@ define(gcc_UNLOCKED_FUNCS, clearerr_unlocked feof_unlocked dnl
 AC_CHECK_FUNCS(times clock kill getrlimit setrlimit atoq \
 	popen sysconf strsignal getrusage nl_langinfo \
 	gettimeofday mbstowcs wcswidth mmap posix_fallocate setlocale \
-	gcc_UNLOCKED_FUNCS madvise mallinfo mallinfo2 fstatat)
+	gcc_UNLOCKED_FUNCS madvise mallinfo mallinfo2 fstatat getauxval)
 
 if test x$ac_cv_func_mbstowcs = xyes; then
   AC_CACHE_CHECK(whether mbstowcs works, gcc_cv_func_mbstowcs_works,
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-16 12:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 11:10 [PATCH] MIPS: improve -march=native arch detection YunQiang Su
2022-08-14 10:36 ` YunQiang Su
2022-08-24  4:14 ` YunQiang Su
2022-08-24  5:56 ` Xi Ruoyao
2022-09-16 12:23 ` [PATCH v2] " YunQiang Su

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).