为什么不同类型的阵列下标会用来迭代影响自动矢量化
如下代码显示,为什么uint32_t
防止编译器(GCC 12.1 + O3)通过自动矢量化优化。参见 godbolt 。
#include <cstdint>
// no auto vectorization
void test32(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to) {
for (uint32_t i = from; i < to; i++) {
array[nread++] = i;
}
}
// auto vectorization
void test64(uint32_t *array, uint64_t &nread, uint32_t from, uint32_t to) {
for (uint32_t i = from; i < to; i++) {
array[nread++] = i;
}
}
// no auto vectorization
void test_another_32(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to) {
uint32_t index = nread;
for (uint32_t i = from; i < to; i++) {
array[index++] = i;
}
nread = index;
}
// auto vectorization
void test_another_64(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to) {
uint64_t index = nread;
for (uint32_t i = from; i < to; i++) {
array[index++] = i;
}
nread = index;
}
运行命令g ++ -o3 -fopt -info -vo -vec -missed -c test.cc -o/dev/null
,我得到了以下结果。如何解释它?
bash> g++ -O3 -fopt-info-vec-missed -c test.cc -o /dev/null
test.cc:5:31: missed: couldn't vectorize loop
test.cc:6:24: missed: not vectorized: not suitable for scatter store *_5 = i_18;
test.cc:21:31: missed: couldn't vectorize loop
test.cc:22:24: missed: not vectorized: not suitable for scatter store *_4 = i_22;
As following code shows, why uint32_t
prevents the compiler (GCC 12.1 + O3) from optimizing by auto vectorization. See godbolt.
#include <cstdint>
// no auto vectorization
void test32(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to) {
for (uint32_t i = from; i < to; i++) {
array[nread++] = i;
}
}
// auto vectorization
void test64(uint32_t *array, uint64_t &nread, uint32_t from, uint32_t to) {
for (uint32_t i = from; i < to; i++) {
array[nread++] = i;
}
}
// no auto vectorization
void test_another_32(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to) {
uint32_t index = nread;
for (uint32_t i = from; i < to; i++) {
array[index++] = i;
}
nread = index;
}
// auto vectorization
void test_another_64(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to) {
uint64_t index = nread;
for (uint32_t i = from; i < to; i++) {
array[index++] = i;
}
nread = index;
}
After I ran the command g++ -O3 -fopt-info-vec-missed -c test.cc -o /dev/null
, I got the following result. How to interpret it?
bash> g++ -O3 -fopt-info-vec-missed -c test.cc -o /dev/null
test.cc:5:31: missed: couldn't vectorize loop
test.cc:6:24: missed: not vectorized: not suitable for scatter store *_5 = i_18;
test.cc:21:31: missed: couldn't vectorize loop
test.cc:22:24: missed: not vectorized: not suitable for scatter store *_4 = i_22;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看功能
以及如果这样称呼它应该如何表现:
这称为混叠。
nread
参数可能会从array
中别名元素,因为它们具有相同的类型。但是,如果您有uint32_t
和uint64_t
永远无法具有相同的地址,那么就不会发生任何混叠。注意:传递对函数的引用在内部传递该地址,因此它等同于一个指针的混叠论证。
有一些类型的特殊规则称为混叠类型。 C ++标准说,您可以将
uint32_t*
施放为char*
,然后访问uint32_t
的原始内存。这意味着uint32_t*
和char*
可以合法地指向同一地址。char*
是一种混叠类型,因为它与任何其他类型的(数据)指针混为一谈。无符号char*
或char> char
的任何其他变体也是如此,包括std :: byte
。但是您可以告诉编译器,即使类型可以通过使用限制。
PS:
test_another_32
看起来像是错过的编译器选择性化。Look at the function
and how it should behave if you call it like this:
This is called aliasing. The
nread
parameter might alias elements fromarray
because they have the same type. But when you havethen no aliasing can occur because an
uint32_t
anduint64_t
can never have the same address.Note: passing a reference to a function internally passes the address so it's equivalent to a pointer for the argument of aliasing.
There are some types with special rules called aliasing types. The C++ standard says that you can cast an
uint32_t*
tochar*
and then access the raw memory underlying theuint32_t
. That means anuint32_t*
andchar*
can legally point at the same address.char*
is an aliasing type because it aliases with any other type of (data) pointer. So isunsigned char*
or any other variation ofchar
includingstd::byte
.But you can tell the compiler that 2 pointers are not allowed to alias even if the type would permit it by using restrict.
PS:
test_another_32
looks like a missed compiler optiomization.