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

refactor!: Remove NodeType and input_extensions #1183

Merged
merged 43 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8d117e8
WIP start removing NodeType
acl-cqc May 30, 2024
974236b
Revert "WIP start removing NodeType"
acl-cqc May 30, 2024
16c72e5
Drop validate_with_extension_closure...may want to revert eventually
acl-cqc May 30, 2024
759af86
Rm extension/validate.rs (ExtensionValidator), just check child.is_su…
acl-cqc May 30, 2024
07c09cd
Fix DataflowBlock::inner_signature (fixes 2*cfg tests in infer.rs)
acl-cqc May 30, 2024
08bb5a5
Fix tail_loop tests by adding extension_delta to TailLoop and impl Da…
acl-cqc May 30, 2024
d4e92d8
Fix/regenerate schema for TailLoop
acl-cqc May 30, 2024
968cc15
Fix constant.rs test_sum
acl-cqc May 30, 2024
e744527
Fix prelude.rs tests
acl-cqc May 30, 2024
7aa06b1
Fix constant.rs test
acl-cqc May 30, 2024
fcd10c1
Fix merge_bbs tests
acl-cqc May 30, 2024
664112f
Calculate delta in const_fold
acl-cqc May 30, 2024
fffacf9
const_fold/tests.rs via helper functions
acl-cqc May 30, 2024
3367490
const_fold/test.rs: drop first param to helper
acl-cqc May 30, 2024
8a9628f
Generalize parent_io_mismatch to cover Case, DFG and FuncDefn
acl-cqc May 31, 2024
a6e6459
Drop funcdefn_signature_mismatch(,2) tests
acl-cqc May 31, 2024
abe27fb
Don't call infer_extensions; drop broken tests
acl-cqc May 31, 2024
87b80d3
Rm infer.rs, infer/test.rs, infer_extensions, instantiate, etc. (keep…
acl-cqc May 31, 2024
5715ea1
Bonus (BREAKING): remove io_extensions()
acl-cqc May 31, 2024
d605c5b
Reinstate ExtensionError but as a struct
acl-cqc May 31, 2024
e691dea
And keep infer_extensions around as a No-op
acl-cqc May 31, 2024
4b61ff0
Test for CFG, BB
acl-cqc May 31, 2024
19e64a9
Be more explicit in ModuleRoot check
acl-cqc May 31, 2024
63a8a87
Tweak message
acl-cqc Jun 7, 2024
39fc4fe
Add assert message
acl-cqc Jun 7, 2024
00f152a
Conditional test
acl-cqc Jun 7, 2024
ae55091
Add TailLoop test
acl-cqc Jun 7, 2024
bcd23df
Combine BasicBlock/TailLoop tests
acl-cqc Jun 7, 2024
0f37a8f
Move cfg_children_restrictions out of extension_tests
acl-cqc Jun 7, 2024
53d2b88
...and inline singly-used add_block_children
acl-cqc Jun 7, 2024
bcec7cd
Merge remote-tracking branch 'origin/main' into extension_hierarchy
acl-cqc Jun 7, 2024
7e1cb4a
Add new test of too-many-connections in cfg
acl-cqc Jun 7, 2024
f256960
fix broken doclink
acl-cqc Jun 7, 2024
1bf1c14
clippy
acl-cqc Jun 7, 2024
546ced4
Try to fix 1.75 test
acl-cqc Jun 10, 2024
960f8d3
Remove NodeType
acl-cqc Jun 10, 2024
088f451
Hugr::new takes impl Into<OpType>
acl-cqc Jun 10, 2024
1ac8818
Rm add_child_op, duplicates add_child_node
acl-cqc Jun 10, 2024
8a9549b
ruff
acl-cqc Jun 10, 2024
52f9c66
update schema
acl-cqc Jun 10, 2024
c1950c9
Merge remote-tracking branch 'origin/main' into extension_hierarchy
acl-cqc Jun 12, 2024
5b6e2f5
Merge branch 'extension_hierarchy' into rm_input_exts
acl-cqc Jun 12, 2024
69a8306
Merge remote-tracking branch 'origin/main' into rm_input_exts
acl-cqc Jun 12, 2024
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
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
78 changes: 24 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 @@ -429,6 +400,7 @@ pub trait Dataflow: Container {
just_inputs: impl IntoIterator<Item = (Type, Wire)>,
inputs_outputs: impl IntoIterator<Item = (Type, Wire)>,
just_out_types: TypeRow,
extension_delta: ExtensionSet,
) -> Result<TailLoopBuilder<&mut Hugr>, BuildError> {
let (input_types, mut input_wires): (Vec<Type>, Vec<Wire>) =
just_inputs.into_iter().unzip();
Expand All @@ -440,6 +412,7 @@ pub trait Dataflow: Container {
just_inputs: input_types.into(),
just_outputs: just_out_types,
rest: rest_types.into(),
extension_delta,
};
// TODO: Make input extensions a parameter
let (loop_node, _) = add_node_with_wires(self, tail_loop.clone(), input_wires)?;
Expand Down Expand Up @@ -641,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