@@ -119,7 +119,7 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
119119 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type argType,
120120 mlir::Value scalarInitValue, mlir::Block *initBlock,
121121 mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
122- mlir::Region &cleanupRegion) {
122+ mlir::Region &cleanupRegion, bool isPrivate ) {
123123 mlir::Type ty = fir::unwrapRefType (argType);
124124 builder.setInsertionPointToEnd (initBlock);
125125 auto yield = [&](mlir::Value ret) {
@@ -147,11 +147,10 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
147147 // fir.store %something to %box_alloca
148148 // }
149149 // omp.yield %box_alloca
150- moldArg = builder.loadIfRef (loc, moldArg);
151150 mlir::SmallVector<mlir::Value> lenParams;
152- hlfir::genLengthParameters (loc, builder, hlfir::Entity{moldArg}, lenParams);
153- auto handleNullAllocatable = [&]( mlir::Value boxAlloca ) -> fir::IfOp {
154- mlir::Value addr = builder.create <fir::BoxAddrOp>(loc, moldArg );
151+ auto handleNullAllocatable = [&](mlir::Value boxAlloca,
152+ mlir::Value loadedMold ) -> fir::IfOp {
153+ mlir::Value addr = builder.create <fir::BoxAddrOp>(loc, loadedMold );
155154 mlir::Value isNotAllocated = builder.genIsNullAddr (loc, addr);
156155 fir::IfOp ifOp = builder.create <fir::IfOp>(loc, isNotAllocated,
157156 /* withElseRegion=*/ true );
@@ -171,6 +170,21 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
171170
172171 builder.setInsertionPointToEnd (initBlock);
173172 mlir::Value boxAlloca = allocatedPrivVarArg;
173+
174+ // The initial state of a private pointer is undefined so we don't need to
175+ // match the mold argument (OpenMP 5.2 end of page 106).
176+ if (isPrivate && mlir::isa<fir::PointerType>(boxTy.getEleTy ())) {
177+ // Just incase, do initialize the box with a null value
178+ mlir::Value null = builder.createNullConstant (loc, boxTy.getEleTy ());
179+ mlir::Value nullBox = builder.create <fir::EmboxOp>(loc, boxTy, null);
180+ builder.create <fir::StoreOp>(loc, nullBox, boxAlloca);
181+ yield (boxAlloca);
182+ return ;
183+ }
184+
185+ moldArg = builder.loadIfRef (loc, moldArg);
186+ hlfir::genLengthParameters (loc, builder, hlfir::Entity{moldArg}, lenParams);
187+
174188 mlir::Type innerTy = fir::unwrapRefType (boxTy.getEleTy ());
175189 bool isChar = fir::isa_char (innerTy);
176190 if (fir::isa_trivial (innerTy) || isChar) {
@@ -179,7 +193,7 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
179193 TODO (loc,
180194 " Reduction/Privatization of non-allocatable trivial typed box" );
181195
182- fir::IfOp ifUnallocated = handleNullAllocatable (boxAlloca);
196+ fir::IfOp ifUnallocated = handleNullAllocatable (boxAlloca, moldArg );
183197
184198 builder.setInsertionPointToStart (&ifUnallocated.getElseRegion ().front ());
185199 mlir::Value valAlloc = builder.createHeapTemporary (
@@ -200,9 +214,12 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
200214 if (!innerTy || !mlir::isa<fir::SequenceType>(innerTy))
201215 TODO (loc, " Unsupported boxed type for reduction/privatization" );
202216
217+ moldArg = builder.loadIfRef (loc, moldArg);
218+ hlfir::genLengthParameters (loc, builder, hlfir::Entity{moldArg}, lenParams);
219+
203220 fir::IfOp ifUnallocated{nullptr };
204221 if (isAllocatableOrPointer) {
205- ifUnallocated = handleNullAllocatable (boxAlloca);
222+ ifUnallocated = handleNullAllocatable (boxAlloca, moldArg );
206223 builder.setInsertionPointToStart (&ifUnallocated.getElseRegion ().front ());
207224 }
208225
0 commit comments