diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp --- a/flang/lib/Lower/OpenACC.cpp +++ b/flang/lib/Lower/OpenACC.cpp @@ -374,6 +374,8 @@ static void addDeclareAttr(fir::FirOpBuilder &builder, mlir::Operation *op, mlir::acc::DataClause clause) { + if (!op) + return; op->setAttr(mlir::acc::getDeclareAttrName(), mlir::acc::DeclareAttr::get(builder.getContext(), mlir::acc::DataClauseAttr::get( @@ -2427,6 +2429,13 @@ /*structured=*/true, /*setDeclareAttr=*/true); createEntryOperands.append(dataClauseOperands.begin() + crtDataStart, dataClauseOperands.end()); + } else if (const auto *presentClause = + std::get_if( + &clause.u)) { + genDataOperandOperations( + presentClause->v, converter, semanticsContext, stmtCtx, + dataClauseOperands, mlir::acc::DataClause::acc_present, + /*structured=*/true, /*setDeclareAttr=*/true); } else { mlir::Location clauseLocation = converter.genLocation(clause.source); TODO(clauseLocation, "clause on declare directive"); @@ -2434,15 +2443,18 @@ } builder.create(loc, dataClauseOperands); - // Attach declare exit operation generation to function context. - fctCtx.attachCleanup([&builder, loc, dataClauseOperands, createEntryOperands, - copyEntryOperands]() { - builder.create(loc, dataClauseOperands); - genDataExitOperations( - builder, createEntryOperands, /*structured=*/true, /*implicit=*/false); - genDataExitOperations(builder, - copyEntryOperands, /*structured=*/true, /*implicit=*/false); - }); + if (!createEntryOperands.empty() || !copyEntryOperands.empty()) { + // Attach declare exit operation generation to function context. + fctCtx.attachCleanup([&builder, loc, dataClauseOperands, + createEntryOperands, copyEntryOperands]() { + builder.create(loc, dataClauseOperands); + genDataExitOperations( + builder, createEntryOperands, /*structured=*/true, + /*implicit=*/false); + genDataExitOperations( + builder, copyEntryOperands, /*structured=*/true, /*implicit=*/false); + }); + } } static void diff --git a/flang/test/Lower/OpenACC/acc-declare.f90 b/flang/test/Lower/OpenACC/acc-declare.f90 --- a/flang/test/Lower/OpenACC/acc-declare.f90 +++ b/flang/test/Lower/OpenACC/acc-declare.f90 @@ -118,5 +118,21 @@ ! CHECK: acc.delete accPtr(%[[CREATE]] : !fir.ref>) bounds(%[[BOUND]]) {dataClause = #acc, name = "a"} ! CHECK: return + subroutine acc_declare_present(a) + integer :: a(100), i + !$acc declare present(a) + + do i = 1, 100 + a(i) = i + end do + end subroutine + +! CHECK-LABEL: func.func @_QMacc_declarePacc_declare_present( +! CHECK-SAME: %[[ARG0:.*]]: !fir.ref> {fir.bindc_name = "a"}) +! CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%{{.*}} : index) upperbound(%{{.*}} : index) extent(%{{.*}} : index) stride(%{{.*}} : index) startIdx(%c1 : index) +! CHECK: %[[PRESENT:.*]] = acc.present varPtr(%[[ARG0]] : !fir.ref>) bounds(%[[BOUND]]) -> !fir.ref> {name = "a"} +! CHECK: acc.declare_enter dataOperands(%[[PRESENT]] : !fir.ref>) +! CHECK: %{{.*}}:2 = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%arg{{.*}} = %{{.*}}) -> (index, i32) +! CHECK-NOT: acc.declare_exit end module