18#include "llvm/ADT/SetVector.h"
19#include "llvm/ADT/TypeSwitch.h"
20#include "llvm/IR/Intrinsics.h"
21#include "llvm/Support/Casting.h"
29 auto barg = mlir::dyn_cast<mlir::BlockArgument>(v);
35 mlir::dyn_cast<mlir::acc::ComputeRegionOp>(block->
getParentOp());
36 if (!computeReg || block != computeReg.getBody())
41 return mlir::isa_and_nonnull<ACC_DATA_ENTRY_OPS>(def) ? def :
nullptr;
44template <
typename OpTy>
49 if (!region.
isAncestor(user->getParentRegion()))
51 return mlir::isa<OpTy>(user);
54 return llvm::all_of(val.
getUsers(), checkIfUsedOnlyByOpInside);
67std::optional<mlir::acc::ClauseDefaultValue>
69 std::optional<mlir::acc::ClauseDefaultValue> defaultAttr;
76 while (!defaultAttr.has_value() && currOp) {
79 std::optional<mlir::acc::ClauseDefaultValue>>(currOp)
80 .Case<ACC_COMPUTE_CONSTRUCT_OPS, mlir::acc::DataOp>(
81 [&](
auto op) {
return op.getDefaultAttr(); })
82 .Default([&](
Operation *) {
return std::nullopt; });
90 mlir::acc::VariableTypeCategory typeCategory =
91 mlir::acc::VariableTypeCategory::uncategorized;
92 if (
auto mappableTy = dyn_cast<mlir::acc::MappableType>(var.
getType()))
93 typeCategory = mappableTy.getTypeCategory(var);
94 else if (
auto pointerLikeTy =
95 dyn_cast<mlir::acc::PointerLikeType>(var.
getType()))
96 typeCategory = pointerLikeTy.getPointeeTypeCategory(
98 pointerLikeTy.getElementType());
109 return std::to_string(*constVal);
112 if (
auto varNameAttr =
114 return varNameAttr.getName().str();
117 if (isa<ACC_DATA_ENTRY_OPS>(definingOp))
122 if (
auto viewOp = dyn_cast<ViewLikeOpInterface>(definingOp)) {
123 current = viewOp.getViewSource();
135 assert(kind == mlir::acc::RecipeKind::private_recipe ||
136 kind == mlir::acc::RecipeKind::firstprivate_recipe ||
137 kind == mlir::acc::RecipeKind::reduction_recipe);
138 if (!llvm::isa<mlir::acc::PointerLikeType, mlir::acc::MappableType>(type))
141 std::string recipeName;
142 llvm::raw_string_ostream ss(recipeName);
143 ss << (kind == mlir::acc::RecipeKind::private_recipe ?
"privatization_"
144 : kind == mlir::acc::RecipeKind::firstprivate_recipe
145 ?
"firstprivatization_"
154 for (
char &c : recipeName) {
155 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'.' && c !=
'_') {
160 else if (c ==
'(' || c ==
')' || c ==
'[' || c ==
']' || c ==
'{' ||
161 c ==
'}' || c ==
'<' || c ==
'>')
172 if (
auto partialEntityAccessOp =
174 if (!partialEntityAccessOp.isCompleteView())
175 return partialEntityAccessOp.getBaseEntity();
182 mlir::SymbolRefAttr symbol,
193 *definingOpPtr = definingOp;
198 if (mlir::isa<mlir::acc::PrivateRecipeOp, mlir::acc::ReductionRecipeOp,
199 mlir::acc::FirstprivateRecipeOp>(definingOp))
205 mlir::dyn_cast<mlir::acc::GlobalVariableOpInterface>(definingOp))
206 if (globalVar.isDeviceData())
211 mlir::dyn_cast_if_present<mlir::FunctionOpInterface>(definingOp)) {
224 func.getFunctionBody().empty() &&
func.getName().starts_with(
"llvm.") &&
225 llvm::Intrinsic::lookupIntrinsicID(
func.getName()) !=
226 llvm::Intrinsic::not_intrinsic)
238 if (
auto mappableTy = dyn_cast<mlir::acc::MappableType>(val.
getType()))
239 if (mappableTy.isDeviceData(val))
242 if (
auto pointerLikeTy = dyn_cast<mlir::acc::PointerLikeType>(val.
getType()))
243 if (pointerLikeTy.isDeviceData(val))
249 if (
auto partialAccess =
250 dyn_cast<mlir::acc::PartialEntityAccessOpInterface>(defOp)) {
251 if (
mlir::Value base = partialAccess.getBaseEntity())
256 if (
auto addrOfIface =
257 dyn_cast<mlir::acc::AddressOfGlobalOpInterface>(defOp)) {
258 auto symbol = addrOfIface.getSymbol();
260 mlir::acc::GlobalVariableOpInterface>(defOp, symbol))
261 return global.isDeviceData();
272 llvm::isa<mlir::VectorType>(type))
276 if (isa_and_nonnull<ACC_DATA_ENTRY_OPS>(val.
getDefiningOp()))
294 llvm::SmallSetVector<mlir::Value, 8> dominatingDataClauses;
297 .Case<mlir::acc::ParallelOp, mlir::acc::KernelsOp, mlir::acc::SerialOp>(
299 for (
auto dataClause : op.getDataClauseOperands()) {
300 dominatingDataClauses.insert(dataClause);
307 while (currParentOp) {
308 if (mlir::isa<mlir::acc::DataOp>(currParentOp)) {
309 for (
auto dataClause : mlir::dyn_cast<mlir::acc::DataOp>(currParentOp)
310 .getDataClauseOperands()) {
311 dominatingDataClauses.insert(dataClause);
314 currParentOp = currParentOp->getParentOp();
321 return dominatingDataClauses.takeVector();
326 funcOp->walk([&](mlir::acc::DeclareEnterOp declareEnterOp) {
327 if (domInfo.
dominates(declareEnterOp.getOperation(), computeConstructOp)) {
330 for (
auto *user : declareEnterOp.getToken().getUsers())
331 if (
auto declareExit = mlir::dyn_cast<mlir::acc::DeclareExitOp>(user))
332 exits.push_back(declareExit);
336 if (!exits.empty() &&
337 llvm::all_of(exits, [&](mlir::acc::DeclareExitOp exitOp) {
338 return postDomInfo.postDominates(exitOp, computeConstructOp);
340 for (
auto dataClause : declareEnterOp.getDataClauseOperands())
341 dominatingDataClauses.insert(dataClause);
346 return dominatingDataClauses.takeVector();
351 const std::function<std::string()> &messageFn,
352 llvm::StringRef category) {
359 llvm::StringRef funcName;
360 if (
auto func = dyn_cast<mlir::FunctionOpInterface>(op))
361 funcName =
func.getName();
362 else if (
auto funcOp = op->
getParentOfType<mlir::FunctionOpInterface>())
363 funcName = funcOp.getName();
366 if (!funcName.empty())
367 opts = opts.function(funcName);
369 auto remark = engine->emitOptimizationRemark(loc, opts);
static bool isOnlyUsedByOpClauses(mlir::Value val, mlir::Region ®ion)
static std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
MLIRContext * getContext() const
Return the context this attribute belongs to.
Block represents an ordered list of Operations.
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
A class for computing basic dominance information.
bool dominates(Operation *a, Operation *b) const
Return true if operation A dominates operation B, i.e.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
remark::detail::RemarkEngine * getRemarkEngine()
Returns the remark engine for this context, or nullptr if none has been set.
Operation is the basic unit of execution within MLIR.
bool hasAttr(StringAttr name)
Return true if the operation has an attribute with the provided name, false otherwise.
Location getLoc()
The source location the operation was defined or derived from.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
A class for computing basic postdominance information.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
bool isAncestor(Region *other)
Return true if this region is ancestor of the other region.
ParentT getParentOfType()
Find the first parent operation of the given type, or nullptr if there is no ancestor operation.
@ Private
The symbol is private and may only be referenced by SymbolRefAttrs local to the operations within the...
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
void print(raw_ostream &os) const
Print the current type.
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
user_range getUsers() const
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
#define ACC_COMPUTE_CONSTRUCT_OPS
mlir::acc::VariableTypeCategory getTypeCategory(mlir::Value var)
Get the type category of an OpenACC variable.
std::string getVariableName(mlir::Value v)
Attempts to extract the variable name from a value by walking through view-like operations until an a...
bool isValidSymbolUse(mlir::Operation *user, mlir::SymbolRefAttr symbol, mlir::Operation **definingOpPtr=nullptr)
Check if a symbol use is valid for use in an OpenACC region.
mlir::Operation * getACCDataClauseOpForBlockArg(mlir::Value v)
If v is not a block argument of an acc.compute_region body, returns nullptr.
std::optional< ClauseDefaultValue > getDefaultAttr(mlir::Operation *op)
Looks for an OpenACC default attribute on the current operation op or in a parent operation which enc...
bool isOnlyUsedByReductionClauses(mlir::Value val, mlir::Region ®ion)
Returns true if this value is only used by acc.reduction operations in the region.
std::optional< llvm::StringRef > getVarName(mlir::Operation *accOp)
Used to obtain the name from an acc operation.
static constexpr StringLiteral getRoutineInfoAttrName()
bool isValidValueUse(mlir::Value val, mlir::Region ®ion)
Check if a value use is valid in an OpenACC region.
mlir::Operation * getEnclosingComputeOp(mlir::Region ®ion)
Used to obtain the enclosing compute construct operation that contains the provided region.
llvm::SmallVector< mlir::Value > getDominatingDataClauses(mlir::Operation *computeConstructOp, mlir::DominanceInfo &domInfo, mlir::PostDominanceInfo &postDomInfo)
Collects all data clauses that dominate the compute construct.
static constexpr StringLiteral getVarNameAttrName()
std::string getRecipeName(mlir::acc::RecipeKind kind, mlir::Type type)
Get the recipe name for a given recipe kind and type.
remark::detail::InFlightRemark emitRemark(mlir::Operation *op, const std::function< std::string()> &messageFn, llvm::StringRef category="openacc")
Emit an OpenACC remark with lazy message generation.
static constexpr StringLiteral getDeclareAttrName()
Used to obtain the attribute name for declare.
bool isDeviceValue(mlir::Value val)
Check if a value represents device data.
mlir::Value getBaseEntity(mlir::Value val)
bool isOnlyUsedByPrivateClauses(mlir::Value val, mlir::Region ®ion)
Returns true if this value is only used by acc.private operations in the region.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.