Skip to content

Commit b895e18

Browse files
committed
Fixed clean-ups insertion for atomic capture.
1 parent 3c5c9a2 commit b895e18

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3316,7 +3316,9 @@ static void genAtomicCapture(lower::AbstractConverter &converter,
33163316
}
33173317
firOpBuilder.setInsertionPointToEnd(&block);
33183318
firOpBuilder.create<mlir::omp::TerminatorOp>(loc);
3319-
firOpBuilder.setInsertionPointToStart(&block);
3319+
// The clean-ups associated with the statements inside the capture
3320+
// construct must be generated after the AtomicCaptureOp.
3321+
firOpBuilder.setInsertionPointAfter(atomicCaptureOp);
33203322
}
33213323

33223324
//===----------------------------------------------------------------------===//

flang/test/Lower/OpenMP/atomic-capture.f90

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,23 @@ subroutine pointers_in_atomic_capture()
9797
b = a
9898
!$omp end atomic
9999
end subroutine
100+
101+
! Check that the clean-ups associated with the function call
102+
! are generated after the omp.atomic.capture operation:
103+
! CHECK-LABEL: func.func @_QPfunc_call_cleanup(
104+
subroutine func_call_cleanup(x, v, vv)
105+
integer :: x, v, vv
106+
107+
! CHECK: %[[VAL_7:.*]]:3 = hlfir.associate %{{.*}} {adapt.valuebyref} : (i32) -> (!fir.ref<i32>, !fir.ref<i32>, i1)
108+
! CHECK: %[[VAL_8:.*]] = fir.call @_QPfunc(%[[VAL_7]]#0) fastmath<contract> : (!fir.ref<i32>) -> f32
109+
! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_8]] : (f32) -> i32
110+
! CHECK: omp.atomic.capture {
111+
! CHECK: omp.atomic.read %{{.*}} = %[[VAL_3:.*]]#0 : !fir.ref<i32>, !fir.ref<i32>, i32
112+
! CHECK: omp.atomic.write %[[VAL_3]]#0 = %[[VAL_9]] : !fir.ref<i32>, i32
113+
! CHECK: }
114+
! CHECK: hlfir.end_associate %[[VAL_7]]#1, %[[VAL_7]]#2 : !fir.ref<i32>, i1
115+
!$omp atomic capture
116+
v = x
117+
x = func(vv + 1)
118+
!$omp end atomic
119+
end subroutine func_call_cleanup

flang/test/Lower/OpenMP/atomic-update.f90

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,24 @@ program OmpAtomicUpdate
219219
!$omp atomic update
220220
w = w + g
221221
end program OmpAtomicUpdate
222+
223+
! Check that the clean-ups associated with the function call
224+
! are generated after the omp.atomic.update operation:
225+
! CHECK-LABEL: func.func @_QPfunc_call_cleanup(
226+
subroutine func_call_cleanup(v, vv)
227+
integer v, vv
228+
229+
! CHECK: %[[VAL_6:.*]]:3 = hlfir.associate %{{.*}} {adapt.valuebyref} : (i32) -> (!fir.ref<i32>, !fir.ref<i32>, i1)
230+
! CHECK: %[[VAL_7:.*]] = fir.call @_QPfunc(%[[VAL_6]]#0) fastmath<contract> : (!fir.ref<i32>) -> f32
231+
! CHECK: omp.atomic.update %{{.*}} : !fir.ref<i32> {
232+
! CHECK: ^bb0(%[[VAL_8:.*]]: i32):
233+
! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_8]] : (i32) -> f32
234+
! CHECK: %[[VAL_10:.*]] = arith.addf %[[VAL_9]], %[[VAL_7]] fastmath<contract> : f32
235+
! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (f32) -> i32
236+
! CHECK: omp.yield(%[[VAL_11]] : i32)
237+
! CHECK: }
238+
! CHECK: hlfir.end_associate %[[VAL_6]]#1, %[[VAL_6]]#2 : !fir.ref<i32>, i1
239+
!$omp atomic update
240+
v = v + func(vv + 1)
241+
!$omp end atomic
242+
end subroutine func_call_cleanup

0 commit comments

Comments
 (0)