Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion loki/analyse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
Advanced analysis utilities, such as dataflow analysis functionalities.
"""

from loki.analyse.analyse_dataflow import * # noqa
from loki.analyse.dataflow_analysis import * # noqa
from loki.analyse.abstract_dfa import * # noqa
51 changes: 51 additions & 0 deletions loki/analyse/abstract_dfa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# (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', 'dataflow_analysis_attached']

class AbstractDataflowAnalysis(ABC):
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(module_or_routine, dfa=None):
if dfa is None:
from loki.analyse.dataflow_analysis import DataflowAnalysis # pylint: disable=no-toplevel-import

Check failure on line 47 in loki/analyse/abstract_dfa.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

W0012: Unknown option value for 'disable', expected a valid pylint message and got 'no-toplevel-import' (unknown-option-value)

Check failure on line 47 in loki/analyse/abstract_dfa.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

C0415: Import outside toplevel (loki.analyse.dataflow_analysis.DataflowAnalysis) (import-outside-toplevel)
dfa = DataflowAnalysis()
dfa.attach_dataflow_analysis(module_or_routine)
yield
dfa.detach_dataflow_analysis(module_or_routine)

Check failure on line 51 in loki/analyse/abstract_dfa.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

C0304: Final newline missing (missing-final-newline)
Loading
Loading