MLIR 23.0.0git
OpenACCUtils.h
Go to the documentation of this file.
1//===- OpenACCUtils.h - OpenACC Utilities -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_DIALECT_OPENACC_OPENACCUTILS_H_
10#define MLIR_DIALECT_OPENACC_OPENACCUTILS_H_
11
13#include "mlir/IR/Remarks.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16#include <functional>
17
18namespace mlir {
19class DominanceInfo;
21namespace acc {
22
23/// Used to obtain the enclosing compute construct operation that contains
24/// the provided `region`. Returns nullptr if no compute construct operation
25/// is found. The returned operation is one of types defined by
26/// `ACC_COMPUTE_CONSTRUCT_OPS`.
27mlir::Operation *getEnclosingComputeOp(mlir::Region &region);
28
29/// Returns true if this value is only used by `acc.private` operations in the
30/// `region`.
31bool isOnlyUsedByPrivateClauses(mlir::Value val, mlir::Region &region);
32
33/// Returns true if this value is only used by `acc.reduction` operations in
34/// the `region`.
35bool isOnlyUsedByReductionClauses(mlir::Value val, mlir::Region &region);
36
37/// Looks for an OpenACC default attribute on the current operation `op` or in
38/// a parent operation which encloses `op`. This is useful because OpenACC
39/// specification notes that a visible default clause is the nearest default
40/// clause appearing on the compute construct or a lexically containing data
41/// construct.
42std::optional<ClauseDefaultValue> getDefaultAttr(mlir::Operation *op);
43
44/// Get the type category of an OpenACC variable.
45mlir::acc::VariableTypeCategory getTypeCategory(mlir::Value var);
46
47/// Attempts to extract the variable name from a value by walking through
48/// view-like operations until an `acc.var_name` attribute is found. Returns
49/// empty string if no name is found.
50std::string getVariableName(mlir::Value v);
51
52/// Get the recipe name for a given recipe kind and type.
53/// Returns an empty string if not possible to generate a recipe name.
54std::string getRecipeName(mlir::acc::RecipeKind kind, mlir::Type type);
55
56// Get the base entity from partial entity access. This is used for getting
57// the base `struct` from an operation that only accesses a field or the
58// base `array` from an operation that only accesses a subarray.
59mlir::Value getBaseEntity(mlir::Value val);
60
61/// Check if a symbol use is valid for use in an OpenACC region.
62/// This includes looking for various attributes such as `acc.routine_info`
63/// and `acc.declare` attributes.
64/// \param user The operation using the symbol
65/// \param symbol The symbol reference being used
66/// \param definingOpPtr Optional output parameter to receive the defining op
67/// \return true if the symbol use is valid, false otherwise
68bool isValidSymbolUse(mlir::Operation *user, mlir::SymbolRefAttr symbol,
69 mlir::Operation **definingOpPtr = nullptr);
70
71/// Check if a value represents device data.
72/// This checks if the value represents device data via the
73/// MappableType, PointerLikeType, and GlobalVariableOpInterface interfaces.
74/// \param val The value to check
75/// \return true if the value is device data, false otherwise
76bool isDeviceValue(mlir::Value val);
77
78/// Check if a value use is valid in an OpenACC region.
79/// This is true if:
80/// - The value is produced by an ACC data entry operation
81/// - The value is device data
82/// - The value is only used by private clauses in the region
83/// \param val The value to check
84/// \param region The OpenACC region
85/// \return true if the value use is valid, false otherwise
86bool isValidValueUse(mlir::Value val, mlir::Region &region);
87
88/// Collects all data clauses that dominate the compute construct.
89/// This includes data clauses from:
90/// - The compute construct itself
91/// - Enclosing data constructs
92/// - Applicable declare directives (those that dominate and post-dominate)
93/// This is used to determine if a variable is already covered by an existing
94/// data clause.
95/// \param computeConstructOp The compute construct operation
96/// \param domInfo Dominance information
97/// \param postDomInfo Post-dominance information
98/// \return Vector of data clause values that dominate the compute construct
99llvm::SmallVector<mlir::Value>
100getDominatingDataClauses(mlir::Operation *computeConstructOp,
101 mlir::DominanceInfo &domInfo,
102 mlir::PostDominanceInfo &postDomInfo);
103
104/// Emit an OpenACC remark with lazy message generation.
105///
106/// The messageFn is only invoked if remarks are enabled, allowing callers
107/// to avoid constructing expensive messages when remarks are disabled.
108///
109/// \param op The operation to emit the remark for.
110/// \param messageFn A callable that returns the remark message.
111/// \param category Optional category for the remark. Defaults to "openacc".
112/// \return An in-flight remark object that can be used to append
113/// additional information to the remark.
114remark::detail::InFlightRemark
115emitRemark(mlir::Operation *op, const std::function<std::string()> &messageFn,
116 llvm::StringRef category = "openacc");
117
118/// Emit an OpenACC remark for the given operation with the given message.
119///
120/// \param op The operation to emit the remark for.
121/// \param message The remark message.
122/// \param category Optional category for the remark. Defaults to "openacc".
123/// \return An in-flight remark object that can be used to append
124/// additional information to the remark.
125inline remark::detail::InFlightRemark
126emitRemark(mlir::Operation *op, const llvm::Twine &message,
127 llvm::StringRef category = "openacc") {
128 return emitRemark(
129 op, std::function<std::string()>([msg = message.str()]() { return msg; }),
130 category);
131}
132
133} // namespace acc
134} // namespace mlir
135
136#endif // MLIR_DIALECT_OPENACC_OPENACCUTILS_H_
A class for computing basic dominance information.
Definition Dominance.h:140
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
A class for computing basic postdominance information.
Definition Dominance.h:204
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.
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 &region)
Returns true if this value is only used by acc.reduction operations in the region.
bool isValidValueUse(mlir::Value val, mlir::Region &region)
Check if a value use is valid in an OpenACC region.
mlir::Operation * getEnclosingComputeOp(mlir::Region &region)
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.
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.
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 &region)
Returns true if this value is only used by acc.private operations in the region.
Include the generated interface declarations.