Skip to content

TypeLayout owns every layout: enums per §2, Option niche per §3, fat dyn pointers; codegen builds and verifies from the model (ABI v13) (#1438) - #1960

Merged
ehartford merged 3 commits into
fix-916from
fix-1438
Oct 3, 2026
Merged

ehartford merged 3 commits into
fix-916from
fix-1438

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1438; #1599 closed separately (already fixed by 5655191). Before: 27 of 64 kinds disagreed between the hashed model and emitted LLVM (every Option over a pointer/Box/fn/str/Vec/i64, &dyn/Box[dyn] 8 vs 16, and every payload enum packed at offset 4 against §2). After: 63/64 agree (Unit is spec-sanctioned: zero-size vs i32 carrier). TypeLayout implements §2's enum rule and §3's niche once; codegen builds enum/Option/Result bodies from the model and verifies emitted size, BUG + had_error on mismatch. behav_layout_model_matches_llvm.w: 27 MISMATCH rows on the pinned seed, passes on stage1. ABI 12→13, hash re-recorded. :fixpoint byte-identical, :drop-audit 254/0, gate green.

Blocked on Eric (docs/spec untouched): §3 text naming Box[T]/extern fn as niches (matches emitted ABI and Rust's set); §1 one line on fat dyn pointers; the v13 history entry. #1958 (filed): Option over @[align(N)] structs / >16-byte SIMD vectors now refused loudly (was a silent mismatch) — fix before merge. ABI change: batteries alone.

@ehartford

Copy link
Copy Markdown
Contributor Author

b8e7bcf fixes #1958 on this branch: Option bodies are built from the model via enum_body_from_layout (both the Sema and the Sema-less alias/container paths), one accessor pair (option_tag_* / option_payload_*) replaces 22 struct-indexing sites; the two 'read field 1's type' fallbacks are gone (missing Sema payload type is a sema_phase_bug, D65). Also fixed: Result over a packed-body payload via a type alias measured with abi_align_of (72/8 vs model 96/32). behav_1958_option_over_aligned_payload.w: BUG on the pre-fix binary, ok after; layout matrix gained 6 kinds. drop-audit 254/0, fixpoint, gate green. Filed #1964 for the same class on tuples. Still blocked on Eric's three spec sentences before this is marked ready.

Eric Hartford added 3 commits October 2, 2026 20:20
…t dyn pointers, nullable Option; enum bodies read the model; ABI v13

TypeLayout (the hashed ABI source) and the LLVM types codegen emitted
disagreed for 27 of 64 kinds: Option[&T]/Option[*T]/Option[Box[T]]/
Option[extern fn] (model tagged, emitted nullable pointer), &dyn and
Box[dyn] (model one word, emitted fat), every Option whose payload is
aligned above 4 (model packed at offset 4, emitted at the payload's
alignment), and user/generic/Result/repr enums (emitted packed at offset
4 against with-abi.md §2). Every comptime T.size()/T.align(), union
member, Channel[T] slot and map tuple read the model, so each row was a
wrong size in generated code.

- TypeLayout: one enum shape (type_layout_enum_shape: tag, payload area
  at the largest payload's alignment, aligned to the larger of the two,
  §2), measured once for plain and generic enums; a pointer to a trait
  object is two words (§1); type_layout_option_is_nullable names §3's
  single-address niche (&T, *T, extern fn, std Box[T]; not &str, not
  &dyn).
- Codegen: declare_enum_type, get_or_create_generic_enum_type,
  declare_disc_enum_type and get_or_create_result_type build
  { tag, [n x unit] } from the model's frozen size/align
  (enum_body_from_layout) and measure nothing themselves;
  get_or_create_option_type reads the model's nullable verdict and only
  confirms it against the payload's LLVM shape; check_enum_layout proves
  every emitted enum/Option/Result measures what the model says (a
  mismatch is a loud BUG).
- WITH_ABI_VERSION 13: an enum whose payload alignment exceeds its tag's
  grows to the §2 shape (E64 12 -> 16, Result[i64, str] 20 -> 24).
- behav_layout_model_matches_llvm pins model == LLVM for 63 kinds;
  enum_layout_declaration_order's pins move to the §2 numbers.
…s not cover; one accessor pair for its tag and payload (#1958)

After #1438, `Option[T]` kept `{ i32, T }`, with the payload read and
written as LLVM field 1 by value. That body places T at LLVM's alignment
for it, not TypeLayout's: an `@[align(N)]` record (§16.4, packed LLVM
body, abi align 1) sat at offset 4 where the model puts it at N, and a
SIMD vector LLVM aligns above the target's rule (§4.3d, `Vector[8, f32]`
on AArch64) made the Option 64/32 where the model says 48/16. The #1438
check refused such programs loudly: `BUG: Option 'Option[Al]' is emitted
as 72 bytes aligned 8, but TypeLayout lays it out as 96 bytes aligned 32`.

- get_or_create_option_type builds `{ i32, [n x unit] }` through
  enum_body_from_layout from the model's frozen size/align, like every
  other tagged enum; an Option with no Sema type (a container's element
  Option, the `type T = Option[...]` alias path) takes the same shape over
  its LLVM payload's size and declared_align_of (§16.4/§4.3d), so both
  paths produce one literal type. Result's Sema-less branch measured the
  payload with abi_align_of (1 for a packed body) and is on
  declared_align_of too (`Result[Al32, i8]` was 72/8 against 96/32).
- One accessor pair in Codegen.w — option_tag_value / option_tag_ptr for
  the tag and option_payload_ptr (the payload area as an untyped place)
  with option_payload_load/store/value over it, taking the payload's type
  from Sema since the body no longer names it. A payload access carries
  the model's alignment where it is below LLVM's (wl_set_alignment; a
  `<8 x float>` at a 16-byte offset must not promise 32).
- Every site that indexed the Option struct itself is routed through the
  pair: build_option_some/none, Enum.from_int's insertvalue construction,
  OPT_IS_SOME/OPT_IS_NONE/unwrap/filter, channel recv, the slot-pointer
  Option (its `&str` view type now comes from Sema, not field 1), the
  place-projection payload GEPs, and the 29 callers of
  mir_option_payload_value (now option_payload_value). filter's and
  unwrap's "field 1's type" fallbacks are gone: a missing Sema payload
  type is a BUG, not a representation to read back.
- behav_1958_option_over_aligned_payload pins Option[Al] (align 32),
  Option[Option[Al]], Option[Vector[4, f32]], Option[Vector[8, f32]] (a
  verdict only; the vector's alignment is the target's): construct,
  match, is_some, move-out, unwrap, Vec.pop, and model == LLVM.
  behav_layout_model_matches_llvm gains Option[Al32], Option[Option[Al32]],
  Option[V4], Option[Option[V4]], Result[Al32,i8], Gen[Al32].

A tuple over the same payloads is laid out at LLVM's alignment with no
check reporting it: #1964.
…n's nullable-pointer set names Box[T] and extern fn (§3); fat dyn pointers (§1) — Eric's words (blessed 2026-10-02)
@ehartford
ehartford changed the base branch from main to fix-916 October 3, 2026 03:21
@ehartford
ehartford added this pull request to stack #1988 October 3, 2026 03:21
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on stack top 1033f91 (seed-driven, with run tools/battery.w; stack #1988 = #1987 → #1960, on main 9307016): gate rc=0 wall=616s, battery-checks rc=0 wall=635s — fixpoint, drop and move audits, every corpus. The earlier #1960 red (spec_ss14_11_await_combinator_cancel_joins passing under its #916 known-issue) is resolved by #1987 fixing #916 for real; the known-issue and aarch64 skip lines are gone. Merged by the coordinator per Eric's standing direction (2026-10-02: merge stacked PRs without waiting).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant