Skip to content

engine: VM applies one override to every instance of a shared module #616

Open

Description

@bpowers

When a model uses the same submodel twice, setting a constant inside each instance to a different value does not work on the VM: the last set_value wins for both instances.
The wasm backend gets it right.

Example

A submodel with constant k and out = in + k, instantiated as sub0 and sub1, with in wired to 7.
Set sub0.k = 100 and sub1.k = 200:

sub0.out sub1.out
expected (wasm) 107 207
VM 207 207

Setting both instances to the same value works.

Why it matters

It needs a model that instantiates one module more than once and a host that overrides a constant inside it differently per instance.
No in-repo caller does this, and it has not been seen in a real model; it came from a constructed wasm-backend test.
But the result is a silently wrong number, which is worse than an error.

Fix

The VM applies an override by rewriting the constant's literal in the module's bytecode, and two instances of one module share that bytecode.

Recommended: make set_value return an error when the constant lives in a module that is instantiated more than once, instead of silently overwriting the other instance.
This is a check at override time, off the hot path.

Alternative: keep overrides per instance, as the wasm backend does (an override table addressed by absolute slot).
On the VM that means an extra branch in the constant-assignment opcode, and the perf notes in docs/design/engine-performance.md show small changes to that loop can cost more than expected; measure it.

Pointers

  • src/simlin-engine/src/vm.rs: collect_constant_info, apply_override, write_literal
  • src/simlin-engine/src/wasmgen/module_tests.rs: compile_simulation_two_instances_distinct_overrides

Activity

  1. added
    bug
    engine
    rust
    on May 22, 2026
  2. changed the title [-]engine: VM set_value cannot apply distinct per-instance overrides to two instances of the same shared CompiledModule[/-] [+]engine: VM applies one override to every instance of a shared module[/+] 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

    bugenginerust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions