0283. Shared runtime state for split C units
- Status: Accepted (maintainer, 2026-11-22; "I'm fine with these" for O5, including the frozen-file edit this record lists)
- Date: 2026-11-22
- Plan: O5 / M20.5 (visibility slice); the unit splitter stays a later M20.5 task
- Builds on: ADR-0045 (runtime headers), ADR-0233 (TinyCC dev loop)
Context
Research #7 measured a dashboard gcc -O2 build at 7.46 s with
one generated C unit and 2.48 s with eight. Design Critic #15
asked to split units only for optimized builds and caching, not
the TinyCC dev loop. A survey of the tree found dozens of mutable
file-scope static variables in runtime/
and plugins/*/native/ headers (stdout buffering,
fuel, arenas, cli argv, agent handles, and similar). If each
compiled unit includes those headers, it gets a private copy, so
output, traps, fuel and plugin state diverge. The splitter is
unsound until that state has exactly one definition. Maintainer
approved the frozen-file edit this record lists.
Decision
- Visibility. Mutable file-scope state in
runtime and plugin native headers is declared
externand defined in exactly one translation unit.runtime/trt_state.hprovidesTRT_STATE/TRT_STATE_INIT. The unit that should hold the definitions compiles withoutTRT_NO_STATE_DEFS;tenon_rt.hthen setsTRT_DEFINE_STATEand the headers emit definitions. Other units (once they exist) compile with-DTRT_NO_STATE_DEFSand see only declarations. Immutablestatic consttables may staystatic. - Not in this slice.
split_units,TENON_C_UNITS, parallel compile and per-unit caching stay later M20.5 work. They apply only toBuildMode::ReleasewithCcSpeed::Normal(never the TinyCC dev loop). - Frozen files this record authorizes,
file-scope mutable state only:
plugins/cli/native/trt_cli.h,trt_net.h,trt_io.h;plugins/agent/native/trt_agent.h,trt_ws_api.h, and the probe-serialstaticintrt_ws.h. Function-localstaticin those inline headers is lifted to file scope so it also has one definition. Runtime headers and non-frozen plugin headers follow the same policy in a later non-frozen slice; they are not blocked on further owner approval.
Alternatives considered
- Keep
staticand only split functions that need no shared state: too little of the generated program is independent of fuel, arenas or I/O. - Have the compiler emit
externdeclarations and a definition unit: same end state, but it couples the splitter to a generated symbol list. Header macros stay correct when a shim is compiled on its own. TRT_SINGLE_UNITthat keeps today'sstaticfor dev builds: two copies of every header to maintain. One extern/definition scheme works for both the current single TU and later split units.
Consequences
Single-TU programs keep the same observable behavior: one
definition of each object still lives in the generated
main.c. Split optimized builds become possible after
the remaining headers and the splitter land. A later unit that
forgets -DTRT_NO_STATE_DEFS will fail at link time
with duplicate symbols, which is the desired failure.