Skip to content

Option[T] over an @[align]-ed struct or an over-aligned SIMD vector is refused: its { i32, T } body cannot place T at TypeLayout's alignment #1958

Closed

Description

@ehartford

Summary

Option[T] is emitted as the LLVM struct { i32, T }, and the payload is read and written as LLVM field 1 by value (55 extractvalue/insertvalue sites in src/Codegen.w and src/CodegenDispatch.w). That body places T at LLVM's alignment for T, which is not TypeLayout's alignment (with-abi.md §2) in two cases:

  1. T carries a declared alignment above its LLVM body's — a struct with an @[align(N)] field (§16.4). Its LLVM body is packed with explicit padding (abi align 1, struct_declared_align holds N), so { i32, T } puts it at offset 4 where the model puts it at N.
  2. T is a SIMD vector LLVM aligns above the target's rule (§4.3d) — Vector[8, f32] on AArch64: TypeLayout caps at 16, LLVM <8 x float> is 32-aligned, so { i32, <8 x float> } is 64 bytes aligned 32 where the model says 48 aligned 16.

Every other enum (declare_enum_type, generic instances, Result, repr enums) builds { tag, [n x unit] } from the model since #1438 and matches in both cases; only Option's value-level body is left.

Repro (after #1438's branch; before it the same programs compiled with the silent mismatch)

type V8 = Vector[8, f32]
type Al { a: i8, @[align(16)] b: i64 }

fn main:
    let o: Option[V8] = Some(V8.splat(1.0))
    let p: Option[Al] = Some(Al { a: 1, b: 2 })
    if o.is_some() and p.is_some(): print("values")
BUG: Option 'Option[f32x8]' is emitted as 64 bytes aligned 32, but TypeLayout lays it out as 48 bytes aligned 16 (#1438)
BUG: Option 'Option[Al]' is emitted as 40 bytes aligned 8, but TypeLayout lays it out as 48 bytes aligned 16 (#1438)
error: code generation failed

check_enum_layout (#1438) refuses the program loudly: a model/LLVM disagreement is memory corruption wherever the model is consulted (comptime T.size(), union members, Channel[T] slots, map tuples), so refusing beats compiling.

Fix direction

Give Option the same { tag, [n x unit] } body as every other enum and route the payload through memory (GEP field 1, load/store T) behind one accessor, or keep { i32, T } for the common case and add a padding element with the payload index served by one option_payload_index(ty) so the 55 sites stop spelling 1. Either way the body is read from TypeLayout's shape, never measured in codegen. Option[&T]/Option[*T]/Option[Box[T]]/Option[extern fn] (the §3 nullable pointer) are unaffected.

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by ac522dc (#1960, ABI v13): Option bodies are built from TypeLayout's model via enum_body_from_layout for every payload the nullable niche doesn't cover; one accessor pair replaces 22 struct-indexing sites; the emitted size is verified against the model. behav_1958_option_over_aligned_payload.w: BUG on the pre-fix binary, ok after (Option[Al] 96/32, Option[Option[Al]] 128/32, Option[V4] 32/16). Battery green on stack #1988.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions