[flang-commits] [flang] 580c31f - [flang][openacc] Add initial support to lower private clause to the new design

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Mon May 22 09:52:19 PDT 2023


Author: Valentin Clement
Date: 2023-05-22T09:52:11-07:00
New Revision: 580c31f44631b999a502300f69adc94e52f59541

URL: https://github.com/llvm/llvm-project/commit/580c31f44631b999a502300f69adc94e52f59541
DIFF: https://github.com/llvm/llvm-project/commit/580c31f44631b999a502300f69adc94e52f59541.diff

LOG: [flang][openacc] Add initial support to lower private clause to the new design

This patch adds the initial infrastructure to lower the private clause to the
new design introduced in D150622. The init region is not implemented yet
and currently only yield the input argument (semantic is then similar with the previous
design currently). Implementation will come in a follow up patch to keep patch.

This patch also re-enable test commented out for `acc serial`, `acc serial loop`,
`acc parallel` and `acc parallel loop` with private clause.

Depends on D150972, D150973

Reviewed By: razvanlupusoru, jeanPerier

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

Added: 
    

Modified: 
    flang/lib/Lower/OpenACC.cpp
    flang/test/Lower/OpenACC/acc-parallel-loop.f90
    flang/test/Lower/OpenACC/acc-parallel.f90
    flang/test/Lower/OpenACC/acc-serial-loop.f90
    flang/test/Lower/OpenACC/acc-serial.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index cc2113c230b4e..8c98fe70446c5 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -478,6 +478,57 @@ static void genDataExitOperations(fir::FirOpBuilder &builder,
   }
 }
 
+static mlir::acc::PrivateRecipeOp
+createBasePrivateRecipeOp(fir::FirOpBuilder &builder, mlir::Value input,
+                          llvm::StringRef recipeName, mlir::Location loc) {
+  mlir::ModuleOp mod = builder.getModule();
+  mlir::OpBuilder modBuilder(mod.getBodyRegion());
+  mlir::Type ty = input.getType();
+  auto recipe =
+      modBuilder.create<mlir::acc::PrivateRecipeOp>(loc, recipeName, ty);
+  builder.createBlock(&recipe.getInitRegion(), recipe.getInitRegion().end(),
+                      {ty}, {loc});
+  builder.setInsertionPointToEnd(&recipe.getInitRegion().back());
+  builder.create<mlir::acc::YieldOp>(
+      loc, recipe.getInitRegion().front().getArgument(0));
+  return recipe;
+}
+
+static void
+genPrivatizations(const Fortran::parser::AccObjectList &objectList,
+                  Fortran::lower::AbstractConverter &converter,
+                  Fortran::semantics::SemanticsContext &semanticsContext,
+                  Fortran::lower::StatementContext &stmtCtx,
+                  llvm::SmallVectorImpl<mlir::Value> &dataOperands,
+                  llvm::SmallVector<mlir::Attribute> &privatizations) {
+  fir::FirOpBuilder &builder = converter.getFirOpBuilder();
+  mlir::ModuleOp mod = builder.getModule();
+  for (const auto &accObject : objectList.v) {
+    llvm::SmallVector<mlir::Value> bounds;
+    std::stringstream asFortran;
+    mlir::Location operandLocation = genOperandLocation(converter, accObject);
+    mlir::Value baseAddr = gatherDataOperandAddrAndBounds(
+        converter, builder, semanticsContext, stmtCtx, accObject,
+        operandLocation, asFortran, bounds);
+
+    std::string recipeName = fir::getTypeAsString(
+        baseAddr.getType(), converter.getKindMap(), "privatization");
+    if (auto recipe =
+            mod.lookupSymbol<mlir::acc::PrivateRecipeOp>(recipeName)) {
+      privatizations.push_back(mlir::SymbolRefAttr::get(
+          builder.getContext(), recipe.getSymName().str()));
+    } else {
+      auto crtPos = builder.saveInsertionPoint();
+      mlir::acc::PrivateRecipeOp newRecipe = createBasePrivateRecipeOp(
+          builder, baseAddr, recipeName, operandLocation);
+      builder.restoreInsertionPoint(crtPos);
+      privatizations.push_back(mlir::SymbolRefAttr::get(
+          builder.getContext(), newRecipe.getSymName().str()));
+    }
+    dataOperands.push_back(baseAddr);
+  }
+}
+
 template <typename Clause>
 static void genObjectListWithModifier(
     const Clause *x, Fortran::lower::AbstractConverter &converter,
@@ -824,9 +875,9 @@ createComputeOp(Fortran::lower::AbstractConverter &converter,
       copyEntryOperands, copyoutEntryOperands, createEntryOperands,
       dataClauseOperands;
 
-  // TODO: need to more work/design.
   llvm::SmallVector<mlir::Value> reductionOperands, privateOperands,
       firstprivateOperands;
+  llvm::SmallVector<mlir::Attribute> privatizations;
 
   // Async, wait and self clause have optional values but can be present with
   // no value as well. When there is no value, the op has an attribute to
@@ -973,8 +1024,8 @@ createComputeOp(Fortran::lower::AbstractConverter &converter,
     } else if (const auto *privateClause =
                    std::get_if<Fortran::parser::AccClause::Private>(
                        &clause.u)) {
-      genObjectList(privateClause->v, converter, semanticsContext, stmtCtx,
-                    privateOperands);
+      genPrivatizations(privateClause->v, converter, semanticsContext, stmtCtx,
+                        privateOperands, privatizations);
     } else if (const auto *firstprivateClause =
                    std::get_if<Fortran::parser::AccClause::Firstprivate>(
                        &clause.u)) {
@@ -1019,6 +1070,12 @@ createComputeOp(Fortran::lower::AbstractConverter &converter,
   if (addSelfAttr)
     computeOp.setSelfAttrAttr(builder.getUnitAttr());
 
+  if constexpr (!std::is_same_v<Op, mlir::acc::KernelsOp>) {
+    if (!privatizations.empty())
+      computeOp.setPrivatizationsAttr(
+          mlir::ArrayAttr::get(builder.getContext(), privatizations));
+  }
+
   auto insPt = builder.saveInsertionPoint();
   builder.setInsertionPointAfter(computeOp);
 

diff  --git a/flang/test/Lower/OpenACC/acc-parallel-loop.f90 b/flang/test/Lower/OpenACC/acc-parallel-loop.f90
index 1b91b7c27a7f0..353081465e8e1 100644
--- a/flang/test/Lower/OpenACC/acc-parallel-loop.f90
+++ b/flang/test/Lower/OpenACC/acc-parallel-loop.f90
@@ -2,6 +2,13 @@
 
 ! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
 
+! CHECK-LABEL: acc.private.recipe @privatization_10xf32 : !fir.ref<!fir.array<10xf32>> init {
+! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10xf32>>):
+! CHECK:   acc.yield %{{.*}} : !fir.ref<!fir.array<10xf32>>
+! CHECK: }
+
+! CHECK-LABEL: func.func @_QPacc_parallel_loop()
+
 subroutine acc_parallel_loop
   integer :: i, j
 
@@ -442,19 +449,18 @@ subroutine acc_parallel_loop
 ! CHECK:        acc.yield
 ! CHECK-NEXT: }{{$}}
 
-! TODO: will be updated after lowering change in privatization to MLIR
-!  !$acc parallel loop private(a) firstprivate(b)
-!  DO i = 1, n
-!    a(i) = b(i)
-!  END DO
+  !$acc parallel loop private(a) firstprivate(b)
+  DO i = 1, n
+    a(i) = b(i)
+  END DO
 
-! TODO:      acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
-! TODO:        acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
-! TODO:          fir.do_loop
-! TODO:          acc.yield
-! TODO-NEXT:   }{{$}}
-! TODO:        acc.yield
-! TODO-NEXT: }{{$}}
+! CHECK:      acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(@privatization_10xf32 -> %[[A]] : !fir.ref<!fir.array<10xf32>>) {
+! CHECK:        acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
+! CHECK:          fir.do_loop
+! CHECK:          acc.yield
+! CHECK-NEXT:   }{{$}}
+! CHECK:        acc.yield
+! CHECK-NEXT: }{{$}}
 
   !$acc parallel loop seq
   DO i = 1, n

diff  --git a/flang/test/Lower/OpenACC/acc-parallel.f90 b/flang/test/Lower/OpenACC/acc-parallel.f90
index 93caa367b578b..6d321bf12c4aa 100644
--- a/flang/test/Lower/OpenACC/acc-parallel.f90
+++ b/flang/test/Lower/OpenACC/acc-parallel.f90
@@ -2,6 +2,13 @@
 
 ! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
 
+! CHECK-LABEL: acc.private.recipe @privatization_10x10xf32 : !fir.ref<!fir.array<10x10xf32>> init {
+! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10x10xf32>>):
+! CHECK: acc.yield %{{.*}} : !fir.ref<!fir.array<10x10xf32>>
+! CHECK: }
+
+! CHECK-LABEL: func.func @_QPacc_parallel()
+
 subroutine acc_parallel
   integer :: i, j
 
@@ -288,12 +295,11 @@ subroutine acc_parallel
 !CHECK: acc.detach accPtr(%[[ATTACH_D]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "d"}
 !CHECK: acc.detach accPtr(%[[ATTACH_E]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "e"}
 
-! TODO: will be updated after lowering change in privatization to MLIR
-!  !$acc parallel private(a) firstprivate(b) private(c)
-!  !$acc end parallel
+!$acc parallel private(a) firstprivate(b) private(c)
+!$acc end parallel
 
-!TODO:      acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(%[[A]], %[[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
-!TODO:        acc.yield
-!TODO-NEXT: }{{$}}
+! CHECK:      acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(@privatization_10x10xf32 -> %[[A]] : !fir.ref<!fir.array<10x10xf32>>, @privatization_10x10xf32 -> %[[C]] : !fir.ref<!fir.array<10x10xf32>>) {
+! CHECK:        acc.yield
+! CHECK-NEXT: }{{$}}
 
 end subroutine acc_parallel

diff  --git a/flang/test/Lower/OpenACC/acc-serial-loop.f90 b/flang/test/Lower/OpenACC/acc-serial-loop.f90
index 968fd2e5e4b27..44c5f1cb71bb1 100644
--- a/flang/test/Lower/OpenACC/acc-serial-loop.f90
+++ b/flang/test/Lower/OpenACC/acc-serial-loop.f90
@@ -2,6 +2,13 @@
 
 ! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
 
+! CHECK-LABEL: acc.private.recipe @privatization_10xf32 : !fir.ref<!fir.array<10xf32>> init {
+! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10xf32>>):
+! CHECK:   acc.yield %{{.*}} : !fir.ref<!fir.array<10xf32>>
+! CHECK: }
+
+! CHECK-LABEL: func.func @_QPacc_serial_loop()
+
 subroutine acc_serial_loop
   integer :: i, j
 
@@ -358,19 +365,18 @@ subroutine acc_serial_loop
 ! CHECK:        acc.yield
 ! CHECK-NEXT: }{{$}}
 
-! TODO: update when lowering is updated to new private design
-!  !$acc serial loop private(a) firstprivate(b)
-!  DO i = 1, n
-!    a(i) = b(i)
-!  END DO
+  !$acc serial loop private(a) firstprivate(b)
+  DO i = 1, n
+    a(i) = b(i)
+  END DO
 
-! TODO:      acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
-! TODO:        acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
-! TODO:          fir.do_loop
-! TODO:          acc.yield
-! TODO-NEXT:   }{{$}}
-! TODO:        acc.yield
-! TODO-NEXT: }{{$}}
+! CHECK:      acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(@privatization_10xf32 -> %[[A]] : !fir.ref<!fir.array<10xf32>>) {
+! CHECK:        acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
+! CHECK:          fir.do_loop
+! CHECK:          acc.yield
+! CHECK-NEXT:   }{{$}}
+! CHECK:        acc.yield
+! CHECK-NEXT: }{{$}}
 
   !$acc serial loop seq
   DO i = 1, n

diff  --git a/flang/test/Lower/OpenACC/acc-serial.f90 b/flang/test/Lower/OpenACC/acc-serial.f90
index ab0593608a5b4..6d3b45f246413 100644
--- a/flang/test/Lower/OpenACC/acc-serial.f90
+++ b/flang/test/Lower/OpenACC/acc-serial.f90
@@ -2,6 +2,13 @@
 
 ! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
 
+! CHECK-LABEL: acc.private.recipe @privatization_10x10xf32 : !fir.ref<!fir.array<10x10xf32>> init {
+! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10x10xf32>>):
+! CHECK: acc.yield %{{.*}} : !fir.ref<!fir.array<10x10xf32>>
+! CHECK: }
+
+! CHECK-LABEL: func.func @_QPacc_serial()
+
 subroutine acc_serial
   integer :: i, j
 
@@ -231,12 +238,11 @@ subroutine acc_serial
 ! CHECK: acc.detach accPtr(%[[ATTACH_D]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "d"}
 ! CHECK: acc.detach accPtr(%[[ATTACH_E]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "e"}
 
-! TODO: update when lowering is updated to new private design
-!  !$acc serial private(a) firstprivate(b) private(c)
-!  !$acc end serial
+  !$acc serial private(a) firstprivate(b) private(c)
+  !$acc end serial
 
-! TODO:      acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(%[[A]], %[[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
-! TODO:        acc.yield
-! TODO-NEXT: }{{$}}
+! CHECK:      acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(@privatization_10x10xf32 -> %[[A]] : !fir.ref<!fir.array<10x10xf32>>, @privatization_10x10xf32 -> %[[C]] : !fir.ref<!fir.array<10x10xf32>>) {
+! CHECK:        acc.yield
+! CHECK-NEXT: }{{$}}
 
 end subroutine


        


More information about the flang-commits mailing list