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 (),
@@ -73,7 +74,10 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
7374 fir::MutableBoxValue mutableBox{converted, /* lenParameters=*/ {},
7475 /* mutableProperties=*/ {}};
7576 Fortran::lower::genDeallocateIfAllocated (converter, mutableBox, loc);
76- builder.create <mlir::omp::YieldOp>(loc);
77+ if (isDoConcurrent)
78+ builder.create <fir::YieldOp>(loc);
79+ else
80+ builder.create <mlir::omp::YieldOp>(loc);
7781 return ;
7882 }
7983 }
@@ -101,7 +105,10 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
101105 builder.create <fir::FreeMemOp>(loc, cast);
102106
103107 builder.setInsertionPointAfter (ifOp);
104- builder.create <mlir::omp::YieldOp>(loc);
108+ if (isDoConcurrent)
109+ builder.create <fir::YieldOp>(loc);
110+ else
111+ builder.create <mlir::omp::YieldOp>(loc);
105112 return ;
106113 }
107114
@@ -116,14 +123,18 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
116123 addr = builder.createConvert (loc, heapTy, addr);
117124
118125 builder.create <fir::FreeMemOp>(loc, addr);
119- builder.create <mlir::omp::YieldOp>(loc);
126+ if (isDoConcurrent)
127+ builder.create <fir::YieldOp>(loc);
128+ else
129+ builder.create <mlir::omp::YieldOp>(loc);
130+
120131 return ;
121132 }
122133
123134 typeError ();
124135}
125136
126- fir::ShapeShiftOp Fortran::lower::omp:: getShapeShift (
137+ fir::ShapeShiftOp Fortran::lower::getShapeShift (
127138 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value box,
128139 bool cannotHaveNonDefaultLowerBounds, bool useDefaultLowerBounds) {
129140 fir::SequenceType sequenceType = mlir::cast<fir::SequenceType>(
@@ -263,7 +274,7 @@ static mlir::Value generateZeroShapeForRank(fir::FirOpBuilder &builder,
263274}
264275
265276namespace {
266- using namespace Fortran ::lower::omp ;
277+ using namespace Fortran ::lower;
267278// / Class to store shared data so we don't have to maintain so many function
268279// / arguments
269280class PopulateInitAndCleanupRegionsHelper {
@@ -274,12 +285,13 @@ class PopulateInitAndCleanupRegionsHelper {
274285 mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
275286 mlir::Block *initBlock, mlir::Region &cleanupRegion,
276287 DeclOperationKind kind, const Fortran::semantics::Symbol *sym,
277- bool cannotHaveLowerBounds)
288+ bool cannotHaveLowerBounds, bool isDoConcurrent )
278289 : converter{converter}, builder{converter.getFirOpBuilder ()}, loc{loc},
279290 argType{argType}, scalarInitValue{scalarInitValue},
280291 allocatedPrivVarArg{allocatedPrivVarArg}, moldArg{moldArg},
281292 initBlock{initBlock}, cleanupRegion{cleanupRegion}, kind{kind},
282- sym{sym}, cannotHaveNonDefaultLowerBounds{cannotHaveLowerBounds} {
293+ sym{sym}, cannotHaveNonDefaultLowerBounds{cannotHaveLowerBounds},
294+ isDoConcurrent{isDoConcurrent} {
283295 valType = fir::unwrapRefType (argType);
284296 }
285297
@@ -325,8 +337,13 @@ class PopulateInitAndCleanupRegionsHelper {
325337 // / lower bounds then we don't need to generate code to read them.
326338 bool cannotHaveNonDefaultLowerBounds;
327339
340+ bool isDoConcurrent;
341+
328342 void createYield (mlir::Value ret) {
329- builder.create <mlir::omp::YieldOp>(loc, ret);
343+ if (isDoConcurrent)
344+ builder.create <fir::YieldOp>(loc, ret);
345+ else
346+ builder.create <mlir::omp::YieldOp>(loc, ret);
330347 }
331348
332349 void initTrivialType () {
@@ -430,11 +447,12 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedScalar(
430447 /* slice=*/ mlir::Value{}, lenParams);
431448 initializeIfDerivedTypeBox (
432449 builder, loc, box, getLoadedMoldArg (), needsInitialization,
433- /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivate );
450+ /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivateOrLocalInit );
434451 fir::StoreOp lastOp =
435452 builder.create <fir::StoreOp>(loc, box, allocatedPrivVarArg);
436453
437- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
454+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
455+ isDoConcurrent);
438456
439457 if (ifUnallocated)
440458 builder.setInsertionPointAfter (ifUnallocated);
@@ -471,13 +489,14 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
471489 allocatedArray, shape);
472490 initializeIfDerivedTypeBox (
473491 builder, loc, firClass, source, needsInitialization,
474- /* isFirstprivate=*/ kind == DeclOperationKind::FirstPrivate );
492+ /* isFirstprivate=*/ kind == DeclOperationKind::FirstPrivateOrLocalInit );
475493 builder.create <fir::StoreOp>(loc, firClass, allocatedPrivVarArg);
476494 if (ifUnallocated)
477495 builder.setInsertionPointAfter (ifUnallocated);
478496 createYield (allocatedPrivVarArg);
479497 mlir::OpBuilder::InsertionGuard guard (builder);
480- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
498+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
499+ isDoConcurrent);
481500 return ;
482501 }
483502
@@ -502,7 +521,8 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
502521 " createTempFromMold decides this statically" );
503522 if (cstNeedsDealloc.has_value () && *cstNeedsDealloc != false ) {
504523 mlir::OpBuilder::InsertionGuard guard (builder);
505- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
524+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
525+ isDoConcurrent);
506526 } else {
507527 assert (!isAllocatableOrPointer &&
508528 " Pointer-like arrays must be heap allocated" );
@@ -530,7 +550,7 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
530550
531551 initializeIfDerivedTypeBox (
532552 builder, loc, box, getLoadedMoldArg (), needsInitialization,
533- /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivate );
553+ /* isFirstPrivate=*/ kind == DeclOperationKind::FirstPrivateOrLocalInit );
534554
535555 builder.create <fir::StoreOp>(loc, box, allocatedPrivVarArg);
536556 if (ifUnallocated)
@@ -558,7 +578,8 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxchar(
558578 loc, eleTy, /* name=*/ {}, /* shape=*/ {}, /* lenParams=*/ len);
559579 mlir::Value boxChar = charExprHelper.createEmboxChar (privateAddr, len);
560580
561- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
581+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
582+ isDoConcurrent);
562583
563584 builder.setInsertionPointToEnd (initBlock);
564585 createYield (boxChar);
@@ -573,10 +594,11 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupUnboxedDerivedType(
573594 mlir::Value moldBox = builder.create <fir::EmboxOp>(loc, boxedTy, moldArg);
574595 initializeIfDerivedTypeBox (builder, loc, newBox, moldBox, needsInitialization,
575596 /* isFirstPrivate=*/ kind ==
576- DeclOperationKind::FirstPrivate );
597+ DeclOperationKind::FirstPrivateOrLocalInit );
577598
578599 if (sym && hasFinalization (*sym))
579- createCleanupRegion (converter, loc, argType, cleanupRegion, sym);
600+ createCleanupRegion (converter, loc, argType, cleanupRegion, sym,
601+ isDoConcurrent);
580602
581603 builder.setInsertionPointToEnd (initBlock);
582604 createYield (allocatedPrivVarArg);
@@ -642,15 +664,17 @@ void PopulateInitAndCleanupRegionsHelper::populateByRefInitAndCleanupRegions() {
642664 " creating reduction/privatization init region for unsupported type" );
643665}
644666
645- void Fortran::lower::omp:: populateByRefInitAndCleanupRegions (
667+ void Fortran::lower::populateByRefInitAndCleanupRegions (
646668 Fortran::lower::AbstractConverter &converter, mlir::Location loc,
647669 mlir::Type argType, mlir::Value scalarInitValue, mlir::Block *initBlock,
648670 mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
649671 mlir::Region &cleanupRegion, DeclOperationKind kind,
650- const Fortran::semantics::Symbol *sym, bool cannotHaveLowerBounds) {
672+ const Fortran::semantics::Symbol *sym, bool cannotHaveLowerBounds,
673+ bool isDoConcurrent) {
651674 PopulateInitAndCleanupRegionsHelper helper (
652675 converter, loc, argType, scalarInitValue, allocatedPrivVarArg, moldArg,
653- initBlock, cleanupRegion, kind, sym, cannotHaveLowerBounds);
676+ initBlock, cleanupRegion, kind, sym, cannotHaveLowerBounds,
677+ isDoConcurrent);
654678 helper.populateByRefInitAndCleanupRegions ();
655679
656680 // Often we load moldArg to check something (e.g. length parameters, shape)
0 commit comments