Skip to content

c_import is linear in a header's declarations: emitted names are a hash set, decl locations an index - #1386

Merged
ehartford merged 1 commit into
mainfrom
cimport-emitted-name-set
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
cimport-emitted-name-set

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Two lookups scanned every entry on every query, so c_import cost grew with
the square of a header's declarations:

  • ClangBridge is_name_emitted walked the list of emitted names with
    c_strcmp on every lookup and every mark. It is now an open-addressing
    hash set. ClangBridge is compiled without the prelude, so it cannot use
    std HashMap.
  • CImport ci_get_decl_location walked every top-level cursor, making a
    spelling string for each, once per translated struct. It is now a
    name -> first cursor index, rebuilt when the session or parse generation
    changes and cleared at migration teardown, the same pattern as the c-migrate: compiler-C migration exceeds the 64 GiB memory cap #744
    indexes.

This is why behav_c_import_scales_linearly.w failed on the darwin,
linux-aarch64 and windows-x86_64 nightly legs (runs 35512407331 and
35616828206), and it is a cause of the bzip2/libcurl UAT timeouts over
windows.h on Windows. The installed compiler measured 0.72s / 5.88s /
23.3s at 750 / 3000 / 6000 declarations (8.2x for 4x the input); the test
allows 8x+1s, so a fast Mac passes and a CI runner does not.

Evidence: a sample profile at 6000 declarations had c_strcmp inside
is_name_emitted on top. Once that was fixed, lldb stack samples put 10 of
14 samples in ci_translate_struct -> ci_get_decl_location. A/B of seed-built
stage1 against the base commit, user CPU with fresh headers and about 3.25s
of fixed startup: 3000 declarations went from 15.0-17.3s to 8.4-11.0s,
750 declarations from 5.1-7.1s to 5.0-6.5s. The 40 c_import behavior
fixtures run the same on both.

Draft until the seed-driven battery is green (D56). This is a performance change, so :drop-audit is not needed. Iterate tier so far: build :dev rc=0, and the 40 c_import behavior fixtures give the same results on stage1 with and without the change (3 fail on both, the stage1-only class).

…sh set, decl locations an index

Two lookups scanned every entry on every query, so c_import cost grew with
the square of a header's declarations:

- ClangBridge `is_name_emitted` walked the list of emitted names with
  c_strcmp on every lookup and every mark. It is now an open-addressing
  hash set. ClangBridge is compiled without the prelude, so it cannot use
  std HashMap.
- CImport `ci_get_decl_location` walked every top-level cursor, making a
  spelling string for each, once per translated struct. It is now a
  name -> first cursor index, rebuilt when the session or parse generation
  changes and cleared at migration teardown, the same pattern as the #744
  indexes.

This is why behav_c_import_scales_linearly.w failed on the darwin,
linux-aarch64 and windows-x86_64 nightly legs (runs 35512407331 and
35616828206), and it is a cause of the bzip2/libcurl UAT timeouts over
windows.h on Windows. The installed compiler measured 0.72s / 5.88s /
23.3s at 750 / 3000 / 6000 declarations (8.2x for 4x the input); the test
allows 8x+1s, so a fast Mac passes and a CI runner does not.

Evidence: a `sample` profile at 6000 declarations had c_strcmp inside
is_name_emitted on top. Once that was fixed, lldb stack samples put 10 of
14 samples in ci_translate_struct -> ci_get_decl_location. A/B of seed-built
stage1 against the base commit, user CPU with fresh headers and about 3.25s
of fixed startup: 3000 declarations went from 15.0-17.3s to 8.4-11.0s,
750 declarations from 5.1-7.1s to 5.0-6.5s. The 40 c_import behavior
fixtures run the same on both.

(cherry picked from commit 816b15f)
@ehartford

Copy link
Copy Markdown
Contributor Author

Batch battery (seed-driven) on main 00408eb + #1386 → #1399 as one chain: build ok, :fixpoint ok, :test ok (no reds, incl. behav_c_import_scales_linearly), :test-green ok, :last-green ok. Branches now hold exactly the tested commits; merge #1386 then #1399.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 02:35
@ehartford
ehartford merged commit 641731d into main Sep 23, 2026
@ehartford
ehartford deleted the cimport-emitted-name-set branch September 23, 2026 02:40
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396)

Root cause: ci_translate_struct took with_cimport_decl_name, which for a
file-scope record with no tag and no typedef name is libclang's cursor
spelling `struct (unnamed at file:line:col)`, and emitted it as a type
name; the bindings failed to parse. Every use of such a record translated
as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so
`struct { int b; } g;` also imported as `extern var g: c_void`, an array of
it collapsed to `c_void`, and a function returning one was omitted.

ClangBridge now names each file-scope record with no name after the first
declaration, in translation-unit order, whose type reaches it: `g_anon`
(variable, also through arrays/pointers), `f_return_anon` / `f_p_anon`
(`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name
another declaration already spells takes the first free `_2` suffix. The
cursor spelling cache and type translation both answer with it, so the
record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon`
agree. Names depend only on declaration order and spellings: deterministic.
A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one
inside an expression — has no spelling in C or With and is not emitted
(with_cimport_decl_is_unused_anon_record), as the issue prescribes.

Two neighbours winnt.h/wtypes.h reach next, same root (records libclang
lists only under another record):

- A member record with no name held through an array or pointer
  (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as
  `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's
  member rule, `Parent_field`, CImport emits it, and an array of a demoted
  one demotes its holder. The flexible-array accessor rename replaced every
  occurrence of the field's spelling in the last field, including the new
  type name; it now renames the field only.
- A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`,
  and `enum { A, B } k;` constants) is file scope in C but was never
  collected: the field named an unknown type. hoist_nested_tags lists each
  just before the top-level record holding it. sqlite3.h's
  sqlite3_index_constraint/_orderby/_constraint_usage import with fields
  now instead of opaque.

Lookups stay linear in the header (#1386): a record finds its name through
a hash on its Decl, and a candidate name is checked against a hash set of
the declarations' spellings built once (emitted_name_slot), never a scan.

Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`,
`extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous
enum (constants), anonymous-in-anonymous members, function result and
prototype parameter, pointer typedef, pointer var, name collision, const
initializer, and the C_ASSERT expression record (not emitted).
Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const
initializer) and _uses.w (check-only: every declaration's type identity).
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396)

Root cause: ci_translate_struct took with_cimport_decl_name, which for a
file-scope record with no tag and no typedef name is libclang's cursor
spelling `struct (unnamed at file:line:col)`, and emitted it as a type
name; the bindings failed to parse. Every use of such a record translated
as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so
`struct { int b; } g;` also imported as `extern var g: c_void`, an array of
it collapsed to `c_void`, and a function returning one was omitted.

ClangBridge now names each file-scope record with no name after the first
declaration, in translation-unit order, whose type reaches it: `g_anon`
(variable, also through arrays/pointers), `f_return_anon` / `f_p_anon`
(`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name
another declaration already spells takes the first free `_2` suffix. The
cursor spelling cache and type translation both answer with it, so the
record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon`
agree. Names depend only on declaration order and spellings: deterministic.
A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one
inside an expression — has no spelling in C or With and is not emitted
(with_cimport_decl_is_unused_anon_record), as the issue prescribes.

Two neighbours winnt.h/wtypes.h reach next, same root (records libclang
lists only under another record):

- A member record with no name held through an array or pointer
  (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as
  `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's
  member rule, `Parent_field`, CImport emits it, and an array of a demoted
  one demotes its holder. The flexible-array accessor rename replaced every
  occurrence of the field's spelling in the last field, including the new
  type name; it now renames the field only.
- A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`,
  and `enum { A, B } k;` constants) is file scope in C but was never
  collected: the field named an unknown type. hoist_nested_tags lists each
  just before the top-level record holding it. sqlite3.h's
  sqlite3_index_constraint/_orderby/_constraint_usage import with fields
  now instead of opaque.

Lookups stay linear in the header (#1386): a record finds its name through
a hash on its Decl, and a candidate name is checked against a hash set of
the declarations' spellings built once (emitted_name_slot), never a scan.

Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`,
`extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous
enum (constants), anonymous-in-anonymous members, function result and
prototype parameter, pointer typedef, pointer var, name collision, const
initializer, and the C_ASSERT expression record (not emitted).
Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const
initializer) and _uses.w (check-only: every declaration's type identity).
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396)

Root cause: ci_translate_struct took with_cimport_decl_name, which for a
file-scope record with no tag and no typedef name is libclang's cursor
spelling `struct (unnamed at file:line:col)`, and emitted it as a type
name; the bindings failed to parse. Every use of such a record translated
as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so
`struct { int b; } g;` also imported as `extern var g: c_void`, an array of
it collapsed to `c_void`, and a function returning one was omitted.

ClangBridge now names each file-scope record with no name after the first
declaration, in translation-unit order, whose type reaches it: `g_anon`
(variable, also through arrays/pointers), `f_return_anon` / `f_p_anon`
(`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name
another declaration already spells takes the first free `_2` suffix. The
cursor spelling cache and type translation both answer with it, so the
record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon`
agree. Names depend only on declaration order and spellings: deterministic.
A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one
inside an expression — has no spelling in C or With and is not emitted
(with_cimport_decl_is_unused_anon_record), as the issue prescribes.

Two neighbours winnt.h/wtypes.h reach next, same root (records libclang
lists only under another record):

- A member record with no name held through an array or pointer
  (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as
  `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's
  member rule, `Parent_field`, CImport emits it, and an array of a demoted
  one demotes its holder. The flexible-array accessor rename replaced every
  occurrence of the field's spelling in the last field, including the new
  type name; it now renames the field only.
- A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`,
  and `enum { A, B } k;` constants) is file scope in C but was never
  collected: the field named an unknown type. hoist_nested_tags lists each
  just before the top-level record holding it. sqlite3.h's
  sqlite3_index_constraint/_orderby/_constraint_usage import with fields
  now instead of opaque.

Lookups stay linear in the header (#1386): a record finds its name through
a hash on its Decl, and a candidate name is checked against a hash set of
the declarations' spellings built once (emitted_name_slot), never a scan.

Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`,
`extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous
enum (constants), anonymous-in-anonymous members, function result and
prototype parameter, pointer typedef, pointer var, name collision, const
initializer, and the C_ASSERT expression record (not emitted).
Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const
initializer) and _uses.w (check-only: every declaration's type identity).
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396)

Root cause: ci_translate_struct took with_cimport_decl_name, which for a
file-scope record with no tag and no typedef name is libclang's cursor
spelling `struct (unnamed at file:line:col)`, and emitted it as a type
name; the bindings failed to parse. Every use of such a record translated
as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so
`struct { int b; } g;` also imported as `extern var g: c_void`, an array of
it collapsed to `c_void`, and a function returning one was omitted.

ClangBridge now names each file-scope record with no name after the first
declaration, in translation-unit order, whose type reaches it: `g_anon`
(variable, also through arrays/pointers), `f_return_anon` / `f_p_anon`
(`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name
another declaration already spells takes the first free `_2` suffix. The
cursor spelling cache and type translation both answer with it, so the
record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon`
agree. Names depend only on declaration order and spellings: deterministic.
A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one
inside an expression — has no spelling in C or With and is not emitted
(with_cimport_decl_is_unused_anon_record), as the issue prescribes.

Two neighbours winnt.h/wtypes.h reach next, same root (records libclang
lists only under another record):

- A member record with no name held through an array or pointer
  (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as
  `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's
  member rule, `Parent_field`, CImport emits it, and an array of a demoted
  one demotes its holder. The flexible-array accessor rename replaced every
  occurrence of the field's spelling in the last field, including the new
  type name; it now renames the field only.
- A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`,
  and `enum { A, B } k;` constants) is file scope in C but was never
  collected: the field named an unknown type. hoist_nested_tags lists each
  just before the top-level record holding it. sqlite3.h's
  sqlite3_index_constraint/_orderby/_constraint_usage import with fields
  now instead of opaque.

Lookups stay linear in the header (#1386): a record finds its name through
a hash on its Decl, and a candidate name is checked against a hash set of
the declarations' spellings built once (emitted_name_slot), never a scan.

Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`,
`extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous
enum (constants), anonymous-in-anonymous members, function result and
prototype parameter, pointer typedef, pointer var, name collision, const
initializer, and the C_ASSERT expression record (not emitted).
Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const
initializer) and _uses.w (check-only: every declaration's type identity).
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396)

Root cause: ci_translate_struct took with_cimport_decl_name, which for a
file-scope record with no tag and no typedef name is libclang's cursor
spelling `struct (unnamed at file:line:col)`, and emitted it as a type
name; the bindings failed to parse. Every use of such a record translated
as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so
`struct { int b; } g;` also imported as `extern var g: c_void`, an array of
it collapsed to `c_void`, and a function returning one was omitted.

ClangBridge now names each file-scope record with no name after the first
declaration, in translation-unit order, whose type reaches it: `g_anon`
(variable, also through arrays/pointers), `f_return_anon` / `f_p_anon`
(`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name
another declaration already spells takes the first free `_2` suffix. The
cursor spelling cache and type translation both answer with it, so the
record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon`
agree. Names depend only on declaration order and spellings: deterministic.
A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one
inside an expression — has no spelling in C or With and is not emitted
(with_cimport_decl_is_unused_anon_record), as the issue prescribes.

Two neighbours winnt.h/wtypes.h reach next, same root (records libclang
lists only under another record):

- A member record with no name held through an array or pointer
  (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as
  `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's
  member rule, `Parent_field`, CImport emits it, and an array of a demoted
  one demotes its holder. The flexible-array accessor rename replaced every
  occurrence of the field's spelling in the last field, including the new
  type name; it now renames the field only.
- A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`,
  and `enum { A, B } k;` constants) is file scope in C but was never
  collected: the field named an unknown type. hoist_nested_tags lists each
  just before the top-level record holding it. sqlite3.h's
  sqlite3_index_constraint/_orderby/_constraint_usage import with fields
  now instead of opaque.

Lookups stay linear in the header (#1386): a record finds its name through
a hash on its Decl, and a candidate name is checked against a hash set of
the declarations' spellings built once (emitted_name_slot), never a scan.

Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`,
`extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous
enum (constants), anonymous-in-anonymous members, function result and
prototype parameter, pointer typedef, pointer var, name collision, const
initializer, and the C_ASSERT expression record (not emitted).
Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const
initializer) and _uses.w (check-only: every declaration's type identity).
ehartford added a commit that referenced this pull request Sep 23, 2026
…first user (#1396); pointer type args in sizeof; nested records; unspellable layouts opaque (#1419)

* Parser: sizeof[*mut T]() / transmute[*const T](x) — a raw pointer type argument in expression position parses as a type (§16.12)

`*` followed by `mut` or `const` never dereferences anything, so inside
`name[...]` it begins a type, like the `fn`/`extern` function-pointer case
already beside it. c_import emits `sizeof[*mut c_void]()` for C's
`sizeof(PVOID)`; winnt.h's TOKEN_INTEGRITY_LEVEL_MAX_SIZE failed with
"expected expression" at `mut` once #1396's unnamed records parsed.

Test: behav_sizeof_pointer_type_arg.w.

* c_import: a record with no tag and no typedef name gets the name of the first declaration that uses it; one nothing uses is not emitted (#1396)

Root cause: ci_translate_struct took with_cimport_decl_name, which for a
file-scope record with no tag and no typedef name is libclang's cursor
spelling `struct (unnamed at file:line:col)`, and emitted it as a type
name; the bindings failed to parse. Every use of such a record translated
as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so
`struct { int b; } g;` also imported as `extern var g: c_void`, an array of
it collapsed to `c_void`, and a function returning one was omitted.

ClangBridge now names each file-scope record with no name after the first
declaration, in translation-unit order, whose type reaches it: `g_anon`
(variable, also through arrays/pointers), `f_return_anon` / `f_p_anon`
(`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name
another declaration already spells takes the first free `_2` suffix. The
cursor spelling cache and type translation both answer with it, so the
record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon`
agree. Names depend only on declaration order and spellings: deterministic.
A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one
inside an expression — has no spelling in C or With and is not emitted
(with_cimport_decl_is_unused_anon_record), as the issue prescribes.

Two neighbours winnt.h/wtypes.h reach next, same root (records libclang
lists only under another record):

- A member record with no name held through an array or pointer
  (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as
  `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's
  member rule, `Parent_field`, CImport emits it, and an array of a demoted
  one demotes its holder. The flexible-array accessor rename replaced every
  occurrence of the field's spelling in the last field, including the new
  type name; it now renames the field only.
- A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`,
  and `enum { A, B } k;` constants) is file scope in C but was never
  collected: the field named an unknown type. hoist_nested_tags lists each
  just before the top-level record holding it. sqlite3.h's
  sqlite3_index_constraint/_orderby/_constraint_usage import with fields
  now instead of opaque.

Lookups stay linear in the header (#1386): a record finds its name through
a hash on its Decl, and a candidate name is checked against a hash set of
the declarations' spellings built once (emitted_name_slot), never a scan.

Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`,
`extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous
enum (constants), anonymous-in-anonymous members, function result and
prototype parameter, pointer typedef, pointer var, name collision, const
initializer, and the C_ASSERT expression record (not emitted).
Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const
initializer) and _uses.w (check-only: every declaration's type identity).

* c_import: layouts With cannot spell import opaque — `_`-tagged records with bitfields, bitfields in member records, fields under their natural alignment (#1396)

Three holes in the demotion pre-scan, each an emitted record that either
failed to compile or compiled with the wrong layout:

- ci_collect_demoted_types skipped every tag with a leading underscore,
  though ci_translate_struct emits them: winbase.h's `_DCB` bitfields came
  out as whole DWORD fields with `@[align(1)]` ("alignment is less than
  natural"), and darwin's `union wait` as four 4-byte fields.
- ci_record_decl_directly_demoted_cursor (member records) never looked
  for bitfields (§16.9: a record with bitfields is opaque).
- A field below its natural alignment (`#pragma pack(2)`, wingdi.h's
  BITMAPFILEHEADER) was emitted `@[align(2)]`; §16.4 rule 2 forbids an
  alignment under the natural one, and `@[packed]` would drop the record's
  own alignment to 1. It imports opaque, usable by pointer, like a bitfield
  layout. An exact spelling needs a packed(N) layout the spec does not have.

Windows 10: the 146 "alignment is less than natural" errors in the
windows.h closure are gone. Darwin headers change only where they were
wrong: `union wait` is opaque; forward-declared `_` records gain the
"demoted to opaque" location comment.

Test: behav_c_import_nested_records.w (also covers the array/pointer member
records and nested tags of the previous commit).

---------

Co-authored-by: Eric Hartford <[email protected]>
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