Skip to content

feat: add HierarchyTester for O(1) subtree-containment; rewrite nonlocal edge validation #2232

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

Conversation

acl-cqc
Copy link
Contributor

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

I thought it was worth reifying HierarchyTester as there are more uses upcoming and it seems a good thing to have. I'm not sure if there are other places already existing where we could use it...?

However I admit the validation rewrite is not as compelling as I had hoped, due to the requirement for containing_child

  • The containing_child for Ext edges, I expect to eventually disappear, as I think we are planning to remove the requirement for these Order edges (some other utility will compute the necessary topsort order that puts sources before nodes containing their targets)
  • The containing_child for Dom edges could be removed by computing the same start/stop indices as used in HierarchyTester, but taking dominance into account i.e. by traversing the dominator tree. The downside is then we'd most likely have to compute dominators for every CFG in the Hugr as soon as we need any HierarchyTester-ing, rather than (what we do now:) for each CFG as/if we need it. I could do this in this PR, but haven't ATM.

@acl-cqc acl-cqc requested a review from a team as a code owner May 16, 2025 20:25
@acl-cqc acl-cqc requested a review from doug-q May 16, 2025 20:25
Self { hugr, dominators }
Self {
hugr,
hierarchy_tester: None,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also just compute this now, rather than if-needed. Whereas dominators are only needed for Dom edges, the hierarchy tester is needed for both Dom and Ext edges, so more likely. (One might argue that any Hugr that doesn't need it, will probably be so small that computing it is insignificant??)

Copy link

codecov bot commented May 16, 2025

Codecov Report

Attention: Patch coverage is 94.89051% with 7 lines in your changes missing coverage. Please review.

Project coverage is 82.03%. Comparing base (3e2376e) to head (d8bf180).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
hugr-core/src/hugr/validate.rs 92.00% 6 Missing ⚠️
hugr-core/src/hierarchy.rs 98.38% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2232      +/-   ##
==========================================
+ Coverage   82.00%   82.03%   +0.03%     
==========================================
  Files         234      235       +1     
  Lines       41618    41689      +71     
  Branches    37532    37602      +70     
==========================================
+ Hits        34127    34200      +73     
  Misses       5518     5518              
+ Partials     1973     1971       -2     
Flag Coverage Δ
python 85.22% <ø> (+<0.01%) ⬆️
rust 81.68% <94.89%> (+0.03%) ⬆️

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 changed the title feat: add HierarchyTester for O(1) subtree-containment; rewrite validation feat: add HierarchyTester for O(1) subtree-containment; rewrite nonlocal edge validation May 16, 2025
Comment on lines +18 to +19
#[allow(unused)] // Just make sure the Hugr isn't changing behind our back
hugr: &'a H,
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if we want to mutate the hugr though? Not all changes affect the hierarchy.
(See e.g. petgraph's Topo iterator)


/// The entry and exit indices (inclusive, note every entry number is different);
/// and the nearest strictly-enclosing FuncDefn.
type NodeData<N> = (usize, usize, Option<N>);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be a struct with named fields.

Copy link
Collaborator

Choose a reason for hiding this comment

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

An alternative implementation (maybe a 2nd definition) is lazily computing these values.

See portgraph's Region::is_descendant, which has an amortized O(1) complexity too, and here would let us add nodes after the fact.

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.

2 participants