[flang-commits] [flang] 9334671 - [flang][openacc] Lower simple acc routine directive

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Tue Aug 15 09:46:02 PDT 2023


Author: Valentin Clement
Date: 2023-08-15T09:45:57-07:00
New Revision: 9334671aa7a8f73e529a653d5c6a0be75dad67bc

URL: https://github.com/llvm/llvm-project/commit/9334671aa7a8f73e529a653d5c6a0be75dad67bc
DIFF: https://github.com/llvm/llvm-project/commit/9334671aa7a8f73e529a653d5c6a0be75dad67bc.diff

LOG: [flang][openacc] Lower simple acc routine directive

This patch lower simple acc routine directive
with no clauses and no name inside function/subroutine.

Patch to handle name and clauses will follow up.
Patch to add attribute to the original routine will follow as well.

Reviewed By: razvanlupusoru

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

Added: 
    flang/test/Lower/OpenACC/acc-routine.f90

Modified: 
    flang/lib/Lower/OpenACC.cpp

Removed: 
    flang/test/Lower/OpenACC/Todo/acc-routine.f90


################################################################################
diff  --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 68e3f2701b9172..0658ad773d4510 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -31,6 +31,9 @@
 // Special value for * passed in device_type or gang clauses.
 static constexpr std::int64_t starCst = -1;
 
+static unsigned routineCounter = 0;
+static constexpr llvm::StringRef accRoutinePrefix = "acc_routine_";
+
 /// Generate the acc.bounds operation from the descriptor information.
 static llvm::SmallVector<mlir::Value>
 genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
@@ -2721,6 +2724,32 @@ static void genACC(Fortran::lower::AbstractConverter &converter,
   llvm_unreachable("unsupported declarative directive");
 }
 
+static void
+genACC(Fortran::lower::AbstractConverter &converter,
+       Fortran::lower::pft::Evaluation &eval,
+       const Fortran::parser::OpenACCRoutineConstruct &routineConstruct) {
+  fir::FirOpBuilder &builder = converter.getFirOpBuilder();
+  mlir::Location loc = converter.genLocation(routineConstruct.source);
+  std::optional<Fortran::parser::Name> name =
+      std::get<std::optional<Fortran::parser::Name>>(routineConstruct.t);
+  const auto &clauses =
+      std::get<Fortran::parser::AccClauseList>(routineConstruct.t);
+  if (name)
+    TODO(loc, "acc routine with name");
+  if (!clauses.v.empty())
+    TODO(loc, "acc routine with clauses");
+
+  mlir::func::FuncOp func = builder.getFunction();
+  mlir::ModuleOp mod = builder.getModule();
+  mlir::OpBuilder modBuilder(mod.getBodyRegion());
+  std::stringstream routineOpName;
+  routineOpName << accRoutinePrefix.str() << routineCounter++;
+  modBuilder.create<mlir::acc::RoutineOp>(
+      loc, routineOpName.str(), func.getName(), mlir::StringAttr{},
+      mlir::UnitAttr{}, mlir::UnitAttr{}, mlir::UnitAttr{}, mlir::UnitAttr{},
+      mlir::UnitAttr{}, mlir::UnitAttr{}, mlir::IntegerAttr{});
+}
+
 void Fortran::lower::genOpenACCConstruct(
     Fortran::lower::AbstractConverter &converter,
     Fortran::semantics::SemanticsContext &semanticsContext,
@@ -2774,8 +2803,7 @@ void Fortran::lower::genOpenACCDeclarativeConstruct(
           },
           [&](const Fortran::parser::OpenACCRoutineConstruct
                   &routineConstruct) {
-            TODO(converter.genLocation(routineConstruct.source),
-                 "OpenACC Routine construct not lowered yet!");
+            genACC(converter, eval, routineConstruct);
           },
       },
       accDeclConstruct.u);

diff  --git a/flang/test/Lower/OpenACC/Todo/acc-routine.f90 b/flang/test/Lower/OpenACC/Todo/acc-routine.f90
deleted file mode 100644
index 62e39759d76238..00000000000000
--- a/flang/test/Lower/OpenACC/Todo/acc-routine.f90
+++ /dev/null
@@ -1,12 +0,0 @@
-! This test checks lowering of OpenACC routine Directive.
-
-// RUN: not flang-new -fc1 -emit-fir -fopenacc %s 2>&1 | FileCheck %s
-
-program main
-  // CHECK: not yet implemented: OpenACC Routine construct not lowered yet!
-  !$acc routine(sub) seq
-contains
-  subroutine sub(a)
-    real :: a(:)
-  end
-end

diff  --git a/flang/test/Lower/OpenACC/acc-routine.f90 b/flang/test/Lower/OpenACC/acc-routine.f90
new file mode 100644
index 00000000000000..c25629024642fe
--- /dev/null
+++ b/flang/test/Lower/OpenACC/acc-routine.f90
@@ -0,0 +1,10 @@
+! This test checks lowering of OpenACC routine directive.
+
+! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
+
+subroutine acc_routine1()
+  !$acc routine
+end subroutine
+
+! CHECK: acc.routine @acc_routine_0 func(@_QPacc_routine1)
+! CHECK-LABEL: func.func @_QPacc_routine1()


        


More information about the flang-commits mailing list