Skip to content

Commit

Permalink
[flang][openacc] Add lowering support for device_resident clause on O…
Browse files Browse the repository at this point in the history
…penACC declare

Depends on D156828

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D156829
  • Loading branch information
clementval committed Aug 7, 2023
1 parent fe05193 commit 5cb48f7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
21 changes: 18 additions & 3 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
mlir::Location loc,
const Fortran::parser::AccClauseList &accClauseList) {
llvm::SmallVector<mlir::Value> dataClauseOperands, copyEntryOperands,
createEntryOperands, copyoutEntryOperands;
createEntryOperands, copyoutEntryOperands, deviceResidentEntryOperands;
Fortran::lower::StatementContext stmtCtx;
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
for (const Fortran::parser::AccClause &clause : accClauseList.v) {
Expand Down Expand Up @@ -2471,6 +2471,17 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
linkClause->v, converter, semanticsContext, stmtCtx,
dataClauseOperands, mlir::acc::DataClause::acc_declare_link,
/*structured=*/true);
} else if (const auto *deviceResidentClause =
std::get_if<Fortran::parser::AccClause::DeviceResident>(
&clause.u)) {
auto crtDataStart = dataClauseOperands.size();
genDataOperandOperations<mlir::acc::DeclareDeviceResidentOp>(
deviceResidentClause->v, converter, semanticsContext, stmtCtx,
dataClauseOperands,
mlir::acc::DataClause::acc_declare_device_resident,
/*structured=*/true);
deviceResidentEntryOperands.append(
dataClauseOperands.begin() + crtDataStart, dataClauseOperands.end());
} else {
mlir::Location clauseLocation = converter.genLocation(clause.source);
TODO(clauseLocation, "clause on declare directive");
Expand All @@ -2479,15 +2490,19 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
builder.create<mlir::acc::DeclareEnterOp>(loc, dataClauseOperands);

if (!createEntryOperands.empty() || !copyEntryOperands.empty() ||
!copyoutEntryOperands.empty()) {
!copyoutEntryOperands.empty() || !deviceResidentEntryOperands.empty()) {
// Attach declare exit operation generation to function context.
fctCtx.attachCleanup([&builder, loc, dataClauseOperands,
createEntryOperands, copyEntryOperands,
copyoutEntryOperands]() {
copyoutEntryOperands, deviceResidentEntryOperands]() {
builder.create<mlir::acc::DeclareExitOp>(loc, dataClauseOperands);
genDataExitOperations<mlir::acc::CreateOp, mlir::acc::DeleteOp>(
builder, createEntryOperands, /*structured=*/true,
/*implicit=*/false);
genDataExitOperations<mlir::acc::DeclareDeviceResidentOp,
mlir::acc::DeleteOp>(
builder, deviceResidentEntryOperands, /*structured=*/true,
/*implicit=*/false);
genDataExitOperations<mlir::acc::CopyinOp, mlir::acc::CopyoutOp>(
builder, copyEntryOperands, /*structured=*/true, /*implicit=*/false);
genDataExitOperations<mlir::acc::CreateOp, mlir::acc::CopyoutOp>(
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Lower/OpenACC/acc-declare.f90
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,21 @@ subroutine acc_declare_link(a)
! CHECK: %{{.*}}:2 = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%arg{{.*}} = %{{.*}}) -> (index, i32)
! CHECK-NOT: acc.declare_exit

subroutine acc_declare_device_resident(a)
integer :: a(100), i
!$acc declare device_resident(a)

do i = 1, 100
a(i) = i
end do
end subroutine

! CHECK-LABEL: func.func @_QMacc_declarePacc_declare_device_resident(
! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>> {fir.bindc_name = "a"})
! CHECK: %[[DEVICERES:.*]] = acc.declare_device_resident varPtr(%[[ARG0]] : !fir.ref<!fir.array<100xi32>>) bounds(%{{.*}}) -> !fir.ref<!fir.array<100xi32>> {name = "a"}
! CHECK: acc.declare_enter dataOperands(%[[DEVICERES]] : !fir.ref<!fir.array<100xi32>>)
! CHECK: %{{.*}}:2 = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%arg{{.*}} = %{{.*}}) -> (index, i32)
! CHECK: acc.declare_exit dataOperands(%[[DEVICERES]] : !fir.ref<!fir.array<100xi32>>)
! CHECK: acc.delete accPtr(%[[DEVICERES]] : !fir.ref<!fir.array<100xi32>>) bounds(%{{.*}}) {dataClause = #acc<data_clause acc_declare_device_resident>, name = "a"}

end module

0 comments on commit 5cb48f7

Please sign in to comment.