Refactor optimizer with aggressive collapsing
The optimizer has been refactored to separate its phases and introduce more aggressive collapsing capabilities. Phase 2 (Cracking) now focuses on stateless transformations, making it easier to reason about and potentially parallelize. Phase 2.5 (Aggressive Collapsing) has been introduced, enabling optimizations like: - Beta-reduction for lambda literals. - Cracking and inlining for constant closures. - Folding intrinsics for constant arithmetic. - Short-circuiting conditional expressions. These changes aim to improve performance by reducing redundant computations and code bloat. The maximum number of optimization passes has also been increased to 5 to allow for more complex transformations.
This commit is contained in:
+98
-72
@@ -1,22 +1,22 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::compiler::binder::Binder;
|
||||
use crate::ast::compiler::{TypeChecker, TypedNode};
|
||||
use crate::ast::nodes::{Node, Symbol, UntypedKind};
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::types::{Object, StaticType, Value};
|
||||
use crate::ast::vm::{TracingObserver, VM};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use crate::ast::types::{Value, StaticType, Object};
|
||||
use crate::ast::nodes::{Node, UntypedKind, Symbol};
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::compiler::binder::Binder;
|
||||
use crate::ast::compiler::{TypedNode, TypeChecker};
|
||||
use crate::ast::vm::{VM, TracingObserver};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::compiler::tco::TCO;
|
||||
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundNode};
|
||||
use crate::ast::compiler::dumper::Dumper;
|
||||
use crate::ast::compiler::macros::{MacroExpander, MacroRegistry, MacroEvaluator};
|
||||
use crate::ast::compiler::specializer::{Specializer, MonoCache, FunctionRegistry};
|
||||
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
|
||||
use crate::ast::compiler::optimizer::Optimizer;
|
||||
use crate::ast::compiler::specializer::{FunctionRegistry, MonoCache, Specializer};
|
||||
use crate::ast::compiler::tco::TCO;
|
||||
use crate::ast::rtl;
|
||||
use crate::ast::rtl::intrinsics;
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundNode};
|
||||
|
||||
pub struct Environment {
|
||||
pub global_names: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
@@ -50,19 +50,24 @@ struct RuntimeMacroEvaluator {
|
||||
}
|
||||
|
||||
impl MacroEvaluator for RuntimeMacroEvaluator {
|
||||
fn evaluate(&self, node: &Node<UntypedKind>, bindings: &HashMap<Rc<str>, Node<UntypedKind>>) -> Result<Value, String> {
|
||||
fn evaluate(
|
||||
&self,
|
||||
node: &Node<UntypedKind>,
|
||||
bindings: &HashMap<Rc<str>, Node<UntypedKind>>,
|
||||
) -> Result<Value, String> {
|
||||
// 1. Check if it's a simple parameter substitution
|
||||
if let UntypedKind::Identifier(sym) = &node.kind
|
||||
&& let Some(arg_node) = bindings.get(&sym.name) {
|
||||
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
|
||||
if let UntypedKind::Identifier(sym) = &node.kind
|
||||
&& let Some(arg_node) = bindings.get(&sym.name)
|
||||
{
|
||||
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
|
||||
}
|
||||
|
||||
// 2. Full evaluation for complex compile-time expressions
|
||||
let bound_ast = Binder::bind_root(self.global_names.clone(), node)?;
|
||||
|
||||
|
||||
let checker = TypeChecker::new(self.global_types.clone());
|
||||
let typed_ast = checker.check(bound_ast, &[])?;
|
||||
|
||||
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
vm.run(&typed_ast)
|
||||
}
|
||||
@@ -102,7 +107,12 @@ impl Environment {
|
||||
MacroExpander::new(MacroRegistry::new(), evaluator)
|
||||
}
|
||||
|
||||
pub fn register_native(&self, name: &str, ty: StaticType, func: impl Fn(Vec<Value>) -> Value + 'static) {
|
||||
pub fn register_native(
|
||||
&self,
|
||||
name: &str,
|
||||
ty: StaticType,
|
||||
func: impl Fn(Vec<Value>) -> Value + 'static,
|
||||
) {
|
||||
let mut names = self.global_names.borrow_mut();
|
||||
let mut types = self.global_types.borrow_mut();
|
||||
let mut values = self.global_values.borrow_mut();
|
||||
@@ -143,7 +153,10 @@ impl Environment {
|
||||
|
||||
// 2. Check for trailing tokens
|
||||
if !parser.at_eof() {
|
||||
return Err("Unexpected trailing expressions in script. Use (do ...) for sequences.".to_string());
|
||||
return Err(
|
||||
"Unexpected trailing expressions in script. Use (do ...) for sequences."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Expand Macros
|
||||
@@ -179,64 +192,71 @@ impl Environment {
|
||||
let registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: self.function_registry.clone(),
|
||||
});
|
||||
|
||||
|
||||
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
|
||||
let func_reg = self.function_registry.clone();
|
||||
let mono_cache = self.monomorph_cache.clone();
|
||||
let global_values = self.global_values.clone();
|
||||
let global_types = self.global_types.clone();
|
||||
let opt_level = self.optimization_level;
|
||||
|
||||
let compiler = Rc::new(move |func_template: BoundNode, arg_types: &[StaticType]| -> Result<(Value, StaticType), String> {
|
||||
// 1. Re-TypeCheck the template with concrete argument types
|
||||
let checker = TypeChecker::new(global_types.clone());
|
||||
let retyped_ast = checker.check(func_template, arg_types)?;
|
||||
|
||||
// 2. Specialize (Recursive)
|
||||
let sub_registry = Rc::new(EnvFunctionRegistry { registry: func_reg.clone() });
|
||||
let sub_rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
let sub_specializer = Specializer::new(
|
||||
Some(sub_registry),
|
||||
None,
|
||||
Some(sub_rtl_lookup),
|
||||
Some(mono_cache.clone())
|
||||
);
|
||||
|
||||
let specialized_ast = sub_specializer.specialize(retyped_ast);
|
||||
|
||||
// 3. Optimize (Phase 2: Cracking & Folding)
|
||||
let optimizer = Optimizer::new(opt_level);
|
||||
let optimized_ast = optimizer.optimize(specialized_ast);
|
||||
|
||||
// 4. TCO
|
||||
let tco_ast = TCO::optimize(optimized_ast);
|
||||
|
||||
// 5. Compile to Value (VM)
|
||||
let mut vm = VM::new(global_values.clone());
|
||||
let compiled_val = match vm.run(&tco_ast) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(format!("VM Error during specialization: {}", e)),
|
||||
};
|
||||
|
||||
// 6. Determine correct return type from the newly inferred function signature
|
||||
let ret_type = if let StaticType::Function(sig) = &tco_ast.ty {
|
||||
sig.ret.clone()
|
||||
} else {
|
||||
StaticType::Any
|
||||
};
|
||||
let compiler = Rc::new(
|
||||
move |func_template: BoundNode,
|
||||
arg_types: &[StaticType]|
|
||||
-> Result<(Value, StaticType), String> {
|
||||
// 1. Re-TypeCheck the template with concrete argument types
|
||||
let checker = TypeChecker::new(global_types.clone());
|
||||
let retyped_ast = checker.check(func_template, arg_types)?;
|
||||
|
||||
// 2. Specialize (Recursive)
|
||||
let sub_registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: func_reg.clone(),
|
||||
});
|
||||
let sub_rtl_lookup =
|
||||
Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
let sub_specializer = Specializer::new(
|
||||
Some(sub_registry),
|
||||
None,
|
||||
Some(sub_rtl_lookup),
|
||||
Some(mono_cache.clone()),
|
||||
);
|
||||
|
||||
let specialized_ast = sub_specializer.specialize(retyped_ast);
|
||||
|
||||
// 3. Optimize (Phase 2: Cracking & Folding)
|
||||
let optimizer = Optimizer::new(opt_level);
|
||||
let optimized_ast = optimizer.optimize(specialized_ast);
|
||||
|
||||
// 4. TCO
|
||||
let tco_ast = TCO::optimize(optimized_ast);
|
||||
|
||||
// 5. Compile to Value (VM)
|
||||
let mut vm = VM::new(global_values.clone());
|
||||
let compiled_val = match vm.run(&tco_ast) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(format!("VM Error during specialization: {}", e)),
|
||||
};
|
||||
|
||||
// 6. Determine correct return type from the newly inferred function signature
|
||||
let ret_type = if let StaticType::Function(sig) = &tco_ast.ty {
|
||||
sig.ret.clone()
|
||||
} else {
|
||||
StaticType::Any
|
||||
};
|
||||
|
||||
Ok((compiled_val, ret_type))
|
||||
},
|
||||
);
|
||||
|
||||
Ok((compiled_val, ret_type))
|
||||
});
|
||||
|
||||
let specializer = Specializer::new(
|
||||
Some(registry),
|
||||
Some(compiler),
|
||||
Some(rtl_lookup),
|
||||
Some(self.monomorph_cache.clone()),
|
||||
);
|
||||
|
||||
|
||||
specializer.specialize(node)
|
||||
}
|
||||
|
||||
@@ -244,10 +264,10 @@ impl Environment {
|
||||
pub fn run(&self, node: &TypedNode) -> Result<Value, String> {
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
let mut result = vm.run(node)?;
|
||||
|
||||
|
||||
// Handle potential script body closure
|
||||
if let Value::Object(obj) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
if let Value::Object(obj) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
{
|
||||
result = vm.run(&closure.function_node)?;
|
||||
}
|
||||
@@ -258,7 +278,10 @@ impl Environment {
|
||||
if let Some(closure) = next_obj.as_any().downcast_ref::<crate::ast::vm::Closure>() {
|
||||
result = vm.run_with_args(closure, next_args)?;
|
||||
} else {
|
||||
return Err(format!("Tail call target is not a closure: {}", next_obj.type_name()));
|
||||
return Err(format!(
|
||||
"Tail call target is not a closure: {}",
|
||||
next_obj.type_name()
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
@@ -286,10 +309,10 @@ impl Environment {
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
let mut observer = TracingObserver::new();
|
||||
let mut result = vm.run_with_observer(&mut observer, &linked);
|
||||
|
||||
|
||||
// If result is a closure (script entry), execute the body too
|
||||
if let Ok(Value::Object(obj)) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
if let Ok(Value::Object(obj)) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
{
|
||||
result = vm.run_with_observer(&mut observer, &closure.function_node);
|
||||
}
|
||||
@@ -300,7 +323,10 @@ impl Environment {
|
||||
if let Some(closure) = next_obj.as_any().downcast_ref::<crate::ast::vm::Closure>() {
|
||||
result = vm.run_with_args_observed(&mut observer, closure, next_args);
|
||||
} else {
|
||||
result = Err(format!("Tail call target is not a closure: {}", next_obj.type_name()));
|
||||
result = Err(format!(
|
||||
"Tail call target is not a closure: {}",
|
||||
next_obj.type_name()
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user