Skip to content

[flang][OpenMP] Map basic local specifiers to private clauses #142735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: users/ergawy/convert_locality_specs_to_clauses_2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ namespace Fortran {
namespace lower {
class AbstractConverter;

namespace omp {

enum class DeclOperationKind { Private, FirstPrivate, Reduction };
enum class DeclOperationKind {
PrivateOrLocal,
FirstPrivateOrLocalInit,
Reduction
};
inline bool isPrivatization(DeclOperationKind kind) {
return (kind == DeclOperationKind::FirstPrivate) ||
(kind == DeclOperationKind::Private);
return (kind == DeclOperationKind::FirstPrivateOrLocalInit) ||
(kind == DeclOperationKind::PrivateOrLocal);
}
inline bool isReduction(DeclOperationKind kind) {
return kind == DeclOperationKind::Reduction;
Expand All @@ -56,7 +58,7 @@ void populateByRefInitAndCleanupRegions(
mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
mlir::Region &cleanupRegion, DeclOperationKind kind,
const Fortran::semantics::Symbol *sym = nullptr,
bool cannotHaveNonDefaultLowerBounds = false);
bool cannotHaveNonDefaultLowerBounds = false, bool isDoConcurrent = false);

/// Generate a fir::ShapeShift op describing the provided boxed array.
/// `cannotHaveNonDefaultLowerBounds` should be set if `box` is known to have
Expand All @@ -69,7 +71,6 @@ fir::ShapeShiftOp getShapeShift(fir::FirOpBuilder &builder, mlir::Location loc,
bool cannotHaveNonDefaultLowerBounds = false,
bool useDefaultLowerBounds = false);

} // namespace omp
} // namespace lower
} // namespace Fortran

Expand Down
4 changes: 3 additions & 1 deletion flang/include/flang/Lower/Support/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringRef.h"

namespace Fortran::lower {
Expand Down Expand Up @@ -98,8 +99,9 @@ bool isEqual(const Fortran::lower::ExplicitIterSpace::ArrayBases &x,
template <typename OpType, typename OperandsStructType>
void privatizeSymbol(
lower::AbstractConverter &converter, fir::FirOpBuilder &firOpBuilder,
lower::SymMap &symTable, std::function<void(OpType, mlir::Type)> initGen,
lower::SymMap &symTable,
llvm::SetVector<const semantics::Symbol *> &allPrivatizedSymbols,
llvm::SmallSet<const semantics::Symbol *, 16> &mightHaveReadHostSym,
const semantics::Symbol *symToPrivatize, OperandsStructType *clauseOps);

} // end namespace Fortran::lower
Expand Down
39 changes: 14 additions & 25 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "flang/Lower/Bridge.h"

#include "OpenMP/DataSharingProcessor.h"
#include "flang/Lower/Allocatable.h"
#include "flang/Lower/CallInterface.h"
#include "flang/Lower/Coarray.h"
Expand Down Expand Up @@ -2034,50 +2033,40 @@ class FirConverter : public Fortran::lower::AbstractConverter {
fir::LocalitySpecifierOperands privateClauseOps;
auto doConcurrentLoopOp =
mlir::dyn_cast_if_present<fir::DoConcurrentLoopOp>(info.loopOp);
// TODO Promote to using `enableDelayedPrivatization` (which is enabled by
// default unlike the staging flag) once the implementation of this is more
// complete.
bool useDelayedPriv =
enableDelayedPrivatizationStaging && doConcurrentLoopOp;
bool useDelayedPriv = enableDelayedPrivatization && doConcurrentLoopOp;
llvm::SetVector<const Fortran::semantics::Symbol *> allPrivatizedSymbols;
llvm::SmallSet<const Fortran::semantics::Symbol *, 16> mightHaveReadHostSym;

for (const Fortran::semantics::Symbol *sym : info.localSymList) {
for (const Fortran::semantics::Symbol *symToPrivatize : info.localSymList) {
if (useDelayedPriv) {
Fortran::lower::privatizeSymbol<fir::LocalitySpecifierOp>(
*this, this->getFirOpBuilder(), localSymbols,
[this](fir::LocalitySpecifierOp result, mlir::Type argType) {
TODO(this->toLocation(),
"Localizers that need init regions are not supported yet.");
},
allPrivatizedSymbols, sym, &privateClauseOps);
*this, this->getFirOpBuilder(), localSymbols, allPrivatizedSymbols,
mightHaveReadHostSym, symToPrivatize, &privateClauseOps);
continue;
}

createHostAssociateVarClone(*sym, /*skipDefaultInit=*/false);
createHostAssociateVarClone(*symToPrivatize, /*skipDefaultInit=*/false);
}

for (const Fortran::semantics::Symbol *sym : info.localInitSymList) {
for (const Fortran::semantics::Symbol *symToPrivatize :
info.localInitSymList) {
if (useDelayedPriv) {
Fortran::lower::privatizeSymbol<fir::LocalitySpecifierOp>(
*this, this->getFirOpBuilder(), localSymbols,
[this](fir::LocalitySpecifierOp result, mlir::Type argType) {
TODO(this->toLocation(),
"Localizers that need init regions are not supported yet.");
},
allPrivatizedSymbols, sym, &privateClauseOps);
*this, this->getFirOpBuilder(), localSymbols, allPrivatizedSymbols,
mightHaveReadHostSym, symToPrivatize, &privateClauseOps);
continue;
}

createHostAssociateVarClone(*sym, /*skipDefaultInit=*/true);
createHostAssociateVarClone(*symToPrivatize, /*skipDefaultInit=*/true);
const auto *hostDetails =
sym->detailsIf<Fortran::semantics::HostAssocDetails>();
symToPrivatize->detailsIf<Fortran::semantics::HostAssocDetails>();
assert(hostDetails && "missing locality spec host symbol");
const Fortran::semantics::Symbol *hostSym = &hostDetails->symbol();
Fortran::evaluate::ExpressionAnalyzer ea{semanticsContext};
Fortran::evaluate::Assignment assign{
ea.Designate(Fortran::evaluate::DataRef{*sym}).value(),
ea.Designate(Fortran::evaluate::DataRef{*symToPrivatize}).value(),
ea.Designate(Fortran::evaluate::DataRef{*hostSym}).value()};
if (Fortran::semantics::IsPointer(*sym))
if (Fortran::semantics::IsPointer(*symToPrivatize))
assign.u = Fortran::evaluate::Assignment::BoundsSpec{};
genAssignment(assign);
}
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ add_flang_library(FortranLower
OpenMP/DataSharingProcessor.cpp
OpenMP/Decomposer.cpp
OpenMP/OpenMP.cpp
OpenMP/PrivateReductionUtils.cpp
OpenMP/ReductionProcessor.cpp
OpenMP/Utils.cpp
PFTBuilder.cpp
Runtime.cpp
Support/PrivateReductionUtils.cpp
Support/Utils.cpp
SymbolMap.cpp
VectorSubscripts.cpp
Expand Down
34 changes: 3 additions & 31 deletions flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

#include "DataSharingProcessor.h"

#include "PrivateReductionUtils.h"
#include "Utils.h"
#include "flang/Lower/ConvertVariable.h"
#include "flang/Lower/PFTBuilder.h"
#include "flang/Lower/Support/PrivateReductionUtils.h"
#include "flang/Lower/Support/Utils.h"
#include "flang/Lower/SymbolMap.h"
#include "flang/Optimizer/Builder/BoxValue.h"
Expand Down Expand Up @@ -537,38 +537,10 @@ void DataSharingProcessor::privatizeSymbol(
return;
}

auto initGen = [&](mlir::omp::PrivateClauseOp result, mlir::Type argType) {
lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol(*symToPrivatize);
assert(hsb && "Host symbol box not found");
hlfir::Entity entity{hsb.getAddr()};
bool cannotHaveNonDefaultLowerBounds =
!entity.mayHaveNonDefaultLowerBounds();

mlir::Region &initRegion = result.getInitRegion();
mlir::Location symLoc = hsb.getAddr().getLoc();
mlir::Block *initBlock = firOpBuilder.createBlock(
&initRegion, /*insertPt=*/{}, {argType, argType}, {symLoc, symLoc});

bool emitCopyRegion =
symToPrivatize->test(semantics::Symbol::Flag::OmpFirstPrivate);

populateByRefInitAndCleanupRegions(
converter, symLoc, argType, /*scalarInitValue=*/nullptr, initBlock,
result.getInitPrivateArg(), result.getInitMoldArg(),
result.getDeallocRegion(),
emitCopyRegion ? omp::DeclOperationKind::FirstPrivate
: omp::DeclOperationKind::Private,
symToPrivatize, cannotHaveNonDefaultLowerBounds);
// TODO: currently there are false positives from dead uses of the mold
// arg
if (result.initReadsFromMold())
mightHaveReadHostSym.insert(symToPrivatize);
};

Fortran::lower::privatizeSymbol<mlir::omp::PrivateClauseOp,
mlir::omp::PrivateClauseOps>(
converter, firOpBuilder, symTable, initGen, allPrivatizedSymbols,
symToPrivatize, clauseOps);
converter, firOpBuilder, symTable, allPrivatizedSymbols,
mightHaveReadHostSym, symToPrivatize, clauseOps);
}
} // namespace omp
} // namespace lower
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/OpenMP/ReductionProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

#include "ReductionProcessor.h"

#include "PrivateReductionUtils.h"
#include "flang/Lower/AbstractConverter.h"
#include "flang/Lower/ConvertType.h"
#include "flang/Lower/Support/PrivateReductionUtils.h"
#include "flang/Lower/SymbolMap.h"
#include "flang/Optimizer/Builder/Complex.h"
#include "flang/Optimizer/Builder/HLFIRTools.h"
Expand Down
Loading