Skip to content

[Flang][OpenMP] Support unstructured code in target data #71051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions flang/lib/Lower/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2173,7 +2173,8 @@ static void createBodyOfOp(
}

static void createBodyOfTargetDataOp(
Fortran::lower::AbstractConverter &converter, mlir::omp::DataOp &dataOp,
Fortran::lower::AbstractConverter &converter,
Fortran::lower::pft::Evaluation &eval, mlir::omp::DataOp &dataOp,
const llvm::SmallVector<mlir::Type> &useDeviceTypes,
const llvm::SmallVector<mlir::Location> &useDeviceLocs,
const llvm::SmallVector<const Fortran::semantics::Symbol *>
Expand All @@ -2183,8 +2184,6 @@ static void createBodyOfTargetDataOp(
mlir::Region &region = dataOp.getRegion();

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

unsigned argIndex = 0;
for (const Fortran::semantics::Symbol *sym : useDeviceSymbols) {
Expand Down Expand Up @@ -2213,6 +2212,27 @@ static void createBodyOfTargetDataOp(
}
argIndex++;
}

// Insert dummy instruction to remember the insertion position. The
// marker will be deleted by clean up passes since there are no uses.
// Remembering the position for further insertion is important since
// there are hlfir.declares inserted above while setting block arguments
// and new code from the body should be inserted after that.
mlir::Value undefMarker = firOpBuilder.create<fir::UndefOp>(
dataOp.getOperation()->getLoc(), firOpBuilder.getIndexType());

// Create blocks for unstructured regions. This has to be done since
// blocks are initially allocated with the function as the parent region.
if (eval.lowerAsUnstructured()) {
Fortran::lower::createEmptyRegionBlocks<mlir::omp::TerminatorOp,
mlir::omp::YieldOp>(
firOpBuilder, eval.getNestedEvaluations());
}

firOpBuilder.create<mlir::omp::TerminatorOp>(currentLocation);

// Set the insertion point after the marker.
firOpBuilder.setInsertionPointAfter(undefMarker.getDefiningOp());
}

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

static mlir::omp::DataOp
genDataOp(Fortran::lower::AbstractConverter &converter,
Fortran::lower::pft::Evaluation &eval,
Fortran::semantics::SemanticsContext &semanticsContext,
mlir::Location currentLocation,
const Fortran::parser::OmpClauseList &clauseList) {
Expand All @@ -2386,8 +2407,8 @@ genDataOp(Fortran::lower::AbstractConverter &converter,
auto dataOp = converter.getFirOpBuilder().create<mlir::omp::DataOp>(
currentLocation, ifClauseOperand, deviceOperand, devicePtrOperands,
deviceAddrOperands, mapOperands);
createBodyOfTargetDataOp(converter, dataOp, useDeviceTypes, useDeviceLocs,
useDeviceSymbols, currentLocation);
createBodyOfTargetDataOp(converter, eval, dataOp, useDeviceTypes,
useDeviceLocs, useDeviceSymbols, currentLocation);
return dataOp;
}

Expand Down Expand Up @@ -2603,7 +2624,7 @@ genOmpSimpleStandalone(Fortran::lower::AbstractConverter &converter,
firOpBuilder.create<mlir::omp::TaskyieldOp>(currentLocation);
break;
case llvm::omp::Directive::OMPD_target_data:
genDataOp(converter, semanticsContext, currentLocation, opClauseList);
genDataOp(converter, eval, semanticsContext, currentLocation, opClauseList);
break;
case llvm::omp::Directive::OMPD_target_enter_data:
genEnterExitDataOp<mlir::omp::EnterDataOp>(converter, semanticsContext,
Expand Down Expand Up @@ -2892,7 +2913,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
beginClauseList, directive.v);
break;
case llvm::omp::Directive::OMPD_target_data:
genDataOp(converter, semanticsContext, currentLocation, beginClauseList);
genDataOp(converter, eval, semanticsContext, currentLocation,
beginClauseList);
break;
case llvm::omp::Directive::OMPD_task:
genTaskOp(converter, eval, currentLocation, beginClauseList);
Expand Down
23 changes: 23 additions & 0 deletions flang/test/Lower/OpenMP/target.f90
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,29 @@ subroutine omp_target_device_addr
!CHECK: }
end subroutine omp_target_device_addr


!===============================================================================
! Target Data with unstructured code
!===============================================================================
!CHECK-LABEL: func.func @_QPsb
subroutine sb
integer :: i = 1
integer :: j = 11
!CHECK: omp.target_data map_entries(%{{.*}}, %{{.*}} : !fir.ref<i32>, !fir.ref<i32>)
!$omp target data map(tofrom: i, j)
j = j - 1
!CHECK: %[[J_VAL:.*]] = arith.subi
!CHECK: hlfir.assign %[[J_VAL]]
!CHECK: cf.br ^[[BB:.*]]
!CHECK: ^[[BB]]:
goto 20
20 i = i + 1
!CHECK: %[[I_VAL:.*]] = arith.addi
!CHECK: hlfir.assign %[[I_VAL]]
!CHECK: omp.terminator
!$omp end target data
end subroutine

!===============================================================================
! Target with parallel loop
!===============================================================================
Expand Down