Skip to content

engine: emit layout-independent fragments directly #964

Closed

Description

@bpowers

Summary

The production salsa compiler is logically a per-variable, layout-independent compiler, but it reaches that result through the old whole-module compiler:

  1. Build a minimal ModelStage0 containing the variable and selected dependencies.
  2. Assign arbitrary concrete offsets in a per-variable mini-layout.
  3. Construct a mostly-empty compiler::Module for one variable and one phase.
  4. Emit layout-dependent CompiledModule bytecode.
  5. Reverse-map every concrete variable offset back to SymVarRef { name, element_offset }.
  6. 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-954 builds a fake ModelStage0, sequential mini-offset metadata, a VariableLayout, and a ReverseOffsetMap solely so later symbolization can recover names.
  • db/assemble.rs:719-819 clones the mini offset map, tables, dimensions, dimension context, and module refs into a one-variable compiler::Module; calls Module::compile; then immediately calls symbolize_bytecode, symbolize_static_view, and symbolize_module_decl.
  • The same compile-and-symbolize closure is independently reproduced for LTM synthetic variables and helpers in db/ltm/compile.rs:1507-1604 and :2519-2616.
  • compiler/symbolic.rs:5-13 documents the current pipeline explicitly as concrete bytecode -> symbolize -> symbolic bytecode -> resolve -> concrete bytecode.
  • compiler::Module remains 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_errors and ModelStage0 / ModelStage1.errors are 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 Module path 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:

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::Module literal or Module::compile call 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::None path silently loses the cause.

Related, not duplicates

Activity

  1. added
    engine
    rust
    on Jul 15, 2026
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

    enginerusttech debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions