Skip to main content

OptimizationPipeline

Struct OptimizationPipeline 

Source
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

  1. Negation optimization: Applies De Morgan’s laws and eliminates double negations. This exposes more opportunities for subsequent passes.

  2. Constant folding: Evaluates constant expressions at compile time. This creates simpler expressions with fewer operations.

  3. Algebraic simplification: Applies mathematical identities like x + 0 = x, x * 1 = x, etc. This can create new constants for folding.

  4. Strength reduction: Replaces expensive operations with cheaper equivalents (e.g., x^2 → x*x, exp(log(x)) → x).

  5. Distributivity: Factors common subexpressions to reduce redundant computation (e.g., ab + ac → a*(b+c)).

  6. Quantifier optimization: Hoists loop-invariant expressions out of quantifiers (e.g., ∃x.(a + p(x)) → a + ∃x.p(x)).

  7. 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

Source

pub fn new() -> Self

Create a new optimization pipeline with default configuration.

Source

pub fn with_config(config: PipelineConfig) -> Self

Create a new optimization pipeline with custom configuration.

Source

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.

Source

pub fn config(&self) -> &PipelineConfig

Get the current configuration.

Trait Implementations§

Source§

impl Default for OptimizationPipeline

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.