[flang-commits] [flang] 392203d - [flang][openacc] Add lowering support for present clause on OpenACC declare

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Tue Aug 1 14:14:59 PDT 2023


Author: Valentin Clement
Date: 2023-08-01T14:14:48-07:00
New Revision: 392203d8acee745b5f7259776827c9474dbeb3ab

URL: https://github.com/llvm/llvm-project/commit/392203d8acee745b5f7259776827c9474dbeb3ab
DIFF: https://github.com/llvm/llvm-project/commit/392203d8acee745b5f7259776827c9474dbeb3ab.diff

LOG: [flang][openacc] Add lowering support for present clause on OpenACC declare

Lower the present clause on the OpenACC declare construct in
function/subroutine.

Depends on D156572

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D156721

Added: 
    

Modified: 
    flang/lib/Lower/OpenACC.cpp
    flang/test/Lower/OpenACC/acc-declare.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 937815c8f947ba..a25d7b2b5dcbaa 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -374,6 +374,8 @@ static Op createDataEntryOp(fir::FirOpBuilder &builder, mlir::Location loc,
 
 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 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
           /*structured=*/true, /*setDeclareAttr=*/true);
       createEntryOperands.append(dataClauseOperands.begin() + crtDataStart,
                                  dataClauseOperands.end());
+    } else if (const auto *presentClause =
+                   std::get_if<Fortran::parser::AccClause::Present>(
+                       &clause.u)) {
+      genDataOperandOperations<mlir::acc::PresentOp>(
+          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 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
   }
   builder.create<mlir::acc::DeclareEnterOp>(loc, dataClauseOperands);
 
-  // Attach declare exit operation generation to function context.
-  fctCtx.attachCleanup([&builder, loc, dataClauseOperands, createEntryOperands,
-                        copyEntryOperands]() {
-    builder.create<mlir::acc::DeclareExitOp>(loc, dataClauseOperands);
-    genDataExitOperations<mlir::acc::CreateOp, mlir::acc::DeleteOp>(
-        builder, createEntryOperands, /*structured=*/true, /*implicit=*/false);
-    genDataExitOperations<mlir::acc::CopyinOp, mlir::acc::CopyoutOp>(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<mlir::acc::DeclareExitOp>(loc, dataClauseOperands);
+      genDataExitOperations<mlir::acc::CreateOp, mlir::acc::DeleteOp>(
+          builder, createEntryOperands, /*structured=*/true,
+          /*implicit=*/false);
+      genDataExitOperations<mlir::acc::CopyinOp, mlir::acc::CopyoutOp>(
+          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
index 294f495fb36e05..6a0b8b71f7e731 100644
--- a/flang/test/Lower/OpenACC/acc-declare.f90
+++ b/flang/test/Lower/OpenACC/acc-declare.f90
@@ -118,5 +118,21 @@ subroutine acc_declare_create()
 ! CHECK: acc.delete accPtr(%[[CREATE]] : !fir.ref<!fir.array<100xi32>>) bounds(%[[BOUND]]) {dataClause = #acc<data_clause acc_create>, 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.array<100xi32>> {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<!fir.array<100xi32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<100xi32>> {name = "a"}
+! CHECK: acc.declare_enter dataOperands(%[[PRESENT]] : !fir.ref<!fir.array<100xi32>>)
+! CHECK: %{{.*}}:2 = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%arg{{.*}} = %{{.*}}) -> (index, i32)
+! CHECK-NOT: acc.declare_exit
 
 end module


        


More information about the flang-commits mailing list