diff mbox

[ARM] -m{cpu,tune,arch}=native

Message ID 4E57B995.2010605@codesourcery.com
State New
Headers show

Commit Message

Andrew Stubbs Aug. 26, 2011, 3:19 p.m. UTC
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

Comments

Joseph Myers Aug. 26, 2011, 4:16 p.m. UTC | #1
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.
Michael Hope Aug. 29, 2011, 3:29 a.m. UTC | #2
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
Andrew Stubbs Aug. 30, 2011, 10:47 a.m. UTC | #3
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
Michael Hope Aug. 30, 2011, 7:45 p.m. UTC | #4
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
diff mbox

Patch

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}