-
Notifications
You must be signed in to change notification settings - Fork 19
Constant Propagation Transform #515
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
base: main
Are you sure you want to change the base?
Changes from all commits
b98b3c6
926dcfe
b12e2ce
7c710e9
9f92c85
14913ad
fb0d395
586fbf9
42ba0ae
01eb414
8e0bd0f
30fd0a7
56acf41
b8bb823
44b0c7f
04fac04
65e6335
9e8a243
5be52e7
f1b7bca
b91f6d9
2cfffb9
eeb073d
2e2eb9a
da86b4f
5b112f4
c08310a
53ce0a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # (C) Copyright 2024- ECMWF. | ||
| # This software is licensed under the terms of the Apache Licence Version 2.0 | ||
| # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # In applying this licence, ECMWF does not waive the privileges and immunities | ||
| # granted to it by virtue of its status as an intergovernmental organisation | ||
| # nor does it submit to any jurisdiction. | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from contextlib import contextmanager | ||
|
|
||
| from loki import Transformer | ||
|
|
||
| __all__ = ['AbstractDataflowAnalysis'] | ||
|
|
||
| class AbstractDataflowAnalysis(Transformer, ABC): | ||
|
Collaborator
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. A few docstrings would be useful here.
Collaborator
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. I'm not following why this needs to inherit from |
||
| class _Attacher(Transformer): | ||
| pass | ||
|
|
||
| class _Detacher(Transformer): | ||
| pass | ||
|
|
||
| def get_attacher(self): | ||
| return self._Attacher() | ||
|
|
||
| def get_detacher(self): | ||
| return self._Detacher() | ||
|
|
||
| @abstractmethod | ||
| def attach_dataflow_analysis(self, module_or_routine): | ||
| pass | ||
|
|
||
| def detach_dataflow_analysis(self, module_or_routine): | ||
| """ | ||
| Remove from each IR node the stored dataflow analysis metadata. | ||
|
|
||
| Accessing the relevant attributes afterwards raises :py:class:`RuntimeError`. | ||
| """ | ||
|
|
||
| if hasattr(module_or_routine, 'spec'): | ||
| self.get_detacher().visit(module_or_routine.spec) | ||
| if hasattr(module_or_routine, 'body'): | ||
| self.get_detacher().visit(module_or_routine.body) | ||
|
|
||
| @contextmanager | ||
| def dataflow_analysis_attached(self, module_or_routine): | ||
|
Collaborator
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. I understand the rationale for embedding this inside the class to directly relate to the implementation of the specific DFA class. However, I'd prefer to also keep the current API with a class-free contextmanager. This could instead receive an optional argument @contextmanager
def dataflow_analysis_attached(self, module_or_routine, dfa=None):
if dfa is None:
from loki.analyse.dataflow_analysis import DataflowAnalysis # pylint: disable=no-toplevel-import
dfa = DataFlowAnalysis()
dfa.attach_dataflow_analysis(module_or_routine)
yield
dfa.detach_dataflow_analysis(module_or_routine) |
||
| self.attach_dataflow_analysis(module_or_routine) | ||
| try: | ||
| yield module_or_routine | ||
| finally: | ||
| self.detach_dataflow_analysis(module_or_routine) | ||
This file was deleted.
Large diffs are not rendered by default.
|
Collaborator
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. Let's call this |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # (C) Copyright 2024- ECMWF. | ||
| # This software is licensed under the terms of the Apache Licence Version 2.0 | ||
| # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # In applying this licence, ECMWF does not waive the privileges and immunities | ||
| # granted to it by virtue of its status as an intergovernmental organisation | ||
| # nor does it submit to any jurisdiction. | ||
|
|
||
| from loki.analyse.constant_propagation_analysis import ConstantPropagationAnalysis | ||
| from loki import Transformer, Subroutine | ||
|
|
||
| __all__ = ['ConstantPropagationTransformer'] | ||
|
|
||
| class ConstantPropagationTransformer(Transformer): | ||
|
|
||
| def __init__(self, fold_floats=True, unroll_loops=True): | ||
| self.fold_floats = fold_floats | ||
| self.unroll_loops = unroll_loops | ||
| super().__init__() | ||
|
|
||
| def visit(self, expr, *args, **kwargs): | ||
| const_prop = ConstantPropagationAnalysis(self.fold_floats, self.unroll_loops, True) | ||
| constants_map = kwargs.get('constants_map', dict()) | ||
| try: | ||
| declarations_map = const_prop.generate_declarations_map(expr) | ||
| # If a user specifies their own map, they probably want it to override these | ||
| declarations_map.update(constants_map) | ||
| constants_map = declarations_map | ||
| except AttributeError: | ||
| pass | ||
|
|
||
| is_routine = isinstance(expr, Subroutine) | ||
| target = expr.body if is_routine else expr | ||
|
|
||
| target = const_prop.get_attacher().visit(target, constants_map=constants_map) | ||
| target = const_prop.get_detacher().visit(target) | ||
|
|
||
| if is_routine: | ||
| expr.body = target | ||
| return expr | ||
|
|
||
| return target |
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.
Missing import: