Skip to content

Commit 4c8a13c

Browse files
authored
fix: Inline constant functions in QSystemPass (#961)
Fixes #958 The `inline_constant_functions` pass is defined in hugr-llvm so we need to gate on the llvm feature :/
1 parent effc360 commit 4c8a13c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tket2-hseries/src/lib.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use extension::{
2121
qsystem::{LowerTk2Error, LowerTket2ToQSystemPass, QSystemOp},
2222
};
2323

24+
#[cfg(feature = "llvm")]
25+
use hugr::llvm::utils::inline_constant_functions;
26+
2427
#[cfg(feature = "cli")]
2528
pub mod cli;
2629
pub mod extension;
@@ -66,6 +69,9 @@ pub enum QSystemPassError<N = Node> {
6669
ConstantFoldError(ConstFoldError),
6770
/// An error from the component [LinearizeArrayPass] pass.
6871
LinearizeArrayError(ReplaceTypesError),
72+
#[cfg(feature = "llvm")]
73+
/// An error from the component [inline_constant_functions()] pass.
74+
InlineConstantFunctionsError(anyhow::Error),
6975
/// An error when running [RemoveDeadFuncsPass] after the monomorphisation
7076
/// pass.
7177
///
@@ -112,6 +118,12 @@ impl QSystemPass {
112118
self.replace_bools().run(hugr)?;
113119
}
114120
self.linearize_arrays().run(hugr)?;
121+
#[cfg(feature = "llvm")]
122+
{
123+
// TODO: Remove "llvm" feature gate once `inline_constant_functions` is moved to
124+
// `hugr-passes`. See https://github.com/CQCL/hugr/issues/2419
125+
inline_constant_functions(hugr)?;
126+
}
115127
if self.constant_fold {
116128
self.constant_fold().run(hugr)?;
117129
}
@@ -326,4 +338,49 @@ mod test {
326338
assert!(get_pos(call_node) < get_pos(n));
327339
}
328340
}
341+
342+
#[cfg(feature = "llvm")]
343+
#[test]
344+
fn const_function() {
345+
use hugr::builder::DataflowHugr;
346+
use hugr::builder::{DFGBuilder, ModuleBuilder};
347+
use hugr::ops::{CallIndirect, Value};
348+
349+
let qb_sig: Signature = Signature::new_endo(qb_t());
350+
let mut hugr = {
351+
let mut builder = ModuleBuilder::new();
352+
let val = Value::function({
353+
let builder = DFGBuilder::new(Signature::new_endo(qb_t())).unwrap();
354+
let [r] = builder.input_wires_arr();
355+
builder.finish_hugr_with_outputs([r]).unwrap()
356+
})
357+
.unwrap();
358+
let const_node = builder.add_constant(val);
359+
{
360+
let mut builder = builder.define_function("main", qb_sig.clone()).unwrap();
361+
let [i] = builder.input_wires_arr();
362+
let fun = builder.load_const(&const_node);
363+
let [r] = builder
364+
.add_dataflow_op(
365+
CallIndirect {
366+
signature: qb_sig.clone(),
367+
},
368+
[fun, i],
369+
)
370+
.unwrap()
371+
.outputs_arr();
372+
builder.finish_with_outputs([r]).unwrap();
373+
};
374+
builder.finish_hugr().unwrap()
375+
};
376+
377+
QSystemPass::default().run(&mut hugr).unwrap();
378+
379+
// QSystemPass should have removed the const function
380+
for n in hugr.descendants(hugr.module_root()) {
381+
if hugr.get_optype(n).as_const().is_some() {
382+
panic!("Const function is still there!");
383+
}
384+
}
385+
}
329386
}

0 commit comments

Comments
 (0)