Skip to content

Add InitLocals instruction for bulk local initialization#3122

Merged
antoniosarosi merged 2 commits intocanaryfrom
antonio/vm-locals-init
Feb 17, 2026
Merged

Add InitLocals instruction for bulk local initialization#3122
antoniosarosi merged 2 commits intocanaryfrom
antonio/vm-locals-init

Conversation

@antoniosarosi
Copy link
Copy Markdown
Contributor

@antoniosarosi antoniosarosi commented Feb 17, 2026

Summary

  • Adds a new InitLocals(n) bytecode instruction that pushes n null values onto the stack in one shot, replacing N consecutive LoadConst(null) instructions emitted at the start of every function for local variable pre-allocation.
  • Uses Vec::resize for the VM handler instead of per-element push, avoiding N dispatch cycles through the main match loop.
  • Reduces bytecode size across all 33 codegen snapshots (e.g., 7 instructions → 1 for the common baml.llm.call_llm_function prologue).

Test plan

  • All 139 baml_compiler_emit tests pass
  • All 375 baml_tests pass (33 codegen snapshots auto-updated)
  • All 24 bex_vm tests pass
  • cargo fmt --check clean
  • cargo clippy clean on affected crates

Summary by CodeRabbit

  • Refactor
    • Consolidated local variable pre-allocation into a single bytecode initialization step instead of multiple per-slot null pushes.
  • New Features
    • VM and bytecode now recognize an explicit local-initialization instruction; debugger output and coloring updated accordingly.
  • Tests
    • Updated emitted-code expectations and test harness to reflect the consolidated local initialization.

Replace N consecutive LoadConst(null) instructions at the start of
each function with a single InitLocals(N) that bulk-initializes local
variable stack slots. Reduces bytecode size, dispatch overhead, and
improves readability.
@vercel
Copy link
Copy Markdown

vercel bot commented Feb 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Feb 17, 2026 8:20pm
promptfiddle Ready Ready Preview, Comment Feb 17, 2026 8:20pm

Request Review

@antoniosarosi antoniosarosi changed the title feat: add InitLocals instruction for bulk local initialization Add InitLocals instruction for bulk local initialization Feb 17, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 17, 2026

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

Introduce a new InitLocals(usize) bytecode instruction to pre-allocate N local slots; replace emitted per-slot LoadConst(Value::Null) sequences with a single InitLocals(n). Compiler emit, VM types, VM executor, test harness, and tests updated to emit, represent, execute, and validate the instruction. (44 words)

Changes

Cohort / File(s) Summary
Compiler Emission
baml_language/crates/baml_compiler_emit/src/emit.rs
Replaced per-slot LoadConst(Value::Null) emissions with a single InitLocals(slots_to_allocate) emission for local pre-allocation.
Compiler Tests
baml_language/crates/baml_compiler_emit/tests/classes.rs, baml_language/crates/baml_compiler_emit/tests/for_loops.rs, baml_language/crates/baml_compiler_emit/tests/if_else.rs, baml_language/crates/baml_compiler_emit/tests/maps.rs, baml_language/crates/baml_compiler_emit/tests/match_expr.rs, baml_language/crates/baml_compiler_emit/tests/operators.rs, baml_language/crates/baml_compiler_emit/tests/watch.rs, baml_language/crates/baml_compiler_emit/tests/while_loops.rs
Updated expected MIR/bytecode in tests to use InitLocals(n) instead of sequences of LoadConst(Value::Null); only the initialization prelude changed.
Test Framework
baml_language/crates/baml_tests/src/vm.rs, baml_language/crates/baml_tests/src/codegen.rs
Added InitLocals(usize) to the test Instruction enum and mapping from runtime instructions to test instructions.
VM Execution
baml_language/crates/bex_vm/src/vm.rs
Implemented execution for InitLocals(count) by extending the evaluation stack with count null-initialized slots.
VM Debug Support
baml_language/crates/bex_vm/src/debug.rs
Display handling added for InitLocals(_) and assigned debugger color Green.
VM Types / Bytecode
baml_language/crates/bex_vm_types/src/bytecode.rs
Added public InitLocals(usize) instruction variant and Display formatting as INIT_LOCALS {n}.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Compiler as Compiler (emit)
participant Bytecode as Bytecode (Instruction::InitLocals)
participant VM as VM Executor
participant Tests as Test Harness
Note over Compiler,Bytecode: Emit consolidated local pre-allocation
Compiler->>Bytecode: emit InitLocals(n)
Bytecode->>Tests: included in expected instruction stream
Tests->>VM: load bytecode (contains InitLocals)
VM->>VM: execute InitLocals -> expand eval stack by n (Null)
VM->>VM: continue executing subsequent instructions

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add InitLocals instruction for bulk local initialization' accurately describes the main change: introducing a new instruction that replaces multiple LoadConst(null) operations with a single bulk initialization operation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch antonio/vm-locals-init

Comment @coderabbitai help to get the list of available commands and usage tips.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Feb 17, 2026

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 15 untouched benchmarks
⏩ 84 skipped benchmarks1


Comparing antonio/vm-locals-init (a65666a) with canary (9215d51)

Open in CodSpeed

Footnotes

  1. 84 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 17, 2026

Binary size checks passed

7 passed

Artifact Platform Gzip Baseline Delta Status
bridge_cffi Linux 4.0 MB 4.0 MB +393 B (+0.0%) OK
bridge_cffi-stripped Linux 2.1 MB 2.1 MB +98 B (+0.0%) OK
bridge_cffi macOS 3.3 MB 3.3 MB +340 B (+0.0%) OK
bridge_cffi-stripped macOS 1.7 MB 1.7 MB +207 B (+0.0%) OK
bridge_cffi Windows 3.3 MB 3.3 MB +539 B (+0.0%) OK
bridge_cffi-stripped Windows 1.8 MB 1.8 MB +817 B (+0.0%) OK
bridge_wasm WASM 1.3 MB 1.3 MB +223 B (+0.0%) OK

Generated by cargo size-gate · workflow run

@antoniosarosi antoniosarosi added this pull request to the merge queue Feb 17, 2026
Merged via the queue into canary with commit f469b6d Feb 17, 2026
43 of 44 checks passed
@antoniosarosi antoniosarosi deleted the antonio/vm-locals-init branch February 17, 2026 20:14
antoniosarosi added a commit that referenced this pull request Feb 17, 2026
Integrates 4 canary PRs:
- #3126: MIR analysis soundness (StatementRef, unified walkers)
- #3124: Type variants for type expressions (parse takes Type)
- #3122: InitLocals(n) instruction
- #3107: Full tracing system (bex_events, Collector)

Conflict resolutions:
- llm.baml: keep orchestration loop with panic, update parse() to use get_return_type
- baml_builtins: keep both Enum and Type TypePattern variants, add get_return_type alongside orchestration builtins
- baml_compiler_emit: keep HIDDEN_LLM_BUILTINS removal
- baml_compiler_tir: add both Enum and Type arms in substitute functions
- bex_vm_types: merge both ClientBuild* and CollectorRef re-exports
- llm_render tests: use new 4-arg call_function signature, fix PromptAst FQN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant