Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OutlineCfg rewrite returns new BB and CFG nodes #554

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/hugr/rewrite/outline_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ impl OutlineCfg {

impl Rewrite for OutlineCfg {
type Error = OutlineCfgError;
type ApplyResult = ();
/// The newly-created basic block, and the [CFG] node inside it
///
/// [CFG]: OpType::CFG
type ApplyResult = (Node, Node);

const UNCHANGED_ON_FAILURE: bool = true;
fn verify(&self, h: &impl HugrView) -> Result<(), OutlineCfgError> {
self.compute_entry_exit_outside_extensions(h)?;
Ok(())
}
fn apply(self, h: &mut impl HugrMut) -> Result<(), OutlineCfgError> {
fn apply(self, h: &mut impl HugrMut) -> Result<(Node, Node), OutlineCfgError> {
let (entry, exit, outside, extension_delta) =
self.compute_entry_exit_outside_extensions(h)?;
// 1. Compute signature
Expand Down Expand Up @@ -208,7 +211,7 @@ impl Rewrite for OutlineCfg {
.connect(exit, exit_port.index(), inner_exit, 0)
.unwrap();

Ok(())
Ok((new_block, cfg_node))
}
}

Expand Down Expand Up @@ -253,6 +256,7 @@ mod test {
use crate::hugr::views::sibling::SiblingMut;
use crate::hugr::HugrMut;
use crate::ops::handle::{BasicBlockID, CfgID, NodeHandle};
use crate::ops::{BasicBlock, OpType};
use crate::types::FunctionType;
use crate::{type_row, Hugr, HugrView, Node};
use cool_asserts::assert_matches;
Expand Down Expand Up @@ -323,11 +327,14 @@ mod test {
assert_eq!(depth(h.base_hugr(), n), expected_depth);
}
let blocks = [head, left, right, merge];
h.apply_rewrite(OutlineCfg::new(blocks)).unwrap();
let (new_block, new_cfg) = h.apply_rewrite(OutlineCfg::new(blocks)).unwrap();
for n in blocks {
assert_eq!(depth(h.base_hugr(), n), expected_depth + 2);
}
let new_block = h.output_neighbours(entry).exactly_one().ok().unwrap();
assert_eq!(
new_block,
h.output_neighbours(entry).exactly_one().ok().unwrap()
);
for n in [entry, exit, tail, new_block] {
assert_eq!(depth(h.base_hugr(), n), expected_depth);
}
Expand All @@ -339,6 +346,12 @@ mod test {
h.output_neighbours(tail).take(2).collect::<HashSet<Node>>(),
HashSet::from([exit, new_block])
);
assert_matches!(
h.get_optype(new_block),
OpType::BasicBlock(BasicBlock::DFB { .. })
);
assert_eq!(h.base_hugr().get_parent(new_cfg), Some(new_block));
assert_matches!(h.base_hugr().get_optype(new_cfg), OpType::CFG(_));
}

#[test]
Expand Down Expand Up @@ -389,15 +402,22 @@ mod test {
for &n in blocks_to_move.iter().chain(other_blocks.iter()) {
assert_eq!(depth(&h, n), 1);
}
h.apply_rewrite(OutlineCfg::new(blocks_to_move.iter().copied()))
let (new_block, new_cfg) = h
.apply_rewrite(OutlineCfg::new(blocks_to_move.iter().copied()))
.unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
let new_entry = h.children(h.root()).next().unwrap();
assert_eq!(new_block, h.children(h.root()).next().unwrap());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_entry, although correct, could also suggest the entry node inside the new sub-cfg, hence "renaming" to new_block

assert_matches!(
h.get_optype(new_block),
OpType::BasicBlock(BasicBlock::DFB { .. })
);
assert_eq!(h.get_parent(new_cfg), Some(new_block));
assert_matches!(h.get_optype(new_cfg), OpType::CFG(_));
for n in other_blocks {
assert_eq!(depth(&h, n), 1);
}
for n in blocks_to_move {
assert_eq!(h.get_parent(h.get_parent(n).unwrap()).unwrap(), new_entry);
assert_eq!(h.get_parent(n).unwrap(), new_cfg);
}
}
}