Question

I'm compiling my C++ app using GCC 4.3. Instead of manually selecting the optimization flags I'm using -march=native, which in theory should add all optimization flags applicable to the hardware I'm compiling on. But how can I check which flags is it actually using?

Was it helpful?

Solution

You can use the -Q --help=target options:

gcc -march=native -Q --help=target ...

The -v option may also be of use.

You can see the documentation on the --help option here.

OTHER TIPS

To see command-line flags, use:

gcc -march=native -E -v - </dev/null 2>&1 | grep cc1

If you want to see the compiler/precompiler defines set by certain parameters, do this:

echo | gcc -dM -E - -march=native

It should be (-### is similar to -v):

echo | gcc -### -E - -march=native 

To show the "real" native flags for gcc.

You can make them appear more "clearly" with a command:

gcc -### -E - -march=native 2>&1 | sed -r '/cc1/!d;s/(")|(^.* - )//g'

and you can get rid of flags with -mno-* with:

gcc -### -E - -march=native 2>&1 | sed -r '/cc1/!d;s/(")|(^.* - )|( -mno-[^\ ]+)//g'

If you want to find out how to set-up a non-native cross compile, I found this useful:

On the target machine,

% gcc -march=native -Q --help=target | grep march
-march=                               core-avx-i

Then use this on the build machine:

% gcc -march=core-avx-i ...

I'm going to throw my two cents into this question and suggest a slightly more verbose extension of elias's answer. As of gcc 4.6, running of gcc -march=native -v -E - < /dev/null emits an increasing amount of spam in the form of superfluous -mno-* flags. The following will strip these:

gcc -march=native -v -E - < /dev/null 2>&1 | grep cc1 | perl -pe 's/ -mno-\S+//g; s/^.* - //g;'

However, I have only verified the correctness of this on two different CPUs (an Intel Core2 and AMD Phenom), so I suggest also running the following script to be sure that all of these -mno-* flags can be safely stripped.

#!/bin/bash

gcc_cmd="gcc"

# Optionally supply path to gcc as first argument
if (($#)); then
    gcc_cmd="$1"
fi

with_mno=$(
    "${gcc_cmd}" -march=native -mtune=native -v -E - < /dev/null 2>&1 |
    grep cc1 |
    perl -pe 's/^.* - //g;'
)
without_mno=$(echo "${with_mno}" | perl -pe 's/ -mno-\S+//g;')

"${gcc_cmd}" ${with_mno}    -dM -E - < /dev/null > /tmp/gcctest.a.$$
"${gcc_cmd}" ${without_mno} -dM -E - < /dev/null > /tmp/gcctest.b.$$

if diff -u /tmp/gcctest.{a,b}.$$; then
    echo "Safe to strip -mno-* options."
else
    echo
    echo "WARNING! Some -mno-* options are needed!"
    exit 1
fi

rm /tmp/gcctest.{a,b}.$$

I haven't found a difference between gcc -march=native -v -E - < /dev/null and gcc -march=native -### -E - < /dev/null other than some parameters being quoted -- and parameters that contain no special characters, so I'm not sure under what circumstances this makes any real difference.

Finally, note that --march=native was introduced in gcc 4.2, prior to which it is just an unrecognized argument.

I wrote a script that will produce a flag list ready to be fed back into gcc from the output of gcc -march=native -mtune=native -Q --help=target. It's a little rough, not efficient, but it seems to do the trick.

There's one caveat I am aware of: options with an equal sign (=) and no value are removed, intentionally.

gcc -march=native -mtune=native -Q --help=target -v 2>&1 \
| grep -h "The following options" -A200 -B0 \
| tail -n +2 \
| grep -h "Known assembler" -A0 -B999 \
| head -n -2 \
| grep -v "disabled" \
| sed -r 's/\[(enabled|default)\]//g'\
| sed -r 's/\s*//g' \
| sed -r 's/\=$//g' \
| sed -r 's/<.*>//g' \
| xargs

You can remove the xargs at the end to see the flags line by line.

Edit: Including an example output. On my machine (Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz ), this gives me:

-m128bit-long-double -m64 -m80387 -mabi=sysv -mabm -maddress-mode=long -maes -malign-data=compat -malign-functions=0 -malign-jumps=0 -malign-loops=0 -malign-stringops -march=haswell -masm=att -mavx -mavx2 -mbmi -mbmi2 -mbranch-cost=3 -mcmodel -mcpu -mcx16 -mf16c -mfancy-math-387 -mfma -mfp-ret-in-387 -mfpmath=sse -mfsgsbase -mfunction-return=keep -mfused-madd -mfxsr -mglibc -mhard-float -mhle -mieee-fp
-mincoming-stack-boundary=0 -mindirect-branch=keep -mintel-syntax -mlarge-data-threshold=65536 -mlong-double-80 -mlzcnt -mmemcpy-strategy -mmemset-strategy -mmmx -mmovbe -mpclmul -mpopcnt -mprefer-avx128 -mprefer-vector-width=none -mpreferred-stack-boundary=0 -mpush-args -mrdrnd -mrecip -mred-zone -mregparm=6 -msahf -msse -msse2 -msse3 -msse4 -msse4.1 -msse4.2 -msse5 -mssse3 -mstack-protector-guard-offset -mstack-protector-guard-reg -mstack-protector-guard-symbol -mstack-protector-guard=tls -mstringop-strategy -mstv
-mtls-dialect=gnu -mtls-direct-seg-refs -mtune-ctrl -mtune=haswell -mveclibabi -mvzeroupper -mxsave -mxsaveopt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top