Introduce Purity enum for optimizer

Refactor the optimizer to use a Purity enum instead of a boolean for
tracking function purity. This allows for a more granular representation
of purity:

- `Impure`: Functions with side effects.
- `SideEffectFree`: Functions without side effects but may not be
  deterministic (e.g., `now()`, `random()`).
- `Pure`: Functions without side effects and are deterministic.

This change enhances the optimizer's ability to perform more aggressive
optimizations by accurately determining function purity.
This commit is contained in:
Michael Schimmel
2026-02-22 08:42:22 +01:00
parent 329b885c4b
commit f35606616b
4 changed files with 86 additions and 60 deletions
+60 -36
View File
@@ -6,13 +6,20 @@ use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Purity {
Impure, // Has side effects (e.g. Set, Print)
SideEffectFree, // No side effects, but not deterministic (e.g. now(), random())
Pure, // No side effects and deterministic (e.g. sin(x), 1 + 2)
}
/// The Optimizer performs Phase 2 (Cracking), Phase 2.5/2.6 (Aggressive Collapsing & DCE)
/// and Phase 4 (Global Inlining).
pub struct Optimizer {
pub enabled: bool,
max_passes: usize,
pub globals: Option<Rc<RefCell<Vec<Value>>>>,
pub global_purity: Option<Rc<RefCell<HashMap<u32, bool>>>>,
pub global_purity: Option<Rc<RefCell<HashMap<u32, Purity>>>>,
pub lambda_registry: Option<Rc<RefCell<HashMap<u32, TypedNode>>>>,
}
@@ -32,7 +39,7 @@ impl Optimizer {
self
}
pub fn with_purity(mut self, purity: Rc<RefCell<HashMap<u32, bool>>>) -> Self {
pub fn with_purity(mut self, purity: Rc<RefCell<HashMap<u32, Purity>>>) -> Self {
self.global_purity = Some(purity);
self
}
@@ -239,7 +246,8 @@ impl Optimizer {
if let BoundKind::Constant(Value::Object(ref obj)) = callee.kind
&& sub.inlining_depth < 5
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
&& (closure.upvalues.is_empty() || self.is_pure(&closure.function_node))
&& (closure.upvalues.is_empty()
|| self.purity_of(&closure.function_node) >= Purity::SideEffectFree)
{
let mut closure_sub = SubstitutionMap::new();
closure_sub.inlining_depth = sub.inlining_depth + 1;
@@ -268,7 +276,8 @@ impl Optimizer {
&& let BoundKind::Constant(Value::Object(ref obj)) = callee.kind
&& sub.inlining_depth < 5
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
&& (closure.upvalues.is_empty() || self.is_pure(&closure.function_node))
&& (closure.upvalues.is_empty()
|| self.purity_of(&closure.function_node) >= Purity::SideEffectFree)
{
let mut closure_sub = SubstitutionMap::new();
closure_sub.inlining_depth = sub.inlining_depth + 1;
@@ -396,7 +405,7 @@ impl Optimizer {
BoundKind::DefLocal { slot, value, .. } => {
!used_in_block.contains(slot)
&& !sub.captured_slots.contains(slot)
&& self.is_pure(value)
&& self.purity_of(value) >= Purity::SideEffectFree
}
BoundKind::DefGlobal {
global_index,
@@ -404,7 +413,7 @@ impl Optimizer {
..
} => {
!used_globals_in_block.contains(global_index)
&& (self.is_pure(value)
&& (self.purity_of(value) >= Purity::SideEffectFree
|| matches!(value.kind, BoundKind::Lambda { .. }))
}
BoundKind::Set {
@@ -414,9 +423,9 @@ impl Optimizer {
} => {
!used_in_block.contains(slot)
&& !sub.captured_slots.contains(slot)
&& self.is_pure(value)
&& self.purity_of(value) >= Purity::SideEffectFree
}
_ => self.is_pure(&e),
_ => self.purity_of(&e) >= Purity::SideEffectFree,
};
if removable {
@@ -562,7 +571,7 @@ impl Optimizer {
sub.locals.remove(&slot);
}
if self.is_pure(&value) {
if self.purity_of(&value) == Purity::Pure {
sub.pure_slots.insert(slot);
} else {
sub.pure_slots.remove(&slot);
@@ -589,10 +598,11 @@ impl Optimizer {
if let BoundKind::Constant(val) = &value.kind {
sub.add_global(global_index, val.clone());
}
if self.is_pure(&value)
let p = self.purity_of(&value);
if p > Purity::Impure
&& let Some(purity_rc) = &self.global_purity
{
purity_rc.borrow_mut().insert(global_index, true);
purity_rc.borrow_mut().insert(global_index, p);
}
(
BoundKind::DefGlobal {
@@ -678,62 +688,75 @@ impl Optimizer {
}
}
fn is_pure(&self, node: &TypedNode) -> bool {
fn purity_of(&self, node: &TypedNode) -> Purity {
match &node.kind {
BoundKind::Constant(_) | BoundKind::Parameter { .. } | BoundKind::Nop => true,
BoundKind::Lambda { body, .. } => self.is_pure(body),
BoundKind::Constant(_) | BoundKind::Parameter { .. } | BoundKind::Nop => Purity::Pure,
BoundKind::Lambda { body, .. } => self.purity_of(body),
BoundKind::Get { addr, .. } => match addr {
Address::Global(idx) => {
if let Some(purity_rc) = &self.global_purity {
*purity_rc.borrow().get(idx).unwrap_or(&false)
*purity_rc.borrow().get(idx).unwrap_or(&Purity::Impure)
} else {
false
Purity::Impure
}
}
_ => true,
_ => Purity::Pure,
},
BoundKind::Tuple { elements } => elements.iter().all(|e| self.is_pure(e)),
BoundKind::Tuple { elements } => elements
.iter()
.map(|e| self.purity_of(e))
.min()
.unwrap_or(Purity::Pure),
BoundKind::Record { fields } => fields
.iter()
.all(|(k, v)| self.is_pure(k) && self.is_pure(v)),
.map(|(k, v)| self.purity_of(k).min(self.purity_of(v)))
.min()
.unwrap_or(Purity::Pure),
BoundKind::If {
cond,
then_br,
else_br,
} => {
self.is_pure(cond)
&& self.is_pure(then_br)
&& else_br.as_ref().is_none_or(|e| self.is_pure(e))
let p = self.purity_of(cond).min(self.purity_of(then_br));
if let Some(e) = else_br {
p.min(self.purity_of(e))
} else {
p
}
}
BoundKind::Block { exprs } => exprs.iter().all(|e| self.is_pure(e)),
BoundKind::Expansion { bound_expanded, .. } => self.is_pure(bound_expanded),
BoundKind::DefLocal { value, .. } => self.is_pure(value),
BoundKind::Set { .. } => false,
BoundKind::Block { exprs } => exprs
.iter()
.map(|e| self.purity_of(e))
.min()
.unwrap_or(Purity::Pure),
BoundKind::Expansion { bound_expanded, .. } => self.purity_of(bound_expanded),
BoundKind::DefLocal { value, .. } => self.purity_of(value),
BoundKind::Set { .. } => Purity::Impure,
BoundKind::Call { callee, args } => {
let callee_pure = match &callee.kind {
let callee_purity = match &callee.kind {
BoundKind::Get {
addr: Address::Global(idx),
..
} => {
if let Some(purity_rc) = &self.global_purity {
*purity_rc.borrow().get(idx).unwrap_or(&false)
*purity_rc.borrow().get(idx).unwrap_or(&Purity::Impure)
} else {
false
Purity::Impure
}
}
BoundKind::Constant(Value::Function(_)) => true,
BoundKind::Constant(Value::Function(_)) => Purity::Pure,
BoundKind::Constant(Value::Object(obj)) => {
if let Some(closure) = obj.as_any().downcast_ref::<Closure>() {
self.is_pure(&closure.function_node)
self.purity_of(&closure.function_node)
} else {
false
Purity::Impure
}
}
_ => false,
_ => Purity::Impure,
};
callee_pure && self.is_pure(args)
callee_purity.min(self.purity_of(args))
}
_ => false,
_ => Purity::Impure,
}
}
@@ -746,7 +769,8 @@ impl Optimizer {
args: Box::new(args.clone()),
},
};
if !self.is_pure(&temp_call) {
// Folding requires the function AND arguments to be truly Pure (deterministic).
if self.purity_of(&temp_call) < Purity::Pure {
return None;
}
let mut arg_nodes = Vec::new();