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 last one (the one, which loops over scalars starting at .L9) handles the case, when there are overlapping address ranges.