[flang-commits] [flang] 99e880b - [flang][openacc][NFC] Add API to create acc.private.recipe from FIR type

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Tue May 23 15:08:49 PDT 2023


Author: Valentin Clement
Date: 2023-05-23T15:08:44-07:00
New Revision: 99e880b672c98b791418d0ed8a41c30f1c52140b

URL: https://github.com/llvm/llvm-project/commit/99e880b672c98b791418d0ed8a41c30f1c52140b
DIFF: https://github.com/llvm/llvm-project/commit/99e880b672c98b791418d0ed8a41c30f1c52140b.diff

LOG: [flang][openacc][NFC] Add API to create acc.private.recipe from FIR type

Simply make the creation of acc.private.recipe accesible through an API.
This will be useful when we will implement passes like the implicit
privatization.

Reviewed By: razvanlupusoru

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

Added: 
    

Modified: 
    flang/include/flang/Lower/OpenACC.h
    flang/lib/Lower/OpenACC.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Lower/OpenACC.h b/flang/include/flang/Lower/OpenACC.h
index 95cc617531226..cd876ea2517aa 100644
--- a/flang/include/flang/Lower/OpenACC.h
+++ b/flang/include/flang/Lower/OpenACC.h
@@ -13,6 +13,19 @@
 #ifndef FORTRAN_LOWER_OPENACC_H
 #define FORTRAN_LOWER_OPENACC_H
 
+namespace llvm {
+class StringRef;
+}
+
+namespace mlir {
+class Location;
+class Type;
+class OpBuilder;
+namespace acc {
+class PrivateRecipeOp;
+}
+} // namespace mlir
+
 namespace Fortran {
 namespace parser {
 struct OpenACCConstruct;
@@ -38,6 +51,12 @@ void genOpenACCDeclarativeConstruct(
     AbstractConverter &, pft::Evaluation &,
     const parser::OpenACCDeclarativeConstruct &);
 
+/// Get a acc.private.recipe op for the given type or create it if it does not
+/// exist yet.
+mlir::acc::PrivateRecipeOp createOrGetPrivateRecipe(mlir::OpBuilder &,
+                                                    llvm::StringRef,
+                                                    mlir::Location, mlir::Type);
+
 } // namespace lower
 } // namespace Fortran
 

diff  --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 2f74f083159c2..371f5484c09ce 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -478,12 +478,17 @@ 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::acc::PrivateRecipeOp
+Fortran::lower::createOrGetPrivateRecipe(mlir::OpBuilder &builder,
+                                         llvm::StringRef recipeName,
+                                         mlir::Location loc, mlir::Type ty) {
+  mlir::ModuleOp mod =
+      builder.getBlock()->getParent()->getParentOfType<mlir::ModuleOp>();
+  if (auto recipe = mod.lookupSymbol<mlir::acc::PrivateRecipeOp>(recipeName))
+    return recipe;
+
+  auto crtPos = builder.saveInsertionPoint();
   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(),
@@ -491,6 +496,7 @@ createBasePrivateRecipeOp(fir::FirOpBuilder &builder, mlir::Value input,
   builder.setInsertionPointToEnd(&recipe.getInitRegion().back());
   builder.create<mlir::acc::YieldOp>(
       loc, recipe.getInitRegion().front().getArgument(0));
+  builder.restoreInsertionPoint(crtPos);
   return recipe;
 }
 
@@ -502,7 +508,6 @@ genPrivatizations(const Fortran::parser::AccObjectList &objectList,
                   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;
@@ -510,21 +515,13 @@ genPrivatizations(const Fortran::parser::AccObjectList &objectList,
     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()));
-    }
+    mlir::acc::PrivateRecipeOp recipe =
+        Fortran::lower::createOrGetPrivateRecipe(
+            builder, recipeName, operandLocation, baseAddr.getType());
+    privatizations.push_back(mlir::SymbolRefAttr::get(
+        builder.getContext(), recipe.getSymName().str()));
     dataOperands.push_back(baseAddr);
   }
 }


        


More information about the flang-commits mailing list