| 1 |
# (C) Copyright 2018- ECMWF. |
| 2 |
# This software is licensed under the terms of the Apache Licence Version 2.0 |
| 3 |
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 4 |
# In applying this licence, ECMWF does not waive the privileges and immunities |
| 5 |
# granted to it by virtue of its status as an intergovernmental organisation |
| 6 |
# nor does it submit to any jurisdiction. |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from loki import Module |
| 11 |
from loki.frontend import available_frontends |
| 12 |
from loki.ir import nodes as ir, FindNodes |
| 13 |
|
| 14 |
from loki.transformations.sanitise import ( |
| 15 |
SanitiseTransformation, SanitisePipeline |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 20 |
@pytest.mark.parametrize('resolve_associate', [True, False]) |
| 21 |
@pytest.mark.parametrize('resolve_sequence', [True, False]) |
| 22 |
def test_transformation_sanitise(frontend, resolve_associate, resolve_sequence, tmp_path): |
| 23 |
""" |
| 24 |
Test that the selective dispatch of the sanitisations works. |
| 25 |
""" |
| 26 |
|
| 27 |
fcode = """ |
| 28 |
module test_transformation_sanitise_mod |
| 29 |
implicit none |
| 30 |
|
| 31 |
type rick |
| 32 |
real :: scalar |
| 33 |
end type rick |
| 34 |
contains |
| 35 |
|
| 36 |
subroutine test_transformation_sanitise(a, dave) |
| 37 |
real, intent(inout) :: a(3) |
| 38 |
type(rick), intent(inout) :: dave |
| 39 |
|
| 40 |
associate(scalar => dave%scalar) |
| 41 |
scalar = a(1) + a(2) |
| 42 |
|
| 43 |
call vadd(a(1), 2.0, 3) |
| 44 |
end associate |
| 45 |
|
| 46 |
contains |
| 47 |
subroutine vadd(x, y, n) |
| 48 |
real, intent(inout) :: x(n) |
| 49 |
real, intent(inout) :: y |
| 50 |
integer, intent(in) :: n |
| 51 |
|
| 52 |
x = x + 2.0 |
| 53 |
end subroutine vadd |
| 54 |
end subroutine test_transformation_sanitise |
| 55 |
end module test_transformation_sanitise_mod |
| 56 |
""" |
| 57 |
module = Module.from_source(fcode, frontend=frontend, xmods=[tmp_path]) |
| 58 |
routine = module['test_transformation_sanitise'] |
| 59 |
|
| 60 |
assoc = FindNodes(ir.Associate).visit(routine.body) |
| 61 |
assert len(assoc) == 1 |
| 62 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 63 |
assert len(calls) == 1 |
| 64 |
assert calls[0].arguments[0] == 'a(1)' |
| 65 |
|
| 66 |
trafo = SanitiseTransformation( |
| 67 |
resolve_associate_mappings=resolve_associate, |
| 68 |
resolve_sequence_association=resolve_sequence, |
| 69 |
) |
| 70 |
trafo.apply(routine) |
| 71 |
|
| 72 |
assoc = FindNodes(ir.Associate).visit(routine.body) |
| 73 |
assert len(assoc) == 0 if resolve_associate else 1 |
| 74 |
|
| 75 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 76 |
assert len(calls) == 1 |
| 77 |
assert calls[0].arguments[0] == 'a(1:3)' if resolve_sequence else 'a(1)' |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 81 |
@pytest.mark.parametrize('substitute_expressions', [True, False]) |
| 82 |
@pytest.mark.parametrize('resolve_associates', [True, False]) |
| 83 |
@pytest.mark.parametrize('resolve_sequence_associations', [True, False]) |
| 84 |
def test_sanitise_pipeline( |
| 85 |
tmp_path, frontend, substitute_expressions, |
| 86 |
resolve_associates, resolve_sequence_associations |
| 87 |
): |
| 88 |
""" |
| 89 |
Test the agglomerated :any:`SanitisePipeline` with different settings. |
| 90 |
""" |
| 91 |
fcode = """ |
| 92 |
module test_sanitise_pipeline_mod |
| 93 |
implicit none |
| 94 |
|
| 95 |
type rick |
| 96 |
real :: scalar |
| 97 |
end type rick |
| 98 |
contains |
| 99 |
|
| 100 |
subroutine test_pipeline_sanitise(n, a, dave) |
| 101 |
integer, intent(in) :: n |
| 102 |
real, intent(inout) :: a(n+1) |
| 103 |
type(rick), intent(inout) :: dave |
| 104 |
|
| 105 |
associate(scalar => dave%scalar) |
| 106 |
scalar = a(1) + a(2) |
| 107 |
|
| 108 |
call vadd(a(1), 2.0, n+1) |
| 109 |
end associate |
| 110 |
|
| 111 |
contains |
| 112 |
subroutine vadd(x, y, n) |
| 113 |
real, intent(inout) :: x(n) |
| 114 |
real, intent(inout) :: y |
| 115 |
integer, intent(in) :: n |
| 116 |
|
| 117 |
x = x + 2.0 |
| 118 |
end subroutine vadd |
| 119 |
end subroutine test_pipeline_sanitise |
| 120 |
end module test_sanitise_pipeline_mod |
| 121 |
""" |
| 122 |
module = Module.from_source(fcode, frontend=frontend, xmods=[tmp_path]) |
| 123 |
routine = module['test_pipeline_sanitise'] |
| 124 |
|
| 125 |
assoc = FindNodes(ir.Associate).visit(routine.body) |
| 126 |
assert len(assoc) == 1 |
| 127 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 128 |
assert len(calls) == 1 |
| 129 |
|
| 130 |
pipeline = SanitisePipeline( |
| 131 |
substitute_expressions=substitute_expressions, |
| 132 |
expression_map={'n + 1': 'n'}, |
| 133 |
resolve_associates=resolve_associates, |
| 134 |
resolve_sequence_associations=resolve_sequence_associations |
| 135 |
) |
| 136 |
pipeline.apply(routine) |
| 137 |
|
| 138 |
assoc = FindNodes(ir.Associate).visit(routine.body) |
| 139 |
assert len(assoc) == 0 if resolve_associates else 1 |
| 140 |
|
| 141 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 142 |
assert len(calls) == 1 |
| 143 |
if resolve_sequence_associations: |
| 144 |
assert calls[0].arguments[0] == 'a(1:n)' if substitute_expressions else 'a(1:n+1)' |
| 145 |
else: |
| 146 |
assert calls[0].arguments[0] == 'a(1)' |