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

feat: add verification to constant folding #1030

Merged
merged 6 commits into from
May 14, 2024
Merged
Changes from 1 commit
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
59 changes: 27 additions & 32 deletions hugr/src/algorithm/const_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ pub struct ConstFoldConfig {
}

impl ConstFoldConfig {
/// TODO
/// Create a new `ConstFoldConfig` with default configuration.
pub fn new() -> Self {
Self::default()
}

/// TODO
/// Build a `ConstFoldConfig` with the given [VerifyLevel].
pub fn with_verify(mut self, verify: VerifyLevel) -> Self {
self.verify = verify;
self
Expand All @@ -78,26 +78,27 @@ impl ConstFoldConfig {
.map_err(|err| ConstFoldError::verify_err(label, err))
}

/// TODO
/// Run the Constant Folding pass.
pub fn run(&self, h: &mut impl HugrMut, reg: &ExtensionRegistry) -> Result<(), ConstFoldError> {
Copy link
Member

Choose a reason for hiding this comment

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

not sure I like having a run method on something with the suffix Config. There's the beginnings of a general "algorithm" interface here (configuration, transformation, verfification). Is it worth deferring some of these changes in to a generic follow up PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I can do that. I didn't want to through away verifying before and after the pass, but I can change that to be guarded behind #[cfg(test)].

self.verify_impl("input", h, reg)?;
loop {
// would be preferable if the candidates were updated to be just the
// neighbouring nodes of those added.
let rewrites = find_consts(h, h.nodes(), reg).collect_vec();
if rewrites.is_empty() {
// We can only safely apply a single replacement. Applying a
Copy link
Member

Choose a reason for hiding this comment

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

yes, the original code was based on the assumption that find_consts would only return rewrites for ops that only had const inputs (which would I think be safe to apply all of them?) but that's not what find_consts does

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes I think that would be safe, if that's what find_consts did.

// replacement removes nodes and edges which may be referenced by
// further replacements returned by find_consts. Even worse, if we
// attempted to apply those replacements, expecting them to fail if
// the nodes and edges they reference had been deleted, they may
// succeed because new nodes and edges reused the ids.
//
// We could be a lot smarter here, keeping track of `LoadConstant`
// nodes and only looking at their out neighbours.
let Some((replace, removes)) = find_consts(h, h.nodes(), reg).next() else {
break;
}
for (replace, removes) in rewrites {
h.apply_rewrite(replace)?;
for rem in removes {
if let Ok(const_node) = h.apply_rewrite(rem) {
// if the LoadConst was removed, try removing the Const too.
if h.apply_rewrite(RemoveConst(const_node)).is_err() {
// const cannot be removed - no problem
continue;
}
}
};
h.apply_rewrite(replace)?;
for rem in removes {
if let Ok(const_node) = h.apply_rewrite(rem) {
// if the LoadConst was removed, try removing the Const too.
let _ = h.apply_rewrite(RemoveConst(const_node));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it safe to ignore all errors here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes it is, but I'll add a comment to explain why.

}
}
}
Expand Down Expand Up @@ -230,18 +231,16 @@ fn fold_op(
})
.unzip();
// attempt to evaluate op
let folded = fold_leaf_op(neighbour_op, &in_consts)?;
let (op_outs, consts): (Vec<_>, Vec<_>) = folded.into_iter().unzip();
let nu_out = op_outs
let (nu_out, consts): (HashMap<_, _>, Vec<_>) = fold_leaf_op(neighbour_op, &in_consts)?
.into_iter()
.enumerate()
.filter_map(|(i, out)| {
// map from the ports the op was linked to, to the output ports of
// the replacement.
hugr.single_linked_input(op_node, out)
.map(|np| (np, i.into()))
.filter_map(|(i, (op_out, konst))| {
// for each used port of the op give the nu_out entry and the
// corresponding Value
hugr.single_linked_input(op_node, op_out)
.map(|np| ((np, i.into()), konst))
})
.collect();
.unzip();
let replacement = const_graph(consts, reg);
let sibling_graph = SiblingSubgraph::try_from_nodes([op_node], hugr)
.expect("Operation should form valid subgraph.");
Expand All @@ -262,11 +261,8 @@ fn get_const(hugr: &impl HugrView, op_node: Node, in_p: IncomingPort) -> Option<
let (load_n, _) = hugr.single_linked_output(op_node, in_p)?;
let load_op = hugr.get_optype(load_n).as_load_constant()?;
let const_node = hugr
.linked_outputs(load_n, load_op.constant_port())
.exactly_one()
.ok()?
.single_linked_output(load_n, load_op.constant_port())?
.0;

let const_op = hugr.get_optype(const_node).as_const()?;

// TODO avoid const clone here
Expand Down Expand Up @@ -468,7 +464,6 @@ mod test {
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it worth also including the test from #996 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, will do.

#[test]
#[should_panic]
fn orphan_output() {
// pseudocode:
// x0 := bool(true)
Expand Down