You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
compiler: multi-module library support — shared globals need single-definition linkage #101
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:
`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
`var` globals from an imported module should generate `extern` references in importers, with storage only in the defining module
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.
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.
Activity
ehartford commentedon Apr 10, 2026
Fixed on
mainin3a4a57c(Fix module object import ownership).Root cause:
--emit-objstill 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 importedletand 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 buildWITH=./out/bin/with-stage2 ./scripts/run_cli_selfhost_tests.shWITH=./out/bin/with-stage2 make selfcheckWITH=./out/bin/with-stage2 make fixpointDirect repro after the fix:
__with_mod_<hash>__shared_fn,__with_mod_<hash>__shared_let,__with_mod_<hash>__shared_varUand no longer carries local copies