Applies common optimizations to all graphs in the context. The graphs must be fully inlined. The primary targets of the optimizations here are to remove inefficiencies which happen because of the boilerplate from Iterate inlining.
(context: Context, mut evaluator: T)
| 13 | /// The primary targets of the optimizations here are to remove inefficiencies |
| 14 | /// which happen because of the boilerplate from Iterate inlining. |
| 15 | pub fn optimize_context<T: Evaluator>(context: Context, mut evaluator: T) -> Result<Context> { |
| 16 | context.check_finalized()?; |
| 17 | evaluator.preprocess(context.clone())?; |
| 18 | let output_context = create_context()?; |
| 19 | for graph in context.get_graphs() { |
| 20 | let (_const_context, const_graph) = graph_in_new_context(graph.clone())?; |
| 21 | optimize_graph_constants(graph.clone(), const_graph.clone(), &mut evaluator)?; |
| 22 | const_graph.finalize()?; |
| 23 | |
| 24 | let (_meta_context, meta_graph) = graph_in_new_context(graph.clone())?; |
| 25 | optimize_graph_meta_operations(const_graph.clone(), meta_graph.clone())?; |
| 26 | meta_graph.finalize()?; |
| 27 | |
| 28 | let (_dup_context, dup_graph) = graph_in_new_context(graph.clone())?; |
| 29 | optimize_graph_duplicates(meta_graph.clone(), dup_graph.clone())?; |
| 30 | dup_graph.finalize()?; |
| 31 | |
| 32 | let final_graph = add_graph_to_context(output_context.clone(), graph.clone())?; |
| 33 | optimize_graph_dangling_nodes(dup_graph.clone(), final_graph.clone())?; |
| 34 | final_graph.finalize()?; |
| 35 | |
| 36 | if graph == context.get_main_graph()? { |
| 37 | final_graph.set_as_main()?; |
| 38 | } |
| 39 | } |
| 40 | output_context.finalize()?; |
| 41 | Ok(output_context) |
| 42 | } |
| 43 | |
| 44 | fn add_graph_to_context(context: Context, source_graph: Graph) -> Result<Graph> { |
| 45 | let new_graph = context.create_graph()?; |