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

Add support for assumed size declarations to Fparser #486

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

Conversation

awnawab
Copy link
Contributor

@awnawab awnawab commented Feb 12, 2025

This PR adds support for assumed size declarations (😑 ) for Fparser, and also adds the mechanics to the ArgumentShapeAnalysis to resolve the corresponding explicit shape.

Copy link

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

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.18%. Comparing base (48c5cbf) to head (7eff911).

Additional details and impacted files
@@           Coverage Diff            @@
##             main     #486    +/-   ##
========================================
  Coverage   96.17%   96.18%            
========================================
  Files         224      224            
  Lines       40386    40489   +103     
========================================
+ Hits        38842    38945   +103     
  Misses       1544     1544            
Flag Coverage Δ
lint_rules 96.39% <ø> (ø)
loki 96.18% <100.00%> (+<0.01%) ⬆️

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.

@awnawab awnawab marked this pull request as ready for review February 13, 2025 09:21
@awnawab awnawab requested review from mlange05 and reuterbal and removed request for mlange05 February 13, 2025 09:21
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.

Sorry you had to deal with this, but many thanks for taking care of it!

I left a few pointers where I think the transformation could be simplified, otherwise this looks good to me.

implicit none
real, intent(in) :: a(*)
real, intent(in) :: b(8,*)
real, intent(in) :: c(8,0:*)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you mind adding a d(2,4,3:*) to demonstrate that non-zero lower bounds and multiple preceeding explicit-shape-specs are working as well? It looks to me like the implementation would handle this correctly but worth double-checking.

@@ -28,6 +29,7 @@
)
from loki.tools import as_tuple, CaseInsensitiveDict
from loki.types import BasicType
from loki.expression import symbols as sym, simplify
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add this to the existing import from loki.expression above

@@ -80,6 +82,35 @@ def transform_subroutine(self, routine, **kwargs): # pylint: disable=arguments-
if d == ':']
vmap[arg] = arg.clone(type=arg.type.clone(shape=new_shape))

elif re.search(r'\*$', str(arg.shape[-1])):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need a regex here? Wouldn't the following work?

Suggested change
elif re.search(r'\*$', str(arg.shape[-1])):
elif str(arg.shape[-1])[-1] == '*':

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes that is much cleaner, thanks!

@@ -80,6 +82,35 @@ def transform_subroutine(self, routine, **kwargs): # pylint: disable=arguments-
if d == ':']
vmap[arg] = arg.clone(type=arg.type.clone(shape=new_shape))

elif re.search(r'\*$', str(arg.shape[-1])):
# determine extent of explicit shape part of assumed size declaration
expl_shape_len = [loc for loc, dim in enumerate(arg.shape) if '*' in dim][0]
Copy link
Collaborator

Choose a reason for hiding this comment

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

The standard explicitly mandates expl_shape_len = len(arg.shape)-1, can't we use that knowledge here?

The rank is equal to one plus the number of explicit-shape-specs.
https://j3-fortran.org/doc/year/10/10-007.pdf#page=114

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes great catch, thanks!

@@ -119,13 +150,15 @@ class ExplicitArgumentArrayShapeTransformation(Transformation):

def transform_subroutine(self, routine, **kwargs): # pylint: disable=arguments-differ

def assumed(dims):
return all(d == ':' for d in dims) or re.search(r'\*$', str(dims[-1]))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment about the need for a regex

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 for making the changes. Looks good to me now!

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