Skip to content

Commit

Permalink
[flang][openacc][NFC] Add API to create acc.private.recipe from FIR type
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clementval committed May 23, 2023
1 parent 6e3dcd3 commit 99e880b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
19 changes: 19 additions & 0 deletions flang/include/flang/Lower/OpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand Down
35 changes: 16 additions & 19 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,19 +478,25 @@ 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(),
{ty}, {loc});
builder.setInsertionPointToEnd(&recipe.getInitRegion().back());
builder.create<mlir::acc::YieldOp>(
loc, recipe.getInitRegion().front().getArgument(0));
builder.restoreInsertionPoint(crtPos);
return recipe;
}

Expand All @@ -502,29 +508,20 @@ 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;
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()));
}
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);
}
}
Expand Down

0 comments on commit 99e880b

Please sign in to comment.