Skip to content

engine: C-LEARN fails with "failed to compile fragments" #580

Closed

Description

@bpowers

Once C-LEARN's false circular-dependency error was fixed, compiling it failed one step later with

failed to compile fragments for variables: $⁚pct_change_in_gdp_from_rs⁚0⁚arg0⁚remaining_developed, ... rs_global_ch4, rs_global_pfc, rs_global_sf6, rs_global_n2o, global_rs_co2_ff, rs_ff_co2_ff_aggregated

There were two causes.
About 140 internal helper variables (one per element of a PREVIOUS/INITIAL argument) failed because the dependency pass compared a dimension's display name (Aggregated Regions) with its canonical name in a mapping, so a mapped dimension was missing when the helper was compiled.
Six real variables failed because an arrayed graphical function applied inside SUM or VECTOR SELECT, as in SUM(RS CH4[COP!](Time/One year)), had no code-generation path.

Status

Fixed on main by #592: the name comparison is canonical, and a per-element graphical function applied to an index now produces an array the reducer can read.
Fixing it exposed #582 and #583.
C-LEARN now compiles and runs in the default test suite (simulates_clearn).

Pointers

  • src/simlin-engine/tests/integration/simulate.rs: simulates_clearn

Activity

  1. added
    bug
    engine
    on May 19, 2026
  2. bpowers commented on May 19, 2026

    @bpowers
    OwnerAuthor

    Root-cause correction — Bug A (verified during Phase 6 Task 4, commit b25dc06)

    The "Root-cause direction" above attributed Bug A (the ~140 synthetic `$⁚⁚0⁚arg0⁚` temp-arg helpers) to `make_temp_arg`/`substitute_dimension_refs` + `DimensionsContext::translate_via_mapping`/`translate_to_source_via_mapping` bailing on a group/unequal-cardinality mapping (`dimensions.rs:455-457`/`:513-515`), with a prescribed fix of a new `translate_via_group_mapping` resolver.

    That diagnosis was empirically disproved before the fix was written (two rounds of in-situ probes). The group mapping's `element_map` IS fully expanded by `build_element_map`, and `translate_via_mapping`'s `element_map` branch resolves the per-element correspondence correctly — once the mapped dimension is present in the per-variable `DimensionsContext`.

    The actual root cause is in `db.rs::expand_maps_to_chains` (the salsa-invalidation-scoping pass that decides each variable's `DimensionsContext` subset): `SourceDimension.name` retains the as-written display casing (`"Aggregated Regions"`) while `maps_to`/`mappings[].target` are stored canonical (`"cop"`) by the MDL/XMILE importers. The reachability passes compared them with a raw `==`, so the reverse-mapping pass never pulled the mapped source dimension into the context. With the dimension absent, `substitute_dimension_refs` saw `has_mapping_to == false` and left the bare `[aggregated_regions]` full-dimension subscript → `DimensionInScalarContext` → all-`None` helper fragment → `missing_vars`. A `translate_via_group_mapping` would have been dead code (still gated on the absent dimension being in `ctx`).

    As-built fix: `expand_maps_to_chains` now canonicalizes every reachability comparison and resolves each canonical target back to its display name before insertion — fixing both the reverse direction (the C-LEARN bug) and a latent forward-direction bug for importer-sourced models. `substitute_dimension_refs` and `dimensions.rs` are left byte-identical (so the shared LTM/mapping consumers are untouched — structurally, not just by test). A loud-safe companion in `lower_implicit_var` re-checks the post-`lower_variable` errors and returns `None` with a per-helper diagnostic on any residual un-translatable shape.

    Result: the ~140 synthetic temp-arg helper names are gone from `compile_project_incremental`'s fatal `Err`. #580 is NOT yet resolved — the residual is exactly Bug B's six real arrayed vars (`rs_global_pfc/sf6/ch4/n2o`, `global_rs_co2_ff`, `rs_ff_co2_ff_aggregated` — the `SUM(arrayed-GF(...))` / `VECTOR SELECT` isolated-recompile failure), addressed by Phase 6 Task 5. #580 closes when Task 5 lands.

    (A distinct, pre-existing observability gap surfaced by this same work — assembly-stage `CompilationDiagnostic`s being silently discarded because `IN_TRACKED_CONTEXT` is never set `true` — is being tracked separately.)

  3. bpowers commented on May 19, 2026

    @bpowers
    OwnerAuthor

    Bug B fixed; a distinct GF-id capacity overflow is the new C-LEARN residual (Phase 6 Task 5, commit ad432fb)

    Bug B root-cause correction (verified during execution)

    The "Bug B" section above attributed the 6 real arrayed vars' failure to db_var_fragment.rs not reconstructing the arrayed-GF dependency's dimensions / per-element tables in the mini-layout. That diagnosis was empirically disproved. In-situ probes established: lower_var_fragment's Var::new succeeds for all 6 vars (no build_var error); the tables map IS correctly populated with the dep's per-element tables (e.g. rs_co2_ff: 7 tables) and the dep dims ARE correct (StaticSubscript [7] over cop). The failure is in compile_phase_to_per_var_bytecodes's module.compile() (the symbolic-bytecode-emission stage, db.rs:3203) — and the Var::new output is byte-identical to what the (deleted) monolithic path produced, so it is not a per-variable-isolation gap.

    The actual root cause is a general engine codegen gap, not a stub/dep-table problem: Var::new lowers a per-element arrayed graphical function applied with an index — SUM(RS X[COP!](Time/One year)) / VECTOR SELECT(..., RS CO2 FF[COP!](Time/One year)*..., ...) — to App(Lookup(<full COP array view>, scalar-index)), a per-element GF lookup over an array. No codegen path materialized that as an array view: walk_expr_as_view (the reducer arg path) fell through with Generic, "Cannot push view for expression type ... expected array expression" (the SUM shape), and extract_table_info (the scalar/vector arg path) rejected the multi-element StaticSubscript table base with BadTable, "range subscripts not supported in lookup tables" (the VECTOR SELECT shape). The two symptoms are the same root cause. This shape was never implemented (no prior test of SUM(arrayed-GF[D!](index))), not "masked by the cycle gate" — the cycle/Bug-A fixes simply let the 6 fragments reach assembly where the gap surfaces.

    As-built fix (commit ad432fb)

    A new Opcode::LookupArray { base_gf, table_count, mode, write_temp_id } (mirroring VectorSortOrder: pops the scalar index, reads the GF array view, writes the per-element-lookup array to a temp; symbolize/desymbolize/renumber arms added, no element offsets so layout-independent like Lookup). Pass1Context::transform (the Expr3 array-decomposition pass) now decomposes a per-element arrayed-GF apply into its own AssignTemp(t, App(Lookup(g[*], idx)), view) + TempArray(t) at a single point (transform_inner's App arm) — so it is a TempArray the surrounding reducer / vector op / arithmetic consumes as an ordinary array view, instead of an enclosing Op2 being hoisted whole and burying the un-walkable App(Lookup) in a BeginIter body. Codegen's AssignTemp arm emits LookupArray for the bare-App(Lookup) body. Model-agnostic unit tests (arrayed_gf_in_reducer_tests::sum_of_arrayed_gf_compiles_and_simulates, ::vector_select_of_arrayed_gf_compiles_and_simulates) compile + simulate to hand-computed values. All 6 C-LEARN vars now compile their fragments (rs_global_ch4/n2o/pfc/sf6, global_rs_co2_ff, rs_ff_co2_ff_aggregated are gone from compile_project_incremental's missing_vars).

    New residual: distinct GF-id capacity overflow

    With all 6 fragments now compiling and entering assemble_module's concatenate_fragments, the C-LEARN compile surfaces a distinct, previously-masked, general failure:

    Error { kind: Simulation, code: NotSimulatable, details: Some(
      "graphical function offset 493 exceeds GraphicalFunctionId capacity (u8::MAX = 255)") }
    

    This is not arrayed-GF-specific and not Bug B: GraphicalFunctionId = u8 (bytecode.rs:21); concatenate_fragments's absorb (compiler/symbolic.rs) appends every fragment's graphical_functions to merged_gf with no de-duplication — each fragment carries its referenced deps' tables (via extract_tables_from_source_var), so a dep arrayed GF referenced by many consumers is duplicated per fragment. C-LEARN has only ~165 distinct GF tables, but the per-fragment duplication accumulates merged_gf.len() to 493+, and renumber_opcode's gf_off > u8::MAX guard (symbolic.rs:1546) trips on the first fragment past offset 255 (the scalar Lookup opcode's base_gf: u8 is renumbered the same way — this would fire for any opcode with a GF base in such a fragment). It was masked because before this fix all 6 arrayed-GF-referencing fragments were dropped from all_fragments, so their dep tables never accumulated. The monolithic Module::compile deduplicates GF tables (one per variable, via Compiler::new's module.tables loop), so this is a per-fragment-vs-monolithic divergence in the fragment-concatenation GF remapping, requiring either cross-fragment GF de-duplication in concatenate_fragments or widening GraphicalFunctionId/gf_off to u16 — both substantial and orthogonal to Bug B.

    #580 Bug B (the codegen gap) is fixed. This GF-id overflow is a separate latent bug now blocking the C-LEARN structural gate (Phase 6 AC7.1).

  4. bpowers commented on May 19, 2026

    @bpowers
    OwnerAuthor

    The "distinct GF-id capacity overflow" residual described in the comment above is now tracked separately as #582 (engine: GraphicalFunctionId u8 overflow in concatenate_fragments (no cross-fragment GF de-duplication)). #580's Bug B (the arrayed-GF-in-reducer codegen gap) is fixed by ad432fb; #582 is the independent fragment-concatenation defect that fix unmasked.

  5. changed the title [-]C-LEARN: post-cycle-gate assemble_simulation fragment-compile failure on synthetic temp-arg helpers[/-] [+]engine: C-LEARN fails with "failed to compile fragments"[/+] on Oct 12, 2026
  6. bpowers commented on Oct 12, 2026

    @bpowers
    OwnerAuthor

    Closing: already fixed on main.

    simulates_clearn (src/simlin-engine/tests/integration/simulate.rs:4011) is un-ignored and compiles C-LEARN via compile_project_incremental(...).unwrap() before comparing to Ref.vdf, so any "failed to compile fragments" error fails the default suite. The arrayed-GF-in-reducer shape is also pinned by sum_of_arrayed_gf_compiles_and_simulates / vector_select_of_arrayed_gf_compiles_and_simulates (src/array_tests.rs:4822, 4882).

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

    bugengine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions