“-flax-vector-conversions”对于 ARM 编译器到底意味着什么?

What does "-flax-vector-conversions" exactly mean for ARM compiler?

我正在尝试从 arm-linux-gnueabihf gcc/g++ 编译器编写一个 xxx.toolchain.cmake。

让我困惑的是,我是否应该使用-flax-vector-conversions编译标志。我阅读了编译器的 doc/man 页面,它告诉我:

-flax-vector-conversions

Allow implicit conversions between vectors with differing numbers of elements and/or incompatible element types. This option should not be used for new code.

(通过 https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html

我的两个困惑:

  1. 这个解释中的“向量”是什么意思?有什么例子可以说明吗?

  2. “新代码”是什么意思?为什么“新代码”不应与此编译选项一起使用?

GCC 提供 vector extensions 旨在提供一种以独立于机器的方式(与内部函数相反)访问 SIMD 指令的方法。这涉及使用 __attribute__((vector_size(n))) 定义的特殊向量类型,以帮助编译器理解 SIMD 指令使用的打包多元素数据类型。请注意,这与 C++ 的 std::vector 容器无关。

考虑以下代码:

typedef short eight_short __attribute__((vector_size(16)));
typedef int four_int __attribute__((vector_size(16)));

eight_short v1;
four_int v2;

void foo(void) {
    v2 = v1;
}

这里four_inteight_short是对应元素个数和类型的向量。它们都是 16 字节,因此适合存储在 128 位 SIMD 寄存器中。将一个分配给另一个显然意味着“重新解释”(又名位转换),但它也违反了类型安全。大概旧版本的编译器曾经接受这样的代码,并且可能有这样的代码,但编译器作者不希望这样做。所以 such code now causes an error by default, but they provide the option -flax-vector-conversions that you can use when compiling old code like this to suppress the error.

“新代码”表示您是第一次编写代码,您可以选择如何编写它。对于这样的代码,您很可能会使用 v2 = reinterpret_cast<four_int>(v1);,而不是 -flax-vector-conversions。然后编译器将标记您忘记转换的任何地方(因为它可能是一个错误,您实际上意味着其他东西)。

如果您正在编译遗留代码,最好的办法是先尝试在没有此选项的情况下进行构建。如果构建成功,则不需要该选项,因此不要使用它。如果在向量类型转换时出现错误,您可以考虑使用此选项,或者在需要时使用显式转换重写代码。