pub struct OptimizationPipeline { /* private fields */ }Expand description
Multi-pass optimization pipeline for TLExpr expressions.
The pipeline applies 7 optimization passes in sequence, iterating until a fixed point is reached or the maximum number of iterations is hit.
§Pass Order
-
Negation optimization: Applies De Morgan’s laws and eliminates double negations. This exposes more opportunities for subsequent passes.
-
Constant folding: Evaluates constant expressions at compile time. This creates simpler expressions with fewer operations.
-
Algebraic simplification: Applies mathematical identities like x + 0 = x, x * 1 = x, etc. This can create new constants for folding.
-
Strength reduction: Replaces expensive operations with cheaper equivalents (e.g., x^2 → x*x, exp(log(x)) → x).
-
Distributivity: Factors common subexpressions to reduce redundant computation (e.g., ab + ac → a*(b+c)).
-
Quantifier optimization: Hoists loop-invariant expressions out of quantifiers (e.g., ∃x.(a + p(x)) → a + ∃x.p(x)).
-
Dead code elimination: Removes unreachable code and eliminates branches with constant conditions (e.g., if true then A else B → A).
§Fixed Point Detection
The pipeline tracks whether each pass makes changes. If an entire iteration produces no changes (i.e., the expression is unchanged), a fixed point has been reached and optimization stops early.
§Examples
use tensorlogic_compiler::optimize::{OptimizationPipeline, PipelineConfig};
use tensorlogic_ir::{TLExpr, Term};
// Default pipeline
let pipeline = OptimizationPipeline::new();
// Custom configuration
let config = PipelineConfig::default()
.with_max_iterations(5)
.with_constant_folding(true);
let pipeline = OptimizationPipeline::with_config(config);
// Optimize an expression
let expr = TLExpr::add(
TLExpr::mul(TLExpr::Constant(2.0), TLExpr::Constant(3.0)),
TLExpr::Constant(0.0)
);
let (optimized, stats) = pipeline.optimize(&expr);Implementations§
Source§impl OptimizationPipeline
impl OptimizationPipeline
Sourcepub fn with_config(config: PipelineConfig) -> Self
pub fn with_config(config: PipelineConfig) -> Self
Create a new optimization pipeline with custom configuration.
Sourcepub fn optimize(&self, expr: &TLExpr) -> (TLExpr, PipelineStats)
pub fn optimize(&self, expr: &TLExpr) -> (TLExpr, PipelineStats)
Optimize an expression using the configured pipeline.
Returns the optimized expression and statistics about the optimizations applied.
Sourcepub fn config(&self) -> &PipelineConfig
pub fn config(&self) -> &PipelineConfig
Get the current configuration.