Skip to content

D147: a migrated record with bitfields keeps C's layout; STC stc_core and hmap_test join the corpus (#2553) - #2559

Merged
ehartford merged 6 commits into
mainfrom
bitfields
Oct 22, 2026
Merged

ehartford merged 6 commits into
mainfrom
bitfields

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

D147: a migrated record with bitfields keeps C's layout (#2553)

Eric's ruling on #2553 ("Bless it", five points), implemented:

  • Storage units and accessors (points 3, 4). with migrate no longer demotes a bitfield record to opaque. Its bitfield members group into storage units — an unsigned integer of the unit's size, __bits_<k>, at clang's offset under @[repr(C)] — with a getter and a setter per named member (pub fn dist() -> c_uint, pub mut fn set_dist(v: c_uint)). Member reads lower to the getter; assignments, compound assignments and ++/-- to the setter; a designated init builds the record and runs the setters. Unnamed and zero-width members are padding; an enum-typed member's getter returns the enum's integer alias; a _Bool member is its bit; a volatile member is refused.
  • Stores truncate as C does (point 2). The setter masks to the width (a signed member two's-complement truncates): m->dist++ at 1023 stores 0 whatever the project's overflow setting.
  • One layout for every target or a loud refusal (point 1). A unit holding members of differing declared size (Itanium packs across, MSVC starts a new unit), a unit that is not a whole aligned integer (a packed record), and a bitfield in a member record are refused naming the record and the reason. A comptime assertion of the record's size and every unit's offset is emitted beside it, guarded on the migrating target's data model (sizeof[*const u8]() == 8): a corpus is compiled once per target (D38) and a pointer-sized member before a unit moves it on wasm32 legitimately (STC's cco_task_state was the first). sizeof, alignof and offsetof are comptime-evaluable now (ComptimeEval reads Sema's TypeLayout).
  • c_import (point 5). Keeps §16.9's demotion; the spec notes the migrator's representation is the intended one for both.

STC's stc_core (the coroutine runtime, cco_group { int32_t spawn_count:24; … }) joins the engine corpus and hmap_test (hmap_meta { uint16_t hashx:6, dist:10; }) the test corpus: 8 of 8 upstream STC programs pass, fresh and promoted, with no hand edits.

Also in this PR

  • codegen: a Unit module binding has no storage. const x: Unit = f() and let x: Unit = g() trapped LLVM (a void global). A comptime-evaluated one emits nothing; a runtime one runs its initializer before main for its effects. Fixture behav_unit_global_binding.w.
  • migrate: a compound assignment on a promoted left operand computes in int. uint32_t x:14; x += 5 lowered to c_int +% c_uint, which does not compile; it is ((x as c_int) +% 5) as c_uint.
  • migrate: --macro-prefix-map OLD=NEW (clang's -fmacro-prefix-map), so a __FILE__ a unit bakes names no staging directory; the STC test lane uses it, which keeps the engine modules shared across programs. The test promotion is atomic (a refused one had deleted every program's defs.w).
  • Filed Option.map(it as i64) in a compiler method links to an undefined bare ___closure in the multi-unit build #2558: Option.map(it as i64) inside a compiler method links to an undefined bare ___closure in the multi-unit build (worked around with a plain if).

Decision log: docs/meetings/2026-10-22-D147-bitfield-records-in-with-migrate.md. Fixtures: bs_check_migrate_stc_expression_forms (the positive record set and three refusals).

Evidence

Gate and battery: below.

Eric Hartford added 6 commits October 22, 2026 12:07
`const check: Unit = f()` and `let check: Unit = g()` trapped codegen
(`LLVMAddGlobal` of a void type; `getTypeSizeInBits` on it). A `Unit`
binding now gets no global: a comptime-evaluated one (the transform's
empty block) emits nothing, a runtime one runs its initializer before
main for its effects and stores nothing. The first one written was
D147's layout assertion (`const __ci_layout_R_check: Unit = …`).

Fixture: test/behavior/behav_unit_global_binding.w.
`with migrate` no longer demotes a bitfield record to opaque. Its
bitfield members group into storage units — an unsigned integer of the
unit's size, `__bits_<k>`, at clang's offset under `@[repr(C)]` — with a
getter and a setter per named member; member reads lower to the getter,
assignments, compound assignments and `++`/`--` to the setter, which
masks the value to the width (a signed member two's-complement
truncates, so `dist++` at 1023 is 0, as C stores it). A designated init
builds the record and runs the setters. Unnamed and zero-width members
are padding; an enum-typed member's getter returns its integer alias; a
`_Bool` member is its bit.

One layout for every target or a loud refusal (ruling point 1): a unit
holding members of differing declared size (Itanium packs across, MSVC
starts a new unit), a unit that is not a whole aligned integer (a packed
record), a volatile member, and a bitfield in a local or member record
are refused naming the record and the reason. A `comptime` assertion of
the record's size and every unit's offset is emitted beside it:
`sizeof`, `alignof` and `offsetof` are now comptime-evaluable
(ComptimeEval reads Sema's TypeLayout; the field is resolved by name
when the body is evaluated before its check).

`c_import` keeps §16.9's demotion; the spec notes the migrator's
representation is the intended one for both (point 5).

Also in the migrator: a compound assignment whose left operand is
promoted (a bitfield narrower than int, a small integer) computes in
int — the right operand is no longer cast to the unsigned result type
(`c_int +% c_uint` did not compile).

Fixtures: bs_check_migrate_stc_expression_forms gains the positive
record set and the three refusals. STC's stc_core unit and hmap_test
program join their lanes.

Decision log: docs/meetings/2026-10-22-D147-bitfield-records-in-with-migrate.md.
A `__FILE__` a translation unit bakes (stc_core's coroutine.h diagnostics,
ctest's assertion sites) spells the path clang saw, which named each
test program's staging directory (`source-<program>/../include/...`), so
the engine module differed per program and the shared engine/ layout
refused the promotion. `with migrate --macro-prefix-map OLD=NEW` forwards
clang's -fmacro-prefix-map (a further clang argument beside the include
paths, `with_cimport_add_clang_arg`); the STC test lane maps each
program's staging directory to "", so every program spells the header
`../include/stc/coroutine.h`.

The test promotion assembles beside the tree and publishes whole once
every program's engine copy agreed: a refused promotion had deleted every
program's defs.w before refusing.
…per data model

The layout assertion is guarded on the migrating target's data model
(`sizeof[*const u8]() == 8`): a migrated corpus is compiled once per
target (D38), and a pointer-sized member before a unit moves the unit on
wasm32 legitimately (`cco_task_state`, 40 bytes with its unit at 31 on
arm64) while the unit itself sits where C puts a whole integer member on
every target. A member record with a bitfield is refused where it would
render, not in the demotion predicate that scans every declaration the
headers bring in (sys/wait.h's `union wait`). An accessor's receiver
prints as the place with its own `unsafe` (`(unsafe m.meta[i]).dist()`),
parenthesized unless it is a name or a field.

The STC corpus is re-promoted with stc_core (the coroutine runtime) and
the test corpus with hmap_test: 8 of 8 upstream programs pass, fresh and
promoted.
The spec inventory lists `--macro-prefix-map` with the corpus migrations'
other build-passed flags (build/stc.w's); the migrator's option function
drops its explicit Unit return (unit-return-review). The stdlib sourcing
plan records D147's close of the pipeline half: 8 of 8 STC test programs.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on 4017f94 (seed-driven, with run tools/battery.w, wall 341 s): gate, uat, battery-checks, green evidence recorded. STC lanes: 8 of 8 upstream programs pass, fresh and promoted; :c-migrator-core-tests green.

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