Skip to content

Commit 8a3d7ce

Browse files
authored
[Flang][OpenMP] Per-sym checks to introduce barriers (#127074)
Whenever there is a `lastprivate` variable and another unrelated variable sets the `mightHaveReadHostSym` flag during Flang lowering privatization, this will result in the insertion of a barrier. This patch modifies this behavior such that this barrier will not be inserted unless the same symbol both sets the flag and has `lastprivate`.
1 parent 004a5fe commit 8a3d7ce

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void DataSharingProcessor::cloneSymbol(const semantics::Symbol *sym) {
166166

167167
if (needInitClone()) {
168168
Fortran::lower::initializeCloneAtRuntime(converter, *sym, symTable);
169-
mightHaveReadHostSym = true;
169+
mightHaveReadHostSym.insert(sym);
170170
}
171171
}
172172

@@ -222,7 +222,7 @@ bool DataSharingProcessor::needBarrier() {
222222
for (const semantics::Symbol *sym : allPrivatizedSymbols) {
223223
if (sym->test(semantics::Symbol::Flag::OmpLastPrivate) &&
224224
(sym->test(semantics::Symbol::Flag::OmpFirstPrivate) ||
225-
mightHaveReadHostSym))
225+
mightHaveReadHostSym.contains(sym)))
226226
return true;
227227
}
228228
return false;
@@ -594,7 +594,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
594594
// TODO: currently there are false positives from dead uses of the mold
595595
// arg
596596
if (!result.getInitMoldArg().getUses().empty())
597-
mightHaveReadHostSym = true;
597+
mightHaveReadHostSym.insert(sym);
598598
}
599599

600600
// Populate the `copy` region if this is a `firstprivate`.

flang/lib/Lower/OpenMP/DataSharingProcessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class DataSharingProcessor {
8686
lower::pft::Evaluation &eval;
8787
bool shouldCollectPreDeterminedSymbols;
8888
bool useDelayedPrivatization;
89-
bool mightHaveReadHostSym = false;
89+
llvm::SmallSet<const semantics::Symbol *, 16> mightHaveReadHostSym;
9090
lower::SymMap &symTable;
9191
OMPConstructSymbolVisitor visitor;
9292

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
! RUN: %flang_fc1 -fopenmp -mmlir --openmp-enable-delayed-privatization-staging=true -emit-hlfir %s -o - | FileCheck %s
2+
3+
subroutine first_and_lastprivate
4+
integer i
5+
integer, dimension(3) :: var
6+
7+
!$omp parallel do lastprivate(i) private(var)
8+
do i=1,1
9+
end do
10+
!$omp end parallel do
11+
end subroutine
12+
13+
! CHECK: omp.private {type = private} @[[VAR_PRIVATIZER:.*Evar_private_box_3xi32]] : [[BOX_TYPE:!fir\.box<!fir\.array<3xi32>>]] init {
14+
! CHECK-NEXT: ^bb0(%[[ORIG_REF:.*]]: {{.*}}, %[[PRIV_REF:.*]]: {{.*}}):
15+
! CHECK: %[[ORIG_VAL:.*]] = fir.load %[[ORIG_REF]]
16+
! CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[ORIG_VAL]], %{{.*}} : ([[BOX_TYPE]], index) -> (index, index, index)
17+
! CHECK: %[[SHAPE_SHIFT:.*]] = fir.shape_shift %[[BOX_DIMS]]#0, %[[BOX_DIMS]]#1
18+
! CHECK: %[[EMBOX:.*]] = fir.embox %{{.*}}(%[[SHAPE_SHIFT]]) : {{.*}} -> [[BOX_TYPE]]
19+
! CHECK: fir.store %[[EMBOX]] to %[[PRIV_REF]]
20+
! CHECK: omp.yield(%[[PRIV_REF]] : !fir.ref<[[BOX_TYPE]]>)
21+
! CHECK: }
22+
23+
! CHECK: omp.private {type = private} @[[I_PRIVATIZER:.*Ei_private_i32]] : i32
24+
25+
! CHECK: func.func @{{.*}}first_and_lastprivate()
26+
! CHECK: %[[ORIG_I_DECL:.*]]:2 = hlfir.declare {{.*}} {uniq_name = "{{.*}}Ei"}
27+
! CHECK: omp.parallel {
28+
! CHECK-NOT: omp.barrier
29+
! CHECK: omp.wsloop private(@[[I_PRIVATIZER]] %[[ORIG_I_DECL]]#0 -> %[[I_ARG:.*]], @[[VAR_PRIVATIZER]] {{.*}}) {
30+
! CHECK: omp.loop_nest {{.*}} {
31+
! CHECK: %[[PRIV_I_DECL:.*]]:2 = hlfir.declare %[[I_ARG]] {uniq_name = "{{.*}}Ei"}
32+
! CHECK: fir.if %{{.*}} {
33+
! CHECK: %[[PRIV_I_VAL:.*]] = fir.load %[[PRIV_I_DECL]]#0 : !fir.ref<i32>
34+
! CHECK: hlfir.assign %[[PRIV_I_VAL]] to %[[ORIG_I_DECL]]#0
35+
! CHECK: }
36+
! CHECK: omp.yield
37+
! CHECK: }
38+
! CHECK: }
39+
! CHECK: omp.terminator
40+
! CHECK: }

0 commit comments

Comments
 (0)