BeeOnRope
BeeOnRope

Reputation: 64905

How to disable vectorization in clang++?

Consider the following small search function:

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;
}

At -O2 or higher, clang vectorizes this search, e.g,. resulting in code like this (for 10 elements):

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

How can I disable this vectorization on the command line or using a #pragma in the code?

I tried the following command line arguments, none of which prevented the vectorization:

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

I also tried #pragma clang loop vectorize(disable) above the loop as shown (commented out) in the code above, without luck.

Upvotes: 13

Views: 5597

Answers (2)

atomsymbol
atomsymbol

Reputation: 421

Add -mprefer-vector-width=1 to clang's command-line options.

Upvotes: 1

Justin
Justin

Reputation: 25297

Turn off SLP Vectorization:

clang++ -O2 -fno-slp-vectorize

Godbolt Link

Upvotes: 11

Related Questions