| 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 BasicType, Subroutine |
| 11 |
from loki.expression import symbols as sym |
| 12 |
from loki.frontend import available_frontends, OMNI |
| 13 |
from loki.ir import nodes as ir, FindNodes |
| 14 |
|
| 15 |
from loki.transformations.sanitise import ( |
| 16 |
do_resolve_associates, do_merge_associates, |
| 17 |
ResolveAssociatesTransformer, AssociatesTransformation |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 22 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 23 |
)) |
| 24 |
def test_transform_associates_simple(frontend): |
| 25 |
""" |
| 26 |
Test association resolver on simple cases. |
| 27 |
""" |
| 28 |
fcode = """ |
| 29 |
subroutine transform_associates_simple |
| 30 |
use some_module, only: some_obj |
| 31 |
implicit none |
| 32 |
|
| 33 |
real :: local_var |
| 34 |
|
| 35 |
associate (a => some_obj%a) |
| 36 |
local_var = a(:) |
| 37 |
end associate |
| 38 |
end subroutine transform_associates_simple |
| 39 |
""" |
| 40 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 41 |
|
| 42 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 1 |
| 43 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 1 |
| 44 |
assign = FindNodes(ir.Assignment).visit(routine.body)[0] |
| 45 |
assert assign.rhs == 'a(:)' and 'some_obj' not in assign.rhs |
| 46 |
assert assign.rhs.type.dtype == BasicType.DEFERRED |
| 47 |
|
| 48 |
# Now apply the association resolver |
| 49 |
do_resolve_associates(routine) |
| 50 |
|
| 51 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 0 |
| 52 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 1 |
| 53 |
assign = FindNodes(ir.Assignment).visit(routine.body)[0] |
| 54 |
assert assign.rhs == 'some_obj%a(:)' |
| 55 |
assert assign.rhs.parent == 'some_obj' |
| 56 |
assert assign.rhs.type.dtype == BasicType.DEFERRED |
| 57 |
assert assign.rhs.scope == routine |
| 58 |
assert assign.rhs.parent.scope == routine |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 62 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 63 |
)) |
| 64 |
def test_transform_associates_nested(frontend): |
| 65 |
""" |
| 66 |
Test association resolver with deeply nested associates. |
| 67 |
""" |
| 68 |
fcode = """ |
| 69 |
subroutine transform_associates_nested |
| 70 |
use some_module, only: some_obj |
| 71 |
implicit none |
| 72 |
|
| 73 |
real :: rick |
| 74 |
|
| 75 |
associate (never => some_obj%never) |
| 76 |
associate (gonna => never%gonna) |
| 77 |
associate (a => gonna%give%you%up) |
| 78 |
rick = a |
| 79 |
end associate |
| 80 |
end associate |
| 81 |
end associate |
| 82 |
end subroutine transform_associates_nested |
| 83 |
""" |
| 84 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 85 |
|
| 86 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 3 |
| 87 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 1 |
| 88 |
assign = FindNodes(ir.Assignment).visit(routine.body)[0] |
| 89 |
assert assign.lhs == 'rick' and assign.rhs == 'a' |
| 90 |
assert assign.rhs.type.dtype == BasicType.DEFERRED |
| 91 |
|
| 92 |
# Now apply the association resolver |
| 93 |
do_resolve_associates(routine) |
| 94 |
|
| 95 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 0 |
| 96 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 1 |
| 97 |
assign = FindNodes(ir.Assignment).visit(routine.body)[0] |
| 98 |
assert assign.rhs == 'some_obj%never%gonna%give%you%up' |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 102 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 103 |
)) |
| 104 |
def test_transform_associates_ambiguous(frontend): |
| 105 |
""" |
| 106 |
Test association resolver with some ambiguity. |
| 107 |
""" |
| 108 |
fcode = """ |
| 109 |
subroutine transform_associates_ambiguous |
| 110 |
use some_module, only: some_obj |
| 111 |
use other_module, only: other_obj |
| 112 |
implicit none |
| 113 |
|
| 114 |
associate(ndim => some_obj%ndim, ndims => some_obj%ndims, nested_obj => other_obj%nested) |
| 115 |
nested_obj%ndim = 2 |
| 116 |
nested_obj%ndims(:) = 5 |
| 117 |
call some_func(nested_obj%ndim) |
| 118 |
end associate |
| 119 |
end subroutine transform_associates_ambiguous |
| 120 |
""" |
| 121 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 122 |
|
| 123 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 1 |
| 124 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 125 |
assert len(assigns) == 2 |
| 126 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 127 |
assert len(calls) == 1 |
| 128 |
assert assigns[0].lhs == 'nested_obj%ndim' and assigns[0].rhs == '2' |
| 129 |
assert assigns[1].lhs == 'nested_obj%ndims(:)' and assigns[1].rhs == '5' |
| 130 |
assert calls[0].arguments[0] == 'nested_obj%ndim' |
| 131 |
|
| 132 |
# Now apply the association resolver |
| 133 |
do_resolve_associates(routine) |
| 134 |
|
| 135 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 0 |
| 136 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 137 |
assert len(assigns) == 2 |
| 138 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 139 |
assert len(calls) == 1 |
| 140 |
assert assigns[0].lhs == 'other_obj%nested%ndim' and assigns[0].rhs == '2' |
| 141 |
assert assigns[1].lhs == 'other_obj%nested%ndims(:)' and assigns[1].rhs == '5' |
| 142 |
assert calls[0].arguments[0] == 'other_obj%nested%ndim' |
| 143 |
|
| 144 |
|
| 145 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 146 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 147 |
)) |
| 148 |
def test_transform_associates_array_call(frontend): |
| 149 |
""" |
| 150 |
Test a neat corner case where a component of an associated array |
| 151 |
is used as a keyword argument in a subroutine call. |
| 152 |
""" |
| 153 |
fcode = """ |
| 154 |
subroutine transform_associates_simple |
| 155 |
use some_module, only: some_obj |
| 156 |
implicit none |
| 157 |
|
| 158 |
integer :: i |
| 159 |
real :: local_var |
| 160 |
real, allocatable :: local_arr(:) |
| 161 |
|
| 162 |
associate (some_array => some_obj%some_array, a => some_obj%a) |
| 163 |
allocate(local_arr(a%n)) |
| 164 |
|
| 165 |
do i=1, 5 |
| 166 |
call another_routine(i, n=some_array(i)%n) |
| 167 |
end do |
| 168 |
end associate |
| 169 |
end subroutine transform_associates_simple |
| 170 |
""" |
| 171 |
|
| 172 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 173 |
|
| 174 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 1 |
| 175 |
assert len(FindNodes(ir.CallStatement).visit(routine.body)) == 1 |
| 176 |
call = FindNodes(ir.CallStatement).visit(routine.body)[0] |
| 177 |
assert call.kwarguments[0][1] == 'some_array(i)%n' |
| 178 |
assert call.kwarguments[0][1].type.dtype == BasicType.DEFERRED |
| 179 |
assert routine.variable_map['local_arr'].type.shape == (':',) |
| 180 |
allocs = FindNodes(ir.Allocation).visit(routine.body) |
| 181 |
assert allocs[0].variables[0].dimensions == ('a%n',) |
| 182 |
|
| 183 |
# Now apply the association resolver |
| 184 |
do_resolve_associates(routine) |
| 185 |
|
| 186 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 0 |
| 187 |
assert len(FindNodes(ir.CallStatement).visit(routine.body)) == 1 |
| 188 |
call = FindNodes(ir.CallStatement).visit(routine.body)[0] |
| 189 |
assert call.kwarguments[0][1] == 'some_obj%some_array(i)%n' |
| 190 |
assert call.kwarguments[0][1].scope == routine |
| 191 |
assert call.kwarguments[0][1].type.dtype == BasicType.DEFERRED |
| 192 |
|
| 193 |
# Test that symbols in the allocation have been resolved |
| 194 |
assert routine.variable_map['local_arr'].type.shape == (':',) |
| 195 |
allocs = FindNodes(ir.Allocation).visit(routine.body) |
| 196 |
assert allocs[0].variables[0].dimensions == ('some_obj%a%n',) |
| 197 |
|
| 198 |
|
| 199 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 200 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 201 |
)) |
| 202 |
def test_transform_associates_array_slices(frontend): |
| 203 |
""" |
| 204 |
Test the resolution of associated array slices. |
| 205 |
""" |
| 206 |
fcode = """ |
| 207 |
subroutine transform_associates_slices(arr2d, arr3d) |
| 208 |
use some_module, only: some_obj, another_routine |
| 209 |
implicit none |
| 210 |
real, intent(inout) :: arr2d(:,:), arr3d(:,:,:) |
| 211 |
integer :: i, j |
| 212 |
integer, parameter :: idx_a = 2 |
| 213 |
integer, parameter :: idx_c = 3 |
| 214 |
|
| 215 |
associate (a => arr2d(:, 1), b=>arr2d(:, idx_a), & |
| 216 |
& c => arr3d(:,:,idx_c), idx => some_obj%idx) |
| 217 |
b(:) = 42.0 |
| 218 |
do i=1, 5 |
| 219 |
a(i) = b(i+2) |
| 220 |
call another_routine(i, a(2:4), b) |
| 221 |
do j=1, 7 |
| 222 |
c(i, j) = c(i, j) + b(j) |
| 223 |
c(i, idx) = c(i, idx) + 42.0 |
| 224 |
end do |
| 225 |
end do |
| 226 |
end associate |
| 227 |
end subroutine transform_associates_slices |
| 228 |
""" |
| 229 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 230 |
|
| 231 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 1 |
| 232 |
assert len(FindNodes(ir.CallStatement).visit(routine.body)) == 1 |
| 233 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 234 |
assert len(assigns) == 4 |
| 235 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 236 |
assert len(calls) == 1 |
| 237 |
assert calls[0].arguments[1] == 'a(2:4)' |
| 238 |
assert calls[0].arguments[2] == 'b' |
| 239 |
|
| 240 |
# Now apply the association resolver |
| 241 |
do_resolve_associates(routine) |
| 242 |
|
| 243 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 0 |
| 244 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 245 |
assert len(assigns) == 4 |
| 246 |
assert assigns[0].lhs == 'arr2d(:, idx_a)' |
| 247 |
assert assigns[1].lhs == 'arr2d(i, 1)' |
| 248 |
assert assigns[1].rhs == 'arr2d(i+2, idx_a)' |
| 249 |
assert assigns[2].lhs == 'arr3d(i, j, idx_c)' |
| 250 |
assert assigns[2].rhs == 'arr3d(i, j, idx_c) + arr2d(j, idx_a)' |
| 251 |
assert assigns[3].lhs == 'arr3d(i, some_obj%idx, idx_c)' |
| 252 |
assert assigns[3].rhs == 'arr3d(i, some_obj%idx, idx_c) + 42.0' |
| 253 |
|
| 254 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 255 |
assert len(calls) == 1 |
| 256 |
assert calls[0].arguments[1] == 'arr2d(2:4, 1)' |
| 257 |
assert calls[0].arguments[2] == 'arr2d(:, idx_a)' |
| 258 |
|
| 259 |
|
| 260 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 261 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 262 |
)) |
| 263 |
def test_transform_associates_nested_conditional(frontend): |
| 264 |
""" |
| 265 |
Test association resolver when associate is nested into a conditional. |
| 266 |
""" |
| 267 |
fcode = """ |
| 268 |
subroutine transform_associates_nested_conditional |
| 269 |
use some_module, only: some_obj, some_flag |
| 270 |
implicit none |
| 271 |
|
| 272 |
real :: local_var |
| 273 |
|
| 274 |
if (some_flag) then |
| 275 |
local_var = 0. |
| 276 |
else |
| 277 |
! Other nodes before the associate |
| 278 |
! This one, too |
| 279 |
|
| 280 |
! And this one |
| 281 |
associate (a => some_obj%a) |
| 282 |
local_var = a |
| 283 |
! And a conditional which may inject a tuple nesting in the IR |
| 284 |
if (local_var > 10.) then |
| 285 |
local_var = 10. |
| 286 |
end if |
| 287 |
end associate |
| 288 |
! And nodes after it |
| 289 |
|
| 290 |
! like this |
| 291 |
end if |
| 292 |
end subroutine transform_associates_nested_conditional |
| 293 |
""" |
| 294 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 295 |
|
| 296 |
assert len(FindNodes(ir.Conditional).visit(routine.body)) == 2 |
| 297 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 1 |
| 298 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 3 |
| 299 |
assign = FindNodes(ir.Assignment).visit(routine.body)[1] |
| 300 |
assert assign.rhs == 'a' and 'some_obj' not in assign.rhs |
| 301 |
assert assign.rhs.type.dtype == BasicType.DEFERRED |
| 302 |
|
| 303 |
# Now apply the association resolver |
| 304 |
do_resolve_associates(routine) |
| 305 |
|
| 306 |
assert len(FindNodes(ir.Conditional).visit(routine.body)) == 2 |
| 307 |
assert len(FindNodes(ir.Associate).visit(routine.body)) == 0 |
| 308 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 3 |
| 309 |
assign = FindNodes(ir.Assignment).visit(routine.body)[1] |
| 310 |
assert assign.rhs == 'some_obj%a' |
| 311 |
assert assign.rhs.parent == 'some_obj' |
| 312 |
assert assign.rhs.type.dtype == BasicType.DEFERRED |
| 313 |
assert assign.rhs.scope == routine |
| 314 |
assert assign.rhs.parent.scope == routine |
| 315 |
|
| 316 |
|
| 317 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 318 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 319 |
)) |
| 320 |
def test_transform_associates_partial_body(frontend): |
| 321 |
""" |
| 322 |
Test resolving associated symbols, but only for a part of an |
| 323 |
associate's body. |
| 324 |
""" |
| 325 |
fcode = """ |
| 326 |
subroutine transform_associates_partial |
| 327 |
use some_module, only: some_obj |
| 328 |
implicit none |
| 329 |
|
| 330 |
integer :: i |
| 331 |
real :: local_var |
| 332 |
|
| 333 |
associate (a=>some_obj%a, b=>some_obj%b) |
| 334 |
local_var = a(1) |
| 335 |
|
| 336 |
do i=1, some_obj%n |
| 337 |
a(i) = a(i) + 1. |
| 338 |
b(i) = b(i) + 1. |
| 339 |
end do |
| 340 |
end associate |
| 341 |
end subroutine transform_associates_partial |
| 342 |
""" |
| 343 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 344 |
|
| 345 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 3 |
| 346 |
loops = FindNodes(ir.Loop).visit(routine.body) |
| 347 |
assert len(loops) == 1 |
| 348 |
|
| 349 |
transformer = ResolveAssociatesTransformer(inplace=True) |
| 350 |
transformer.visit(loops[0]) |
| 351 |
|
| 352 |
# Check that associated symbols have been resolved in loop body only |
| 353 |
assert len(FindNodes(ir.Loop).visit(routine.body)) == 1 |
| 354 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 355 |
assert len(assigns) == 3 |
| 356 |
assert assigns[0].lhs == 'local_var' |
| 357 |
assert assigns[0].rhs == 'a(1)' |
| 358 |
assert assigns[1].lhs == 'some_obj%a(i)' |
| 359 |
assert assigns[1].rhs == 'some_obj%a(i) + 1.' |
| 360 |
assert assigns[2].lhs == 'some_obj%b(i)' |
| 361 |
assert assigns[2].rhs == 'some_obj%b(i) + 1.' |
| 362 |
|
| 363 |
|
| 364 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 365 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 366 |
)) |
| 367 |
def test_transform_associates_start_depth(frontend): |
| 368 |
""" |
| 369 |
Test resolving associated symbols, but only for a part of an |
| 370 |
associate's body. |
| 371 |
""" |
| 372 |
fcode = """ |
| 373 |
subroutine transform_associates_partial |
| 374 |
use some_module, only: some_obj |
| 375 |
implicit none |
| 376 |
|
| 377 |
integer :: i |
| 378 |
real :: local_var |
| 379 |
|
| 380 |
associate (a=>some_obj%a, b=>some_obj%b) |
| 381 |
associate (c=>a%b, d=>b%d) |
| 382 |
local_var = a(1) |
| 383 |
|
| 384 |
do i=1, some_obj%n |
| 385 |
c(i) = c(i) + 1. |
| 386 |
d(i) = d(i) + 1. |
| 387 |
end do |
| 388 |
end associate |
| 389 |
end associate |
| 390 |
end subroutine transform_associates_partial |
| 391 |
""" |
| 392 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 393 |
|
| 394 |
assert len(FindNodes(ir.Assignment).visit(routine.body)) == 3 |
| 395 |
loops = FindNodes(ir.Loop).visit(routine.body) |
| 396 |
assert len(loops) == 1 |
| 397 |
|
| 398 |
# Resolve all expect the outermost associate block |
| 399 |
do_resolve_associates(routine, start_depth=1) |
| 400 |
|
| 401 |
# Check that associated symbols have been resolved in loop body only |
| 402 |
assert len(FindNodes(ir.Loop).visit(routine.body)) == 1 |
| 403 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 404 |
assert len(assigns) == 3 |
| 405 |
assert assigns[0].lhs == 'local_var' |
| 406 |
assert assigns[0].rhs == 'a(1)' |
| 407 |
assert assigns[1].lhs == 'a%b(i)' |
| 408 |
assert assigns[1].rhs == 'a%b(i) + 1.' |
| 409 |
assert assigns[2].lhs == 'b%d(i)' |
| 410 |
assert assigns[2].rhs == 'b%d(i) + 1.' |
| 411 |
|
| 412 |
|
| 413 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 414 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 415 |
)) |
| 416 |
def test_merge_associates_nested(frontend): |
| 417 |
""" |
| 418 |
Test association merging for nested mappings. |
| 419 |
""" |
| 420 |
fcode = """ |
| 421 |
subroutine merge_associates_simple(base) |
| 422 |
use some_module, only: some_type |
| 423 |
implicit none |
| 424 |
|
| 425 |
type(some_type), intent(inout) :: base |
| 426 |
integer :: i |
| 427 |
real :: local_var |
| 428 |
|
| 429 |
associate(a => base%a) |
| 430 |
associate(b => base%other%symbol) |
| 431 |
associate(d => base%other%symbol%really%deep, & |
| 432 |
& a => base%a, c => a%more) |
| 433 |
do i=1, 5 |
| 434 |
call another_routine(i, n=b(c)%n) |
| 435 |
|
| 436 |
d(i) = 42.0 |
| 437 |
end do |
| 438 |
end associate |
| 439 |
end associate |
| 440 |
end associate |
| 441 |
end subroutine merge_associates_simple |
| 442 |
""" |
| 443 |
|
| 444 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 445 |
|
| 446 |
assocs = FindNodes(ir.Associate).visit(routine.body) |
| 447 |
assert len(assocs) == 3 |
| 448 |
assert len(assocs[0].associations) == 1 |
| 449 |
assert len(assocs[1].associations) == 1 |
| 450 |
assert len(assocs[2].associations) == 3 |
| 451 |
|
| 452 |
# Move associate mapping around |
| 453 |
do_merge_associates(routine, max_parents=2) |
| 454 |
|
| 455 |
assocs = FindNodes(ir.Associate).visit(routine.body) |
| 456 |
assert len(assocs) == 3 |
| 457 |
assert len(assocs[0].associations) == 2 |
| 458 |
assert assocs[0].associations[0] == ('base%a', 'a') |
| 459 |
assert assocs[0].associations[1] == ('base%other%symbol', 'b') |
| 460 |
assert len(assocs[1].associations) == 1 |
| 461 |
assert assocs[1].associations[0] == ('a%more', 'c') |
| 462 |
assert len(assocs[2].associations) == 1 |
| 463 |
assert assocs[2].associations[0] == ('base%other%symbol%really%deep', 'd') |
| 464 |
|
| 465 |
# Check that body symbols have been rescoped correctly |
| 466 |
call = FindNodes(ir.CallStatement).visit(routine.body)[0] |
| 467 |
b_c_n = call.kwarguments[0][1] # b(c)%n |
| 468 |
assert b_c_n.scope == assocs[0] |
| 469 |
|
| 470 |
|
| 471 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 472 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 473 |
)) |
| 474 |
@pytest.mark.parametrize('merge', [False, True]) |
| 475 |
@pytest.mark.parametrize('resolve', [False, True]) |
| 476 |
def test_associates_transformation(frontend, merge, resolve): |
| 477 |
""" |
| 478 |
Test association merging paired with partial resolution of inner |
| 479 |
scopes via :any:`AssociatesTransformation`. |
| 480 |
""" |
| 481 |
fcode = """ |
| 482 |
subroutine merge_associates_simple(base) |
| 483 |
use some_module, only: some_type |
| 484 |
implicit none |
| 485 |
|
| 486 |
type(some_type), intent(inout) :: base |
| 487 |
integer :: i |
| 488 |
real :: local_var |
| 489 |
|
| 490 |
associate(a => base%a) |
| 491 |
associate(b => base%b) |
| 492 |
associate(c => a%c) |
| 493 |
associate(d => c%d) |
| 494 |
do i=1, 5 |
| 495 |
call another_routine(b(i), c%n) |
| 496 |
|
| 497 |
d(i) = 42.0 |
| 498 |
end do |
| 499 |
end associate |
| 500 |
end associate |
| 501 |
end associate |
| 502 |
end associate |
| 503 |
end subroutine merge_associates_simple |
| 504 |
""" |
| 505 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 506 |
|
| 507 |
AssociatesTransformation( |
| 508 |
resolve_associates=resolve, merge_associates=merge, start_depth=1 |
| 509 |
).apply(routine) |
| 510 |
|
| 511 |
assocs = FindNodes(ir.Associate).visit(routine.body) |
| 512 |
call = FindNodes(ir.CallStatement).visit(routine.body)[0] |
| 513 |
assign = FindNodes(ir.Assignment).visit(routine.body)[0] |
| 514 |
|
| 515 |
if not merge and not resolve: |
| 516 |
assert len(assocs) == 4 |
| 517 |
assert all(len(a.associations) == 1 for a in assocs) |
| 518 |
|
| 519 |
assert call.arguments[0] == 'b(i)' |
| 520 |
assert call.arguments[1] == 'c%n' |
| 521 |
assert assign.lhs == 'd(i)' |
| 522 |
|
| 523 |
if merge and not resolve: |
| 524 |
assert len(assocs) == 4 |
| 525 |
assert assocs[0].associations == (('base%a', 'a'), ('base%b', 'b')) |
| 526 |
assert assocs[1].associations == (('a%c', 'c'), ) |
| 527 |
assert assocs[2].associations == () |
| 528 |
assert assocs[3].associations == (('c%d', 'd'), ) |
| 529 |
|
| 530 |
assert call.arguments[0] == 'b(i)' |
| 531 |
assert call.arguments[1] == 'c%n' |
| 532 |
assert assign.lhs == 'd(i)' |
| 533 |
|
| 534 |
if not merge and resolve: |
| 535 |
assert len(assocs) == 1 |
| 536 |
assert assocs[0].associations == (('base%a', 'a'),) |
| 537 |
|
| 538 |
assert call.arguments[0] == 'base%b(i)' |
| 539 |
assert call.arguments[1] == 'a%c%n' |
| 540 |
assert assign.lhs == 'a%c%d(i)' |
| 541 |
|
| 542 |
if merge and resolve: |
| 543 |
assert len(assocs) == 1 |
| 544 |
assert assocs[0].associations == (('base%a', 'a'), ('base%b', 'b')) |
| 545 |
|
| 546 |
assert call.arguments[0] == 'b(i)' |
| 547 |
assert call.arguments[1] == 'a%c%n' |
| 548 |
assert assign.lhs == 'a%c%d(i)' |
| 549 |
|
| 550 |
|
| 551 |
@pytest.mark.parametrize('frontend', available_frontends( |
| 552 |
skip=[(OMNI, 'OMNI does not handle missing type definitions')] |
| 553 |
)) |
| 554 |
@pytest.mark.parametrize('depth', [0, 1, 2]) |
| 555 |
def test_resolve_associates_stmt_func(frontend, depth): |
| 556 |
""" |
| 557 |
Test scope management for stmt funcs, either as |
| 558 |
:any:`ProcedureSymbol` or :any:`DeferredTypeSymbol`. |
| 559 |
""" |
| 560 |
fcode = """ |
| 561 |
subroutine test_associates_stmt_func(ydcst, a, b) |
| 562 |
use yomcst, only: tcst |
| 563 |
implicit none |
| 564 |
type(tcst), intent(in) :: ydcst |
| 565 |
real(kind=8), intent(inout) :: a, b |
| 566 |
#include "some_stmt.func.h" |
| 567 |
real(kind=8) :: not_an_array |
| 568 |
not_an_array ( x, y ) = x * y |
| 569 |
|
| 570 |
associate(d=>b) |
| 571 |
associate(c=>a) |
| 572 |
associate(RTT=>YDCST%RTT) |
| 573 |
a = not_an_array(RTT, 1.0) + a |
| 574 |
b = some_stmt_func(RTT, 1.0) + b |
| 575 |
end associate |
| 576 |
end associate |
| 577 |
end associate |
| 578 |
end subroutine test_associates_stmt_func |
| 579 |
""" |
| 580 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 581 |
|
| 582 |
associates = FindNodes(ir.Associate).visit(routine.body) |
| 583 |
assert len(associates) == 3 |
| 584 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 585 |
assert len(assigns) == 2 |
| 586 |
assert isinstance(assigns[0].rhs.children[0], sym.InlineCall) |
| 587 |
assert assigns[0].rhs.children[0].function.scope == associates[2] |
| 588 |
assert isinstance(assigns[1].rhs.children[0], sym.InlineCall) |
| 589 |
assert assigns[1].rhs.children[0].function.scope == associates[2] |
| 590 |
|
| 591 |
do_resolve_associates(routine, start_depth=depth) |
| 592 |
|
| 593 |
associates = FindNodes(ir.Associate).visit(routine.body) |
| 594 |
assert len(associates) == depth |
| 595 |
|
| 596 |
assigns = FindNodes(ir.Assignment).visit(routine.body) |
| 597 |
# Determine the outer routine or last associate left |
| 598 |
outer_scope = routine if depth == 0 else associates[depth-1] |
| 599 |
assert len(assigns) == 2 |
| 600 |
assert assigns[0].rhs == 'not_an_array(YDCST%RTT, 1.0) + a' |
| 601 |
assert assigns[1].rhs == 'some_stmt_func(YDCST%RTT, 1.0) + b' |
| 602 |
assert isinstance(assigns[0].rhs.children[0], sym.InlineCall) |
| 603 |
assert assigns[0].rhs.children[0].function.scope == outer_scope |
| 604 |
assert isinstance(assigns[1].rhs.children[0], sym.InlineCall) |
| 605 |
assert assigns[1].rhs.children[0].function.scope == outer_scope |
| 606 |
|
| 607 |
# Trigger a full clone, which would fail if scopes are missing |
| 608 |
routine.clone() |