是否可以在一个 CPU 微架构上为另一个(一个架构的不同代)编译代码?

Is it possible to compile code on one CPU microarch for another (different generation of one arch)?

我想使用支持 AVX 指令的 CPU 的 GCC 编译器编译 C 代码。但是我的构建机器使用不支持 AVX 的 Core Quad CPU。

我看了很多关于交叉编译的题目都没有找到答案。下面是一些具体细节。

Intel 的 C 编译器可以在一个二进制文件中构建多个微架构特定版本的函数。在运行时选择一个特定的。

Compiling codes on different systems:

-xAVX -axCORE-AVX2

where -x gives the baseline for the compilation and -ax is a list of the feature-specific code paths to build. As the Intel compiler documentation explains:

If the compiler finds such an opportunity, it first checks whether generating a feature-specific version of a function is likely to result in a performance gain. If this is the case, the compiler generates both a feature-specific version of a function and a baseline version of the function. At run time, one of the versions is chosen to execute, depending on the Intel(R) processor in use. In this way, the program can benefit from performance gains on more advanced Intel processors, while still working properly on older processors and non-Intel processors. A non-Intel processor always executes the baseline code path.

这是否意味着英特尔的 C 编译器将 microarch 特定指令放在一个版本的函数中,而不将这些指令放在另一个版本的函数中?

GCC 编译器不以这种方式支持多微架构(摘自上层link):

The GCC compilers do not support multiple code paths and so a "universally" optimized binary is not possible. Here -march gives the baseline and -mtune the processor to tune for whilst respecting the instruction set of the baseline

-march=corei7-avx -mtune=core-avx2

This means "using the features available to the SandyBridge processors tune the code so that it would run optimally on a Haswell processor". Such an optimization would not be able to make use of the FMA instructions as they are not present on the baseline.

这意味着 GCC 编译器不会为目标优化微架构添加新指令。不是吗?


回到任务。对于 Intel 编译器,我可以将基本微架构设置为 Core Quad (-x) 并为最近的微架构 (-ax) 设置优化。应该是工作我还没有测试过它。

但是我如何处理 GCC 编译器?它不会为目标优化微架构 (-mtune) 添加新指令。如果我设置基本微架构 (-march),那么 configure 会提供下一条消息:

configure: error: cannot run test program while cross compiling

是否可以使用 GCC 编译器解决此任务?

问题没说清楚。问题主要属于 configure 脚本。当我将 --host=x86_64-pc-linux-gnu 传递给 configure 脚本时,它启用了 cross-compile 模式 而没有 运行 任何 AC_TRY_RUN 指令。

成功编译后,我反汇编了输出二进制文件并找到了 AVX 指令和寄存器。

所以我认为多版本功能是在 GCC 4.8 中引入的。它是通过 __attribute__ 预处理器指令的设备。感谢@phuclv 提供有关 GCC 的信息!