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