Skip to content

Over-aligned elements: Vec buffers aligned for their element; emit-C records take TypeLayout's layout, asserted (#2022) - #2037

Merged
ehartford merged 2 commits into
mainfrom
fix-2022
Oct 3, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-2022

Conversation

@ehartford

@ehartford ehartford commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2022. Stacked on #2033.

Vec buffers (37feebe4, rt/rt_core.w): a Vec of an @[align(N)] element now gets an N-aligned buffer. The header stores only the element size, and TypeLayout rounds every size up to its alignment, so the largest power of two dividing the size (capped at §16.4's 65536) is at least the element's alignment. When that is more than 16, vec_buffer_alloc places the buffer at that alignment inside a larger block and keeps the block's start in the word just below the buffer. vec_grow, with_capacity and both free forms go through it. Trade-off: an element whose size is a multiple of 32 but whose alignment is lower also takes this path, at a cost of up to align extra bytes per buffer.

emit-C layout (7cc85779, src/CCodegen.w):

Tests (each failed before the change):

  • test/behavior/behav_2022_vec_over_aligned_elements.w: the old compiler printed aligned 0 for 3 of 4 rows.
  • test/emit_c/aligned_fields.w: the old C output printed outer offset 16, vec aligned 1.

Verification: :dev, :emit-c-smoke (27 cases), :drop-audit (254 cells, 0 regressions), and src/main build :gate are all green. The Vec fixture under --debug-alloc reports leak count 0.

Not covered: Box[T], Mutex[T] and other stdlib with_alloc(sizeof[T]) paths are still 16-aligned for over-aligned T.

@ehartford
ehartford added this pull request to stack #2038 October 3, 2026 10:59
@ehartford ehartford changed the title fix 2022 Over-aligned elements: Vec buffers aligned for their element; emit-C records take TypeLayout's layout, asserted (#2022) Oct 3, 2026
@ehartford
ehartford removed this pull request from stack #2038 October 3, 2026 11:27
An error occurred while trying to automatically change base from fix-2017 to impl-1903 October 3, 2026 15:13
Eric Hartford added 2 commits October 3, 2026 08:17
A Vec of an `@[align(N)]` element (§16.4) kept its elements in the
allocator's 16-aligned payload: `v[i].b` of a Vec[Al] (`@[align(32)] b`)
sat at 16 mod 32, and a 128-aligned element at no 128 boundary at all.

The header carries only the element size (with-abi.md §3), and TypeLayout
rounds every size up to its alignment (§2), so the largest power of two
dividing the size, at most §16.4's 65536, is at least the element's
alignment. vec_buffer_alloc places a buffer that needs more than 16 at that
alignment inside a block `align` bytes larger, the block's start in the
word below the buffer; vec_grow, with_vec_new_with_capacity_out and both
with_vec_free_buffer forms allocate and free through it. Elements whose
size is a multiple of 32 but whose alignment is lower take the same path
and pay up to `align` bytes per buffer.

behav_2022_vec_over_aligned_elements: Vec[Al] past three regrowths, a
128-aligned element, with_capacity, and a Drop-holding 64-aligned element;
the seed's compiler printed `aligned 0` for three of the four rows.
…sert; unions are unions (#2022)

`type Al { a: i8, @[align(32)] b: i64 }` was emitted `struct Al { int8_t
a; int64_t b; }`: 16 bytes against the model's 64, so `o.al.b` of
`Outer { c: u8, al: Al }` sat at offset 16 where the LLVM backend and every
TypeLayout constant put it at 64.

- A field with a declared alignment is emitted `_Alignas(N)`.
- The D72 hidden liveness byte (§2.5.1) is emitted after the last field of
  a record that has one.
- Every emitted struct, union and tuple is followed by _Static_asserts that
  its sizeof, _Alignof and each field's offsetof equal TypeLayout's: a
  difference is a C compile error naming the type and field.
- The asserts found a second divergence at once: a §16.4 union was emitted
  as a struct, its members end to end (pcre2's class_bits_storage 64 bytes
  for the model's 32, moving every later field of compile_block_8). Union
  types are now emitted `union`.
- Records holding a nullable-pointer Option are not yet asserted: this
  backend's Option[&T]/Option[Box[T]] is a tagged struct, not with-abi §3's
  pointer (#2036, filed).

test/emit_c/aligned_fields.w: the C program printed `outer offset 16
aligned false` and `vec aligned 1`; it now prints the LLVM backend's lines.
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.

emit-C ignores @[align(N)] (§16.4): a record's C struct is C's natural layout, not TypeLayout's (Al is 16 bytes in C, 64 in the model)

1 participant