Skip to content

Build time: a program that imports one compiler module lowers every body of the compiler (60 s of a 106 s build for a 31-line test) #3351

Closed

Description

@ehartford

Found on nightly #3231. test/internals_compiler/comptime_value_share_test.w is 31 lines and imports ComptimeValue, which imports Sema. WITH_PROFILE=1 with build of it on an 18-core host (main 8d6e104), 106 s wall:

[profile] frontend.sema  18646 ms  decls=14322
[profile] mir.lower      59479 ms  bodies=10616
[profile] llvm.units_total 17822 ms

MIR lowering is serial and lowers all 10,616 bodies the imports declare; the test calls three functions. On the 3-core nightly runner, beside the other lanes, that build ran past the 30-minute per-file guard (#3350 and the lane change after it work around the ordering, not the cost).

Expected: an executable lowers and emits what it can reach: main, exported functions, the methods a dyn use or a trait table needs, drop and clone glue of the types it constructs. Sema still checks every body (an error in an unused function is an error); lowering and codegen of a body nothing reaches is work nobody reads. The same cost is in every user program that imports a large module for one function.

Measure first: bodies reached from main in this test against 10,616; where mir.lower spends its 59 s (it is 5.6 ms a body, serial).

Activity

  1. ehartford commented on Nov 16, 2026

    @ehartford
    ContributorAuthor

    Measured (installed compiler, main b349b75): with analyze test/internals_compiler/comptime_value_share_test.w 'closure:call:main' reports reachable=11. The build lowers 10,616 bodies to run 11. The same holds for the 114 of 121 internals-tests that import a compiler module; on the Windows nightly they run out of memory building four at a time (#3031).

    Plan: lower_module (src/MirLower.w) lowers from roots instead of every NK_FN_DECL: main, exported and test functions, trait impl and Drop methods (reached through tables codegen builds, so kept as roots first), global initializers; each lowered body adds the functions its MIR calls or takes as values. A library or object build keeps lowering everything. Codegen refuses a call of a With function that has no body with a message naming it, so a root kind the walk misses is loud, never a wrong program.

  2. ehartford commented on Nov 16, 2026

    @ehartford
    ContributorAuthor

    Correction, from timing the phase step by step (dev compiler, branch reachable-lowering): the cost is not lowering unused bodies.

    I built the root-driven lowering behind a switch. It lowered 330 bodies in place of 11,939, and mir.lower still took 46 s. Inside lower_module: the function bodies take 18 ms; the specialization loop takes the rest, 42 s over 64 specializations. Nine of them each spend ~4.7 s in preregister_mir_types -> eager_type_caches_pass, which reported fresh=22672 every time: every type's layout computed again.

    Exact line. Sema.prepare_comptime_eval_copy (src/Sema.w) replaces the nine type caches and the enum-payload cache with empty ones. That was needed when a comptime evaluation worked on a copy beside a live original. Since #2449/D120 the evaluator takes the Sema whole (comptime_take_sema writes a placeholder back), so it is the one holder, and the evaluated Sema is written back with its caches empty. Each specialization whose recheck evaluates something (print, eprint, List.sort, BTreeSet.drop_entries) therefore costs one full recomputation of the layout of every type in the program.

    Fix (in battery): the caches stay; they are facts of type ids, which only grow. mir.lower for comptime_value_share_test: 51 s -> 3.9 s on the dev compiler.

    Lowering only reachable bodies is still worth doing (codegen and link of unused functions: ~13 s of LLVM here), but it is the smaller part and I dropped the experimental switch from this branch. What that work needs is recorded above.

  3. ehartford commented on Nov 16, 2026

    @ehartford
    ContributorAuthor

    Merged in #3359: code is generated only for the declared functions a program reaches (every body is still lowered and checked). LLVM time for the 31-line Sema-importing test 12.6 s -> 4.2 s on the dev compiler; internals-tests 337 s -> 260 s.

    What is left of that test's build time is Sema: checking all 19,462 declarations of what it imports (about 78 s of 99 s on the dev compiler, which also checks std and the corpora from source). That is the remaining part of this issue: check a body only when something needs it.

  4. ehartford commented on Nov 16, 2026

    @ehartford
    ContributorAuthor

    What is left, and a direction (2026-11-16)

    After #3359 the cost of a test that imports one compiler module is Sema over everything that module reaches: about 78 s of 99 s, 19,462 declarations, for a 31-line test. Two ways to remove it, and they are different in kind.

    A. Check a body only when something needs it. Zig does this: a declaration nothing references is never analyzed, so an error in it is never reported. With does not: every body of a compiled module is checked, and an error in an unused function is an error. Changing that changes what programs are accepted. Dropped: it trades a diagnostic for build time, and the mission asks the compiler to catch what it can.

    B. A module that has not changed is not checked again. This is what .wo bundles already do for the migrated engines (D38/D39, wo_bundles.md): the object is built once per (content, target, ABI), the program reads the interface (.wi: declarations, bodies removed) and Sema's lazy layer collects only what is demanded. Go's export data, Rust's rlib metadata and Swift's .swiftmodule are the same idea; none re-checks a dependency's bodies per importer. Applied here: the compiler's modules (src/) become a bundle of the tree, keyed by their content, built once per tree by the build; a test in test/internals* that says use Sema reads the interface and links the object. Every body is still checked, once, when the bundle is built. Nothing about the language changes.

    What B needs that does not exist yet:

    • an interface for modules that are not pub-clean the way a corpus is (the internals tests reach private functions on purpose: pub-lint-check exists because of it), so the test-facing interface must carry what tests name, without making it pub;
    • generic and specialization bodies in the interface (a corpus has few; Sema has many);
    • the bundle's key and store for a tree in flux (every src/ edit is a new key: the build already has the pieces in build/retention.w).

    For user programs the same mechanism is the answer to "a program that imports a large module for one function": a package compiled once. That is a larger design (package bundles) and is not proposed here.

    Prediction: B, for the compiler's own modules first, 70%. It is build-system work only and reversible. I have not started it: it is a new mechanism, and "how" is not "go". Say go and I take it as the next build-performance item.

  5. ehartford commented on Nov 17, 2026

    @ehartford
    ContributorAuthor

    Merged in #3393: the compiler's modules are a tree bundle (compiler-bundle), and the two internals lanes link it. internals-tests 260 s -> 16 s, internals-compiler-tests 10 s; the 31-line test's Sema 33.8 s -> 1.1 s. The mechanism and its limits are in wo_bundles.md ("Tree bundles") and the PR description.

    What is left of this issue is the same cost for user programs (a program that imports a large module for one function): a package compiled once. That is a design of its own (package bundles) and is not started.

  6. ehartford commented on Nov 17, 2026

    @ehartford
    ContributorAuthor

    Closing: this issue's repro is fixed. test/internals_compiler/comptime_value_share_test.w (31 lines, imports a compiler module) no longer checks, lowers or generates the compiler: it links the compiler's bundle (#3393; Windows and the interface check in #3397). Sema for it went from 33.8 s to 1.1 s; internals-tests from 260 s to 16 s on the Mac and 18 s on Linux.

    The same cost for a user's program that imports a large package is a design of its own and is #3401.

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