Skip to content

Commit 0c6516a

Browse files
committed
[flang] Basic PFT to MLIR lowering for do concurrent locality specifiers
1 parent 749a7a6 commit 0c6516a

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed

flang/include/flang/Lower/AbstractConverter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ class AbstractConverter {
348348
virtual Fortran::lower::SymbolBox
349349
lookupOneLevelUpSymbol(const Fortran::semantics::Symbol &sym) = 0;
350350

351+
virtual Fortran::lower::SymbolBox
352+
shallowLookupSymbol(const Fortran::semantics::Symbol &sym) = 0;
353+
351354
/// Return the mlir::SymbolTable associated to the ModuleOp.
352355
/// Look-ups are faster using it than using module.lookup<>,
353356
/// but the module op should be queried in case of failure

flang/lib/Lower/Bridge.cpp

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "flang/Lower/Bridge.h"
1414

15+
#include "OpenMP/DataSharingProcessor.h"
16+
#include "OpenMP/Utils.h"
1517
#include "flang/Lower/Allocatable.h"
1618
#include "flang/Lower/CallInterface.h"
1719
#include "flang/Lower/Coarray.h"
@@ -1136,6 +1138,14 @@ class FirConverter : public Fortran::lower::AbstractConverter {
11361138
return name;
11371139
}
11381140

1141+
/// Find the symbol in the inner-most level of the local map or return null.
1142+
Fortran::lower::SymbolBox
1143+
shallowLookupSymbol(const Fortran::semantics::Symbol &sym) override {
1144+
if (Fortran::lower::SymbolBox v = localSymbols.shallowLookupSymbol(sym))
1145+
return v;
1146+
return {};
1147+
}
1148+
11391149
private:
11401150
FirConverter() = delete;
11411151
FirConverter(const FirConverter &) = delete;
@@ -1210,14 +1220,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
12101220
return {};
12111221
}
12121222

1213-
/// Find the symbol in the inner-most level of the local map or return null.
1214-
Fortran::lower::SymbolBox
1215-
shallowLookupSymbol(const Fortran::semantics::Symbol &sym) {
1216-
if (Fortran::lower::SymbolBox v = localSymbols.shallowLookupSymbol(sym))
1217-
return v;
1218-
return {};
1219-
}
1220-
12211223
/// Find the symbol in one level up of symbol map such as for host-association
12221224
/// in OpenMP code or return null.
12231225
Fortran::lower::SymbolBox
@@ -2014,12 +2016,29 @@ class FirConverter : public Fortran::lower::AbstractConverter {
20142016

20152017
/// Create DO CONCURRENT construct symbol bindings and generate LOCAL_INIT
20162018
/// assignments.
2017-
void handleLocalitySpecs(const IncrementLoopInfo &info) {
2019+
void handleLocalitySpecs(IncrementLoopInfo &info) {
20182020
Fortran::semantics::SemanticsContext &semanticsContext =
20192021
bridge.getSemanticsContext();
2020-
for (const Fortran::semantics::Symbol *sym : info.localSymList)
2022+
Fortran::lower::omp::DataSharingProcessor dsp(
2023+
*this, semanticsContext, getEval(),
2024+
/*useDelayedPrivatization=*/true, localSymbols);
2025+
mlir::omp::PrivateClauseOps privateClauseOps;
2026+
2027+
for (const Fortran::semantics::Symbol *sym : info.localSymList) {
2028+
if (enableDelayedPrivatizationStaging) {
2029+
dsp.doPrivatize(sym, &privateClauseOps);
2030+
continue;
2031+
}
2032+
20212033
createHostAssociateVarClone(*sym, /*skipDefaultInit=*/false);
2034+
}
2035+
20222036
for (const Fortran::semantics::Symbol *sym : info.localInitSymList) {
2037+
if (enableDelayedPrivatizationStaging) {
2038+
dsp.doPrivatize(sym, &privateClauseOps);
2039+
continue;
2040+
}
2041+
20232042
createHostAssociateVarClone(*sym, /*skipDefaultInit=*/true);
20242043
const auto *hostDetails =
20252044
sym->detailsIf<Fortran::semantics::HostAssocDetails>();
@@ -2033,11 +2052,27 @@ class FirConverter : public Fortran::lower::AbstractConverter {
20332052
assign.u = Fortran::evaluate::Assignment::BoundsSpec{};
20342053
genAssignment(assign);
20352054
}
2055+
20362056
for (const Fortran::semantics::Symbol *sym : info.sharedSymList) {
20372057
const auto *hostDetails =
20382058
sym->detailsIf<Fortran::semantics::HostAssocDetails>();
20392059
copySymbolBinding(hostDetails->symbol(), *sym);
20402060
}
2061+
2062+
info.doLoop.getPrivateVarsMutable().assign(privateClauseOps.privateVars);
2063+
info.doLoop.setPrivateSymsAttr(
2064+
builder->getArrayAttr(privateClauseOps.privateSyms));
2065+
2066+
for (auto [sym, privateVar] : llvm::zip_equal(
2067+
dsp.getAllSymbolsToPrivatize(), privateClauseOps.privateVars)) {
2068+
auto arg = info.doLoop.getRegion().begin()->addArgument(
2069+
privateVar.getType(), info.doLoop.getLoc());
2070+
bindSymbol(*sym, hlfir::translateToExtendedValue(
2071+
privateVar.getLoc(), *builder, hlfir::Entity{arg},
2072+
/*contiguousHint=*/true)
2073+
.first);
2074+
}
2075+
20412076
// Note that allocatable, types with ultimate components, and type
20422077
// requiring finalization are forbidden in LOCAL/LOCAL_INIT (F2023 C1130),
20432078
// so no clean-up needs to be generated for these entities.

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ DataSharingProcessor::DataSharingProcessor(
5353
});
5454
}
5555

56+
DataSharingProcessor::DataSharingProcessor(lower::AbstractConverter &converter,
57+
semantics::SemanticsContext &semaCtx,
58+
lower::pft::Evaluation &eval,
59+
bool useDelayedPrivatization,
60+
lower::SymMap &symTable)
61+
: DataSharingProcessor(converter, semaCtx, {}, eval,
62+
/*shouldCollectPreDeterminedSymols=*/false,
63+
useDelayedPrivatization, symTable) {}
64+
5665
void DataSharingProcessor::processStep1(
5766
mlir::omp::PrivateClauseOps *clauseOps) {
5867
collectSymbolsForPrivatization();
@@ -498,20 +507,26 @@ void DataSharingProcessor::copyLastPrivatize(mlir::Operation *op) {
498507
}
499508
}
500509

501-
void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
510+
void DataSharingProcessor::doPrivatize(const semantics::Symbol *symToPrivatize,
502511
mlir::omp::PrivateClauseOps *clauseOps) {
503512
if (!useDelayedPrivatization) {
504-
cloneSymbol(sym);
505-
copyFirstPrivateSymbol(sym);
513+
cloneSymbol(symToPrivatize);
514+
copyFirstPrivateSymbol(symToPrivatize);
506515
return;
507516
}
508517

509-
lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol(*sym);
518+
const semantics::Symbol *sym = symToPrivatize->HasLocalLocality()
519+
? &symToPrivatize->GetUltimate()
520+
: symToPrivatize;
521+
lower::SymbolBox hsb = symToPrivatize->HasLocalLocality()
522+
? converter.shallowLookupSymbol(*sym)
523+
: converter.lookupOneLevelUpSymbol(*sym);
510524
assert(hsb && "Host symbol box not found");
511525

512526
mlir::Location symLoc = hsb.getAddr().getLoc();
513527
std::string privatizerName = sym->name().ToString() + ".privatizer";
514-
bool isFirstPrivate = sym->test(semantics::Symbol::Flag::OmpFirstPrivate);
528+
bool isFirstPrivate = sym->test(semantics::Symbol::Flag::OmpFirstPrivate) ||
529+
sym->test(semantics::Symbol::Flag::LocalityLocalInit);
515530

516531
mlir::Value privVal = hsb.getAddr();
517532
mlir::Type allocType = privVal.getType();
@@ -638,6 +653,8 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
638653
}
639654

640655
symToPrivatizer[sym] = privatizerOp;
656+
if (symToPrivatize->HasLocalLocality())
657+
allPrivatizedSymbols.insert(symToPrivatize);
641658
}
642659

643660
} // namespace omp

flang/lib/Lower/OpenMP/DataSharingProcessor.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ class DataSharingProcessor {
105105
void collectImplicitSymbols();
106106
void collectPreDeterminedSymbols();
107107
void privatize(mlir::omp::PrivateClauseOps *clauseOps);
108-
void doPrivatize(const semantics::Symbol *sym,
109-
mlir::omp::PrivateClauseOps *clauseOps);
110108
void copyLastPrivatize(mlir::Operation *op);
111109
void insertLastPrivateCompare(mlir::Operation *op);
112110
void cloneSymbol(const semantics::Symbol *sym);
@@ -125,6 +123,11 @@ class DataSharingProcessor {
125123
bool shouldCollectPreDeterminedSymbols,
126124
bool useDelayedPrivatization, lower::SymMap &symTable);
127125

126+
DataSharingProcessor(lower::AbstractConverter &converter,
127+
semantics::SemanticsContext &semaCtx,
128+
lower::pft::Evaluation &eval,
129+
bool useDelayedPrivatization, lower::SymMap &symTable);
130+
128131
// Privatisation is split into two steps.
129132
// Step1 performs cloning of all privatisation clauses and copying for
130133
// firstprivates. Step1 is performed at the place where process/processStep1
@@ -151,6 +154,9 @@ class DataSharingProcessor {
151154
? allPrivatizedSymbols.getArrayRef()
152155
: llvm::ArrayRef<const semantics::Symbol *>();
153156
}
157+
158+
void doPrivatize(const semantics::Symbol *sym,
159+
mlir::omp::PrivateClauseOps *clauseOps);
154160
};
155161

156162
} // namespace omp

0 commit comments

Comments
 (0)