3232#include " mlir/Dialect/SCF/IR/SCF.h"
3333#include " mlir/Transforms/RegionUtils.h"
3434#include " llvm/ADT/STLExtras.h"
35+ #include " llvm/ADT/StringSet.h"
3536#include " llvm/Frontend/OpenMP/OMPConstants.h"
3637#include " llvm/Support/CommandLine.h"
3738
@@ -169,7 +170,8 @@ class DataSharingProcessor {
169170 const Fortran::parser::OmpClauseList &opClauseList;
170171 Fortran::lower::pft::Evaluation &eval;
171172
172- bool useDelayedPrivatizationWhenPossible;
173+ bool useDelayedPrivatization;
174+ llvm::SetVector<mlir::StringRef> existingPrivatizerNames;
173175 Fortran::lower::SymMap *symTable;
174176
175177 DelayedPrivatizationInfo delayedPrivatizationInfo;
@@ -184,6 +186,8 @@ class DataSharingProcessor {
184186 void collectDefaultSymbols ();
185187 void privatize ();
186188 void defaultPrivatize ();
189+ void doPrivatize (const Fortran::semantics::Symbol *sym);
190+
187191 void copyLastPrivatize (mlir::Operation *op);
188192 void insertLastPrivateCompare (mlir::Operation *op);
189193 void cloneSymbol (const Fortran::semantics::Symbol *sym);
@@ -196,13 +200,19 @@ class DataSharingProcessor {
196200 DataSharingProcessor (Fortran::lower::AbstractConverter &converter,
197201 const Fortran::parser::OmpClauseList &opClauseList,
198202 Fortran::lower::pft::Evaluation &eval,
199- bool useDelayedPrivatizationWhenPossible = false ,
203+ bool useDelayedPrivatization = false ,
200204 Fortran::lower::SymMap *symTable = nullptr )
201205 : hasLastPrivateOp(false ), converter(converter),
202206 firOpBuilder (converter.getFirOpBuilder()), opClauseList(opClauseList),
203- eval(eval), useDelayedPrivatizationWhenPossible(
204- useDelayedPrivatizationWhenPossible),
205- symTable(symTable) {}
207+ eval(eval), useDelayedPrivatization(useDelayedPrivatization),
208+ symTable(symTable) {
209+ for (auto privateOp : converter.getModuleOp ()
210+ .getRegion ()
211+ .getOps <mlir::omp::PrivateClauseOp>()) {
212+ existingPrivatizerNames.insert (privateOp.getSymName ());
213+ }
214+ }
215+
206216 // Privatisation is split into two steps.
207217 // Step1 performs cloning of all privatisation clauses and copying for
208218 // firstprivates. Step1 is performed at the place where process/processStep1
@@ -509,56 +519,15 @@ void DataSharingProcessor::collectDefaultSymbols() {
509519}
510520
511521void DataSharingProcessor::privatize () {
522+
512523 for (const Fortran::semantics::Symbol *sym : privatizedSymbols) {
513524 if (const auto *commonDet =
514525 sym->detailsIf <Fortran::semantics::CommonBlockDetails>()) {
515526 for (const auto &mem : commonDet->objects ()) {
516- cloneSymbol (&*mem);
517- copyFirstPrivateSymbol (&*mem);
527+ doPrivatize (&*mem);
518528 }
519529 } else {
520- if (useDelayedPrivatizationWhenPossible) {
521- auto ip = firOpBuilder.saveInsertionPoint ();
522-
523- auto moduleOp = firOpBuilder.getInsertionBlock ()
524- ->getParentOp ()
525- ->getParentOfType <mlir::ModuleOp>();
526-
527- firOpBuilder.setInsertionPoint (&moduleOp.getBodyRegion ().front (),
528- moduleOp.getBodyRegion ().front ().end ());
529-
530- Fortran::lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol (*sym);
531- assert (hsb && " Host symbol box not found" );
532-
533- auto symType = hsb.getAddr ().getType ();
534- auto symLoc = hsb.getAddr ().getLoc ();
535- auto privatizerOp = firOpBuilder.create <mlir::omp::PrivateClauseOp>(
536- symLoc, symType, sym->name ().ToString ());
537- firOpBuilder.setInsertionPointToEnd (&privatizerOp.getBody ().front ());
538-
539- symTable->pushScope ();
540- symTable->addSymbol (*sym, privatizerOp.getArgument (0 ));
541- symTable->pushScope ();
542-
543- cloneSymbol (sym);
544- copyFirstPrivateSymbol (sym);
545-
546- firOpBuilder.create <mlir::omp::YieldOp>(
547- hsb.getAddr ().getLoc (),
548- symTable->shallowLookupSymbol (*sym).getAddr ());
549-
550- symTable->popScope ();
551- symTable->popScope ();
552- firOpBuilder.restoreInsertionPoint (ip);
553-
554- delayedPrivatizationInfo.privatizers .insert (
555- mlir::SymbolRefAttr::get (privatizerOp));
556- delayedPrivatizationInfo.hostAddresses .insert (hsb.getAddr ());
557- delayedPrivatizationInfo.hostSymbols .insert (sym);
558- } else {
559- cloneSymbol (sym);
560- copyFirstPrivateSymbol (sym);
561- }
530+ doPrivatize (sym);
562531 }
563532 }
564533}
@@ -584,12 +553,66 @@ void DataSharingProcessor::defaultPrivatize() {
584553 !symbolsInNestedRegions.contains (sym) &&
585554 !symbolsInParentRegions.contains (sym) &&
586555 !privatizedSymbols.contains (sym)) {
587- cloneSymbol (sym);
588- copyFirstPrivateSymbol (sym);
556+ doPrivatize (sym);
589557 }
590558 }
591559}
592560
561+ void DataSharingProcessor::doPrivatize (const Fortran::semantics::Symbol *sym) {
562+ if (useDelayedPrivatization) {
563+ auto ip = firOpBuilder.saveInsertionPoint ();
564+
565+ auto moduleOp = firOpBuilder.getInsertionBlock ()
566+ ->getParentOp ()
567+ ->getParentOfType <mlir::ModuleOp>();
568+
569+ firOpBuilder.setInsertionPoint (&moduleOp.getBodyRegion ().front (),
570+ moduleOp.getBodyRegion ().front ().end ());
571+
572+ Fortran::lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol (*sym);
573+ assert (hsb && " Host symbol box not found" );
574+
575+ mlir::Type symType = hsb.getAddr ().getType ();
576+ mlir::Location symLoc = hsb.getAddr ().getLoc ();
577+ std::string privatizerName = sym->name ().ToString () + " .privatizer" ;
578+
579+ unsigned uniquingCounter = 0 ;
580+ auto uniquePrivatizerName = mlir::SymbolTable::generateSymbolName<64 >(
581+ privatizerName,
582+ [&](auto &suggestedName) {
583+ return existingPrivatizerNames.count (suggestedName);
584+ },
585+ uniquingCounter);
586+
587+ auto privatizerOp = firOpBuilder.create <mlir::omp::PrivateClauseOp>(
588+ symLoc, symType, uniquePrivatizerName);
589+ firOpBuilder.setInsertionPointToEnd (&privatizerOp.getBody ().front ());
590+
591+ symTable->pushScope ();
592+ symTable->addSymbol (*sym, privatizerOp.getArgument (0 ));
593+ symTable->pushScope ();
594+
595+ cloneSymbol (sym);
596+ copyFirstPrivateSymbol (sym);
597+
598+ firOpBuilder.create <mlir::omp::YieldOp>(
599+ hsb.getAddr ().getLoc (), symTable->shallowLookupSymbol (*sym).getAddr ());
600+
601+ symTable->popScope ();
602+ symTable->popScope ();
603+ firOpBuilder.restoreInsertionPoint (ip);
604+
605+ delayedPrivatizationInfo.privatizers .insert (
606+ mlir::SymbolRefAttr::get (privatizerOp));
607+ delayedPrivatizationInfo.hostAddresses .insert (hsb.getAddr ());
608+ delayedPrivatizationInfo.hostSymbols .insert (sym);
609+ existingPrivatizerNames.insert (uniquePrivatizerName);
610+ } else {
611+ cloneSymbol (sym);
612+ copyFirstPrivateSymbol (sym);
613+ }
614+ }
615+
593616// ===----------------------------------------------------------------------===//
594617// ClauseProcessor
595618// ===----------------------------------------------------------------------===//
@@ -2576,8 +2599,7 @@ genParallelOp(Fortran::lower::AbstractConverter &converter,
25762599
25772600 bool privatize = !outerCombined;
25782601 DataSharingProcessor dsp (converter, clauseList, eval,
2579- /* useDelayedPrivatizationWhenPossible=*/ true ,
2580- &symTable);
2602+ /* useDelayedPrivatization=*/ true , &symTable);
25812603
25822604 if (privatize) {
25832605 dsp.processStep1 ();
0 commit comments