r/simd • • Jul 22 '25

Do compilers auto-align?

The following source code produces auto-vectorized code, which might crash:

typedef __attribute__(( aligned(32))) double aligned_double;

void add(aligned_double* a, aligned_double* b, aligned_double* c, int end, int start)
{
    for (decltype(end) i = start; i < end; ++i)
        c[i] = a[i] + b[i];
}

(gcc 15.1 -O3 -march=core-avx2, playground: https://godbolt.org/z/3erEnff3q)

The vectorized memory access instructions are aligned. If the value of start is unaligned (e.g. ==1), a seg fault happens. I am unsure, if that's a compiler bug or just a misuse of aligned_double. Anyway...

Does someone know a compiler, which is capable of auto-generating a scalar prologue loop in such cases to ensure a proper alignment of the vectorized loop?

6 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/barr520 Jul 22 '25 edited Jul 22 '25

Even after fixing the alignment on the arrays I'm getting a segmentation fault, something does seem to be wrong here.

The promise to the compiler was that the first element of the array is aligned, and that promise is kept regardless of the start parameter.

The fact that the start parameter wants to start from a non aligned member just means that the compiler must take care of the head and not just the tail, but it does not.

Also, trying with clang, i'm getting a "passing 8-byte aligned argument to 32-byte aligned parameter" warning, which is weird since the argument *is* aligned to 32 bytes

1

u/ronniethelizard Jul 22 '25

I went through and:

  1. set each array to length 12.
  2. put alignas(32) before each array.

and still got the segfault.

When I change start to 0, the segfault goes away. I strongly suspect that it is the compiler doesn't handle the head condition properly.

u/nimogoham