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

Utilities to merge associate blocks and restrict depth of associate resolution #388

Merged
merged 8 commits into from
Oct 18, 2024

Conversation

mlange05
Copy link
Collaborator

@mlange05 mlange05 commented Oct 2, 2024

Note: This sits on top of PR #387 and requires this to be merged first.

This PR adds a new utility to merge Associate blocks and adds a feature to the existing resolve_associations method to restrict the starting depth of the association resolution. Combining the two allows for a neat look and feel, where all except the first layer of (non-nested) associations get resolved after all non-nested ones have been merged into the outermost associate block. Tests have been added for both features.

Edit: This one took on a life of its own after the interior mechanics of ResolveAssociates were changed ( PR #387 ). As the new resolver relies very strictly on identifying the "depth" via finding the defining scope, the new merge mechanism needs to carefully update the changes scopes to re-build the local symbol table of the Associate nodes. This is done by moving the derivation mechanics from the frontend into the IR node and triggering it specifically (comment and suggestions welcome).

I've also added a rudimentary node-level test for Associate nodes and improved the associate mappings to allow symbols as keys, as well as strings.

Copy link

github-actions bot commented Oct 2, 2024

Documentation for this branch can be viewed at https://sites.ecmwf.int/docs/loki/388/index.html

Copy link

codecov bot commented Oct 7, 2024

Codecov Report

Attention: Patch coverage is 96.42857% with 5 lines in your changes missing coverage. Please review.

Project coverage is 95.96%. Comparing base (2bbe704) to head (63b7f9c).
Report is 41 commits behind head on main.

Files with missing lines Patch % Lines
loki/tools/util.py 75.00% 4 Missing ⚠️
loki/ir/nodes.py 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #388      +/-   ##
==========================================
+ Coverage   95.57%   95.96%   +0.38%     
==========================================
  Files         201      198       -3     
  Lines       39833    39279     -554     
==========================================
- Hits        38071    37694     -377     
+ Misses       1762     1585     -177     
Flag Coverage Δ
lint_rules 96.39% <ø> (ø)
loki 95.95% <96.42%> (+0.39%) ⬆️

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.

@mlange05 mlange05 force-pushed the naml-resolve-assoc-merge branch 2 times, most recently from 02c5b7a to 5f9779e Compare October 14, 2024 13:11
@mlange05 mlange05 marked this pull request as ready for review October 14, 2024 13:11
@mlange05 mlange05 marked this pull request as draft October 14, 2024 13:50
@mlange05 mlange05 marked this pull request as ready for review October 16, 2024 05:15
@mlange05
Copy link
Collaborator Author

@reuterbal This one took a few little detours, but is now ready I think. Please have a look.

Copy link
Collaborator

@reuterbal reuterbal left a comment

Choose a reason for hiding this comment

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

Many thanks, this is very nice! The implementation looks very clean and neat. Just a few suggestions to harden the test a little further, but I'd consider them optional.

@@ -239,33 +239,41 @@ class CaseInsensitiveDict(OrderedDict):
https://stackoverflow.com/questions/2082152/case-insensitive-dictionary
"""
def __setitem__(self, key, value):
super().__setitem__(key.lower(), value)
key = key.lower() if isinstance(key, str) else key
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fantastic 😍 This has annoyed me so often but I haven't come around to fixing it...

Comment on lines 316 to 319
assert 'B%a' in assoc.association_map and assoc.association_map['B%a'] == a
assert b_a in assoc.association_map and assoc.association_map[b_a] == a
assert 'a' in assoc.inverse_map and assoc.inverse_map['a'] == b_a
assert a in assoc.inverse_map and assoc.inverse_map[a] == b_a
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could probably be stricter here and mandate is relationship, to establish that there shouldn't be object copies at play?

Suggested change
assert 'B%a' in assoc.association_map and assoc.association_map['B%a'] == a
assert b_a in assoc.association_map and assoc.association_map[b_a] == a
assert 'a' in assoc.inverse_map and assoc.inverse_map['a'] == b_a
assert a in assoc.inverse_map and assoc.inverse_map[a] == b_a
assert 'B%a' in assoc.association_map and assoc.association_map['B%a'] is a
assert b_a in assoc.association_map and assoc.association_map[b_a] is a
assert 'a' in assoc.inverse_map and assoc.inverse_map['a'] is b_a
assert a in assoc.inverse_map and assoc.inverse_map[a] is b_a

Comment on lines 322 to 326
assert assign.lhs.scope == scope
assert assign2.lhs.scope == scope
assoc.rescope_symbols()
assert assign.lhs.scope == assoc
assert assign2.lhs.scope == scope
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here? (is instead of ==)

@reuterbal reuterbal added the ready for merge This PR has been approved and is ready to be merged label Oct 18, 2024
@reuterbal reuterbal merged commit 56ab8d7 into main Oct 18, 2024
13 checks passed
@reuterbal reuterbal deleted the naml-resolve-assoc-merge branch October 18, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for merge This PR has been approved and is ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants