Skip to content

feat: LocalizeEdges pass #2237

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

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open

feat: LocalizeEdges pass #2237

wants to merge 54 commits into from

Conversation

acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented May 20, 2025

Based off @doug-q's #1912 but with "outside-in" transformation step avoiding much data storage (e.g. ParentSourceMap).

Copy link

codecov bot commented May 20, 2025

Codecov Report

Attention: Patch coverage is 96.16088% with 21 lines in your changes missing coverage. Please review.

Project coverage is 82.22%. Comparing base (9985143) to head (e2f11d4).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
hugr-passes/src/non_local/localize.rs 96.14% 11 Missing ⚠️
hugr-passes/src/non_local.rs 96.18% 6 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2237      +/-   ##
==========================================
+ Coverage   82.00%   82.22%   +0.22%     
==========================================
  Files         234      235       +1     
  Lines       41618    42244     +626     
  Branches    37532    38156     +624     
==========================================
+ Hits        34127    34735     +608     
- Misses       5518     5530      +12     
- Partials     1973     1979       +6     
Flag Coverage Δ
python 85.34% <ø> (+0.12%) ⬆️
rust 81.89% <96.16%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@acl-cqc acl-cqc marked this pull request as ready for review May 20, 2025 13:56
@acl-cqc acl-cqc requested a review from a team as a code owner May 20, 2025 13:56
@acl-cqc acl-cqc requested a review from aborgna-q May 20, 2025 13:56
@acl-cqc acl-cqc self-assigned this May 21, 2025
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

Adding some comments, I still need to finish localize.rs

/// [ComposablePass] that converts all non-local edges in a Hugr
/// into local ones, by inserting extra inputs to container nodes
/// and extra outports to Input nodes.
pub struct LocalizeEdges;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
pub struct LocalizeEdges;
#[derive(Clone, Debug, Hash)]
pub struct LocalizeEdges;


#[derive(derive_more::Error, derive_more::Display, derive_more::From, Debug, PartialEq)]
#[non_exhaustive]
pub enum LocalizeEdgesError {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We really need to start checking for rustdocs...

#![expect(missing_docs)] // TODO: Fix...

fn run(&self, hugr: &mut H) -> Result<Self::Result, Self::Error> {
remove_nonlocal_edges(hugr)
}
}

/// Returns an iterator over all non local edges in a Hugr.
Copy link
Collaborator

Choose a reason for hiding this comment

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

drive-by: This is no longer "all nl edges", but the ones that interact with the entrypoint descendants.

hugr.linked_outputs(node, in_p)
.any(|(neighbour_node, _)| parent != hugr.get_parent(neighbour_node))
.then_some((node, in_p))
let (src, _) = hugr.single_linked_output(node, in_p)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We are also now missing edges that connect out of the entrypoint descendants. (I realize it's annoying to check and avoid duplicates :/)

// Group all the non-local edges in the graph by target node,
// storing for each the source and type (well-defined as these are Value edges).
let nonlocal_edges_map: HashMap<_, _> = nonlocal_edges(hugr)
.filter_map(|(node, inport)| {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this a filter_map instead of a map?

else {
panic!("impossible")
};
Some((node, (source, ty)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't we have multiple nl edges going to the same node?
This will drop all but one while collecting

v.into_iter().map(|(_, t)| t.clone())
}

pub fn remove_nonlocal_edges<H: HugrMut>(hugr: &mut H) -> Result<(), LocalizeEdgesError> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Docs!

Comment on lines +34 to +37
match self.0.get(&node) {
Some(x) => Either::Left(x.iter()),
None => Either::Right(std::iter::empty()),
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit :)

Suggested change
match self.0.get(&node) {
Some(x) => Either::Left(x.iter()),
None => Either::Right(std::iter::empty()),
}
self.0.get(&node).into_iter().flat_map(BTreeMap::iter)

@aborgna-q aborgna-q self-requested a review May 21, 2025 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants