Creates a new context from the given context, recursively inlining operations in the main graph with accordance to the given InlineConfig. In case of inlined operations with graph dependencies, these graphs are recursively processed with the same config. The inlining process preserves node annotations and names of nodes in the main graph. The name of a to-be-inlined Call/Iterate node is passed to
(context: Context, config: InlineConfig)
| 137 | /// The inlining process preserves node annotations and names of nodes in the main graph. |
| 138 | /// The name of a to-be-inlined Call/Iterate node is passed to a node containing its output. |
| 139 | pub fn inline_operations(context: Context, config: InlineConfig) -> Result<Context> { |
| 140 | context.check_finalized()?; |
| 141 | // First, collect all graphs reachable from the main graph which won't be |
| 142 | // inlined. |
| 143 | let mut graph_ids_to_process = HashSet::<u64>::new(); |
| 144 | let mut graph_ids_seen = HashSet::<u64>::new(); |
| 145 | let main_graph = context.get_main_graph()?; |
| 146 | graph_ids_to_process.insert(main_graph.get_id()); |
| 147 | graph_ids_seen.insert(main_graph.get_id()); |
| 148 | collect_graphs( |
| 149 | main_graph.clone(), |
| 150 | config.clone(), |
| 151 | &mut graph_ids_to_process, |
| 152 | &mut graph_ids_seen, |
| 153 | )?; |
| 154 | let output_context = create_context()?; |
| 155 | let mut inlining_context = InliningContext { |
| 156 | config, |
| 157 | context_mapping: ContextMappings::default(), |
| 158 | ephemeral_context_mapping: ContextMappings::default(), |
| 159 | }; |
| 160 | // Now, apply inlining to all of the collected graphs in the topological order. |
| 161 | for graph in context.get_graphs() { |
| 162 | if graph_ids_to_process.contains(&graph.get_id()) { |
| 163 | let new_graph = output_context.create_graph()?; |
| 164 | for annotation in graph.get_annotations()? { |
| 165 | new_graph.add_annotation(annotation)?; |
| 166 | } |
| 167 | inlining_context.insert_graph(graph.clone(), new_graph.clone()); |
| 168 | let output_node = |
| 169 | recursively_inline_graph(graph, new_graph.clone(), &mut inlining_context)?; |
| 170 | new_graph.set_output_node(output_node)?; |
| 171 | new_graph.finalize()?; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | output_context.set_main_graph(inlining_context.get_graph(main_graph))?; |
| 176 | output_context.finalize()?; |
| 177 | Ok(output_context) |
| 178 | } |
| 179 | |
| 180 | /// Determines all subgraphs reachable from graph which won't be inlined. |
| 181 | fn collect_graphs( |