MLIR 24.0.0git
OpenACCRuntimeUtils.h
Go to the documentation of this file.
1//===- OpenACCRuntimeUtils.h - OpenACC runtime call 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// Utilities for resolving OpenACC compiler-to-runtime entry points declared in
10// OpenACCRuntimeFunctions.def.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_DIALECT_OPENACC_OPENACCRUNTIMEUTILS_H
15#define MLIR_DIALECT_OPENACC_OPENACCRUNTIMEUTILS_H
16
19#include "mlir/IR/Builders.h"
20#include "mlir/IR/BuiltinOps.h"
21#include "mlir/IR/Region.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/StringRef.h"
26
27#include <cstdint>
28#include <functional>
29#include <string>
30
31namespace mlir {
32namespace acc {
33
34/// IDs for OpenACC compiler-to-runtime entry points (`__tgt_acc_*`).
35enum class RuntimeFunction {
36#define ACC_RTL(Enum, ...) Enum,
37#include "mlir/Dialect/OpenACC/OpenACCRuntimeFunctions.def"
38};
39
40/// Returns the default runtime symbol name for \p fn.
42
43/// Builds the LLVM function type for \p fn in \p ctx.
44LLVM::LLVMFunctionType getRuntimeFunctionType(MLIRContext *ctx,
46
47/// Status returned by the runtime entry points that report whether they
48/// completed. The values are part of the runtime ABI.
49enum class RuntimeResult : int32_t {
51 Error = 1,
52};
53
54/// IDs for the argument descriptors of the OpenACC data entry points, declared
55/// in OpenACCRuntimeDescriptors.def.
56enum class DataDescriptor {
57#define ACC_DESC_BEGIN(Enum, ...) Enum,
58#include "mlir/Dialect/OpenACC/OpenACCRuntimeDescriptors.def"
59};
60
61/// Field indices of each descriptor, in declaration order.
62#define ACC_DESC_BEGIN(Enum, ...) enum class Enum##Field : int64_t {
63#define ACC_DESC_FIELD(Enum, Field, TypeToken) Field,
64#define ACC_DESC_END(Enum) \
65 } \
66 ;
67#include "mlir/Dialect/OpenACC/OpenACCRuntimeDescriptors.def"
68
69/// The index an insert or extract of \p field addresses.
70template <typename FieldEnum>
72 return static_cast<int64_t>(field);
73}
74
75/// Fields of the argument struct passed to `__tgt_acc_kernel`, in declaration
76/// order. Declared in OpenACCRuntimeKernelArgs.def.
78#define ACC_KERNEL_ARG_FIELD(Field, TypeToken) Field,
79#include "mlir/Dialect/OpenACC/OpenACCRuntimeKernelArgs.def"
80};
81
82/// The index an insert of \p field addresses.
84 return static_cast<int64_t>(field);
85}
86
87/// The LLVM type of the argument struct passed to `__tgt_acc_kernel`.
88LLVM::LLVMStructType getKernelArgumentType(MLIRContext *ctx);
89
90/// Returns the name of the runtime type \p desc materializes.
92
93/// Returns the descriptor kind the runtime reads from the version field of
94/// \p desc. An overlay contributes its kind to the descriptor it nests.
95DataDescKind getDataDescriptorKind(DataDescriptor desc);
96
97/// Builds the LLVM type of \p desc in \p ctx. A descriptor that nests another
98/// one takes it as \p baseType, which is ignored otherwise.
99LLVM::LLVMStructType getDataDescriptorType(MLIRContext *ctx,
100 DataDescriptor desc,
101 Type baseType = {});
102
103/// Configuration for OpenACC to LLVM runtime lowering. Device types and map
104/// flags use the OpenACC dialect encodings by default.
106public:
107 using FunctionDisplayNameFn = std::function<std::string(StringRef)>;
108
110
111 void setName(RuntimeFunction fn, StringRef name);
112 StringRef getName(RuntimeFunction fn) const;
113
115 std::string getFunctionDisplayName(StringRef mangledOrSymbol) const;
116
117 /// Map an OpenACC dialect \p DeviceType to the integer encoding expected by
118 /// the target runtime. Dialect ordinals and runtime ABI values are not
119 /// required to match. A target whose ABI differs from the default dialect
120 /// encoding must install its mapping here. Querying an unmapped type is an
121 /// error.
122 void setDeviceTypeRuntimeValue(DeviceType type, int64_t runtimeValue);
123 int64_t getDeviceTypeRuntimeValue(DeviceType type) const;
124
125 /// Map a single OpenACC dialect \p MapFlags bit to the bit the target runtime
126 /// gives the same meaning. getMapFlagsRuntimeValue combines the bits of a set
127 /// of flags into the encoding the runtime reads from an argument-type slot.
128 /// As with device types, dialect and runtime encodings are not required to
129 /// match. A target whose ABI differs from the default dialect encoding must
130 /// install its mapping here, and a set bit with no mapping is an error.
131 void setMapFlagRuntimeValue(MapFlags flag, int64_t runtimeValue);
132 int64_t getMapFlagsRuntimeValue(MapFlags flags) const;
133
134 /// Adjust the flags of a mapping before they are encoded. The hook is keyed
135 /// on the operation that states the mapping and is applied to every mapped
136 /// object before anything is derived from the flags. The flags are used as
137 /// computed when no hook is installed.
139 std::function<MapFlags(Operation *mapOp, MapFlags flags)>;
141 MapFlags postProcessMapFlags(Operation *mapOp, MapFlags flags) const;
142
143 /// Renders \p flags for a diagnostic as the names of the set bits and the
144 /// decimal and hexadecimal encoding the runtime reads.
145 std::string formatMapFlags(MapFlags flags) const;
146
147 /// Runtime encoding of `acc_async_sync`, used when an operation carries no
148 /// `async` clause. OpenACC defines the name of this queue but leaves its
149 /// value to the implementation, so it is part of the runtime ABI.
150 void setAsyncSyncRuntimeValue(int64_t runtimeValue);
152
153 /// Runtime encoding of `acc_async_noval`, used for an `async` clause without
154 /// an argument. As with `acc_async_sync`, the value is implementation-defined
155 void setAsyncNoValueRuntimeValue(int64_t runtimeValue);
157
158 /// Materialize the target-specific binary descriptor passed to
159 /// `__tgt_acc_declare`. The default is a null pointer.
163
164 /// Materialize the target-specific binary descriptor passed to
165 /// `__tgt_acc_kernel`. The default is a null pointer.
169
170private:
172 DenseMap<DeviceType, int64_t> deviceTypeRuntimeValues;
173 DenseMap<MapFlags, int64_t> mapFlagRuntimeValues;
174 FunctionDisplayNameFn functionDisplayNameFn;
175 MapFlagsPostProcessFn mapFlagsPostProcessFn;
176 DeclareBinaryDescriptorFn declareBinaryDescriptorFn;
177 KernelBinaryDescriptorFn kernelBinaryDescriptorFn;
178 // Default to the encodings used by openacc.h (`acc_async_sync` /
179 // `acc_async_noval`).
180 int64_t asyncSyncRuntimeValue = -1;
181 int64_t asyncNoValueRuntimeValue = -4;
182};
183
184/// Install a device-type mapping that uses OpenACC dialect enum ordinals as the
185/// runtime encoding. This is only correct when the target runtime happens to
186/// use the same numbering; runtimes with a different ABI must install their
187/// own mapping via \c setDeviceTypeRuntimeValue.
189
190/// Install a map-flag mapping that uses the OpenACC dialect bit positions as
191/// the runtime encoding, with the same caveat as
192/// \c populateDialectIdentityDeviceTypeMapping.
194
195/// Declares (if needed) and returns a call to the runtime function identified
196/// by \p fn using the name from \p config. Fails and emits a diagnostic if the
197/// symbol is already declared with a signature the runtime cannot be called
198/// through. The declaration is created in \p globalSymbolRegion and
199/// registered in \p symbolTable.
200FailureOr<LLVM::CallOp> createRuntimeCall(Location loc, OpBuilder &builder,
201 Region &globalSymbolRegion,
202 SymbolTable &symbolTable,
204 const ACCRuntimeCallConfig &config,
205 ArrayRef<Value> arguments);
206
207} // namespace acc
208} // namespace mlir
209
210#endif // MLIR_DIALECT_OPENACC_OPENACCRUNTIMEUTILS_H
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
This class helps build Operations.
Definition Builders.h:210
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:25
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Configuration for OpenACC to LLVM runtime lowering.
StringRef getName(RuntimeFunction fn) const
std::string formatMapFlags(MapFlags flags) const
Renders flags for a diagnostic as the names of the set bits and the decimal and hexadecimal encoding ...
std::string getFunctionDisplayName(StringRef mangledOrSymbol) const
std::function< Value(Location, OpBuilder &)> KernelBinaryDescriptorFn
Materialize the target-specific binary descriptor passed to __tgt_acc_kernel.
void setKernelBinaryDescriptorFn(KernelBinaryDescriptorFn fn)
void setName(RuntimeFunction fn, StringRef name)
Value createDeclareBinaryDescriptor(Location loc, OpBuilder &builder) const
void setAsyncSyncRuntimeValue(int64_t runtimeValue)
Runtime encoding of acc_async_sync, used when an operation carries no async clause.
void setDeviceTypeRuntimeValue(DeviceType type, int64_t runtimeValue)
Map an OpenACC dialect DeviceType to the integer encoding expected by the target runtime.
void setMapFlagRuntimeValue(MapFlags flag, int64_t runtimeValue)
Map a single OpenACC dialect MapFlags bit to the bit the target runtime gives the same meaning.
int64_t getDeviceTypeRuntimeValue(DeviceType type) const
std::function< MapFlags(Operation *mapOp, MapFlags flags)> MapFlagsPostProcessFn
Adjust the flags of a mapping before they are encoded.
void setFunctionDisplayNameFn(FunctionDisplayNameFn fn)
std::function< std::string(StringRef)> FunctionDisplayNameFn
int64_t getMapFlagsRuntimeValue(MapFlags flags) const
std::function< Value(Location, OpBuilder &)> DeclareBinaryDescriptorFn
Materialize the target-specific binary descriptor passed to __tgt_acc_declare.
MapFlags postProcessMapFlags(Operation *mapOp, MapFlags flags) const
void setMapFlagsPostProcessFn(MapFlagsPostProcessFn fn)
Value createKernelBinaryDescriptor(Location loc, OpBuilder &builder) const
void setDeclareBinaryDescriptorFn(DeclareBinaryDescriptorFn fn)
void setAsyncNoValueRuntimeValue(int64_t runtimeValue)
Runtime encoding of acc_async_noval, used for an async clause without an argument.
LLVM::LLVMFunctionType getRuntimeFunctionType(MLIRContext *ctx, RuntimeFunction fn)
Builds the LLVM function type for fn in ctx.
void populateDialectIdentityDeviceTypeMapping(ACCRuntimeCallConfig &config)
Install a device-type mapping that uses OpenACC dialect enum ordinals as the runtime encoding.
StringRef getRuntimeFunctionName(RuntimeFunction fn)
Returns the default runtime symbol name for fn.
int64_t getKernelArgumentFieldIndex(KernelArgumentField field)
The index an insert of field addresses.
LLVM::LLVMStructType getDataDescriptorType(MLIRContext *ctx, DataDescriptor desc, Type baseType={})
Builds the LLVM type of desc in ctx.
DataDescKind getDataDescriptorKind(DataDescriptor desc)
Returns the descriptor kind the runtime reads from the version field of desc.
LLVM::LLVMStructType getKernelArgumentType(MLIRContext *ctx)
The LLVM type of the argument struct passed to __tgt_acc_kernel.
StringRef getDataDescriptorName(DataDescriptor desc)
Returns the name of the runtime type desc materializes.
DataDescriptor
IDs for the argument descriptors of the OpenACC data entry points, declared in OpenACCRuntimeDescript...
RuntimeResult
Status returned by the runtime entry points that report whether they completed.
RuntimeFunction
IDs for OpenACC compiler-to-runtime entry points (__tgt_acc_*).
void populateDialectIdentityMapFlagsMapping(ACCRuntimeCallConfig &config)
Install a map-flag mapping that uses the OpenACC dialect bit positions as the runtime encoding,...
int64_t getDataDescriptorFieldIndex(FieldEnum field)
The index an insert or extract of field addresses.
KernelArgumentField
Fields of the argument struct passed to __tgt_acc_kernel, in declaration order.
FailureOr< LLVM::CallOp > createRuntimeCall(Location loc, OpBuilder &builder, Region &globalSymbolRegion, SymbolTable &symbolTable, RuntimeFunction fn, const ACCRuntimeCallConfig &config, ArrayRef< Value > arguments)
Declares (if needed) and returns a call to the runtime function identified by fn using the name from ...
Include the generated interface declarations.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123