Skip to content

c-migrate: compiler-C migration exceeds the 64 GiB memory cap #744

Closed

Description

@ehartford

Repro

with build :emit-c-roundtrip
# or directly, after the roundtrip has produced out/emit-c-roundtrip/main.c (~48 MB):
out/release/bin/with migrate out/emit-c-roundtrip/main.c -o /tmp/main_roundtrip.w \
  -I runtime -include out/gen/wl_decls.h --no-c-export

Actual

error: with: memory limit exceeded: committed=68719426576 requested=65536 limit=68719476736 bytes

The CLI's default build memory cap is 64 GiB (CLI_DEFAULT_BUILD_MEMORY_LIMIT_BYTES,
overridable via the documented env var; 0 disables). Committed bytes on a 48 MB C
input exceed it — ~1300x the input size.

Evidence so far

  • Observed RSS: ~44 GB at 4.5 min into the run; ceiling (64 GiB committed) hit at ~9-10 min.
  • After d39fd50 (large-range ownership checks binary-search; previously ~90% of CPU),
    the CPU profile during the climb is dominated by ci_str_matches_at /
    ci_find_str via ci_lookup_macro_value / ci_lookup_known — migrator text
    scanning, i.e. real work, so this is retention, not scan overhead.
  • Cursor/children cache tables in src/compiler/ClangBridge.w (store_cursor,
    ensure_children_cached) are linear in TU size — estimated ~1 GB for this input,
    so they cannot account for the bulk. Prime suspects: per-expression clang string
    copies (session_make_str / session_strdup — session-arena strings retained
    until session end), tokenization buffers, and CiIR pools retained TU-wide instead
    of per-decl.

Expected

Migration of the emitted compiler C completes within the default cap. Peak memory
should be roughly proportional to the largest declaration being lowered plus
TU-level indexes, not to the whole TU's expression count.

Impact

Blocks with build :emit-c-roundtrip at the migrate-compiler-c step (its other
known blockers were fixed in 1d4fa51 / 29a369b / abf1e2e / d39fd50; migration
now reaches ~11 min of real work before dying at the ceiling).

Notes

  • Do not raise the cap or the step timeout to make the target green; the retention
    is the defect (mission: leaks must take deliberate effort).
  • First step is attribution: an allocation-site histogram at ceiling-exceeded (or a
    debug-allocator mode that dumps live-bytes-by-site) would settle where the bytes
    live before any redesign.

Activity

  1. ehartford commented on Jul 31, 2026

    @ehartford
    ContributorAuthor

    Investigation complete — primary cause is the pending #691 str flip; two migrator amplifiers fixed in 22d0950

    Method

    WITH_ALLOC_SYSTEM=1 MallocStackLogging=lite run sampled with
    malloc_history -allBySize at ~13 GB footprint, plus lldb breakpoint
    sampling on session_strdup/rt_alloc_unlocked (the malloc_history
    unwinder cannot walk pure-With frames; lldb can), plus a debug-alloc leak
    probe on a reduction.

    Findings

    1. Primary: transient str drops do not free. A 1000-iteration loop
      binding one f-string per iteration reports 2002 debug-alloc leaks — str
      buffers are pending the Campaign: the wide flip — containers own their buffers (POD-element drop elaboration) #691 wide-flip stage (docs/decisions.md: 'the
      Campaign: the wide flip — containers own their buffers (POD-element drop elaboration) #691 str flip must decide what Copy means for str-bearing structs'; the
      Vec-buffer half landed). Migrating 48 MB of C creates hundreds of millions
      of transient strings (type spellings, cursor source text, rendered
      fragments); with no frees, committed bytes reach total-ever-allocated.
      Measured peak with the cap disabled: 68.4 GB, with a sharp drop at
      session dispose — i.e., the classic leak-until-arena-teardown curve. Any
      comparably string-heavy workload dies the same way until the str flip
      lands; the migrator is just the first one this big.

    2. Amplifier A (fixed): per-probe session retention in decl lookups.
      ci_lookup_c_function_decl_idx / ci_lookup_c_function_return_type
      rescanned all decls per call and every probe session-retained a fresh
      with_cimport_decl_name copy — 82M+ live strings at ~1/4 progress;
      5/5 lldb-sampled allocations went through this path. Now one-pass
      owned-copy indexes, keyed by session + a new parse generation.

    3. Amplifier B (fixed): duplicate cursor stores.
      with_cimport_decl_cursor re-stored the cursor per call; each fresh
      index rebuilt whole subtree children caches (4 GB / ~125M cursors at the
      same sample). Now memoized per decl.

    4. Sibling rescan loops remain in ci_type_field_count/name/type
      (CImport.w:14634+) — same shape, not observed dominating; worth the same
      index treatment when touched.

    5. Behavior preservation: the fixed migrator's output is byte-identical
      to the pre-fix compiler's on a prelude-scale input (5 MB fixture C).

    Measurements

    Configuration Outcome
    pre-session migrate died at the 900 s step timeout (~90% CPU in the allocator's linear large-range scan; fixed d39fd50)
    post-d39fd508, 64 GiB cap ~11 min of real work, killed by the cap at ~9–10 min
    post-22d09501, cap disabled peak 68.4 GB, phases complete in sequence; run killed at 642 s by an environment reaper (below), not by the code

    Environment caveat that blocked final verification

    This dev box (macOS 26.5) SIGKILLs long CPU-pegged processes at ~10–11
    minutes regardless of nohup/disown or launchd hosting — 66 launchd-looped
    roundtrip attempts all died at migrate-compiler-c with rc=137 and empty
    (buffer-lost) captures, with no jetsam/memorystatus log entries. The full
    migrate needs one uninterrupted ~11-minute run; from an interactive terminal:

    WITH_MEMORY_LIMIT_BYTES=0 with build :emit-c-roundtrip

    (Once migration completes, the next known fallout item is #746 —
    positional struct literals from the goto-CFG memcpy path.)

    Where this leaves the fix

  2. ehartford commented on Jul 31, 2026

    @ehartford
    ContributorAuthor

    D28 ruled (both predictions confirmed by Eric): the fix is the #691 str flip, now filed as #747 with the de-Copy conformance list; ephemeral view-structs (the view-token mechanism's new shape) filed as #748. Interim: the roundtrip's migrate step disables its memory cap per D28 ruling 3 (bdcb9b3), revert condition named in the code — the #608/#693 pin doctrine. This issue stays open to track the un-pin: it closes when #747 lands and the migrate step fits the default cap again.

  3. ehartford commented on Aug 2, 2026

    @ehartford
    ContributorAuthor

    Roundtrip update: the migrate step's i32 session-string-counter overflow is fixed (record/typedef lookups indexed per the fn-index pattern; counters widened to i64 — commit on main). With that fixed, the step now dies at max RSS 94.4 GiB (OS kill, no jetsam log): the D28-acknowledged transient-str retention has outgrown this machine — the pre-#749 baseline peaked 68.4 GB and survived; the #749 payload-enum/access-hop translation plus #750-era work added enough transient string churn to cross physical limits. The step is memory-infeasible until #747 (the str flip) lands, which is already next in the D29 sequence. The RSS trace and repro command are in docs/handoff.md.

  4. added a commit that references this issue on Aug 17, 2026
  5. ehartford commented on Aug 31, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-08-31: CLOSE (overcome by events). The cap failure is dead: retention root-caused and fixed (22d0950, d39fd50, 7af076d) plus the #747 flip removing the transient class; the full 47.6MB compiler-C migration has since completed (~2GB peak) and been censused. Remaining roundtrip redness is downstream compile errors, tracked elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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