You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
alignment of arrays is supposed to be at least 16 bytes if the size of the array is >= 16 bytes #10809
An array uses the same alignment as its elements, except that a local or global
array variable that requires at least 16 bytes, or a C99 local or global variable-
length array variable, always has alignment of at least 16 bytes.
Currently Zig does not observe this rule correctly and uses alignment of the element type.
I started implementing this change, and got this far:
--- a/src/type.zig+++ b/src/type.zig@@ -2289,8 +2289,6 @@ pub const Type = extern union {
.u8,
.i8,
.bool,
- .array_u8_sentinel_0,- .array_u8,
.atomic_order,
.atomic_rmw_op,
.calling_convention,
@@ -2389,7 +2387,49 @@ pub const Type = extern union {
.error_set_merged,
=> return 2, // TODO revisit this when we have the concept of the error tag type
- .array, .array_sentinel => return self.elemType().abiAlignment(target),+ .array_u8_sentinel_0,+ .array_u8,+ => {+ if (target.cpu.arch == .x86_64) {+ const len = self.cast(Payload.Len).?.data;+ if (len >= 16) {+ return 16;+ }+ }+ return 1;+ },++ .array => {+ const data = self.castTag(.array).?.data;+ const elem_align = data.elem_type.abiAlignment(target);+ const max_16_align = @maximum(16, elem_align);++ if (max_16_align > elem_align and+ target.cpu.arch == .x86_64 and+ elem_align != 0 and+ data.elem_type.abiSize(target) * data.len >= 16)+ {+ return max_16_align;+ }++ return elem_align;+ },++ .array_sentinel => {+ const data = self.castTag(.array_sentinel).?.data;+ const elem_align = data.elem_type.abiAlignment(target);+ const max_16_align = @maximum(16, elem_align);++ if (max_16_align > elem_align and+ target.cpu.arch == .x86_64 and+ elem_align != 0 and+ data.elem_type.abiSize(target) * data.len >= 16)+ {+ return max_16_align;+ }++ return elem_align;+ },
// TODO audit this - is there any more complicated logic to determine
// ABI alignment of vectors?
--- a/lib/std/elf.zig+++ b/lib/std/elf.zig@@ -561,7 +561,7 @@ pub const Elf64_Section = u16;
pub const Elf32_Versym = Elf32_Half;
pub const Elf64_Versym = Elf64_Half;
pub const Elf32_Ehdr = extern struct {
- e_ident: [EI_NIDENT]u8,+ e_ident: [EI_NIDENT]u8 align(4),
e_type: ET,
e_machine: EM,
e_version: Elf32_Word,
It seems like the change would mostly work fine but it does have some non-obvious language specification implications. For example this behavior test started failing:
../test/behavior/slice.zig:274:29: note: called from here
comptime try expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));
^
Seems like this issue would benefit from a discussion.
I don't think the language should expose this at all. Zig should use the alignment of the element type, and the backend should ensure that locals and globals have greater alignment if needed.
Activity
andrewrk commentedon Mar 14, 2022
I started implementing this change, and got this far:
It seems like the change would mostly work fine but it does have some non-obvious language specification implications. For example this behavior test started failing:
Seems like this issue would benefit from a discussion.
SpexGuy commentedon Mar 14, 2022
I don't think the language should expose this at all. Zig should use the alignment of the element type, and the backend should ensure that locals and globals have greater alignment if needed.