Expand description
Constant Propagation pass for TLExpr trees.
This pass performs compile-time evaluation of subexpressions whose operands
are all TLExpr::Constant(f64) values. It complements the crate::dead_code
pass (which handles structural boolean short-circuiting) by focusing on numeric
arithmetic, comparison folding, and unary math folding.
§Boolean Constants Convention
Consistent with the rest of the codebase:
TLExpr::Constant(1.0)represents logical TrueTLExpr::Constant(0.0)represents logical False
Comparison operations that evaluate to a definite truth value produce one of these two constants.
§Example
use tensorlogic_compiler::const_prop::{ConstantPropagator, ConstPropConfig};
use tensorlogic_ir::TLExpr;
let propagator = ConstantPropagator::with_default();
// Add(Mul(2, 3), 4) → 10
let expr = TLExpr::add(
TLExpr::mul(TLExpr::Constant(2.0), TLExpr::Constant(3.0)),
TLExpr::Constant(4.0),
);
let (result, stats) = propagator.run(expr);
assert!(matches!(result, TLExpr::Constant(v) if (v - 10.0).abs() < 1e-12));
assert!(stats.arithmetic_folds >= 2);Structs§
- Const
Prop Config - Configuration for the constant propagation pass.
- Const
Prop Stats - Statistics collected during a constant propagation run.
- Constant
Propagator - The constant propagation compiler pass.