Skip to content

Commit 94204f5

Browse files
authored
[flang][OpenMP] Fix location of barrier in copyin clause (#91214)
Insert the barrier after the last _executed_ copy, not the most recently inserted copy. This fixes #91205.
1 parent e68d505 commit 94204f5

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,16 @@ bool ClauseProcessor::processCopyin() const {
555555
// synchronize threads and avoid data races on propagation master's thread
556556
// values of threadprivate variables to local instances of that variables of
557557
// all other implicit threads.
558+
559+
// All copies are inserted at either "insPt" (i.e. immediately before it),
560+
// or at some earlier point (as determined by "copyHostAssociateVar").
561+
// Unless the insertion point is given to "copyHostAssociateVar" explicitly,
562+
// it will not restore the builder's insertion point. Since the copies may be
563+
// inserted in any order (not following the execution order), make sure the
564+
// barrier is inserted following all of them.
565+
firOpBuilder.restoreInsertionPoint(insPt);
558566
if (hasCopyin)
559567
firOpBuilder.create<mlir::omp::BarrierOp>(converter.getCurrentLocation());
560-
firOpBuilder.restoreInsertionPoint(insPt);
561568
return hasCopyin;
562569
}
563570

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
!RUN: bbc -fopenmp -emit-hlfir -o - %s | FileCheck %s
2+
3+
!CHECK: omp.parallel if(%{{[0-9]+}} : i1) {
4+
!CHECK: %[[THP1:[0-9]+]] = omp.threadprivate %{{[0-9]+}}#1
5+
!CHECK: %[[DCL1:[0-9]+]]:2 = hlfir.declare %[[THP1]] {uniq_name = "_QFcopyin_scalar_arrayEx1"}
6+
!CHECK: %[[LD1:[0-9]+]] = fir.load %{{[0-9]+}}#0
7+
!CHECK: hlfir.assign %[[LD1]] to %[[DCL1]]#0 temporary_lhs
8+
!CHECK: %[[THP2:[0-9]+]] = omp.threadprivate %{{[0-9]+}}#1
9+
!CHECK: %[[SHP2:[0-9]+]] = fir.shape %c{{[0-9]+}}
10+
!CHECK: %[[DCL2:[0-9]+]]:2 = hlfir.declare %[[THP2]](%[[SHP2]]) {uniq_name = "_QFcopyin_scalar_arrayEx2"}
11+
!CHECK: hlfir.assign %{{[0-9]+}}#0 to %[[DCL2]]#0 temporary_lhs
12+
!CHECK: omp.barrier
13+
!CHECK: fir.call @_QPsub1(%[[DCL1]]#1, %[[DCL2]]#1)
14+
!CHECK: omp.terminator
15+
!CHECK: }
16+
17+
!https://github.com/llvm/llvm-project/issues/91205
18+
19+
subroutine copyin_scalar_array()
20+
integer(kind=4), save :: x1
21+
integer(kind=8), save :: x2(10)
22+
!$omp threadprivate(x1, x2)
23+
24+
! Have x1 appear before x2 in the AST node for the `parallel construct,
25+
! but at the same time have them in a different order in `copyin`.
26+
!$omp parallel if (x1 .eq. x2(1)) copyin(x2, x1)
27+
call sub1(x1, x2)
28+
!$omp end parallel
29+
30+
end
31+

0 commit comments

Comments
 (0)