考虑以下小搜索函数:
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)
,就像你在上面的代码中看到的那样,没有运气。
2条答案
按热度按时间yvgpqqbh1#
关闭SLP矢量化:
字符串
Godbolt Link
nvbavucw2#
将
-mprefer-vector-width=1
添加到clang的命令行选项中。