Skip to content

compiler: multi-module library support — shared globals need single-definition linkage #101

Closed

Description

@ehartford

Summary

When compiling a multi-module library (like lib/std/re/ with 32 .w files), the preamble from defs.w is inlined into every module via `use std.re.defs`. This creates per-module copies of shared global variables, all with their own storage. At link time, either:

  • They duplicate (external linkage → linker error)
  • They become internal (internal linkage → each module has its own zero-initialized copy, can't share state)

Neither works for variables that need a single shared definition with non-zero initialization.

Concrete example

`_pcre2_default_compile_context_8` is a struct variable that needs to contain malloc/free function pointers. It's declared in defs.w and inlined into 18 modules. At runtime, every module has its own zero-initialized copy. A C init file provides the correct values, but the With modules reference their own copies, not the C one.

What's needed

When `use foo.bar` imports a module, symbols from that module should be referenced, not redefined. Specifically:

  1. `let` constants from an imported module should resolve to the single definition in the defining module's .o file, not be re-emitted in every importer
  2. `var` globals from an imported module should generate `extern` references in importers, with storage only in the defining module
  3. Functions from an imported module should either be inlined or referenced as extern

This is the difference between C's `#include` (textual inclusion, current behavior) and a real module system (symbol-level linking).

Impact

Blocks PCRE2 runtime testing. pcre2test links but crashes because default context structs are zero-initialized in the With modules instead of using the C-initialized copy.

Workaround explored

  • Internal linkage for all lib/std/re/ vars: avoids duplicate symbols but every module gets its own zero copy
  • extern var for specific symbols via workflow post-processing: only works for the declaration module, not for modules that got the var from inlined defs.w
  • C constructor function to initialize defaults: writes to the C-side copy but With modules reference their own internal copies

Suggested approach

Short-term: make the `use` mechanism for multi-file libraries emit `extern` references for imported top-level bindings instead of re-emitting definitions.

Long-term: proper module-aware compilation where each module is compiled once and importers link against it.

Activity

  1. ehartford commented on Apr 10, 2026

    @ehartford
    ContributorAuthor

    Fixed on main in 3a4a57c (Fix module object import ownership).

    Root cause: --emit-obj still used whole-program import expansion, so imported top-level decls were parsed into each compilation unit and codegen re-emitted them as if they belonged to the current source file. Mutable globals had a partial extern workaround, but imported let and imported functions still materialized private copies in every importer.

    Fix: object-mode codegen now preserves declaration ownership by source file. Non-exported top-level bindings get a stable module-scoped link name derived from the owning source path, imported top-level globals are emitted as declarations instead of definitions, and imported top-level function bodies are skipped while keeping external declarations.

    Verified with:

    • WITH=./out/bin/with-stage2 make build
    • WITH=./out/bin/with-stage2 ./scripts/run_cli_selfhost_tests.sh
    • WITH=./out/bin/with-stage2 make selfcheck
    • WITH=./out/bin/with-stage2 make fixpoint

    Direct repro after the fix:

    • owner object defines __with_mod_<hash>__shared_fn, __with_mod_<hash>__shared_let, __with_mod_<hash>__shared_var
    • importer object sees all three as U and no longer carries local copies
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