compiler: add Enum.reduce_while/3 for integer range compilation#114
Merged
compiler: add Enum.reduce_while/3 for integer range compilation#114
Conversation
Add compilation of Enum.reduce_while/3 over integer ranges (plain and
stepped) to efficient tail-recursive WASM loops with early termination.
The reducer function body containing {:halt, val} and {:cont, val}
is transformed at the AST level before IR compilation:
- {:halt, val} → return val immediately (no recursion)
- {:cont, val} → recursive call to helper with val as new accumulator
Free variables from the outer function scope (e.g., function parameters
referenced inside the reducer) are automatically detected and threaded
through as extra helper function parameters.
Supports:
- Plain ranges: Enum.reduce_while(1..n, init, fn i, acc -> ... end)
- Stepped ranges: Enum.reduce_while(0..n//2, init, fn i, acc -> ... end)
- Negative steps: Enum.reduce_while(n..1//-1, init, fn i, acc -> ... end)
- Dynamic steps: Enum.reduce_while(a..b//s, init, fn i, acc -> ... end)
- Block bodies with let bindings
- Nested if/case/cond with halt/cont in any branch
- Closures over outer function parameters
The generated helper function benefits from existing optimization passes:
TCO converts it to a loop, constant propagation simplifies invariants,
and strength reduction optimizes the arithmetic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add compilation of
Enum.reduce_while/3over integer ranges to efficient tail-recursive WASM loops with early termination.Motivation
Enum.reduce_while/3is a common Elixir pattern for reductions that may terminate early based on a condition. Previously, this function wasn't supported in the compilable subset, forcing users to write manual recursive functions for early-exit accumulation patterns.Implementation
The reducer function body uses
{:halt, val}and{:cont, val}to signal early termination or continuation. These are transformed at the AST level before IR compilation:{:halt, val}→ return val immediately (loop exit){:cont, val}→ recursive call to helper with val as new accumulator (loop continue)Free variables from the outer function scope (e.g., function parameters like
thresholdreferenced inside the reducer) are automatically detected and threaded through as extra helper function parameters.Supported patterns
Enum.reduce_while(1..n, init, fn i, acc -> ... end)Enum.reduce_while(0..n//2, init, fn i, acc -> ... end)Enum.reduce_while(n..1//-1, init, fn i, acc -> ... end)Enum.reduce_while(a..b//s, init, fn i, acc -> ... end)Example
Compiles to a tail-recursive helper that TCO converts to a WASM loop with
br_tableexit.Tests
11 new tests covering: