[英]How to see which flags -march=native will activate?
I'm compiling my C++ app using GCC 4.3.我正在使用 GCC 4.3 编译我的 C++ 应用程序。 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.我使用的是-march=native
,而不是手动选择优化标志,理论上应该添加适用于我正在编译的硬件的所有优化标志。 But how can I check which flags is it actually using?但是我怎样才能检查它实际使用了哪些标志呢?
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
):它应该是( -###
类似于-v
):
echo | gcc -### -E - -march=native
To show the "real" native flags for gcc.显示 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:你可以用 -mno-* 去掉标志:
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.从 gcc 4.6 开始,运行gcc -march=native -v -E - < /dev/null
会以多余的-mno-*
标志的形式发出越来越多的垃圾邮件。 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.但是,我只在两个不同的 CPU(Intel Core2 和 AMD Phenom)上验证了它的正确性,因此我建议还运行以下脚本以确保所有这些-mno-*
标志都可以安全地剥离。
2021 EDIT: There are indeed machines where -march=native
uses a particular -march
value, but must disable some implied ISAs (Instruction Set Architecture) with -mno-*
. 2021 编辑:确实存在-march=native
使用特定-march
值的机器,但必须使用-mno-*
禁用某些隐含的 ISA(指令集架构)。
#!/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.除了引用一些参数之外,我没有发现gcc -march=native -v -E - < /dev/null
和gcc -march=native -### -E - < /dev/null
之间的区别 - 和不包含特殊字符的参数,所以我不确定在什么情况下这会产生真正的区别。
Finally, note that --march=native
was introduced in gcc 4.2, prior to which it is just an unrecognized argument.最后,请注意--march=native
是在 gcc 4.2 中引入的,在此之前它只是一个无法识别的参数。
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
. 我写了一个脚本,它将生成一个标志列表,准备从gcc -march=native -mtune=native -Q --help=target
的输出反馈到gcc
。 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. 您可以删除末尾的xargs
以逐行查看标记。
Edit: Including an example output. 编辑:包括示例输出。 On my machine (Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz ), this gives me: 在我的机器上(Intel(R)Core(TM)i5-4300U CPU @ 1.90GHz),这给了我:
-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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.