-
Notifications
You must be signed in to change notification settings - Fork 12
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
extend hoist variables functionality #357
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,8 @@ | |
SubstituteExpressions, is_dimension_constant | ||
) | ||
from loki.ir import ( | ||
CallStatement, Allocation, Deallocation, Transformer, FindNodes, Comment, Import | ||
CallStatement, Allocation, Deallocation, Transformer, FindNodes, Comment, Import, | ||
Assignment | ||
) | ||
from loki.tools.util import is_iterable, as_tuple, CaseInsensitiveDict, flatten | ||
|
||
|
@@ -204,12 +205,17 @@ class HoistVariablesTransformation(Transformation): | |
---------- | ||
as_kwarguments : boolean | ||
Whether to pass the hoisted arguments as `args` or `kwargs`. | ||
remap_dimensions : boolean | ||
Remap dimensions based on variables that are used for initializing | ||
other variables that could end up as dimensions for hoisted arrays. | ||
Thus, account for possibly uninitialized variables used as dimensions. | ||
""" | ||
|
||
_key = 'HoistVariablesTransformation' | ||
|
||
def __init__(self, as_kwarguments=False): | ||
def __init__(self, as_kwarguments=False, remap_dimensions=True): | ||
self.as_kwarguments = as_kwarguments | ||
self.remap_dimensions = remap_dimensions | ||
|
||
def transform_subroutine(self, routine, **kwargs): | ||
""" | ||
|
@@ -242,7 +248,12 @@ def transform_subroutine(self, routine, **kwargs): | |
f'the correct key.') | ||
|
||
if role == 'driver': | ||
self.driver_variable_declaration(routine, item.trafo_data[self._key]["to_hoist"]) | ||
if self.remap_dimensions: | ||
to_hoist = self.driver_variable_declaration_dim_remapping(routine, | ||
item.trafo_data[self._key]["to_hoist"]) | ||
else: | ||
to_hoist = item.trafo_data[self._key]["to_hoist"] | ||
self.driver_variable_declaration(routine, to_hoist) | ||
else: | ||
# We build the list of temporaries that are hoisted to the calling routine | ||
# Because this requires adding an intent, we need to make sure they are not | ||
|
@@ -319,6 +330,33 @@ def driver_variable_declaration(self, routine, variables): | |
""" | ||
routine.variables += tuple(v.rescope(routine) for v in variables) | ||
|
||
@staticmethod | ||
def driver_variable_declaration_dim_remapping(routine, variables): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [no action]: Just a general note: we keep hitting this remapping problem in many use cases - so eventually we might want a general utility that can do this. But for this use case this looks great. |
||
""" | ||
Take a list of variables and remap their dimensions for those being | ||
arrays to account for possibly uninitialized variables/dimensions. | ||
|
||
Parameters | ||
---------- | ||
routine : :any:`Subroutine` | ||
The relevant subroutine. | ||
variables : tuple of :any:`Variable` | ||
The tuple of variables for remapping. | ||
""" | ||
dim_vars = [ | ||
dim_var | ||
for var in variables if isinstance(var, sym.Array) | ||
for dim_var in FindVariables().visit(var.dimensions) | ||
] | ||
dim_map = { | ||
assignment.lhs: assignment.rhs | ||
for assignment in FindNodes(Assignment).visit(routine.body) | ||
if assignment.lhs in dim_vars | ||
} | ||
variables = [var.clone(dimensions=SubstituteExpressions(dim_map).visit(var.dimensions)) | ||
if isinstance(var, sym.Array) else var for var in variables] | ||
return variables | ||
|
||
def driver_call_argument_remapping(self, routine, call, variables): | ||
""" | ||
Callback method to re-map hoisted arguments for the driver-level routine. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a situation where we wouldn't want to do this? It seems to me like we might not need to make this an option and instead always do this.
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.
Good question ... I can't think of a situation we don't want to do that but on the other hand I think having a switch is not a bad idea?!
Anyway, I switched the default but if you insist we can remove this flag.
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.
No, flipping the default should be enough. Thanks!