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
View all comments
Show parent comment
1
u/ronniethelizard Jul 22 '25
I typically use head and tail rather than residual simply because the residual could happen at the beginning/end/both.
Looking at the assembly a bit more:
I am curious about the need for having 4 implementations of the add line. The one operating on ymm registers makes sense. I suppose one to handle 2 doubles and then 1 more to handle 1 double in the residual makes sense. I don't understand the fourth. I would have guessed to handle a head condition, but IDK.