@@ -2173,7 +2173,8 @@ static void createBodyOfOp(
2173
2173
}
2174
2174
2175
2175
static void createBodyOfTargetDataOp (
2176
- Fortran::lower::AbstractConverter &converter, mlir::omp::DataOp &dataOp,
2176
+ Fortran::lower::AbstractConverter &converter,
2177
+ Fortran::lower::pft::Evaluation &eval, mlir::omp::DataOp &dataOp,
2177
2178
const llvm::SmallVector<mlir::Type> &useDeviceTypes,
2178
2179
const llvm::SmallVector<mlir::Location> &useDeviceLocs,
2179
2180
const llvm::SmallVector<const Fortran::semantics::Symbol *>
@@ -2183,8 +2184,6 @@ static void createBodyOfTargetDataOp(
2183
2184
mlir::Region ®ion = dataOp.getRegion ();
2184
2185
2185
2186
firOpBuilder.createBlock (®ion, {}, useDeviceTypes, useDeviceLocs);
2186
- firOpBuilder.create <mlir::omp::TerminatorOp>(currentLocation);
2187
- firOpBuilder.setInsertionPointToStart (®ion.front ());
2188
2187
2189
2188
unsigned argIndex = 0 ;
2190
2189
for (const Fortran::semantics::Symbol *sym : useDeviceSymbols) {
@@ -2213,6 +2212,27 @@ static void createBodyOfTargetDataOp(
2213
2212
}
2214
2213
argIndex++;
2215
2214
}
2215
+
2216
+ // Insert dummy instruction to remember the insertion position. The
2217
+ // marker will be deleted by clean up passes since there are no uses.
2218
+ // Remembering the position for further insertion is important since
2219
+ // there are hlfir.declares inserted above while setting block arguments
2220
+ // and new code from the body should be inserted after that.
2221
+ mlir::Value undefMarker = firOpBuilder.create <fir::UndefOp>(
2222
+ dataOp.getOperation ()->getLoc (), firOpBuilder.getIndexType ());
2223
+
2224
+ // Create blocks for unstructured regions. This has to be done since
2225
+ // blocks are initially allocated with the function as the parent region.
2226
+ if (eval.lowerAsUnstructured ()) {
2227
+ Fortran::lower::createEmptyRegionBlocks<mlir::omp::TerminatorOp,
2228
+ mlir::omp::YieldOp>(
2229
+ firOpBuilder, eval.getNestedEvaluations ());
2230
+ }
2231
+
2232
+ firOpBuilder.create <mlir::omp::TerminatorOp>(currentLocation);
2233
+
2234
+ // Set the insertion point after the marker.
2235
+ firOpBuilder.setInsertionPointAfter (undefMarker.getDefiningOp ());
2216
2236
}
2217
2237
2218
2238
template <typename OpTy, typename ... Args>
@@ -2360,6 +2380,7 @@ genTaskGroupOp(Fortran::lower::AbstractConverter &converter,
2360
2380
2361
2381
static mlir::omp::DataOp
2362
2382
genDataOp (Fortran::lower::AbstractConverter &converter,
2383
+ Fortran::lower::pft::Evaluation &eval,
2363
2384
Fortran::semantics::SemanticsContext &semanticsContext,
2364
2385
mlir::Location currentLocation,
2365
2386
const Fortran::parser::OmpClauseList &clauseList) {
@@ -2386,8 +2407,8 @@ genDataOp(Fortran::lower::AbstractConverter &converter,
2386
2407
auto dataOp = converter.getFirOpBuilder ().create <mlir::omp::DataOp>(
2387
2408
currentLocation, ifClauseOperand, deviceOperand, devicePtrOperands,
2388
2409
deviceAddrOperands, mapOperands);
2389
- createBodyOfTargetDataOp (converter, dataOp, useDeviceTypes, useDeviceLocs ,
2390
- useDeviceSymbols, currentLocation);
2410
+ createBodyOfTargetDataOp (converter, eval, dataOp, useDeviceTypes ,
2411
+ useDeviceLocs, useDeviceSymbols, currentLocation);
2391
2412
return dataOp;
2392
2413
}
2393
2414
@@ -2603,7 +2624,7 @@ genOmpSimpleStandalone(Fortran::lower::AbstractConverter &converter,
2603
2624
firOpBuilder.create <mlir::omp::TaskyieldOp>(currentLocation);
2604
2625
break ;
2605
2626
case llvm::omp::Directive::OMPD_target_data:
2606
- genDataOp (converter, semanticsContext, currentLocation, opClauseList);
2627
+ genDataOp (converter, eval, semanticsContext, currentLocation, opClauseList);
2607
2628
break ;
2608
2629
case llvm::omp::Directive::OMPD_target_enter_data:
2609
2630
genEnterExitDataOp<mlir::omp::EnterDataOp>(converter, semanticsContext,
@@ -2892,7 +2913,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
2892
2913
beginClauseList, directive.v );
2893
2914
break ;
2894
2915
case llvm::omp::Directive::OMPD_target_data:
2895
- genDataOp (converter, semanticsContext, currentLocation, beginClauseList);
2916
+ genDataOp (converter, eval, semanticsContext, currentLocation,
2917
+ beginClauseList);
2896
2918
break ;
2897
2919
case llvm::omp::Directive::OMPD_task:
2898
2920
genTaskOp (converter, eval, currentLocation, beginClauseList);
0 commit comments