Repository navigation
engine: hoist nested vector builtins for scalar reducers - #369
Conversation
Scalar equations like sum(vector_elm_map(...)) were lowered without AssignTemp hoisting, so VM codegen tried to treat a builtin application as a view and failed compilation. This patch rewrites nested array-producing builtins in scalar equations into AssignTemp + TempArray before assignment, matching the A2A nested-hoisting model and preserving full-array reducer semantics. The regression tests add VM coverage for scalar and A2A reducer wrappers around vector_elm_map, vector_sort_order, and allocate_available. These lock in the shared invariant that reducers consume materialized array temps rather than per-element projections.
Code ReviewSummaryThis PR fixes scalar equation lowering to hoist nested array-producing builtins (like AnalysisCorrectness of The new function delegates to the existing
Since Coverage of
Guard conditions (line 760): The early return when Tests: Six new VM regression tests cover FindingsNo bugs found. VerdictCorrect. The patch correctly extends the existing A2A nested-hoisting logic to scalar equation contexts. The approach reuses the well-tested |
|
Completed one local automated review iteration with codex after opening this PR, and no additional actionable issues were identified relative to This branch keeps the compiler change focused on one root cause: scalar equations now hoist nested array-producing builtins into |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1bc25d24d7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| &placeholder_view, | ||
| &mut temp_id, | ||
| &mut hoisted, | ||
| true, | ||
| NestedBuiltinArgMode::ArrayValue, |
There was a problem hiding this comment.
Restrict scalar hoisting to reducer array arguments
This helper rewrites any nested array-producing builtin in scalar equations using NestedBuiltinArgMode::ArrayValue, including cases where the parent operation expects scalar operands (for example max(vector_elm_map(source[*], offsets[*]), 15)). That produces TempArray nodes in scalar positions, and VM codegen’s scalar BuiltinFn::Max path (src/simlin-engine/src/compiler/codegen.rs, walk_expr(...).unwrap() around lines 794-799) panics because TempArray returns None outside view consumers. I reproduced this with a scalar max(vector_elm_map(...), 15) equation, which now crashes compilation instead of returning a structured compile error.
Useful? React with 👍 / 👎.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #369 +/- ##
==========================================
+ Coverage 79.04% 79.07% +0.03%
==========================================
Files 142 142
Lines 37396 37475 +79
==========================================
+ Hits 29558 29635 +77
- Misses 7838 7840 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
A scalar hoisting regression rewrote nested array-producing builtins in scalar-argument positions (for example max(vector_elm_map(...), 15)). That produced TempArray nodes in scalar codegen paths and triggered an unwrap panic during VM compilation. This change introduces a scalar-context mode in nested builtin rewriting so hoisting only occurs in array-value consumers like reducers and vector-array arguments. Scalar positions are left untouched, preserving the existing structured compile error behavior. Added a regression test that reproduces the previous panic and asserts a structured VM compile error instead.
Code ReviewReviewed the compiler changes for hoisting nested vector builtins in scalar reducer contexts. Changes analyzed:
Verification:
Findings: None. The patch is correct. Overall correctness: Correct. Existing code paths are unaffected by the new enum variant, the new scalar hoisting logic is well-scoped, and test coverage validates the key scenarios for all three array-producing builtins. |
Summary
AssignTemp + TempArrayso reducers consume full arraysvector_elm_map,vector_sort_order, andallocate_availableRoot Cause
Scalar lowering (
Ast::Scalar) did not run nested array-builtin hoisting. That left expressions likesum(vector_elm_map(...))as direct builtin apps inside reducer args, and VM codegen cannot push views for those app nodes.A2A nested-hoisting logic already handled array-value vs scalar-element argument modes, but test coverage only exercised interpreter behavior for reducer-wrapped builtins in array contexts.
Validation
cargo test -p simlin-engine --test compiler_vector -- --nocapturecargo test -p simlin-engine --lib array_tests::vector_elm_map_tests -- --nocaptureCloses #367
Related to #366