为什么不同类型的阵列下标会用来迭代影响自动矢量化

发布于 2025-02-10 13:24:13 字数 1609 浏览 0 评论 0原文

如下代码显示,为什么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 技术交流群。

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

发布评论

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

评论(1

微暖i 2025-02-17 13:24:13

查看功能

void test32(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to)

以及如果这样称呼它应该如何表现:

uint32_t arr[16];
test32(arr, arr[3], &arr[0], &arr[15]);

这称为混叠。 nread参数可能会从array中别名元素,因为它们具有相同的类型。但是,如果您有

void test64(uint32_t *array, uint64_t &nread, uint32_t from, uint32_t to)

uint32_tuint64_t永远无法具有相同的地址,那么就不会发生任何混叠。

注意:传递对函数的引用在内部传递该地址,因此它等同于一个指针的混叠论证。

有一些类型的特殊规则称为混叠类型。 C ++标准说,您可以将uint32_t*施放为char*,然后访问uint32_t的原始内存。这意味着uint32_t*char*可以合法地指向同一地址。 char*是一种混叠类型,因为它与任何其他类型的(数据)指针混为一谈。 无符号char*char> char的任何其他变体也是如此,包括std :: byte

但是您可以告诉编译器,即使类型可以通过使用限制

void test32(uint32_t *array, uint32_t & restrict nread, uint32_t from, uint32_t to)

PS:test_another_32看起来像是错过的编译器选择性化。

Look at the function

void test32(uint32_t *array, uint32_t &nread, uint32_t from, uint32_t to)

and how it should behave if you call it like this:

uint32_t arr[16];
test32(arr, arr[3], &arr[0], &arr[15]);

This is called aliasing. The nread parameter might alias elements from array because they have the same type. But when you have

void test64(uint32_t *array, uint64_t &nread, uint32_t from, uint32_t to)

then no aliasing can occur because an uint32_t and uint64_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* to char* and then access the raw memory underlying the uint32_t. That means an uint32_t* and char* can legally point at the same address. char* is an aliasing type because it aliases with any other type of (data) pointer. So is unsigned char* or any other variation of char including std::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.

void test32(uint32_t *array, uint32_t & restrict nread, uint32_t from, uint32_t to)

PS: test_another_32 looks like a missed compiler optiomization.

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