-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
feat: LocalizeEdges pass #2237
Conversation
Codecov ReportAttention: Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
clippy objected to use of `mut` inside a debug_assert!, even though the mutated thing was local to the assert...
There was a problem hiding this 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 {} |
There was a problem hiding this comment.
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...
Line 2 in 2d97665
#![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. |
There was a problem hiding this comment.
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)?; |
There was a problem hiding this comment.
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)| { |
There was a problem hiding this comment.
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))) |
There was a problem hiding this comment.
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 collect
ing
v.into_iter().map(|(_, t)| t.clone()) | ||
} | ||
|
||
pub fn remove_nonlocal_edges<H: HugrMut>(hugr: &mut H) -> Result<(), LocalizeEdgesError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs!
match self.0.get(&node) { | ||
Some(x) => Either::Left(x.iter()), | ||
None => Either::Right(std::iter::empty()), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit :)
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) |
Based off @doug-q's #1912 but with "outside-in" transformation step avoiding much data storage (e.g. ParentSourceMap).