Skip to content

Commit a5a18e5

Browse files
committed
WIP Add link_name, builder methods, update some passes
dead_funcs.rs: add enum IncludeExports monomorphize.rs: bodge TODO
1 parent 187cc8b commit a5a18e5

File tree

18 files changed

+361
-204
lines changed

18 files changed

+361
-204
lines changed

hugr-core/src/builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ pub(crate) mod test {
295295

296296
#[fixture]
297297
pub(crate) fn simple_funcdef_hugr() -> Hugr {
298-
let fn_builder =
299-
FunctionBuilder::new("test", Signature::new(vec![bool_t()], vec![bool_t()])).unwrap();
298+
let fn_builder = FunctionBuilder::new("test", Signature::new_endo(bool_t())).unwrap();
300299
let [i1] = fn_builder.input_wires_arr();
301300
fn_builder.finish_hugr_with_outputs([i1]).unwrap()
302301
}

hugr-core/src/builder/build_traits.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ pub trait Container {
127127
}
128128
}
129129

130+
/*pub(super) fn define_function_link_name<C: Container + ?Sized>(
131+
ctr: &mut C,
132+
name: impl Into<String>,
133+
signature: impl Into<PolyFuncType>,
134+
link_name: impl Into<Option<String>>,
135+
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
136+
let signature = signature.into();
137+
let body = signature.body().clone();
138+
let f_node = ctr.add_child_node(ops::FuncDefn::new(name.into(), signature, link_name));
139+
140+
// Add the extensions used by the function types.
141+
ctr.use_extensions(
142+
body.used_extensions()
143+
.unwrap_or_else(|e| panic!("Build-time signatures should have valid extensions. {e}")),
144+
);
145+
146+
let db = DFGBuilder::create_with_io(ctr.hugr_mut(), f_node, body)?;
147+
Ok(FunctionBuilder::from_dfg_builder(db))
148+
}*/
149+
130150
/// Types implementing this trait can be used to build complete HUGRs
131151
/// (with varying entrypoint node types)
132152
pub trait HugrBuilder: Container {

hugr-core/src/builder/dataflow.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,46 @@ impl<B, T> DFGWrapper<B, T> {
152152
pub type FunctionBuilder<B> = DFGWrapper<B, BuildHandle<FuncID<true>>>;
153153

154154
impl FunctionBuilder<Hugr> {
155-
/// Initialize a builder for a `FuncDefn` rooted HUGR
155+
/// Initialize a builder for a [`FuncDefn`](ops::FuncDefn)-rooted HUGR; the function will be private.
156+
/// (See also [Self::new_pub], [Self::new_link_name].)
157+
///
156158
/// # Errors
157159
///
158160
/// Error in adding DFG child nodes.
159161
pub fn new(
160162
name: impl Into<String>,
161163
signature: impl Into<PolyFuncType>,
162164
) -> Result<Self, BuildError> {
163-
let signature: PolyFuncType = signature.into();
164-
let body = signature.body().clone();
165-
let op = ops::FuncDefn::new(name, signature);
165+
Self::new_link_name(name, signature, None)
166+
}
167+
168+
/// Initialize a builder for a FuncDefn-rooted HUGR; the function will be public
169+
/// with the same name (see also [Self::new_link_name]).
170+
///
171+
/// # Errors
172+
///
173+
/// Error in adding DFG child nodes.
174+
pub fn new_pub(
175+
name: impl Into<String>,
176+
signature: impl Into<PolyFuncType>,
177+
) -> Result<Self, BuildError> {
178+
let name = name.into();
179+
Self::new_link_name(name.clone(), signature, Some(name))
180+
}
181+
182+
/// Initialize a builder for a FuncDefn-rooted HUGR, with the specified
183+
/// [link_name](ops::FuncDefn::link_name).
184+
///
185+
/// # Errors
186+
///
187+
/// Error in adding DFG child nodes.
188+
pub fn new_link_name(
189+
name: impl Into<String>,
190+
signature: impl Into<PolyFuncType>,
191+
link_name: impl Into<Option<String>>,
192+
) -> Result<Self, BuildError> {
193+
let op = ops::FuncDefn::new_link_name(name.into(), signature.into(), link_name);
194+
let body = op.signature().body().clone();
166195

167196
let base = Hugr::new_with_entrypoint(op).expect("FuncDefn entrypoint should be valid");
168197
let root = base.entrypoint();

hugr-core/src/builder/module.rs

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ use crate::hugr::ValidationError;
88
use crate::hugr::internal::HugrMutInternals;
99
use crate::hugr::views::HugrView;
1010
use crate::ops;
11-
use crate::types::{PolyFuncType, Type, TypeBound};
12-
1311
use crate::ops::handle::{AliasID, FuncID, NodeHandle};
14-
12+
use crate::types::{PolyFuncType, Type, TypeBound};
1513
use crate::{Hugr, Node};
14+
1615
use smol_str::SmolStr;
1716

1817
/// Builder for a HUGR module.
@@ -51,7 +50,7 @@ impl HugrBuilder for ModuleBuilder<Hugr> {
5150
}
5251

5352
impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
54-
/// Replace a [`ops::FuncDecl`] with [`ops::FuncDefn`] and return a builder for
53+
/// Replace a [`ops::FuncDecl`] with public [`ops::FuncDefn`] and return a builder for
5554
/// the defining graph.
5655
///
5756
/// # Errors
@@ -63,19 +62,50 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
6362
f_id: &FuncID<false>,
6463
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
6564
let f_node = f_id.node();
66-
let decl =
67-
self.hugr()
68-
.get_optype(f_node)
69-
.as_func_decl()
70-
.ok_or(BuildError::UnexpectedType {
71-
node: f_node,
72-
op_desc: "crate::ops::OpType::FuncDecl",
73-
})?;
74-
let name = decl.func_name().clone();
75-
let sig = decl.signature().clone();
76-
let body = sig.body().clone();
77-
self.hugr_mut()
78-
.replace_op(f_node, ops::FuncDefn::new(name, sig));
65+
let opty = self.hugr_mut().optype_mut(f_node);
66+
let ops::OpType::FuncDecl(decl) = opty else {
67+
return Err(BuildError::UnexpectedType {
68+
node: f_node,
69+
op_desc: "crate::ops::OpType::FuncDecl",
70+
});
71+
};
72+
73+
let body = decl.signature().body().clone();
74+
// TODO look for `name_hint` metadata on the FuncDecl and copy to FuncDefn
75+
*opty = ops::FuncDefn::new_link_name(
76+
format!("From Decl {}", decl.link_name()),
77+
decl.signature().clone(),
78+
decl.link_name().clone(),
79+
)
80+
.into();
81+
82+
let db = DFGBuilder::create_with_io(self.hugr_mut(), f_node, body)?;
83+
Ok(FunctionBuilder::from_dfg_builder(db))
84+
}
85+
86+
/// Add a [`ops::FuncDefn`] node, with both `name` and `link_name` explicitly specified.
87+
/// Returns a builder to define the function body graph.
88+
///
89+
/// # Errors
90+
///
91+
/// This function will return an error if there is an error in adding the
92+
/// [`ops::FuncDefn`] node.
93+
pub fn define_function_link_name(
94+
&mut self,
95+
name: impl Into<String>,
96+
signature: impl Into<PolyFuncType>,
97+
link_name: impl Into<Option<String>>,
98+
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
99+
let signature: PolyFuncType = signature.into();
100+
let body = signature.body().clone();
101+
let f_node = self.add_child_node(ops::FuncDefn::new_link_name(name, signature, link_name));
102+
103+
// Add the extensions used by the function types.
104+
self.use_extensions(
105+
body.used_extensions().unwrap_or_else(|e| {
106+
panic!("Build-time signatures should have valid extensions. {e}")
107+
}),
108+
);
79109

80110
let db = DFGBuilder::create_with_io(self.hugr_mut(), f_node, body)?;
81111
Ok(FunctionBuilder::from_dfg_builder(db))
@@ -106,31 +136,36 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
106136
Ok(declare_n.into())
107137
}
108138

109-
/// Add a [`ops::FuncDefn`] node and returns a builder to define the function
139+
/// Adds a private [`ops::FuncDefn`] node and returns a builder to define the function
110140
/// body graph.
111141
///
112142
/// # Errors
113143
///
114144
/// This function will return an error if there is an error in adding the
115145
/// [`ops::FuncDefn`] node.
146+
// ALAN TODO? deprecate and rename to define_private_func(tion)? (Rather long...)
116147
pub fn define_function(
117148
&mut self,
118149
name: impl Into<String>,
119150
signature: impl Into<PolyFuncType>,
120151
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
121-
let signature: PolyFuncType = signature.into();
122-
let body = signature.body().clone();
123-
let f_node = self.add_child_node(ops::FuncDefn::new(name, signature));
124-
125-
// Add the extensions used by the function types.
126-
self.use_extensions(
127-
body.used_extensions().unwrap_or_else(|e| {
128-
panic!("Build-time signatures should have valid extensions. {e}")
129-
}),
130-
);
152+
self.define_function_link_name(name, signature, None)
153+
}
131154

132-
let db = DFGBuilder::create_with_io(self.hugr_mut(), f_node, body)?;
133-
Ok(FunctionBuilder::from_dfg_builder(db))
155+
/// Adds a public [`ops::FuncDefn`] node, with `link_name` the same as `name`,
156+
/// and returns a builder to define the function body graph.
157+
///
158+
/// # Errors
159+
///
160+
/// This function will return an error if there is an error in adding the
161+
/// [`ops::FuncDefn`] node.
162+
pub fn define_function_pub(
163+
&mut self,
164+
name: impl Into<String>,
165+
signature: impl Into<PolyFuncType>,
166+
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
167+
let name = name.into();
168+
self.define_function_link_name(name.clone(), signature, name)
134169
}
135170

136171
/// Add a [`crate::ops::OpType::AliasDefn`] node and return a handle to the Alias.
@@ -218,10 +253,7 @@ mod test {
218253

219254
let f_build = module_builder.define_function(
220255
"main",
221-
Signature::new(
222-
vec![qubit_state_type.get_alias_type()],
223-
vec![qubit_state_type.get_alias_type()],
224-
),
256+
Signature::new_endo(qubit_state_type.get_alias_type()),
225257
)?;
226258
n_identity(f_build)?;
227259
module_builder.finish_hugr()

hugr-core/src/hugr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ fn make_module_hugr(root_op: OpType, nodes: usize, ports: usize) -> Option<Hugr>
500500
let dataflow_inputs = signature.input_count();
501501
let dataflow_outputs = signature.output_count();
502502

503-
let func = hugr.add_node_with_parent(module, ops::FuncDefn::new("main", signature.clone()));
503+
let func =
504+
hugr.add_node_with_parent(module, ops::FuncDefn::new_public("main", signature.clone()));
504505
let inp = hugr.add_node_with_parent(
505506
func,
506507
ops::Input {

hugr-core/src/hugr/hugrmut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ mod test {
630630
// Start a main function with two nat inputs.
631631
let f: Node = hugr.add_node_with_parent(
632632
module,
633-
ops::FuncDefn::new(
633+
ops::FuncDefn::new_public(
634634
"main",
635635
Signature::new(usize_t(), vec![usize_t(), usize_t()]),
636636
),

hugr-core/src/hugr/serialize/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ fn roundtrip_polyfunctype_varlen(#[case] poly_func_type: PolyFuncTypeRV) {
506506

507507
#[rstest]
508508
#[case(ops::Module::new())]
509-
#[case(ops::FuncDefn::new("polyfunc1", polyfunctype1()))]
509+
#[case(ops::FuncDefn::new_private("polyfunc1", polyfunctype1()))]
510+
#[case(ops::FuncDefn::new_link_name("pubfunc1", polyfunctype1(), "func1linkname".to_string()))]
510511
#[case(ops::FuncDecl::new("polyfunc2", polyfunctype1()))]
511512
#[case(ops::AliasDefn { name: "aliasdefn".into(), definition: Type::new_unit_sum(4)})]
512513
#[case(ops::AliasDecl { name: "aliasdecl".into(), bound: TypeBound::Any})]

0 commit comments

Comments
 (0)