如何查看 -march=native 将激活哪些标志?

发布于 2024-10-27 12:06:42 字数 123 浏览 2 评论 0 原文

我正在使用 GCC 4.3 编译我的 C++ 应用程序。我没有手动选择优化标志,而是使用 -march=native,理论上它应该添加适用于我正在编译的硬件的所有优化标志。但我如何检查它实际使用了哪些标志?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

纵情客 2024-11-03 12:06:42

您可以使用 -Q --help=target 选项:

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

-v 选项也可能有用。

您可以查看有关 --help 选项的文档 此处

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.

挽梦忆笙歌 2024-11-03 12:06:42

要查看命令行标志,请使用:

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

如果您想查看由某些参数设置的编译器/预编译器定义,请执行以下操作:

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

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
酒浓于脸红 2024-11-03 12:06:42

它应该是(-### 类似于 -v):

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

显示 gcc 的“真实”本机标志。

您可以使用命令使它们看起来更“清晰”:

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

并且您可以使用 -mno-* 删除标志:

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

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'
霓裳挽歌倾城醉 2024-11-03 12:06:42

如果您想了解如何设置非本机交叉编译,我发现这很有用:

在目标计算机上,

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

然后在构建计算机上使用它:

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

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 ...
离线来电— 2024-11-03 12:06:42

我将在这个问题上投入我的两分钱,并建议对埃利亚斯的答案进行稍微更详细的扩展。从 gcc 4.6 开始,运行 gcc -march=native -v -E - /dev/null 以多余的 -mno-* 标志的形式发出越来越多的垃圾邮件。以下内容将删除这些内容:

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

但是,我仅在两个不同的 CPU(Intel Core2 和 AMD Phenom)上验证了此操作的正确性,因此我建议还运行以下脚本以确保所有这些 -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}.$

我还没有发现 gcc -march=native -v -E - gcc -march=native -v -E - 之间的区别/dev/nullgcc -march=native -### -E - /dev/null 除了一些被引用的参数之外——以及不包含特殊字符的参数,所以我不确定在什么情况下这会产生任何真正的区别。

最后,请注意 --march=native 是在 gcc 4.2 中引入的,在此之前它只是一个无法识别的参数。

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.

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

#!/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.

玻璃人 2024-11-03 12:06:42

resolve-march-native 正是为此:它解析 GCC 输出并结合将多次调用 GCC 的输出转化为最终答案。例如:

# resolve-march-native --vertical
-march=sandybridge
-maes
--param=l1-cache-line-size=64
--param=l1-cache-size=32
--param=l2-cache-size=3072

还有 distccflags 但它 似乎已停止

PS:我是resolve-march-native的作者。

There is resolve-march-native for exactly this: It parses GCC output and combines the output from multiple calls to GCC into the eventual answer. For example:

# resolve-march-native --vertical
-march=sandybridge
-maes
--param=l1-cache-line-size=64
--param=l1-cache-size=32
--param=l2-cache-size=3072

There is also distccflags but it seems discontinued.

PS: I am the author of resolve-march-native.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文