Skip to content

Commit 057b6fb

Browse files
committed
[flang] Fix CONTIGUOUS attribute for construct entities
Currently, the selector of a construct entity (e.g., ASSOCIATE(x=>a(1:20))) is inheriting the CONTIGUOUS attribute from its associated variable even if it has subscripts that make it noncontiguous (a(1:20:2)). Add construct entities to the dynamic contiguity predicate instead. Differential Revision: https://reviews.llvm.org/D145114
1 parent 51b2f21 commit 057b6fb

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

flang/lib/Evaluate/check-expression.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,8 @@ class IsContiguousHelper
765765
// simple contiguity to allow their use in contexts like
766766
// data targets in pointer assignments with remapping.
767767
return true;
768+
} else if (ultimate.has<semantics::AssocEntityDetails>()) {
769+
return Base::operator()(ultimate); // use expr
768770
} else if (semantics::IsPointer(ultimate) ||
769771
semantics::IsAssumedShape(ultimate)) {
770772
return std::nullopt;

flang/lib/Semantics/resolve-names.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6655,8 +6655,8 @@ void ConstructVisitor::SetTypeFromAssociation(Symbol &symbol) {
66556655
// If current selector is a variable, set some of its attributes on symbol.
66566656
void ConstructVisitor::SetAttrsFromAssociation(Symbol &symbol) {
66576657
Attrs attrs{evaluate::GetAttrs(GetCurrentAssociation().selector.expr)};
6658-
symbol.attrs() |= attrs &
6659-
Attrs{Attr::TARGET, Attr::ASYNCHRONOUS, Attr::VOLATILE, Attr::CONTIGUOUS};
6658+
symbol.attrs() |=
6659+
attrs & Attrs{Attr::TARGET, Attr::ASYNCHRONOUS, Attr::VOLATILE};
66606660
if (attrs.test(Attr::POINTER)) {
66616661
SetImplicitAttr(symbol, Attr::TARGET);
66626662
}

flang/test/Evaluate/folding09.f90

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
! RUN: %python %S/test_folding.py %s %flang_fc1
2-
! Test folding of IS_CONTIGUOUS on simply contiguous items (9.5.4)
3-
! When IS_CONTIGUOUS() is constant, it's .TRUE.
2+
! Test folding of IS_CONTIGUOUS
43

54
module m
65
real, target :: hosted(2)
@@ -14,17 +13,39 @@ subroutine test(arr1, arr2, arr3, mat, alloc)
1413
real, intent(in), contiguous :: arr3(:)
1514
real, allocatable :: alloc(:)
1615
real :: scalar
17-
logical, parameter :: test_isc01 = is_contiguous(0)
18-
logical, parameter :: test_isc02 = is_contiguous(scalar)
19-
logical, parameter :: test_isc03 = is_contiguous(scalar + scalar)
20-
logical, parameter :: test_isc04 = is_contiguous([0, 1, 2])
21-
logical, parameter :: test_isc05 = is_contiguous(arr1 + 1.0)
22-
logical, parameter :: test_isc06 = is_contiguous(arr2)
23-
logical, parameter :: test_isc07 = is_contiguous(mat)
24-
logical, parameter :: test_isc08 = is_contiguous(mat(1:10,1))
25-
logical, parameter :: test_isc09 = is_contiguous(arr2(1:10:1))
26-
logical, parameter :: test_isc10 = is_contiguous(arr3)
27-
logical, parameter :: test_isc11 = is_contiguous(f())
28-
logical, parameter :: test_isc12 = is_contiguous(alloc)
16+
integer(kind=merge(1,-1, is_contiguous(0))) t01
17+
integer(kind=merge(1,-1, is_contiguous(scalar))) t02
18+
integer(kind=merge(1,-1, is_contiguous(scalar + scalar))) t03
19+
integer(kind=merge(1,-1, is_contiguous([0, 1, 2]))) t04
20+
integer(kind=merge(1,-1, is_contiguous(arr1 + 1.0))) t05
21+
integer(kind=merge(1,-1, is_contiguous(arr2))) t06
22+
integer(kind=merge(1,-1, is_contiguous(mat))) t07
23+
integer(kind=merge(1,-1, is_contiguous(mat(1:10,1)))) t08
24+
integer(kind=merge(1,-1, is_contiguous(arr2(1:10:1)))) t09
25+
integer(kind=merge(1,-1, .not. is_contiguous(arr2(1:10:2)))) t10
26+
integer(kind=merge(1,-1, is_contiguous(arr3))) t11
27+
integer(kind=merge(1,-1, .not. is_contiguous(arr3(1:10:2)))) t12
28+
integer(kind=merge(1,-1, is_contiguous(f()))) t13
29+
integer(kind=merge(1,-1, is_contiguous(alloc))) t14
30+
associate (x => arr2)
31+
block
32+
integer(kind=merge(1,-1,is_contiguous(x))) n
33+
end block
34+
end associate
35+
associate (x => arr2(1:10:2))
36+
block
37+
integer(kind=merge(1,-1,.not. is_contiguous(x))) n
38+
end block
39+
end associate
40+
associate (x => arr3)
41+
block
42+
integer(kind=merge(1,-1,is_contiguous(x))) n
43+
end block
44+
end associate
45+
associate (x => arr3(1:10:2))
46+
block
47+
integer(kind=merge(1,-1,.not. is_contiguous(x))) n
48+
end block
49+
end associate
2950
end subroutine
3051
end module

flang/test/Lower/HLFIR/associate-construct.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ subroutine associate_var(x)
3232

3333
subroutine associate_pointer(x)
3434
integer, pointer, contiguous :: x(:)
35-
! Check that "y" has the target and contiguous attributes.
35+
! Check that "y" has the target attribute.
3636
associate(y => x)
3737
print *, y
3838
end associate
@@ -44,7 +44,7 @@ subroutine associate_pointer(x)
4444
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
4545
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.ptr<!fir.array<?xi32>>>, index) -> (index, index, index)
4646
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
47-
! CHECK: %[[VAL_7:.*]]:2 = hlfir.declare %[[VAL_3]](%[[VAL_6]]) {fortran_attrs = #fir.var_attrs<contiguous, target>, uniq_name = "_QFassociate_pointerEy"} : (!fir.ptr<!fir.array<?xi32>>, !fir.shapeshift<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.ptr<!fir.array<?xi32>>)
47+
! CHECK: %[[VAL_7:.*]]:2 = hlfir.declare %[[VAL_3]](%[[VAL_6]]) {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFassociate_pointerEy"} : (!fir.ptr<!fir.array<?xi32>>, !fir.shapeshift<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.ptr<!fir.array<?xi32>>)
4848
! CHECK: fir.call @_FortranAioEndIoStatement
4949
! CHECK-NEXT: return
5050

@@ -92,6 +92,6 @@ subroutine associate_pointer_section(x)
9292
! CHECK: %[[VAL_6:.*]] = arith.constant 20 : index
9393
! CHECK: %[[VAL_8:.*]] = hlfir.designate %[[VAL_2]]{{.*}}
9494
! CHECK: %[[VAL_9:.*]] = fir.shape %[[VAL_6]] : (index) -> !fir.shape<1>
95-
! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_8]](%[[VAL_9]]) {fortran_attrs = #fir.var_attrs<contiguous, target>, uniq_name = "_QFassociate_pointer_sectionEy"} : (!fir.ref<!fir.array<20xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<20xi32>>, !fir.ref<!fir.array<20xi32>>)
95+
! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_8]](%[[VAL_9]]) {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFassociate_pointer_sectionEy"} : (!fir.ref<!fir.array<20xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<20xi32>>, !fir.ref<!fir.array<20xi32>>)
9696
! CHECK: fir.call @_FortranAioEndIoStatement
9797
! CHECK-NEXT: return

0 commit comments

Comments
 (0)