Skip to content

A tuple over an @[align(N)] record or an over-aligned SIMD vector is laid out at LLVM's alignment, not TypeLayout's, and no check reports it (#1438 class) #1964

Closed

Description

@ehartford

Summary

Sibling of #1438/#1958, found while fixing #1958. A tuple's LLVM body is the literal struct of its element types, so an element whose model alignment differs from its LLVM body's lands at LLVM's offset:

  • (i8, Al) with type Al { a: i8, @[align(32)] b: i64 } (§16.4: the record's LLVM body is packed, abi align 1, struct_declared_align holds 32): TypeLayout says 96 bytes aligned 32, LLVM emits 72 bytes aligned 8.
  • (i8, Vector[8, f32]) on AArch64 (§4.3d: TypeLayout caps at 16, LLVM <8 x float> is 32-aligned): TypeLayout says 48/16, LLVM emits 64/32.
  • [Al; 2] agrees (128/32): arrays take the element's declared alignment.

Unlike enums/Option/Result (check_enum_layout, #1438), nothing compares a tuple's emitted layout to the model, so comptime T.size(), union members, Channel[T] slots and map tuples over such a tuple are silently mis-sized — the tool stayed silent.

Repro (stage1 of fix-1438, after #1958's fix)

type Al { a: i8, @[align(32)] b: i64 }
type V8 = Vector[8, f32]
type TAl = (i8, Al)
type TV8 = (i8, V8)
type AAl = [Al; 2]
type W_TAl = (i8, TAl)
type W_TV8 = (i8, TV8)
type W_AAl = (i8, AAl)
fn main:
    print(f"(i8,Al) model {comptime TAl.size()}/{comptime TAl.align()} llvm {size_of[TAl]()} pair {size_of[W_TAl]()}")
    print(f"(i8,V8) model {comptime TV8.size()}/{comptime TV8.align()} llvm {size_of[TV8]()} pair {size_of[W_TV8]()}")
    print(f"[Al;2] model {comptime AAl.size()}/{comptime AAl.align()} llvm {size_of[AAl]()} pair {size_of[W_AAl]()}")
(i8,Al) model 96/32 llvm 72 pair 80      # LLVM: 72 bytes aligned 8
(i8,V8) model 48/16 llvm 64 pair 96      # LLVM: 64 bytes aligned 32 (AArch64)
[Al;2] model 128/32 llvm 128 pair 136    # agrees

Fix direction

Build tuple bodies the way declare_struct_type builds a record with a declared-alignment field (the padded body under struct_declared_align / declared_align_of, #1445), and add a check_tuple_layout next to check_enum_layout so a model/LLVM disagreement is a loud BUG. behav_layout_model_matches_llvm.w should then pin (i8, Al32) and (i8, Vector[4, f32]) rows.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions