public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH][ARM] -m{cpu,tune,arch}=native
@ 2011-08-26 16:16 Andrew Stubbs
  2011-08-26 17:17 ` Joseph S. Myers
  2011-08-29  8:37 ` Michael Hope
  0 siblings, 2 replies; 15+ messages in thread
From: Andrew Stubbs @ 2011-08-26 16:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: patches

[-- Attachment #1: Type: text/plain, Size: 532 bytes --]

Hi all,

This patch adds support for -mcpu=native, -mtune=native, and 
-march=native for ARM Linux hosts.

So far, it only recognises Cortex-A8 and Cortex-A9, so I really need to 
find out what the magic part numbers are for other cpus before this 
patch is complete. I couldn't just find this information listed 
anywhere. I think there are a lot of clues in the kernel code, but it's 
hard to mine and it mostly only goes as far the architecture version, 
not the individual cpu. Any suggestions?

Otherwise, is this OK?

Andrew


[-- Attachment #2: tune-native.patch --]
[-- Type: text/x-patch, Size: 6824 bytes --]

2011-08-26  Andrew Stubbs  <ams@codesourcery.com>

	gcc/
	* config.host (arm*-*-linux*): Add driver-arm.o and x-arm.
	* config/arm/arm-tables.opt: Add 'native' processor type and
	architecture type.
	* config/arm/arm.h (host_detect_local_cpu): New prototype.
	(EXTRA_SPEC_FUNCTIONS): New define.
	(MCPU_MTUNE_NATIVE_SPECS): New define.
	(DRIVER_SELF_SPECS): New define.
	* config/arm/driver-arm.c: New file.
	* config/arm/x-arm: New file.
	* doc/invoke.texi (ARM Options): Document -mcpu=native,
	-mtune=native and -march=native.

--- a/gcc/config.host
+++ b/gcc/config.host
@@ -100,6 +100,14 @@ case ${host} in
 esac
 
 case ${host} in
+  arm*-*-linux*)
+    case ${target} in
+      arm*-*-*)
+	host_extra_gcc_objs="driver-arm.o"
+	host_xmake_file="${host_xmake_file} arm/x-arm"
+	;;
+    esac
+    ;;
   alpha*-*-linux* | alpha*-dec-osf*)
     case ${target} in
       alpha*-*-linux* | alpha*-dec-osf*)
--- a/gcc/config/arm/arm-tables.opt
+++ b/gcc/config/arm/arm-tables.opt
@@ -25,6 +25,9 @@ Name(processor_type) Type(enum processor_type)
 Known ARM CPUs (for use with the -mcpu= and -mtune= options):
 
 EnumValue
+Enum(processor_type) String(native) Value(-1) DriverOnly
+
+EnumValue
 Enum(processor_type) String(arm2) Value(arm2)
 
 EnumValue
@@ -269,6 +272,9 @@ Name(arm_arch) Type(int)
 Known ARM architectures (for use with the -march= option):
 
 EnumValue
+Enum(arm_arch) String(native) Value(-1) DriverOnly
+
+EnumValue
 Enum(arm_arch) String(armv2) Value(0)
 
 EnumValue
--- a/gcc/config/arm/arm.h
+++ b/gcc/config/arm/arm.h
@@ -2223,4 +2223,21 @@ extern int making_const_table;
    instruction.  */
 #define MAX_LDM_STM_OPS 4
 
+/* -mcpu=native handling only makes sense with compiler running on
+   an ARM chip.  */
+#if defined(__arm__)
+extern const char *host_detect_local_cpu (int argc, const char **argv);
+# define EXTRA_SPEC_FUNCTIONS						\
+  { "local_cpu_detect", host_detect_local_cpu },
+
+# define MCPU_MTUNE_NATIVE_SPECS					\
+   " %{march=native:%<march=native %:local_cpu_detect(arch)}"		\
+   " %{mcpu=native:%<mcpu=native %:local_cpu_detect(cpu)}"		\
+   " %{mtune=native:%<mtune=native %:local_cpu_detect(tune)}"
+#else
+# define MCPU_MTUNE_NATIVE_SPECS ""
+#endif
+
+#define DRIVER_SELF_SPECS MCPU_MTUNE_NATIVE_SPECS
+
 #endif /* ! GCC_ARM_H */
--- /dev/null
+++ b/gcc/config/arm/driver-arm.c
@@ -0,0 +1,86 @@
+/* Subroutines for the gcc driver.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+
+static struct {
+  const char *part_no;
+  const char *arch_name;
+  const char *cpu_name;
+} cpu_table[] = {
+    {"0xc08", "armv7-a", "cortex-a8"},
+    {"0xc09", "armv7-a", "cortex-a9"},
+    {NULL, NULL, NULL}
+};
+
+/* This will be called by the spec parser in gcc.c when it sees
+   a %:local_cpu_detect(args) construct.  Currently it will be called
+   with either "arch", "cpu" or "tune" as argument depending on if
+   -march=native, -mcpu=native or -mtune=native is to be substituted.
+
+   It returns a string containing new command line parameters to be
+   put at the place of the above two options, depending on what CPU
+   this is executed.  E.g. "-march=armv7-a" on a Cortex-A8 for
+   -march=native.  If the routine can't detect a known processor,
+   the -march or -mtune option is discarded.
+
+   ARGC and ARGV are set depending on the actual arguments given
+   in the spec.  */
+const char *
+host_detect_local_cpu (int argc, const char **argv)
+{
+  const char *val = NULL;
+  char buf[128];
+  FILE *f;
+  bool arch;
+
+  if (argc < 1)
+    return NULL;
+
+  arch = strcmp (argv[0], "arch") == 0;
+  if (!arch && strcmp (argv[0], "cpu") != 0 && strcmp (argv[0], "tune"))
+    return NULL;
+
+  f = fopen ("/proc/cpuinfo", "r");
+  if (f == NULL)
+    return NULL;
+
+  while (fgets (buf, sizeof (buf), f) != NULL)
+    if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
+      {
+	int i;
+	for (i = 0; cpu_table[i].part_no != NULL; i++)
+	  if (strstr (buf, cpu_table[i].part_no) != NULL)
+	    {
+	      val = arch ? cpu_table[i].arch_name : cpu_table[i].cpu_name;
+	      break;
+	    }
+	break;
+      }
+
+  fclose (f);
+
+  if (val == NULL)
+    return NULL;
+
+  return concat ("-m", argv[0], "=", val, NULL);
+}
--- /dev/null
+++ b/gcc/config/arm/x-arm
@@ -0,0 +1,3 @@
+driver-arm.o: $(srcdir)/config/arm/driver-arm.c \
+  $(CONFIG_H) $(SYSTEM_H)
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -10318,6 +10318,11 @@ assembly code.  Permissible names are: @samp{arm2}, @samp{arm250},
 @samp{fa526}, @samp{fa626},
 @samp{fa606te}, @samp{fa626te}, @samp{fmp626}, @samp{fa726te}.
 
+@option{-mcpu=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mtune=@var{name}
 @opindex mtune
 This option is very similar to the @option{-mcpu=} option, except that
@@ -10329,6 +10334,11 @@ will generate based on the CPU specified by a @option{-mcpu=} option.
 For some ARM implementations better performance can be obtained by using
 this option.
 
+@option{-mtune=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -march=@var{name}
 @opindex march
 This specifies the name of the target ARM architecture.  GCC uses this
@@ -10342,6 +10352,11 @@ of the @option{-mcpu=} option.  Permissible names are: @samp{armv2},
 @samp{armv7}, @samp{armv7-a}, @samp{armv7-r}, @samp{armv7-m},
 @samp{iwmmxt}, @samp{iwmmxt2}, @samp{ep9312}.
 
+@option{-march=native} causes the compiler to auto-detect the architecture
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mfpu=@var{name}
 @itemx -mfpe=@var{number}
 @itemx -mfp=@var{number}

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-08-26 16:16 [PATCH][ARM] -m{cpu,tune,arch}=native Andrew Stubbs
@ 2011-08-26 17:17 ` Joseph S. Myers
  2011-08-28  9:43   ` Andrew Stubbs
  2011-08-29  8:37 ` Michael Hope
  1 sibling, 1 reply; 15+ messages in thread
From: Joseph S. Myers @ 2011-08-26 17:17 UTC (permalink / raw)
  To: Andrew Stubbs; +Cc: gcc-patches, patches

On Fri, 26 Aug 2011, Andrew Stubbs wrote:

> Hi all,
> 
> This patch adds support for -mcpu=native, -mtune=native, and -march=native for
> ARM Linux hosts.
> 
> So far, it only recognises Cortex-A8 and Cortex-A9, so I really need to find
> out what the magic part numbers are for other cpus before this patch is
> complete. I couldn't just find this information listed anywhere. I think there
> are a lot of clues in the kernel code, but it's hard to mine and it mostly
> only goes as far the architecture version, not the individual cpu. Any
> suggestions?
> 
> Otherwise, is this OK?

arm-tables.opt is a generated file.  You need to modify the source files 
and regenerate it, not modify the generated file.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-08-26 17:17 ` Joseph S. Myers
@ 2011-08-28  9:43   ` Andrew Stubbs
  2011-09-06 13:35     ` Andrew Stubbs
  0 siblings, 1 reply; 15+ messages in thread
From: Andrew Stubbs @ 2011-08-28  9:43 UTC (permalink / raw)
  To: Joseph S. Myers; +Cc: gcc-patches, patches

[-- Attachment #1: Type: text/plain, Size: 270 bytes --]

On 26/08/11 17:16, Joseph S. Myers wrote:
> arm-tables.opt is a generated file.  You need to modify the source files
> and regenerate it, not modify the generated file.

Fixed; the "native" option value is now defined in arm.opt. Thanks for 
spotting this.

OK?

Andrew

[-- Attachment #2: tune-native.patch --]
[-- Type: text/x-patch, Size: 7201 bytes --]

2011-08-27  Andrew Stubbs  <ams@codesourcery.com>

	gcc/
	* config.host (arm*-*-linux*): Add driver-arm.o and x-arm.
	* config/arm/arm.opt: Add 'native' processor_type and
	arm_arch enum values.
	* config/arm/arm.h (host_detect_local_cpu): New prototype.
	(EXTRA_SPEC_FUNCTIONS): New define.
	(MCPU_MTUNE_NATIVE_SPECS): New define.
	(DRIVER_SELF_SPECS): New define.
	* config/arm/driver-arm.c: New file.
	* config/arm/x-arm: New file.
	* doc/invoke.texi (ARM Options): Document -mcpu=native,
	-mtune=native and -march=native.

--- a/gcc/config.host
+++ b/gcc/config.host
@@ -100,6 +100,14 @@ case ${host} in
 esac
 
 case ${host} in
+  arm*-*-linux*)
+    case ${target} in
+      arm*-*-*)
+	host_extra_gcc_objs="driver-arm.o"
+	host_xmake_file="${host_xmake_file} arm/x-arm"
+	;;
+    esac
+    ;;
   alpha*-*-linux* | alpha*-dec-osf*)
     case ${target} in
       alpha*-*-linux* | alpha*-dec-osf*)
--- a/gcc/config/arm/arm.h
+++ b/gcc/config/arm/arm.h
@@ -2223,4 +2223,21 @@ extern int making_const_table;
    instruction.  */
 #define MAX_LDM_STM_OPS 4
 
+/* -mcpu=native handling only makes sense with compiler running on
+   an ARM chip.  */
+#if defined(__arm__)
+extern const char *host_detect_local_cpu (int argc, const char **argv);
+# define EXTRA_SPEC_FUNCTIONS						\
+  { "local_cpu_detect", host_detect_local_cpu },
+
+# define MCPU_MTUNE_NATIVE_SPECS					\
+   " %{march=native:%<march=native %:local_cpu_detect(arch)}"		\
+   " %{mcpu=native:%<mcpu=native %:local_cpu_detect(cpu)}"		\
+   " %{mtune=native:%<mtune=native %:local_cpu_detect(tune)}"
+#else
+# define MCPU_MTUNE_NATIVE_SPECS ""
+#endif
+
+#define DRIVER_SELF_SPECS MCPU_MTUNE_NATIVE_SPECS
+
 #endif /* ! GCC_ARM_H */
--- a/gcc/config/arm/arm.opt
+++ b/gcc/config/arm/arm.opt
@@ -80,6 +80,11 @@ march=
 Target RejectNegative Joined Enum(arm_arch) Var(arm_arch_option)
 Specify the name of the target architecture
 
+; Other arm_arch values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(arm_arch) String(native) Value(-1) DriverOnly
+
 marm
 Target Report RejectNegative InverseMask(THUMB)
 Generate code in 32 bit ARM state.
@@ -233,6 +238,11 @@ mtune=
 Target RejectNegative Joined Enum(processor_type) Var(arm_tune_option) Init(arm_none)
 Tune code for the given processor
 
+; Other processor_type values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(processor_type) String(native) Value(-1) DriverOnly
+
 mwords-little-endian
 Target Report RejectNegative Mask(LITTLE_WORDS)
 Assume big endian bytes, little endian words.  This option is deprecated.
--- /dev/null
+++ b/gcc/config/arm/driver-arm.c
@@ -0,0 +1,86 @@
+/* Subroutines for the gcc driver.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+
+static struct {
+  const char *part_no;
+  const char *arch_name;
+  const char *cpu_name;
+} cpu_table[] = {
+    {"0xc08", "armv7-a", "cortex-a8"},
+    {"0xc09", "armv7-a", "cortex-a9"},
+    {NULL, NULL, NULL}
+};
+
+/* This will be called by the spec parser in gcc.c when it sees
+   a %:local_cpu_detect(args) construct.  Currently it will be called
+   with either "arch", "cpu" or "tune" as argument depending on if
+   -march=native, -mcpu=native or -mtune=native is to be substituted.
+
+   It returns a string containing new command line parameters to be
+   put at the place of the above two options, depending on what CPU
+   this is executed.  E.g. "-march=armv7-a" on a Cortex-A8 for
+   -march=native.  If the routine can't detect a known processor,
+   the -march or -mtune option is discarded.
+
+   ARGC and ARGV are set depending on the actual arguments given
+   in the spec.  */
+const char *
+host_detect_local_cpu (int argc, const char **argv)
+{
+  const char *val = NULL;
+  char buf[128];
+  FILE *f;
+  bool arch;
+
+  if (argc < 1)
+    return NULL;
+
+  arch = strcmp (argv[0], "arch") == 0;
+  if (!arch && strcmp (argv[0], "cpu") != 0 && strcmp (argv[0], "tune"))
+    return NULL;
+
+  f = fopen ("/proc/cpuinfo", "r");
+  if (f == NULL)
+    return NULL;
+
+  while (fgets (buf, sizeof (buf), f) != NULL)
+    if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
+      {
+	int i;
+	for (i = 0; cpu_table[i].part_no != NULL; i++)
+	  if (strstr (buf, cpu_table[i].part_no) != NULL)
+	    {
+	      val = arch ? cpu_table[i].arch_name : cpu_table[i].cpu_name;
+	      break;
+	    }
+	break;
+      }
+
+  fclose (f);
+
+  if (val == NULL)
+    return NULL;
+
+  return concat ("-m", argv[0], "=", val, NULL);
+}
--- /dev/null
+++ b/gcc/config/arm/x-arm
@@ -0,0 +1,3 @@
+driver-arm.o: $(srcdir)/config/arm/driver-arm.c \
+  $(CONFIG_H) $(SYSTEM_H)
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -10318,6 +10318,11 @@ assembly code.  Permissible names are: @samp{arm2}, @samp{arm250},
 @samp{fa526}, @samp{fa626},
 @samp{fa606te}, @samp{fa626te}, @samp{fmp626}, @samp{fa726te}.
 
+@option{-mcpu=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mtune=@var{name}
 @opindex mtune
 This option is very similar to the @option{-mcpu=} option, except that
@@ -10329,6 +10334,11 @@ will generate based on the CPU specified by a @option{-mcpu=} option.
 For some ARM implementations better performance can be obtained by using
 this option.
 
+@option{-mtune=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -march=@var{name}
 @opindex march
 This specifies the name of the target ARM architecture.  GCC uses this
@@ -10342,6 +10352,11 @@ of the @option{-mcpu=} option.  Permissible names are: @samp{armv2},
 @samp{armv7}, @samp{armv7-a}, @samp{armv7-r}, @samp{armv7-m},
 @samp{iwmmxt}, @samp{iwmmxt2}, @samp{ep9312}.
 
+@option{-march=native} causes the compiler to auto-detect the architecture
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mfpu=@var{name}
 @itemx -mfpe=@var{number}
 @itemx -mfp=@var{number}

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-08-26 16:16 [PATCH][ARM] -m{cpu,tune,arch}=native Andrew Stubbs
  2011-08-26 17:17 ` Joseph S. Myers
@ 2011-08-29  8:37 ` Michael Hope
  2011-08-30 12:22   ` Stubbs, Andrew
  1 sibling, 1 reply; 15+ messages in thread
From: Michael Hope @ 2011-08-29  8:37 UTC (permalink / raw)
  To: Andrew Stubbs; +Cc: gcc-patches, patches

On Sat, Aug 27, 2011 at 3:19 AM, Andrew Stubbs <ams@codesourcery.com> wrote:
> Hi all,
>
> This patch adds support for -mcpu=native, -mtune=native, and -march=native
> for ARM Linux hosts.
>
> So far, it only recognises Cortex-A8 and Cortex-A9, so I really need to find
> out what the magic part numbers are for other cpus before this patch is
> complete. I couldn't just find this information listed anywhere. I think
> there are a lot of clues in the kernel code, but it's hard to mine and it
> mostly only goes as far the architecture version, not the individual cpu.

Could you send linaro-dev@ an email and ask them where this API is documented?

Hmm.  Should there be a -mfpu=native that picks between soft, VFP, and
NEON based on the host?

-- Michael

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-08-29  8:37 ` Michael Hope
@ 2011-08-30 12:22   ` Stubbs, Andrew
  2011-08-30 21:36     ` Michael Hope
  0 siblings, 1 reply; 15+ messages in thread
From: Stubbs, Andrew @ 2011-08-30 12:22 UTC (permalink / raw)
  To: Michael Hope; +Cc: Stubbs, Andrew - Code Sourcery, gcc-patches, patches

On 29/08/11 04:29, Michael Hope wrote:
> On Sat, Aug 27, 2011 at 3:19 AM, Andrew Stubbs<ams@codesourcery.com>  wrote:
>> Hi all,
>>
>> This patch adds support for -mcpu=native, -mtune=native, and -march=native
>> for ARM Linux hosts.
>>
>> So far, it only recognises Cortex-A8 and Cortex-A9, so I really need to find
>> out what the magic part numbers are for other cpus before this patch is
>> complete. I couldn't just find this information listed anywhere. I think
>> there are a lot of clues in the kernel code, but it's hard to mine and it
>> mostly only goes as far the architecture version, not the individual cpu.
>
> Could you send linaro-dev@ an email and ask them where this API is documented?

API? It reads /proc/cpuinfo, and that just spits out the magic numbers 
from the processor registers. Linaro-dev might know more magic numbers 
though.

> Hmm.  Should there be a -mfpu=native that picks between soft, VFP, and
> NEON based on the host?

Yes, we could do that also, based on the CPU feature flags. I'll add it 
to my list of things to do, but save it for another patch.

Andrew

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-08-30 12:22   ` Stubbs, Andrew
@ 2011-08-30 21:36     ` Michael Hope
  0 siblings, 0 replies; 15+ messages in thread
From: Michael Hope @ 2011-08-30 21:36 UTC (permalink / raw)
  To: Stubbs, Andrew; +Cc: Stubbs, Andrew - Code Sourcery, gcc-patches, patches

On Tue, Aug 30, 2011 at 10:47 PM, Stubbs, Andrew
<Andrew_Stubbs@mentor.com> wrote:
> On 29/08/11 04:29, Michael Hope wrote:
>> On Sat, Aug 27, 2011 at 3:19 AM, Andrew Stubbs<ams@codesourcery.com>  wrote:
>>> Hi all,
>>>
>>> This patch adds support for -mcpu=native, -mtune=native, and -march=native
>>> for ARM Linux hosts.
>>>
>>> So far, it only recognises Cortex-A8 and Cortex-A9, so I really need to find
>>> out what the magic part numbers are for other cpus before this patch is
>>> complete. I couldn't just find this information listed anywhere. I think
>>> there are a lot of clues in the kernel code, but it's hard to mine and it
>>> mostly only goes as far the architecture version, not the individual cpu.
>>
>> Could you send linaro-dev@ an email and ask them where this API is documented?
>
> API? It reads /proc/cpuinfo, and that just spits out the magic numbers
> from the processor registers. Linaro-dev might know more magic numbers
> though.

Yip, most (all?) of the files under /proc are an API between user
space and the kernel and have the usual requirements of usability and
compatibility.

-- Michael

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-08-28  9:43   ` Andrew Stubbs
@ 2011-09-06 13:35     ` Andrew Stubbs
  2011-09-09 12:48       ` Richard Earnshaw
  0 siblings, 1 reply; 15+ messages in thread
From: Andrew Stubbs @ 2011-09-06 13:35 UTC (permalink / raw)
  Cc: Joseph S. Myers, gcc-patches, patches

[-- Attachment #1: Type: text/plain, Size: 501 bytes --]

This update adds many more "magic numbers" for various ARM CPUs, and 
also ensures that the implementer is ARM (as opposed to Marvell, etc.). 
The list is far from comprehensive, but it should cover many (but by no 
means all) of the cores in current use and it would not be hard to add 
support for other implementers and CPU names in future.

It has been suggested that this patch should use auxv rather than 
/proc/cpuinfo. Does anybody here have any insight/preferences?

Is the patch OK?

Andrew

[-- Attachment #2: tune-native.patch --]
[-- Type: text/x-patch, Size: 7998 bytes --]

2011-08-27  Andrew Stubbs  <ams@codesourcery.com>

	gcc/
	* config.host (arm*-*-linux*): Add driver-arm.o and x-arm.
	* config/arm/arm.opt: Add 'native' processor_type and
	arm_arch enum values.
	* config/arm/arm.h (host_detect_local_cpu): New prototype.
	(EXTRA_SPEC_FUNCTIONS): New define.
	(MCPU_MTUNE_NATIVE_SPECS): New define.
	(DRIVER_SELF_SPECS): New define.
	* config/arm/driver-arm.c: New file.
	* config/arm/x-arm: New file.
	* doc/invoke.texi (ARM Options): Document -mcpu=native,
	-mtune=native and -march=native.

--- a/gcc/config.host
+++ b/gcc/config.host
@@ -100,6 +100,14 @@ case ${host} in
 esac
 
 case ${host} in
+  arm*-*-linux*)
+    case ${target} in
+      arm*-*-*)
+	host_extra_gcc_objs="driver-arm.o"
+	host_xmake_file="${host_xmake_file} arm/x-arm"
+	;;
+    esac
+    ;;
   alpha*-*-linux* | alpha*-dec-osf*)
     case ${target} in
       alpha*-*-linux* | alpha*-dec-osf*)
--- a/gcc/config/arm/arm.h
+++ b/gcc/config/arm/arm.h
@@ -2223,4 +2223,21 @@ extern int making_const_table;
    instruction.  */
 #define MAX_LDM_STM_OPS 4
 
+/* -mcpu=native handling only makes sense with compiler running on
+   an ARM chip.  */
+#if defined(__arm__)
+extern const char *host_detect_local_cpu (int argc, const char **argv);
+# define EXTRA_SPEC_FUNCTIONS						\
+  { "local_cpu_detect", host_detect_local_cpu },
+
+# define MCPU_MTUNE_NATIVE_SPECS					\
+   " %{march=native:%<march=native %:local_cpu_detect(arch)}"		\
+   " %{mcpu=native:%<mcpu=native %:local_cpu_detect(cpu)}"		\
+   " %{mtune=native:%<mtune=native %:local_cpu_detect(tune)}"
+#else
+# define MCPU_MTUNE_NATIVE_SPECS ""
+#endif
+
+#define DRIVER_SELF_SPECS MCPU_MTUNE_NATIVE_SPECS
+
 #endif /* ! GCC_ARM_H */
--- a/gcc/config/arm/arm.opt
+++ b/gcc/config/arm/arm.opt
@@ -80,6 +80,11 @@ march=
 Target RejectNegative Joined Enum(arm_arch) Var(arm_arch_option)
 Specify the name of the target architecture
 
+; Other arm_arch values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(arm_arch) String(native) Value(-1) DriverOnly
+
 marm
 Target Report RejectNegative InverseMask(THUMB)
 Generate code in 32 bit ARM state.
@@ -233,6 +238,11 @@ mtune=
 Target RejectNegative Joined Enum(processor_type) Var(arm_tune_option) Init(arm_none)
 Tune code for the given processor
 
+; Other processor_type values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(processor_type) String(native) Value(-1) DriverOnly
+
 mwords-little-endian
 Target Report RejectNegative Mask(LITTLE_WORDS)
 Assume big endian bytes, little endian words.  This option is deprecated.
--- /dev/null
+++ b/gcc/config/arm/driver-arm.c
@@ -0,0 +1,108 @@
+/* Subroutines for the gcc driver.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+
+static struct {
+  const char *part_no;
+  const char *arch_name;
+  const char *cpu_name;
+} cpu_table[] = {
+    {"0x926", "armv5te", "arm926ej-s"},
+    {"0xa26", "armv5te", "arm1026ej-s"},
+    {"0xb02", "armv6k", "mpcore"},
+    {"0xb36", "armv6j", "arm1136j-s"},
+    {"0xb56", "armv6t2", "arm1156t2-s"},
+    {"0xb76", "armv6zk", "arm1176jz-s"},
+    {"0xc05", "armv7-a", "cortex-a5"},
+    {"0xc08", "armv7-a", "cortex-a8"},
+    {"0xc09", "armv7-a", "cortex-a9"},
+    {"0xc0f", "armv7-a", "cortex-a15"},
+    {"0xc14", "armv7-r", "cortex-r4"},
+    {"0xc15", "armv7-r", "cortex-r5"},
+    {"0xc20", "armv6-m", "cortex-m0"},
+    {"0xc21", "armv6-m", "cortex-m1"},
+    {"0xc23", "armv7-m", "cortex-m3"},
+    {"0xc24", "armv7e-m", "cortex-m4"},
+    {NULL, NULL, NULL}
+};
+
+/* This will be called by the spec parser in gcc.c when it sees
+   a %:local_cpu_detect(args) construct.  Currently it will be called
+   with either "arch", "cpu" or "tune" as argument depending on if
+   -march=native, -mcpu=native or -mtune=native is to be substituted.
+
+   It returns a string containing new command line parameters to be
+   put at the place of the above two options, depending on what CPU
+   this is executed.  E.g. "-march=armv7-a" on a Cortex-A8 for
+   -march=native.  If the routine can't detect a known processor,
+   the -march or -mtune option is discarded.
+
+   ARGC and ARGV are set depending on the actual arguments given
+   in the spec.  */
+const char *
+host_detect_local_cpu (int argc, const char **argv)
+{
+  const char *val = NULL;
+  char buf[128];
+  FILE *f;
+  bool arch;
+
+  if (argc < 1)
+    return NULL;
+
+  arch = strcmp (argv[0], "arch") == 0;
+  if (!arch && strcmp (argv[0], "cpu") != 0 && strcmp (argv[0], "tune"))
+    return NULL;
+
+  f = fopen ("/proc/cpuinfo", "r");
+  if (f == NULL)
+    return NULL;
+
+  while (fgets (buf, sizeof (buf), f) != NULL)
+    {
+      /* Ensure that CPU implementer is ARM (0x41).  */
+      if (strncmp (buf, "CPU implementer", sizeof ("CPU implementer") - 1) == 0
+	  && strstr (buf, "0x41") == NULL)
+	return NULL;
+
+      /* Detect arch/cpu.  */
+      if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
+	{
+	  int i;
+	  for (i = 0; cpu_table[i].part_no != NULL; i++)
+	    if (strstr (buf, cpu_table[i].part_no) != NULL)
+	      {
+		val = arch ? cpu_table[i].arch_name : cpu_table[i].cpu_name;
+		break;
+	      }
+	  break;
+	}
+    }
+
+  fclose (f);
+
+  if (val == NULL)
+    return NULL;
+
+  return concat ("-m", argv[0], "=", val, NULL);
+}
--- /dev/null
+++ b/gcc/config/arm/x-arm
@@ -0,0 +1,3 @@
+driver-arm.o: $(srcdir)/config/arm/driver-arm.c \
+  $(CONFIG_H) $(SYSTEM_H)
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -10319,6 +10319,11 @@ assembly code.  Permissible names are: @samp{arm2}, @samp{arm250},
 @samp{fa526}, @samp{fa626},
 @samp{fa606te}, @samp{fa626te}, @samp{fmp626}, @samp{fa726te}.
 
+@option{-mcpu=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mtune=@var{name}
 @opindex mtune
 This option is very similar to the @option{-mcpu=} option, except that
@@ -10330,6 +10335,11 @@ will generate based on the CPU specified by a @option{-mcpu=} option.
 For some ARM implementations better performance can be obtained by using
 this option.
 
+@option{-mtune=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -march=@var{name}
 @opindex march
 This specifies the name of the target ARM architecture.  GCC uses this
@@ -10343,6 +10353,11 @@ of the @option{-mcpu=} option.  Permissible names are: @samp{armv2},
 @samp{armv7}, @samp{armv7-a}, @samp{armv7-r}, @samp{armv7-m},
 @samp{iwmmxt}, @samp{iwmmxt2}, @samp{ep9312}.
 
+@option{-march=native} causes the compiler to auto-detect the architecture
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mfpu=@var{name}
 @itemx -mfpe=@var{number}
 @itemx -mfp=@var{number}

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-09-06 13:35     ` Andrew Stubbs
@ 2011-09-09 12:48       ` Richard Earnshaw
  2011-09-20 11:38         ` Andrew Stubbs
  0 siblings, 1 reply; 15+ messages in thread
From: Richard Earnshaw @ 2011-09-09 12:48 UTC (permalink / raw)
  To: Andrew Stubbs; +Cc: Joseph S. Myers, gcc-patches, patches

On 06/09/11 14:35, Andrew Stubbs wrote:
> This update adds many more "magic numbers" for various ARM CPUs, and 
> also ensures that the implementer is ARM (as opposed to Marvell, etc.). 
> The list is far from comprehensive, but it should cover many (but by no 
> means all) of the cores in current use and it would not be hard to add 
> support for other implementers and CPU names in future.
> 
> It has been suggested that this patch should use auxv rather than 
> /proc/cpuinfo. Does anybody here have any insight/preferences?
> 
> Is the patch OK?
> 
> Andrew
> 
> 
> tune-native.patch
> 
> 
> 2011-08-27  Andrew Stubbs  <ams@codesourcery.com>
> 
> 	gcc/
> 	* config.host (arm*-*-linux*): Add driver-arm.o and x-arm.
> 	* config/arm/arm.opt: Add 'native' processor_type and
> 	arm_arch enum values.
> 	* config/arm/arm.h (host_detect_local_cpu): New prototype.
> 	(EXTRA_SPEC_FUNCTIONS): New define.
> 	(MCPU_MTUNE_NATIVE_SPECS): New define.
> 	(DRIVER_SELF_SPECS): New define.
> 	* config/arm/driver-arm.c: New file.
> 	* config/arm/x-arm: New file.
> 	* doc/invoke.texi (ARM Options): Document -mcpu=native,
> 	-mtune=native and -march=native.
> 

The part number field is meaningless outside of the context of a a
specific vendor -- only taken as a pair can they refer to a specific
part.  So why is the vendor field hard-coded rather than factored into
the table of parts.

Maybe it would be better to have a table of tables, with the top-level
table being indexed by vendor id.  Something like

struct vendor_cpu {
  const char *part_no;
  const char *arch_name;
  const char *cpu_name;
};

struct all_cpus {
  const char *vendor_no;
  const struct vendor_cpu *vendor_parts;
}

struct vendor_cpu vendor_arm[] = { ... }

Now your code will allow easy addition of third-party cores.

R.

> --- a/gcc/config.host
> +++ b/gcc/config.host
> @@ -100,6 +100,14 @@ case ${host} in
>  esac
>  
>  case ${host} in
> +  arm*-*-linux*)
> +    case ${target} in
> +      arm*-*-*)
> +	host_extra_gcc_objs="driver-arm.o"
> +	host_xmake_file="${host_xmake_file} arm/x-arm"
> +	;;
> +    esac
> +    ;;
>    alpha*-*-linux* | alpha*-dec-osf*)
>      case ${target} in
>        alpha*-*-linux* | alpha*-dec-osf*)
> --- a/gcc/config/arm/arm.h
> +++ b/gcc/config/arm/arm.h
> @@ -2223,4 +2223,21 @@ extern int making_const_table;
>     instruction.  */
>  #define MAX_LDM_STM_OPS 4
>  
> +/* -mcpu=native handling only makes sense with compiler running on
> +   an ARM chip.  */
> +#if defined(__arm__)
> +extern const char *host_detect_local_cpu (int argc, const char **argv);
> +# define EXTRA_SPEC_FUNCTIONS						\
> +  { "local_cpu_detect", host_detect_local_cpu },
> +
> +# define MCPU_MTUNE_NATIVE_SPECS					\
> +   " %{march=native:%<march=native %:local_cpu_detect(arch)}"		\
> +   " %{mcpu=native:%<mcpu=native %:local_cpu_detect(cpu)}"		\
> +   " %{mtune=native:%<mtune=native %:local_cpu_detect(tune)}"
> +#else
> +# define MCPU_MTUNE_NATIVE_SPECS ""
> +#endif
> +
> +#define DRIVER_SELF_SPECS MCPU_MTUNE_NATIVE_SPECS
> +
>  #endif /* ! GCC_ARM_H */
> --- a/gcc/config/arm/arm.opt
> +++ b/gcc/config/arm/arm.opt
> @@ -80,6 +80,11 @@ march=
>  Target RejectNegative Joined Enum(arm_arch) Var(arm_arch_option)
>  Specify the name of the target architecture
>  
> +; Other arm_arch values are loaded from arm-tables.opt
> +; but that is a generated file and this is an odd-one-out.
> +EnumValue
> +Enum(arm_arch) String(native) Value(-1) DriverOnly
> +
>  marm
>  Target Report RejectNegative InverseMask(THUMB)
>  Generate code in 32 bit ARM state.
> @@ -233,6 +238,11 @@ mtune=
>  Target RejectNegative Joined Enum(processor_type) Var(arm_tune_option) Init(arm_none)
>  Tune code for the given processor
>  
> +; Other processor_type values are loaded from arm-tables.opt
> +; but that is a generated file and this is an odd-one-out.
> +EnumValue
> +Enum(processor_type) String(native) Value(-1) DriverOnly
> +
>  mwords-little-endian
>  Target Report RejectNegative Mask(LITTLE_WORDS)
>  Assume big endian bytes, little endian words.  This option is deprecated.
> --- /dev/null
> +++ b/gcc/config/arm/driver-arm.c
> @@ -0,0 +1,108 @@
> +/* Subroutines for the gcc driver.
> +   Copyright (C) 2011 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify
> +it under the terms of the GNU General Public License as published by
> +the Free Software Foundation; either version 3, or (at your option)
> +any later version.
> +
> +GCC is distributed in the hope that it will be useful,
> +but WITHOUT ANY WARRANTY; without even the implied warranty of
> +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +GNU General Public License for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#include "config.h"
> +#include "system.h"
> +#include "coretypes.h"
> +#include "tm.h"
> +
> +static struct {
> +  const char *part_no;
> +  const char *arch_name;
> +  const char *cpu_name;
> +} cpu_table[] = {
> +    {"0x926", "armv5te", "arm926ej-s"},
> +    {"0xa26", "armv5te", "arm1026ej-s"},
> +    {"0xb02", "armv6k", "mpcore"},
> +    {"0xb36", "armv6j", "arm1136j-s"},
> +    {"0xb56", "armv6t2", "arm1156t2-s"},
> +    {"0xb76", "armv6zk", "arm1176jz-s"},
> +    {"0xc05", "armv7-a", "cortex-a5"},
> +    {"0xc08", "armv7-a", "cortex-a8"},
> +    {"0xc09", "armv7-a", "cortex-a9"},
> +    {"0xc0f", "armv7-a", "cortex-a15"},
> +    {"0xc14", "armv7-r", "cortex-r4"},
> +    {"0xc15", "armv7-r", "cortex-r5"},
> +    {"0xc20", "armv6-m", "cortex-m0"},
> +    {"0xc21", "armv6-m", "cortex-m1"},
> +    {"0xc23", "armv7-m", "cortex-m3"},
> +    {"0xc24", "armv7e-m", "cortex-m4"},
> +    {NULL, NULL, NULL}
> +};
> +
> +/* This will be called by the spec parser in gcc.c when it sees
> +   a %:local_cpu_detect(args) construct.  Currently it will be called
> +   with either "arch", "cpu" or "tune" as argument depending on if
> +   -march=native, -mcpu=native or -mtune=native is to be substituted.
> +
> +   It returns a string containing new command line parameters to be
> +   put at the place of the above two options, depending on what CPU
> +   this is executed.  E.g. "-march=armv7-a" on a Cortex-A8 for
> +   -march=native.  If the routine can't detect a known processor,
> +   the -march or -mtune option is discarded.
> +
> +   ARGC and ARGV are set depending on the actual arguments given
> +   in the spec.  */
> +const char *
> +host_detect_local_cpu (int argc, const char **argv)
> +{
> +  const char *val = NULL;
> +  char buf[128];
> +  FILE *f;
> +  bool arch;
> +
> +  if (argc < 1)
> +    return NULL;
> +
> +  arch = strcmp (argv[0], "arch") == 0;
> +  if (!arch && strcmp (argv[0], "cpu") != 0 && strcmp (argv[0], "tune"))
> +    return NULL;
> +
> +  f = fopen ("/proc/cpuinfo", "r");
> +  if (f == NULL)
> +    return NULL;
> +
> +  while (fgets (buf, sizeof (buf), f) != NULL)
> +    {
> +      /* Ensure that CPU implementer is ARM (0x41).  */
> +      if (strncmp (buf, "CPU implementer", sizeof ("CPU implementer") - 1) == 0
> +	  && strstr (buf, "0x41") == NULL)
> +	return NULL;
> +
> +      /* Detect arch/cpu.  */
> +      if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
> +	{
> +	  int i;
> +	  for (i = 0; cpu_table[i].part_no != NULL; i++)
> +	    if (strstr (buf, cpu_table[i].part_no) != NULL)
> +	      {
> +		val = arch ? cpu_table[i].arch_name : cpu_table[i].cpu_name;
> +		break;
> +	      }
> +	  break;
> +	}
> +    }
> +
> +  fclose (f);
> +
> +  if (val == NULL)
> +    return NULL;
> +
> +  return concat ("-m", argv[0], "=", val, NULL);
> +}
> --- /dev/null
> +++ b/gcc/config/arm/x-arm
> @@ -0,0 +1,3 @@
> +driver-arm.o: $(srcdir)/config/arm/driver-arm.c \
> +  $(CONFIG_H) $(SYSTEM_H)
> +	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -10319,6 +10319,11 @@ assembly code.  Permissible names are: @samp{arm2}, @samp{arm250},
>  @samp{fa526}, @samp{fa626},
>  @samp{fa606te}, @samp{fa626te}, @samp{fmp626}, @samp{fa726te}.
>  
> +@option{-mcpu=native} causes the compiler to auto-detect the CPU
> +of the build computer.  At present, this feature is only supported on
> +Linux, and not all architectures are recognised.  If the auto-detect is
> +unsuccessful the option has no effect.
> +
>  @item -mtune=@var{name}
>  @opindex mtune
>  This option is very similar to the @option{-mcpu=} option, except that
> @@ -10330,6 +10335,11 @@ will generate based on the CPU specified by a @option{-mcpu=} option.
>  For some ARM implementations better performance can be obtained by using
>  this option.
>  
> +@option{-mtune=native} causes the compiler to auto-detect the CPU
> +of the build computer.  At present, this feature is only supported on
> +Linux, and not all architectures are recognised.  If the auto-detect is
> +unsuccessful the option has no effect.
> +
>  @item -march=@var{name}
>  @opindex march
>  This specifies the name of the target ARM architecture.  GCC uses this
> @@ -10343,6 +10353,11 @@ of the @option{-mcpu=} option.  Permissible names are: @samp{armv2},
>  @samp{armv7}, @samp{armv7-a}, @samp{armv7-r}, @samp{armv7-m},
>  @samp{iwmmxt}, @samp{iwmmxt2}, @samp{ep9312}.
>  
> +@option{-march=native} causes the compiler to auto-detect the architecture
> +of the build computer.  At present, this feature is only supported on
> +Linux, and not all architectures are recognised.  If the auto-detect is
> +unsuccessful the option has no effect.
> +
>  @item -mfpu=@var{name}
>  @itemx -mfpe=@var{number}
>  @itemx -mfp=@var{number}

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-09-09 12:48       ` Richard Earnshaw
@ 2011-09-20 11:38         ` Andrew Stubbs
  2011-10-13 15:08           ` Andrew Stubbs
  2011-10-17 13:42           ` Richard Earnshaw
  0 siblings, 2 replies; 15+ messages in thread
From: Andrew Stubbs @ 2011-09-20 11:38 UTC (permalink / raw)
  To: Richard Earnshaw; +Cc: Joseph S. Myers, gcc-patches, patches

[-- Attachment #1: Type: text/plain, Size: 772 bytes --]

On 09/09/11 12:55, Richard Earnshaw wrote:
> The part number field is meaningless outside of the context of a a
> specific vendor -- only taken as a pair can they refer to a specific
> part.  So why is the vendor field hard-coded rather than factored into
> the table of parts.
>
> Maybe it would be better to have a table of tables, with the top-level
> table being indexed by vendor id.  Something like

Yes, but since I only have part numbers for one vendor, I left that sort 
of thing out on the principle that it's best not to add complexity until 
you need it.

Anyway, I have done it now, so here it is. :)

I've also fixed the problem that if it didn't recognise the CPU, it 
defaulted to the hard default, ignoring the --with-cpu configured default.

OK?

Andrew

[-- Attachment #2: tune-native.patch --]
[-- Type: text/x-patch, Size: 9185 bytes --]

2011-09-20  Andrew Stubbs  <ams@codesourcery.com>

	gcc/
	* config.host (arm*-*-linux*): Add driver-arm.o and x-arm.
	* config/arm/arm.opt: Add 'native' processor_type and
	arm_arch enum values.
	* config/arm/arm.h (host_detect_local_cpu): New prototype.
	(EXTRA_SPEC_FUNCTIONS): New define.
	(MCPU_MTUNE_NATIVE_SPECS): New define.
	(DRIVER_SELF_SPECS): New define.
	* config/arm/driver-arm.c: New file.
	* config/arm/x-arm: New file.
	* doc/invoke.texi (ARM Options): Document -mcpu=native,
	-mtune=native and -march=native.

--- a/gcc/config.host
+++ b/gcc/config.host
@@ -100,6 +100,14 @@ case ${host} in
 esac
 
 case ${host} in
+  arm*-*-linux*)
+    case ${target} in
+      arm*-*-*)
+	host_extra_gcc_objs="driver-arm.o"
+	host_xmake_file="${host_xmake_file} arm/x-arm"
+	;;
+    esac
+    ;;
   alpha*-*-linux* | alpha*-dec-osf*)
     case ${target} in
       alpha*-*-linux* | alpha*-dec-osf*)
--- a/gcc/config/arm/arm.h
+++ b/gcc/config/arm/arm.h
@@ -2228,4 +2228,21 @@ extern int making_const_table;
    " %{mcpu=generic-*:-march=%*;"				\
    "   :%{mcpu=*:-mcpu=%*} %{march=*:-march=%*}}"
 
+/* -mcpu=native handling only makes sense with compiler running on
+   an ARM chip.  */
+#if defined(__arm__)
+extern const char *host_detect_local_cpu (int argc, const char **argv);
+# define EXTRA_SPEC_FUNCTIONS						\
+  { "local_cpu_detect", host_detect_local_cpu },
+
+# define MCPU_MTUNE_NATIVE_SPECS					\
+   " %{march=native:%<march=native %:local_cpu_detect(arch)}"		\
+   " %{mcpu=native:%<mcpu=native %:local_cpu_detect(cpu)}"		\
+   " %{mtune=native:%<mtune=native %:local_cpu_detect(tune)}"
+#else
+# define MCPU_MTUNE_NATIVE_SPECS ""
+#endif
+
+#define DRIVER_SELF_SPECS MCPU_MTUNE_NATIVE_SPECS
+
 #endif /* ! GCC_ARM_H */
--- a/gcc/config/arm/arm.opt
+++ b/gcc/config/arm/arm.opt
@@ -80,6 +80,11 @@ march=
 Target RejectNegative Joined Enum(arm_arch) Var(arm_arch_option)
 Specify the name of the target architecture
 
+; Other arm_arch values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(arm_arch) String(native) Value(-1) DriverOnly
+
 marm
 Target Report RejectNegative InverseMask(THUMB)
 Generate code in 32 bit ARM state.
@@ -233,6 +238,11 @@ mtune=
 Target RejectNegative Joined Enum(processor_type) Var(arm_tune_option) Init(arm_none)
 Tune code for the given processor
 
+; Other processor_type values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(processor_type) String(native) Value(-1) DriverOnly
+
 mwords-little-endian
 Target Report RejectNegative Mask(LITTLE_WORDS)
 Assume big endian bytes, little endian words.  This option is deprecated.
--- /dev/null
+++ b/gcc/config/arm/driver-arm.c
@@ -0,0 +1,145 @@
+/* Subroutines for the gcc driver.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "configargs.h"
+
+struct vendor_cpu {
+  const char *part_no;
+  const char *arch_name;
+  const char *cpu_name;
+};
+
+static struct vendor_cpu arm_cpu_table[] = {
+    {"0x926", "armv5te", "arm926ej-s"},
+    {"0xa26", "armv5te", "arm1026ej-s"},
+    {"0xb02", "armv6k", "mpcore"},
+    {"0xb36", "armv6j", "arm1136j-s"},
+    {"0xb56", "armv6t2", "arm1156t2-s"},
+    {"0xb76", "armv6zk", "arm1176jz-s"},
+    {"0xc05", "armv7-a", "cortex-a5"},
+    {"0xc08", "armv7-a", "cortex-a8"},
+    {"0xc09", "armv7-a", "cortex-a9"},
+    {"0xc0f", "armv7-a", "cortex-a15"},
+    {"0xc14", "armv7-r", "cortex-r4"},
+    {"0xc15", "armv7-r", "cortex-r5"},
+    {"0xc20", "armv6-m", "cortex-m0"},
+    {"0xc21", "armv6-m", "cortex-m1"},
+    {"0xc23", "armv7-m", "cortex-m3"},
+    {"0xc24", "armv7e-m", "cortex-m4"},
+    {NULL, NULL, NULL}
+};
+
+struct {
+  const char *vendor_no;
+  const struct vendor_cpu *vendor_parts;
+} vendors[] = {
+    {"0x41", arm_cpu_table},
+    {NULL, NULL}
+};
+
+/* This will be called by the spec parser in gcc.c when it sees
+   a %:local_cpu_detect(args) construct.  Currently it will be called
+   with either "arch", "cpu" or "tune" as argument depending on if
+   -march=native, -mcpu=native or -mtune=native is to be substituted.
+
+   It returns a string containing new command line parameters to be
+   put at the place of the above two options, depending on what CPU
+   this is executed.  E.g. "-march=armv7-a" on a Cortex-A8 for
+   -march=native.  If the routine can't detect a known processor,
+   the -march or -mtune option is discarded.
+
+   ARGC and ARGV are set depending on the actual arguments given
+   in the spec.  */
+const char *
+host_detect_local_cpu (int argc, const char **argv)
+{
+  const char *val = NULL;
+  char buf[128];
+  FILE *f;
+  bool arch;
+  const struct vendor_cpu *cpu_table = NULL;
+
+  if (argc < 1)
+    goto not_found;
+
+  arch = strcmp (argv[0], "arch") == 0;
+  if (!arch && strcmp (argv[0], "cpu") != 0 && strcmp (argv[0], "tune"))
+    goto not_found;
+
+  f = fopen ("/proc/cpuinfo", "r");
+  if (f == NULL)
+    goto not_found;
+
+  while (fgets (buf, sizeof (buf), f) != NULL)
+    {
+      /* Ensure that CPU implementer is ARM (0x41).  */
+      if (strncmp (buf, "CPU implementer", sizeof ("CPU implementer") - 1) == 0)
+	{
+	  int i;
+	  for (i = 0; vendors[i].vendor_no != NULL; i++)
+	    if (strstr (buf, vendors[i].vendor_no) != NULL)
+	      {
+		cpu_table = vendors[i].vendor_parts;
+		break;
+	      }
+	}
+
+      /* Detect arch/cpu.  */
+      if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
+	{
+	  int i;
+
+	  if (cpu_table == NULL)
+	    goto not_found;
+
+	  for (i = 0; cpu_table[i].part_no != NULL; i++)
+	    if (strstr (buf, cpu_table[i].part_no) != NULL)
+	      {
+		val = arch ? cpu_table[i].arch_name : cpu_table[i].cpu_name;
+		break;
+	      }
+	  break;
+	}
+    }
+
+  fclose (f);
+
+  if (val == NULL)
+    goto not_found;
+
+  return concat ("-m", argv[0], "=", val, NULL);
+
+not_found:
+  {
+    unsigned int i;
+    unsigned int opt;
+    const char *search[] = {NULL, "arch"};
+    search[0] = argv[0];
+    for (opt = 0; opt < ARRAY_SIZE (search); opt++)
+      for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
+	if (strcmp (configure_default_options[i].name, search[opt]) == 0)
+	  return concat ("-m", search[opt], "=",
+			 configure_default_options[i].value, NULL);
+    return NULL;
+  }
+}
--- /dev/null
+++ b/gcc/config/arm/x-arm
@@ -0,0 +1,3 @@
+driver-arm.o: $(srcdir)/config/arm/driver-arm.c \
+  $(CONFIG_H) $(SYSTEM_H)
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -10329,10 +10329,16 @@ assembly code.  Permissible names are: @samp{arm2}, @samp{arm250},
 @samp{fa526}, @samp{fa626},
 @samp{fa606te}, @samp{fa626te}, @samp{fmp626}, @samp{fa726te}.
 
+
 @option{-mcpu=generic-@var{arch}} is also permissible, and is
 equivalent to @option{-march=@var{arch} -mtune=generic-@var{arch}}.
 See @option{-mtune} for more information.
 
+@option{-mcpu=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mtune=@var{name}
 @opindex mtune
 This option is very similar to the @option{-mcpu=} option, except that
@@ -10351,6 +10357,11 @@ processors, balancing between optimizations that benefit some CPUs in the
 range, and avoiding performance pitfalls of other CPUs.  The effects of
 this option may change in future GCC versions as CPU models come and go.
 
+@option{-mtune=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -march=@var{name}
 @opindex march
 This specifies the name of the target ARM architecture.  GCC uses this
@@ -10364,6 +10375,11 @@ of the @option{-mcpu=} option.  Permissible names are: @samp{armv2},
 @samp{armv7}, @samp{armv7-a}, @samp{armv7-r}, @samp{armv7-m},
 @samp{iwmmxt}, @samp{iwmmxt2}, @samp{ep9312}.
 
+@option{-march=native} causes the compiler to auto-detect the architecture
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mfpu=@var{name}
 @itemx -mfpe=@var{number}
 @itemx -mfp=@var{number}

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-09-20 11:38         ` Andrew Stubbs
@ 2011-10-13 15:08           ` Andrew Stubbs
  2011-10-17 13:42           ` Richard Earnshaw
  1 sibling, 0 replies; 15+ messages in thread
From: Andrew Stubbs @ 2011-10-13 15:08 UTC (permalink / raw)
  Cc: Richard Earnshaw, Joseph S. Myers, gcc-patches, patches

Ping.

On 20/09/11 11:51, Andrew Stubbs wrote:
> On 09/09/11 12:55, Richard Earnshaw wrote:
>> The part number field is meaningless outside of the context of a a
>> specific vendor -- only taken as a pair can they refer to a specific
>> part. So why is the vendor field hard-coded rather than factored into
>> the table of parts.
>>
>> Maybe it would be better to have a table of tables, with the top-level
>> table being indexed by vendor id. Something like
>
> Yes, but since I only have part numbers for one vendor, I left that sort
> of thing out on the principle that it's best not to add complexity until
> you need it.
>
> Anyway, I have done it now, so here it is. :)
>
> I've also fixed the problem that if it didn't recognise the CPU, it
> defaulted to the hard default, ignoring the --with-cpu configured default.
>
> OK?
>
> Andrew

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-09-20 11:38         ` Andrew Stubbs
  2011-10-13 15:08           ` Andrew Stubbs
@ 2011-10-17 13:42           ` Richard Earnshaw
  2011-10-18 14:43             ` Andrew Stubbs
  1 sibling, 1 reply; 15+ messages in thread
From: Richard Earnshaw @ 2011-10-17 13:42 UTC (permalink / raw)
  To: Andrew Stubbs; +Cc: Joseph S. Myers, gcc-patches, patches

On 20/09/11 11:51, Andrew Stubbs wrote:
> On 09/09/11 12:55, Richard Earnshaw wrote:
>> The part number field is meaningless outside of the context of a a
>> specific vendor -- only taken as a pair can they refer to a specific
>> part.  So why is the vendor field hard-coded rather than factored into
>> the table of parts.
>>
>> Maybe it would be better to have a table of tables, with the top-level
>> table being indexed by vendor id.  Something like
> 
> Yes, but since I only have part numbers for one vendor, I left that sort 
> of thing out on the principle that it's best not to add complexity until 
> you need it.
> 
> Anyway, I have done it now, so here it is. :)
> 
> I've also fixed the problem that if it didn't recognise the CPU, it 
> defaulted to the hard default, ignoring the --with-cpu configured default.
> 
> OK?
> 
> Andrew
> 
> 
> tune-native.patch
> 
> 
> 2011-09-20  Andrew Stubbs  <ams@codesourcery.com>
> 
> 	gcc/
> 	* config.host (arm*-*-linux*): Add driver-arm.o and x-arm.
> 	* config/arm/arm.opt: Add 'native' processor_type and
> 	arm_arch enum values.
> 	* config/arm/arm.h (host_detect_local_cpu): New prototype.
> 	(EXTRA_SPEC_FUNCTIONS): New define.
> 	(MCPU_MTUNE_NATIVE_SPECS): New define.
> 	(DRIVER_SELF_SPECS): New define.
> 	* config/arm/driver-arm.c: New file.
> 	* config/arm/x-arm: New file.
> 	* doc/invoke.texi (ARM Options): Document -mcpu=native,
> 	-mtune=native and -march=native.
> 

There's a presumption in host_detect_local_cpu() that "CPU implementer"
will appear before  "CPU part" in the output of /proc/cpuinfo.  That's
probably a pretty safe assumption (and it appears that it will handle
that case relatively safely -- ie not crash).  I'll let you decide
whether that's important enough to fix.

However another problem I've just spotted is that you never close the
file descriptor if you fail to parse the output properly (ie jump to
not_found).  That should be fixed before this is committed.

OK with the second issue resolved.


R.

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-10-17 13:42           ` Richard Earnshaw
@ 2011-10-18 14:43             ` Andrew Stubbs
  2011-10-18 14:51               ` Richard Earnshaw
  0 siblings, 1 reply; 15+ messages in thread
From: Andrew Stubbs @ 2011-10-18 14:43 UTC (permalink / raw)
  To: Richard Earnshaw; +Cc: Joseph S. Myers, gcc-patches, patches

[-- Attachment #1: Type: text/plain, Size: 984 bytes --]

On 17/10/11 14:24, Richard Earnshaw wrote:
> There's a presumption in host_detect_local_cpu() that "CPU implementer"
> will appear before  "CPU part" in the output of /proc/cpuinfo.  That's
> probably a pretty safe assumption (and it appears that it will handle
> that case relatively safely -- ie not crash).  I'll let you decide
> whether that's important enough to fix.

Yes, I was aware of that. I believe it wouldn't be worth the effort 
required to fix it, and it would only complicate the code for no advantage.

If, at some point in the future, the format of /proc/cpuinfo changes 
enough to break this assumption then it'll probably be the least of the 
problems needing fixing.

> However another problem I've just spotted is that you never close the
> file descriptor if you fail to parse the output properly (ie jump to
> not_found).  That should be fixed before this is committed.
>
> OK with the second issue resolved.

Thanks, fixed and committed, as attached.

Andrew

[-- Attachment #2: tune-native.patch --]
[-- Type: text/x-patch, Size: 9124 bytes --]

2011-10-18  Andrew Stubbs  <ams@codesourcery.com>

	gcc/
	* config.host (arm*-*-linux*): Add driver-arm.o and x-arm.
	* config/arm/arm.opt: Add 'native' processor_type and
	arm_arch enum values.
	* config/arm/arm.h (host_detect_local_cpu): New prototype.
	(EXTRA_SPEC_FUNCTIONS): New define.
	(MCPU_MTUNE_NATIVE_SPECS): New define.
	(DRIVER_SELF_SPECS): New define.
	* config/arm/driver-arm.c: New file.
	* config/arm/x-arm: New file.
	* doc/invoke.texi (ARM Options): Document -mcpu=native,
	-mtune=native and -march=native.

--- a/gcc/config.host
+++ b/gcc/config.host
@@ -100,6 +100,14 @@ case ${host} in
 esac
 
 case ${host} in
+  arm*-*-linux*)
+    case ${target} in
+      arm*-*-*)
+	host_extra_gcc_objs="driver-arm.o"
+	host_xmake_file="${host_xmake_file} arm/x-arm"
+	;;
+    esac
+    ;;
   alpha*-*-linux* | alpha*-dec-osf*)
     case ${target} in
       alpha*-*-linux* | alpha*-dec-osf*)
--- a/gcc/config/arm/arm.h
+++ b/gcc/config/arm/arm.h
@@ -2255,4 +2255,21 @@ extern int making_const_table;
     }									\
   while (0)
 
+/* -mcpu=native handling only makes sense with compiler running on
+   an ARM chip.  */
+#if defined(__arm__)
+extern const char *host_detect_local_cpu (int argc, const char **argv);
+# define EXTRA_SPEC_FUNCTIONS						\
+  { "local_cpu_detect", host_detect_local_cpu },
+
+# define MCPU_MTUNE_NATIVE_SPECS					\
+   " %{march=native:%<march=native %:local_cpu_detect(arch)}"		\
+   " %{mcpu=native:%<mcpu=native %:local_cpu_detect(cpu)}"		\
+   " %{mtune=native:%<mtune=native %:local_cpu_detect(tune)}"
+#else
+# define MCPU_MTUNE_NATIVE_SPECS ""
+#endif
+
+#define DRIVER_SELF_SPECS MCPU_MTUNE_NATIVE_SPECS
+
 #endif /* ! GCC_ARM_H */
--- a/gcc/config/arm/arm.opt
+++ b/gcc/config/arm/arm.opt
@@ -80,6 +80,11 @@ march=
 Target RejectNegative Joined Enum(arm_arch) Var(arm_arch_option)
 Specify the name of the target architecture
 
+; Other arm_arch values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(arm_arch) String(native) Value(-1) DriverOnly
+
 marm
 Target Report RejectNegative InverseMask(THUMB)
 Generate code in 32 bit ARM state.
@@ -233,6 +238,11 @@ mtune=
 Target RejectNegative Joined Enum(processor_type) Var(arm_tune_option) Init(arm_none)
 Tune code for the given processor
 
+; Other processor_type values are loaded from arm-tables.opt
+; but that is a generated file and this is an odd-one-out.
+EnumValue
+Enum(processor_type) String(native) Value(-1) DriverOnly
+
 mwords-little-endian
 Target Report RejectNegative Mask(LITTLE_WORDS)
 Assume big endian bytes, little endian words.  This option is deprecated.
--- /dev/null
+++ b/gcc/config/arm/driver-arm.c
@@ -0,0 +1,145 @@
+/* Subroutines for the gcc driver.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "configargs.h"
+
+struct vendor_cpu {
+  const char *part_no;
+  const char *arch_name;
+  const char *cpu_name;
+};
+
+static struct vendor_cpu arm_cpu_table[] = {
+    {"0x926", "armv5te", "arm926ej-s"},
+    {"0xa26", "armv5te", "arm1026ej-s"},
+    {"0xb02", "armv6k", "mpcore"},
+    {"0xb36", "armv6j", "arm1136j-s"},
+    {"0xb56", "armv6t2", "arm1156t2-s"},
+    {"0xb76", "armv6zk", "arm1176jz-s"},
+    {"0xc05", "armv7-a", "cortex-a5"},
+    {"0xc08", "armv7-a", "cortex-a8"},
+    {"0xc09", "armv7-a", "cortex-a9"},
+    {"0xc0f", "armv7-a", "cortex-a15"},
+    {"0xc14", "armv7-r", "cortex-r4"},
+    {"0xc15", "armv7-r", "cortex-r5"},
+    {"0xc20", "armv6-m", "cortex-m0"},
+    {"0xc21", "armv6-m", "cortex-m1"},
+    {"0xc23", "armv7-m", "cortex-m3"},
+    {"0xc24", "armv7e-m", "cortex-m4"},
+    {NULL, NULL, NULL}
+};
+
+struct {
+  const char *vendor_no;
+  const struct vendor_cpu *vendor_parts;
+} vendors[] = {
+    {"0x41", arm_cpu_table},
+    {NULL, NULL}
+};
+
+/* This will be called by the spec parser in gcc.c when it sees
+   a %:local_cpu_detect(args) construct.  Currently it will be called
+   with either "arch", "cpu" or "tune" as argument depending on if
+   -march=native, -mcpu=native or -mtune=native is to be substituted.
+
+   It returns a string containing new command line parameters to be
+   put at the place of the above two options, depending on what CPU
+   this is executed.  E.g. "-march=armv7-a" on a Cortex-A8 for
+   -march=native.  If the routine can't detect a known processor,
+   the -march or -mtune option is discarded.
+
+   ARGC and ARGV are set depending on the actual arguments given
+   in the spec.  */
+const char *
+host_detect_local_cpu (int argc, const char **argv)
+{
+  const char *val = NULL;
+  char buf[128];
+  FILE *f;
+  bool arch;
+  const struct vendor_cpu *cpu_table = NULL;
+
+  if (argc < 1)
+    goto not_found;
+
+  arch = strcmp (argv[0], "arch") == 0;
+  if (!arch && strcmp (argv[0], "cpu") != 0 && strcmp (argv[0], "tune"))
+    goto not_found;
+
+  f = fopen ("/proc/cpuinfo", "r");
+  if (f == NULL)
+    goto not_found;
+
+  while (fgets (buf, sizeof (buf), f) != NULL)
+    {
+      /* Ensure that CPU implementer is ARM (0x41).  */
+      if (strncmp (buf, "CPU implementer", sizeof ("CPU implementer") - 1) == 0)
+	{
+	  int i;
+	  for (i = 0; vendors[i].vendor_no != NULL; i++)
+	    if (strstr (buf, vendors[i].vendor_no) != NULL)
+	      {
+		cpu_table = vendors[i].vendor_parts;
+		break;
+	      }
+	}
+
+      /* Detect arch/cpu.  */
+      if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
+	{
+	  int i;
+
+	  if (cpu_table == NULL)
+	    goto not_found;
+
+	  for (i = 0; cpu_table[i].part_no != NULL; i++)
+	    if (strstr (buf, cpu_table[i].part_no) != NULL)
+	      {
+		val = arch ? cpu_table[i].arch_name : cpu_table[i].cpu_name;
+		break;
+	      }
+	  break;
+	}
+    }
+
+  fclose (f);
+
+  if (val == NULL)
+    goto not_found;
+
+  return concat ("-m", argv[0], "=", val, NULL);
+
+not_found:
+  {
+    unsigned int i;
+    unsigned int opt;
+    const char *search[] = {NULL, "arch"};
+    search[0] = argv[0];
+    for (opt = 0; opt < ARRAY_SIZE (search); opt++)
+      for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
+	if (strcmp (configure_default_options[i].name, search[opt]) == 0)
+	  return concat ("-m", search[opt], "=",
+			 configure_default_options[i].value, NULL);
+    return NULL;
+  }
+}
--- /dev/null
+++ b/gcc/config/arm/x-arm
@@ -0,0 +1,3 @@
+driver-arm.o: $(srcdir)/config/arm/driver-arm.c \
+  $(CONFIG_H) $(SYSTEM_H)
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -10397,10 +10397,16 @@ assembly code.  Permissible names are: @samp{arm2}, @samp{arm250},
 @samp{fa526}, @samp{fa626},
 @samp{fa606te}, @samp{fa626te}, @samp{fmp626}, @samp{fa726te}.
 
+
 @option{-mcpu=generic-@var{arch}} is also permissible, and is
 equivalent to @option{-march=@var{arch} -mtune=generic-@var{arch}}.
 See @option{-mtune} for more information.
 
+@option{-mcpu=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mtune=@var{name}
 @opindex mtune
 This option is very similar to the @option{-mcpu=} option, except that
@@ -10419,6 +10425,11 @@ processors, balancing between optimizations that benefit some CPUs in the
 range, and avoiding performance pitfalls of other CPUs.  The effects of
 this option may change in future GCC versions as CPU models come and go.
 
+@option{-mtune=native} causes the compiler to auto-detect the CPU
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -march=@var{name}
 @opindex march
 This specifies the name of the target ARM architecture.  GCC uses this
@@ -10432,6 +10443,11 @@ of the @option{-mcpu=} option.  Permissible names are: @samp{armv2},
 @samp{armv7}, @samp{armv7-a}, @samp{armv7-r}, @samp{armv7-m},
 @samp{iwmmxt}, @samp{iwmmxt2}, @samp{ep9312}.
 
+@option{-march=native} causes the compiler to auto-detect the architecture
+of the build computer.  At present, this feature is only supported on
+Linux, and not all architectures are recognised.  If the auto-detect is
+unsuccessful the option has no effect.
+
 @item -mfpu=@var{name}
 @itemx -mfpe=@var{number}
 @itemx -mfp=@var{number}

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-10-18 14:43             ` Andrew Stubbs
@ 2011-10-18 14:51               ` Richard Earnshaw
  2011-10-18 15:19                 ` Andrew Stubbs
  0 siblings, 1 reply; 15+ messages in thread
From: Richard Earnshaw @ 2011-10-18 14:51 UTC (permalink / raw)
  To: Andrew Stubbs; +Cc: Joseph S. Myers, gcc-patches, patches

On 18/10/11 15:23, Andrew Stubbs wrote:
> +      /* Detect arch/cpu.  */
> +      if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
> +	{
> +	  int i;
> +
> +	  if (cpu_table == NULL)
> +	    goto not_found;
> +

Which still jumps to not_found without closing f.

R.

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-10-18 14:51               ` Richard Earnshaw
@ 2011-10-18 15:19                 ` Andrew Stubbs
  2011-10-19 12:06                   ` Andrew Stubbs
  0 siblings, 1 reply; 15+ messages in thread
From: Andrew Stubbs @ 2011-10-18 15:19 UTC (permalink / raw)
  To: Richard Earnshaw; +Cc: Andrew Stubbs, Joseph S. Myers, gcc-patches, patches

On 18/10/11 15:34, Richard Earnshaw wrote:
> On 18/10/11 15:23, Andrew Stubbs wrote:
>> +      /* Detect arch/cpu.  */
>> +      if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
>> +	{
>> +	  int i;
>> +
>> +	  if (cpu_table == NULL)
>> +	    goto not_found;
>> +
>
> Which still jumps to not_found without closing f.

Hmmm, I know I fixed that, I know I did!

But I appear to have lost the change somewhere when I updated my checkout?

I'll fix it now.

Andrew

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

* Re: [PATCH][ARM] -m{cpu,tune,arch}=native
  2011-10-18 15:19                 ` Andrew Stubbs
@ 2011-10-19 12:06                   ` Andrew Stubbs
  0 siblings, 0 replies; 15+ messages in thread
From: Andrew Stubbs @ 2011-10-19 12:06 UTC (permalink / raw)
  To: Andrew Stubbs; +Cc: Richard Earnshaw, Joseph S. Myers, gcc-patches, patches

[-- Attachment #1: Type: text/plain, Size: 311 bytes --]

On 18/10/11 15:42, Andrew Stubbs wrote:
>> Which still jumps to not_found without closing f.
>
> Hmmm, I know I fixed that, I know I did!
>
> But I appear to have lost the change somewhere when I updated my checkout?
>
> I'll fix it now.

Fixed and committed as attached.

Apologies for the cock-up. :(

Andrew

[-- Attachment #2: tune-native-fix.patch --]
[-- Type: text/x-patch, Size: 1001 bytes --]

2011-10-18  Andrew Stubbs  <ams@codesourcery.com>

	* config/arm/driver-arm.c (host_detect_local_cpu): Close the file
	before exiting.

---
 src/gcc-mainline/gcc/config/arm/driver-arm.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/src/gcc-mainline/gcc/config/arm/driver-arm.c b/src/gcc-mainline/gcc/config/arm/driver-arm.c
index 43b6e58..9a6762b 100644
--- a/src/gcc-mainline/gcc/config/arm/driver-arm.c
+++ b/src/gcc-mainline/gcc/config/arm/driver-arm.c
@@ -75,7 +75,7 @@ host_detect_local_cpu (int argc, const char **argv)
 {
   const char *val = NULL;
   char buf[128];
-  FILE *f;
+  FILE *f = NULL;
   bool arch;
   const struct vendor_cpu *cpu_table = NULL;
 
@@ -134,6 +134,10 @@ not_found:
     unsigned int i;
     unsigned int opt;
     const char *search[] = {NULL, "arch"};
+
+    if (f)
+      fclose (f);
+
     search[0] = argv[0];
     for (opt = 0; opt < ARRAY_SIZE (search); opt++)
       for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)

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

end of thread, other threads:[~2011-10-19 10:01 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-26 16:16 [PATCH][ARM] -m{cpu,tune,arch}=native Andrew Stubbs
2011-08-26 17:17 ` Joseph S. Myers
2011-08-28  9:43   ` Andrew Stubbs
2011-09-06 13:35     ` Andrew Stubbs
2011-09-09 12:48       ` Richard Earnshaw
2011-09-20 11:38         ` Andrew Stubbs
2011-10-13 15:08           ` Andrew Stubbs
2011-10-17 13:42           ` Richard Earnshaw
2011-10-18 14:43             ` Andrew Stubbs
2011-10-18 14:51               ` Richard Earnshaw
2011-10-18 15:19                 ` Andrew Stubbs
2011-10-19 12:06                   ` Andrew Stubbs
2011-08-29  8:37 ` Michael Hope
2011-08-30 12:22   ` Stubbs, Andrew
2011-08-30 21:36     ` Michael Hope

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