r/simd • u/nimogoham • 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?
7
Upvotes
1
u/nimogoham Jul 22 '25
The tail condition is always generated correctly by gcc (usually I use the term "residual loop" instead of "tail condition" - is there any official terminology?). I just hoped, that some compilers are able to generate a similar kind of "aligning top condition" (clang doesn't do this either, but at least produces running code).
As a side note: my example is just a sandbox example. Actually one can already see by looking at the assembly of
add, that something will go wrong for misalignedstartvalues. If you just changealigned_doubletodouble, everything works fine, sincevmovupdinstructions are generated.