Skip to content

Commit b0976ad

Browse files
committed
[flang][MLIR][OpenMP] Extend delayed privatization for scalar allocatables and pointers
One more step in extending support for delayed privatization. This diff adds support for scalar allocatables and pointers.
1 parent 72d85b0 commit b0976ad

5 files changed

+143
-14
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
748748

749749
void copyVar(mlir::Location loc, mlir::Value dst,
750750
mlir::Value src) override final {
751-
copyVarHLFIR(loc, dst, src);
751+
copyVarHLFIR(loc, Fortran::lower::SymbolBox::Intrinsic{dst},
752+
Fortran::lower::SymbolBox::Intrinsic{src});
752753
}
753754

754755
void copyHostAssociateVar(
@@ -1009,10 +1010,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
10091010
// `omp.private`'s `alloc` block. If this is the case, we return this
10101011
// `SymbolBox::Intrinsic` value.
10111012
if (Fortran::lower::SymbolBox v = symMap->lookupSymbol(sym))
1012-
return v.match(
1013-
[&](const Fortran::lower::SymbolBox::Intrinsic &)
1014-
-> Fortran::lower::SymbolBox { return v; },
1015-
[](const auto &) -> Fortran::lower::SymbolBox { return {}; });
1013+
return v;
10161014

10171015
return {};
10181016
}
@@ -1060,15 +1058,16 @@ class FirConverter : public Fortran::lower::AbstractConverter {
10601058
const Fortran::lower::SymbolBox &rhs_sb) {
10611059
mlir::Location loc = genLocation(sym.name());
10621060
if (lowerToHighLevelFIR())
1063-
copyVarHLFIR(loc, lhs_sb.getAddr(), rhs_sb.getAddr());
1061+
copyVarHLFIR(loc, lhs_sb, rhs_sb);
10641062
else
10651063
copyVarFIR(loc, sym, lhs_sb, rhs_sb);
10661064
}
10671065

1068-
void copyVarHLFIR(mlir::Location loc, mlir::Value dst, mlir::Value src) {
1066+
void copyVarHLFIR(mlir::Location loc, Fortran::lower::SymbolBox dst,
1067+
Fortran::lower::SymbolBox src) {
10691068
assert(lowerToHighLevelFIR());
1070-
hlfir::Entity lhs{dst};
1071-
hlfir::Entity rhs{src};
1069+
hlfir::Entity lhs{dst.getAddr()};
1070+
hlfir::Entity rhs{src.getAddr()};
10721071
// Temporary_lhs is set to true in hlfir.assign below to avoid user
10731072
// assignment to be used and finalization to be called on the LHS.
10741073
// This may or may not be correct but mimics the current behaviour
@@ -1082,7 +1081,22 @@ class FirConverter : public Fortran::lower::AbstractConverter {
10821081
/*keepLhsLengthInAllocatableAssignment=*/false,
10831082
/*temporary_lhs=*/true);
10841083
};
1085-
if (lhs.isAllocatable()) {
1084+
1085+
bool isBoxAllocatable = dst.match(
1086+
[](const fir::MutableBoxValue &box) { return box.isAllocatable(); },
1087+
[](const fir::FortranVariableOpInterface &box) {
1088+
return fir::FortranVariableOpInterface(box).isAllocatable();
1089+
},
1090+
[](const auto &box) { return false; });
1091+
1092+
bool isBoxPointer = dst.match(
1093+
[](const fir::MutableBoxValue &box) { return box.isPointer(); },
1094+
[](const fir::FortranVariableOpInterface &box) {
1095+
return fir::FortranVariableOpInterface(box).isPointer();
1096+
},
1097+
[](const auto &box) { return false; });
1098+
1099+
if (isBoxAllocatable) {
10861100
// Deep copy allocatable if it is allocated.
10871101
// Note that when allocated, the RHS is already allocated with the LHS
10881102
// shape for copy on entry in createHostAssociateVarClone.
@@ -1097,7 +1111,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
10971111
copyData(lhs, rhs);
10981112
})
10991113
.end();
1100-
} else if (lhs.isPointer()) {
1114+
} else if (isBoxPointer) {
11011115
// Set LHS target to the target of RHS (do not copy the RHS
11021116
// target data into the LHS target storage).
11031117
auto loadVal = builder->create<fir::LoadOp>(loc, rhs);

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void DataSharingProcessor::processStep2(mlir::Operation *op, bool isLoop) {
5050
}
5151

5252
void DataSharingProcessor::insertDeallocs() {
53+
// TODO Extend delayed privatization to include a `dealloc` region.
5354
for (const Fortran::semantics::Symbol *sym : privatizedSymbols)
5455
if (Fortran::semantics::IsAllocatable(sym->GetUltimate())) {
5556
converter.createHostAssociateVarCloneDealloc(*sym);
@@ -376,6 +377,7 @@ void DataSharingProcessor::doPrivatize(const Fortran::semantics::Symbol *sym) {
376377
symLoc, uniquePrivatizerName, symType,
377378
isFirstPrivate ? mlir::omp::DataSharingClauseType::FirstPrivate
378379
: mlir::omp::DataSharingClauseType::Private);
380+
fir::ExtendedValue symExV = converter.getSymbolExtendedValue(*sym);
379381

380382
symTable->pushScope();
381383

@@ -386,7 +388,8 @@ void DataSharingProcessor::doPrivatize(const Fortran::semantics::Symbol *sym) {
386388
&allocRegion, /*insertPt=*/{}, symType, symLoc);
387389

388390
firOpBuilder.setInsertionPointToEnd(allocEntryBlock);
389-
symTable->addSymbol(*sym, allocRegion.getArgument(0));
391+
symTable->addSymbol(*sym,
392+
fir::substBase(symExV, allocRegion.getArgument(0)));
390393
symTable->pushScope();
391394
cloneSymbol(sym);
392395
firOpBuilder.create<mlir::omp::YieldOp>(
@@ -403,10 +406,12 @@ void DataSharingProcessor::doPrivatize(const Fortran::semantics::Symbol *sym) {
403406
mlir::Block *copyEntryBlock = firOpBuilder.createBlock(
404407
&copyRegion, /*insertPt=*/{}, {symType, symType}, {symLoc, symLoc});
405408
firOpBuilder.setInsertionPointToEnd(copyEntryBlock);
406-
symTable->addSymbol(*sym, copyRegion.getArgument(0),
409+
symTable->addSymbol(*sym,
410+
fir::substBase(symExV, copyRegion.getArgument(0)),
407411
/*force=*/true);
408412
symTable->pushScope();
409-
symTable->addSymbol(*sym, copyRegion.getArgument(1));
413+
symTable->addSymbol(*sym,
414+
fir::substBase(symExV, copyRegion.getArgument(1)));
410415
auto ip = firOpBuilder.saveInsertionPoint();
411416
copyFirstPrivateSymbol(sym, &ip);
412417

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
! Test delayed privatization for allocatables: `firstprivate`.
2+
3+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -mmlir --openmp-enable-delayed-privatization \
4+
! RUN: -o - %s 2>&1 | FileCheck %s
5+
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %s 2>&1 |\
6+
! RUN: FileCheck %s
7+
8+
subroutine delayed_privatization_allocatable
9+
implicit none
10+
integer, allocatable :: var1
11+
12+
!$omp parallel firstprivate(var1)
13+
var1 = 10
14+
!$omp end parallel
15+
end subroutine
16+
17+
! CHECK-LABEL: omp.private {type = firstprivate}
18+
! CHECK-SAME: @[[PRIVATIZER_SYM:.*]] : [[TYPE:!fir.ref<!fir.box<!fir.heap<i32>>>]] alloc {
19+
20+
! CHECK-NEXT: ^bb0(%[[PRIV_ARG:.*]]: [[TYPE]]):
21+
22+
! CHECK: } copy {
23+
! CHECK: ^bb0(%[[PRIV_ORIG_ARG:.*]]: [[TYPE]], %[[PRIV_PRIV_ARG:.*]]: [[TYPE]]):
24+
25+
! CHECK-NEXT: %[[PRIV_BASE_VAL:.*]] = fir.load %[[PRIV_PRIV_ARG]]
26+
! CHECK-NEXT: %[[PRIV_BASE_BOX:.*]] = fir.box_addr %[[PRIV_BASE_VAL]]
27+
! CHECK-NEXT: %[[PRIV_BASE_ADDR:.*]] = fir.convert %[[PRIV_BASE_BOX]]
28+
! CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : i64
29+
! CHECK-NEXT: %[[COPY_COND:.*]] = arith.cmpi ne, %[[PRIV_BASE_ADDR]], %[[C0]] : i64
30+
31+
! CHECK-NEXT: fir.if %[[COPY_COND]] {
32+
! CHECK-NEXT: %[[ORIG_BASE_VAL:.*]] = fir.load %[[PRIV_ORIG_ARG]]
33+
! CHECK-NEXT: %[[ORIG_BASE_ADDR:.*]] = fir.box_addr %[[ORIG_BASE_VAL]]
34+
! CHECK-NEXT: %[[ORIG_BASE_LD:.*]] = fir.load %[[ORIG_BASE_ADDR]]
35+
! CHECK-NEXT: hlfir.assign %[[ORIG_BASE_LD]] to %[[PRIV_BASE_BOX]] temporary_lhs
36+
! CHECK-NEXT: }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
! Test delayed privatization for allocatables: `private`.
2+
3+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -mmlir --openmp-enable-delayed-privatization \
4+
! RUN: -o - %s 2>&1 | FileCheck %s
5+
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %s 2>&1 |\
6+
! RUN: FileCheck %s
7+
8+
subroutine delayed_privatization_allocatable
9+
implicit none
10+
integer, allocatable :: var1
11+
12+
!$omp parallel private(var1)
13+
var1 = 10
14+
!$omp end parallel
15+
end subroutine
16+
17+
! CHECK-LABEL: omp.private {type = private}
18+
! CHECK-SAME: @[[PRIVATIZER_SYM:.*]] : [[TYPE:!fir.ref<!fir.box<!fir.heap<i32>>>]] alloc {
19+
20+
! CHECK-NEXT: ^bb0(%[[PRIV_ARG:.*]]: [[TYPE]]):
21+
22+
! CHECK-NEXT: %[[PRIV_ALLOC:.*]] = fir.alloca !fir.box<!fir.heap<i32>> {bindc_name = "var1", pinned, uniq_name = "_QFdelayed_privatization_allocatableEvar1"}
23+
24+
! CHECK-NEXT: %[[PRIV_ARG_VAL:.*]] = fir.load %[[PRIV_ARG]] : !fir.ref<!fir.box<!fir.heap<i32>>>
25+
! CHECK-NEXT: %[[PRIV_ARG_BOX:.*]] = fir.box_addr %[[PRIV_ARG_VAL]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
26+
! CHECK-NEXT: %[[PRIV_ARG_ADDR:.*]] = fir.convert %[[PRIV_ARG_BOX]] : (!fir.heap<i32>) -> i64
27+
! CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : i64
28+
! CHECK-NEXT: %[[ALLOC_COND:.*]] = arith.cmpi ne, %[[PRIV_ARG_ADDR]], %[[C0]] : i64
29+
30+
! CHECK-NEXT: fir.if %[[ALLOC_COND]] {
31+
! CHECK-NEXT: %[[PRIV_ALLOCMEM:.*]] = fir.allocmem i32 {fir.must_be_heap = true, uniq_name = "_QFdelayed_privatization_allocatableEvar1.alloc"}
32+
! CHECK-NEXT: %[[PRIV_ALLOCMEM_BOX:.*]] = fir.embox %[[PRIV_ALLOCMEM]] : (!fir.heap<i32>) -> !fir.box<!fir.heap<i32>>
33+
! CHECK-NEXT: fir.store %[[PRIV_ALLOCMEM_BOX]] to %[[PRIV_ALLOC]] : !fir.ref<!fir.box<!fir.heap<i32>>>
34+
! CHECK-NEXT: } else {
35+
! CHECK-NEXT: %[[ZERO_BITS:.*]] = fir.zero_bits !fir.heap<i32>
36+
! CHECK-NEXT: %[[ZERO_BOX:.*]] = fir.embox %[[ZERO_BITS]] : (!fir.heap<i32>) -> !fir.box<!fir.heap<i32>>
37+
! CHECK-NEXT: fir.store %[[ZERO_BOX]] to %[[PRIV_ALLOC]] : !fir.ref<!fir.box<!fir.heap<i32>>>
38+
! CHECK-NEXT: }
39+
40+
! CHECK-NEXT: %[[PRIV_DECL:.*]]:2 = hlfir.declare %[[PRIV_ALLOC]]
41+
! CHECK-NEXT: omp.yield(%[[PRIV_DECL]]#0 : [[TYPE]])
42+
43+
! CHECK-NEXT: }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
! Test delayed privatization for pointers: `private`.
2+
3+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -mmlir --openmp-enable-delayed-privatization \
4+
! RUN: -o - %s 2>&1 | FileCheck %s
5+
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %s 2>&1 |\
6+
! RUN: FileCheck %s
7+
8+
subroutine delayed_privatization_pointer
9+
implicit none
10+
integer, pointer :: var1
11+
12+
!$omp parallel firstprivate(var1)
13+
var1 = 10
14+
!$omp end parallel
15+
end subroutine
16+
17+
! CHECK-LABEL: omp.private {type = firstprivate}
18+
! CHECK-SAME: @[[PRIVATIZER_SYM:.*]] : [[TYPE:!fir.ref<!fir.box<!fir.ptr<i32>>>]] alloc {
19+
20+
! CHECK-NEXT: ^bb0(%[[PRIV_ARG:.*]]: [[TYPE]]):
21+
22+
! CHECK-NEXT: %[[PRIV_ALLOC:.*]] = fir.alloca !fir.box<!fir.ptr<i32>> {bindc_name = "var1", pinned, uniq_name = "_QFdelayed_privatization_pointerEvar1"}
23+
! CHECK-NEXT: %[[PRIV_DECL:.*]]:2 = hlfir.declare %[[PRIV_ALLOC]]
24+
! CHECK-NEXT: omp.yield(%[[PRIV_DECL]]#0 : [[TYPE]])
25+
26+
! CHECK-NEXT: } copy {
27+
! CHECK: ^bb0(%[[PRIV_ORIG_ARG:.*]]: [[TYPE]], %[[PRIV_PRIV_ARG:.*]]: [[TYPE]]):
28+
! CHECK-NEXT: %[[ORIG_BASE_VAL:.*]] = fir.load %[[PRIV_ORIG_ARG]]
29+
! CHECK-NEXT: fir.store %[[ORIG_BASE_VAL]] to %[[PRIV_PRIV_ARG]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
30+
! CHECK-NEXT: omp.yield(%[[PRIV_PRIV_ARG]] : [[TYPE]])
31+
! CHECK-NEXT: }

0 commit comments

Comments
 (0)