Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OutlineCfg use insert_hugr; remove CfgBuilder::from_existing #298

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove CfgBuilder::from_existing
acl-cqc committed Jul 26, 2023
commit de0e1137df1f9bc2236843110366a38d68f6047b
48 changes: 0 additions & 48 deletions src/builder/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use itertools::Itertools;

use super::{
build_traits::SubContainer,
dataflow::{DFGBuilder, DFGWrapper},
@@ -99,21 +97,6 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
})
}

/// Create a CFGBuilder for an existing CFG node (that already has entry + exit nodes)
pub(crate) fn from_existing(base: B, cfg_node: Node) -> Result<Self, BuildError> {
let OpType::CFG(crate::ops::controlflow::CFG {outputs, ..}) = base.get_optype(cfg_node)
else {return Err(BuildError::UnexpectedType{node: cfg_node, op_desc: "Any CFG"});};
let n_out_wires = outputs.len();
let (_, exit_node) = base.children(cfg_node).take(2).collect_tuple().unwrap();
Ok(Self {
base,
cfg_node,
inputs: None, // This will prevent creating an entry node
exit_node,
n_out_wires,
})
}

/// Return a builder for a non-entry [`BasicBlock::DFB`] child graph with `inputs`
/// and `outputs` and the variants of the branching predicate Sum value
/// specified by `predicate_variants`.
@@ -306,8 +289,6 @@ impl BlockBuilder<Hugr> {

#[cfg(test)]
mod test {
use std::collections::HashSet;

use crate::builder::build_traits::HugrBuilder;
use crate::builder::{DataflowSubContainer, ModuleBuilder};
use crate::macros::classic_row;
@@ -350,35 +331,6 @@ mod test {

Ok(())
}
#[test]
fn from_existing() -> Result<(), BuildError> {
let mut cfg_builder = CFGBuilder::new(type_row![NAT], type_row![NAT])?;
build_basic_cfg(&mut cfg_builder)?;
let h = cfg_builder.finish_hugr()?;

let mut new_builder = CFGBuilder::from_existing(h.clone(), h.root())?;
assert_matches!(new_builder.simple_entry_builder(type_row![NAT], 1), Err(_));
let h2 = new_builder.finish_hugr()?;
assert_eq!(h, h2); // No new nodes added

let mut new_builder = CFGBuilder::from_existing(h.clone(), h.root())?;
let block_builder = new_builder.simple_block_builder(
vec![SimpleType::new_simple_predicate(1), NAT].into(),
type_row![NAT],
1,
)?;
let new_bb = block_builder.container_node();
let [pred, nat]: [Wire; 2] = block_builder.input_wires_arr();
block_builder.finish_with_outputs(pred, [nat])?;
let h2 = new_builder.finish_hugr()?;
let expected_nodes = h
.children(h.root())
.chain([new_bb])
.collect::<HashSet<Node>>();
assert_eq!(expected_nodes, HashSet::from_iter(h2.children(h2.root())));

Ok(())
}

fn build_basic_cfg<T: AsMut<Hugr> + AsRef<Hugr>>(
cfg_builder: &mut CFGBuilder<T>,