From 7832cf071cd3780cfa832a87233ceffbd6bdb4b1 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 24 Jun 2024 18:02:51 +0100 Subject: [PATCH 01/22] Insert OpDef extension in compute_signature --- hugr-core/src/extension/op_def.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/hugr-core/src/extension/op_def.rs b/hugr-core/src/extension/op_def.rs index 42edd56dc..24c2a5f23 100644 --- a/hugr-core/src/extension/op_def.rs +++ b/hugr-core/src/extension/op_def.rs @@ -245,10 +245,8 @@ impl SignatureFunc { } }; - let res = pf.instantiate(args, exts)?; - // TODO bring this assert back once resource inference is done? - // https://github.com/CQCL/hugr/issues/388 - // debug_assert!(res.extension_reqs.contains(def.extension())); + let mut res = pf.instantiate(args, exts)?; + res.extension_reqs.insert(&def.extension); Ok(res) } } From d6802c33c8c71ef311062eceeb245de72b790c3e Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 24 Jun 2024 18:06:16 +0100 Subject: [PATCH 02/22] fix tests w/out extension_inference (only 3). 42 failures with --all-features... --- hugr-core/src/extension/op_def.rs | 18 +++++++++--------- .../src/std_extensions/arithmetic/int_ops.rs | 10 ++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/hugr-core/src/extension/op_def.rs b/hugr-core/src/extension/op_def.rs index 24c2a5f23..374f360cd 100644 --- a/hugr-core/src/extension/op_def.rs +++ b/hugr-core/src/extension/op_def.rs @@ -653,10 +653,10 @@ pub(super) mod test { let args = [TypeArg::BoundedNat { n: 3 }, USIZE_T.into()]; assert_eq!( def.compute_signature(&args, &PRELUDE_REGISTRY), - Ok(FunctionType::new( - vec![USIZE_T; 3], - vec![Type::new_tuple(vec![USIZE_T; 3])] - )) + Ok( + FunctionType::new(vec![USIZE_T; 3], vec![Type::new_tuple(vec![USIZE_T; 3])]) + .with_extension_delta(EXT_ID) + ) ); assert_eq!(def.validate_args(&args, &PRELUDE_REGISTRY, &[]), Ok(())); @@ -666,10 +666,10 @@ pub(super) mod test { let args = [TypeArg::BoundedNat { n: 3 }, tyvar.clone().into()]; assert_eq!( def.compute_signature(&args, &PRELUDE_REGISTRY), - Ok(FunctionType::new( - tyvars.clone(), - vec![Type::new_tuple(tyvars)] - )) + Ok( + FunctionType::new(tyvars.clone(), vec![Type::new_tuple(tyvars)]) + .with_extension_delta(EXT_ID) + ) ); def.validate_args(&args, &PRELUDE_REGISTRY, &[TypeBound::Eq.into()]) .unwrap(); @@ -722,7 +722,7 @@ pub(super) mod test { def.validate_args(&args, &EMPTY_REG, &decls).unwrap(); assert_eq!( def.compute_signature(&args, &EMPTY_REG), - Ok(FunctionType::new_endo(vec![tv])) + Ok(FunctionType::new_endo(vec![tv]).with_extension_delta(EXT_ID)) ); // But not with an external row variable let arg: TypeArg = Type::new_row_var_use(0, TypeBound::Eq).into(); diff --git a/hugr-core/src/std_extensions/arithmetic/int_ops.rs b/hugr-core/src/std_extensions/arithmetic/int_ops.rs index 546b2b6c3..cf3f51364 100644 --- a/hugr-core/src/std_extensions/arithmetic/int_ops.rs +++ b/hugr-core/src/std_extensions/arithmetic/int_ops.rs @@ -371,7 +371,7 @@ mod test { .to_extension_op() .unwrap() .signature(), - FunctionType::new(vec![int_type(3)], vec![int_type(4)],) + FunctionType::new(int_type(3), int_type(4)).with_extension_delta(EXTENSION_ID) ); assert_eq!( IntOpDef::iwiden_s @@ -379,7 +379,7 @@ mod test { .to_extension_op() .unwrap() .signature(), - FunctionType::new(vec![int_type(3)], vec![int_type(3)],) + FunctionType::new_endo(int_type(3)).with_extension_delta(EXTENSION_ID) ); assert_eq!( IntOpDef::inarrow_s @@ -387,7 +387,8 @@ mod test { .to_extension_op() .unwrap() .signature(), - FunctionType::new(vec![int_type(3)], vec![sum_with_error(int_type(3)).into()],) + FunctionType::new(vec![int_type(3)], vec![sum_with_error(int_type(3)).into()]) + .with_extension_delta(EXTENSION_ID) ); assert!( IntOpDef::iwiden_u @@ -403,7 +404,8 @@ mod test { .to_extension_op() .unwrap() .signature(), - FunctionType::new(vec![int_type(2)], vec![sum_with_error(int_type(1)).into()],) + FunctionType::new(vec![int_type(2)], vec![sum_with_error(int_type(1)).into()]) + .with_extension_delta(EXTENSION_ID) ); assert!(IntOpDef::inarrow_u From 2c32b1a5b835cc1ae542bf3b56f30bdedd20a4bf Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:17:43 +0100 Subject: [PATCH 03/22] simple_replace/sibling_subgraph - 23 failures remaining --- hugr-core/src/hugr/rewrite/simple_replace.rs | 34 ++++++++------------ hugr-core/src/hugr/views/sibling_subgraph.rs | 32 ++++++++++++++---- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/hugr-core/src/hugr/rewrite/simple_replace.rs b/hugr-core/src/hugr/rewrite/simple_replace.rs index 1863ba5c8..c60401c7e 100644 --- a/hugr-core/src/hugr/rewrite/simple_replace.rs +++ b/hugr-core/src/hugr/rewrite/simple_replace.rs @@ -219,11 +219,11 @@ pub(in crate::hugr::rewrite) mod test { use std::collections::{HashMap, HashSet}; use crate::builder::{ - BuildError, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer, + ft1, ft2, BuildError, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer, HugrBuilder, ModuleBuilder, }; use crate::extension::prelude::BOOL_T; - use crate::extension::{EMPTY_REG, PRELUDE_REGISTRY}; + use crate::extension::{ExtensionSet, EMPTY_REG, PRELUDE_REGISTRY}; use crate::hugr::views::{HugrView, SiblingSubgraph}; use crate::hugr::{Hugr, HugrMut, Rewrite}; use crate::ops::dataflow::DataflowOpTrait; @@ -234,7 +234,7 @@ pub(in crate::hugr::rewrite) mod test { use crate::std_extensions::logic::NotOp; use crate::type_row; use crate::types::{FunctionType, Type}; - use crate::utils::test_quantum_extension::{cx_gate, h_gate}; + use crate::utils::test_quantum_extension::{cx_gate, h_gate, EXTENSION_ID}; use crate::{IncomingPort, Node}; use super::SimpleReplacement; @@ -253,19 +253,17 @@ pub(in crate::hugr::rewrite) mod test { fn make_hugr() -> Result { let mut module_builder = ModuleBuilder::new(); let _f_id = { + let just_q: ExtensionSet = EXTENSION_ID.into(); let mut func_builder = module_builder.define_function( "main", - FunctionType::new(type_row![QB, QB, QB], type_row![QB, QB, QB]), + FunctionType::new_endo(type_row![QB, QB, QB]).with_extension_delta(just_q.clone()), )?; let [qb0, qb1, qb2] = func_builder.input_wires_arr(); let q_out = func_builder.add_dataflow_op(h_gate(), vec![qb2])?; - let mut inner_builder = func_builder.dfg_builder( - FunctionType::new(type_row![QB, QB], type_row![QB, QB]), - [qb0, qb1], - )?; + let mut inner_builder = func_builder.dfg_builder_endo([(QB, qb0), (QB, qb1)])?; let inner_graph = { let [wire0, wire1] = inner_builder.input_wires_arr(); let wire2 = inner_builder.add_dataflow_op(h_gate(), vec![wire0])?; @@ -294,8 +292,7 @@ pub(in crate::hugr::rewrite) mod test { /// ┤ H ├┤ X ├ /// └───┘└───┘ fn make_dfg_hugr() -> Result { - let mut dfg_builder = - DFGBuilder::new(FunctionType::new(type_row![QB, QB], type_row![QB, QB]))?; + let mut dfg_builder = DFGBuilder::new(ft1(type_row![QB, QB]))?; let [wire0, wire1] = dfg_builder.input_wires_arr(); let wire2 = dfg_builder.add_dataflow_op(h_gate(), vec![wire0])?; let wire3 = dfg_builder.add_dataflow_op(h_gate(), vec![wire1])?; @@ -315,8 +312,8 @@ pub(in crate::hugr::rewrite) mod test { /// ┤ H ├ /// └───┘ fn make_dfg_hugr2() -> Result { - let mut dfg_builder = - DFGBuilder::new(FunctionType::new(type_row![QB, QB], type_row![QB, QB]))?; + let mut dfg_builder = DFGBuilder::new(ft1(type_row![QB, QB]))?; + let [wire0, wire1] = dfg_builder.input_wires_arr(); let wire2 = dfg_builder.add_dataflow_op(h_gate(), vec![wire1])?; let wire2out = wire2.outputs().exactly_one().unwrap(); @@ -343,10 +340,8 @@ pub(in crate::hugr::rewrite) mod test { #[fixture] pub(in crate::hugr::rewrite) fn dfg_hugr_copy_bools() -> (Hugr, Vec) { fn build() -> Result<(Hugr, Vec), BuildError> { - let mut dfg_builder = DFGBuilder::new(FunctionType::new( - type_row![BOOL_T], - type_row![BOOL_T, BOOL_T], - ))?; + let mut dfg_builder = + DFGBuilder::new(ft2(type_row![BOOL_T], type_row![BOOL_T, BOOL_T]))?; let [b] = dfg_builder.input_wires_arr(); let not_inp = dfg_builder.add_dataflow_op(NotOp, vec![b])?; @@ -523,7 +518,7 @@ pub(in crate::hugr::rewrite) mod test { #[test] fn test_replace_cx_cross() { let q_row: Vec = vec![QB, QB]; - let mut builder = DFGBuilder::new(FunctionType::new(q_row.clone(), q_row)).unwrap(); + let mut builder = DFGBuilder::new(ft1(q_row)).unwrap(); let mut circ = builder.as_circuit(builder.input_wires()); circ.append(cx_gate(), [0, 1]).unwrap(); circ.append(cx_gate(), [1, 0]).unwrap(); @@ -581,8 +576,7 @@ pub(in crate::hugr::rewrite) mod test { let one_bit = type_row![BOOL_T]; let two_bit = type_row![BOOL_T, BOOL_T]; - let mut builder = - DFGBuilder::new(FunctionType::new(one_bit.clone(), one_bit.clone())).unwrap(); + let mut builder = DFGBuilder::new(ft1(one_bit.clone())).unwrap(); let inw = builder.input_wires().exactly_one().unwrap(); let outw = builder .add_dataflow_op(and_op(), [inw, inw]) @@ -591,7 +585,7 @@ pub(in crate::hugr::rewrite) mod test { let [input, _] = builder.io(); let mut h = builder.finish_hugr_with_outputs(outw, &EMPTY_REG).unwrap(); - let mut builder = DFGBuilder::new(FunctionType::new(two_bit, one_bit)).unwrap(); + let mut builder = DFGBuilder::new(ft2(two_bit, one_bit)).unwrap(); let inw = builder.input_wires(); let outw = builder.add_dataflow_op(and_op(), inw).unwrap().outputs(); let [repl_input, repl_output] = builder.io(); diff --git a/hugr-core/src/hugr/views/sibling_subgraph.rs b/hugr-core/src/hugr/views/sibling_subgraph.rs index 889e5e5ae..e7fc3026c 100644 --- a/hugr-core/src/hugr/views/sibling_subgraph.rs +++ b/hugr-core/src/hugr/views/sibling_subgraph.rs @@ -18,6 +18,7 @@ use portgraph::{view::Subgraph, Direction, PortView}; use thiserror::Error; use crate::builder::{Container, FunctionBuilder}; +use crate::extension::ExtensionSet; use crate::hugr::{HugrMut, HugrView, RootTagged}; use crate::ops::dataflow::DataflowOpTrait; use crate::ops::handle::{ContainerHandle, DataflowOpID}; @@ -307,7 +308,13 @@ impl SiblingSubgraph { sig.port_type(p).cloned().expect("must be dataflow edge") }) .collect_vec(); - FunctionType::new(input, output) + FunctionType::new(input, output).with_extension_delta(ExtensionSet::union_over( + self.nodes.iter().map(|n| { + hugr.signature(*n) + .expect("all nodes must have dataflow signature") + .extension_reqs + }), + )) } /// The parent of the sibling subgraph. @@ -731,8 +738,10 @@ mod tests { use cool_asserts::assert_matches; + use crate::builder::ft2; use crate::extension::PRELUDE_REGISTRY; - use crate::utils::test_quantum_extension::cx_gate; + use crate::std_extensions::logic; + use crate::utils::test_quantum_extension::{self, cx_gate}; use crate::{ builder::{ BuildError, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer, HugrBuilder, @@ -778,7 +787,9 @@ mod tests { let mut mod_builder = ModuleBuilder::new(); let func = mod_builder.declare( "test", - FunctionType::new_endo(type_row![QB_T, QB_T, QB_T]).into(), + FunctionType::new_endo(type_row![QB_T, QB_T, QB_T]) + .with_extension_delta(test_quantum_extension::EXTENSION_ID) + .into(), )?; let func_id = { let mut dfg = mod_builder.define_declaration(&func)?; @@ -794,7 +805,12 @@ mod tests { fn build_3not_hugr() -> Result<(Hugr, Node), BuildError> { let mut mod_builder = ModuleBuilder::new(); - let func = mod_builder.declare("test", FunctionType::new_endo(type_row![BOOL_T]).into())?; + let func = mod_builder.declare( + "test", + FunctionType::new_endo(type_row![BOOL_T]) + .with_extension_delta(logic::EXTENSION_ID) + .into(), + )?; let func_id = { let mut dfg = mod_builder.define_declaration(&func)?; let outs1 = dfg.add_dataflow_op(NotOp, dfg.input_wires())?; @@ -813,7 +829,9 @@ mod tests { let mut mod_builder = ModuleBuilder::new(); let func = mod_builder.declare( "test", - FunctionType::new(type_row![BOOL_T], type_row![BOOL_T]).into(), + FunctionType::new_endo(BOOL_T) + .with_extension_delta(logic::EXTENSION_ID) + .into(), )?; let func_id = { let mut dfg = mod_builder.define_declaration(&func)?; @@ -878,6 +896,7 @@ mod tests { assert_eq!( sub.signature(&func), FunctionType::new_endo(type_row![QB_T, QB_T]) + .with_extension_delta(test_quantum_extension::EXTENSION_ID) ); Ok(()) } @@ -1027,8 +1046,7 @@ mod tests { let one_bit = type_row![BOOL_T]; let two_bit = type_row![BOOL_T, BOOL_T]; - let mut builder = - DFGBuilder::new(FunctionType::new(one_bit.clone(), two_bit.clone())).unwrap(); + let mut builder = DFGBuilder::new(ft2(one_bit.clone(), two_bit.clone())).unwrap(); let inw = builder.input_wires().exactly_one().unwrap(); let outw1 = builder.add_dataflow_op(NotOp, [inw]).unwrap().out_wire(0); let outw2 = builder From 5cb15ca66fbcd1be9aa26ea0ae76f3eea2561e81 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:24:36 +0100 Subject: [PATCH 04/22] validate, note about closed_dfg_root_hugr --- hugr-core/src/builder.rs | 7 +++---- hugr-core/src/hugr/validate/test.rs | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/hugr-core/src/builder.rs b/hugr-core/src/builder.rs index 57fff12e2..317e37620 100644 --- a/hugr-core/src/builder.rs +++ b/hugr-core/src/builder.rs @@ -285,10 +285,9 @@ pub(crate) mod test { cfg_builder.finish_prelude_hugr().unwrap() } - /// A helper method which creates a DFG rooted hugr with closed resources, - /// for tests which want to avoid having open extension variables after - /// inference. Using DFGBuilder will default to a root node with an open - /// extension variable + /// A helper method which creates a DFG rooted hugr with Input and Output node + /// only (no wires), given a function type with extension delta. + // TODO consider taking two type rows and using TO_BE_INFERRED pub(crate) fn closed_dfg_root_hugr(signature: FunctionType) -> Hugr { let mut hugr = Hugr::new(ops::DFG { signature: signature.clone(), diff --git a/hugr-core/src/hugr/validate/test.rs b/hugr-core/src/hugr/validate/test.rs index 9e14ddbbe..ffe7d4be2 100644 --- a/hugr-core/src/hugr/validate/test.rs +++ b/hugr-core/src/hugr/validate/test.rs @@ -207,17 +207,18 @@ fn df_children_restrictions() { #[test] fn test_ext_edge() { - let mut h = closed_dfg_root_hugr(FunctionType::new( - type_row![BOOL_T, BOOL_T], - type_row![BOOL_T], - )); + let mut h = closed_dfg_root_hugr( + FunctionType::new(type_row![BOOL_T, BOOL_T], type_row![BOOL_T]) + .with_extension_delta(TO_BE_INFERRED), + ); let [input, output] = h.get_io(h.root()).unwrap(); // Nested DFG BOOL_T -> BOOL_T let sub_dfg = h.add_node_with_parent( h.root(), ops::DFG { - signature: FunctionType::new_endo(type_row![BOOL_T]), + signature: FunctionType::new_endo(type_row![BOOL_T]) + .with_extension_delta(TO_BE_INFERRED), }, ); // this Xor has its 2nd input unconnected @@ -284,7 +285,8 @@ fn no_ext_edge_into_func() -> Result<(), Box> { #[test] fn test_local_const() { - let mut h = closed_dfg_root_hugr(FunctionType::new(type_row![BOOL_T], type_row![BOOL_T])); + let mut h = + closed_dfg_root_hugr(FunctionType::new_endo(BOOL_T).with_extension_delta(TO_BE_INFERRED)); let [input, output] = h.get_io(h.root()).unwrap(); let and = h.add_node_with_parent(h.root(), and_op()); h.connect(input, 0, and, 0); @@ -606,7 +608,7 @@ fn instantiate_row_variables() -> Result<(), Box> { vec![TypeArg::Type { ty: USIZE_T }; i].into() } let e = extension_with_eval_parallel(); - let mut dfb = DFGBuilder::new(FunctionType::new( + let mut dfb = DFGBuilder::new(ft2( vec![ Type::new_function(FunctionType::new(USIZE_T, vec![USIZE_T, USIZE_T])), USIZE_T, @@ -648,7 +650,7 @@ fn inner_row_variables() -> Result<(), Box> { "id", PolyFuncType::new( [TypeParam::new_list(TypeBound::Any)], - FunctionType::new(inner_ft.clone(), ft_usz), + FunctionType::new(inner_ft.clone(), ft_usz).with_extension_delta(e.name.clone()), ), )?; // All the wires here are carrying higher-order Function values @@ -781,6 +783,7 @@ fn test_polymorphic_call() -> Result<(), Box> { PolyFuncType::new( vec![TypeParam::Extensions], FunctionType::new(vec![utou(es.clone()), int_pair.clone()], int_pair.clone()) + .with_extension_delta(EXT_ID) .with_extension_delta(es.clone()), ), )?; @@ -789,7 +792,7 @@ fn test_polymorphic_call() -> Result<(), Box> { (vec![type_row![USIZE_T; 2]], tup), vec![], type_row![USIZE_T;2], - es.clone(), + TO_BE_INFERRED.into(), //TODO make default //es.clone().union(EXT_ID.into()), )?; let mut cc = c.case_builder(0)?; let [i1, i2] = cc.input_wires_arr(); @@ -824,6 +827,7 @@ fn test_polymorphic_call() -> Result<(), Box> { let h = d.finish_hugr_with_outputs(call.outputs(), ®)?; let call_ty = h.get_optype(call.node()).dataflow_signature().unwrap(); let exp_fun_ty = FunctionType::new(vec![utou(PRELUDE_ID), int_pair.clone()], int_pair) + .with_extension_delta(EXT_ID) .with_extension_delta(PRELUDE_ID); assert_eq!(call_ty, exp_fun_ty); Ok(()) From 5b05df341e5b6bc3b8d21c31d464a809f271e281 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:27:31 +0100 Subject: [PATCH 05/22] extension (op_def.rs, prelude.rs), serialize --- hugr-core/src/extension/op_def.rs | 4 ++-- hugr-core/src/extension/prelude.rs | 4 ++-- hugr-core/src/hugr/serialize/test.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hugr-core/src/extension/op_def.rs b/hugr-core/src/extension/op_def.rs index 374f360cd..dd19525ee 100644 --- a/hugr-core/src/extension/op_def.rs +++ b/hugr-core/src/extension/op_def.rs @@ -478,7 +478,7 @@ pub(super) mod test { use itertools::Itertools; use super::SignatureFromArgs; - use crate::builder::{DFGBuilder, Dataflow, DataflowHugr}; + use crate::builder::{ft1, DFGBuilder, Dataflow, DataflowHugr}; use crate::extension::op_def::{CustomValidator, LowerFunc, OpDef, SignatureFunc}; use crate::extension::prelude::USIZE_T; use crate::extension::{ExtensionRegistry, ExtensionSet, PRELUDE}; @@ -601,7 +601,7 @@ pub(super) mod test { let list_usize = Type::new_extension(list_def.instantiate(vec![TypeArg::Type { ty: USIZE_T }])?); - let mut dfg = DFGBuilder::new(FunctionType::new_endo(vec![list_usize]))?; + let mut dfg = DFGBuilder::new(ft1(vec![list_usize]))?; let rev = dfg.add_dataflow_op( CustomOp::new_extension( e.instantiate_extension_op(&OP_NAME, vec![TypeArg::Type { ty: USIZE_T }], ®) diff --git a/hugr-core/src/extension/prelude.rs b/hugr-core/src/extension/prelude.rs index b2981a3b3..67481811d 100644 --- a/hugr-core/src/extension/prelude.rs +++ b/hugr-core/src/extension/prelude.rs @@ -402,7 +402,7 @@ impl CustomConst for ConstExternalSymbol { #[cfg(test)] mod test { use crate::{ - builder::{ft1, DFGBuilder, Dataflow, DataflowHugr}, + builder::{ft1, ft2, DFGBuilder, Dataflow, DataflowHugr}, utils::test_quantum_extension::cx_gate, Hugr, Wire, }; @@ -412,7 +412,7 @@ mod test { #[test] /// Test building a HUGR involving a new_array operation. fn test_new_array() { - let mut b = DFGBuilder::new(FunctionType::new( + let mut b = DFGBuilder::new(ft2( vec![QB_T, QB_T], vec![array_type(TypeArg::BoundedNat { n: 2 }, QB_T)], )) diff --git a/hugr-core/src/hugr/serialize/test.rs b/hugr-core/src/hugr/serialize/test.rs index a5d377c03..eaf61e64c 100644 --- a/hugr-core/src/hugr/serialize/test.rs +++ b/hugr-core/src/hugr/serialize/test.rs @@ -1,6 +1,6 @@ use super::*; use crate::builder::{ - ft2, test::closed_dfg_root_hugr, Container, DFGBuilder, Dataflow, DataflowHugr, + ft1, ft2, test::closed_dfg_root_hugr, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer, HugrBuilder, ModuleBuilder, }; use crate::extension::prelude::{BOOL_T, PRELUDE_ID, QB_T, USIZE_T}; @@ -298,7 +298,7 @@ fn dfg_roundtrip() -> Result<(), Box> { #[test] fn opaque_ops() -> Result<(), Box> { let tp: Vec = vec![BOOL_T; 1]; - let mut dfg = DFGBuilder::new(FunctionType::new_endo(tp))?; + let mut dfg = DFGBuilder::new(ft1(tp))?; let [wire] = dfg.input_wires_arr(); // Add an extension operation From 78f7aed8e931fbbd7afee548308809b54a364e5a Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 08:12:46 +0100 Subject: [PATCH 06/22] replace.rs - need better CFG support here --- hugr-core/src/hugr/rewrite/replace.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/hugr-core/src/hugr/rewrite/replace.rs b/hugr-core/src/hugr/rewrite/replace.rs index 08bea175c..09467720f 100644 --- a/hugr-core/src/hugr/rewrite/replace.rs +++ b/hugr-core/src/hugr/rewrite/replace.rs @@ -446,7 +446,7 @@ mod test { use itertools::Itertools; use crate::builder::{ - BuildError, CFGBuilder, Container, DFGBuilder, Dataflow, DataflowHugr, + ft1, BuildError, CFGBuilder, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer, HugrBuilder, SubContainer, }; use crate::extension::prelude::{BOOL_T, USIZE_T}; @@ -490,11 +490,7 @@ mod test { let just_list = TypeRow::from(vec![listy.clone()]); let intermed = TypeRow::from(vec![listy.clone(), USIZE_T]); - let mut cfg = CFGBuilder::new( - // One might expect an extension_delta of "collections" here, but push/pop - // have an empty delta themselves, pending https://github.com/CQCL/hugr/issues/388 - FunctionType::new_endo(just_list.clone()), - )?; + let mut cfg = CFGBuilder::new(ft1(just_list.clone()))?; let pred_const = cfg.add_constant(ops::Value::unary_unit_sum()); @@ -532,9 +528,7 @@ mod test { inputs: vec![listy.clone()].into(), sum_rows: vec![type_row![]], other_outputs: vec![listy.clone()].into(), - // This should be ExtensionSet::singleton(&collections::EXTENSION_NAME), - // at least when https://github.com/CQCL/issues/388 is fixed - extension_delta: ExtensionSet::new(), + extension_delta: collections::EXTENSION_NAME.into(), }, ); let r_df1 = replacement.add_node_with_parent( @@ -543,13 +537,15 @@ mod test { signature: FunctionType::new( vec![listy.clone()], simple_unary_plus(intermed.clone()), - ), + ) + .with_extension_delta(collections::EXTENSION_NAME), }, ); let r_df2 = replacement.add_node_with_parent( r_bb, DFG { - signature: FunctionType::new(intermed, simple_unary_plus(just_list.clone())), + signature: FunctionType::new(intermed, simple_unary_plus(just_list.clone())) + .with_extension_delta(collections::EXTENSION_NAME), }, ); [0, 1] From 856bfd47359f47cca18b6dc1b8ed41a1e5bfb0f7 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:29:23 +0100 Subject: [PATCH 07/22] builder copy_insertion+nested_identity (change from module+function to dfg) --- hugr-core/src/builder/dataflow.rs | 47 +++++++++++-------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/hugr-core/src/builder/dataflow.rs b/hugr-core/src/builder/dataflow.rs index 8446544a9..13bc7c948 100644 --- a/hugr-core/src/builder/dataflow.rs +++ b/hugr-core/src/builder/dataflow.rs @@ -203,7 +203,7 @@ pub(crate) mod test { use serde_json::json; use crate::builder::build_traits::DataflowHugr; - use crate::builder::{ft1, BuilderWiringError, DataflowSubContainer, ModuleBuilder}; + use crate::builder::{ft1, ft2, BuilderWiringError, DataflowSubContainer, ModuleBuilder}; use crate::extension::prelude::{BOOL_T, USIZE_T}; use crate::extension::{ExtensionId, SignatureError, EMPTY_REG, PRELUDE_REGISTRY}; use crate::hugr::validate::InterGraphEdgeError; @@ -224,25 +224,17 @@ pub(crate) mod test { #[test] fn nested_identity() -> Result<(), BuildError> { let build_result = { - let mut module_builder = ModuleBuilder::new(); - - let _f_id = { - let mut func_builder = module_builder.define_function( - "main", - FunctionType::new(type_row![NAT, QB], type_row![NAT, QB]), - )?; + let mut outer_builder = DFGBuilder::new(ft1(type_row![NAT, QB]))?; - let [int, qb] = func_builder.input_wires_arr(); + let [int, qb] = outer_builder.input_wires_arr(); - let q_out = func_builder.add_dataflow_op(h_gate(), vec![qb])?; + let q_out = outer_builder.add_dataflow_op(h_gate(), vec![qb])?; - let inner_builder = func_builder - .dfg_builder(FunctionType::new(type_row![NAT], type_row![NAT]), [int])?; - let inner_id = n_identity(inner_builder)?; + let inner_builder = outer_builder.dfg_builder_endo([(NAT, int)])?; + let inner_id = n_identity(inner_builder)?; - func_builder.finish_with_outputs(inner_id.outputs().chain(q_out.outputs()))? - }; - module_builder.finish_prelude_hugr() + outer_builder + .finish_prelude_hugr_with_outputs(inner_id.outputs().chain(q_out.outputs())) }; assert_eq!(build_result.err(), None); @@ -253,19 +245,14 @@ pub(crate) mod test { // Scaffolding for copy insertion tests fn copy_scaffold(f: F, msg: &'static str) -> Result<(), BuildError> where - F: FnOnce(FunctionBuilder<&mut Hugr>) -> Result>, BuildError>, + F: FnOnce(&mut DFGBuilder) -> Result<(), BuildError>, { let build_result = { - let mut module_builder = ModuleBuilder::new(); + let mut builder = DFGBuilder::new(ft2(BOOL_T, type_row![BOOL_T, BOOL_T]))?; - let f_build = module_builder.define_function( - "main", - FunctionType::new(type_row![BOOL_T], type_row![BOOL_T, BOOL_T]), - )?; + f(&mut builder)?; - f(f_build)?; - - module_builder.finish_hugr(&EMPTY_REG) + builder.finish_hugr(&EMPTY_REG) }; assert_matches!(build_result, Ok(_), "Failed on example: {}", msg); @@ -276,26 +263,26 @@ pub(crate) mod test { copy_scaffold( |f_build| { let [b1] = f_build.input_wires_arr(); - f_build.finish_with_outputs([b1, b1]) + f_build.set_outputs([b1, b1]) }, "Copy input and output", )?; copy_scaffold( - |mut f_build| { + |f_build| { let [b1] = f_build.input_wires_arr(); let xor = f_build.add_dataflow_op(and_op(), [b1, b1])?; - f_build.finish_with_outputs([xor.out_wire(0), b1]) + f_build.set_outputs([xor.out_wire(0), b1]) }, "Copy input and use with binary function", )?; copy_scaffold( - |mut f_build| { + |f_build| { let [b1] = f_build.input_wires_arr(); let xor1 = f_build.add_dataflow_op(and_op(), [b1, b1])?; let xor2 = f_build.add_dataflow_op(and_op(), [b1, xor1.out_wire(0)])?; - f_build.finish_with_outputs([xor2.out_wire(0), b1]) + f_build.set_outputs([xor2.out_wire(0), b1]) }, "Copy multiple times", )?; From b2c09d58bfcfea5fa659e16aec832132904115c3 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 09:26:09 +0100 Subject: [PATCH 08/22] circuit (explicit Function) --- hugr-core/src/builder/circuit.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hugr-core/src/builder/circuit.rs b/hugr-core/src/builder/circuit.rs index 2fecff49d..272ddead5 100644 --- a/hugr-core/src/builder/circuit.rs +++ b/hugr-core/src/builder/circuit.rs @@ -244,7 +244,7 @@ mod test { use crate::std_extensions::arithmetic::float_types::{self, ConstF64}; use crate::utils::test_quantum_extension::{ - cx_gate, h_gate, measure, q_alloc, q_discard, rz_f64, + self, cx_gate, h_gate, measure, q_alloc, q_discard, rz_f64, }; use crate::{ builder::{ @@ -261,6 +261,7 @@ mod test { fn simple_linear() { let build_res = build_main( FunctionType::new(type_row![QB, QB], type_row![QB, QB]) + .with_extension_delta(test_quantum_extension::EXTENSION_ID) .with_extension_delta(float_types::EXTENSION_ID) .into(), |mut f_build| { @@ -302,7 +303,9 @@ mod test { FunctionType::new(vec![QB, NAT], vec![QB]), )); let build_res = build_main( - FunctionType::new(type_row![QB, QB, NAT], type_row![QB, QB, BOOL_T]).into(), + FunctionType::new(type_row![QB, QB, NAT], type_row![QB, QB, BOOL_T]) + .with_extension_delta(test_quantum_extension::EXTENSION_ID) + .into(), |mut f_build| { let [q0, q1, angle]: [Wire; 3] = f_build.input_wires_arr(); @@ -327,7 +330,9 @@ mod test { #[test] fn ancillae() { let build_res = build_main( - FunctionType::new(type_row![QB], type_row![QB]).into(), + FunctionType::new_endo(QB) + .with_extension_delta(test_quantum_extension::EXTENSION_ID) + .into(), |mut f_build| { let mut circ = f_build.as_circuit(f_build.input_wires()); assert_eq!(circ.n_wires(), 1); From 10bab5d53bee0b205eee3927d0310f6402a5f19a Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:31:16 +0100 Subject: [PATCH 09/22] views (hugr-core now ok, and so, onto hugr-passes) --- hugr-core/src/hugr/views/descendants.rs | 10 +++++++--- hugr-core/src/hugr/views/tests.rs | 16 ++++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/hugr-core/src/hugr/views/descendants.rs b/hugr-core/src/hugr/views/descendants.rs index 35a6e46c6..a4598ac0e 100644 --- a/hugr-core/src/hugr/views/descendants.rs +++ b/hugr-core/src/hugr/views/descendants.rs @@ -205,7 +205,7 @@ pub(super) mod test { builder::{Container, Dataflow, DataflowSubContainer, HugrBuilder, ModuleBuilder}, type_row, types::{FunctionType, Type}, - utils::test_quantum_extension::h_gate, + utils::test_quantum_extension::{h_gate, EXTENSION_ID}, }; use super::*; @@ -223,7 +223,7 @@ pub(super) mod test { let (f_id, inner_id) = { let mut func_builder = module_builder.define_function( "main", - FunctionType::new(type_row![NAT, QB], type_row![NAT, QB]), + FunctionType::new_endo(type_row![NAT, QB]).with_extension_delta(EXTENSION_ID), )?; let [int, qb] = func_builder.input_wires_arr(); @@ -259,7 +259,11 @@ pub(super) mod test { assert_eq!( region.get_function_type(), - Some(FunctionType::new_endo(type_row![NAT, QB]).into()) + Some( + FunctionType::new_endo(type_row![NAT, QB]) + .with_extension_delta(EXTENSION_ID) + .into() + ) ); let inner_region: DescendantsGraph = DescendantsGraph::try_new(&hugr, inner)?; assert_eq!( diff --git a/hugr-core/src/hugr/views/tests.rs b/hugr-core/src/hugr/views/tests.rs index fbbb6f965..d79f0348a 100644 --- a/hugr-core/src/hugr/views/tests.rs +++ b/hugr-core/src/hugr/views/tests.rs @@ -2,7 +2,7 @@ use portgraph::PortOffset; use rstest::{fixture, rstest}; use crate::{ - builder::{ft2, BuildError, BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr}, + builder::{ft1, ft2, BuildError, BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr}, extension::prelude::QB_T, ops::{ handle::{DataflowOpID, NodeHandle}, @@ -16,11 +16,7 @@ use crate::{ #[fixture] fn sample_hugr() -> (Hugr, BuildHandle, BuildHandle) { - let mut dfg = DFGBuilder::new(FunctionType::new( - type_row![QB_T, QB_T], - type_row![QB_T, QB_T], - )) - .unwrap(); + let mut dfg = DFGBuilder::new(ft1(type_row![QB_T, QB_T])).unwrap(); let [q1, q2] = dfg.input_wires_arr(); @@ -125,11 +121,7 @@ fn value_types() { use crate::utils::test_quantum_extension::h_gate; use itertools::Itertools; - let mut dfg = DFGBuilder::new(FunctionType::new( - type_row![QB_T, BOOL_T], - type_row![BOOL_T, QB_T], - )) - .unwrap(); + let mut dfg = DFGBuilder::new(ft2(type_row![QB_T, BOOL_T], type_row![BOOL_T, QB_T])).unwrap(); let [q, b] = dfg.input_wires_arr(); let n1 = dfg.add_dataflow_op(h_gate(), [q]).unwrap(); @@ -176,7 +168,7 @@ fn test_dataflow_ports_only() { use crate::std_extensions::logic::NotOp; use itertools::Itertools; - let mut dfg = DFGBuilder::new(FunctionType::new(type_row![BOOL_T], type_row![BOOL_T])).unwrap(); + let mut dfg = DFGBuilder::new(ft1(BOOL_T)).unwrap(); let local_and = { let local_and = dfg .define_function( From 9e47b987a579558e5f3aa647557fe5120f45ad21 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:36:28 +0100 Subject: [PATCH 10/22] const_fold (WIN) - int_fn+float_fn+others => noargfn calling ft2 --- hugr-passes/src/const_fold/test.rs | 121 ++++++++++++++--------------- 1 file changed, 58 insertions(+), 63 deletions(-) diff --git a/hugr-passes/src/const_fold/test.rs b/hugr-passes/src/const_fold/test.rs index 60a9ea4cf..e1970f8ae 100644 --- a/hugr-passes/src/const_fold/test.rs +++ b/hugr-passes/src/const_fold/test.rs @@ -3,9 +3,9 @@ use hugr_core::builder::{DFGBuilder, Dataflow, DataflowHugr}; use hugr_core::extension::prelude::{sum_with_error, ConstError, ConstString, BOOL_T, STRING_TYPE}; use hugr_core::extension::{ExtensionRegistry, PRELUDE}; use hugr_core::ops::Value; +use hugr_core::std_extensions::arithmetic; use hugr_core::std_extensions::arithmetic::int_ops::IntOpDef; use hugr_core::std_extensions::arithmetic::int_types::{ConstInt, INT_TYPES}; -use hugr_core::std_extensions::arithmetic::{self, float_types, int_types}; use hugr_core::std_extensions::logic::{self, NaryLogic, NotOp}; use hugr_core::type_row; use hugr_core::types::{FunctionType, Type, TypeRow}; @@ -73,8 +73,8 @@ fn test_add(#[case] a: f64, #[case] b: f64, #[case] c: f64) { assert_eq!(outs.as_slice(), &[(0.into(), c)]); } -fn float_fn(outputs: impl Into) -> FunctionType { - FunctionType::new(type_row![], outputs).with_extension_delta(float_types::EXTENSION_ID) +fn noargfn(outputs: impl Into) -> FunctionType { + ft2(type_row![], outputs) } #[test] @@ -85,7 +85,7 @@ fn test_big() { int(x.0 - x.1) == 2 */ let sum_type = sum_with_error(INT_TYPES[5].to_owned()); - let mut build = DFGBuilder::new(float_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let tup = build.add_load_const(Value::tuple([f2c(5.6), f2c(3.2)])); @@ -170,7 +170,7 @@ fn test_fold_and() { // x0, x1 := bool(true), bool(true) // x2 := and(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(FunctionType::new(type_row![], vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(BOOL_T)).unwrap(); let x0 = build.add_load_const(Value::true_val()); let x1 = build.add_load_const(Value::true_val()); let x2 = build @@ -190,7 +190,7 @@ fn test_fold_or() { // x0, x1 := bool(true), bool(false) // x2 := or(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(FunctionType::new(type_row![], vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(BOOL_T)).unwrap(); let x0 = build.add_load_const(Value::true_val()); let x1 = build.add_load_const(Value::false_val()); let x2 = build @@ -210,7 +210,7 @@ fn test_fold_not() { // x0 := bool(true) // x1 := not(x0) // output x1 == false; - let mut build = DFGBuilder::new(FunctionType::new(type_row![], vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(BOOL_T)).unwrap(); let x0 = build.add_load_const(Value::true_val()); let x1 = build.add_dataflow_op(NotOp, [x0]).unwrap(); let reg = @@ -233,7 +233,7 @@ fn orphan_output() { // with no outputs. use hugr_core::ops::handle::NodeHandle; - let mut build = DFGBuilder::new(FunctionType::new(type_row![], vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let true_wire = build.add_load_value(Value::true_val()); // this Not will be manually replaced let orig_not = build.add_dataflow_op(NotOp, [true_wire]).unwrap(); @@ -273,7 +273,7 @@ fn test_folding_pass_issue_996() { // x6 := flt(x0, x5) // false // x7 := or(x4, x6) // true // output x7 - let mut build = DFGBuilder::new(float_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstF64::new(3.0))); let x1 = build.add_load_const(Value::extension(ConstF64::new(4.0))); let x2 = build.add_dataflow_op(FloatOps::fne, [x0, x1]).unwrap(); @@ -313,7 +313,7 @@ fn test_const_fold_to_nonfinite() { .unwrap(); // HUGR computing 1.0 / 1.0 - let mut build = DFGBuilder::new(float_fn(vec![FLOAT64_TYPE])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![FLOAT64_TYPE])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstF64::new(1.0))); let x1 = build.add_load_const(Value::extension(ConstF64::new(1.0))); let x2 = build.add_dataflow_op(FloatOps::fdiv, [x0, x1]).unwrap(); @@ -325,7 +325,7 @@ fn test_const_fold_to_nonfinite() { assert_eq!(h0.node_count(), 5); // HUGR computing 1.0 / 0.0 - let mut build = DFGBuilder::new(float_fn(vec![FLOAT64_TYPE])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![FLOAT64_TYPE])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstF64::new(1.0))); let x1 = build.add_load_const(Value::extension(ConstF64::new(0.0))); let x2 = build.add_dataflow_op(FloatOps::fdiv, [x0, x1]).unwrap(); @@ -334,10 +334,6 @@ fn test_const_fold_to_nonfinite() { assert_eq!(h1.node_count(), 8); } -fn int_fn(outputs: impl Into) -> FunctionType { - FunctionType::new(type_row![], outputs).with_extension_delta(int_types::EXTENSION_ID) -} - #[test] fn test_fold_iwiden_u() { // pseudocode: @@ -345,7 +341,7 @@ fn test_fold_iwiden_u() { // x0 := int_u<4>(13); // x1 := iwiden_u<4, 5>(x0); // output x1 == int_u<5>(13); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(4, 13).unwrap())); let x1 = build .add_dataflow_op(IntOpDef::iwiden_u.with_two_log_widths(4, 5), [x0]) @@ -368,7 +364,7 @@ fn test_fold_iwiden_s() { // x0 := int_u<4>(-3); // x1 := iwiden_u<4, 5>(x0); // output x1 == int_s<5>(-3); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(4, -3).unwrap())); let x1 = build .add_dataflow_op(IntOpDef::iwiden_s.with_two_log_widths(4, 5), [x0]) @@ -413,7 +409,7 @@ fn test_fold_inarrow, E: std::fmt::Debug>( // succeeds => whether to expect a int variant or an error // variant. let sum_type = sum_with_error(INT_TYPES[to_log_width as usize].to_owned()); - let mut build = DFGBuilder::new(int_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let x0 = build.add_load_const(mk_const(from_log_width, val).unwrap().into()); let x1 = build .add_dataflow_op( @@ -450,7 +446,7 @@ fn test_fold_itobool() { // x0 := int_u<0>(1); // x1 := itobool(x0); // output x1 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(0, 1).unwrap())); let x1 = build .add_dataflow_op(IntOpDef::itobool.without_log_width(), [x0]) @@ -473,8 +469,7 @@ fn test_fold_ifrombool() { // x0 := false // x1 := ifrombool(x0); // output x1 == int_u<0>(0); - let mut build = - DFGBuilder::new(FunctionType::new(type_row![], vec![INT_TYPES[0].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[0].clone()])).unwrap(); let x0 = build.add_load_const(Value::false_val()); let x1 = build .add_dataflow_op(IntOpDef::ifrombool.without_log_width(), [x0]) @@ -496,7 +491,7 @@ fn test_fold_ieq() { // x0, x1 := int_s<3>(-1), int_u<3>(255) // x2 := ieq(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(3, -1).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 255).unwrap())); let x2 = build @@ -519,7 +514,7 @@ fn test_fold_ine() { // x0, x1 := int_u<5>(3), int_u<5>(4) // x2 := ine(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 4).unwrap())); let x2 = build @@ -542,7 +537,7 @@ fn test_fold_ilt_u() { // x0, x1 := int_u<5>(3), int_u<5>(4) // x2 := ilt_u(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 4).unwrap())); let x2 = build @@ -565,7 +560,7 @@ fn test_fold_ilt_s() { // x0, x1 := int_s<5>(3), int_s<5>(-4) // x2 := ilt_s(x0, x1) // output x2 == false; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, -4).unwrap())); let x2 = build @@ -588,7 +583,7 @@ fn test_fold_igt_u() { // x0, x1 := int_u<5>(3), int_u<5>(4) // x2 := ilt_u(x0, x1) // output x2 == false; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 4).unwrap())); let x2 = build @@ -611,7 +606,7 @@ fn test_fold_igt_s() { // x0, x1 := int_s<5>(3), int_s<5>(-4) // x2 := ilt_s(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, -4).unwrap())); let x2 = build @@ -634,7 +629,7 @@ fn test_fold_ile_u() { // x0, x1 := int_u<5>(3), int_u<5>(3) // x2 := ile_u(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 3).unwrap())); let x2 = build @@ -657,7 +652,7 @@ fn test_fold_ile_s() { // x0, x1 := int_s<5>(-4), int_s<5>(-4) // x2 := ile_s(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -4).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, -4).unwrap())); let x2 = build @@ -680,7 +675,7 @@ fn test_fold_ige_u() { // x0, x1 := int_u<5>(3), int_u<5>(4) // x2 := ilt_u(x0, x1) // output x2 == false; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 4).unwrap())); let x2 = build @@ -703,7 +698,7 @@ fn test_fold_ige_s() { // x0, x1 := int_s<5>(3), int_s<5>(-4) // x2 := ilt_s(x0, x1) // output x2 == true; - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, -4).unwrap())); let x2 = build @@ -726,7 +721,7 @@ fn test_fold_imax_u() { // x0, x1 := int_u<5>(7), int_u<5>(11); // x2 := imax_u(x0, x1); // output x2 == int_u<5>(11); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 7).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 11).unwrap())); let x2 = build @@ -749,7 +744,7 @@ fn test_fold_imax_s() { // x0, x1 := int_s<5>(-2), int_s<5>(1); // x2 := imax_u(x0, x1); // output x2 == int_s<5>(1); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -2).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, 1).unwrap())); let x2 = build @@ -772,7 +767,7 @@ fn test_fold_imin_u() { // x0, x1 := int_u<5>(7), int_u<5>(11); // x2 := imin_u(x0, x1); // output x2 == int_u<5>(7); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 7).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 11).unwrap())); let x2 = build @@ -795,7 +790,7 @@ fn test_fold_imin_s() { // x0, x1 := int_s<5>(-2), int_s<5>(1); // x2 := imin_u(x0, x1); // output x2 == int_s<5>(-2); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -2).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, 1).unwrap())); let x2 = build @@ -818,7 +813,7 @@ fn test_fold_iadd() { // x0, x1 := int_s<5>(-2), int_s<5>(1); // x2 := iadd(x0, x1); // output x2 == int_s<5>(-1); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -2).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, 1).unwrap())); let x2 = build @@ -841,7 +836,7 @@ fn test_fold_isub() { // x0, x1 := int_s<5>(-2), int_s<5>(1); // x2 := isub(x0, x1); // output x2 == int_s<5>(-3); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -2).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, 1).unwrap())); let x2 = build @@ -864,7 +859,7 @@ fn test_fold_ineg() { // x0 := int_s<5>(-2); // x1 := ineg(x0); // output x1 == int_s<5>(2); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -2).unwrap())); let x2 = build .add_dataflow_op(IntOpDef::ineg.with_log_width(5), [x0]) @@ -886,7 +881,7 @@ fn test_fold_imul() { // x0, x1 := int_s<5>(-2), int_s<5>(7); // x2 := imul(x0, x1); // output x2 == int_s<5>(-14); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -2).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_s(5, 7).unwrap())); let x2 = build @@ -911,7 +906,7 @@ fn test_fold_idivmod_checked_u() { // output x2 == error let intpair: TypeRow = vec![INT_TYPES[5].clone(), INT_TYPES[3].clone()].into(); let sum_type = sum_with_error(Type::new_tuple(intpair)); - let mut build = DFGBuilder::new(int_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 0).unwrap())); let x2 = build @@ -948,7 +943,7 @@ fn test_fold_idivmod_u() { // x4 := iwiden_u<3,5>(x3); // 2 // x5 := iadd<5>(x2, x4); // 8 // output x5 == int_u<5>(8); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let [x2, x3] = build @@ -981,7 +976,7 @@ fn test_fold_idivmod_checked_s() { // output x2 == error let intpair: TypeRow = vec![INT_TYPES[5].clone(), INT_TYPES[3].clone()].into(); let sum_type = sum_with_error(Type::new_tuple(intpair)); - let mut build = DFGBuilder::new(int_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 0).unwrap())); let x2 = build @@ -1019,7 +1014,7 @@ fn test_fold_idivmod_checked_s() { #[case(i64::MIN, 1u64 << 63, -1)] // c = a/b + a%b fn test_fold_idivmod_s(#[case] a: i64, #[case] b: u64, #[case] c: i64) { - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[6].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[6].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(6, a).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(6, b).unwrap())); let [x2, x3] = build @@ -1047,7 +1042,7 @@ fn test_fold_idiv_checked_u() { // x2 := idiv_checked_u(x0, x1) // output x2 == error let sum_type = sum_with_error(INT_TYPES[5].to_owned()); - let mut build = DFGBuilder::new(int_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 0).unwrap())); let x2 = build @@ -1079,7 +1074,7 @@ fn test_fold_idiv_u() { // x0, x1 := int_u<5>(20), int_u<3>(3); // x2 := idiv_u(x0, x1); // output x2 == int_u<5>(6); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let x2 = build @@ -1103,7 +1098,7 @@ fn test_fold_imod_checked_u() { // x2 := imod_checked_u(x0, x1) // output x2 == error let sum_type = sum_with_error(INT_TYPES[3].to_owned()); - let mut build = DFGBuilder::new(int_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 0).unwrap())); let x2 = build @@ -1135,7 +1130,7 @@ fn test_fold_imod_u() { // x0, x1 := int_u<5>(20), int_u<3>(3); // x2 := imod_u(x0, x1); // output x2 == int_u<3>(2); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[3].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[3].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let x2 = build @@ -1159,7 +1154,7 @@ fn test_fold_idiv_checked_s() { // x2 := idiv_checked_s(x0, x1) // output x2 == error let sum_type = sum_with_error(INT_TYPES[5].to_owned()); - let mut build = DFGBuilder::new(int_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 0).unwrap())); let x2 = build @@ -1191,7 +1186,7 @@ fn test_fold_idiv_s() { // x0, x1 := int_s<5>(-20), int_u<3>(3); // x2 := idiv_s(x0, x1); // output x2 == int_s<5>(-7); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let x2 = build @@ -1215,7 +1210,7 @@ fn test_fold_imod_checked_s() { // x2 := imod_checked_u(x0, x1) // output x2 == error let sum_type = sum_with_error(INT_TYPES[3].to_owned()); - let mut build = DFGBuilder::new(int_fn(vec![sum_type.clone().into()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![sum_type.clone().into()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 0).unwrap())); let x2 = build @@ -1247,7 +1242,7 @@ fn test_fold_imod_s() { // x0, x1 := int_s<5>(-20), int_u<3>(3); // x2 := imod_s(x0, x1); // output x2 == int_u<3>(1); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[3].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[3].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -20).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let x2 = build @@ -1270,7 +1265,7 @@ fn test_fold_iabs() { // x0 := int_s<5>(-2); // x1 := iabs(x0); // output x1 == int_s<5>(2); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -2).unwrap())); let x2 = build .add_dataflow_op(IntOpDef::iabs.with_log_width(5), [x0]) @@ -1292,7 +1287,7 @@ fn test_fold_iand() { // x0, x1 := int_u<5>(14), int_u<5>(20); // x2 := iand(x0, x1); // output x2 == int_u<5>(4); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 14).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x2 = build @@ -1315,7 +1310,7 @@ fn test_fold_ior() { // x0, x1 := int_u<5>(14), int_u<5>(20); // x2 := ior(x0, x1); // output x2 == int_u<5>(30); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 14).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x2 = build @@ -1338,7 +1333,7 @@ fn test_fold_ixor() { // x0, x1 := int_u<5>(14), int_u<5>(20); // x2 := ixor(x0, x1); // output x2 == int_u<5>(26); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 14).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 20).unwrap())); let x2 = build @@ -1361,7 +1356,7 @@ fn test_fold_inot() { // x0 := int_u<5>(14); // x1 := inot(x0); // output x1 == int_u<5>(17); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 14).unwrap())); let x2 = build .add_dataflow_op(IntOpDef::inot.with_log_width(5), [x0]) @@ -1383,7 +1378,7 @@ fn test_fold_ishl() { // x0, x1 := int_u<5>(14), int_u<3>(3); // x2 := ishl(x0, x1); // output x2 == int_u<5>(112); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, 14).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let x2 = build @@ -1406,7 +1401,7 @@ fn test_fold_ishr() { // x0, x1 := int_u<5>(14), int_u<3>(3); // x2 := ishr(x0, x1); // output x2 == int_u<5>(1); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, 14).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let x2 = build @@ -1429,7 +1424,7 @@ fn test_fold_irotl() { // x0, x1 := int_u<5>(14), int_u<3>(61); // x2 := irotl(x0, x1); // output x2 == int_u<5>(2^30 + 2^31 + 1); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, 14).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 61).unwrap())); let x2 = build @@ -1452,7 +1447,7 @@ fn test_fold_irotr() { // x0, x1 := int_u<5>(14), int_u<3>(3); // x2 := irotr(x0, x1); // output x2 == int_u<5>(2^30 + 2^31 + 1); - let mut build = DFGBuilder::new(int_fn(vec![INT_TYPES[5].clone()])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![INT_TYPES[5].clone()])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, 14).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(3, 3).unwrap())); let x2 = build @@ -1475,7 +1470,7 @@ fn test_fold_itostring_u() { // x0 := int_u<5>(17); // x1 := itostring_u(x0); // output x2 := "17"; - let mut build = DFGBuilder::new(int_fn(vec![STRING_TYPE])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![STRING_TYPE])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 17).unwrap())); let x1 = build .add_dataflow_op(IntOpDef::itostring_u.with_log_width(5), [x0]) @@ -1497,7 +1492,7 @@ fn test_fold_itostring_s() { // x0 := int_s<5>(-17); // x1 := itostring_s(x0); // output x2 := "-17"; - let mut build = DFGBuilder::new(int_fn(vec![STRING_TYPE])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![STRING_TYPE])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -17).unwrap())); let x1 = build .add_dataflow_op(IntOpDef::itostring_s.with_log_width(5), [x0]) @@ -1526,7 +1521,7 @@ fn test_fold_int_ops() { // x6 := ilt_s(x0, x5) // false // x7 := or(x4, x6) // true // output x7 - let mut build = DFGBuilder::new(int_fn(vec![BOOL_T])).unwrap(); + let mut build = DFGBuilder::new(noargfn(vec![BOOL_T])).unwrap(); let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 3).unwrap())); let x1 = build.add_load_const(Value::extension(ConstInt::new_u(5, 4).unwrap())); let x2 = build From 290bb6aac8e49b0f517d63f5d7ca1ca6c5c4d836 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 14:38:12 +0100 Subject: [PATCH 11/22] merge_bbs (TODO simple_block_builder, block_builder) --- hugr-passes/src/merge_bbs.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/hugr-passes/src/merge_bbs.rs b/hugr-passes/src/merge_bbs.rs index 399e86055..cc0e0bf20 100644 --- a/hugr-passes/src/merge_bbs.rs +++ b/hugr-passes/src/merge_bbs.rs @@ -161,9 +161,9 @@ mod test { use itertools::Itertools; use rstest::rstest; - use hugr_core::builder::{CFGBuilder, DFGWrapper, Dataflow, HugrBuilder}; + use hugr_core::builder::{ft2, CFGBuilder, DFGWrapper, Dataflow, HugrBuilder}; use hugr_core::extension::prelude::{ConstUsize, PRELUDE_ID, QB_T, USIZE_T}; - use hugr_core::extension::{ExtensionRegistry, ExtensionSet, PRELUDE, PRELUDE_REGISTRY}; + use hugr_core::extension::{ExtensionRegistry, PRELUDE, PRELUDE_REGISTRY, TO_BE_INFERRED}; use hugr_core::hugr::views::sibling::SiblingMut; use hugr_core::ops::constant::Value; use hugr_core::ops::handle::CfgID; @@ -223,10 +223,7 @@ mod test { let e = extension(); let tst_op = e.instantiate_extension_op("Test", [], &PRELUDE_REGISTRY)?; let reg = ExtensionRegistry::try_new([PRELUDE.to_owned(), e])?; - let mut h = CFGBuilder::new( - FunctionType::new(loop_variants.clone(), exit_types.clone()) - .with_extension_delta(ExtensionSet::singleton(&PRELUDE_ID)), - )?; + let mut h = CFGBuilder::new(ft2(loop_variants.clone(), exit_types.clone()))?; let mut no_b1 = h.simple_entry_builder(loop_variants.clone(), 1, PRELUDE_ID.into())?; let n = no_b1.add_dataflow_op(Noop::new(QB_T), no_b1.input_wires())?; let br = lifted_unary_unit_sum(&mut no_b1); @@ -234,7 +231,7 @@ mod test { let mut test_block = h.block_builder( loop_variants.clone(), vec![loop_variants.clone(), exit_types], - PRELUDE_ID.into(), + TO_BE_INFERRED.into(), // TODO infer by default type_row![], )?; let [test_input] = test_block.input_wires_arr(); @@ -247,7 +244,7 @@ mod test { no_b1 } else { let mut no_b2 = h.simple_block_builder( - FunctionType::new_endo(loop_variants).with_extension_delta(PRELUDE_ID), + FunctionType::new_endo(loop_variants).with_extension_delta(TO_BE_INFERRED), // TODO infer by default 1, )?; let n = no_b2.add_dataflow_op(Noop::new(QB_T), no_b2.input_wires())?; @@ -326,13 +323,12 @@ mod test { .try_into() .unwrap(); let mut h = CFGBuilder::new( - FunctionType::new(QB_T, res_t.clone()) - .with_extension_delta(ExtensionSet::singleton(&PRELUDE_ID)), + ft2(QB_T, res_t.clone()) )?; let mut bb1 = h.entry_builder( vec![type_row![]], type_row![USIZE_T, QB_T], - ExtensionSet::singleton(&PRELUDE_ID), + TO_BE_INFERRED.into(), // TODO by default )?; let [inw] = bb1.input_wires_arr(); let load_cst = bb1.add_load_value(ConstUsize::new(1)); @@ -342,7 +338,7 @@ mod test { let mut bb2 = h.block_builder( type_row![USIZE_T, QB_T], vec![type_row![]], - PRELUDE_ID.into(), + TO_BE_INFERRED.into(), type_row![QB_T, USIZE_T], )?; let [u, q] = bb2.input_wires_arr(); @@ -352,7 +348,7 @@ mod test { let mut bb3 = h.block_builder( type_row![QB_T, USIZE_T], vec![type_row![]], - PRELUDE_ID.into(), + TO_BE_INFERRED.into(), res_t.clone().into(), )?; let [q, u] = bb3.input_wires_arr(); From eca016db3678668f5e6b25f2e32256a38eb3760e Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:38:27 +0100 Subject: [PATCH 12/22] doctests --- hugr-core/src/builder.rs | 7 ++++--- hugr/src/lib.rs | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/hugr-core/src/builder.rs b/hugr-core/src/builder.rs index 317e37620..33f890ffa 100644 --- a/hugr-core/src/builder.rs +++ b/hugr-core/src/builder.rs @@ -29,7 +29,7 @@ //! # use hugr::Hugr; //! # use hugr::builder::{BuildError, BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr, ModuleBuilder, DataflowSubContainer, HugrBuilder}; //! use hugr::extension::prelude::BOOL_T; -//! use hugr::std_extensions::logic::{NotOp, LOGIC_REG}; +//! use hugr::std_extensions::logic::{EXTENSION_ID, LOGIC_REG, NotOp}; //! use hugr::types::FunctionType; //! //! # fn doctest() -> Result<(), BuildError> { @@ -42,7 +42,7 @@ //! let _dfg_handle = { //! let mut dfg = module_builder.define_function( //! "main", -//! FunctionType::new(vec![BOOL_T], vec![BOOL_T]), +//! FunctionType::new_endo(BOOL_T).with_extension_delta(EXTENSION_ID), //! )?; //! //! // Get the wires from the function inputs. @@ -59,7 +59,8 @@ //! let _circuit_handle = { //! let mut dfg = module_builder.define_function( //! "circuit", -//! FunctionType::new_endo(vec![BOOL_T, BOOL_T]), +//! FunctionType::new_endo(vec![BOOL_T, BOOL_T]) +//! .with_extension_delta(EXTENSION_ID), //! )?; //! let mut circuit = dfg.as_circuit(dfg.input_wires()); //! diff --git a/hugr/src/lib.rs b/hugr/src/lib.rs index 7e2562cf3..95c6a3d6b 100644 --- a/hugr/src/lib.rs +++ b/hugr/src/lib.rs @@ -27,7 +27,7 @@ //! To build a HUGR for a simple quantum circuit and then serialize it to a buffer, we can define //! a simple quantum extension and then use the [[builder::DFGBuilder]] as follows: //! ``` -//! use hugr::builder::{BuildError, DFGBuilder, Dataflow, DataflowHugr}; +//! use hugr::builder::{BuildError, DFGBuilder, Dataflow, DataflowHugr, ft2}; //! use hugr::extension::prelude::{BOOL_T, QB_T}; //! use hugr::hugr::Hugr; //! use hugr::type_row; @@ -115,7 +115,7 @@ //! // └───┘└───┘└╥┘ //! // c: ╚═ //! fn make_dfg_hugr() -> Result { -//! let mut dfg_builder = DFGBuilder::new(FunctionType::new( +//! let mut dfg_builder = DFGBuilder::new(ft2( //! type_row![QB_T, QB_T], //! type_row![QB_T, QB_T, BOOL_T], //! ))?; From abbfa2a1120ecb3df4dbc4836c808f2338ba9cf8 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 15:24:08 +0100 Subject: [PATCH 13/22] merge_bbs use simple_entry_builder --- hugr-passes/src/merge_bbs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hugr-passes/src/merge_bbs.rs b/hugr-passes/src/merge_bbs.rs index cc0e0bf20..ba0f4fd97 100644 --- a/hugr-passes/src/merge_bbs.rs +++ b/hugr-passes/src/merge_bbs.rs @@ -325,9 +325,9 @@ mod test { let mut h = CFGBuilder::new( ft2(QB_T, res_t.clone()) )?; - let mut bb1 = h.entry_builder( - vec![type_row![]], + let mut bb1 = h.simple_entry_builder( type_row![USIZE_T, QB_T], + 1, TO_BE_INFERRED.into(), // TODO by default )?; let [inw] = bb1.input_wires_arr(); From 3994ee9cd36b53a6c3c251cc6085b6775c5da3af Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 18:49:47 +0100 Subject: [PATCH 14/22] (simple_)block_builder variants, fix tests --- hugr-core/src/builder/cfg.rs | 112 +++++++++++++++++----- hugr-core/src/hugr/rewrite/outline_cfg.rs | 4 +- hugr-core/src/hugr/rewrite/replace.rs | 2 +- hugr-core/src/hugr/validate/test.rs | 4 +- hugr-passes/src/merge_bbs.rs | 24 ++--- hugr-passes/src/nest_cfgs.rs | 53 +++------- 6 files changed, 112 insertions(+), 87 deletions(-) diff --git a/hugr-core/src/builder/cfg.rs b/hugr-core/src/builder/cfg.rs index 8068748da..d37f78360 100644 --- a/hugr-core/src/builder/cfg.rs +++ b/hugr-core/src/builder/cfg.rs @@ -5,7 +5,10 @@ use super::{ BasicBlockID, BuildError, CfgID, Container, Dataflow, HugrBuilder, Wire, }; -use crate::ops::{self, handle::NodeHandle, DataflowBlock, DataflowParent, ExitBlock, OpType}; +use crate::{ + extension::TO_BE_INFERRED, + ops::{self, handle::NodeHandle, DataflowBlock, DataflowParent, ExitBlock, OpType}, +}; use crate::{ extension::{ExtensionRegistry, ExtensionSet}, types::FunctionType, @@ -43,7 +46,7 @@ use crate::{hugr::HugrMut, type_row, Hugr}; /// +------------+ /// */ /// use hugr::{ -/// builder::{BuildError, CFGBuilder, Container, Dataflow, HugrBuilder}, +/// builder::{BuildError, CFGBuilder, Container, Dataflow, HugrBuilder, ft1, ft2}, /// extension::{prelude, ExtensionSet}, /// ops, type_row, /// types::{FunctionType, SumType, Type}, @@ -62,8 +65,7 @@ use crate::{hugr::HugrMut, type_row, Hugr}; /// /// // The second argument says what types will be passed through to every /// // successor, in addition to the appropriate `sum_variants` type. -/// let mut entry_b = -/// cfg_builder.entry_builder(sum_variants.clone(), type_row![NAT], ExtensionSet::new())?; +/// let mut entry_b = cfg_builder.entry_builder(sum_variants.clone(), type_row![NAT])?; /// /// let [inw] = entry_b.input_wires_arr(); /// let entry = { @@ -82,7 +84,7 @@ use crate::{hugr::HugrMut, type_row, Hugr}; /// // `NAT` arguments: one from the `sum_variants` type, and another from the /// // entry node's `other_outputs`. /// let mut successor_builder = cfg_builder.simple_block_builder( -/// FunctionType::new(type_row![NAT, NAT], type_row![NAT]), +/// ft2(type_row![NAT, NAT], NAT), /// 1, // only one successor to this block /// )?; /// let successor_a = { @@ -96,8 +98,7 @@ use crate::{hugr::HugrMut, type_row, Hugr}; /// }; /// /// // The only argument to this block is the entry node's `other_outputs`. -/// let mut successor_builder = cfg_builder -/// .simple_block_builder(FunctionType::new(type_row![NAT], type_row![NAT]), 1)?; +/// let mut successor_builder = cfg_builder.simple_block_builder(ft1(NAT), 1)?; /// let successor_b = { /// let sum_unary = successor_builder.add_load_value(ops::Value::unary_unit_sum()); /// let [in_wire] = successor_builder.input_wires_arr(); @@ -197,7 +198,7 @@ impl + AsRef> CFGBuilder { /// Return a builder for a non-entry [`DataflowBlock`] child graph with `inputs` /// and `outputs` and the variants of the branching Sum value - /// specified by `sum_rows`. + /// specified by `sum_rows`. Extension delta will be inferred. /// /// # Errors /// @@ -206,18 +207,40 @@ impl + AsRef> CFGBuilder { &mut self, inputs: TypeRow, sum_rows: impl IntoIterator, - extension_delta: ExtensionSet, other_outputs: TypeRow, ) -> Result, BuildError> { - self.any_block_builder(inputs, sum_rows, other_outputs, extension_delta, false) + self.block_builder_exts(inputs, sum_rows, TO_BE_INFERRED, other_outputs) } - fn any_block_builder( + /// Return a builder for a non-entry [`DataflowBlock`] child graph with `inputs` + /// and `outputs` and the variants of the branching Sum value + /// specified by `sum_rows`. Extension delta will be inferred. + /// + /// # Errors + /// + /// This function will return an error if there is an error adding the node. + pub fn block_builder_exts( &mut self, inputs: TypeRow, sum_rows: impl IntoIterator, + extension_delta: impl Into, other_outputs: TypeRow, + ) -> Result, BuildError> { + self.any_block_builder( + inputs, + extension_delta.into(), + sum_rows, + other_outputs, + false, + ) + } + + fn any_block_builder( + &mut self, + inputs: TypeRow, extension_delta: ExtensionSet, + sum_rows: impl IntoIterator, + other_outputs: TypeRow, entry: bool, ) -> Result, BuildError> { let sum_rows: Vec<_> = sum_rows.into_iter().collect(); @@ -241,7 +264,8 @@ impl + AsRef> CFGBuilder { } /// Return a builder for a non-entry [`DataflowBlock`] child graph with `inputs` - /// and `outputs` and a UnitSum type: a Sum of `n_cases` unit types. + /// and `outputs` and `extension_delta` explicitly specified, plus a UnitSum type + /// (a Sum of `n_cases` unit types) to select the successor. /// /// # Errors /// @@ -251,7 +275,7 @@ impl + AsRef> CFGBuilder { signature: FunctionType, n_cases: usize, ) -> Result, BuildError> { - self.block_builder( + self.block_builder_exts( signature.input, vec![type_row![]; n_cases], signature.extension_reqs, @@ -259,6 +283,21 @@ impl + AsRef> CFGBuilder { ) } + /// Return a builder for the entry [`DataflowBlock`] child graph with + /// `outputs` and the variants of the branching Sum value + /// specified by `sum_rows`. + /// + /// # Errors + /// + /// This function will return an error if an entry block has already been built. + pub fn entry_builder( + &mut self, + sum_rows: impl IntoIterator, + other_outputs: TypeRow, + ) -> Result, BuildError> { + self.entry_builder_exts(TO_BE_INFERRED, sum_rows, other_outputs) + } + /// Return a builder for the entry [`DataflowBlock`] child graph with `inputs` /// and `outputs` and the variants of the branching Sum value /// specified by `sum_rows`. @@ -266,17 +305,23 @@ impl + AsRef> CFGBuilder { /// # Errors /// /// This function will return an error if an entry block has already been built. - pub fn entry_builder( + pub fn entry_builder_exts( &mut self, + extension_delta: impl Into, sum_rows: impl IntoIterator, other_outputs: TypeRow, - extension_delta: ExtensionSet, ) -> Result, BuildError> { let inputs = self .inputs .take() .ok_or(BuildError::EntryBuiltError(self.cfg_node))?; - self.any_block_builder(inputs, sum_rows, other_outputs, extension_delta, true) + self.any_block_builder( + inputs, + extension_delta.into(), + sum_rows, + other_outputs, + true, + ) } /// Return a builder for the entry [`DataflowBlock`] child graph with `inputs` @@ -289,9 +334,23 @@ impl + AsRef> CFGBuilder { &mut self, outputs: TypeRow, n_cases: usize, - extension_delta: ExtensionSet, ) -> Result, BuildError> { - self.entry_builder(vec![type_row![]; n_cases], outputs, extension_delta) + self.entry_builder(vec![type_row![]; n_cases], outputs) + } + + /// Return a builder for the entry [`DataflowBlock`] child graph with `inputs` + /// and `outputs` and a UnitSum type: a Sum of `n_cases` unit types. + /// + /// # Errors + /// + /// This function will return an error if there is an error adding the node. + pub fn simple_entry_builder_exts( + &mut self, + outputs: TypeRow, + n_cases: usize, + extension_delta: impl Into, + ) -> Result, BuildError> { + self.entry_builder_exts(extension_delta, vec![type_row![]; n_cases], outputs) } /// Returns the exit block of this [`CFGBuilder`]. @@ -439,8 +498,11 @@ pub(crate) mod test { cfg_builder: &mut CFGBuilder, ) -> Result<(), BuildError> { let sum2_variants = vec![type_row![NAT], type_row![NAT]]; - let mut entry_b = - cfg_builder.entry_builder(sum2_variants.clone(), type_row![], ExtensionSet::new())?; + let mut entry_b = cfg_builder.entry_builder_exts( + ExtensionSet::new(), + sum2_variants.clone(), + type_row![], + )?; let entry = { let [inw] = entry_b.input_wires_arr(); @@ -466,8 +528,11 @@ pub(crate) mod test { let sum_tuple_const = cfg_builder.add_constant(ops::Value::unary_unit_sum()); let sum_variants = vec![type_row![]]; - let mut entry_b = - cfg_builder.entry_builder(sum_variants.clone(), type_row![], ExtensionSet::new())?; + let mut entry_b = cfg_builder.entry_builder_exts( + ExtensionSet::new(), + sum_variants.clone(), + type_row![], + )?; let [inw] = entry_b.input_wires_arr(); let entry = { let sum = entry_b.load_const(&sum_tuple_const); @@ -501,8 +566,7 @@ pub(crate) mod test { middle_b.finish_with_outputs(c, [inw])? }; - let mut entry_b = - cfg_builder.entry_builder(sum_variants.clone(), type_row![NAT], ExtensionSet::new())?; + let mut entry_b = cfg_builder.entry_builder(sum_variants.clone(), type_row![NAT])?; let entry = { let sum = entry_b.load_const(&sum_tuple_const); // entry block uses wire from middle block even though middle block diff --git a/hugr-core/src/hugr/rewrite/outline_cfg.rs b/hugr-core/src/hugr/rewrite/outline_cfg.rs index 0f206a287..1d2ce2ee5 100644 --- a/hugr-core/src/hugr/rewrite/outline_cfg.rs +++ b/hugr-core/src/hugr/rewrite/outline_cfg.rs @@ -252,7 +252,7 @@ mod test { HugrBuilder, ModuleBuilder, }; use crate::extension::prelude::USIZE_T; - use crate::extension::{ExtensionSet, PRELUDE_REGISTRY}; + use crate::extension::PRELUDE_REGISTRY; use crate::hugr::views::sibling::SiblingMut; use crate::hugr::HugrMut; use crate::ops::constant::Value; @@ -295,7 +295,7 @@ mod test { }; let entry = n_identity( - cfg_builder.simple_entry_builder(USIZE_T.into(), 2, ExtensionSet::new())?, + cfg_builder.simple_entry_builder(USIZE_T.into(), 2)?, &pred_const, )?; diff --git a/hugr-core/src/hugr/rewrite/replace.rs b/hugr-core/src/hugr/rewrite/replace.rs index 09467720f..fe40bd0cb 100644 --- a/hugr-core/src/hugr/rewrite/replace.rs +++ b/hugr-core/src/hugr/rewrite/replace.rs @@ -628,7 +628,7 @@ mod test { }, op_sig.input() ); - h.simple_entry_builder(op_sig.output, 1, op_sig.extension_reqs.clone())? + h.simple_entry_builder_exts(op_sig.output, 1, op_sig.extension_reqs.clone())? } else { h.simple_block_builder(op_sig, 1)? }; diff --git a/hugr-core/src/hugr/validate/test.rs b/hugr-core/src/hugr/validate/test.rs index ffe7d4be2..3e65454b3 100644 --- a/hugr-core/src/hugr/validate/test.rs +++ b/hugr-core/src/hugr/validate/test.rs @@ -973,7 +973,7 @@ fn cfg_connections() -> Result<(), Box> { let mut hugr = CFGBuilder::new(FunctionType::new_endo(USIZE_T))?; let unary_pred = hugr.add_constant(Value::unary_unit_sum()); - let mut entry = hugr.simple_entry_builder(type_row![USIZE_T], 1, ExtensionSet::new())?; + let mut entry = hugr.simple_entry_builder_exts(type_row![USIZE_T], 1, ExtensionSet::new())?; let p = entry.load_const(&unary_pred); let ins = entry.input_wires(); let entry = entry.finish_with_outputs(p, ins)?; @@ -1095,7 +1095,7 @@ mod extension_tests { let mut cfg = CFGBuilder::new( FunctionType::new_endo(USIZE_T).with_extension_delta(parent_extensions.clone()), )?; - let mut bb = cfg.simple_entry_builder(USIZE_T.into(), 1, XB.into())?; + let mut bb = cfg.simple_entry_builder_exts(USIZE_T.into(), 1, XB)?; let pred = bb.add_load_value(Value::unary_unit_sum()); let inputs = bb.input_wires(); let blk = bb.finish_with_outputs(pred, inputs)?; diff --git a/hugr-passes/src/merge_bbs.rs b/hugr-passes/src/merge_bbs.rs index ba0f4fd97..e9b715f80 100644 --- a/hugr-passes/src/merge_bbs.rs +++ b/hugr-passes/src/merge_bbs.rs @@ -161,9 +161,9 @@ mod test { use itertools::Itertools; use rstest::rstest; - use hugr_core::builder::{ft2, CFGBuilder, DFGWrapper, Dataflow, HugrBuilder}; + use hugr_core::builder::{ft1, ft2, CFGBuilder, DFGWrapper, Dataflow, HugrBuilder}; use hugr_core::extension::prelude::{ConstUsize, PRELUDE_ID, QB_T, USIZE_T}; - use hugr_core::extension::{ExtensionRegistry, PRELUDE, PRELUDE_REGISTRY, TO_BE_INFERRED}; + use hugr_core::extension::{ExtensionRegistry, PRELUDE, PRELUDE_REGISTRY}; use hugr_core::hugr::views::sibling::SiblingMut; use hugr_core::ops::constant::Value; use hugr_core::ops::handle::CfgID; @@ -224,14 +224,13 @@ mod test { let tst_op = e.instantiate_extension_op("Test", [], &PRELUDE_REGISTRY)?; let reg = ExtensionRegistry::try_new([PRELUDE.to_owned(), e])?; let mut h = CFGBuilder::new(ft2(loop_variants.clone(), exit_types.clone()))?; - let mut no_b1 = h.simple_entry_builder(loop_variants.clone(), 1, PRELUDE_ID.into())?; + let mut no_b1 = h.simple_entry_builder_exts(loop_variants.clone(), 1, PRELUDE_ID)?; let n = no_b1.add_dataflow_op(Noop::new(QB_T), no_b1.input_wires())?; let br = lifted_unary_unit_sum(&mut no_b1); let no_b1 = no_b1.finish_with_outputs(br, n.outputs())?; let mut test_block = h.block_builder( loop_variants.clone(), vec![loop_variants.clone(), exit_types], - TO_BE_INFERRED.into(), // TODO infer by default type_row![], )?; let [test_input] = test_block.input_wires_arr(); @@ -243,10 +242,7 @@ mod test { let loop_backedge_target = if self_loop { no_b1 } else { - let mut no_b2 = h.simple_block_builder( - FunctionType::new_endo(loop_variants).with_extension_delta(TO_BE_INFERRED), // TODO infer by default - 1, - )?; + let mut no_b2 = h.simple_block_builder(ft1(loop_variants), 1)?; let n = no_b2.add_dataflow_op(Noop::new(QB_T), no_b2.input_wires())?; let br = lifted_unary_unit_sum(&mut no_b2); let nid = no_b2.finish_with_outputs(br, n.outputs())?; @@ -322,14 +318,8 @@ mod test { .into_owned() .try_into() .unwrap(); - let mut h = CFGBuilder::new( - ft2(QB_T, res_t.clone()) - )?; - let mut bb1 = h.simple_entry_builder( - type_row![USIZE_T, QB_T], - 1, - TO_BE_INFERRED.into(), // TODO by default - )?; + let mut h = CFGBuilder::new(ft2(QB_T, res_t.clone()))?; + let mut bb1 = h.simple_entry_builder(type_row![USIZE_T, QB_T], 1)?; let [inw] = bb1.input_wires_arr(); let load_cst = bb1.add_load_value(ConstUsize::new(1)); let pred = lifted_unary_unit_sum(&mut bb1); @@ -338,7 +328,6 @@ mod test { let mut bb2 = h.block_builder( type_row![USIZE_T, QB_T], vec![type_row![]], - TO_BE_INFERRED.into(), type_row![QB_T, USIZE_T], )?; let [u, q] = bb2.input_wires_arr(); @@ -348,7 +337,6 @@ mod test { let mut bb3 = h.block_builder( type_row![QB_T, USIZE_T], vec![type_row![]], - TO_BE_INFERRED.into(), res_t.clone().into(), )?; let [q, u] = bb3.input_wires_arr(); diff --git a/hugr-passes/src/nest_cfgs.rs b/hugr-passes/src/nest_cfgs.rs index 8753464d0..8e13281fb 100644 --- a/hugr-passes/src/nest_cfgs.rs +++ b/hugr-passes/src/nest_cfgs.rs @@ -575,7 +575,7 @@ impl EdgeClassifier { pub(crate) mod test { use super::*; use hugr_core::builder::{ - BuildError, CFGBuilder, Container, DataflowSubContainer, HugrBuilder, + ft1, BuildError, CFGBuilder, Container, DataflowSubContainer, HugrBuilder, }; use hugr_core::extension::PRELUDE_REGISTRY; use hugr_core::extension::{prelude::USIZE_T, ExtensionSet}; @@ -614,19 +614,13 @@ pub(crate) mod test { let const_unit = cfg_builder.add_constant(Value::unary_unit_sum()); let entry = n_identity( - cfg_builder.simple_entry_builder(type_row![NAT], 1, ExtensionSet::new())?, + cfg_builder.simple_entry_builder_exts(type_row![NAT], 1, ExtensionSet::new())?, &const_unit, )?; let (split, merge) = build_if_then_else_merge(&mut cfg_builder, &pred_const, &const_unit)?; cfg_builder.branch(&entry, 0, &split)?; - let head = n_identity( - cfg_builder.simple_block_builder(FunctionType::new_endo(NAT), 1)?, - &const_unit, - )?; - let tail = n_identity( - cfg_builder.simple_block_builder(FunctionType::new_endo(NAT), 2)?, - &pred_const, - )?; + let head = n_identity(cfg_builder.simple_block_builder(ft1(NAT), 1)?, &const_unit)?; + let tail = n_identity(cfg_builder.simple_block_builder(ft1(NAT), 2)?, &pred_const)?; cfg_builder.branch(&tail, 1, &head)?; cfg_builder.branch(&head, 0, &tail)?; // trivial "loop body" cfg_builder.branch(&merge, 0, &head)?; @@ -851,10 +845,7 @@ pub(crate) mod test { const_pred: &ConstID, unit_const: &ConstID, ) -> Result<(BasicBlockID, BasicBlockID), BuildError> { - let split = n_identity( - cfg.simple_block_builder(FunctionType::new_endo(NAT), 2)?, - const_pred, - )?; + let split = n_identity(cfg.simple_block_builder(ft1(NAT), 2)?, const_pred)?; let merge = build_then_else_merge_from_if(cfg, unit_const, split)?; Ok((split, merge)) } @@ -864,18 +855,9 @@ pub(crate) mod test { unit_const: &ConstID, split: BasicBlockID, ) -> Result { - let merge = n_identity( - cfg.simple_block_builder(FunctionType::new_endo(NAT), 1)?, - unit_const, - )?; - let left = n_identity( - cfg.simple_block_builder(FunctionType::new_endo(NAT), 1)?, - unit_const, - )?; - let right = n_identity( - cfg.simple_block_builder(FunctionType::new_endo(NAT), 1)?, - unit_const, - )?; + let merge = n_identity(cfg.simple_block_builder(ft1(NAT), 1)?, unit_const)?; + let left = n_identity(cfg.simple_block_builder(ft1(NAT), 1)?, unit_const)?; + let right = n_identity(cfg.simple_block_builder(ft1(NAT), 1)?, unit_const)?; cfg.branch(&split, 0, &left)?; cfg.branch(&split, 1, &right)?; cfg.branch(&left, 0, &merge)?; @@ -893,15 +875,12 @@ pub(crate) mod test { let const_unit = cfg_builder.add_constant(Value::unary_unit_sum()); let entry = n_identity( - cfg_builder.simple_entry_builder(type_row![NAT], 2, ExtensionSet::new())?, + cfg_builder.simple_entry_builder(type_row![NAT], 2)?, &pred_const, )?; let merge = build_then_else_merge_from_if(&mut cfg_builder, &const_unit, entry)?; // The merge block is also the loop header (so it merges three incoming control-flow edges) - let tail = n_identity( - cfg_builder.simple_block_builder(FunctionType::new_endo(NAT), 2)?, - &pred_const, - )?; + let tail = n_identity(cfg_builder.simple_block_builder(ft1(NAT), 2)?, &pred_const)?; cfg_builder.branch(&tail, 1, &merge)?; cfg_builder.branch(&merge, 0, &tail)?; // trivial "loop body" let exit = cfg_builder.exit_block(); @@ -929,26 +908,20 @@ pub(crate) mod test { let const_unit = cfg_builder.add_constant(Value::unary_unit_sum()); let entry = n_identity( - cfg_builder.simple_entry_builder(type_row![NAT], 1, ExtensionSet::new())?, + cfg_builder.simple_entry_builder(type_row![NAT], 1)?, &const_unit, )?; let (split, merge) = build_if_then_else_merge(cfg_builder, &pred_const, &const_unit)?; let head = if separate_headers { - let head = n_identity( - cfg_builder.simple_block_builder(FunctionType::new_endo(NAT), 1)?, - &const_unit, - )?; + let head = n_identity(cfg_builder.simple_block_builder(ft1(NAT), 1)?, &const_unit)?; cfg_builder.branch(&head, 0, &split)?; head } else { // Combine loop header with split. split }; - let tail = n_identity( - cfg_builder.simple_block_builder(FunctionType::new_endo(NAT), 2)?, - &pred_const, - )?; + let tail = n_identity(cfg_builder.simple_block_builder(ft1(NAT), 2)?, &pred_const)?; cfg_builder.branch(&tail, 1, &head)?; cfg_builder.branch(&merge, 0, &tail)?; From 0da41d8701aff7f423ff806dd09713ca85274cb1 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 19:09:06 +0100 Subject: [PATCH 15/22] Conditional builder (_exts) --- hugr-core/src/builder/build_traits.rs | 27 +++++++++++++++++++++--- hugr-core/src/builder/conditional.rs | 1 - hugr-core/src/builder/tail_loop.rs | 1 - hugr-core/src/hugr/rewrite/inline_dfg.rs | 4 ++-- hugr-core/src/hugr/rewrite/replace.rs | 1 - hugr-core/src/hugr/validate/test.rs | 1 - 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/hugr-core/src/builder/build_traits.rs b/hugr-core/src/builder/build_traits.rs index 7581cd0cf..6ea962473 100644 --- a/hugr-core/src/builder/build_traits.rs +++ b/hugr-core/src/builder/build_traits.rs @@ -437,6 +437,27 @@ pub trait Dataflow: Container { TailLoopBuilder::create_with_io(self.hugr_mut(), loop_node, &tail_loop) } + /// Return a builder for a [`crate::ops::Conditional`] node. + /// `sum_input` is a tuple of the type of the Sum + /// variants and the corresponding wire. + /// + /// The `other_inputs` must be an iterable over pairs of the type of the input and + /// the corresponding wire. + /// The `outputs` are the types of the outputs. Extension delta will be inferred. + /// + /// # Errors + /// + /// This function will return an error if there is an error when building + /// the Conditional node. + fn conditional_builder( + &mut self, + sum_input: (impl IntoIterator, Wire), + other_inputs: impl IntoIterator, + output_types: TypeRow, + ) -> Result, BuildError> { + self.conditional_builder_exts(sum_input, other_inputs, TO_BE_INFERRED, output_types) + } + /// Return a builder for a [`crate::ops::Conditional`] node. /// `sum_rows` and `sum_wire` define the type of the Sum /// variants and the wire carrying the Sum respectively. @@ -449,12 +470,12 @@ pub trait Dataflow: Container { /// /// This function will return an error if there is an error when building /// the Conditional node. - fn conditional_builder( + fn conditional_builder_exts( &mut self, (sum_rows, sum_wire): (impl IntoIterator, Wire), other_inputs: impl IntoIterator, + extension_delta: impl Into, output_types: TypeRow, - extension_delta: ExtensionSet, ) -> Result, BuildError> { let mut input_wires = vec![sum_wire]; let (input_types, rest_input_wires): (Vec, Vec) = @@ -471,7 +492,7 @@ pub trait Dataflow: Container { sum_rows, other_inputs: inputs, outputs: output_types, - extension_delta, + extension_delta: extension_delta.into(), }, input_wires, )?; diff --git a/hugr-core/src/builder/conditional.rs b/hugr-core/src/builder/conditional.rs index ab4716d66..bfdd859a5 100644 --- a/hugr-core/src/builder/conditional.rs +++ b/hugr-core/src/builder/conditional.rs @@ -245,7 +245,6 @@ mod test { ([type_row![], type_row![]], const_wire), other_inputs, outputs, - ExtensionSet::new(), )?; n_identity(conditional_b.case_builder(0)?)?; diff --git a/hugr-core/src/builder/tail_loop.rs b/hugr-core/src/builder/tail_loop.rs index 1d0983e24..9f0ad44cd 100644 --- a/hugr-core/src/builder/tail_loop.rs +++ b/hugr-core/src/builder/tail_loop.rs @@ -165,7 +165,6 @@ mod test { ([type_row![], type_row![]], const_wire), vec![(BIT, b1)], output_row, - PRELUDE_ID.into(), )?; let mut branch_0 = conditional_b.case_builder(0)?; diff --git a/hugr-core/src/hugr/rewrite/inline_dfg.rs b/hugr-core/src/hugr/rewrite/inline_dfg.rs index 35971dc2e..fa6c15560 100644 --- a/hugr-core/src/hugr/rewrite/inline_dfg.rs +++ b/hugr-core/src/hugr/rewrite/inline_dfg.rs @@ -352,11 +352,11 @@ mod test { .add_dataflow_op(test_quantum_extension::measure(), r.outputs())? .outputs_arr(); // Node using the boolean. Here we just select between two empty computations. - let mut if_n = inner.conditional_builder( + let mut if_n = inner.conditional_builder_exts( ([type_row![], type_row![]], b), [], - type_row![], ExtensionSet::new(), + type_row![], )?; if_n.case_builder(0)?.finish_with_outputs([])?; if_n.case_builder(1)?.finish_with_outputs([])?; diff --git a/hugr-core/src/hugr/rewrite/replace.rs b/hugr-core/src/hugr/rewrite/replace.rs index fe40bd0cb..10808bf14 100644 --- a/hugr-core/src/hugr/rewrite/replace.rs +++ b/hugr-core/src/hugr/rewrite/replace.rs @@ -665,7 +665,6 @@ mod test { (vec![type_row![]; 2], b), [(USIZE_T, i)], type_row![USIZE_T], - ExtensionSet::new(), )?; let mut case1 = cond.case_builder(0)?; let foo = case1.add_dataflow_op(mk_op("foo"), case1.input_wires())?; diff --git a/hugr-core/src/hugr/validate/test.rs b/hugr-core/src/hugr/validate/test.rs index 3e65454b3..6fc6e3a83 100644 --- a/hugr-core/src/hugr/validate/test.rs +++ b/hugr-core/src/hugr/validate/test.rs @@ -792,7 +792,6 @@ fn test_polymorphic_call() -> Result<(), Box> { (vec![type_row![USIZE_T; 2]], tup), vec![], type_row![USIZE_T;2], - TO_BE_INFERRED.into(), //TODO make default //es.clone().union(EXT_ID.into()), )?; let mut cc = c.case_builder(0)?; let [i1, i2] = cc.input_wires_arr(); From a238f215c7721aea97e80d7606cbe81ef69e7401 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 25 Jun 2024 19:09:58 +0100 Subject: [PATCH 16/22] benches --- hugr/benches/benchmarks/hugr.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hugr/benches/benchmarks/hugr.rs b/hugr/benches/benchmarks/hugr.rs index b01d72e25..eb7fe60de 100644 --- a/hugr/benches/benchmarks/hugr.rs +++ b/hugr/benches/benchmarks/hugr.rs @@ -3,7 +3,6 @@ use criterion::{black_box, criterion_group, AxisScale, Criterion, PlotConfiguration}; use hugr::builder::{BuildError, CFGBuilder, DFGBuilder, Dataflow, DataflowHugr, HugrBuilder}; use hugr::extension::prelude::{BOOL_T, USIZE_T}; -use hugr::extension::ExtensionSet; use hugr::types::FunctionType; use hugr::{type_row, Hugr}; @@ -18,8 +17,7 @@ pub fn simple_cfg_builder + AsRef>( cfg_builder: &mut CFGBuilder, ) -> Result<(), BuildError> { let sum2_variants = vec![type_row![USIZE_T], type_row![USIZE_T]]; - let mut entry_b = - cfg_builder.entry_builder(sum2_variants.clone(), type_row![], ExtensionSet::new())?; + let mut entry_b = cfg_builder.entry_builder(sum2_variants.clone(), type_row![])?; let entry = { let [inw] = entry_b.input_wires_arr(); From 79da2882eacf26d32c4a873b7fe4945e9bec7541 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Fri, 28 Jun 2024 10:49:48 +0100 Subject: [PATCH 17/22] clippy --- hugr-core/src/hugr/rewrite/replace.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hugr-core/src/hugr/rewrite/replace.rs b/hugr-core/src/hugr/rewrite/replace.rs index 10808bf14..af20bed44 100644 --- a/hugr-core/src/hugr/rewrite/replace.rs +++ b/hugr-core/src/hugr/rewrite/replace.rs @@ -450,9 +450,7 @@ mod test { DataflowSubContainer, HugrBuilder, SubContainer, }; use crate::extension::prelude::{BOOL_T, USIZE_T}; - use crate::extension::{ - ExtensionId, ExtensionRegistry, ExtensionSet, PRELUDE, PRELUDE_REGISTRY, - }; + use crate::extension::{ExtensionId, ExtensionRegistry, PRELUDE, PRELUDE_REGISTRY}; use crate::hugr::internal::HugrMutInternals; use crate::hugr::rewrite::replace::WhichHugr; use crate::hugr::{HugrMut, Rewrite}; From 1b5eebd02be4bd8905879c206984e492bc423584 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 15 Jul 2024 12:04:36 +0100 Subject: [PATCH 18/22] doc improvements --- hugr-core/src/builder/build_traits.rs | 1 + hugr-core/src/builder/cfg.rs | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/hugr-core/src/builder/build_traits.rs b/hugr-core/src/builder/build_traits.rs index 6ea962473..fa0c7b76e 100644 --- a/hugr-core/src/builder/build_traits.rs +++ b/hugr-core/src/builder/build_traits.rs @@ -465,6 +465,7 @@ pub trait Dataflow: Container { /// The `other_inputs` must be an iterable over pairs of the type of the input and /// the corresponding wire. /// The `outputs` are the types of the outputs. + /// `exts` is the extension delta, here explicit. [conditional_builder](Self::conditional_builder) may be used to infer. /// /// # Errors /// diff --git a/hugr-core/src/builder/cfg.rs b/hugr-core/src/builder/cfg.rs index c04175b5b..d0e7fda34 100644 --- a/hugr-core/src/builder/cfg.rs +++ b/hugr-core/src/builder/cfg.rs @@ -283,9 +283,9 @@ impl + AsRef> CFGBuilder { ) } - /// Return a builder for the entry [`DataflowBlock`] child graph with - /// `outputs` and the variants of the branching Sum value - /// specified by `sum_rows`. + /// Return a builder for the entry [`DataflowBlock`] child graph with `outputs` + /// and the variants of the branching Sum value specified by `sum_rows`. + /// Extension delta will be inferred. /// /// # Errors /// @@ -298,9 +298,10 @@ impl + AsRef> CFGBuilder { self.entry_builder_exts(TO_BE_INFERRED, sum_rows, other_outputs) } - /// Return a builder for the entry [`DataflowBlock`] child graph with `inputs` - /// and `outputs` and the variants of the branching Sum value - /// specified by `sum_rows`. + /// Return a builder for the entry [`DataflowBlock`] child graph with `outputs`, + /// the variants of the branching Sum value specified by `sum_rows`, and + /// `extension_delta` explicitly specified. ([entry_builder](Self::entry_builder) + /// may be used to infer.) /// /// # Errors /// @@ -324,8 +325,8 @@ impl + AsRef> CFGBuilder { ) } - /// Return a builder for the entry [`DataflowBlock`] child graph with `inputs` - /// and `outputs` and a UnitSum type: a Sum of `n_cases` unit types. + /// Return a builder for the entry [`DataflowBlock`] child graph with + /// `outputs` and a UnitSum type: a Sum of `n_cases` unit types. /// /// # Errors /// @@ -338,8 +339,9 @@ impl + AsRef> CFGBuilder { self.entry_builder(vec![type_row![]; n_cases], outputs) } - /// Return a builder for the entry [`DataflowBlock`] child graph with `inputs` - /// and `outputs` and a UnitSum type: a Sum of `n_cases` unit types. + /// Return a builder for the entry [`DataflowBlock`] child graph with + /// `outputs` and a Sum of `n_cases` unit types, and explicit `extension_delta`. + /// ([simple_entry_builder](Self::simple_entry_builder) may be used to infer.) /// /// # Errors /// From eb6d1d0793f7d4ecc821289e76f83d0979603978 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 15 Jul 2024 12:31:29 +0100 Subject: [PATCH 19/22] fix sibling_subgraph --- hugr-core/src/hugr/views/sibling_subgraph.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hugr-core/src/hugr/views/sibling_subgraph.rs b/hugr-core/src/hugr/views/sibling_subgraph.rs index 63e6adea0..07367e529 100644 --- a/hugr-core/src/hugr/views/sibling_subgraph.rs +++ b/hugr-core/src/hugr/views/sibling_subgraph.rs @@ -825,7 +825,9 @@ mod tests { let mut mod_builder = ModuleBuilder::new(); let func = mod_builder.declare( "test", - FunctionType::new(type_row![BOOL_T], type_row![BOOL_T, BOOL_T]).into(), + FunctionType::new(BOOL_T, type_row![BOOL_T, BOOL_T]) + .with_extension_delta(logic::EXTENSION_ID) + .into(), )?; let func_id = { let mut dfg = mod_builder.define_declaration(&func)?; From a574c6d0188dd61aed7d4406279c753c57943ea8 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 15 Jul 2024 12:35:36 +0100 Subject: [PATCH 20/22] force_order --- hugr-passes/src/force_order.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/hugr-passes/src/force_order.rs b/hugr-passes/src/force_order.rs index 4278b2879..2bef343ac 100644 --- a/hugr-passes/src/force_order.rs +++ b/hugr-passes/src/force_order.rs @@ -190,12 +190,11 @@ mod test { use std::collections::HashMap; use super::*; - use hugr_core::builder::{BuildHandle, Dataflow, DataflowHugr}; + use hugr_core::builder::{endo_ft, BuildHandle, Dataflow, DataflowHugr}; use hugr_core::ops::handle::{DataflowOpID, NodeHandle}; use hugr_core::std_extensions::arithmetic::int_ops::{self, IntOpDef}; use hugr_core::std_extensions::arithmetic::int_types::INT_TYPES; - use hugr_core::types::FunctionType; use hugr_core::{builder::DFGBuilder, hugr::Hugr}; use hugr_core::{HugrView, Wire}; @@ -231,8 +230,7 @@ mod test { /// Output fn test_hugr() -> (Hugr, [Node; 4]) { let t = INT_TYPES[I as usize].clone(); - let mut builder = - DFGBuilder::new(FunctionType::new_endo(vec![t.clone(), t.clone()])).unwrap(); + let mut builder = DFGBuilder::new(endo_ft(vec![t.clone(), t.clone()])).unwrap(); let [iw1, iw2] = builder.input_wires_arr(); let v0 = build_neg(&mut builder, iw1); let v1 = build_neg(&mut builder, iw2); From aaa1cf3ea243e241cc339695590fc03687215d13 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 15 Jul 2024 15:09:10 +0100 Subject: [PATCH 21/22] docs: correct output_types parameter name --- hugr-core/src/builder/build_traits.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hugr-core/src/builder/build_traits.rs b/hugr-core/src/builder/build_traits.rs index fa0c7b76e..f1ef3808f 100644 --- a/hugr-core/src/builder/build_traits.rs +++ b/hugr-core/src/builder/build_traits.rs @@ -443,7 +443,7 @@ pub trait Dataflow: Container { /// /// The `other_inputs` must be an iterable over pairs of the type of the input and /// the corresponding wire. - /// The `outputs` are the types of the outputs. Extension delta will be inferred. + /// The `output_types` are the types of the outputs. Extension delta will be inferred. /// /// # Errors /// @@ -464,8 +464,9 @@ pub trait Dataflow: Container { /// /// The `other_inputs` must be an iterable over pairs of the type of the input and /// the corresponding wire. - /// The `outputs` are the types of the outputs. - /// `exts` is the extension delta, here explicit. [conditional_builder](Self::conditional_builder) may be used to infer. + /// The `output_types` are the types of the outputs. + /// `exts` explicitly specifies the extension delta. Alternatively + /// [conditional_builder](Self::conditional_builder) may be used to infer it. /// /// # Errors /// From 2c56bf6dd22fe8ba2fccb311431066d161984ee8 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 15 Jul 2024 15:18:02 +0100 Subject: [PATCH 22/22] extension_delta last param everytime --- hugr-core/src/builder/build_traits.rs | 4 ++-- hugr-core/src/builder/cfg.rs | 16 ++++++++-------- hugr-core/src/hugr/rewrite/inline_dfg.rs | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/hugr-core/src/builder/build_traits.rs b/hugr-core/src/builder/build_traits.rs index f1ef3808f..3c72d0d43 100644 --- a/hugr-core/src/builder/build_traits.rs +++ b/hugr-core/src/builder/build_traits.rs @@ -455,7 +455,7 @@ pub trait Dataflow: Container { other_inputs: impl IntoIterator, output_types: TypeRow, ) -> Result, BuildError> { - self.conditional_builder_exts(sum_input, other_inputs, TO_BE_INFERRED, output_types) + self.conditional_builder_exts(sum_input, other_inputs, output_types, TO_BE_INFERRED) } /// Return a builder for a [`crate::ops::Conditional`] node. @@ -476,8 +476,8 @@ pub trait Dataflow: Container { &mut self, (sum_rows, sum_wire): (impl IntoIterator, Wire), other_inputs: impl IntoIterator, - extension_delta: impl Into, output_types: TypeRow, + extension_delta: impl Into, ) -> Result, BuildError> { let mut input_wires = vec![sum_wire]; let (input_types, rest_input_wires): (Vec, Vec) = diff --git a/hugr-core/src/builder/cfg.rs b/hugr-core/src/builder/cfg.rs index d0e7fda34..0d37992d0 100644 --- a/hugr-core/src/builder/cfg.rs +++ b/hugr-core/src/builder/cfg.rs @@ -209,7 +209,7 @@ impl + AsRef> CFGBuilder { sum_rows: impl IntoIterator, other_outputs: TypeRow, ) -> Result, BuildError> { - self.block_builder_exts(inputs, sum_rows, TO_BE_INFERRED, other_outputs) + self.block_builder_exts(inputs, sum_rows, other_outputs, TO_BE_INFERRED) } /// Return a builder for a non-entry [`DataflowBlock`] child graph with `inputs` @@ -223,8 +223,8 @@ impl + AsRef> CFGBuilder { &mut self, inputs: TypeRow, sum_rows: impl IntoIterator, - extension_delta: impl Into, other_outputs: TypeRow, + extension_delta: impl Into, ) -> Result, BuildError> { self.any_block_builder( inputs, @@ -278,8 +278,8 @@ impl + AsRef> CFGBuilder { self.block_builder_exts( signature.input, vec![type_row![]; n_cases], - signature.extension_reqs, signature.output, + signature.extension_reqs, ) } @@ -295,7 +295,7 @@ impl + AsRef> CFGBuilder { sum_rows: impl IntoIterator, other_outputs: TypeRow, ) -> Result, BuildError> { - self.entry_builder_exts(TO_BE_INFERRED, sum_rows, other_outputs) + self.entry_builder_exts(sum_rows, other_outputs, TO_BE_INFERRED) } /// Return a builder for the entry [`DataflowBlock`] child graph with `outputs`, @@ -308,9 +308,9 @@ impl + AsRef> CFGBuilder { /// This function will return an error if an entry block has already been built. pub fn entry_builder_exts( &mut self, - extension_delta: impl Into, sum_rows: impl IntoIterator, other_outputs: TypeRow, + extension_delta: impl Into, ) -> Result, BuildError> { let inputs = self .inputs @@ -352,7 +352,7 @@ impl + AsRef> CFGBuilder { n_cases: usize, extension_delta: impl Into, ) -> Result, BuildError> { - self.entry_builder_exts(extension_delta, vec![type_row![]; n_cases], outputs) + self.entry_builder_exts(vec![type_row![]; n_cases], outputs, extension_delta) } /// Returns the exit block of this [`CFGBuilder`]. @@ -501,9 +501,9 @@ pub(crate) mod test { ) -> Result<(), BuildError> { let sum2_variants = vec![type_row![NAT], type_row![NAT]]; let mut entry_b = cfg_builder.entry_builder_exts( - ExtensionSet::new(), sum2_variants.clone(), type_row![], + ExtensionSet::new(), )?; let entry = { let [inw] = entry_b.input_wires_arr(); @@ -531,9 +531,9 @@ pub(crate) mod test { let sum_variants = vec![type_row![]]; let mut entry_b = cfg_builder.entry_builder_exts( - ExtensionSet::new(), sum_variants.clone(), type_row![], + ExtensionSet::new(), )?; let [inw] = entry_b.input_wires_arr(); let entry = { diff --git a/hugr-core/src/hugr/rewrite/inline_dfg.rs b/hugr-core/src/hugr/rewrite/inline_dfg.rs index 63359e486..37f3a1cee 100644 --- a/hugr-core/src/hugr/rewrite/inline_dfg.rs +++ b/hugr-core/src/hugr/rewrite/inline_dfg.rs @@ -356,8 +356,8 @@ mod test { let mut if_n = inner.conditional_builder_exts( ([type_row![], type_row![]], b), [], - ExtensionSet::new(), type_row![], + ExtensionSet::new(), )?; if_n.case_builder(0)?.finish_with_outputs([])?; if_n.case_builder(1)?.finish_with_outputs([])?;