Repository navigation
Description
Summary
The production salsa compiler is logically a per-variable, layout-independent compiler, but it reaches that result through the old whole-module compiler:
- Build a minimal
ModelStage0containing the variable and selected dependencies. - Assign arbitrary concrete offsets in a per-variable mini-layout.
- Construct a mostly-empty
compiler::Modulefor one variable and one phase. - Emit layout-dependent
CompiledModulebytecode. - Reverse-map every concrete variable offset back to
SymVarRef { name, element_offset }. - Merge and renumber the symbolic fragments, then resolve the names back to final concrete offsets during assembly.
This works, but the address round trip and fake whole-module context are accidental complexity. They make production retain much of the monolithic compiler shape, duplicate fragment assembly code, and force new resource-layout behavior to keep matching the legacy path.
Severity: medium architectural tech debt. There is no known wrong result caused solely by the round trip, but it increases change surface and has already amplified performance and fragment-assembly work.
Evidence
db/var_fragment.rs:777-954builds a fakeModelStage0, sequential mini-offset metadata, aVariableLayout, and aReverseOffsetMapsolely so later symbolization can recover names.db/assemble.rs:719-819clones the mini offset map, tables, dimensions, dimension context, and module refs into a one-variablecompiler::Module; callsModule::compile; then immediately callssymbolize_bytecode,symbolize_static_view, andsymbolize_module_decl.- The same compile-and-symbolize closure is independently reproduced for LTM synthetic variables and helpers in
db/ltm/compile.rs:1507-1604and:2519-2616. compiler/symbolic.rs:5-13documents the current pipeline explicitly as concrete bytecode -> symbolize -> symbolic bytecode -> resolve -> concrete bytecode.compiler::Moduleremains a production type because of this bridge, while its full constructor is#[cfg(test)](compiler/mod.rs:2585-2604,:2788-2990). Several fields are already#[allow(dead_code)].Variable.errors/unit_errorsandModelStage0/ModelStage1.errorsare still populated or stubbed by this legacy-shaped lowering path, despite salsa diagnostics being the production source of truth (tech-debt item 17).
The resulting coupling is visible in FragmentMerger: substantial logic and tests are expressed as matching monolithic Module::compile resource layout (temp recycling and GF block layout), even though the production unit of compilation is a symbolic fragment.
Why simplify this
A fragment compiler should consume one lowered variable/phase plus named dependency metadata and directly produce:
- symbolic opcodes;
- fragment-local literals, lookup tables, module declarations, views, temps, and dimension lists;
- structured compile diagnostics.
Only final assembly should assign model-global variable offsets and merge fragment-local resource IDs. That gives the compiler one production architecture instead of a fragment pipeline wrapped around a whole-module compiler.
Expected benefits:
- remove the fake
ModelStage0/ mini-layout / reverse-offset-map lifecycle; - eliminate concrete -> symbolic -> concrete address conversion and its failure modes;
- consolidate explicit, implicit, and LTM fragment emission behind one entry point;
- reduce per-fragment cloning and setup overhead, especially for LTM-heavy models;
- make resource ownership and diagnostics local to the fragment compiler;
- allow the remaining monolithic
Modulepath and legacy diagnostic fields to become genuinely test-only or be removed.
Staged approach
1. Pin current behavior
Add characterization tests that compare the current bridge with a new direct-fragment entry point for representative scalar, stock, array/A2A, lookup, module, PREVIOUS / INIT, vector-op, and resolved-SCC cases. Compare both symbolic fragments and final VM results. Add a cache regression showing that a layout-only edit does not recompute an unchanged fragment.
2. Separate emission semantics from address encoding
Introduce a narrow fragment compile context containing named variable/element references and fragment-local resources. Split codegen so load/store/view/module reference emission targets an address-neutral sink (or an equivalent small abstraction). Keep the existing concrete sink temporarily for parity tests; add a symbolic sink that emits SymbolicOpcode / symbolic views and module declarations directly.
Avoid cloning Module into a second fragment-shaped container. The new context should contain only data required to emit one variable phase.
3. Make lowering references layout-independent
Change the per-variable lowering boundary so references reach fragment codegen as names plus element offsets rather than arbitrary mini-layout offsets. This may be a dedicated fragment expression/reference type or a generic reference representation; it should not require constructing VariableLayout plus ReverseOffsetMap.
Migrate compile_phase_to_per_var_bytecodes first, then route explicit variables, implicit helpers, and both LTM entry points through that single implementation.
4. Keep layout work at assembly
Retain one late pass that merges fragment-local resources and resolves symbolic variable references against the final VariableLayout. Simplify FragmentMerger invariants around fragment semantics instead of parity with monolithic compiler internals. Do not change bytecode serialization assumptions; VM bytecode is not persisted.
5. Delete scaffolding after parity is established
Once production has no compiler::Module literal or Module::compile call:
- move/delete the monolithic compiler path and migrate tests that still depend on it;
- remove the fake
ModelStage0construction where no other lowering concern requires it; - remove legacy
errors/unit_errorsfields per tech-debt item 17; - resolve or narrow the legacy compiler/cycle-gate debt in engine: two divergent circular-dependency cycle-gate code paths (System A salsa vs System B model.rs:442) #568.
Acceptance criteria
- Production per-variable compilation emits layout-independent fragments directly; it does not create a concrete variable layout and reverse-map it solely for symbolization.
- No production
compiler::Moduleliteral orModule::compilecall remains in the salsa fragment path, including LTM helpers. - Explicit, implicit, and LTM variables share one fragment emission implementation.
- Final assembly is the only point that resolves symbolic variable names to model-global offsets.
- Existing engine and corpus tests retain numerical parity; focused tests cover every reference-bearing opcode/view/module declaration family.
- Layout-only project edits continue to reuse unchanged salsa-cached fragments.
- Compile failures remain structured and attributable to a variable/phase; no
Option::Nonepath silently loses the cause.
Related, not duplicates
- ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655 tracks measured LTM per-fragment overhead (dimension/context setup and repeated parsing), not the compiler architecture that causes the
Modulebridge. - engine: two divergent circular-dependency cycle-gate code paths (System A salsa vs System B model.rs:442) #568 tracks the divergent legacy circular-dependency gate, not fragment emission.
- engine: models with many shared graphical functions fail to compile #582 and engine: an equation needing over 256 temp arrays silently aliases them #583 track resource-layout consequences in fragment merging.
docs/tech-debt.mditem 17 tracks only legacy embedded error fields; it can be completed as the final cleanup stage here.
Activity