Skip to content

TypeLayout (Sema's layout model, the hashed ABI source) disagrees with the LLVM types codegen emits: Option, Option[&T], dyn fat pointers; enum layout lives in unhashed Codegen.w #1438

Closed

Description

@ehartford

Summary

Sema's layout model (src/TypeLayout.w, the hashed ABI-defining source that docs/with-abi.md §2/§7 names as the home of the aggregate layouts) and the LLVM types codegen actually emits disagree for several type kinds. Everything that asks the model — comptime T.size()/T.align()/T.fields() offsets, union layout (Codegen.declare_union_type), Channel[T] element size, tuple-value offsets in map iteration — gets a size that is not the size of the emitted value. And the enum/Option/Result layouts that are emitted are derived in src/Codegen.w (declare_enum_type, get_or_create_option_type, get_or_create_result_type, get_or_create_generic_enum_type), which the abi-hash-check does not cover, so an edit there can change the emitted ABI without tripping the hash.

Found while fixing #1430 (which keeps enum layout in codegen and only makes it order-independent; moving it into TypeLayout would have changed the layouts below).

Repro (origin/main 1c76062, base stage2)

use std.collections.HashMap
use std.box.Box

trait Shape:
    fn area(self: &Self) -> i32

type P { x: i64, y: i32 }
type TOptRef = Option[&P]
type TOptP = Option[P]
type TBoxDyn = Box[dyn Shape]
type TRefDyn = &dyn Shape

enum EOpt:
    A(r: Option[P])
    B
enum EBoxDyn:
    A(b: Box[dyn Shape])
    B

const S_OPTREF: usize = comptime TOptRef.size()
const S_OPTP: usize = comptime TOptP.size()
const S_BOXDYN: usize = comptime TBoxDyn.size()
const S_REFDYN: usize = comptime TRefDyn.size()
const S_EOPT: usize = comptime EOpt.size()
const S_EBOXDYN: usize = comptime EBoxDyn.size()

fn main:
    print(f"Option[&P] model={S_OPTREF} llvm={size_of[TOptRef]()}")
    print(f"Option[P] model={S_OPTP} llvm={size_of[TOptP]()}")
    print(f"Box[dyn] model={S_BOXDYN} llvm={size_of[TBoxDyn]()}")
    print(f"&dyn model={S_REFDYN} llvm={size_of[TRefDyn]()}")
    print(f"enum{{Option[P]}} model={S_EOPT} llvm={size_of[EOpt]()}")
    print(f"enum{{Box[dyn]}} model={S_EBOXDYN} llvm={size_of[EBoxDyn]()}")
type TypeLayout (comptime T.size()) emitted LLVM (size_of[T]()) why
Option[&P] 12 8 codegen lowers Option[&T]/Option[*T] to a nullable pointer (with-abi.md §3); the model lays it out as a tagged enum
Option[P], P = {i64, i32} 20 24 codegen's Option is { i32, T } (payload at its own alignment); the model packs the payload at offset 4 like a user enum
Box[dyn Shape] 8 16 codegen's Box[dyn T] is the fat {data, vtable} pair; the model sizes Box's field *T as one pointer
&dyn Shape 8 16 same: &dyn is a fat pointer in codegen, TY_REF = one pointer in the model
enum { A(Option[P]) } 24 28 inherits the Option row
enum { A(Box[dyn]) } 12 20 inherits the Box[dyn] row

Rows that agree: Vec[i32] 32, HashMap 8, Option[i32] 8, Box[P] 8, str 16, fn 16, (i8, i64, i8) 24, Result[i64, str] 20, []i32 16, [i8; 5] 5, String 16, enum { A(Vec[i32]) } 36.

Also

docs/with-abi.md §2 says an enum "is aligned to the larger of the tag's and the payloads' alignment". Neither implementation does that for a user enum: codegen emits { i32, [N x i8] } (align 4, payload at offset 4) and TypeLayout.type_layout_enum_align_of returns the tag's alignment. Codegen's Option does align its payload. The doc, the model and the emitted code need one rule.

Expected

One layout rule per type kind, in the hashed ABI file, read by both codegen's LLVM body construction and every Sema query (D6's single-source principle applied to layout). Until then, a cross-check that the model's size equals the emitted LLVM size for every type codegen lays out would have turned each row above into a loud error.

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