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 ASSOCIATE blocks #2165

Open
arporter opened this issue Jun 7, 2023 · 9 comments
Open

Add support for ASSOCIATE blocks #2165

arporter opened this issue Jun 7, 2023 · 9 comments
Assignees
Labels
NEMOVAR Relating to the NEMOVAR code.

Comments

@arporter
Copy link
Member

arporter commented Jun 7, 2023

Fortran 2003 has the ASSOCIATE block:
image
and this is used quite heavily in NEMOVAR.
Essentially, it boils down to syntactic sugar in order to simplify the use of complex expressions, e.g.:
image

@arporter arporter added the NEMOVAR Relating to the NEMOVAR code. label Jun 7, 2023
@arporter arporter self-assigned this Jun 7, 2023
@arporter
Copy link
Member Author

arporter commented Jun 7, 2023

I think this should just be a matter of identifying references to an associate-name and replacing it with the equivalent PSyIR fragment which we can construct when we encounter the ASSOCIATE.

@arporter
Copy link
Member Author

arporter commented Jun 8, 2023

I thought about this some more last night and decided it wouldn't actually be that easy - as illustrated by the last two examples above, the associate-name can be a reference to a structure access. This is then effectively the same problem that we've already solved in the in-lining support. That might need some refactoring to support this case.

@arporter
Copy link
Member Author

arporter commented Jun 8, 2023

There is a subtlety:
image
this means that if we simply replace an associate-name with its corresponding expression, we have to make sure that none of its constituent references are written to before the substitution. This is pretty much impossible for us to do if the associate block contains calls to impure routines that could potentially modify data accessed in the block. The alternative is to assign the expressions to variables but this requires us to be able to determine the type of the expression.

arporter added a commit that referenced this issue Jun 8, 2023
@arporter
Copy link
Member Author

arporter commented Jun 8, 2023

I now have a first working version that simply creates an instance of InlineTrans and uses its internal method to perform the necessary substitutions. This works out of the box for quite complex examples :-) I now need to:

  • tidy the code in InlineTrans;
  • move that code to a different location (maybe in tools?);
  • allow for the subtlety above by using dependence analysis to check we can't break anything;

@arporter
Copy link
Member Author

I started implementing functionality to assign the associate-expressions to local temporaries and immediately hit (yet) another problem: associate-names can appear on the LHS of assignments (as in the examples in the description of this Issue). Obviously it then makes no sense to have a local temporary :-(

@arporter
Copy link
Member Author

Unfortunately, if an associate-name is passed to a Call then we don't know whether or not it is written to.

@arporter
Copy link
Member Author

Our normal approach to this sort of problem is to give the user the nuclear option (i.e. tell us that it is safe). However, this functionality is in the PSyIR front end and so we have no current mechanism for exposing such an option (and then it would have to be on a per-file basis anyway).

An alternative approach would be to leave the front end as is and supply a Transformation that attempts to convert a CodeBlock containing an ASSOCIATE block into code. That could then be given a force option. What do @rupertford and @sergisiso think?

@rupertford
Copy link
Collaborator

Why does it matter if the associate name appears on the LHS of an expression if the associate name is translated to a local variable?

@arporter
Copy link
Member Author

We've just discussed this at length and have concluded that the only general solution is to either declare a pointer if the expression corresponds to an address in memory OR evaluate it and store the result if it has a value. The question is, how easy is it going to be to modify the declaration of the thing we need to point to (i.e. give it the TARGET attribute) so that Fortran is happy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NEMOVAR Relating to the NEMOVAR code.
Projects
None yet
Development

No branches or pull requests

2 participants