Skip to content

Commit a56f37d

Browse files
authored
[flang][Lower] get ultimate symbol when querying if pointer or allocatable (#99528)
This fixes a bug in OpenMP privatisation. The privatised variables are created as though they are host associated clones of the original variables. These privatised variables do not contain the allocatable attribute themselves and so we need to check if the ultimate symbol is allocatable. Having or not having this flag influences whether lowering determines that this is a whole allocatable assignment, which then causes hlfir.assign not to get the realloc flag, which cases the allocatable not to be allocated when it is assigned to (leading to a segfault running the newly added test). I also did the same for pointer variables because I would imagine they could experience the same issue. There is no fallout on tests outside of OpenMP, and the gfortran test suite still passes, so I think this doesn't break host other kinds of host associated symbols.
1 parent 9ba5244 commit a56f37d

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

flang/lib/Lower/Allocatable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,14 +1104,14 @@ void Fortran::lower::associateMutableBox(
11041104
bool Fortran::lower::isWholeAllocatable(const Fortran::lower::SomeExpr &expr) {
11051105
if (const Fortran::semantics::Symbol *sym =
11061106
Fortran::evaluate::UnwrapWholeSymbolOrComponentDataRef(expr))
1107-
return Fortran::semantics::IsAllocatable(*sym);
1107+
return Fortran::semantics::IsAllocatable(sym->GetUltimate());
11081108
return false;
11091109
}
11101110

11111111
bool Fortran::lower::isWholePointer(const Fortran::lower::SomeExpr &expr) {
11121112
if (const Fortran::semantics::Symbol *sym =
11131113
Fortran::evaluate::UnwrapWholeSymbolOrComponentDataRef(expr))
1114-
return Fortran::semantics::IsPointer(*sym);
1114+
return Fortran::semantics::IsPointer(sym->GetUltimate());
11151115
return false;
11161116
}
11171117

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
! RUN: bbc -emit-hlfir -fopenmp -o - %s | FileCheck %s
2+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s | FileCheck %s
3+
4+
program firstprivateallocatable
5+
Integer, Allocatable :: a,u
6+
a = 137
7+
8+
!$omp parallel firstprivate(a,u)
9+
u = a**2
10+
!$omp end parallel
11+
end program
12+
13+
14+
! CHECK-LABEL: func.func @_QQmain()
15+
! [...]
16+
! CHECK: omp.parallel {
17+
! [...]
18+
! CHECK: %[[VAL_50:.*]] = arith.constant 2 : i32
19+
! CHECK: %[[VAL_51:.*]] = math.ipowi %{{.*}}, %[[VAL_50]] : i32
20+
! this is what we are really checking: the hlfir.assign must have realloc so that
21+
! u is allocated when the assignment occurs
22+
! CHECK: hlfir.assign %[[VAL_51]] to %{{.*}}#0 realloc : i32, !fir.ref<!fir.box<!fir.heap<i32>>>
23+
! [...]
24+
! CHECK: omp.terminator
25+
! CHECK: }

flang/test/Lower/OpenMP/wsloop-reduction-allocatable-array-minmax.f90

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ program reduce15
244244
! CHECK: %[[VAL_85:.*]] = arith.select %[[VAL_84]], %[[VAL_76]], %[[VAL_83]] : i32
245245
! CHECK: hlfir.yield_element %[[VAL_85]] : i32
246246
! CHECK: }
247-
! CHECK: %[[VAL_86:.*]] = fir.load %[[VAL_62]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
248-
! CHECK: hlfir.assign %[[VAL_68]] to %[[VAL_86]] : !hlfir.expr<?xi32>, !fir.box<!fir.heap<!fir.array<?xi32>>>
247+
! CHECK: hlfir.assign %[[VAL_68]] to %[[VAL_62]]#0 realloc : !hlfir.expr<?xi32>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
249248
! CHECK: hlfir.destroy %[[VAL_68]] : !hlfir.expr<?xi32>
250249
! CHECK: omp.yield
251250
! CHECK: }
@@ -288,8 +287,7 @@ program reduce15
288287
! CHECK: %[[VAL_117:.*]] = arith.select %[[VAL_116]], %[[VAL_108]], %[[VAL_115]] : i32
289288
! CHECK: hlfir.yield_element %[[VAL_117]] : i32
290289
! CHECK: }
291-
! CHECK: %[[VAL_118:.*]] = fir.load %[[VAL_94]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
292-
! CHECK: hlfir.assign %[[VAL_100]] to %[[VAL_118]] : !hlfir.expr<?xi32>, !fir.box<!fir.heap<!fir.array<?xi32>>>
290+
! CHECK: hlfir.assign %[[VAL_100]] to %[[VAL_94]]#0 realloc : !hlfir.expr<?xi32>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
293291
! CHECK: hlfir.destroy %[[VAL_100]] : !hlfir.expr<?xi32>
294292
! CHECK: omp.yield
295293
! CHECK: }

flang/test/Lower/OpenMP/wsloop-reduction-allocatable.f90

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ program reduce
8585
! CHECK: %[[VAL_16:.*]]:2 = hlfir.declare %[[VAL_14]] {fortran_attrs = {{.*}}<allocatable>, uniq_name = "_QFEr"} : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> (!fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.heap<i32>>>)
8686
! CHECK: fir.store %[[VAL_15]] to %[[VAL_10]]#1 : !fir.ref<i32>
8787
! CHECK: %[[VAL_17:.*]] = fir.load %[[VAL_10]]#0 : !fir.ref<i32>
88-
! CHECK: %[[VAL_18:.*]] = fir.load %[[VAL_16]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>
89-
! CHECK: %[[VAL_19:.*]] = fir.box_addr %[[VAL_18]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
90-
! CHECK: hlfir.assign %[[VAL_17]] to %[[VAL_19]] : i32, !fir.heap<i32>
88+
! CHECK: hlfir.assign %[[VAL_17]] to %[[VAL_16]]#0 realloc : i32, !fir.ref<!fir.box<!fir.heap<i32>>>
9189
! CHECK: omp.yield
9290
! CHECK: }
9391
! CHECK: omp.terminator

0 commit comments

Comments
 (0)