MLIR 24.0.0git
OpenACC.h
Go to the documentation of this file.
1//===- OpenACC.h - MLIR OpenACC Dialect -------------------------*- C++ -*-===//
2//
3// Part of the MLIR 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// This file declares the OpenACC dialect in MLIR.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_DIALECT_OPENACC_OPENACC_H_
14#define MLIR_DIALECT_OPENACC_OPENACC_H_
15
18#include "mlir/IR/Dialect.h"
21#include "mlir/IR/SymbolTable.h"
22
26#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
27#include "mlir/Dialect/OpenACC/OpenACCOpsEnums.h.inc"
28#include "mlir/Dialect/OpenACC/OpenACCOpsInterfaces.h.inc"
29#include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.h.inc"
31#include "mlir/IR/Value.h"
36#include "llvm/ADT/SmallVector.h"
37#include <variant>
38
39#define GET_TYPEDEF_CLASSES
40#include "mlir/Dialect/OpenACC/OpenACCOpsTypes.h.inc"
41
42#define GET_ATTRDEF_CLASSES
43#include "mlir/Dialect/OpenACC/OpenACCOpsAttributes.h.inc"
44
46
47#define GET_OP_CLASSES
48#include "mlir/Dialect/OpenACC/OpenACCOps.h.inc"
49
50#define ACC_DATA_ENTRY_OPS \
51 mlir::acc::CopyinOp, mlir::acc::CreateOp, mlir::acc::PresentOp, \
52 mlir::acc::NoCreateOp, mlir::acc::AttachOp, mlir::acc::DevicePtrOp, \
53 mlir::acc::GetDevicePtrOp, mlir::acc::PrivateOp, \
54 mlir::acc::FirstprivateOp, mlir::acc::FirstprivateMapInitialOp, \
55 mlir::acc::UpdateDeviceOp, mlir::acc::UseDeviceOp, \
56 mlir::acc::ReductionOp, mlir::acc::DeclareDeviceResidentOp, \
57 mlir::acc::DeclareLinkOp, mlir::acc::CacheOp
58#define ACC_DATA_ENTRY_AND_INIT_OPS \
59 ACC_DATA_ENTRY_OPS, mlir::acc::ReductionInitOp
60#define ACC_DATA_EXIT_OPS \
61 mlir::acc::CopyoutOp, mlir::acc::DeleteOp, mlir::acc::DetachOp, \
62 mlir::acc::UpdateHostOp
63#define ACC_DATA_CLAUSE_OPS ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS
64#define ACC_COMPUTE_CONSTRUCT_OPS \
65 mlir::acc::ParallelOp, mlir::acc::KernelsOp, mlir::acc::SerialOp
66#define ACC_COMPUTE_CONSTRUCT_AND_LOOP_OPS \
67 ACC_COMPUTE_CONSTRUCT_OPS, mlir::acc::LoopOp
68#define ACC_DATA_CONSTRUCT_STRUCTURED_OPS \
69 mlir::acc::DataOp, mlir::acc::DeclareOp, mlir::acc::HostDataOp
70#define ACC_DATA_CONSTRUCT_UNSTRUCTURED_OPS \
71 mlir::acc::EnterDataOp, mlir::acc::ExitDataOp, mlir::acc::UpdateOp, \
72 mlir::acc::DeclareEnterOp, mlir::acc::DeclareExitOp
73#define ACC_DATA_CONSTRUCT_OPS \
74 ACC_DATA_CONSTRUCT_STRUCTURED_OPS, ACC_DATA_CONSTRUCT_UNSTRUCTURED_OPS
75#define ACC_COMPUTE_AND_DATA_CONSTRUCT_OPS \
76 ACC_COMPUTE_CONSTRUCT_OPS, ACC_DATA_CONSTRUCT_OPS
77#define ACC_COMPUTE_LOOP_AND_DATA_CONSTRUCT_OPS \
78 ACC_COMPUTE_CONSTRUCT_AND_LOOP_OPS, ACC_DATA_CONSTRUCT_OPS
79
80namespace mlir {
81namespace acc {
82/// Enumeration used to encode the execution mapping on a loop construct.
83/// They refer directly to the OpenACC 3.3 standard:
84/// 2.9.2. gang
85/// 2.9.3. worker
86/// 2.9.4. vector
87///
88/// Value can be combined bitwise to reflect the mapping applied to the
89/// construct. e.g. `acc.loop gang vector`, the `gang` and `vector` could be
90/// combined and the final mapping value would be 5 (4 | 1).
91enum OpenACCExecMapping { NONE = 0, VECTOR = 1, WORKER = 2, GANG = 4 };
92
93/// Used to obtain the `var` from a data clause operation.
94/// Returns empty value if not a data clause operation or is a data exit
95/// operation with no `var`.
96mlir::Value getVar(mlir::Operation *accDataClauseOp);
97
98/// Used to obtain the `var` from a data clause operation if it implements
99/// `PointerLikeType`.
101getVarPtr(mlir::Operation *accDataClauseOp);
102
103/// Used to obtains the `varType` from a data clause operation which records
104/// the type of variable. When `var` is `PointerLikeType`, this returns
105/// the type of the pointer target.
106mlir::Type getVarType(mlir::Operation *accDataClauseOp);
107
108/// Used to obtain the `accVar` from a data clause operation.
109/// When a data entry operation, it obtains its result `accVar` value.
110/// If a data exit operation, it obtains its operand `accVar` value.
111/// Returns empty value if not a data clause operation.
112mlir::Value getAccVar(mlir::Operation *accDataClauseOp);
113
114/// Used to obtain the `accVar` from a data clause operation if it implements
115/// `PointerLikeType`.
117getAccPtr(mlir::Operation *accDataClauseOp);
118
119/// Used to obtain the `varPtrPtr` from a data clause operation.
120/// Returns empty value if not a data clause operation.
122
123/// Used to obtain `bounds` from an acc data clause operation.
124/// Returns an empty vector if there are no bounds.
126
127/// Used to obtain `async` operands from an acc data clause operation.
128/// Returns an empty vector if there are no such operands.
130getAsyncOperands(mlir::Operation *accDataClauseOp);
131
132/// Returns an array of acc:DeviceTypeAttr attributes attached to
133/// an acc data clause operation, that correspond to the device types
134/// associated with the async clauses with an async-value.
135mlir::ArrayAttr getAsyncOperandsDeviceType(mlir::Operation *accDataClauseOp);
136
137/// Returns an array of acc:DeviceTypeAttr attributes attached to
138/// an acc data clause operation, that correspond to the device types
139/// associated with the async clauses without an async-value.
140mlir::ArrayAttr getAsyncOnly(mlir::Operation *accDataClauseOp);
141
142/// The clauses of a construct can be given once per device type. Of the values
143/// that reach a given device type, the ones naming it are the most specific,
144/// then the ones naming every device type, then the ones given before any
145/// device_type clause.
147
148/// Used to obtain the `name` from an acc operation.
149std::optional<llvm::StringRef> getVarName(mlir::Operation *accOp);
150
151/// Used to find out whether `op` is a data entry operation or an
152/// `acc.map_info`. `op` may be null.
154
155/// Used to obtain the `dataClause` from a data entry operation.
156/// Returns empty optional if not a data entry operation.
157std::optional<mlir::acc::DataClause>
158getDataClause(mlir::Operation *accDataEntryOp);
159
160/// Used to find out whether data operation is implicit.
161/// Returns false if not a data operation or if it is a data operation without
162/// implicit flag.
163bool getImplicitFlag(mlir::Operation *accDataEntryOp);
164
165/// Used to find out whether the implementation created the data operation for
166/// its own bookkeeping, instead of to represent a data attribute of the
167/// program being compiled. Such an operation has no counterpart in that
168/// program, so it must not be described to the user.
169/// Returns false if not a data clause operation or if it is a data clause
170/// operation without the synthetic flag.
171bool getSyntheticFlag(mlir::Operation *accDataClauseOp);
172
173/// Used to get an immutable range iterating over the data operands.
175
176/// Used to get a mutable range iterating over the data operands.
178
179/// Used to get the recipe attribute from a data clause operation.
180mlir::SymbolRefAttr getRecipe(mlir::Operation *accOp);
181
182/// Used to check whether the provided `type` implements the `PointerLikeType`
183/// interface.
184inline bool isPointerLikeType(mlir::Type type) {
185 return mlir::isa<mlir::acc::PointerLikeType>(type);
186}
187
188/// Used to check whether the provided `type` implements the `MappableType`
189/// interface.
190inline bool isMappableType(mlir::Type type) {
191 return mlir::isa<mlir::acc::MappableType>(type);
192}
193
194/// Used to obtain the attribute name for declare.
195static constexpr StringLiteral getDeclareAttrName() {
196 return StringLiteral("acc.declare");
197}
198
199static constexpr StringLiteral getDeclareActionAttrName() {
200 return StringLiteral("acc.declare_action");
201}
202
203static constexpr StringLiteral getRoutineInfoAttrName() {
204 return RoutineInfoAttr::name;
205}
206
207static constexpr StringLiteral getSpecializedRoutineAttrName() {
208 return SpecializedRoutineAttr::name;
209}
210
211/// Used to check whether the current operation is marked with
212/// `acc routine`. The operation passed in should be a function.
216
217/// Used to check whether this is a specialized accelerator version of
218/// `acc routine` function.
223
224static constexpr StringLiteral getFromDefaultClauseAttrName() {
225 return StringLiteral("acc.from_default");
226}
227
228/// Name for a unit attribute on a data entry operation stating that nothing in
229/// the region reads the descriptor its variable is reached through, so the
230/// mapping supplies the bounds but needs no device copy of the descriptor.
231static constexpr StringLiteral getNoDeviceDescriptorAttrName() {
232 return StringLiteral("acc.no_device_descriptor");
233}
234
235/// Name for an attribute attached to a loop indicating the number of loops
236/// collapsed to create that loop
237static constexpr StringLiteral getCollapseCountAttrName() {
238 return StringLiteral("acc.collapse_count");
239}
240
241/// Name for a unit attribute attached to the region an unstructured
242/// independent loop was converted to, recording that the loop could not be
243/// represented as a structured loop and is therefore not parallelized.
244static constexpr StringLiteral getUnstructuredIndependentLoopAttrName() {
245 return StringLiteral("acc.unstructured_independent_loop");
246}
247
248static constexpr StringLiteral getVarNameAttrName() {
249 return VarNameAttr::name;
250}
251
252static constexpr StringLiteral getCombinedConstructsAttrName() {
253 return CombinedConstructsTypeAttr::name;
254}
255
257 : public mlir::SideEffects::Resource::Base<RuntimeCounters> {
258 mlir::StringRef getName() const final { return "AccRuntimeCounters"; }
259 bool isAddressable() const override { return false; }
260};
261
263 : public mlir::SideEffects::Resource::Base<ConstructResource> {
264 mlir::StringRef getName() const final { return "AccConstructResource"; }
265 bool isAddressable() const override { return false; }
266};
267
269 : public mlir::SideEffects::Resource::Base<CurrentDeviceIdResource> {
270 mlir::StringRef getName() const final { return "AccCurrentDeviceIdResource"; }
271 bool isAddressable() const override { return false; }
272};
273
274template <typename ComputeOpT>
275bool isGangWorkerVectorAllOne(ComputeOpT op) {
276 // Strip index_cast operations from a value before checking for a constant.
277 auto stripIndexCasts = [](Value val) -> Value {
278 while (auto castOp = val.getDefiningOp<arith::IndexCastOp>())
279 val = castOp.getIn();
280 return val;
281 };
282
283 auto numGangs = op.getNumGangsValues();
284 if (numGangs.empty())
285 return false;
286 for (Value gangSize : numGangs) {
287 if (!isConstantIntValue(stripIndexCasts(gangSize), 1))
288 return false;
289 }
290 Value numWorkers = op.getNumWorkersValue();
291 if (!numWorkers)
292 return false;
293 Value vectorLength = op.getVectorLengthValue();
294 if (!vectorLength)
295 return false;
296 return isConstantIntValue(stripIndexCasts(numWorkers), 1) &&
297 isConstantIntValue(stripIndexCasts(vectorLength), 1);
298}
299} // namespace acc
300} // namespace mlir
301
302#endif // MLIR_DIALECT_OPENACC_OPENACC_H_
This class provides a mutable adaptor for a range of operands.
Definition ValueRange.h:119
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
bool hasDiscardableAttr(StringRef name)
Return true if this operation has a discardable attribute with the provided name.
Definition Operation.h:503
This base class is used for derived effects that are non-parametric.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:389
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
static constexpr StringLiteral getNoDeviceDescriptorAttrName()
Name for a unit attribute on a data entry operation stating that nothing in the region reads the desc...
Definition OpenACC.h:231
SmallVector< DeviceType, 3 > getDeviceTypesByPrecedence(DeviceType deviceType)
The clauses of a construct can be given once per device type.
Definition OpenACC.cpp:5138
bool getSyntheticFlag(mlir::Operation *accDataClauseOp)
Used to find out whether the implementation created the data operation for its own bookkeeping,...
Definition OpenACC.cpp:5183
static constexpr StringLiteral getFromDefaultClauseAttrName()
Definition OpenACC.h:224
mlir::Value getAccVar(mlir::Operation *accDataClauseOp)
Used to obtain the accVar from a data clause operation.
Definition OpenACC.cpp:5072
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
Definition OpenACC.cpp:5040
bool isAccRoutine(mlir::Operation *op)
Used to check whether the current operation is marked with acc routine.
Definition OpenACC.h:213
mlir::TypedValue< mlir::acc::PointerLikeType > getAccPtr(mlir::Operation *accDataClauseOp)
Used to obtain the accVar from a data clause operation if it implements PointerLikeType.
Definition OpenACC.cpp:5059
static constexpr StringLiteral getSpecializedRoutineAttrName()
Definition OpenACC.h:207
std::optional< mlir::acc::DataClause > getDataClause(mlir::Operation *accDataEntryOp)
Used to obtain the dataClause from a data entry operation.
Definition OpenACC.cpp:5159
bool isPointerLikeType(mlir::Type type)
Used to check whether the provided type implements the PointerLikeType interface.
Definition OpenACC.h:184
mlir::MutableOperandRange getMutableDataOperands(mlir::Operation *accOp)
Used to get a mutable range iterating over the data operands.
Definition OpenACC.cpp:5201
mlir::SmallVector< mlir::Value > getBounds(mlir::Operation *accDataClauseOp)
Used to obtain bounds from an acc data clause operation.
Definition OpenACC.cpp:5091
bool isDataEntryOp(mlir::Operation *op)
Used to find out whether op is a data entry operation or an acc.map_info.
Definition OpenACC.cpp:5169
OpenACCExecMapping
Enumeration used to encode the execution mapping on a loop construct.
Definition OpenACC.h:91
bool isSpecializedAccRoutine(mlir::Operation *op)
Used to check whether this is a specialized accelerator version of acc routine function.
Definition OpenACC.h:219
static constexpr StringLiteral getUnstructuredIndependentLoopAttrName()
Name for a unit attribute attached to the region an unstructured independent loop was converted to,...
Definition OpenACC.h:244
mlir::ValueRange getDataOperands(mlir::Operation *accOp)
Used to get an immutable range iterating over the data operands.
Definition OpenACC.cpp:5190
std::optional< llvm::StringRef > getVarName(mlir::Operation *accOp)
Used to obtain the name from an acc operation.
Definition OpenACC.cpp:5147
static constexpr StringLiteral getRoutineInfoAttrName()
Definition OpenACC.h:203
bool isGangWorkerVectorAllOne(ComputeOpT op)
Definition OpenACC.h:275
bool getImplicitFlag(mlir::Operation *accDataEntryOp)
Used to find out whether data operation is implicit.
Definition OpenACC.cpp:5173
mlir::SymbolRefAttr getRecipe(mlir::Operation *accOp)
Used to get the recipe attribute from a data clause operation.
Definition OpenACC.cpp:5211
mlir::SmallVector< mlir::Value > getAsyncOperands(mlir::Operation *accDataClauseOp)
Used to obtain async operands from an acc data clause operation.
Definition OpenACC.cpp:5108
bool isMappableType(mlir::Type type)
Used to check whether the provided type implements the MappableType interface.
Definition OpenACC.h:190
static constexpr StringLiteral getCombinedConstructsAttrName()
Definition OpenACC.h:252
mlir::Value getVarPtrPtr(mlir::Operation *accDataClauseOp)
Used to obtain the varPtrPtr from a data clause operation.
Definition OpenACC.cpp:5081
static constexpr StringLiteral getVarNameAttrName()
Definition OpenACC.h:248
mlir::ArrayAttr getAsyncOnly(mlir::Operation *accDataClauseOp)
Returns an array of acc:DeviceTypeAttr attributes attached to an acc data clause operation,...
Definition OpenACC.cpp:5130
static constexpr StringLiteral getDeclareAttrName()
Used to obtain the attribute name for declare.
Definition OpenACC.h:195
static constexpr StringLiteral getDeclareActionAttrName()
Definition OpenACC.h:199
mlir::Type getVarType(mlir::Operation *accDataClauseOp)
Used to obtains the varType from a data clause operation which records the type of variable.
Definition OpenACC.cpp:5048
static constexpr StringLiteral getCollapseCountAttrName()
Name for an attribute attached to a loop indicating the number of loops collapsed to create that loop...
Definition OpenACC.h:237
mlir::TypedValue< mlir::acc::PointerLikeType > getVarPtr(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation if it implements PointerLikeType.
Definition OpenACC.cpp:5026
mlir::ArrayAttr getAsyncOperandsDeviceType(mlir::Operation *accDataClauseOp)
Returns an array of acc:DeviceTypeAttr attributes attached to an acc data clause operation,...
Definition OpenACC.cpp:5122
Include the generated interface declarations.
bool isConstantIntValue(OpFoldResult ofr, int64_t value)
Return true if ofr is constant integer equal to value.
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.
Definition Value.h:494
mlir::StringRef getName() const final
Return a string name of the resource.
Definition OpenACC.h:264
bool isAddressable() const override
Returns true if this resource is addressable (effects on it can alias pointer-based memory).
Definition OpenACC.h:265
bool isAddressable() const override
Returns true if this resource is addressable (effects on it can alias pointer-based memory).
Definition OpenACC.h:271
mlir::StringRef getName() const final
Return a string name of the resource.
Definition OpenACC.h:270
mlir::StringRef getName() const final
Return a string name of the resource.
Definition OpenACC.h:258
bool isAddressable() const override
Returns true if this resource is addressable (effects on it can alias pointer-based memory).
Definition OpenACC.h:259