Skip to content

alignment of arrays is supposed to be at least 16 bytes if the size of the array is >= 16 bytes #10809

Open

Description

@andrewrk

According to System V Application Binary Interface,

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.

Activity

  1. added
    bug
    arch-x86_64
    on Feb 5, 2022
  2. added this to the 0.10.0 milestone on Feb 5, 2022
  3. andrewrk commented on Mar 14, 2022

    @andrewrk
    MemberAuthor

    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.

  4. SpexGuy commented on Mar 14, 2022

    @SpexGuy
    Contributor

    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.

  5. added
    proposal
    and removed
    bug
    on Mar 14, 2022
  6. modified the milestones: 0.10.0, 0.11.0 on Apr 16, 2022
  7. modified the milestones: 0.11.0, 0.12.0 on Jun 19, 2023
  8. modified the milestones: 0.14.0, 0.15.0 on Jul 26, 2024
  9. added
    bug
    and removed
    proposal
    on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-x86_64bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions