[flang-commits] [flang] 14741ef - [flang][openacc] Lower the exit part for OpenACC declare in function/subroutine

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


Author: Valentin Clement
Date: 2023-08-01T14:10:33-07:00
New Revision: 14741ef88f2a00f4b5f92b981e1aec04bfa08d36

URL: https://github.com/llvm/llvm-project/commit/14741ef88f2a00f4b5f92b981e1aec04bfa08d36
DIFF: https://github.com/llvm/llvm-project/commit/14741ef88f2a00f4b5f92b981e1aec04bfa08d36.diff

LOG: [flang][openacc] Lower the exit part for OpenACC declare in function/subroutine

This patch adds lowering for the exit part of the OpenACC declare construct
in function/subroutine.

Depends on D156560

Reviewed By: razvanlupusoru

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Lower/OpenACC.h b/flang/include/flang/Lower/OpenACC.h
index 0bdc5cf9bbde1d..ef0360be439c4f 100644
--- a/flang/include/flang/Lower/OpenACC.h
+++ b/flang/include/flang/Lower/OpenACC.h
@@ -45,6 +45,7 @@ class SemanticsContext;
 namespace lower {
 
 class AbstractConverter;
+class StatementContext;
 
 namespace pft {
 struct Evaluation;
@@ -55,7 +56,8 @@ void genOpenACCConstruct(AbstractConverter &,
                          pft::Evaluation &, const parser::OpenACCConstruct &);
 void genOpenACCDeclarativeConstruct(
     AbstractConverter &, Fortran::semantics::SemanticsContext &,
-    pft::Evaluation &, const parser::OpenACCDeclarativeConstruct &);
+    StatementContext &, pft::Evaluation &,
+    const parser::OpenACCDeclarativeConstruct &);
 
 /// Get a acc.private.recipe op for the given type or create it if it does not
 /// exist yet.

diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 4d2209a5200279..5e52ca69079aac 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -2230,7 +2230,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   void genFIR(const Fortran::parser::OpenACCDeclarativeConstruct &accDecl) {
     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
     genOpenACCDeclarativeConstruct(*this, bridge.getSemanticsContext(),
-                                   getEval(), accDecl);
+                                   bridge.fctCtx(), getEval(), accDecl);
     for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
       genFIR(e);
     builder->restoreInsertionPoint(insertPt);

diff  --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index e59478556544a5..fd8bf143a70d6f 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -2397,6 +2397,7 @@ genGlobalCtorsWithModifier(Fortran::lower::AbstractConverter &converter,
 static void
 genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
                      Fortran::semantics::SemanticsContext &semanticsContext,
+                     Fortran::lower::StatementContext &fctCtx,
                      mlir::Location loc,
                      const Fortran::parser::AccClauseList &accClauseList) {
   llvm::SmallVector<mlir::Value> dataClauseOperands, copyEntryOperands;
@@ -2418,8 +2419,14 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
       TODO(clauseLocation, "clause on declare directive");
     }
   }
-
   builder.create<mlir::acc::DeclareEnterOp>(loc, dataClauseOperands);
+
+  // Attach declare exit operation generation to function context.
+  fctCtx.attachCleanup([&builder, loc, copyEntryOperands]() {
+    builder.create<mlir::acc::DeclareExitOp>(loc, copyEntryOperands);
+    genDataExitOperations<mlir::acc::CopyinOp, mlir::acc::CopyoutOp>(
+        builder, copyEntryOperands, /*structured=*/true, /*implicit=*/false);
+  });
 }
 
 static void
@@ -2465,6 +2472,7 @@ genDeclareInModule(Fortran::lower::AbstractConverter &converter,
 
 static void genACC(Fortran::lower::AbstractConverter &converter,
                    Fortran::semantics::SemanticsContext &semanticsContext,
+                   Fortran::lower::StatementContext &fctCtx,
                    Fortran::lower::pft::Evaluation &eval,
                    const Fortran::parser::OpenACCStandaloneDeclarativeConstruct
                        &declareConstruct) {
@@ -2483,8 +2491,8 @@ static void genACC(Fortran::lower::AbstractConverter &converter,
     auto funcOp =
         builder.getBlock()->getParent()->getParentOfType<mlir::func::FuncOp>();
     if (funcOp)
-      genDeclareInFunction(converter, semanticsContext, directiveLocation,
-                           accClauseList);
+      genDeclareInFunction(converter, semanticsContext, fctCtx,
+                           directiveLocation, accClauseList);
     else if (moduleOp)
       genDeclareInModule(converter, moduleOp, accClauseList);
     return;
@@ -2532,6 +2540,7 @@ void Fortran::lower::genOpenACCConstruct(
 void Fortran::lower::genOpenACCDeclarativeConstruct(
     Fortran::lower::AbstractConverter &converter,
     Fortran::semantics::SemanticsContext &semanticsContext,
+    Fortran::lower::StatementContext &fctCtx,
     Fortran::lower::pft::Evaluation &eval,
     const Fortran::parser::OpenACCDeclarativeConstruct &accDeclConstruct) {
 
@@ -2539,7 +2548,7 @@ void Fortran::lower::genOpenACCDeclarativeConstruct(
       common::visitors{
           [&](const Fortran::parser::OpenACCStandaloneDeclarativeConstruct
                   &standaloneDeclarativeConstruct) {
-            genACC(converter, semanticsContext, eval,
+            genACC(converter, semanticsContext, fctCtx, eval,
                    standaloneDeclarativeConstruct);
           },
           [&](const Fortran::parser::OpenACCRoutineConstruct

diff  --git a/flang/test/Lower/OpenACC/acc-declare.f90 b/flang/test/Lower/OpenACC/acc-declare.f90
index 237da9e264267e..c0ad91581e7e24 100644
--- a/flang/test/Lower/OpenACC/acc-declare.f90
+++ b/flang/test/Lower/OpenACC/acc-declare.f90
@@ -87,4 +87,12 @@ subroutine acc_declare_copy()
 ! CHECK: %[[COPYIN:.*]] = acc.copyin varPtr(%[[ALLOCA]] : !fir.ref<!fir.array<100xi32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<100xi32>> {dataClause = #acc<data_clause acc_copy>, name = "a"}
 ! CHECK: acc.declare_enter dataOperands(%[[COPYIN]] : !fir.ref<!fir.array<100xi32>>)
 
+! CHECK: %{{.*}}:2 = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%{{.*}} = %{{.*}}) -> (index, i32) {
+! CHECK: }
+
+! CHECK: acc.declare_exit dataOperands(%[[COPYIN]] : !fir.ref<!fir.array<100xi32>>)
+! CHECK: acc.copyout accPtr(%[[COPYIN]] : !fir.ref<!fir.array<100xi32>>) bounds(%[[BOUND]]) to varPtr(%[[ALLOCA]] : !fir.ref<!fir.array<100xi32>>) {dataClause = #acc<data_clause acc_copy>, name = "a"}
+
+! CHECK: return
+
 end module


        


More information about the flang-commits mailing list