|
| 1 | +/// Contains a pass to lower "drop" ops from the Guppy extension |
| 2 | +use hugr::algorithms::replace_types::{NodeTemplate, ReplaceTypesError, ReplacementOptions}; |
| 3 | +use hugr::algorithms::{ComposablePass, ReplaceTypes}; |
| 4 | +use hugr::builder::{Container, DFGBuilder}; |
| 5 | +use hugr::types::{Signature, Term}; |
| 6 | +use hugr::{hugr::hugrmut::HugrMut, Node}; |
| 7 | +use tket::extension::guppy::{DROP_OP_NAME, GUPPY_EXTENSION}; |
| 8 | + |
| 9 | +/// A pass that lowers "drop" ops from [GUPPY_EXTENSION] |
| 10 | +#[derive(Default, Debug, Clone)] |
| 11 | +pub struct LowerDropsPass; |
| 12 | + |
| 13 | +impl<H: HugrMut<Node = Node>> ComposablePass<H> for LowerDropsPass { |
| 14 | + type Error = ReplaceTypesError; |
| 15 | + |
| 16 | + /// Returns whether any drops were lowered |
| 17 | + type Result = bool; |
| 18 | + |
| 19 | + fn run(&self, hugr: &mut H) -> Result<Self::Result, Self::Error> { |
| 20 | + let mut rt = ReplaceTypes::default(); |
| 21 | + rt.replace_parametrized_op_with( |
| 22 | + GUPPY_EXTENSION.get_op(DROP_OP_NAME.as_str()).unwrap(), |
| 23 | + |targs| { |
| 24 | + let [Term::Runtime(ty)] = targs else { |
| 25 | + panic!("Expected just one type") |
| 26 | + }; |
| 27 | + // The Hugr here is invalid, so we have to pull it out manually |
| 28 | + let mut dfb = DFGBuilder::new(Signature::new(ty.clone(), vec![])).unwrap(); |
| 29 | + let h = std::mem::take(dfb.hugr_mut()); |
| 30 | + Some(NodeTemplate::CompoundOp(Box::new(h))) |
| 31 | + }, |
| 32 | + ReplacementOptions::default().with_linearization(true), |
| 33 | + ); |
| 34 | + rt.run(hugr) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod test { |
| 40 | + use std::sync::Arc; |
| 41 | + |
| 42 | + use hugr::builder::{inout_sig, Dataflow, DataflowHugr}; |
| 43 | + use hugr::ops::ExtensionOp; |
| 44 | + use hugr::{extension::prelude::usize_t, std_extensions::collections::array::array_type}; |
| 45 | + use hugr::{Hugr, HugrView}; |
| 46 | + |
| 47 | + use super::*; |
| 48 | + |
| 49 | + #[test] |
| 50 | + fn test_lower_drop() { |
| 51 | + let arr_type = array_type(2, usize_t()); |
| 52 | + let drop_op = GUPPY_EXTENSION.get_op(DROP_OP_NAME.as_str()).unwrap(); |
| 53 | + let drop_node = ExtensionOp::new(drop_op.clone(), [arr_type.clone().into()]).unwrap(); |
| 54 | + let mut b = DFGBuilder::new(inout_sig(arr_type, vec![])).unwrap(); |
| 55 | + let inp = b.input_wires(); |
| 56 | + b.add_dataflow_op(drop_node, inp).unwrap(); |
| 57 | + let mut h = b.finish_hugr_with_outputs([]).unwrap(); |
| 58 | + let count_drops = |h: &Hugr| { |
| 59 | + h.nodes() |
| 60 | + .filter(|n| { |
| 61 | + h.get_optype(*n) |
| 62 | + .as_extension_op() |
| 63 | + .is_some_and(|e| Arc::ptr_eq(e.def_arc(), drop_op)) |
| 64 | + }) |
| 65 | + .count() |
| 66 | + }; |
| 67 | + assert_eq!(count_drops(&h), 1); |
| 68 | + LowerDropsPass.run(&mut h).unwrap(); |
| 69 | + h.validate().unwrap(); |
| 70 | + assert_eq!(count_drops(&h), 0); |
| 71 | + } |
| 72 | +} |
0 commit comments