Closed
Description
Just like global variables and local variables support alignment, struct fields should support alignment.
The alignment of a struct should be the alignment of its most aligned member. As Zig iterates over the fields of a struct, it knows the current alignment at that position. When a field requires more alignment, padding bytes are inserted until the desired alignment is achieved. Zig currently does this incorrectly - See #1248
Related is reordering fields for better performance and memory usage (see #168)
You should also be able to set the alignment of fields in packed structs. This does 2 things:
- acts as assertions - compiler will give an error if the alignment is impossible given that the struct is packed
- determines the overall alignment of the packed struct - it will be the alignment of the most aligned field. if no field is specified to be aligned, the alignment of the packed struct will be 1.
A packed struct in an array should give a compile error if its size is not byte-aligned, or if its size is not aligned to its own alignment.
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
ghost commentedon Sep 18, 2018
Here we can see a use case for specifying the alignment of struct fields: https://github.com/facebook/folly/blob/master/folly/ProducerConsumerQueue.h (search for 'alignas').
They specify the alignment to be the minimum offset between two objects to avoid false sharing.
andrewrk commentedon Dec 20, 2018
Note this workaround that is now in master branch, and also described in #1845 (comment):
zig/src/analyze.cpp
Lines 2698 to 2707 in 459045a
I think we have to solve the underlying issue before this issue can be resolved.
andrewrk commentedon Mar 27, 2019
One more thing that should be done with this issue, is to make packed structs divide based on ABI size, not store size. So for this example:
Currently the struct is generated as
This is problematic because based on
@sizeOf(u24)
one expects to be able to write the 4th byte without overwriting the next field.Instead Zig should generate the struct like this:
and then do the normal bit shifting and masking that happens for bit fields. This will enable packed structs to work effectively when specifying alignment. Otherwise, aligned u24 fields of packed structs would incorrectly be missing a padding byte.
decouple llvm types from zig types
4 remaining items
daurnimator commentedon Aug 5, 2019
@nrdmn notes that this is required for certain UEFI structures.
andrewrk commentedon Aug 22, 2019
I'm in the middle of fixing the bugs regarding struct alignment (#2174). Once that's done, the main blocker to this will be updating the grammar, parser, zig fmt, etc. Anyone want to take a crack at that? The syntax/parser updating work can be merged even if the analysis/codegen isn't done yet.
Here's the current syntax proposal:
andrewrk commentedon Aug 23, 2019
Thanks @Tetralux for doing the stage1 and stage2 parser/renderer for this in #3114!
andrewrk commentedon Aug 27, 2019
Done with the merge of #3115. See #3125 for unions.
Improving the semantics and alignment considerations of packed structs will be a separate issue.