Skip to content

Commit 374a5be

Browse files
[Flang][OpenMP] Add PointerAssociateScalar to Cray Pointer used in the DSA (#133232)
Issue: Cray Pointer is not associated to Cray Pointee, leading to Segmentation fault Fix: GetUltimate, retrieves the base symbol in the current scope, which gets passed all the references and returns the original symbol --------- Co-authored-by: Michael Klemm <[email protected]>
1 parent 3e742b5 commit 374a5be

File tree

4 files changed

+244
-2
lines changed

4 files changed

+244
-2
lines changed

flang/lib/Lower/ConvertExprToHLFIR.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ class HlfirDesignatorBuilder {
279279
gen(const Fortran::evaluate::SymbolRef &symbolRef) {
280280
if (std::optional<fir::FortranVariableOpInterface> varDef =
281281
getSymMap().lookupVariableDefinition(symbolRef)) {
282-
if (symbolRef->test(Fortran::semantics::Symbol::Flag::CrayPointee)) {
282+
if (symbolRef.get().GetUltimate().test(
283+
Fortran::semantics::Symbol::Flag::CrayPointee)) {
283284
// The pointee is represented with a descriptor inheriting
284285
// the shape and type parameters of the pointee.
285286
// We have to update the base_addr to point to the current

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2306,7 +2306,7 @@ void OmpAttributeVisitor::Post(const parser::Name &name) {
23062306
// the scope of the parallel region, and not in this scope.
23072307
// TODO: check whether this should be caught in IsObjectWithDSA
23082308
!symbol->test(Symbol::Flag::OmpPrivate)) {
2309-
if (symbol->test(Symbol::Flag::CrayPointee)) {
2309+
if (symbol->GetUltimate().test(Symbol::Flag::CrayPointee)) {
23102310
std::string crayPtrName{
23112311
semantics::GetCrayPointer(*symbol).name().ToString()};
23122312
if (!IsObjectWithDSA(*currScope().FindSymbol(crayPtrName)))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
! Test lowering of Cray pointee references.
2+
! RUN: flang -fc1 -emit-hlfir -fopenmp %s -o - 2>&1 | FileCheck %s
3+
4+
module test_host_assoc_cray_pointer
5+
! CHECK-LABEL: fir.global @_QMtest_host_assoc_cray_pointerEivar : i64
6+
real*8 var(*)
7+
! CHECK-LABEL: fir.global @_QMtest_host_assoc_cray_pointerEvar : !fir.array<?xf64>
8+
pointer(ivar,var)
9+
10+
contains
11+
12+
! CHECK-LABEL: func.func @_QMtest_host_assoc_cray_pointerPset_cray_pointer()
13+
subroutine set_cray_pointer
14+
! CHECK: %[[ALLOCA:.*]] = fir.alloca !fir.box<!fir.ptr<!fir.array<?xf64>>>
15+
! CHECK: %[[IVAR_ADDR:.*]] = fir.address_of(@_QMtest_host_assoc_cray_pointerEivar) : !fir.ref<i64>
16+
! CHECK: %[[IVAR_DECL:.*]]:2 = hlfir.declare %[[IVAR_ADDR]] {uniq_name = "_QMtest_host_assoc_cray_pointerEivar"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)
17+
! CHECK: %[[VAR_DECL:.*]]:2 = hlfir.declare %[[ALLOCA]] {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QMtest_host_assoc_cray_pointerEvar"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>) -> (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>)
18+
real*8 pointee(2)
19+
pointee(1) = 42.0
20+
21+
ivar = loc(pointee)
22+
23+
!$omp parallel default(none) shared(ivar)
24+
! CHECK: omp.parallel
25+
! CHECK: %[[I_01:.*]] = fir.convert %[[IVAR_DECL]]#0 : (!fir.ref<i64>) -> !fir.ref<!fir.ptr<i64>>
26+
! CHECK: %[[I_02:.*]] = fir.load %[[I_01]] : !fir.ref<!fir.ptr<i64>>
27+
! CHECK: %[[I_03:.*]] = fir.convert %[[VAR_DECL]]#0 : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>) -> !fir.ref<!fir.box<none>>
28+
! CHECK: %[[I_04:.*]] = fir.convert %[[I_02]] : (!fir.ptr<i64>) -> !fir.llvm_ptr<i8>
29+
! CHECK: fir.call @_FortranAPointerAssociateScalar(%[[I_03]], %[[I_04]]) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
30+
print *, var(1)
31+
!$omp end parallel
32+
end subroutine
33+
end module
34+
35+
program test_cray_pointers_01
36+
real*8, save :: var(*)
37+
! CHECK: %[[BOX_ALLOCA:.*]] = fir.alloca !fir.box<!fir.ptr<!fir.array<?xf64>>>
38+
! CHECK: %[[IVAR_ALLOCA:.*]] = fir.alloca i64 {bindc_name = "ivar", uniq_name = "_QFEivar"}
39+
! CHECK: %[[IVAR_DECL_01:.*]]:2 = hlfir.declare %[[IVAR_ALLOCA]] {uniq_name = "_QFEivar"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)
40+
pointer(ivar,var)
41+
! CHECK: %[[VAR_DECL_02:.*]]:2 = hlfir.declare %[[BOX_ALLOCA]] {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFEvar"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>) -> (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>)
42+
43+
real*8 pointee(2)
44+
pointee(1) = 42.0
45+
46+
!$omp parallel default(none) private(ivar) shared(pointee)
47+
! CHECK: omp.parallel private({{.*}} %[[IVAR_DECL_01]]#0 -> %[[ARG0:.*]] : !fir.ref<i64>) {
48+
! CHECK: %[[IVAR_DECL_02:.*]]:2 = hlfir.declare %[[ARG0]] {uniq_name = "_QFEivar"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)
49+
! CHECK: hlfir.assign %{{.*}} to %[[IVAR_DECL_02]]#0 : i64, !fir.ref<i64>
50+
ivar = loc(pointee)
51+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
52+
! CHECK: %[[CONST_2:.*]] = arith.constant 2 : i32
53+
! CHECK: {{.*}} = math.fpowi {{.*}}, %[[CONST_2]] fastmath<contract> : f64, i32
54+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
55+
var(1) = var(1) ** 2
56+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
57+
print *, var(1)
58+
! CHECK: omp.terminator
59+
! CHECK: }
60+
!$omp end parallel
61+
end program test_cray_pointers_01
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
! Test lowering of Cray pointee references.
2+
! RUN: flang -fc1 -emit-hlfir -fopenmp %s -o - 2>&1 | FileCheck %s
3+
4+
! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "test_cray_pointers_02"}
5+
program test_cray_pointers_02
6+
implicit none
7+
8+
! CHECK: fir.call @_QPnone_shared() fastmath<contract> : () -> ()
9+
! CHECK: fir.call @_QPnone_private() fastmath<contract> : () -> ()
10+
! CHECK: fir.call @_QPnone_firstprivate() fastmath<contract> : () -> ()
11+
! CHECK: fir.call @_QPprivate_shared() fastmath<contract> : () -> ()
12+
! CHECK: fir.call @_QPprivate_firstprivate() fastmath<contract> : () -> ()
13+
! CHECK: fir.call @_QPfirstprivate_shared() fastmath<contract> : () -> ()
14+
! CHECK: fir.call @_QPfirstprivate_private() fastmath<contract> : () -> ()
15+
call none_shared()
16+
call none_private()
17+
call none_firstprivate()
18+
call private_shared()
19+
call private_firstprivate()
20+
call firstprivate_shared()
21+
call firstprivate_private()
22+
! CHECK: return
23+
! CHECK: }
24+
end program test_cray_pointers_02
25+
26+
! CHECK-LABEL: func.func @_QPnone_shared()
27+
subroutine none_shared()
28+
implicit none
29+
integer var(*)
30+
pointer(ivar,var)
31+
integer pointee(8)
32+
33+
pointee(1) = 42
34+
ivar = loc(pointee)
35+
36+
!$omp parallel num_threads(1) default(none) shared(ivar)
37+
! CHECK: omp.parallel
38+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
39+
! CHECK: {{.*}} = arith.divsi
40+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
41+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
42+
var(1) = var(1) / 2
43+
print '(A24,I6)', 'none_shared', var(1)
44+
!$omp end parallel
45+
! CHECK: return
46+
end subroutine
47+
48+
! CHECK-LABEL: func.func @_QPnone_private()
49+
subroutine none_private()
50+
implicit none
51+
integer var(*)
52+
pointer(ivar,var)
53+
integer pointee(8)
54+
55+
pointee(1) = 42
56+
ivar = loc(pointee)
57+
58+
!$omp parallel num_threads(1) default(none) private(ivar) shared(pointee)
59+
! CHECK: omp.parallel
60+
ivar = loc(pointee)
61+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
62+
! CHECK: {{.*}} = arith.addi
63+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
64+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
65+
var(1) = var(1) + 2
66+
print '(A24,I6)', 'none_private', var(1)
67+
!$omp end parallel
68+
! CHECK: return
69+
end subroutine
70+
71+
! CHECK-LABEL: func.func @_QPnone_firstprivate()
72+
subroutine none_firstprivate()
73+
implicit none
74+
integer var(*)
75+
pointer(ivar,var)
76+
integer pointee(8)
77+
78+
pointee(1) = 42
79+
ivar = loc(pointee)
80+
81+
!$omp parallel num_threads(1) default(none) firstprivate(ivar)
82+
! CHECK: omp.parallel
83+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
84+
! CHECK: {{.*}} = arith.muli
85+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
86+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
87+
var(1) = var(1) * 2
88+
print '(A24,I6)', 'none_firstprivate', var(1)
89+
!$omp end parallel
90+
! CHECK: return
91+
end subroutine
92+
93+
! CHECK-LABEL: func.func @_QPprivate_shared()
94+
subroutine private_shared()
95+
implicit none
96+
integer var(*)
97+
pointer(ivar,var)
98+
integer pointee(8)
99+
100+
pointee(1) = 42
101+
ivar = loc(pointee)
102+
103+
!$omp parallel num_threads(1) default(private) shared(ivar)
104+
! CHECK: omp.parallel
105+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
106+
! CHECK: {{.*}} = math.ipowi
107+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
108+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
109+
var(1) = var(1) ** 2
110+
print '(A24,I6)', 'private_shared', var(1)
111+
!$omp end parallel
112+
! CHECK: return
113+
end subroutine
114+
115+
! CHECK-LABEL: func.func @_QPprivate_firstprivate()
116+
subroutine private_firstprivate()
117+
implicit none
118+
integer var(*)
119+
pointer(ivar,var)
120+
integer pointee(8)
121+
122+
pointee(1) = 42
123+
ivar = loc(pointee)
124+
125+
!$omp parallel num_threads(1) default(private) firstprivate(ivar)
126+
! CHECK: omp.parallel
127+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
128+
! CHECK: {{.*}} = arith.subi
129+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
130+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
131+
var(1) = var(1) - 2
132+
print '(A24,I6)', 'private_firstprivate', var(1)
133+
!$omp end parallel
134+
! CHECK: return
135+
end subroutine
136+
137+
! CHECK-LABEL: func.func @_QPfirstprivate_shared()
138+
subroutine firstprivate_shared()
139+
implicit none
140+
integer var(*)
141+
pointer(ivar,var)
142+
integer pointee(8)
143+
144+
pointee(1) = 42
145+
ivar = loc(pointee)
146+
147+
!$omp parallel num_threads(1) default(firstprivate) shared(ivar)
148+
! CHECK: omp.parallel
149+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
150+
! CHECK: {{.*}} = arith.divsi
151+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
152+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
153+
var(1) = var(1) / 2
154+
print '(A24,I6)', 'firstprivate_shared', var(1)
155+
!$omp end parallel
156+
! CHECK: return
157+
end subroutine
158+
159+
! CHECK-LABEL: func.func @_QPfirstprivate_private()
160+
subroutine firstprivate_private()
161+
implicit none
162+
integer var(*)
163+
pointer(ivar,var)
164+
integer pointee(8)
165+
166+
pointee(1) = 42
167+
ivar = loc(pointee)
168+
169+
!$omp parallel num_threads(1) default(firstprivate) private(ivar) shared(pointee)
170+
! CHECK: omp.parallel
171+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
172+
! CHECK: {{.*}} = math.ipowi
173+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
174+
! CHECK: fir.call @_FortranAPointerAssociateScalar({{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.llvm_ptr<i8>) -> ()
175+
ivar = loc(pointee)
176+
var(1) = var(1) ** 2
177+
print '(A24,I6)', 'firstprivate_private', var(1)
178+
!$omp end parallel
179+
! CHECK: return
180+
end subroutine

0 commit comments

Comments
 (0)