Skip to content

Commit

Permalink
refactor!: Remove NodeType and input_extensions (#1183)
Browse files Browse the repository at this point in the history
Following #1142 the `input_extensions` are unused, so remove the storage
for them

BREAKING CHANGE: * `add_child_op`(`_with_parent`), etc., gone; use
`add_child_node`(`_with_parent`) with an (impl Into-)OpType.
* `get_nodetype` gone - use `get_optype`.
* `NodeType` gone - use `OpType` directly. 
* Various (Into<)Option<ExtensionSet> params removed from builder
methods especially {cfg_,dfg_}builder.
* `input_extensions` removed from serialization schema.
  • Loading branch information
acl-cqc authored Jun 12, 2024
1 parent 65718f7 commit ea5213d
Show file tree
Hide file tree
Showing 31 changed files with 196 additions and 1,932 deletions.
6 changes: 3 additions & 3 deletions hugr-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub enum BuilderWiringError {
pub(crate) mod test {
use rstest::fixture;

use crate::hugr::{views::HugrView, HugrMut, NodeType};
use crate::hugr::{views::HugrView, HugrMut};
use crate::ops;
use crate::std_extensions::arithmetic::float_ops::FLOAT_OPS_REGISTRY;
use crate::types::{FunctionType, PolyFuncType, Type};
Expand Down Expand Up @@ -278,9 +278,9 @@ pub(crate) mod test {
/// inference. Using DFGBuilder will default to a root node with an open
/// extension variable
pub(crate) fn closed_dfg_root_hugr(signature: FunctionType) -> Hugr {
let mut hugr = Hugr::new(NodeType::new_pure(ops::DFG {
let mut hugr = Hugr::new(ops::DFG {
signature: signature.clone(),
}));
});
hugr.add_node_with_parent(
hugr.root(),
ops::Input {
Expand Down
76 changes: 22 additions & 54 deletions hugr-core/src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::{
use super::{BuilderWiringError, FunctionBuilder};

use crate::{
hugr::NodeType,
ops::handle::{ConstID, DataflowOpID, FuncID, NodeHandle},
types::EdgeKind,
};
Expand Down Expand Up @@ -45,12 +44,7 @@ pub trait Container {
/// Immutable reference to HUGR being built
fn hugr(&self) -> &Hugr;
/// Add an [`OpType`] as the final child of the container.
fn add_child_op(&mut self, op: impl Into<OpType>) -> Node {
let parent = self.container_node();
self.hugr_mut().add_node_with_parent(parent, op)
}
/// Add a [`NodeType`] as the final child of the container.
fn add_child_node(&mut self, node: NodeType) -> Node {
fn add_child_node(&mut self, node: impl Into<OpType>) -> Node {
let parent = self.container_node();
self.hugr_mut().add_node_with_parent(parent, node)
}
Expand All @@ -71,8 +65,7 @@ pub trait Container {
/// This function will return an error if there is an error in adding the
/// [`OpType::Const`] node.
fn add_constant(&mut self, constant: impl Into<ops::Const>) -> ConstID {
self.add_child_node(NodeType::new_pure(constant.into()))
.into()
self.add_child_node(constant.into()).into()
}

/// Add a [`ops::FuncDefn`] node and returns a builder to define the function
Expand All @@ -88,13 +81,12 @@ pub trait Container {
signature: PolyFuncType,
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
let body = signature.body().clone();
let f_node = self.add_child_node(NodeType::new_pure(ops::FuncDefn {
let f_node = self.add_child_node(ops::FuncDefn {
name: name.into(),
signature,
}));
});

let db =
DFGBuilder::create_with_io(self.hugr_mut(), f_node, body, Some(ExtensionSet::new()))?;
let db = DFGBuilder::create_with_io(self.hugr_mut(), f_node, body)?;
Ok(FunctionBuilder::from_dfg_builder(db))
}

Expand Down Expand Up @@ -182,29 +174,15 @@ pub trait Dataflow: Container {
fn input_wires(&self) -> Outputs {
self.input().outputs()
}
/// Add a dataflow op to the sibling graph, wiring up the `input_wires` to the
/// Add a dataflow [`OpType`] to the sibling graph, wiring up the `input_wires` to the
/// incoming ports of the resulting node.
///
/// # Errors
///
/// Returns a [`BuildError::OperationWiring`] error if the `input_wires` cannot be connected.
fn add_dataflow_op(
&mut self,
op: impl Into<OpType>,
input_wires: impl IntoIterator<Item = Wire>,
) -> Result<BuildHandle<DataflowOpID>, BuildError> {
self.add_dataflow_node(NodeType::new_auto(op), input_wires)
}

/// Add a dataflow [`NodeType`] to the sibling graph, wiring up the `input_wires` to the
/// incoming ports of the resulting node.
///
/// # Errors
///
/// Returns a [`BuildError::OperationWiring`] error if the `input_wires` cannot be connected.
fn add_dataflow_node(
&mut self,
nodetype: NodeType,
nodetype: impl Into<OpType>,
input_wires: impl IntoIterator<Item = Wire>,
) -> Result<BuildHandle<DataflowOpID>, BuildError> {
let outs = add_node_with_wires(self, nodetype, input_wires)?;
Expand Down Expand Up @@ -297,16 +275,14 @@ pub trait Dataflow: Container {
fn dfg_builder(
&mut self,
signature: FunctionType,
input_extensions: Option<ExtensionSet>,
input_wires: impl IntoIterator<Item = Wire>,
) -> Result<DFGBuilder<&mut Hugr>, BuildError> {
let op = ops::DFG {
signature: signature.clone(),
};
let nodetype = NodeType::new(op, input_extensions.clone());
let (dfg_n, _) = add_node_with_wires(self, nodetype, input_wires)?;
let (dfg_n, _) = add_node_with_wires(self, op, input_wires)?;

DFGBuilder::create_with_io(self.hugr_mut(), dfg_n, signature, input_extensions)
DFGBuilder::create_with_io(self.hugr_mut(), dfg_n, signature)
}

/// Return a builder for a [`crate::ops::CFG`] node,
Expand All @@ -322,7 +298,6 @@ pub trait Dataflow: Container {
fn cfg_builder(
&mut self,
inputs: impl IntoIterator<Item = (Type, Wire)>,
input_extensions: impl Into<Option<ExtensionSet>>,
output_types: TypeRow,
extension_delta: ExtensionSet,
) -> Result<CFGBuilder<&mut Hugr>, BuildError> {
Expand All @@ -332,13 +307,10 @@ pub trait Dataflow: Container {

let (cfg_node, _) = add_node_with_wires(
self,
NodeType::new(
ops::CFG {
signature: FunctionType::new(inputs.clone(), output_types.clone())
.with_extension_delta(extension_delta),
},
input_extensions.into(),
),
ops::CFG {
signature: FunctionType::new(inputs.clone(), output_types.clone())
.with_extension_delta(extension_delta),
},
input_wires,
)?;
CFGBuilder::create(self.hugr_mut(), cfg_node, inputs, output_types)
Expand All @@ -348,9 +320,8 @@ pub trait Dataflow: Container {
/// Adds a [`OpType::LoadConstant`] node.
fn load_const(&mut self, cid: &ConstID) -> Wire {
let const_node = cid.node();
let nodetype = self.hugr().get_nodetype(const_node);
let nodetype = self.hugr().get_optype(const_node);
let op: ops::Const = nodetype
.op()
.clone()
.try_into()
.expect("ConstID does not refer to Const op.");
Expand Down Expand Up @@ -394,7 +365,7 @@ pub trait Dataflow: Container {
exts: &ExtensionRegistry,
) -> Result<Wire, BuildError> {
let func_node = fid.node();
let func_op = self.hugr().get_nodetype(func_node).op();
let func_op = self.hugr().get_optype(func_node);
let func_sig = match func_op {
OpType::FuncDefn(ops::FuncDefn { signature, .. })
| OpType::FuncDecl(ops::FuncDecl { signature, .. }) => signature.clone(),
Expand Down Expand Up @@ -643,26 +614,23 @@ pub trait Dataflow: Container {
/// invalid edge.
fn add_node_with_wires<T: Dataflow + ?Sized>(
data_builder: &mut T,
nodetype: impl Into<NodeType>,
nodetype: impl Into<OpType>,
inputs: impl IntoIterator<Item = Wire>,
) -> Result<(Node, usize), BuildError> {
let nodetype: NodeType = nodetype.into();
let op = nodetype.into();
// Check there are no row variables, as that would prevent us
// from indexing into the node's ports in order to wire up
nodetype
.op_signature()
op.dataflow_signature()
.as_ref()
.and_then(FunctionType::find_rowvar)
.map_or(Ok(()), |(idx, _)| {
Err(SignatureError::RowVarWhereTypeExpected { idx })
})?;
let num_outputs = nodetype.op().value_output_count();
let op_node = data_builder.add_child_node(nodetype.clone());
let num_outputs = op.value_output_count();
let op_node = data_builder.add_child_node(op.clone());

wire_up_inputs(inputs, op_node, data_builder).map_err(|error| BuildError::OperationWiring {
op: nodetype.into_op(),
error,
})?;
wire_up_inputs(inputs, op_node, data_builder)
.map_err(|error| BuildError::OperationWiring { op, error })?;

Ok((op_node, num_outputs))
}
Expand Down
18 changes: 4 additions & 14 deletions hugr-core/src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use crate::{
use crate::{hugr::views::HugrView, types::TypeRow};

use crate::Node;
use crate::{
hugr::{HugrMut, NodeType},
type_row, Hugr,
};
use crate::{hugr::HugrMut, type_row, Hugr};

/// Builder for a [`crate::ops::CFG`] child control
/// flow graph.
Expand Down Expand Up @@ -158,7 +155,7 @@ impl CFGBuilder<Hugr> {
signature: signature.clone(),
};

let base = Hugr::new(NodeType::new_open(cfg_op));
let base = Hugr::new(cfg_op);
let cfg_node = base.root();
CFGBuilder::create(base, cfg_node, signature.input, signature.output)
}
Expand Down Expand Up @@ -336,12 +333,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> BlockBuilder<B> {
fn create(base: B, block_n: Node) -> Result<Self, BuildError> {
let block_op = base.get_optype(block_n).as_dataflow_block().unwrap();
let signature = block_op.inner_signature();
let inp_ex = base
.as_ref()
.get_nodetype(block_n)
.input_extensions()
.cloned();
let db = DFGBuilder::create_with_io(base, block_n, signature, inp_ex)?;
let db = DFGBuilder::create_with_io(base, block_n, signature)?;
Ok(BlockBuilder::from_dfg_builder(db))
}

Expand All @@ -363,7 +355,6 @@ impl BlockBuilder<Hugr> {
/// Initialize a [`DataflowBlock`] rooted HUGR builder
pub fn new(
inputs: impl Into<TypeRow>,
input_extensions: impl Into<Option<ExtensionSet>>,
sum_rows: impl IntoIterator<Item = TypeRow>,
other_outputs: impl Into<TypeRow>,
extension_delta: ExtensionSet,
Expand All @@ -378,7 +369,7 @@ impl BlockBuilder<Hugr> {
extension_delta,
};

let base = Hugr::new(NodeType::new(op, input_extensions));
let base = Hugr::new(op);
let root = base.root();
Self::create(base, root)
}
Expand Down Expand Up @@ -418,7 +409,6 @@ pub(crate) mod test {
let cfg_id = {
let mut cfg_builder = func_builder.cfg_builder(
vec![(NAT, int)],
None,
type_row![NAT],
ExtensionSet::new(),
)?;
Expand Down
16 changes: 5 additions & 11 deletions hugr-core/src/builder/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ use super::{
};

use crate::Node;
use crate::{
extension::ExtensionSet,
hugr::{HugrMut, NodeType},
Hugr,
};
use crate::{extension::ExtensionSet, hugr::HugrMut, Hugr};

use std::collections::HashSet;

Expand Down Expand Up @@ -130,7 +126,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
if let Some(&sibling_node) = self.case_nodes[case + 1..].iter().flatten().next() {
self.hugr_mut().add_node_before(sibling_node, case_op)
} else {
self.add_child_op(case_op)
self.add_child_node(case_op)
};

self.case_nodes[case] = Some(case_node);
Expand All @@ -139,7 +135,6 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
self.hugr_mut(),
case_node,
FunctionType::new(inputs, outputs).with_extension_delta(extension_delta),
None,
)?;

Ok(CaseBuilder::from_dfg_builder(dfg_builder))
Expand Down Expand Up @@ -177,8 +172,7 @@ impl ConditionalBuilder<Hugr> {
outputs,
extension_delta,
};
// TODO: Allow input extensions to be specified
let base = Hugr::new(NodeType::new_open(op));
let base = Hugr::new(op);
let conditional_node = base.root();

Ok(ConditionalBuilder {
Expand All @@ -196,9 +190,9 @@ impl CaseBuilder<Hugr> {
let op = ops::Case {
signature: signature.clone(),
};
let base = Hugr::new(NodeType::new_open(op));
let base = Hugr::new(op);
let root = base.root();
let dfg_builder = DFGBuilder::create_with_io(base, root, signature, None)?;
let dfg_builder = DFGBuilder::create_with_io(base, root, signature)?;

Ok(CaseBuilder::from_dfg_builder(dfg_builder))
}
Expand Down
Loading

0 comments on commit ea5213d

Please sign in to comment.