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 57 commits into
base: main
Choose a base branch
from
Open

feat: LocalizeEdges pass #2237

wants to merge 57 commits into from

Conversation

acl-cqc
Copy link
Contributor

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

closes #1234

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.33028% with 20 lines in your changes missing coverage. Please review.

Project coverage is 82.17%. Comparing base (9985143) to head (b4417f3).
Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
hugr-passes/src/non_local/localize.rs 96.11% 11 Missing ⚠️
hugr-passes/src/non_local.rs 96.56% 6 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2237      +/-   ##
==========================================
+ Coverage   82.00%   82.17%   +0.17%     
==========================================
  Files         234      238       +4     
  Lines       41618    43305    +1687     
  Branches    37532    39217    +1685     
==========================================
+ Hits        34127    35587    +1460     
- Misses       5518     5722     +204     
- Partials     1973     1996      +23     
Flag Coverage Δ
python 85.34% <ø> (+0.12%) ⬆️
rust 81.84% <96.33%> (+0.19%) ⬆️

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. Or rather, it's ones that are incoming to the subtree. So actually only one endpoint need be in the subtree.

So my thinking here was that there were two reasonable policies:

  • All nonlocal edges within the Hugr, ignoring hugr.entrypoint().
  • All nonlocal edges entirely beneath the entrypoint.

I meant to go for the latter, on the grounds that you can then get the former behaviour by passing entrypoint==root. Clearly I missed the latter ;-), I think the sensible thing is to put in that check.

However, if you think it'd be better to do this in stages (e.g. have two methods _root and _entrypoint, make the unsuffixed version delegate to _root but @deprecate it, and maybe eventually rename the _entrypoint variant back to no-suffix) then happy to do that

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed that "All nonlocal edges entirely beneath the entrypoint" is the way to go.
I think most passes should avoid out-of-tree modifications unless explicitly stated (e.g. defining a function in the module).

This should be compatible with the pre-entrypoint API.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed that "All nonlocal edges entirely beneath the entrypoint" is the way to go.
I think most passes should avoid out-of-tree modifications unless explicitly stated (e.g. defining a function in the module).

This should be compatible with the pre-entrypoint API.

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 :/)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Argh. Yes.

So my thinking was that for transformation, we should mutate the Hugr only beneath the entrypoint, in which case we only want edges with both ends beneath the entrypoint.

Counterexamples?

  • An edge entering the subtree from a sibling of the entrypoint. This requires retargetting that edge to the entrypoint, and mutating the entrypoint node. Since no node outside the subtree is changed, I guess you could argue this is allowed, although I don't think I would myself....(looks to me like you've changed an edge outside the subtree!)
  • Unless the entrypoint is a DataflowBlock, the only possible NL edge leaving it would be an Ext edge from the entrypoint itself (to a descendant of a sibling). I think that edge is outside the subtree....(indeed, localizing it does not even mutate the entrypoint node)
  • If the entrypoint is a Dataflow Block, then Dom edges could leave its children to (descendants of) other DFB siblings-of-the-entrypoint, i.e. outside the tree. But clearly localizing those is gonna involve widespread changes outside the subtree (as well as to the entrypoint node itself).

// 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good spot, we've already skipped over inports without a single_linked_output in nonlocal_edges(hugr). Thanks!

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

Copy link
Contributor Author

@acl-cqc acl-cqc May 23, 2025

Choose a reason for hiding this comment

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

Ooh good point. ("All compliments accrue to Doug but all shortcomings are my fault" 😉 😉 ). Yes, we dedup sources in the ExtraSourceReqs map (which is correct) but should not do so here. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add a test.

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!

@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.

Create a "remove-nonlocal-edges" pass
3 participants