Skip to content

feat(hoist): pre-compute loop hoisting metadata at compilation time - #483

Merged
anakrish merged 1 commit into
microsoft:mainfrom
anakrish:ast-enhanced
Oct 8, 2025
Merged

feat(hoist): pre-compute loop hoisting metadata at compilation time#483
anakrish merged 1 commit into
microsoft:mainfrom
anakrish:ast-enhanced

Conversation

@anakrish

@anakrish anakrish commented Oct 6, 2025

Copy link
Copy Markdown
Collaborator

Introduce a compiler pass that analyzes and pre-computes loop hoisting information during policy compilation. This hoisted metadata is stored in lookup tables and made available to downstream consumers:

  • interpreter: use HoistedLoop entries during evaluation (replaces runtime scanning)
  • type inference: can leverage pre-computed loop structure for type propagation
  • RVM compiler: will consume hoisting metadata for optimized bytecode generation

Changes:

  • populate loop hoisting tables during engine preparation and query snippet execution
  • refactor eval_stmts_in_loop and eval_output_expr_in_loop to consume HoistedLoop directly
  • add helper methods for accessing loop expressions, collections, and indices from HoistedLoop
  • extend Lookup with get_checked and into_slots for safe query context access and merging

Comment thread src/compiler/hoist.rs Fixed
Comment thread src/compiler/hoist.rs Fixed
Comment thread src/compiler/hoist.rs Fixed
Comment thread src/compiler/hoist.rs Fixed
Comment thread src/compiler/context.rs Fixed
Comment thread src/compiler/hoist.rs Fixed
Comment thread src/interpreter.rs Fixed
Comment thread src/interpreter.rs Fixed
Comment thread src/interpreter.rs Fixed
Comment thread src/interpreter.rs Fixed
Comment thread src/interpreter.rs Fixed
Comment thread src/interpreter.rs Fixed
Comment thread src/interpreter.rs Fixed
Comment thread src/interpreter.rs Fixed
@anakrish
anakrish force-pushed the ast-enhanced branch 3 times, most recently from 7393fc8 to be57589 Compare October 6, 2025 18:14
Comment thread src/compiler/hoist.rs Fixed
Introduce a compiler pass that analyzes and pre-computes loop hoisting information
during policy compilation. This hoisted metadata is stored in lookup tables and made
available to downstream consumers:

- interpreter: use HoistedLoop entries during evaluation (replaces runtime scanning)
- type inference: can leverage pre-computed loop structure for type propagation
- RVM compiler: will consume hoisting metadata for optimized bytecode generation

Changes:
- populate loop hoisting tables during engine preparation and query snippet execution
- refactor eval_stmts_in_loop and eval_output_expr_in_loop to consume HoistedLoop directly
- add helper methods for accessing loop expressions, collections, and indices from HoistedLoop
- extend Lookup with get_checked and into_slots for safe query context access and merging

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
@anakrish
anakrish marked this pull request as ready for review October 7, 2025 20:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a compilation-time loop hoisting optimization that pre-computes loop metadata during policy preparation, storing it in lookup tables for efficient runtime evaluation. This replaces the previous runtime loop scanning approach with cached results that can be reused across multiple evaluations.

  • Adds a new compiler module with loop hoisting and scope context functionality
  • Refactors interpreter loop handling to consume pre-computed HoistedLoop structures
  • Extends the Lookup utility with bounds checking and module management capabilities

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/compiler/hoist.rs Implements the main LoopHoister that pre-computes loop metadata for statements and expressions
src/compiler/context.rs Defines ScopeContext for tracking variable bindings during compilation
src/interpreter.rs Updates loop evaluation to use pre-computed HoistedLoop data instead of runtime analysis
src/engine.rs Integrates loop hoisting into policy preparation and query snippet execution
src/lookup.rs Adds safe bounds checking and module management methods
src/compiled_policy.rs Stores the loop hoisting table in compiled policy data

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/interpreter.rs

#[inline]
fn loop_index_expr(loop_info: &HoistedLoop) -> Option<&ExprRef> {
loop_info.key.as_ref().or(None)

Copilot AI Oct 7, 2025

Copy link

Choose a reason for hiding this comment

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

The .or(None) is redundant here. Option::as_ref() already returns an Option, so this can be simplified to just loop_info.key.as_ref().

Suggested change
loop_info.key.as_ref().or(None)
loop_info.key.as_ref()

Copilot uses AI. Check for mistakes.

@dpokluda dpokluda left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

:shipit:

@anakrish
anakrish merged commit 5d8387f into microsoft:main Oct 8, 2025
40 checks passed
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.

4 participants