Skip to content

Commit eac11c5

Browse files
[Flang][OpenMP] Support unstructured code in target data (#71051)
1 parent ddfee5d commit eac11c5

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,8 @@ static void createBodyOfOp(
21732173
}
21742174

21752175
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,
21772178
const llvm::SmallVector<mlir::Type> &useDeviceTypes,
21782179
const llvm::SmallVector<mlir::Location> &useDeviceLocs,
21792180
const llvm::SmallVector<const Fortran::semantics::Symbol *>
@@ -2183,8 +2184,6 @@ static void createBodyOfTargetDataOp(
21832184
mlir::Region &region = dataOp.getRegion();
21842185

21852186
firOpBuilder.createBlock(&region, {}, useDeviceTypes, useDeviceLocs);
2186-
firOpBuilder.create<mlir::omp::TerminatorOp>(currentLocation);
2187-
firOpBuilder.setInsertionPointToStart(&region.front());
21882187

21892188
unsigned argIndex = 0;
21902189
for (const Fortran::semantics::Symbol *sym : useDeviceSymbols) {
@@ -2213,6 +2212,27 @@ static void createBodyOfTargetDataOp(
22132212
}
22142213
argIndex++;
22152214
}
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());
22162236
}
22172237

22182238
template <typename OpTy, typename... Args>
@@ -2360,6 +2380,7 @@ genTaskGroupOp(Fortran::lower::AbstractConverter &converter,
23602380

23612381
static mlir::omp::DataOp
23622382
genDataOp(Fortran::lower::AbstractConverter &converter,
2383+
Fortran::lower::pft::Evaluation &eval,
23632384
Fortran::semantics::SemanticsContext &semanticsContext,
23642385
mlir::Location currentLocation,
23652386
const Fortran::parser::OmpClauseList &clauseList) {
@@ -2386,8 +2407,8 @@ genDataOp(Fortran::lower::AbstractConverter &converter,
23862407
auto dataOp = converter.getFirOpBuilder().create<mlir::omp::DataOp>(
23872408
currentLocation, ifClauseOperand, deviceOperand, devicePtrOperands,
23882409
deviceAddrOperands, mapOperands);
2389-
createBodyOfTargetDataOp(converter, dataOp, useDeviceTypes, useDeviceLocs,
2390-
useDeviceSymbols, currentLocation);
2410+
createBodyOfTargetDataOp(converter, eval, dataOp, useDeviceTypes,
2411+
useDeviceLocs, useDeviceSymbols, currentLocation);
23912412
return dataOp;
23922413
}
23932414

@@ -2603,7 +2624,7 @@ genOmpSimpleStandalone(Fortran::lower::AbstractConverter &converter,
26032624
firOpBuilder.create<mlir::omp::TaskyieldOp>(currentLocation);
26042625
break;
26052626
case llvm::omp::Directive::OMPD_target_data:
2606-
genDataOp(converter, semanticsContext, currentLocation, opClauseList);
2627+
genDataOp(converter, eval, semanticsContext, currentLocation, opClauseList);
26072628
break;
26082629
case llvm::omp::Directive::OMPD_target_enter_data:
26092630
genEnterExitDataOp<mlir::omp::EnterDataOp>(converter, semanticsContext,
@@ -2892,7 +2913,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
28922913
beginClauseList, directive.v);
28932914
break;
28942915
case llvm::omp::Directive::OMPD_target_data:
2895-
genDataOp(converter, semanticsContext, currentLocation, beginClauseList);
2916+
genDataOp(converter, eval, semanticsContext, currentLocation,
2917+
beginClauseList);
28962918
break;
28972919
case llvm::omp::Directive::OMPD_task:
28982920
genTaskOp(converter, eval, currentLocation, beginClauseList);

flang/test/Lower/OpenMP/target.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,29 @@ subroutine omp_target_device_addr
265265
!CHECK: }
266266
end subroutine omp_target_device_addr
267267

268+
269+
!===============================================================================
270+
! Target Data with unstructured code
271+
!===============================================================================
272+
!CHECK-LABEL: func.func @_QPsb
273+
subroutine sb
274+
integer :: i = 1
275+
integer :: j = 11
276+
!CHECK: omp.target_data map_entries(%{{.*}}, %{{.*}} : !fir.ref<i32>, !fir.ref<i32>)
277+
!$omp target data map(tofrom: i, j)
278+
j = j - 1
279+
!CHECK: %[[J_VAL:.*]] = arith.subi
280+
!CHECK: hlfir.assign %[[J_VAL]]
281+
!CHECK: cf.br ^[[BB:.*]]
282+
!CHECK: ^[[BB]]:
283+
goto 20
284+
20 i = i + 1
285+
!CHECK: %[[I_VAL:.*]] = arith.addi
286+
!CHECK: hlfir.assign %[[I_VAL]]
287+
!CHECK: omp.terminator
288+
!$omp end target data
289+
end subroutine
290+
268291
!===============================================================================
269292
! Target with parallel loop
270293
!===============================================================================

0 commit comments

Comments
 (0)