Skip to content

engine: an equation needing over 256 temp arrays silently aliases them #583

Open

Description

@bpowers

The bytecode names a temp array (scratch storage for results such as VECTOR SORT ORDER) with an 8-bit id.
Code generation narrows each id with a plain cast, so an equation that needs more than 256 temp ids wraps to id % 256 with no error.
If two temps of different sizes end up on one id, the larger write runs into the neighbouring temp's storage: wrong numbers, no diagnostic.

Example

No model in the corpus reaches this.
a_per_element_hoist_past_the_temp_namespace_without_a_temp_view_is_correct builds a 300-element VECTOR SORT ORDER(vals[d], dir[d]); its comment says it uses more than 256 ids and is correct only because every aliased temp has the same size.
The one read path that keeps a 32-bit id, a static view over a temp, already refuses ids above 255.

The original C-LEARN failure (temp offset 347 exceeds TempId capacity) is fixed: temps are now recycled across the fragments of a model instead of summed (#592), and per-element temps reuse one id across elements.

Fix

Make code generation convert the id with a checked conversion and refuse the variable with NotSimulatable above 255, as the view path does.
Check first whether the 300-element test above really still uses more than 256 ids; if it does, it would start being refused, so either recycle its ids too or accept that.
Alternative: widen TempId to u16, which touches bytecode.rs, vm.rs, wasmgen and the merger, to support a case no real model has shown.

Pointers

  • src/simlin-engine/src/compiler/codegen.rs: the *id as TempId casts
  • src/simlin-engine/src/compiler/symbolic.rs: resolve_static_view guard, TempStrategy
  • src/simlin-engine/src/array_operand_materialization_tests.rs: the TempId namespace tests

Activity

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

    @bpowers
    OwnerAuthor

    Forward-sweep finding — supersedes the "widen TempId to u16" recommendation (fix (a))

    A user-authorized forward sweep of the whole incremental-compile assembly path (an all-narrow-types-widened-to-u32 + peak-count probe build, fully reverted afterward) overturns this issue's stated premise that temps are "genuinely per-fragment scratch, summing is correct, no incorrect-by-omission alternative." They are de-duplicatable by recycling, and the monolithic path already does so.

    Evidence:

    • Only two u8 index types exist (GraphicalFunctionId — already bounded to ~162 by engine: models with many shared graphical functions fail to compile #582's dedup — and TempId); every other index is already u16. None serialize to protobuf.
    • The monolithic Module::compile recycles temps: it flattens each variable's 0-based ast and merges via a HashMap<temp_id,size> keyed max-merge (compiler/mod.rs:2720-2740), so variable A's temp_id=5 and variable B's temp_id=5 collapse to one slot. C-LEARN's monolithic n_temps = 21.
    • The incremental path sums instead: `ContextResourceCounts::from_fragments` (`compiler/symbolic.rs:1256-1272`) adds each fragment's `(max_id+1)` (its own comment says "the total is the sum ... not the global max"). → 243 for the `merged` table.
    • Plus a distinct arithmetic bug: `absorb_non_gf:1523` (`temp_offset = merged_temp_sizes.len() + ctx_base.temps`) re-adds `ctx_base.temps` per temp-bearing fragment → 3233 for the `flows_concat` table. So `flows_concat` (max id 3232, `db.rs:5280 compiled_flows`) and `merged` (243 slots, `db.rs:5414 temp_offsets`) disagree — the two tables the VM consumes simultaneously.
    • Widening is the WRONG fix: with `TempId` widened to u32 and the guard relaxed, C-LEARN compiles `Ok` and `Vm::new` succeeds, but the VM then panics at `vm.rs:2372` (`VectorSortOrder` reads `temp_offsets[347]`, table len 243). Widening converts a clean compile `Err` into a runtime out-of-bounds — it does not produce a working binary.

    So this is a #582-class concat-vs-monolithic divergence, not a genuine-capacity wall. The fix (element-cycle-resolution Phase 6 Task 7) is to match the monolithic recycle: (1b) remove the per-fragment `+ ctx_base.temps` re-add so `flows_concat` ≡ `merged`; (1a) max-merge plain-phase fragment temps into a shared keyed pool (→ ~21), keeping `combine_scc_fragment` (the GH #575 combined-SCC machinery) on the disjoint-range path because its per-element segments interleave per `element_order` (overlapping live ranges). `TempId` stays `u8`. Recommendation (a) "widen to u16" is withdrawn; (b) "recycle to match monolithic" is the fix.

    (Caveat: the probe could not exercise AC7.2/7.3 — it panicked before `run_to_end` — so a runtime-numeric/NaN layer may surface once temps are coherent. That would be a distinct, separately-tracked item.)

  3. changed the title [-]engine: TempId u8 overflow in incremental fragment assembly on large models (C-LEARN)[/-] [+]engine: an equation needing over 256 temp arrays silently aliases them[/+] on Oct 12, 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

    bugengine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions