2020#include " flang/Optimizer/Builder/BoxValue.h"
2121#include " flang/Optimizer/Builder/HLFIRTools.h"
2222#include " flang/Optimizer/Builder/Todo.h"
23+ #include " flang/Optimizer/Dialect/FIROps.h"
2324#include " flang/Optimizer/HLFIR/HLFIRDialect.h"
2425#include " flang/Optimizer/HLFIR/HLFIROps.h"
2526#include " flang/Semantics/attr.h"
@@ -53,6 +54,15 @@ DataSharingProcessor::DataSharingProcessor(
5354 });
5455}
5556
57+ DataSharingProcessor::DataSharingProcessor (lower::AbstractConverter &converter,
58+ semantics::SemanticsContext &semaCtx,
59+ lower::pft::Evaluation &eval,
60+ bool useDelayedPrivatization,
61+ lower::SymMap &symTable)
62+ : DataSharingProcessor(converter, semaCtx, {}, eval,
63+ /* shouldCollectPreDeterminedSymols=*/ false ,
64+ useDelayedPrivatization, symTable) {}
65+
5666void DataSharingProcessor::processStep1 (
5767 mlir::omp::PrivateClauseOps *clauseOps) {
5868 collectSymbolsForPrivatization ();
@@ -174,7 +184,8 @@ void DataSharingProcessor::cloneSymbol(const semantics::Symbol *sym) {
174184
175185void DataSharingProcessor::copyFirstPrivateSymbol (
176186 const semantics::Symbol *sym, mlir::OpBuilder::InsertPoint *copyAssignIP) {
177- if (sym->test (semantics::Symbol::Flag::OmpFirstPrivate))
187+ if (sym->test (semantics::Symbol::Flag::OmpFirstPrivate) ||
188+ sym->test (semantics::Symbol::Flag::LocalityLocalInit))
178189 converter.copyHostAssociateVar (*sym, copyAssignIP);
179190}
180191
@@ -497,9 +508,9 @@ void DataSharingProcessor::privatize(mlir::omp::PrivateClauseOps *clauseOps) {
497508 if (const auto *commonDet =
498509 sym->detailsIf <semantics::CommonBlockDetails>()) {
499510 for (const auto &mem : commonDet->objects ())
500- doPrivatize (&*mem, clauseOps);
511+ privatizeSymbol (&*mem, clauseOps);
501512 } else
502- doPrivatize (sym, clauseOps);
513+ privatizeSymbol (sym, clauseOps);
503514 }
504515}
505516
@@ -516,22 +527,30 @@ void DataSharingProcessor::copyLastPrivatize(mlir::Operation *op) {
516527 }
517528}
518529
519- void DataSharingProcessor::doPrivatize (const semantics::Symbol *sym,
520- mlir::omp::PrivateClauseOps *clauseOps) {
530+ template <typename OpType, typename OperandsStructType>
531+ void DataSharingProcessor::privatizeSymbol (
532+ const semantics::Symbol *symToPrivatize, OperandsStructType *clauseOps) {
521533 if (!useDelayedPrivatization) {
522- cloneSymbol (sym );
523- copyFirstPrivateSymbol (sym );
534+ cloneSymbol (symToPrivatize );
535+ copyFirstPrivateSymbol (symToPrivatize );
524536 return ;
525537 }
526538
527- lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol (*sym);
539+ const semantics::Symbol *sym = symToPrivatize->HasLocalLocality ()
540+ ? &symToPrivatize->GetUltimate ()
541+ : symToPrivatize;
542+ lower::SymbolBox hsb = symToPrivatize->HasLocalLocality ()
543+ ? converter.shallowLookupSymbol (*sym)
544+ : converter.lookupOneLevelUpSymbol (*sym);
528545 assert (hsb && " Host symbol box not found" );
529546 hlfir::Entity entity{hsb.getAddr ()};
530547 bool cannotHaveNonDefaultLowerBounds = !entity.mayHaveNonDefaultLowerBounds ();
531548
532549 mlir::Location symLoc = hsb.getAddr ().getLoc ();
533550 std::string privatizerName = sym->name ().ToString () + " .privatizer" ;
534- bool isFirstPrivate = sym->test (semantics::Symbol::Flag::OmpFirstPrivate);
551+ bool isFirstPrivate =
552+ symToPrivatize->test (semantics::Symbol::Flag::OmpFirstPrivate) ||
553+ symToPrivatize->test (semantics::Symbol::Flag::LocalityLocalInit);
535554
536555 mlir::Value privVal = hsb.getAddr ();
537556 mlir::Type allocType = privVal.getType ();
@@ -565,24 +584,33 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
565584
566585 mlir::Type argType = privVal.getType ();
567586
568- mlir::omp::PrivateClauseOp privatizerOp = [&]() {
587+ OpType privatizerOp = [&]() {
569588 auto moduleOp = firOpBuilder.getModule ();
570589 auto uniquePrivatizerName = fir::getTypeAsString (
571590 allocType, converter.getKindMap (),
572591 converter.mangleName (*sym) +
573592 (isFirstPrivate ? " _firstprivate" : " _private" ));
574593
575594 if (auto existingPrivatizer =
576- moduleOp.lookupSymbol <mlir::omp::PrivateClauseOp>(
577- uniquePrivatizerName))
595+ moduleOp.lookupSymbol <OpType>(uniquePrivatizerName))
578596 return existingPrivatizer;
579597
580598 mlir::OpBuilder::InsertionGuard guard (firOpBuilder);
581599 firOpBuilder.setInsertionPointToStart (moduleOp.getBody ());
582- auto result = firOpBuilder.create <mlir::omp::PrivateClauseOp>(
583- symLoc, uniquePrivatizerName, allocType,
584- isFirstPrivate ? mlir::omp::DataSharingClauseType::FirstPrivate
585- : mlir::omp::DataSharingClauseType::Private);
600+ OpType result;
601+
602+ if constexpr (std::is_same_v<OpType, mlir::omp::PrivateClauseOp>) {
603+ result = firOpBuilder.create <OpType>(
604+ symLoc, uniquePrivatizerName, allocType,
605+ isFirstPrivate ? mlir::omp::DataSharingClauseType::FirstPrivate
606+ : mlir::omp::DataSharingClauseType::Private);
607+ } else {
608+ result = firOpBuilder.create <OpType>(
609+ symLoc, uniquePrivatizerName, allocType,
610+ isFirstPrivate ? fir::LocalitySpecifierType::LocalInit
611+ : fir::LocalitySpecifierType::Local);
612+ }
613+
586614 fir::ExtendedValue symExV = converter.getSymbolExtendedValue (*sym);
587615 lower::SymMapScope outerScope (symTable);
588616
@@ -625,27 +653,36 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
625653 ©Region, /* insertPt=*/ {}, {argType, argType}, {symLoc, symLoc});
626654 firOpBuilder.setInsertionPointToEnd (copyEntryBlock);
627655
628- auto addSymbol = [&](unsigned argIdx, bool force = false ) {
656+ auto addSymbol = [&](unsigned argIdx, const semantics::Symbol *symToMap,
657+ bool force = false ) {
629658 symExV.match (
630659 [&](const fir::MutableBoxValue &box) {
631660 symTable.addSymbol (
632- *sym, fir::substBase (box, copyRegion. getArgument (argIdx)) ,
633- force);
661+ *symToMap ,
662+ fir::substBase (box, copyRegion. getArgument (argIdx)), force);
634663 },
635664 [&](const auto &box) {
636- symTable.addSymbol (*sym, copyRegion.getArgument (argIdx), force);
665+ symTable.addSymbol (*symToMap, copyRegion.getArgument (argIdx),
666+ force);
637667 });
638668 };
639669
640- addSymbol (0 , true );
670+ addSymbol (0 , sym, true );
641671 lower::SymMapScope innerScope (symTable);
642- addSymbol (1 );
672+ addSymbol (1 , symToPrivatize );
643673
644674 auto ip = firOpBuilder.saveInsertionPoint ();
645- copyFirstPrivateSymbol (sym, &ip);
646-
647- firOpBuilder.create <mlir::omp::YieldOp>(
648- hsb.getAddr ().getLoc (), symTable.shallowLookupSymbol (*sym).getAddr ());
675+ copyFirstPrivateSymbol (symToPrivatize, &ip);
676+
677+ if constexpr (std::is_same_v<OpType, mlir::omp::PrivateClauseOp>) {
678+ firOpBuilder.create <mlir::omp::YieldOp>(
679+ hsb.getAddr ().getLoc (),
680+ symTable.shallowLookupSymbol (*symToPrivatize).getAddr ());
681+ } else {
682+ firOpBuilder.create <fir::YieldOp>(
683+ hsb.getAddr ().getLoc (),
684+ symTable.shallowLookupSymbol (*symToPrivatize).getAddr ());
685+ }
649686 }
650687
651688 return result;
@@ -656,9 +693,22 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
656693 clauseOps->privateVars .push_back (privVal);
657694 }
658695
659- symToPrivatizer[sym] = privatizerOp;
696+ if (symToPrivatize->HasLocalLocality ())
697+ allPrivatizedSymbols.insert (symToPrivatize);
660698}
661699
700+ template void
701+ DataSharingProcessor::privatizeSymbol<mlir::omp::PrivateClauseOp,
702+ mlir::omp::PrivateClauseOps>(
703+ const semantics::Symbol *symToPrivatize,
704+ mlir::omp::PrivateClauseOps *clauseOps);
705+
706+ template void
707+ DataSharingProcessor::privatizeSymbol<fir::LocalitySpecifierOp,
708+ fir::LocalitySpecifierOperands>(
709+ const semantics::Symbol *symToPrivatize,
710+ fir::LocalitySpecifierOperands *clauseOps);
711+
662712} // namespace omp
663713} // namespace lower
664714} // namespace Fortran
0 commit comments