Skip to content

Graph Optimizer

Dead-code elimination, constant folding, and other graph-level optimizations.

optimize

Optimization passes for DSP graphs.

OptimizeStats

Bases: NamedTuple

Statistics from a single optimize_graph() run.

OptimizeResult

Bases: NamedTuple

Result of optimize_graph(): the optimized graph and pass statistics.

constant_fold

constant_fold(graph: Graph) -> Graph

Replace pure nodes with all-constant inputs by Constant nodes.

Returns a new Graph (immutable transform). Stateful nodes are never folded.

eliminate_dead_nodes

eliminate_dead_nodes(graph: Graph) -> Graph

Remove nodes not reachable from any output.

Walks backward from output sources, following ALL string fields (including feedback edges). When a DelayRead is reachable, the DelayWrite nodes that feed the same delay line are also treated as reachable (side-effecting nodes).

Returns a new Graph with dead nodes removed.

promote_control_rate

promote_control_rate(graph: Graph) -> Graph

Promote audio-rate pure nodes to control-rate when all deps are control/invariant.

A non-stateful node is promoted if it is not already control-rate or loop-invariant, and every string Ref field resolves to a param, literal float, invariant node, or (existing or already-promoted) control-rate node.

Returns a new Graph with additional entries in control_nodes. No-op when control_interval <= 0 or control_nodes is empty.

eliminate_cse

eliminate_cse(graph: Graph) -> Graph

Eliminate common subexpressions from the graph.

Two pure nodes with identical (type, op, resolved ref fields) are duplicates -- the later one is removed and all references rewritten to point to the earlier (canonical) one.

optimize_graph

optimize_graph(graph: Graph) -> OptimizeResult

Apply all optimization passes: constant folding, CSE, then dead node elimination.

Returns an OptimizeResult(graph, stats) named tuple.