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