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

refactor!: s/RemoveConstIgnore/RemoveLoadConstant #789

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions specification/hugr.md
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ omitted it defaults to the parent of `c` (in this case said `c` will
have to be in a DSG or CSG rather than under the Module Root.) If `P` is
provided, it must be a descendent of the parent of `c`.

###### `RemoveConstIgnore`
###### `RemoveLoadConstant`

Given a `LoadConstant<T>` node `n` that has no outgoing edges, remove
it (and its incoming Static edge and any Order edges) from the hugr.
Expand Down Expand Up @@ -1408,7 +1408,7 @@ using `Replace` (with a set of `identity<T>` nodes) followed by
### Normalisation

We envisage that some kind of pass can be used after a rewrite or series
of rewrites to automatically apply RemoveConstIgnore for any unused
of rewrites to automatically apply RemoveLoadConstant for any unused
load\_constants, and other such
tidies. This might be global, or by tracking which parts of the Hugr
have been touched.
Expand Down
10 changes: 5 additions & 5 deletions src/algorithm/const_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
builder::{DFGBuilder, Dataflow, DataflowHugr},
extension::{ConstFoldResult, ExtensionRegistry},
hugr::{
rewrite::consts::{RemoveConst, RemoveConstIgnore},
rewrite::consts::{RemoveConst, RemoveLoadConstant},
views::SiblingSubgraph,
HugrMut,
},
Expand Down Expand Up @@ -96,14 +96,14 @@ fn const_graph(consts: Vec<Const>, reg: &ExtensionRegistry) -> Hugr {
/// return an iterator of possible constant folding rewrites. The
/// [`SimpleReplacement`] replaces an operation with constants that result from
/// evaluating it, the extension registry `reg` is used to validate the
/// replacement HUGR. The vector of [`RemoveConstIgnore`] refer to the
/// replacement HUGR. The vector of [`RemoveLoadConstant`] refer to the
/// LoadConstant nodes that could be removed - they are not automatically
/// removed as they may be used by other operations.
pub fn find_consts<'a, 'r: 'a>(
hugr: &'a impl HugrView,
candidate_nodes: impl IntoIterator<Item = Node> + 'a,
reg: &'r ExtensionRegistry,
) -> impl Iterator<Item = (SimpleReplacement, Vec<RemoveConstIgnore>)> + 'a {
) -> impl Iterator<Item = (SimpleReplacement, Vec<RemoveLoadConstant>)> + 'a {
// track nodes for operations that have already been considered for folding
let mut used_neighbours = BTreeSet::new();

Expand Down Expand Up @@ -135,14 +135,14 @@ fn fold_op(
hugr: &impl HugrView,
op_node: Node,
reg: &ExtensionRegistry,
) -> Option<(SimpleReplacement, Vec<RemoveConstIgnore>)> {
) -> Option<(SimpleReplacement, Vec<RemoveLoadConstant>)> {
// only support leaf folding for now.
let neighbour_op = hugr.get_optype(op_node).as_leaf_op()?;
let (in_consts, removals): (Vec<_>, Vec<_>) = hugr
.node_inputs(op_node)
.filter_map(|in_p| {
let (con_op, load_n) = get_const(hugr, op_node, in_p)?;
Some(((in_p, con_op), RemoveConstIgnore(load_n)))
Some(((in_p, con_op), RemoveLoadConstant(load_n)))
})
.unzip();
// attempt to evaluate op
Expand Down
12 changes: 6 additions & 6 deletions src/hugr/rewrite/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use super::Rewrite;

/// Remove a [`crate::ops::LoadConstant`] node with no consumers.
#[derive(Debug, Clone)]
pub struct RemoveConstIgnore(pub Node);
pub struct RemoveLoadConstant(pub Node);

/// Error from an [`RemoveConst`] or [`RemoveConstIgnore`] operation.
/// Error from an [`RemoveConst`] or [`RemoveLoadConstant`] operation.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
pub enum RemoveError {
/// Invalid node.
Expand All @@ -29,7 +29,7 @@ pub enum RemoveError {
RemoveFail(#[from] HugrError),
}

impl Rewrite for RemoveConstIgnore {
impl Rewrite for RemoveLoadConstant {
type Error = RemoveError;

// The Const node the LoadConstant was connected to.
Expand Down Expand Up @@ -161,20 +161,20 @@ mod test {
);

assert_eq!(
h.apply_rewrite(RemoveConstIgnore(tup_node)),
h.apply_rewrite(RemoveLoadConstant(tup_node)),
Err(RemoveError::InvalidNode(tup_node))
);
let load_1_node = load_1.node();
let load_2_node = load_2.node();
let con_node = con_node.node();

let remove_1 = RemoveConstIgnore(load_1_node);
let remove_1 = RemoveLoadConstant(load_1_node);
assert_eq!(
remove_1.invalidation_set().exactly_one().ok(),
Some(load_1_node)
);

let remove_2 = RemoveConstIgnore(load_2_node);
let remove_2 = RemoveLoadConstant(load_2_node);

let remove_con = RemoveConst(con_node);
assert_eq!(
Expand Down