Skip to content

Commit e2911aa

Browse files
[Flang][OpenMP] Fix crash when loop index var is pointer or allocatable (#129717)
Use hlfir dereferencing for pointers and allocatables and use hlfir assign. Also, change the code updating IV in lastprivate. Note: This is a small change. Modifications in existing tests are changes from fir.store to hlfir.assign. Fixes #121290
1 parent 5ee1c0b commit e2911aa

File tree

65 files changed

+259
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+259
-174
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,10 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
307307
auto ifOp = firOpBuilder.create<fir::IfOp>(loc, cmpOp, /*else*/ false);
308308
firOpBuilder.setInsertionPointToStart(&ifOp.getThenRegion().front());
309309
for (auto [v, loopIV] : llvm::zip_equal(vs, loopIVs)) {
310-
assert(loopIV && "loopIV was not set");
311-
firOpBuilder.createStoreWithConvert(loc, v, loopIV);
310+
hlfir::Entity loopIVEntity{loopIV};
311+
loopIVEntity =
312+
hlfir::derefPointersAndAllocatables(loc, firOpBuilder, loopIVEntity);
313+
firOpBuilder.create<hlfir::AssignOp>(loc, v, loopIVEntity);
312314
}
313315
lastPrivIP = firOpBuilder.saveInsertionPoint();
314316
} else if (mlir::isa<mlir::omp::SectionsOp>(op)) {

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,12 @@ createAndSetPrivatizedLoopVar(lower::AbstractConverter &converter,
789789

790790
firOpBuilder.restoreInsertionPoint(insPt);
791791
mlir::Value cvtVal = firOpBuilder.createConvert(loc, tempTy, indexVal);
792-
mlir::Operation *storeOp = firOpBuilder.create<fir::StoreOp>(
793-
loc, cvtVal, converter.getSymbolAddress(*sym));
792+
hlfir::Entity lhs{converter.getSymbolAddress(*sym)};
793+
794+
lhs = hlfir::derefPointersAndAllocatables(loc, firOpBuilder, lhs);
795+
796+
mlir::Operation *storeOp =
797+
firOpBuilder.create<hlfir::AssignOp>(loc, cvtVal, lhs);
794798
return storeOp;
795799
}
796800

flang/test/Lower/OpenMP/DelayedPrivatization/distribute-standalone-private.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ end subroutine standalone_distribute
3131
! CHECK: %[[VAR_PRIV_DECL:.*]]:2 = hlfir.declare %[[VAR_ARG]]
3232
! CHECK: %[[I_PRIV_DECL:.*]]:2 = hlfir.declare %[[I_ARG]]
3333

34-
! CHECK: fir.store %{{.*}} to %[[I_PRIV_DECL]]#1 : !fir.ref<i32>
34+
! CHECK: hlfir.assign %{{.*}} to %[[I_PRIV_DECL]]#1 : i32, !fir.ref<i32>
3535
! CHECK: %{{.*}} = fir.load %[[VAR_PRIV_DECL]]#0 : !fir.ref<i32>
3636
! CHECK: %{{.*}} = fir.load %[[I_PRIV_DECL]]#0 : !fir.ref<i32>
3737
! CHECK: arith.addi %{{.*}}, %{{.*}} : i32

flang/test/Lower/OpenMP/DelayedPrivatization/wsloop.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ end subroutine wsloop_private
2828
! CHECK: omp.loop_nest (%[[IV:.*]]) : i32 = {{.*}} {
2929
! CHECK: %[[X_PRIV_DECL:.*]]:2 = hlfir.declare %[[X_ARG]] {uniq_name = "{{.*}}x"}
3030
! CHECK: %[[I_PRIV_DECL:.*]]:2 = hlfir.declare %[[I_ARG]] {uniq_name = "{{.*}}i"}
31-
! CHECK: fir.store %[[IV]] to %[[I_PRIV_DECL]]#1
31+
! CHECK: hlfir.assign %[[IV]] to %[[I_PRIV_DECL]]#1
3232
! CHECK: %[[X_VAL:.*]] = fir.load %[[X_PRIV_DECL]]#0
3333
! CHECK: %[[I_VAL:.*]] = fir.load %[[I_PRIV_DECL]]#0
3434
! CHECK: %[[ADD_VAL:.*]] = arith.addi %[[X_VAL]], %[[I_VAL]]

flang/test/Lower/OpenMP/Todo/omp-default-clause-inner-loop.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
! CHECK: %[[const_3:.*]] = arith.constant 1 : i32
1515
! CHECK: omp.wsloop private(@{{.*}} %{{.*}} -> %[[TEMP:.*]] : !fir.ref<i32>) {
1616
! CHECK-NEXT: omp.loop_nest (%[[ARG:.*]]) : i32 = (%[[const_1]]) to (%[[const_2]]) inclusive step (%[[const_3]]) {
17-
! CHECK: fir.store %[[ARG]] to %[[TEMP]] : !fir.ref<i32>
17+
! CHECK: hlfir.assign %[[ARG]] to %[[TEMP]] : i32, !fir.ref<i32>
1818
! EXPECTED: %[[temp_1:.*]] = fir.load %[[PRIVATE_Z]] : !fir.ref<i32>
1919
! CHECK: %[[temp_1:.*]] = fir.load %{{.*}} : !fir.ref<i32>
2020
! CHECK: %[[temp_2:.*]] = fir.load %[[TEMP]] : !fir.ref<i32>

flang/test/Lower/OpenMP/copyin.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ subroutine copyin_derived_type()
161161
! CHECK: omp.wsloop private(@{{.*}} %{{.*}} -> %[[VAL_6:.*]] : !fir.ref<i32>) {
162162
! CHECK-NEXT: omp.loop_nest (%[[VAL_14:.*]]) : i32 = (%[[VAL_11]]) to (%[[VAL_12]]) inclusive step (%[[VAL_13]]) {
163163
! CHECK: %[[VAL_7:.*]]:2 = hlfir.declare %[[VAL_6]] {uniq_name = "_QFcombined_parallel_worksharing_loopEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
164-
! CHECK: fir.store %[[VAL_14]] to %[[VAL_7]]#1 : !fir.ref<i32>
164+
! CHECK: hlfir.assign %[[VAL_14]] to %[[VAL_7]]#1 : i32, !fir.ref<i32>
165165
! CHECK: fir.call @_QPsub4(%[[VAL_9]]#1) fastmath<contract> : (!fir.ref<i32>) -> ()
166166
! CHECK: omp.yield
167167
! CHECK: }
@@ -326,7 +326,7 @@ subroutine common_1()
326326
! CHECK: omp.wsloop private(@{{.*}} %{{.*}} -> %[[VAL_19:.*]] : !fir.ref<i32>) {
327327
! CHECK-NEXT: omp.loop_nest (%[[VAL_37:.*]]) : i32 = (%[[VAL_34]]) to (%[[VAL_35]]) inclusive step (%[[VAL_36]]) {
328328
! CHECK: %[[VAL_20:.*]]:2 = hlfir.declare %[[VAL_19]] {uniq_name = "_QFcommon_2Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
329-
! CHECK: fir.store %[[VAL_37]] to %[[VAL_20]]#1 : !fir.ref<i32>
329+
! CHECK: hlfir.assign %[[VAL_37]] to %[[VAL_20]]#1 : i32, !fir.ref<i32>
330330
! CHECK: %[[VAL_38:.*]] = fir.load %[[VAL_31]]#0 : !fir.ref<i32>
331331
! CHECK: %[[VAL_39:.*]] = fir.load %[[VAL_20]]#0 : !fir.ref<i32>
332332
! CHECK: %[[VAL_40:.*]] = arith.addi %[[VAL_38]], %[[VAL_39]] : i32

flang/test/Lower/OpenMP/critical.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ subroutine predetermined_privatization()
4242
!CHECK: omp.wsloop private(@{{.*}} %{{.*}} -> %[[PRIV_I_ALLOC:.*]] : !fir.ref<i32>)
4343
!CHECK: omp.loop_nest (%[[IV:[^[:space:]]+]])
4444
!CHECK: %[[PRIV_I_DECL:.*]]:2 = hlfir.declare %[[PRIV_I_ALLOC]]
45-
!CHECK: fir.store %[[IV]] to %[[PRIV_I_DECL]]#1
45+
!CHECK: hlfir.assign %[[IV]] to %[[PRIV_I_DECL]]#1
4646
!CHECK: omp.critical
4747
!$omp critical
4848
a(i) = a(i-1) + 1

flang/test/Lower/OpenMP/default-clause.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ subroutine nested_default_clause_test4
291291
!CHECK: omp.loop_nest (%[[ARG:.*]]) : i32 = (%[[CONST_LB]]) to (%[[CONST_UB]]) inclusive step (%[[CONST_STEP]]) {
292292
!CHECK: %[[X_DECLARE:.*]]:2 = hlfir.declare %[[X_ALLOCA]] {{.*}}
293293
!CHECK: %[[LOOP_VAR_DECLARE:.*]]:2 = hlfir.declare %[[LOOP_VAR_ALLOCA]] {{.*}}
294-
!CHECK: fir.store %[[ARG]] to %[[LOOP_VAR_DECLARE]]#1 : !fir.ref<i32>
294+
!CHECK: hlfir.assign %[[ARG]] to %[[LOOP_VAR_DECLARE]]#1 : i32, !fir.ref<i32>
295295
!CHECK: %[[LOADED_X:.*]] = fir.load %[[X_DECLARE]]#0 : !fir.ref<i32>
296296
!CHECK: %[[CONST:.*]] = arith.constant 1 : i32
297297
!CHECK: %[[RESULT:.*]] = arith.addi %[[LOADED_X]], %[[CONST]] : i32
@@ -324,7 +324,7 @@ subroutine nested_default_clause_test5
324324
! CHECK: omp.wsloop private(@{{.*}} %{{.*}} -> %[[LOOP_VAR:.*]] : !fir.ref<i32>) {
325325
!CHECK: omp.loop_nest (%[[ARG:.*]]) : i32 = (%[[CONST_LB]]) to (%[[CONST_UB]]) inclusive step (%[[CONST_STEP]]) {
326326
!CHECK: %[[LOOP_VAR_DECLARE:.*]]:2 = hlfir.declare %[[LOOP_VAR]] {{.*}}
327-
!CHECK: fir.store %[[ARG]] to %[[LOOP_VAR_DECLARE]]#1 : !fir.ref<i32>
327+
!CHECK: hlfir.assign %[[ARG]] to %[[LOOP_VAR_DECLARE]]#1 : i32, !fir.ref<i32>
328328
!CHECK: %[[LOADED_X:.*]] = fir.load %[[X_VAR_DECLARE]]#0 : !fir.ref<i32>
329329
!CHECK: %[[CONST:.*]] = arith.constant 1 : i32
330330
!CHECK: %[[ADD:.*]] = arith.addi %[[LOADED_X]], %[[CONST]] : i32

flang/test/Lower/OpenMP/generic-loop-rewriting.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end subroutine target_teams_loop
5050
!CHECK: omp.loop_nest (%{{.*}}) : i32 =
5151
!CHECK-SAME: (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]]) {
5252
!CHECK: %[[I_PRIV_DECL:.*]]:2 = hlfir.declare %[[I_PRIV_ARG]]
53-
!CHECK: fir.store %{{.*}} to %[[I_PRIV_DECL]]#1 : !fir.ref<i32>
53+
!CHECK: hlfir.assign %{{.*}} to %[[I_PRIV_DECL]]#1 : i32, !fir.ref<i32>
5454
!CHECK: }
5555
!CHECK: }
5656
!CHECK: }

flang/test/Lower/OpenMP/lastprivate-allocatable.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
! if this is the last iteration
1818
! CHECK: fir.if %{{.*}} {
1919
! store loop IV
20-
! CHECK: fir.store %{{.*}} to %[[VAL_18]]#1 : !fir.ref<i32>
20+
! CHECK: hlfir.assign %{{.*}} to %[[VAL_18]]#1 : i32, !fir.ref<i32>
2121
! assign private variable to original copy: realloc
2222
! CHECK: %[[VAL_23:.*]] = fir.load %[[VAL_16]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>
2323
! CHECK: %[[VAL_24:.*]] = fir.box_addr %[[VAL_23]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>

0 commit comments

Comments
 (0)