| 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, Subroutine |
| 11 |
from loki.frontend import available_frontends, OMNI |
| 12 |
from loki.ir import nodes as ir, FindNodes |
| 13 |
|
| 14 |
from loki.transformations.sanitise import ( |
| 15 |
SequenceAssociationTransformation, do_resolve_sequence_association |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 20 |
@pytest.mark.parametrize('use_trafo', [False, True]) |
| 21 |
def test_resolve_sequence_assocaition_scalar_notation(tmp_path, frontend, use_trafo): |
| 22 |
fcode = """ |
| 23 |
module mod_a |
| 24 |
implicit none |
| 25 |
|
| 26 |
type type_b |
| 27 |
integer :: c |
| 28 |
integer :: d |
| 29 |
end type type_b |
| 30 |
|
| 31 |
type type_a |
| 32 |
type(type_b) :: b |
| 33 |
end type type_a |
| 34 |
|
| 35 |
contains |
| 36 |
|
| 37 |
subroutine main() |
| 38 |
type(type_a) :: a |
| 39 |
integer :: k, m, n |
| 40 |
|
| 41 |
real :: array(10,10) |
| 42 |
real :: another(1:8, 2) |
| 43 |
|
| 44 |
! Test array with scalar dimension |
| 45 |
call sub_x(array(1, 1), 1) |
| 46 |
call sub_x(array(2, 2), 2) |
| 47 |
call sub_x(array(m, 1), k) |
| 48 |
call sub_x(array(m-1, 1), k-1) |
| 49 |
call sub_x(array(a%b%c, 1), a%b%d) |
| 50 |
|
| 51 |
! Test array with range dimension |
| 52 |
call sub_x(another(1, 1), 1) |
| 53 |
call sub_x(another(2, 2), 2) |
| 54 |
call sub_x(another(m, 1), k) |
| 55 |
|
| 56 |
contains |
| 57 |
|
| 58 |
subroutine sub_x(array, k) |
| 59 |
integer, intent(in) :: k |
| 60 |
real, intent(in) :: array(k:n) |
| 61 |
|
| 62 |
end subroutine sub_x |
| 63 |
|
| 64 |
end subroutine main |
| 65 |
|
| 66 |
end module mod_a |
| 67 |
""".strip() |
| 68 |
|
| 69 |
module = Module.from_source(fcode, frontend=frontend, xmods=[tmp_path]) |
| 70 |
routine = module['main'] |
| 71 |
|
| 72 |
if use_trafo: |
| 73 |
SequenceAssociationTransformation( |
| 74 |
resolve_sequence_associations=True |
| 75 |
).apply(routine) |
| 76 |
else: |
| 77 |
do_resolve_sequence_association(routine) |
| 78 |
|
| 79 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 80 |
assert all(c.name == 'sub_x' for c in calls) |
| 81 |
|
| 82 |
assert calls[0].arguments == ('array(1:10, 1)', 1) |
| 83 |
assert calls[1].arguments == ('array(2:10, 2)', 2) |
| 84 |
assert calls[2].arguments == ('array(m:10, 1)', 'k') |
| 85 |
assert calls[3].arguments == ('array(m - 1:10, 1)', 'k - 1') |
| 86 |
assert calls[4].arguments == ('array(a%b%c:10, 1)', 'a%b%d') |
| 87 |
|
| 88 |
assert calls[5].arguments == ('another(1:8, 1)', 1) |
| 89 |
assert calls[6].arguments == ('another(2:8, 2)', 2) |
| 90 |
assert calls[7].arguments == ('another(m:8, 1)', 'k') |
| 91 |
|
| 92 |
|
| 93 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 94 |
skip=[(OMNI, 'OMNI does not like not knowing shapes!')] |
| 95 |
)) |
| 96 |
def test_resolve_sequence_assocaition_missing_shape(frontend): |
| 97 |
fcode = """ |
| 98 |
subroutine test_resolve_seq_assoc_no_shape(a, n) |
| 99 |
use ricks_module, only: my_type |
| 100 |
implicit none |
| 101 |
|
| 102 |
type(my_type), intent(inout) :: a |
| 103 |
integer, intent(in) :: n |
| 104 |
|
| 105 |
! Test array with no known shape |
| 106 |
call sub_x(a%a(1, 1), 1) |
| 107 |
call sub_x(a%b(1), 1) |
| 108 |
call sub_x(a%c, 1) |
| 109 |
|
| 110 |
contains |
| 111 |
|
| 112 |
subroutine sub_x(array, k) |
| 113 |
integer, intent(in) :: k |
| 114 |
real, intent(in) :: array(k:n) |
| 115 |
|
| 116 |
end subroutine sub_x |
| 117 |
end subroutine test_resolve_seq_assoc_no_shape |
| 118 |
""" |
| 119 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 120 |
|
| 121 |
do_resolve_sequence_association(routine) |
| 122 |
|
| 123 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 124 |
assert all(c.name == 'sub_x' for c in calls) |
| 125 |
|
| 126 |
assert calls[0].arguments == ('a%a(:, 1)', 1) |
| 127 |
assert calls[1].arguments == ('a%b(:)', 1) |
| 128 |
assert calls[2].arguments == ('a%c', 1) |