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

            Bug ID: 128968
           Summary: [vectorizer] Fails to vectorize int32 loop with 'int32
                    + {uint32,int64,uint64}' as argument to an 'int'
                    lambda function
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
            Blocks: 53947
  Target Milestone: ---
            Target: x86_64-linux-gnu,aarch64-unknown-linux-gnu

The following three variants of a program fail to vectorize with GCC mainline
but vectorize with Clang 23.


On x86-64 using "-mavx2 -O3 -std=c++20 -fno-unroll-loops -fopt-info-vec-all":

bar2.c:6:31: missed: couldn't vectorize loop
bar2.c:4:42: missed: not vectorized: no vectype for stmt: _15 = *_14;
 scalar_type: float
bar2.c:3:6: note: vectorized 0 loops in function.

(or: bar.c:4:42: missed: not vectorized: no vectype for stmt: _16 = *_15;)


Clang 23.1 shows with

-mavx2 -O3 -std=c++20 -fno-unroll-loops -Rpass=loop-vectorize
-Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize

for those:

<source>:10:5: remark: the cost-model indicates that interleaving is beneficial
but is explicitly disabled or interleave count is set to 1
[-Rpass-analysis=loop-vectorize]
   10 |     for (idx_t j = start; j < limit; j++) {
      |     ^
<source>:10:5: remark: vectorized loop (vectorization width: 8, interleaved
count: 1) [-Rpass=loop-vectorize]

* * *

Test program: All arguments are 32bit signed integers, except that
   'j + offset'
in the lambda function call mixes data types, but is still casted
afterwards to 'int'. All of the following fails identically:

   std::uint32_t offset
   std::uint64_t offset
   std::int64_t offset

This pattern can easily occur as users like to use 'int' while
STL, sizeof etc. prefer unsigned data types typically having 64bit.

Kudos to the CExA team at CEA reporting this (based on their finding with
Kokkos), esp. thanks to Hariprasad Kannan!

* * *

Trying this with the ARM64 / armv8-a compiler on godbolt gives
(same command-line arguments except for '-mavx2') gives with GCC:

<source>:10:29: missed: couldn't vectorize loop
<source>:8:38: missed: possible alias involving gather/scatter between *_15 and
*_15
<source>:5:6: note: vectorized 0 loops in function.

while Clang gives:

<source>:10:5: remark: the cost-model indicates that interleaving is beneficial
but is explicitly disabled or interleave count is set to 1
[-Rpass-analysis=loop-vectorize]
<source>:10:5: remark: vectorized loop (vectorization width: 4, interleaved
count: 1) [-Rpass=loop-vectorize]

* * *

#include <cstdint>

using idx_t = std::int32_t;

void square(float* arr, idx_t start, idx_t limit, std::uint64_t offset) {
    // offset begin std::int32_t will lead to auto-vectorization

    auto my_func = [=](int i) { arr[i] += arr[i]; };

    for (idx_t j = start; j < limit; j++) {
        my_func(j + offset);
    }
}


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53947
[Bug 53947] [meta-bug] vectorizer missed-optimizations

Reply via email to