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