Skip to content

emit-C nullable-pointer Options (#2036); over-aligned heap cells and closure environments (#2039, #2042); emit-C rotate at every width (#1006) - #2044

Merged
ehartford merged 4 commits into
fix-2022from
fix-2036
Oct 3, 2026
Merged

ehartford merged 4 commits into
fix-2022from
fix-2036

Conversation

@ehartford

@ehartford ehartford commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #2037 (fix-2022).

Verification on the top commit, rebased onto main 74e3001: :dev green; :emit-c-smoke 30 cases; :drop-audit 254 cells, 0 regressions; src/main build :gate green (fixed list).

Eric Hartford added 3 commits October 3, 2026 08:17
…ery record's layout is asserted (#2036)

with-abi.md §3: Option[&T], Option[*T], Option[extern fn] and Option[Box[T]]
lower to the payload's pointer, null being None (TypeLayout's
type_layout_option_is_nullable). The C backend emitted them as
`{int32_t tag; union {...}}`: Option[Box[Node]] was 16 bytes against the
model's 8, so `Node { v: i32, next: Option[Box[Node]] }` had a 24-byte
stride in C and 16 in the LLVM backend, and #2022's layout asserts had to
skip every record holding one.

- c_type of such an Option is its payload's C type; it has no record of
  its own (collection, readiness and definition skip it).
- One place for each representation fact: enum_tag_text (the null test
  for these Options, `.tag` otherwise), enum_payload_member (the value
  itself for these Options), payload_enum_literal (the payload, or a null
  of the type). The discriminant rvalue, place projections, Display,
  is_some/is_none and unwrap/expect read them.
- The #2036 exemption in layout_static_asserts is gone: every emitted
  struct, union and tuple is asserted.

test/emit_c/option_nullable_pointer.w: a Box list, HashMap.get, a
returned Option[&i32] and a raw-pointer Option in a record; fix-2022's C
printed `stride 24 24` for the model's 16.
…nof reads the model for every kind (#2039)

Box.new, Mutex.new, RwLock.new and generator cores allocated
`with_alloc(sizeof[T])`, whose payloads are 16-aligned: `b.v` of a Box[Al]
(`@[align(32)] v`) landed at 16 mod 32 half the time and a 128-aligned
value at no 128 boundary.

- Runtime: with_alloc_aligned(size, align) places the value at `align`
  inside a larger block. The two words below it describe the placement
  ([p-16] = align ^ RT_PLACED_TAG, [p-8] = the block), and the free path
  recognizes a placement by that description, checked against the
  allocator's tables. So with_free (Box's drop and into_inner, Mutex,
  RwLock, Pulled, Box[dyn] drop glue) takes the block back with no
  alignment argument. Vec buffers use the same placement.
- std: Box.new, Mutex.new, RwLock.new and the generator core pass
  alignof[T]; LLVM's Box.new intercept passes the payload's TypeLayout
  alignment; the C backend's closure cells pass _Alignof of their cell.
- alignof: CodegenDispatch.gen_sizeof_alignof read the model only for a
  struct or enum and LLVM's ABI alignment otherwise, so
  `alignof[(Al, i8)]` was 8 for the model's 32. Every kind now reads
  TypeLayout for Sema's type argument, and a missing Sema type is a loud
  internal error.

Vec keeps its size-derived alignment: its header (with-abi.md §3) has no
word for the element's alignment, and grow/free run with only the header.

behav_2039_heap_cells_over_aligned: alignof in a generic body (record,
Option, tuple), Box of 32- and 128-aligned values, eight Boxes in a row,
Mutex, RwLock, into_inner; the fix-2022 compiler printed
`alignof 32 8 8 32 8`, `box128 false`, `boxes aligned 4`, `mutex false`.
LLVM closure cells are #2042.
…lo the width, never a shift by the width (#1006)

The C backend lowered rotate_left/rotate_right for every integer type as
the 32-bit `(v << n) | (v >> (32 - n))`: a u8, u16 or u64 rotated as a u32,
and a count of 0 (or any multiple of 32) shifted by 32, undefined in C.
The LLVM backend's llvm.fshl/fshr (9475185) and comptime_rotate_bits take
the count modulo the width and leave the value at a count of 0.

rotate_text emits that at W = 8/16/32/64/128: the count masked to W - 1
(its unsigned modulo, so -1 is W - 1), a zero count yielding the value, the
shifts on the unsigned type of the value's width. Any other width refuses
loudly.

test/emit_c/rotate_widths.w: u8, i8, u16, u32, i32, u64 and i64 at counts
1 (right), 0, W, W + 1 and -1; the pre-9475185a LLVM build printed
garbage for every count of 0 or W.
@ehartford
ehartford added this pull request to stack #2045 October 3, 2026 15:17
…ls are allocated at its alignment (#2042)

A `move` closure's environment and its heap cell {drop_fn, clone_fn, env}
were the literal LLVM structs of their members, so an `@[align(32)]`
capture (§16.4) sat where LLVM's 8-byte view of its i8-padded body put it:
the environment at offset 16 of a 16-aligned cell. `(&raw const held.v) %
32` was false for a capture after another, for every closure made in a
loop, and for a 64-aligned str record.

- closure_env_llvm_type places the captures by TypeLayout's tuple rule: a
  by-value capture at its Sema type's model size and alignment, a by-place
  capture as a pointer. tuple_type_from_layout materializes it (padded
  when LLVM's placement differs), for closures and async blocks.
- closure_cell_llvm_type puts the environment at its own alignment after
  the two function pointers.
- Every capture and cell slot is reached through tuple_elem_ptr (creation,
  the closure prologue, the env drop and clone fns, async blocks).
- The cell, its clone and an async environment are allocated with
  with_alloc_aligned at the model alignment; a closure built in a loop
  aligns its stack environment as an entry slot is.

behav_2042_closure_env_over_aligned: a capture alone, after a byte, two
over-aligned captures, closures built in a loop, a Drop-holding 64-aligned
capture; the previous stage1 printed `after byte false`, `loop 0`, `drop
misaligned`. The behavior corpus's closure and async tests pass on the
new stage1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant