Repository navigation
Description
Summary
Codegen requires an array-valued operand of a vector builtin to be a view over storage (StaticSubscript | TempArray | Var | Subscript; compiler/codegen.rs::walk_expr_as_view, ~line 484-578). Any computed array must be materialised into a temp by an AssignTemp earlier in the same fragment. PREVIOUS(<arrayed ref>) is a computed array, and there is no materialiser for it — so it fails to compile.
This is not an LTM problem. These shapes fail at HEAD in ordinary hand-written apply-to-all models with LTM disabled:
| equation | result |
|---|---|
out[d] = VECTOR SORT ORDER(vals[d], 1) |
compiles |
out[d] = VECTOR ELM MAP(vals[e1], offsets[d]) |
compiles |
out[d] = RANK(vals[*], 1) |
compiles |
out[d] = VECTOR SORT ORDER(PREVIOUS(vals[d]), 1) |
fails |
out[d] = VECTOR ELM MAP(PREVIOUS(vals[e1]), offsets[d]) |
fails |
out[d] = VECTOR ELM MAP(vals[e1], PREVIOUS(offsets[d])) |
fails |
out[d] = VECTOR SORT ORDER(INIT(vals[d]), 1) |
fails |
out[d] = VECTOR SORT ORDER(vals[d] * 2, 1) |
fails |
out[d] = VECTOR SELECT(PREVIOUS(offsets[d]), vals[d], 0, 1, 0) |
fails |
out[d] = RANK(vals[*] * 2, 1) |
fails (separate arm, see below) |
Before #994 these reported no diagnostic at all; that PR makes a codegen rejection name its variable, so they now surface properly.
Where the fix belongs
Not in codegen. Temp ids and temp_sizes are derived from the lowered Vec<Expr> (extract_temp_sizes, consumed by db::assemble::compile_phase_to_per_var_bytecodes), so allocation must happen at lowering.
The existing materialiser is ast::expr3::Pass1Context (ast/expr3.rs:500-1125), which already calls maybe_decompose_array_arg_inner on the array-argument positions of VectorSelect/VectorElmMap/VectorSortOrder/AllocateAvailable/AllocateByPriority (expr3.rs:948-1006). It fails to fire for two reasons:
needs_decompositionreturns a blanketfalseforExpr3::App(expr3.rs:1114-1120), soPREVIOUS/INITof an array is never materialised. The existing arrayed-GFLOOKUP(g[D!], idx)special case (expr3.rs:604-633) is the precedent to generalise.- It defers when
has_a2a(expr3.rs:1044-1047), and the per-element path it defers to hoists only whole array-producing builtins, never their complex arguments.
Separate one-arm bug, independently shippable: transform_builtin_inner's Rank arm (expr3.rs:939-946) recurses with transform_inner instead of maybe_decompose_array_arg_inner, unlike every sibling. That is the RANK(vals[*] * 2, 1) row above.
A semantic trap — do NOT simply make everything compile
On C-LEARN, ~21 of these are VECTOR SORT ORDER(PREVIOUS(effective_target_year[cop·X, target·Y]), ascending). The source equation ranks a whole row; the LTM ceteris-paribus wrap pinned the argument down to one element. vm_vector_sort_order.rs on a 1-element view yields rank 0 always — so making it compile converts a loud drop into a silent constant-0 partial and a plausible-but-meaningless score.
The element pin belongs on the result, not on a rank-like builtin's argument. ltm_agg already declines to hoist RANK for exactly this reason (#771, reducer_collapses_to_scalar), so the vocabulary exists — the wrap needs to consult it. Same applies to RANK and both ALLOCATE*.
Options, simplest first
- A. Pass 1 materialises a non-view array operand. Relax
needs_decomposition'sApparm and theview.dims.is_empty()early-return (expr3.rs:1080), and fix theRankarm. One file, no new opcode, no VM change, no wasm change. Must not ship without C — it also makes the rank-like partials compile, to a constant 0. - B. Front-end array capture helper for
PREVIOUS(<arrayed ref>). Synthesiseh[Dim] = PREVIOUS(vals[Dim])and referencehas a whole array, reusingmake_temp_arg's existing arrayed-helper branch (builtins_visitor.rs:710-773). Per element the body isPREVIOUS(<static slot>), which compiles today — so an array freeze needs no runtime support at all. - C. LTM: stop pinning a rank-like builtin's argument (the trap above).
- D.
SymStaticViewBase::PrevVar, a view overprev_values. Cleanest general answer and the only one that makes arrayPREVIOUSfirst-class — but the only one touching the view stack in both backends and the VM-vs-wasm parity gate. B reaches the same models with zero runtime change.
Suggested order: B, C, A. D only if array PREVIOUS is wanted as a language feature.
Scope
109 generating roots on C-LEARN, not the 244 originally measured: the completeness guard in #994 declines 135 of them for an independent reason (an unresolvable dimension-name index surviving into the partial), and 125 vector-builtin scores survive it. Nothing working is lost — those 135 do not compile today either way — and the guard is self-resolving, since it keys on exactly the condition a correct pin would remove.
Caveats to verify before building
- Option A's dependence on the collapsed-view
dims.is_empty()shape is a prediction from reading, not confirmed against a real lowered fragment. Notemaybe_decompose_array_arg_inner's early-return atexpr3.rs:1080keys on the same predicate, so it is likely in scope too. TempIdisu8(256 temps/module) and arrayed LTM targets already reach six temps per fragment. Measure temp pressure before, not after — see engine: an equation needing over 256 temp arrays silently aliases them #583.- No checked-in model in
test/puts an array-producing builtin in a scalar equation or gives one a computed argument, so the change is behaviour-neutral on the corpus. Goldens that would move:db/fragment_char_golden/array_ops.txtand the LTM char goldens.
Related: #578 (a narrower instance of the same contract), #583 (TempId ceiling), #771 (RANK not hoisted).
Activity
engine: address codex review -- dedup pathway warnings, measure runti…
engine: pin executed mapped-reference semantics, close out the GH #996 …
engine: materialize computed array operands of vector builtins
engine: lower array-valued PREVIOUS and INIT to views over the snapsh…
engine: close the LTM mapped-dimension and array-operand gaps (#995, #…