https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127824

            Bug ID: 127824
           Summary: fails to vectorize bounded double comparison with and
                    reduction
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: manu at gcc dot gnu.org
  Target Milestone: ---

https://godbolt.org/z/Pbdc986Wa

unsigned
weakly_dominates(const double * restrict a, const double * restrict b, const
unsigned dim)
{
    enum { DIM_BLOCK=4 };
    int d = 0;
    for (; d+DIM_BLOCK <= dim; d += DIM_BLOCK) {
        unsigned a_leq_b = 1;
        for (unsigned i = 0; i < DIM_BLOCK; i++)
            a_leq_b &= (a[d+i] <= b[d+i]);
        if (!a_leq_b)
            return 0;
    }
    for (; d < dim; d++)
        if (a[d] > b[d])
            return 0;
    return 1;
}


unsigned
weakly_dominates_2(const double * restrict a, const double * restrict b, const
unsigned dim)
{
    unsigned a_leq_b = 1;
    for (unsigned d = 0; d < dim; d++)
        a_leq_b &= (a[d] <= b[d]);
    return a_leq_b;
}

unsigned
weakly_dominates_3(const double * restrict a, const double * restrict b, const
unsigned dim)
{
    for (unsigned d = 0; d < dim; d++)
        if (!(a[d] <= b[d])) 
            return 0;
    return 1;
}


With gcc -O3 -march=x86-64-v3 , the inner for-loop in weakly_dominates() is not
vectorized no matter the value of DIM_BLOCK, but my understanding is that it
should get vectorized exactly like weakly_dominates_3 is.


Clang does seem to be able to vectorize it as:

weakly_dominates:
        mov     ecx, edx
        cmp     edx, 4
        jae     .LBB0_3
        xor     r8d, r8d
        jmp     .LBB0_2
.LBB0_3:
        xor     eax, eax
        xor     r9d, r9d
.LBB0_5:
        vmovupd ymm0, ymmword ptr [rdi + 8*r9]
        vcmpnlepd       ymm0, ymm0, ymmword ptr [rsi + 8*r9]
        vtestpd ymm0, ymm0
        jne     .LBB0_9
        lea     r8, [r9 + 4]
        add     r9, 8
        cmp     r9, rcx
        mov     r9, r8
        jbe     .LBB0_5
.LBB0_2:
        mov     eax, 1
        cmp     edx, r8d
        jbe     .LBB0_9
.LBB0_7:
        vmovsd  xmm0, qword ptr [rdi + 8*r8]
        vucomisd        xmm0, qword ptr [rsi + 8*r8]
        ja      .LBB0_8
        inc     r8
        cmp     rcx, r8
        jne     .LBB0_7
.LBB0_9:
        vzeroupper
        ret
.LBB0_8:
        xor     eax, eax
        vzeroupper
        ret

Reply via email to