Rationale
In certain high performance or specialized data structures/algorithms, it is desirable to enforce an alignment for structs, fields, or locals.
Today, CoreFX provides several specialized data structures for which the runtime either has special alignment handling (System.Numerics.Vector) or for which they have some specialized padding (dotnet/corefx#22724).
As such, the framework/runtime should provide a mechanism for encforcing a specified alignment for structs and fields. Locals should also be included if that is feasible (I'm not sure if that is readily possible today given that attributes cannot be specified on locals).
Additional Thoughts
It might be worthwhile to additionally expose this on the existing StructLayoutAttribute as an Alignment property.
An alignment of 0 should be treated as "Automatic" (the current behavior of letting the runtime decide alignment).
A mechanism for aligning to the cache would be ideal (dotnet/corefx#22724 (comment)). This could perhaps be a special value that would otherwise be invalid (such as Alignment=-1). Other special alignments could also be allowed in a similar manner.
If a field specifies an alignment less than that of the struct, it should be aligned to the alignment of the struct. For example, if you do Alignment=8 on a Vector4 (which has an Alignment=16), the field should be treated as Alignment=16.
[Design Decision] If a struct specifies an alignment less than that of its first field it should either:
A. Align the struct as specified and add the appropriate padding so that the first field is also aligned as specified
-or-
B. Align the struct as per the requirements of the first field
[EDIT] Make reference to the PR a link by @karelz
Rationale
In certain high performance or specialized data structures/algorithms, it is desirable to enforce an alignment for structs, fields, or locals.
Today, CoreFX provides several specialized data structures for which the runtime either has special alignment handling (
System.Numerics.Vector) or for which they have some specialized padding (dotnet/corefx#22724).As such, the framework/runtime should provide a mechanism for encforcing a specified alignment for structs and fields. Locals should also be included if that is feasible (I'm not sure if that is readily possible today given that attributes cannot be specified on locals).
Additional Thoughts
It might be worthwhile to additionally expose this on the existing
StructLayoutAttributeas anAlignmentproperty.An alignment of
0should be treated as "Automatic" (the current behavior of letting the runtime decide alignment).A mechanism for aligning to the cache would be ideal (dotnet/corefx#22724 (comment)). This could perhaps be a special value that would otherwise be invalid (such as
Alignment=-1). Other special alignments could also be allowed in a similar manner.If a field specifies an alignment less than that of the struct, it should be aligned to the alignment of the struct. For example, if you do
Alignment=8on aVector4(which has anAlignment=16), the field should be treated asAlignment=16.[Design Decision] If a struct specifies an alignment less than that of its first field it should either:
A. Align the struct as specified and add the appropriate padding so that the first field is also aligned as specified
-or-
B. Align the struct as per the requirements of the first field
[EDIT] Make reference to the PR a link by @karelz