The following code compiled and run with dmd-2.071 prints:
SIMD vector types inherit their base type's alignment:
float8.alignof yields 4 but should yield 32.
void16.alignof yields 1 but should yield 16.
When embedded, a float4 exposes some hidden alignment, as it changes to 16.
The float8 needs a 32 byte alignment though or we SEGFAULT as soon as we load c[0] or c[1].
Even worse, the stack space occupied by these 32 byte vectors is only 16!
-------------------------
void main()
{
import core.simd;
import std.stdio;
import std.math;
union Matrix4x4 { float[16] a; float4[4] b; float8[2] c; alias a this; }
float8 onStackA;
float8 onStackB;
writeln("SIMD vector types inherit their base type's alignment:");
writefln("float8.alignof yields %s but should yield 32.", float8.alignof);
writefln("void16.alignof yields %s but should yield 16.", void16.alignof);
writefln("When embedded, a float4 exposes some hidden alignment, as it changes to %s.", Matrix4x4.alignof);
writeln("The float8 needs a 32 byte alignment though or we SEGFAULT as soon as we load c[0] or c[1].");
writefln("Even worse, the stack space occupied by these %s byte vectors is only %s!",
float8.sizeof, abs(cast(ptrdiff_t)&onStackB - cast(ptrdiff_t)&onStackA));
}
-------------------------
Compiled programs with too little stack space or incorrect alignment of SIMD types crash.
Comment #1 by Marco.Leise — 2016-05-29T10:59:50Z
Just a clarification, that doesn't change the subject of this report: It occurred to me today that the stack is aligned to 16 bytes and therefore we cannot guarantee correct alignment for AVX vectors placed on the stack.
Comment #2 by Marco.Leise — 2016-05-30T14:11:09Z
Ok, different sources report that major compilers (llvm, gcc, icc) respect alignments >= 16 on the stack. I'll open a separate issue for that.
Comment #4 by dlang-bugzilla — 2017-07-02T18:49:48Z
(In reply to Marco Leise from comment #0)
> writefln("float8.alignof yields %s but should yield 32.",
> float8.alignof);
> writefln("void16.alignof yields %s but should yield 16.",
> void16.alignof);
Fixed in https://github.com/dlang/dmd/pull/6265
> writefln("When embedded, a float4 exposes some hidden alignment, as it
> changes to %s.", Matrix4x4.alignof);
Fixed in https://github.com/dlang/dmd/pull/6582 (issue 17237)