From 219a4572fa6bf6625d86675d85e049422f128736 Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Fri, 13 Jun 2025 12:31:46 +0100 Subject: [PATCH 1/8] #3032 update utils.py --- examples/nemo/scripts/utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/examples/nemo/scripts/utils.py b/examples/nemo/scripts/utils.py index 8084e2e637..ecda207329 100755 --- a/examples/nemo/scripts/utils.py +++ b/examples/nemo/scripts/utils.py @@ -38,7 +38,8 @@ from psyclone.domain.common.transformations import KernelModuleInlineTrans from psyclone.psyir.nodes import ( Assignment, Loop, Directive, Reference, CodeBlock, ArrayReference, - Call, Return, IfBlock, Routine, IntrinsicCall, StructureReference) + Call, Return, IfBlock, Routine, Schedule, IntrinsicCall, + StructureReference) from psyclone.psyir.symbols import ( DataSymbol, INTEGER_TYPE, ScalarType, RoutineSymbol) from psyclone.psyir.transformations import ( @@ -505,9 +506,19 @@ def add_profiling(children): :type children: list of :py:class:`psyclone.psyir.nodes.Node` ''' + if children and isinstance(children, Schedule): + children = children.children + if not children: return + # We do not want profiling calipers inside the PSyclone-generated + # comparison functions + parent_routine = children[0].ancestor(Routine) + if (parent_routine and + "_psyclone_internal_cmp" in parent_routine.name.lower()): + return + node_list = [] for child in children[:]: # Do we want this node to be included in a profiling region? From 54c96ee645f85368e5ff3f0e7600c4fae5e99d5f Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Fri, 13 Jun 2025 13:17:06 +0100 Subject: [PATCH 2/8] #3032 improve comments and type hints --- examples/nemo/scripts/utils.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/nemo/scripts/utils.py b/examples/nemo/scripts/utils.py index ecda207329..e3402ee221 100755 --- a/examples/nemo/scripts/utils.py +++ b/examples/nemo/scripts/utils.py @@ -35,9 +35,11 @@ ''' Utilities file to parallelise Nemo code. ''' +from typing import List, Union + from psyclone.domain.common.transformations import KernelModuleInlineTrans from psyclone.psyir.nodes import ( - Assignment, Loop, Directive, Reference, CodeBlock, ArrayReference, + Assignment, Loop, Directive, Node, Reference, CodeBlock, ArrayReference, Call, Return, IfBlock, Routine, Schedule, IntrinsicCall, StructureReference) from psyclone.psyir.symbols import ( @@ -496,17 +498,17 @@ def insert_explicit_loop_parallelism( continue -def add_profiling(children): +def add_profiling(children: Union[List[Node], Schedule]): ''' - Walks down the PSyIR and inserts the largest possible profiling regions. - Code that contains directives is excluded. + Walks down the PSyIR and inserts the largest possible profiling regions + in place. Code that contains directives is excluded. - :param children: sibling nodes in the PSyIR to which to attempt to add \ - profiling regions. - :type children: list of :py:class:`psyclone.psyir.nodes.Node` + :param children: a Schedule or sibling nodes in the PSyIR to which to + attempt to add profiling regions. ''' if children and isinstance(children, Schedule): + # If we are given a Schedule, we look at its children. children = children.children if not children: From e778f56cfa1e1bc8cb55bcda9f6990750d35e2b9 Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Fri, 13 Jun 2025 14:54:59 +0100 Subject: [PATCH 3/8] #3032 do not add profiling inside any functions --- examples/nemo/scripts/utils.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/nemo/scripts/utils.py b/examples/nemo/scripts/utils.py index e3402ee221..381bf88620 100755 --- a/examples/nemo/scripts/utils.py +++ b/examples/nemo/scripts/utils.py @@ -501,7 +501,7 @@ def insert_explicit_loop_parallelism( def add_profiling(children: Union[List[Node], Schedule]): ''' Walks down the PSyIR and inserts the largest possible profiling regions - in place. Code that contains directives is excluded. + in place. Code inside functions or that contains directives is excluded. :param children: a Schedule or sibling nodes in the PSyIR to which to attempt to add profiling regions. @@ -514,11 +514,10 @@ def add_profiling(children: Union[List[Node], Schedule]): if not children: return - # We do not want profiling calipers inside the PSyclone-generated - # comparison functions + # We do not want profiling calipers inside functions (such as the + # PSyclone-generated comparison functions). parent_routine = children[0].ancestor(Routine) - if (parent_routine and - "_psyclone_internal_cmp" in parent_routine.name.lower()): + if parent_routine and parent_routine.return_symbol: return node_list = [] From 2e4f5dbdc9f353740417d04298069d981af3a159 Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Fri, 13 Jun 2025 15:42:32 +0100 Subject: [PATCH 4/8] #3032 update ProfileTrans to refuse elemental and modify pure routines --- .../psyir/transformations/profile_trans.py | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/psyclone/psyir/transformations/profile_trans.py b/src/psyclone/psyir/transformations/profile_trans.py index fd34f98c2a..eb3b63e861 100644 --- a/src/psyclone/psyir/transformations/profile_trans.py +++ b/src/psyclone/psyir/transformations/profile_trans.py @@ -38,7 +38,9 @@ '''This module provides the Profile transformation. ''' -from psyclone.psyir.nodes import Return, ProfileNode +from typing import Dict, List, Union + +from psyclone.psyir.nodes import Node, Return, ProfileNode, Routine from psyclone.psyir.transformations.psy_data_trans import PSyDataTrans @@ -75,3 +77,38 @@ class ProfileTrans(PSyDataTrans): def __init__(self): super().__init__(ProfileNode) + + def validate(self, + nodes: Union[Node, List[Node]], + options: Dict =None): + ''' + :param nodes: a node or list of nodes to be instrumented with + profiling calls. + + :raises TransformationError: if the target nodes are within an + ELEMENTAL routine. + ''' + node_list = self.get_node_list(nodes) + super().validate(node_list, options=options) + + parent_routine = node_list[0].ancestor(Routine) + if parent_routine and parent_routine.symbol.is_elemental: + raise TransformationError( + f"Cannot add profiling calipers inside ELEMENTAL routine " + f"'{parent_routine.symbol.name}' because it would change its " + f"semantics.") + + def apply(self, + nodes: Union[Node, List[Node]], + options: Dict =None): + ''' + ''' + node_list = self.get_node_list(nodes) + self.validate(node_list, options=options) + super().apply(node_list, options=options) + + # If we've added profiling calipers to a pure routine then it is + # no longer pure. + parent_routine = node_list[0].ancestor(Routine) + if parent_routine and parent_routine.symbol.is_pure: + parent_routine.symbol.is_pure = False From 1061dce1103d6eb3a888e88788fe78c5b78ddec5 Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Fri, 13 Jun 2025 15:50:53 +0100 Subject: [PATCH 5/8] #3032 move changes into base psy_data_trans.py --- .../psyir/transformations/psy_data_trans.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/psyclone/psyir/transformations/psy_data_trans.py b/src/psyclone/psyir/transformations/psy_data_trans.py index 2672aee190..059bc29a75 100644 --- a/src/psyclone/psyir/transformations/psy_data_trans.py +++ b/src/psyclone/psyir/transformations/psy_data_trans.py @@ -201,6 +201,8 @@ def validate(self, nodes, options=None): :raises TransformationError: if there will be a name clash between \ any existing symbols and those that must be imported from the \ appropriate PSyData library. + :raises TransformationError: if the target nodes are within an + ELEMENTAL routine. ''' # pylint: disable=too-many-branches @@ -263,6 +265,13 @@ def validate(self, nodes, options=None): except KeyError: pass + parent_routine = node_list[0].ancestor(Routine) + if parent_routine and parent_routine.symbol.is_elemental: + raise TransformationError( + f"Cannot add PSyData calls inside ELEMENTAL routine " + f"'{parent_routine.symbol.name}' because it would change its " + f"semantics.") + super().validate(node_list, options) # ------------------------------------------------------------------------ @@ -272,6 +281,9 @@ def apply(self, nodes, options=None): schedule - i.e. enclose the specified Nodes in the schedule within a single PSyData region. + Note that if the nodes are within a routine that previously had the + `pure` attribute, this attribute is removed. + :param nodes: can be a single node or a list of nodes. :type nodes: :py:obj:`psyclone.psyir.nodes.Node` or list of \ :py:obj:`psyclone.psyir.nodes.Node` @@ -314,6 +326,12 @@ def apply(self, nodes, options=None): node_list, symbol_table=table, options=options) parent.addchild(psy_data_node, position) + # If we've added PSyData calls to a pure routine then it is + # no longer pure. + parent_routine = node_list[0].ancestor(Routine) + if parent_routine and parent_routine.symbol.is_pure: + parent_routine.symbol.is_pure = False + # ============================================================================= # For AutoAPI documentation generation From 49b40a80d2243b94820ccc4969ba850d62e3cd6b Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Fri, 13 Jun 2025 15:51:19 +0100 Subject: [PATCH 6/8] #3032 revert profile_trans --- .../psyir/transformations/profile_trans.py | 39 +------------------ 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/src/psyclone/psyir/transformations/profile_trans.py b/src/psyclone/psyir/transformations/profile_trans.py index eb3b63e861..fd34f98c2a 100644 --- a/src/psyclone/psyir/transformations/profile_trans.py +++ b/src/psyclone/psyir/transformations/profile_trans.py @@ -38,9 +38,7 @@ '''This module provides the Profile transformation. ''' -from typing import Dict, List, Union - -from psyclone.psyir.nodes import Node, Return, ProfileNode, Routine +from psyclone.psyir.nodes import Return, ProfileNode from psyclone.psyir.transformations.psy_data_trans import PSyDataTrans @@ -77,38 +75,3 @@ class ProfileTrans(PSyDataTrans): def __init__(self): super().__init__(ProfileNode) - - def validate(self, - nodes: Union[Node, List[Node]], - options: Dict =None): - ''' - :param nodes: a node or list of nodes to be instrumented with - profiling calls. - - :raises TransformationError: if the target nodes are within an - ELEMENTAL routine. - ''' - node_list = self.get_node_list(nodes) - super().validate(node_list, options=options) - - parent_routine = node_list[0].ancestor(Routine) - if parent_routine and parent_routine.symbol.is_elemental: - raise TransformationError( - f"Cannot add profiling calipers inside ELEMENTAL routine " - f"'{parent_routine.symbol.name}' because it would change its " - f"semantics.") - - def apply(self, - nodes: Union[Node, List[Node]], - options: Dict =None): - ''' - ''' - node_list = self.get_node_list(nodes) - self.validate(node_list, options=options) - super().apply(node_list, options=options) - - # If we've added profiling calipers to a pure routine then it is - # no longer pure. - parent_routine = node_list[0].ancestor(Routine) - if parent_routine and parent_routine.symbol.is_pure: - parent_routine.symbol.is_pure = False From 33b62d85331eb11e00e9acd2664e3dda28f3c45d Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Mon, 16 Jun 2025 12:23:36 +0100 Subject: [PATCH 7/8] #3032 add tests for coverage --- .../transformations/psy_data_trans_test.py | 66 +++++++++++++++++-- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/src/psyclone/tests/psyir/transformations/psy_data_trans_test.py b/src/psyclone/tests/psyir/transformations/psy_data_trans_test.py index 3ca7f38597..ef36d49eaf 100644 --- a/src/psyclone/tests/psyir/transformations/psy_data_trans_test.py +++ b/src/psyclone/tests/psyir/transformations/psy_data_trans_test.py @@ -40,10 +40,10 @@ from psyclone.configuration import Config from psyclone.errors import InternalError -from psyclone.psyir.nodes import PSyDataNode -from psyclone.psyir.transformations import (ExtractTrans, PSyDataTrans, - ReadOnlyVerifyTrans, - TransformationError) +from psyclone.psyir.nodes import Assignment, Loop, PSyDataNode, Routine +from psyclone.psyir.transformations import ( + ExtractTrans, OMPLoopTrans, PSyDataTrans, + ReadOnlyVerifyTrans, TransformationError) from psyclone.tests.utilities import get_invoke @@ -85,7 +85,44 @@ def test_psy_data_trans_basic(): children[0] is node -# ----------------------------------------------------------------------------- +def test_psy_data_trans_validate_not_inside_loop_directive(fortran_reader): + ''' + Check that the transformation refuses to add caliper nodes between + a loop-directive and the associated loop. + + ''' + otrans = OMPLoopTrans() + psytrans = PSyDataTrans() + psyir = fortran_reader.psyir_from_source('''\ + subroutine a_test() + integer :: i, va(10) + do i = 1, 10 + va(i) = 5 + end do + end subroutine a_test''') + loop = psyir.walk(Loop)[0] + otrans.apply(loop) + with pytest.raises(TransformationError) as err: + psytrans.validate(loop) + assert ("A PSyData node cannot be inserted between an OpenMP/ACC " + "directive and the loop(s)" in str(err.value)) + + +def test_psy_data_trans_validate_no_elemental(fortran_reader): + '''Check that the transformation refuses to act on an elemental routine.''' + data_trans = PSyDataTrans() + psyir = fortran_reader.psyir_from_source('''\ + elemental real function a_test(var) + real, intent(in) :: var + a_test = var*var + end function a_test''') + assign = psyir.walk(Assignment)[0] + with pytest.raises(TransformationError) as err: + data_trans.validate(assign) + assert ("Cannot add PSyData calls inside ELEMENTAL routine 'a_test' " + "because it would change its semantics" in str(err.value)) + + def test_class_definitions(fortran_writer): '''Tests if the class-prefix can be set and behaves as expected. ''' @@ -204,3 +241,22 @@ def test_trans_with_shape_function(monkeypatch, fortran_reader, out = fortran_writer(psyir) assert 'PreDeclareVariable("dummy", dummy)' in out assert 'ProvideVariable("dummy", dummy)' in out + + +def test_psy_data_trans_remove_pure(fortran_reader): + ''' + Test that applying the transformation to a pure routine causes that + attribute to be removed. + + ''' + psyir = fortran_reader.psyir_from_source('''\ + pure subroutine so_clean(var) + integer, intent(inout) :: var + var = var*var + end subroutine so_clean + ''') + routine = psyir.walk(Routine)[0] + assert routine.symbol.is_pure + psytrans = PSyDataTrans() + psytrans.apply(routine.children) + assert not routine.symbol.is_pure From 4d580a470c10f177505a5438d490427a0e2789e5 Mon Sep 17 00:00:00 2001 From: Sergi Siso Date: Thu, 3 Jul 2025 14:57:02 +0100 Subject: [PATCH 8/8] #3032 Update changelog --- changelog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/changelog b/changelog index 134804dad7..795583f6b8 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,7 @@ + 46) PR 3033 for #3031 and #3032. Prevent inserting profiling callipers to + NEMO functions and any elemental function. Also remove pure attribute when + inserting psy_data wrappers. + 45) PR #2948 for #2950. Update extraction transformation to perform extraction and driver creation at lowering