32
32
#include " mlir/Dialect/SCF/IR/SCF.h"
33
33
#include " mlir/Transforms/RegionUtils.h"
34
34
#include " llvm/ADT/STLExtras.h"
35
+ #include " llvm/ADT/StringSet.h"
35
36
#include " llvm/Frontend/OpenMP/OMPConstants.h"
36
37
#include " llvm/Support/CommandLine.h"
37
38
@@ -169,7 +170,8 @@ class DataSharingProcessor {
169
170
const Fortran::parser::OmpClauseList &opClauseList;
170
171
Fortran::lower::pft::Evaluation &eval;
171
172
172
- bool useDelayedPrivatizationWhenPossible;
173
+ bool useDelayedPrivatization;
174
+ llvm::SetVector<mlir::StringRef> existingPrivatizerNames;
173
175
Fortran::lower::SymMap *symTable;
174
176
175
177
DelayedPrivatizationInfo delayedPrivatizationInfo;
@@ -184,6 +186,8 @@ class DataSharingProcessor {
184
186
void collectDefaultSymbols ();
185
187
void privatize ();
186
188
void defaultPrivatize ();
189
+ void doPrivatize (const Fortran::semantics::Symbol *sym);
190
+
187
191
void copyLastPrivatize (mlir::Operation *op);
188
192
void insertLastPrivateCompare (mlir::Operation *op);
189
193
void cloneSymbol (const Fortran::semantics::Symbol *sym);
@@ -196,13 +200,19 @@ class DataSharingProcessor {
196
200
DataSharingProcessor (Fortran::lower::AbstractConverter &converter,
197
201
const Fortran::parser::OmpClauseList &opClauseList,
198
202
Fortran::lower::pft::Evaluation &eval,
199
- bool useDelayedPrivatizationWhenPossible = false ,
203
+ bool useDelayedPrivatization = false ,
200
204
Fortran::lower::SymMap *symTable = nullptr )
201
205
: hasLastPrivateOp(false ), converter(converter),
202
206
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
+
206
216
// Privatisation is split into two steps.
207
217
// Step1 performs cloning of all privatisation clauses and copying for
208
218
// firstprivates. Step1 is performed at the place where process/processStep1
@@ -509,56 +519,15 @@ void DataSharingProcessor::collectDefaultSymbols() {
509
519
}
510
520
511
521
void DataSharingProcessor::privatize () {
522
+
512
523
for (const Fortran::semantics::Symbol *sym : privatizedSymbols) {
513
524
if (const auto *commonDet =
514
525
sym->detailsIf <Fortran::semantics::CommonBlockDetails>()) {
515
526
for (const auto &mem : commonDet->objects ()) {
516
- cloneSymbol (&*mem);
517
- copyFirstPrivateSymbol (&*mem);
527
+ doPrivatize (&*mem);
518
528
}
519
529
} 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);
562
531
}
563
532
}
564
533
}
@@ -584,12 +553,66 @@ void DataSharingProcessor::defaultPrivatize() {
584
553
!symbolsInNestedRegions.contains (sym) &&
585
554
!symbolsInParentRegions.contains (sym) &&
586
555
!privatizedSymbols.contains (sym)) {
587
- cloneSymbol (sym);
588
- copyFirstPrivateSymbol (sym);
556
+ doPrivatize (sym);
589
557
}
590
558
}
591
559
}
592
560
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
+
593
616
// ===----------------------------------------------------------------------===//
594
617
// ClauseProcessor
595
618
// ===----------------------------------------------------------------------===//
@@ -2576,8 +2599,7 @@ genParallelOp(Fortran::lower::AbstractConverter &converter,
2576
2599
2577
2600
bool privatize = !outerCombined;
2578
2601
DataSharingProcessor dsp (converter, clauseList, eval,
2579
- /* useDelayedPrivatizationWhenPossible=*/ true ,
2580
- &symTable);
2602
+ /* useDelayedPrivatization=*/ true , &symTable);
2581
2603
2582
2604
if (privatize) {
2583
2605
dsp.processStep1 ();
0 commit comments