Skip to content

Commit 6c9bce8

Browse files
authored
[flang][OpenMP] Fix privatization when critical is present (#94441)
When a critical construct is present inside another construct where privatizations may occur, such as a parallel construct, some privatizations are skipped if the corresponding symbols are defined inside the critical section only (see the example below). This happens because, while critical constructs have a "body", they don't have a separate scope (which makes sense, since no privatizations can occur in them). Because of this, in semantics phase, it's not possible to insert a new host association symbol, but instead the symbol from the enclosing context is used directly. This makes symbol collection in DataSharingProcessor consider the new symbol to be defined by the critical construct, instead of by the enclosing one, which causes the privatization to be skipped. Example: ``` !$omp parallel default(firstprivate) !$omp critical i = 200 !$omp end critical !$omp end parallel ``` This patch fixes this by identifying constructs where privatizations may not happen and skipping them during the collection of nested symbols. Currently, this seems to happen only with critical constructs, but others can be easily added to the skip list, if needed. Fixes #75767
1 parent c2e62c7 commit 6c9bce8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class DataSharingProcessor {
4444
void Post(const T &) {}
4545

4646
bool Pre(const parser::OpenMPConstruct &omp) {
47-
currentConstruct = &omp;
47+
// Skip constructs that may not have privatizations.
48+
if (!std::holds_alternative<parser::OpenMPCriticalConstruct>(omp.u))
49+
currentConstruct = &omp;
4850
return true;
4951
}
5052

flang/test/Lower/OpenMP/critical.f90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,27 @@ subroutine predetermined_privatization()
5151
end do
5252
!$omp end parallel do
5353
end
54+
55+
! https://github.com/llvm/llvm-project/issues/75767
56+
!CHECK-LABEL: func @_QPparallel_critical_privatization(
57+
subroutine parallel_critical_privatization()
58+
integer :: i
59+
60+
!CHECK: %[[I:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFparallel_critical_privatizationEi"}
61+
!CHECK: %[[I_DECL:.*]]:2 = hlfir.declare %[[I]] {uniq_name = "_QFparallel_critical_privatizationEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
62+
!CHECK: omp.parallel {
63+
!CHECK: %[[PRIV_I:.*]] = fir.alloca i32 {bindc_name = "i", pinned, uniq_name = "_QFparallel_critical_privatizationEi"}
64+
!CHECK: %[[PRIV_I_DECL:.*]]:2 = hlfir.declare %[[PRIV_I]] {uniq_name = "_QFparallel_critical_privatizationEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
65+
!CHECK: %[[TEMP:.*]] = fir.load %[[I_DECL]]#0 : !fir.ref<i32>
66+
!CHECK: hlfir.assign %[[TEMP]] to %[[PRIV_I_DECL]]#0 temporary_lhs : i32, !fir.ref<i32>
67+
!$omp parallel default(firstprivate)
68+
!CHECK: omp.critical {
69+
!$omp critical
70+
!CHECK: %[[C200:.*]] = arith.constant 200 : i32
71+
!CHECK: hlfir.assign %[[C200]] to %[[PRIV_I_DECL]]#0 : i32, !fir.ref<i32>
72+
i = 200
73+
!CHECK: }
74+
!$omp end critical
75+
!CHECK: }
76+
!$omp end parallel
77+
end subroutine

0 commit comments

Comments
 (0)