1010//
1111// ===----------------------------------------------------------------------===//
1212
13- #include " PrivateReductionUtils.h"
13+ #include " flang/Lower/Support/ PrivateReductionUtils.h"
1414
1515#include " flang/Lower/AbstractConverter.h"
1616#include " flang/Lower/Allocatable.h"
@@ -42,7 +42,8 @@ static bool hasFinalization(const Fortran::semantics::Symbol &sym) {
4242static void createCleanupRegion (Fortran::lower::AbstractConverter &converter,
4343 mlir::Location loc, mlir::Type argType,
4444 mlir::Region &cleanupRegion,
45- const Fortran::semantics::Symbol *sym) {
45+ const Fortran::semantics::Symbol *sym,
46+ bool isDoConcurrent) {
4647 fir::FirOpBuilder &builder = converter.getFirOpBuilder ();
4748 assert (cleanupRegion.empty ());
4849 mlir::Block *block = builder.createBlock (&cleanupRegion, cleanupRegion.end (),
@@ -72,7 +73,10 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
7273 fir::MutableBoxValue mutableBox{converted, /* lenParameters=*/ {},
7374 /* mutableProperties=*/ {}};
7475 Fortran::lower::genDeallocateIfAllocated (converter, mutableBox, loc);
75- builder.create <mlir::omp::YieldOp>(loc);
76+ if (isDoConcurrent)
77+ builder.create <fir::YieldOp>(loc);
78+ else
79+ builder.create <mlir::omp::YieldOp>(loc);
7680 return ;
7781 }
7882 }
@@ -100,7 +104,10 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
100104 builder.create <fir::FreeMemOp>(loc, cast);
101105
102106 builder.setInsertionPointAfter (ifOp);
103- builder.create <mlir::omp::YieldOp>(loc);
107+ if (isDoConcurrent)
108+ builder.create <fir::YieldOp>(loc);
109+ else
110+ builder.create <mlir::omp::YieldOp>(loc);
104111 return ;
105112 }
106113
@@ -115,14 +122,18 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
115122 addr = builder.createConvert (loc, heapTy, addr);
116123
117124 builder.create <fir::FreeMemOp>(loc, addr);
118- builder.create <mlir::omp::YieldOp>(loc);
125+ if (isDoConcurrent)
126+ builder.create <fir::YieldOp>(loc);
127+ else
128+ builder.create <mlir::omp::YieldOp>(loc);
129+
119130 return ;
120131 }
121132
122133 typeError ();
123134}
124135
125- fir::ShapeShiftOp Fortran::lower::omp:: getShapeShift (
136+ fir::ShapeShiftOp Fortran::lower::getShapeShift (
126137 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value box,
127138 bool cannotHaveNonDefaultLowerBounds, bool useDefaultLowerBounds) {
128139 fir::SequenceType sequenceType = mlir::cast<fir::SequenceType>(
@@ -262,7 +273,7 @@ static mlir::Value generateZeroShapeForRank(fir::FirOpBuilder &builder,
262273}
263274
264275namespace {
265- using namespace Fortran ::lower::omp ;
276+ using namespace Fortran ::lower;
266277// / Class to store shared data so we don't have to maintain so many function
267278// / arguments
268279class PopulateInitAndCleanupRegionsHelper {
@@ -273,12 +284,13 @@ class PopulateInitAndCleanupRegionsHelper {
273284 mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
274285 mlir::Block *initBlock, mlir::Region &cleanupRegion,
275286 DeclOperationKind kind, const Fortran::semantics::Symbol *sym,
276- bool cannotHaveLowerBounds)
287+ bool cannotHaveLowerBounds, bool isDoConcurrent )
277288 : converter{converter}, builder{converter.getFirOpBuilder ()}, loc{loc},
278289 argType{argType}, scalarInitValue{scalarInitValue},
279290 allocatedPrivVarArg{allocatedPrivVarArg}, moldArg{moldArg},
280291 initBlock{initBlock}, cleanupRegion{cleanupRegion}, kind{kind},
281- sym{sym}, cannotHaveNonDefaultLowerBounds{cannotHaveLowerBounds} {
292+ sym{sym}, cannotHaveNonDefaultLowerBounds{cannotHaveLowerBounds},
293+ isDoConcurrent{isDoConcurrent} {
282294 valType = fir::unwrapRefType (argType);
283295 }
284296
@@ -324,8 +336,13 @@ class PopulateInitAndCleanupRegionsHelper {
324336 // / lower bounds then we don't need to generate code to read them.
325337 bool cannotHaveNonDefaultLowerBounds;
326338
339+ bool isDoConcurrent;
340+
327341 void createYield (mlir::Value ret) {
328- builder.create <mlir::omp::YieldOp>(loc, ret);
342+ if (isDoConcurrent)
343+ builder.create <fir::YieldOp>(loc, ret);
344+ else
345+ builder.create <mlir::omp::YieldOp>(loc, ret);
329346 }
330347
331348 void initTrivialType () {
@@ -429,11 +446,12 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedScalar(
429446 /* slice=*/ mlir::Value{}, lenParams);
430447 initializeIfDerivedTypeBox (
431448 builder, loc, box, getLoadedMoldArg (), needsInitialization,
432- /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivate );
449+ /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivateOrLocalInit );
433450 fir::StoreOp lastOp =
434451 builder.create <fir::StoreOp>(loc, box, allocatedPrivVarArg);
435452
436- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
453+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
454+ isDoConcurrent);
437455
438456 if (ifUnallocated)
439457 builder.setInsertionPointAfter (ifUnallocated);
@@ -470,13 +488,14 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
470488 allocatedArray, shape);
471489 initializeIfDerivedTypeBox (
472490 builder, loc, firClass, source, needsInitialization,
473- /* isFirstprivate=*/ kind == DeclOperationKind::FirstPrivate );
491+ /* isFirstprivate=*/ kind == DeclOperationKind::FirstPrivateOrLocalInit );
474492 builder.create <fir::StoreOp>(loc, firClass, allocatedPrivVarArg);
475493 if (ifUnallocated)
476494 builder.setInsertionPointAfter (ifUnallocated);
477495 createYield (allocatedPrivVarArg);
478496 mlir::OpBuilder::InsertionGuard guard (builder);
479- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
497+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
498+ isDoConcurrent);
480499 return ;
481500 }
482501
@@ -492,7 +511,8 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
492511 " createTempFromMold decides this statically" );
493512 if (cstNeedsDealloc.has_value () && *cstNeedsDealloc != false ) {
494513 mlir::OpBuilder::InsertionGuard guard (builder);
495- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
514+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
515+ isDoConcurrent);
496516 } else {
497517 assert (!isAllocatableOrPointer &&
498518 " Pointer-like arrays must be heap allocated" );
@@ -520,7 +540,7 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
520540
521541 initializeIfDerivedTypeBox (
522542 builder, loc, box, getLoadedMoldArg (), needsInitialization,
523- /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivate );
543+ /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivateOrLocalInit );
524544
525545 builder.create <fir::StoreOp>(loc, box, allocatedPrivVarArg);
526546 if (ifUnallocated)
@@ -548,7 +568,8 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxchar(
548568 loc, eleTy, /* name=*/ {}, /* shape=*/ {}, /* lenParams=*/ len);
549569 mlir::Value boxChar = charExprHelper.createEmboxChar (privateAddr, len);
550570
551- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
571+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
572+ isDoConcurrent);
552573
553574 builder.setInsertionPointToEnd (initBlock);
554575 createYield (boxChar);
@@ -563,10 +584,11 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupUnboxedDerivedType(
563584 mlir::Value moldBox = builder.create <fir::EmboxOp>(loc, boxedTy, moldArg);
564585 initializeIfDerivedTypeBox (builder, loc, newBox, moldBox, needsInitialization,
565586 /* isFirstPrivate=*/ kind ==
566- DeclOperationKind::FirstPrivate );
587+ DeclOperationKind::FirstPrivateOrLocalInit );
567588
568589 if (sym && hasFinalization (*sym))
569- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
590+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
591+ isDoConcurrent);
570592
571593 builder.setInsertionPointToEnd (initBlock);
572594 createYield (allocatedPrivVarArg);
@@ -632,15 +654,17 @@ void PopulateInitAndCleanupRegionsHelper::populateByRefInitAndCleanupRegions() {
632654 " creating reduction/privatization init region for unsupported type" );
633655}
634656
635- void Fortran::lower::omp:: populateByRefInitAndCleanupRegions (
657+ void Fortran::lower::populateByRefInitAndCleanupRegions (
636658 Fortran::lower::AbstractConverter &converter, mlir::Location loc,
637659 mlir::Type argType, mlir::Value scalarInitValue, mlir::Block *initBlock,
638660 mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
639661 mlir::Region &cleanupRegion, DeclOperationKind kind,
640- const Fortran::semantics::Symbol *sym, bool cannotHaveLowerBounds) {
662+ const Fortran::semantics::Symbol *sym, bool cannotHaveLowerBounds,
663+ bool isDoConcurrent) {
641664 PopulateInitAndCleanupRegionsHelper helper (
642665 converter, loc, argType, scalarInitValue, allocatedPrivVarArg, moldArg,
643- initBlock, cleanupRegion, kind, sym, cannotHaveLowerBounds);
666+ initBlock, cleanupRegion, kind, sym, cannotHaveLowerBounds,
667+ isDoConcurrent);
644668 helper.populateByRefInitAndCleanupRegions ();
645669
646670 // Often we load moldArg to check something (e.g. length parameters, shape)
0 commit comments