@@ -66,9 +66,10 @@ void DataSharingProcessor::cloneSymbol(const Fortran::semantics::Symbol *sym) {
66
66
}
67
67
68
68
void DataSharingProcessor::copyFirstPrivateSymbol (
69
- const Fortran::semantics::Symbol *sym) {
69
+ const Fortran::semantics::Symbol *sym,
70
+ mlir::OpBuilder::InsertPoint *copyAssignIP) {
70
71
if (sym->test (Fortran::semantics::Symbol::Flag::OmpFirstPrivate))
71
- converter.copyHostAssociateVar (*sym);
72
+ converter.copyHostAssociateVar (*sym, copyAssignIP );
72
73
}
73
74
74
75
void DataSharingProcessor::copyLastPrivateSymbol (
@@ -307,14 +308,10 @@ void DataSharingProcessor::privatize() {
307
308
for (const Fortran::semantics::Symbol *sym : privatizedSymbols) {
308
309
if (const auto *commonDet =
309
310
sym->detailsIf <Fortran::semantics::CommonBlockDetails>()) {
310
- for (const auto &mem : commonDet->objects ()) {
311
- cloneSymbol (&*mem);
312
- copyFirstPrivateSymbol (&*mem);
313
- }
314
- } else {
315
- cloneSymbol (sym);
316
- copyFirstPrivateSymbol (sym);
317
- }
311
+ for (const auto &mem : commonDet->objects ())
312
+ doPrivatize (&*mem);
313
+ } else
314
+ doPrivatize (sym);
318
315
}
319
316
}
320
317
@@ -338,11 +335,95 @@ void DataSharingProcessor::defaultPrivatize() {
338
335
!sym->GetUltimate ().has <Fortran::semantics::NamelistDetails>() &&
339
336
!symbolsInNestedRegions.contains (sym) &&
340
337
!symbolsInParentRegions.contains (sym) &&
341
- !privatizedSymbols.contains (sym)) {
338
+ !privatizedSymbols.contains (sym))
339
+ doPrivatize (sym);
340
+ }
341
+ }
342
+
343
+ void DataSharingProcessor::doPrivatize (const Fortran::semantics::Symbol *sym) {
344
+ if (!useDelayedPrivatization) {
345
+ cloneSymbol (sym);
346
+ copyFirstPrivateSymbol (sym);
347
+ return ;
348
+ }
349
+
350
+ Fortran::lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol (*sym);
351
+ assert (hsb && " Host symbol box not found" );
352
+
353
+ mlir::Type symType = hsb.getAddr ().getType ();
354
+ mlir::Location symLoc = hsb.getAddr ().getLoc ();
355
+ std::string privatizerName = sym->name ().ToString () + " .privatizer" ;
356
+ bool isFirstPrivate =
357
+ sym->test (Fortran::semantics::Symbol::Flag::OmpFirstPrivate);
358
+
359
+ mlir::omp::PrivateClauseOp privatizerOp = [&]() {
360
+ auto moduleOp = firOpBuilder.getModule ();
361
+ auto uniquePrivatizerName = fir::getTypeAsString (
362
+ symType, converter.getKindMap (),
363
+ converter.mangleName (*sym) +
364
+ (isFirstPrivate ? " _firstprivate" : " _private" ));
365
+
366
+ if (auto existingPrivatizer =
367
+ moduleOp.lookupSymbol <mlir::omp::PrivateClauseOp>(
368
+ uniquePrivatizerName))
369
+ return existingPrivatizer;
370
+
371
+ auto ip = firOpBuilder.saveInsertionPoint ();
372
+ firOpBuilder.setInsertionPoint (&moduleOp.getBodyRegion ().front (),
373
+ moduleOp.getBodyRegion ().front ().begin ());
374
+ auto result = firOpBuilder.create <mlir::omp::PrivateClauseOp>(
375
+ symLoc, uniquePrivatizerName, symType,
376
+ isFirstPrivate ? mlir::omp::DataSharingClauseType::FirstPrivate
377
+ : mlir::omp::DataSharingClauseType::Private);
378
+
379
+ symTable->pushScope ();
380
+
381
+ // Populate the `alloc` region.
382
+ {
383
+ mlir::Region &allocRegion = result.getAllocRegion ();
384
+ mlir::Block *allocEntryBlock = firOpBuilder.createBlock (
385
+ &allocRegion, /* insertPt=*/ {}, symType, symLoc);
386
+
387
+ firOpBuilder.setInsertionPointToEnd (allocEntryBlock);
388
+ symTable->addSymbol (*sym, allocRegion.getArgument (0 ));
389
+ symTable->pushScope ();
342
390
cloneSymbol (sym);
343
- copyFirstPrivateSymbol (sym);
391
+ firOpBuilder.create <mlir::omp::YieldOp>(
392
+ hsb.getAddr ().getLoc (),
393
+ symTable->shallowLookupSymbol (*sym).getAddr ());
394
+ symTable->popScope ();
344
395
}
345
- }
396
+
397
+ // Populate the `copy` region if this is a `firstprivate`.
398
+ if (isFirstPrivate) {
399
+ mlir::Region ©Region = result.getCopyRegion ();
400
+ // First block argument corresponding to the original/host value while
401
+ // second block argument corresponding to the privatized value.
402
+ mlir::Block *copyEntryBlock = firOpBuilder.createBlock (
403
+ ©Region, /* insertPt=*/ {}, {symType, symType}, {symLoc, symLoc});
404
+ firOpBuilder.setInsertionPointToEnd (copyEntryBlock);
405
+ symTable->addSymbol (*sym, copyRegion.getArgument (0 ),
406
+ /* force=*/ true );
407
+ symTable->pushScope ();
408
+ symTable->addSymbol (*sym, copyRegion.getArgument (1 ));
409
+ auto ip = firOpBuilder.saveInsertionPoint ();
410
+ copyFirstPrivateSymbol (sym, &ip);
411
+
412
+ firOpBuilder.create <mlir::omp::YieldOp>(
413
+ hsb.getAddr ().getLoc (),
414
+ symTable->shallowLookupSymbol (*sym).getAddr ());
415
+ symTable->popScope ();
416
+ }
417
+
418
+ symTable->popScope ();
419
+ firOpBuilder.restoreInsertionPoint (ip);
420
+ return result;
421
+ }();
422
+
423
+ delayedPrivatizationInfo.privatizers .push_back (
424
+ mlir::SymbolRefAttr::get (privatizerOp));
425
+ delayedPrivatizationInfo.originalAddresses .push_back (hsb.getAddr ());
426
+ delayedPrivatizationInfo.symbols .push_back (sym);
346
427
}
347
428
348
429
} // namespace omp
0 commit comments