feat: Add typed lambda registry to Optimizer

The Optimizer now accepts a typed lambda registry, allowing it to
perform more aggressive inlining and beta-reduction on globally defined
functions. This is a significant step towards optimizing recursive and
globally defined lambdas more effectively.
This commit is contained in:
Michael Schimmel
2026-02-22 02:31:55 +01:00
parent 8e8d85c06c
commit 2123f1d279
4 changed files with 197 additions and 77 deletions
+7 -7
View File
@@ -3,18 +3,18 @@ use crate::ast::compiler::bound_nodes::{BoundKind, Address, BoundNode};
/// A pass that collects all global function definitions (lambdas) into a registry.
/// This allows the Specializer to retrieve the original AST of a function for monomorphization.
pub struct LambdaCollector<'a> {
registry: &'a mut HashMap<u32, BoundNode>,
pub struct LambdaCollector<'a, T> {
registry: &'a mut HashMap<u32, BoundNode<T>>,
}
impl<'a> LambdaCollector<'a> {
impl<'a, T: Clone> LambdaCollector<'a, T> {
/// Performs a full traversal of the AST and populates the provided registry.
pub fn collect(node: &BoundNode, registry: &'a mut HashMap<u32, BoundNode>) {
pub fn collect(node: &BoundNode<T>, registry: &'a mut HashMap<u32, BoundNode<T>>) {
let mut collector = Self { registry };
collector.visit(node);
}
fn visit(&mut self, node: &BoundNode) {
fn visit(&mut self, node: &BoundNode<T>) {
match &node.kind {
BoundKind::Block { exprs } => {
for expr in exprs {
@@ -25,7 +25,7 @@ impl<'a> LambdaCollector<'a> {
BoundKind::DefGlobal { global_index, value, .. } => {
// Register the full Lambda node as the template.
if let BoundKind::Lambda { .. } = &value.kind {
self.registry.insert(*global_index, (**value).clone());
self.registry.insert(*global_index, (*value).as_ref().clone());
}
self.visit(value);
}
@@ -35,7 +35,7 @@ impl<'a> LambdaCollector<'a> {
if let Address::Global(global_index) = addr
&& let BoundKind::Lambda { .. } = &value.kind
{
self.registry.insert(*global_index, (**value).clone());
self.registry.insert(*global_index, (*value).as_ref().clone());
}
self.visit(value);
}