Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make gcc uses march=native as default?

Tags:

gcc

gcc4

Is there a way to change the specs file so that it will pass -march=native if nothing is specified in command line?

Related things in the default specs file is:

*cc1:
%(cc1_cpu)

*cc1_cpu:
%{march=native:%>march=native %:local_cpu_detect(arch)   %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)}} %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}

I am not sure how specs works. Simply specifying -march=native before or after %(cc1_cpu) doesn't work. However, this line does take effect because GCC will report error if I put -something_wierd instead of -march=native.

Another thing I noticed is if I put %{march=i386:-something_wierd} before %(cc1_cpu), gcc reports error so looks like -march=i386 is always passed in if nothing is specified, so is there a way to distinguish between nothing specified and -march=i386 in specs file?

BTW, what does %> do? Seems like it is not specified in the documentation.

I am using MinGW's gcc-4.6.2.

like image 768
Kan Li Avatar asked Jan 02 '12 22:01

Kan Li


1 Answers

Referring to your last question: The gcc 4.6.1 sources (gcc/gcc.c) contain the following comment on %>:

 %>S    Similar to "%<S", but keep it in the GCC command line.

For the sake of completeness following the comment for %< form the same file:

 %<S    remove all occurrences of -S from the command line.
        Note - this command is position dependent.  % commands in the
        spec string before this one will see -S, % commands in the
        spec string after this one will not.

To answer the first question in short: yes, but ....

... the only generic solution I found has the significant drawback that the -march option will be ignored, so every build is done as if -march=native had been specified. Anyhow there is a workaround to that.

1 The solution (without workaround)

Create a specs-file called let's say specs.nativealways containing:

*cc1_cpu:
%<march=* -march=native %>march=native %:local_cpu_detect(arch) %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)} %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}

When using the specs-file (for example by invoking gcc with the option -specs=specs.nativealways) the build will be done as if -march=native was specified (with the mentioned drawback that any occurrence of option -march=<arch> would have simply been ignored).

2 The workaround

To still by able to override the newly configured default behavior one can use a modified version of the specs-file described above, introducing a new option called -myarch using the same syntax as -march (except for -myarch=native, which won't work, which does not metter as native now is the default).

The modfied specs-file looks like this:

*cc1_cpu:
%<march=* %{myarch=*:%<myarch* -march=%* ; :-march=native %>march=native %:local_cpu_detect(arch) %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)}}  %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}

PS: This has been tested with with gcc 4.6.2 on Linux, but should work on MinGW.

like image 132
alk Avatar answered Nov 11 '22 18:11

alk