Skip to content

wasm-backend Phase 5: broadcast-iteration opcode family is unreached dead code; ViewRangeDynamic unsupported by design (neither in corpus) #612

Closed

Description

@bpowers

Summary

During Phase 5 of the wasm simulation backend (src/simlin-engine/src/wasmgen/, array opcode lowering), two array-lowering coverage/limitation items surfaced that need explicit tracking before the Phase 8 full-corpus-parity goal. Both are internal backend coverage/limitation concerns, not user-facing bugs. They are distinct from #607, which is purely about a false documentation premise ("CompiledSimulation is fully un-fused") in the plan text; these two are about opcode reachability and an architectural limitation in the array lowering. Filed together because both are Phase 5 array-lowering findings with the same Phase 8 relevance.

Investigated and confirmed against the engine; file:line evidence below.


Finding 1: the broadcast-iteration opcode family is dead/speculative code (VM-wide, not just wasm)

The broadcast-iteration opcodes -- BeginBroadcastIter / LoadBroadcastElement / StoreBroadcastElement / NextBroadcastOrJump / EndBroadcastIter -- plus the broadcasting variant of LoadIterViewAt (source dims != iteration dims) are fully implemented and unit-tested in the wasm backend (src/simlin-engine/src/wasmgen/lower.rs:1292-1331, the build_broadcast_ctx helper at lower.rs:1483-1520, views.rs::iter_broadcast_offset at views.rs:356, and the broadcast_iter_* hand-built-bytecode unit tests at lower.rs:5765+). They are also a first-class part of the VM: defined in bytecode.rs:1168-1188, executed by vm.rs:2314-2373, and given a full symbolize/resolve round-trip in compiler/symbolic.rs.

But production codegen never emits any of them. Verified: rg '\.push\(Opcode::(BeginBroadcastIter|LoadBroadcastElement|StoreBroadcastElement|NextBroadcastOrJump|EndBroadcastIter)' src/simlin-engine/src/ returns nothing outside the symbolize/resolve plumbing and tests. Concretely:

  • Arrayed / A2A elementwise assignment is scalar-unrolled at compile time, not broadcast-iterated. compiler/mod.rs:1871-1903 (standard per-element expansion) emits one Expr::AssignCurr(off + i, ...) per element; the lookup-only A2A path at mod.rs:1931+ does the same. There is no BeginBroadcastIter emission anywhere in compiler/codegen.rs.
  • The only iteration codegen emits is the non-broadcast BeginIter (codegen.rs:1328, the NextIterOrJump/EndIter pair), used solely to fill an AssignTemp for an array-producing builtin's elementwise argument. That non-broadcast loop is reachable and is covered end-to-end by the wasm backend (module.rs:2296 compile_simulation_hoisted_reducer_loop_matches_vm).
  • A mismatched-dim reducer argument (the other way a broadcast loop might be imagined to arise) fails the engine's own dimension check (NotSimulatable) before codegen.

Provenance: the family was introduced as scaffolding in commit 5d282349 "engine: add bytecode infrastructure for array support (#135)" and the codegen never grew an emitter for it. This is consistent with tech-debt item #12 ("scaffolding types ... reserved for future strided array views").

The wasm backend documents the gap honestly (src/simlin-engine/src/wasmgen/module.rs:2285-2294): the broadcast paths are "not reachable through the current production codegen ... so those are pinned directly against the VM by hand-built-bytecode unit tests."

Why it matters / why track it. This is dead code in the VM and the wasm backend, carried by both. Two consequences:

  1. The wasm broadcast lowering has unit-test coverage but no end-to-end/corpus coverage, and cannot get any until/unless codegen emits these opcodes -- so it does not contribute to the Phase 8 full-corpus-parity signal and could silently rot.
  2. It is maintenance weight (a whole opcode family + broadcast view arithmetic mirrored in two execution engines) for a code path nothing produces.

Suggested resolution (pick one, needs a decision):

  • (a) Treat it as genuinely dead and remove the broadcast-iteration family from the VM, symbolic.rs, and wasmgen (simplest; matches "simple/general/maintainable over preserving an interface"). The arrayed-broadcast semantics it was meant to serve are already delivered by compile-time scalar unrolling.
  • (b) If broadcast iteration is intended to replace scalar unrolling later (e.g. to bound code size on large arrays), keep it but clearly mark it speculative/unreached in bytecode.rs and wasmgen, and add a tracked task to either wire up a codegen emitter (which would then need corpus coverage) or delete it.

Either way the wasm side should not be expected to provide Phase 8 corpus parity for a construct codegen never emits; that expectation should be recorded.


Finding 2: ViewRangeDynamic (runtime range bounds) is Unsupported by design; no corpus model exercises it

A runtime range subscript -- arr[i:j] where i/j are not compile-time constants -- lowers to Opcode::ViewRangeDynamic (emitted at compiler/codegen.rs:436 from a non-constant SubscriptIndex::Range). The wasm backend returns WasmGenError::Unsupported for it (src/simlin-engine/src/wasmgen/lower.rs:1151-1156): the Phase 5 emitter fully unrolls every iteration body per compile-time index, so a range whose size is unknown at compile time cannot be unrolled. Such a model stays Skipped and is caught honestly by the parity floor gate.

A literal range is not affected: ast/expr3.rs:334-341 constant-folds IndexExpr2::Range(const, const) into IndexExpr3::StaticRange, which lowers to the static Opcode::ViewRange -- and the wasm backend supports that (lower.rs:1089, unit-tested at lower.rs:5041).

Corpus assessment (done): across all 495 model files under test/ (*.xmile/*.stmx/*.mdl/*.xml), there are zero genuine range subscripts of either form:

  • No dynamic arr[i:j].
  • No static numeric arr[1:3] either (rg '\[[0-9]+:[0-9]+\]' over the corpus is empty).
  • The only colon-in-bracket patterns in the corpus are [*:SubDim] StarRange (e.g. MAX(x[*:SubX]), SUM(z[*:SubA]), SUM(Transition_Matrix[Cats, *:dummy]*...) in test/sdeverywhere/models/{vector,except,except2,sum} and test/test-models/tests/subscript_switching). StarRange is a distinct construct (IndexExpr3::StarRange -> Opcode::ViewStarRange) that the wasm backend does support (lower.rs:1096).

Severity / Phase 8 impact: low; does NOT block full-corpus parity. Runtime ranges are nonexistent in the corpus and rare in practice. This is a documented capability gap, not a regression: affected models stay Skipped and are surfaced by the floor gate rather than miscompiled.

Suggested resolution: document ViewRangeDynamic as a known wasm-backend limitation (the unrolling architecture cannot express a runtime-sized view), and revisit only if a real model is found to need it -- at which point the unrolling emitter would need a runtime-loop variant for the dynamic-range body. No action required for Phase 8 parity beyond recording the exclusion.


Components affected

  • src/simlin-engine/src/wasmgen/ (lower.rs, views.rs, module.rs)
  • src/simlin-engine/src/bytecode.rs, src/simlin-engine/src/vm.rs, src/simlin-engine/src/compiler/symbolic.rs (for Finding 1's dead-code removal/marking)

How discovered

Surfaced by the implementing agent during Phase 5 (array support) of the wasm-backend implementation plan (docs/implementation-plans/2026-05-20-wasm-backend/); the agent could not file these itself. Investigated and confirmed before filing. Related to #607 (false un-fused premise) only by shared epic; the concerns are independent.

Activity

  1. added
    engine
    hygiene
    rust
    on May 21, 2026
  2. bpowers commented on Aug 26, 2026

    @bpowers
    OwnerAuthor

    Retired on the compiler-unification branch (commit subject engine: retire the unemitted opcode families, part (b) of Phase 2 of docs/design-plans/2026-08-25-compiler-unification.md). Finding 1 is resolved by deletion: the owner chose option (a).

    Deleted SymbolicOpcode variants and their Opcode twins -- the sixteen that carried #[allow(dead_code)] because Compiler never constructed them:

    • incremental view-stack construction: PushTempView, ViewSubscriptConst, ViewRange, ViewStarRange, ViewWildcard, ViewTranspose, DupView, LoadTempDynamic, LoadIterElement, LoadIterTempElement, LoadIterViewTop
    • broadcast iteration: BeginBroadcastIter, LoadBroadcastElement, StoreBroadcastElement, NextBroadcastOrJump, EndBroadcastIter

    Deleted with them, because they existed only for those opcodes:

    • compiler/symbolic.rs: the resolve_opcode, renumber_opcode, gf_run and jump_offset arms; the SymbolicCompiledModule.arrays / .subdim_relations fields
    • bytecode.rs: the stack_effect / name / jump_offset arms; ByteCodeContext.arrays and .subdim_relations (always vec![] in production) with the ArrayDefinition and bytecode::SubdimensionRelation types; RuntimeView::for_temp, apply_range, apply_sparse, apply_sparse_with_dim_id, transpose (dimensions::SubdimensionRelation, the compiler-side relation, is untouched)
    • vm.rs: the sixteen eval_bytecode arms, BroadcastState / BroadcastSourceInfo, the broadcast_stack on Vm and EvalState, and IterState.flat_offsets with the BeginIter precompute that filled it (its only reader was LoadIterElement)
    • compiler/codegen.rs: Compiler.subdim_relations, get_or_add_subdim_relation, find_dim_id_by_name, and the ModuleCtx.dimensions_ctx field only they read
    • wasmgen/lower.rs: the lowering arms, BroadcastCtx, build_broadcast_ctx, emit_load_broadcast_element, IterKind (iter_span is BeginIter-only), resolve_dim_list_dims, emit_load_temp_dynamic, current_iter_index; wasmgen/views.rs: ViewDesc::apply_single_subscript, apply_range, apply_sparse, transpose
    • the hand-built-bytecode tests of each (lower_tests.rs view-transform / LoadTempDynamic / LoadIterElement / LoadIterTempElement / broadcast_iter_*, views.rs transform tests, the bytecode.rs / symbolic.rs fixtures, compiler/dimensions.rs::test_lazy_subdimension_relation), with the geometry they checked re-pinned on static views built the way codegen builds them

    What enforces it from here: Compiler is the only producer of a SymbolicOpcode and resolve_opcode the only producer of an Opcode, so a variant nothing emits is a dead_code warning on the lib target, and both the pre-commit hook (cargo clippy --all-targets --all-features -- -D warnings) and CI (cargo clippy --workspace -- -D warnings) deny it. The enum carries no allow(dead_code).

    ViewRangeDynamic is NOT part of this: codegen emits it for a runtime arr[i:j] range (compiler/codegen.rs), the VM executes it, and the wasm backend still returns WasmGenError::Unsupported for it by design, exactly as Finding 2 describes. That limitation stands and is documented in wasmgen/lower.rs.

    Compiled artifacts are byte-identical on C-LEARN (nothing emitted changed); the engine suite, the wasm parity corpus, and the determinism suites are green.

    Measured: on the run channel the identical C-LEARN artifact retires +0.9% instructions (dispatch-loop codegen in the smaller eval_bytecode; the opcode stream is identical), accepted as a simplicity trade and recorded in the design plan's ledger.

  3. bpowers commented on Sep 3, 2026

    @bpowers
    OwnerAuthor

    Closing with the two findings resolved as follows.

    Finding 1 (the broadcast-iteration opcode family is unreached dead code): removed on main by ca39c1c6 (engine: retire the unemitted opcode families), which deletes the broadcast-iteration and incremental view-stack families and everything that existed only for them. The standing pin is structural: Compiler is the only producer of a SymbolicOpcode, the enum carries no allow(dead_code), so a variant codegen does not emit is a dead_code warning that clippy -D warnings rejects in the hook and CI.

    Finding 2 (ViewRangeDynamic unsupported in wasmgen): this is the documented backend limitation (src/simlin-engine/src/wasmgen/mod.rs and module.rs module docs), not a defect of the compiler-unification work; wasmgen/lower.rs refuses the opcode with an explicit error naming the dimension rather than mis-compiling it, and the VM remains wasmgen's oracle on every exposed slot. A wasm-side implementation of runtime view ranges would be its own issue against wasmgen.

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

    enginehygienerust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions