c++ 如何在clang++中禁用矢量化?

u0sqgete  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(110)

考虑以下小搜索函数:

template <uint32_t N>
int32_t countsearch(const uint32_t *base, uint32_t needle) {
    uint32_t count = 0;
    #pragma clang loop vectorize(disable)
    for (const uint32_t *probe = base; probe < base + N; probe++) {
        if (*probe < needle)
            count++;
    }
    return count;
}

字符串
-O2或更高,clang vectorizes this search,例如,.导致这样的代码(对于10个元素):

int countsearch<10u>(unsigned int const*, unsigned int):            # @int countsearch<10u>(unsigned int const*, unsigned int)
        vmovd   xmm0, esi
        vpbroadcastd    ymm0, xmm0
        vpbroadcastd    ymm1, dword ptr [rip + .LCPI0_0] # ymm1 = [2147483648,2147483648,2147483648,2147483648,2147483648,2147483648,2147483648,2147483648]
        vpxor   ymm2, ymm1, ymmword ptr [rdi]
        vpxor   ymm0, ymm0, ymm1
        vpcmpgtd        ymm0, ymm0, ymm2
        cmp     dword ptr [rdi + 32], esi
        vpsrld  ymm1, ymm0, 31
        vextracti128    xmm1, ymm1, 1
        vpsubd  ymm0, ymm1, ymm0
        vpshufd xmm1, xmm0, 78          # xmm1 = xmm0[2,3,0,1]
        vpaddd  ymm0, ymm0, ymm1
        vphaddd ymm0, ymm0, ymm0
        vmovd   eax, xmm0
        adc     eax, 0
        cmp     dword ptr [rdi + 36], esi
        adc     eax, 0
        vzeroupper
        ret


如何在命令行或代码中使用#pragma禁用此矢量化?
我尝试了以下命令行参数,没有一个能阻止矢量化:

-disable-loop-vectorization 
-disable-vectorization
-fno-vectorize 
-fno-tree-vectorize


我还尝试了循环上面的#pragma clang loop vectorize(disable),就像你在上面的代码中看到的那样,没有运气。

yvgpqqbh

yvgpqqbh1#

关闭SLP矢量化:

clang++ -O2 -fno-slp-vectorize

字符串
Godbolt Link

nvbavucw

nvbavucw2#

-mprefer-vector-width=1添加到clang的命令行选项中。

相关问题