Skip to content

Commit 89b8a47

Browse files
committed
[HLFIR][flang][OpenMP][MLIR] Basic support for delayed privatization code-gen
Similar to llvm#81833 but for `hlfir` since we need different handling for the `fir` vs. `hlfir` dialect.
1 parent ed35b6f commit 89b8a47

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,17 @@ class FirConverter : public Fortran::lower::AbstractConverter {
10521052
if (sym.detailsIf<Fortran::semantics::CommonBlockDetails>())
10531053
return symMap->lookupSymbol(sym);
10541054

1055+
// For symbols to be privatized in OMP, the symbol is mapped to an
1056+
// instance of `SymbolBox::Intrinsic` (i.e. a direct mapping to an MLIR
1057+
// SSA value). This MLIR SSA value is the block argument to the
1058+
// `omp.private`'s `alloc` block. If this is the case, we return this
1059+
// `SymbolBox::Intrinsic` value.
1060+
if (Fortran::lower::SymbolBox v = symMap->lookupSymbol(sym))
1061+
return v.match(
1062+
[&](const Fortran::lower::SymbolBox::Intrinsic &)
1063+
-> Fortran::lower::SymbolBox { return v; },
1064+
[](const auto &) -> Fortran::lower::SymbolBox { return {}; });
1065+
10551066
return {};
10561067
}
10571068
if (Fortran::lower::SymbolBox v = symMap->lookupSymbol(sym))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
! Test delayed privatization for the `firstprivate` clause.
2+
3+
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %s 2>&1 | FileCheck %s
4+
5+
subroutine delayed_privatization_firstprivate
6+
implicit none
7+
integer :: var1
8+
9+
!$OMP PARALLEL FIRSTPRIVATE(var1)
10+
var1 = 10
11+
!$OMP END PARALLEL
12+
end subroutine
13+
14+
! CHECK-LABEL: omp.private {type = firstprivate}
15+
! CHECK-SAME: @[[VAR1_PRIVATIZER_SYM:.*]] : !fir.ref<i32> alloc {
16+
! CHECK-NEXT: ^bb0(%[[PRIV_ARG:.*]]: !fir.ref<i32>):
17+
! CHECK-NEXT: %[[PRIV_ALLOC:.*]] = fir.alloca i32 {bindc_name = "var1", pinned, uniq_name = "_QFdelayed_privatization_firstprivateEvar1"}
18+
! CHECK-NEXT: %[[PRIV_DECL:.*]]:2 = hlfir.declare %[[PRIV_ALLOC]] {uniq_name = "_QFdelayed_privatization_firstprivateEvar1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
19+
! CHECK-NEXT: omp.yield(%[[PRIV_DECL]]#0 : !fir.ref<i32>)
20+
! CHECK: } copy {
21+
! CHECK: ^bb0(%[[PRIV_ORIG_ARG:.*]]: !fir.ref<i32>, %[[PRIV_PRIV_ARG:.*]]: !fir.ref<i32>):
22+
! CHECK: %[[ORIG_VAL:.*]] = fir.load %[[PRIV_ORIG_ARG]] : !fir.ref<i32>
23+
! CHECK: hlfir.assign %[[ORIG_VAL]] to %[[PRIV_PRIV_ARG]] temporary_lhs : i32, !fir.ref<i32>
24+
! CHECK: omp.yield(%[[PRIV_PRIV_ARG]] : !fir.ref<i32>)
25+
! CHECK: }
26+
27+
! CHECK-LABEL: @_QPdelayed_privatization_firstprivate
28+
! CHECK: omp.parallel private(@[[VAR1_PRIVATIZER_SYM]] %{{.*}} -> %{{.*}} : !fir.ref<i32>) {
29+
! CHECK: omp.terminator
30+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
! Test delayed privatization for the `private` clause.
2+
3+
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %s 2>&1 | FileCheck %s
4+
5+
subroutine delayed_privatization_private
6+
implicit none
7+
integer :: var1
8+
9+
!$OMP PARALLEL PRIVATE(var1)
10+
var1 = 10
11+
!$OMP END PARALLEL
12+
end subroutine
13+
14+
! CHECK-LABEL: omp.private {type = private}
15+
! CHECK-SAME: @[[PRIVATIZER_SYM:.*]] : !fir.ref<i32> alloc {
16+
! CHECK-NEXT: ^bb0(%[[PRIV_ARG:.*]]: !fir.ref<i32>):
17+
! CHECK-NEXT: %[[PRIV_ALLOC:.*]] = fir.alloca i32 {bindc_name = "var1", pinned, uniq_name = "_QFdelayed_privatization_privateEvar1"}
18+
! CHECK-NEXT: %[[PRIV_DECL:.*]]:2 = hlfir.declare %[[PRIV_ALLOC]] {uniq_name = "_QFdelayed_privatization_privateEvar1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
19+
! CHECK-NEXT: omp.yield(%[[PRIV_DECL]]#0 : !fir.ref<i32>)
20+
! CHECK-NOT: } copy {
21+
22+
! CHECK-LABEL: @_QPdelayed_privatization_private
23+
! CHECK: %[[ORIG_ALLOC:.*]] = fir.alloca i32 {bindc_name = "var1", uniq_name = "_QFdelayed_privatization_privateEvar1"}
24+
! CHECK: %[[ORIG_DECL:.*]]:2 = hlfir.declare %[[ORIG_ALLOC]] {uniq_name = "_QFdelayed_privatization_privateEvar1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
25+
! CHECK: omp.parallel private(@[[PRIVATIZER_SYM]] %[[ORIG_DECL]]#0 -> %[[PAR_ARG:.*]] : !fir.ref<i32>) {
26+
! CHECK: %[[PAR_ARG_DECL:.*]]:2 = hlfir.declare %[[PAR_ARG]] {uniq_name = "_QFdelayed_privatization_privateEvar1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
27+
! CHECK: hlfir.assign %{{.*}} to %[[PAR_ARG_DECL]]#0 : i32, !fir.ref<i32>
28+
! CHECK: omp.terminator

0 commit comments

Comments
 (0)