macOS/Apple Silicon 和 SHA-3 指令上的 gcc 11.3.0

gcc 11.3.0 on macOS/Apple Silicon and SHA-3 instructions

我在配备 Apple Silicon M1 的 MacBook Air 上使用 Homebrew 安装了 gcc 11.3.0 CPU。二进制文件是 aarch64 本机版本,不是 Rosetta 仿真的。安装的OS是macOS Monterey 12.3.

我在编译使用 M1 CPU 支持的 ARMv8.2-A SHA-3 扩展指令的程序时遇到问题。这是一个最小的可重现示例:

#include <arm_neon.h>

int main() {
    uint64x2_t a = {0}, b = {0}, c = {0};
    veor3q_u64(a, b, c);
    return 0;
}

使用 Apple 提供的 clang 编译器可以很好地编译此代码。

我使用以下命令行为 gcc 11 编译了它:

gcc-11 -o test test.c -march=armv8-a+sha3

这会导致以下错误:

In file included from test.c:1:
test.c: In function 'main':
/opt/homebrew/Cellar/gcc/11.3.0/lib/gcc/11/gcc/aarch64-apple-darwin21/11/include/arm_neon.h:32320:1: error: inlining failed in call to 'always_inline' 'veor3q_u64': target specific option mismatch
32320 | veor3q_u64 (uint64x2_t __a, uint64x2_t __b, uint64x2_t __c)
      | ^~~~~~~~~~
test.c:5:5: note: called from here
    5 |     veor3q_u64(a, b, c);
      |     ^~~~~~~~~~~~~~~~~~~

这是这个特定 hardware/software 组合中的错误,还是有一些命令行选项我可以传递给 gcc 以使这个特定程序编译?

问题解决了。事实证明,gcc 需要 -march=armv8.2-a+sha3 而不仅仅是 -march=armv8-a+sha3 来编译这个内在函数。实际上,在 arm_neon.h 的 gcc 版本中,可以在包含 veor3q_u64:

的内部函数块之前找到它
#pragma GCC target ("arch=armv8.2-a+sha3")