Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to set alignment on struct fields #1512

Open
andrewrk opened this Issue Sep 13, 2018 · 2 comments

Comments

Projects
None yet
1 participant
@andrewrk
Copy link
Member

andrewrk commented Sep 13, 2018

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.

@ghost

This comment has been minimized.

Copy link

ghost commented 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

This comment has been minimized.

Copy link
Member Author

andrewrk commented 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

// TODO If we have no type_entry for the field, we've already failed to
// compile the program correctly. This stage1 compiler needs a deeper
// reworking to make this correct, or we can ignore the problem
// and make sure it is fixed in stage2. This workaround is for when
// there is a false positive of a dependency loop, of alignment depending
// on itself. When this false positive happens we assume a pointer-aligned
// field, which is usually fine but could be incorrectly over-aligned or
// even under-aligned. See https://github.com/ziglang/zig/issues/1512
if (field->type_entry == nullptr) {
this_field_align = LLVMABIAlignmentOfType(g->target_data_ref, LLVMPointerType(LLVMInt8Type(), 0));

I think we have to solve the underlying issue before this issue can be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.