MLIR 24.0.0git
OpenACCRuntimeUtils.cpp
Go to the documentation of this file.
1//===- OpenACCRuntimeUtils.cpp - 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
10
14#include "mlir/IR/SymbolTable.h"
15#include "llvm/Support/ErrorHandling.h"
16
17#include <cassert>
18#include <optional>
19
20using namespace mlir;
21using namespace mlir::acc;
22
27
29 switch (fn) {
30#define ACC_RTL(Enum, Str, ...) \
31 case RuntimeFunction::Enum: \
32 return Str;
33#include "mlir/Dialect/OpenACC/OpenACCRuntimeFunctions.def"
34 }
35 llvm_unreachable("unknown ACC runtime function");
36}
37
38LLVM::LLVMFunctionType acc::getRuntimeFunctionType(MLIRContext *ctx,
39 RuntimeFunction fn) {
40 Type Void = LLVM::LLVMVoidType::get(ctx);
41 Type Ptr = LLVM::LLVMPointerType::get(ctx);
42 Type Int32 = IntegerType::get(ctx, 32);
43 Type Int64 = IntegerType::get(ctx, 64);
44
45 switch (fn) {
46#define ACC_RTL(Enum, Str, IsVarArg, ReturnType, ...) \
47 case RuntimeFunction::Enum: \
48 return LLVM::LLVMFunctionType::get(ReturnType, \
49 ArrayRef<Type>{__VA_ARGS__}, IsVarArg);
50#include "mlir/Dialect/OpenACC/OpenACCRuntimeFunctions.def"
51 }
52 llvm_unreachable("unknown ACC runtime function");
53}
54
56 switch (desc) {
57#define ACC_DESC_BEGIN(Enum, NameStr, DescKind) \
58 case DataDescriptor::Enum: \
59 return NameStr;
60#include "mlir/Dialect/OpenACC/OpenACCRuntimeDescriptors.def"
61 }
62 llvm_unreachable("unknown ACC data descriptor");
63}
64
66 switch (desc) {
67#define ACC_DESC_BEGIN(Enum, NameStr, DescKind) \
68 case DataDescriptor::Enum: \
69 return DescKind;
70#include "mlir/Dialect/OpenACC/OpenACCRuntimeDescriptors.def"
71 }
72 llvm_unreachable("unknown ACC data descriptor");
73}
74
75LLVM::LLVMStructType acc::getDataDescriptorType(MLIRContext *ctx,
76 DataDescriptor desc,
77 Type baseType) {
78 Type Int8 = IntegerType::get(ctx, 8);
79 Type Int32 = IntegerType::get(ctx, 32);
80 Type Int64 = IntegerType::get(ctx, 64);
81 Type Ptr = LLVM::LLVMPointerType::get(ctx);
82 Type Base = baseType;
83
84 switch (desc) {
85#define ACC_DESC_BEGIN(Enum, NameStr, DescKind) \
86 case DataDescriptor::Enum: { \
87 SmallVector<Type> fields;
88#define ACC_DESC_FIELD(Enum, Field, TypeToken) \
89 assert(TypeToken && "no type for descriptor field " #Field); \
90 fields.push_back(TypeToken);
91#define ACC_DESC_END(Enum) \
92 return LLVM::LLVMStructType::getLiteral(ctx, fields); \
93 }
94#include "mlir/Dialect/OpenACC/OpenACCRuntimeDescriptors.def"
95 }
96 llvm_unreachable("unknown ACC data descriptor");
97}
98
99LLVM::LLVMStructType acc::getKernelArgumentType(MLIRContext *ctx) {
100 Type Int32 = IntegerType::get(ctx, 32);
101 Type Int64 = IntegerType::get(ctx, 64);
102 Type Ptr = LLVM::LLVMPointerType::get(ctx);
103 Type Int64x3 = LLVM::LLVMArrayType::get(Int64, 3);
104 SmallVector<Type> fields;
105#define ACC_KERNEL_ARG_FIELD(Field, TypeToken) fields.push_back(TypeToken);
106#include "mlir/Dialect/OpenACC/OpenACCRuntimeKernelArgs.def"
107 return LLVM::LLVMStructType::getLiteral(ctx, fields);
108}
109
111 overrides[fn] = name.str();
112}
113
115 if (auto it = overrides.find(fn); it != overrides.end())
116 return it->second;
117 return getRuntimeFunctionName(fn);
118}
119
121 functionDisplayNameFn = std::move(fn);
122}
123
124std::string
125ACCRuntimeCallConfig::getFunctionDisplayName(StringRef mangledOrSymbol) const {
126 if (functionDisplayNameFn)
127 return functionDisplayNameFn(mangledOrSymbol);
128 return mangledOrSymbol.str();
129}
130
132 int64_t runtimeValue) {
133 deviceTypeRuntimeValues[type] = runtimeValue;
134}
135
137 if (auto it = deviceTypeRuntimeValues.find(type);
138 it != deviceTypeRuntimeValues.end())
139 return it->second;
140 llvm::report_fatal_error(
141 llvm::Twine("missing OpenACC runtime device-type mapping for ") +
142 stringifyDeviceType(type));
143}
144
146 int64_t runtimeValue) {
147 mapFlagRuntimeValues[flag] = runtimeValue;
148}
149
151 int64_t runtimeValue = 0;
152 for (unsigned bit = 0; bit != 32; ++bit) {
153 auto flag = static_cast<MapFlags>(1u << bit);
154 if (!bitEnumContainsAny(flags, flag))
155 continue;
156 auto it = mapFlagRuntimeValues.find(flag);
157 if (it == mapFlagRuntimeValues.end())
158 llvm::report_fatal_error(
159 llvm::Twine("missing OpenACC runtime map-flag mapping for ") +
160 stringifyMapFlags(flag));
161 runtimeValue |= it->second;
162 }
163 return runtimeValue;
164}
165
167 mapFlagsPostProcessFn = std::move(fn);
168}
169
171 MapFlags flags) const {
172 if (mapFlagsPostProcessFn)
173 return mapFlagsPostProcessFn(mapOp, flags);
174 return flags;
175}
176
177std::string ACCRuntimeCallConfig::formatMapFlags(MapFlags flags) const {
178 int64_t runtimeValue = getMapFlagsRuntimeValue(flags);
179 std::string rendered = stringifyMapFlags(flags);
180 rendered += " (";
181 rendered += std::to_string(runtimeValue);
182 rendered += " / 0x";
183 rendered += llvm::Twine::utohexstr(runtimeValue).str();
184 rendered += ")";
185 return rendered;
186}
187
189 asyncSyncRuntimeValue = runtimeValue;
190}
191
193 return asyncSyncRuntimeValue;
194}
195
197 asyncNoValueRuntimeValue = runtimeValue;
198}
199
201 return asyncNoValueRuntimeValue;
202}
203
206 declareBinaryDescriptorFn = std::move(fn);
207}
208
210 Location loc, OpBuilder &builder) const {
211 if (declareBinaryDescriptorFn)
212 return declareBinaryDescriptorFn(loc, builder);
213 return LLVM::ZeroOp::create(builder, loc,
214 LLVM::LLVMPointerType::get(builder.getContext()));
215}
216
219 kernelBinaryDescriptorFn = std::move(fn);
220}
221
223 Location loc, OpBuilder &builder) const {
224 if (kernelBinaryDescriptorFn)
225 return kernelBinaryDescriptorFn(loc, builder);
226 return LLVM::ZeroOp::create(builder, loc,
227 LLVM::LLVMPointerType::get(builder.getContext()));
228}
229
231 ACCRuntimeCallConfig &config) {
232 for (uint32_t value = 0; value <= getMaxEnumValForDeviceType(); ++value)
233 if (std::optional<DeviceType> type = symbolizeDeviceType(value))
234 config.setDeviceTypeRuntimeValue(*type, value);
235}
236
238 for (unsigned bit = 0; bit != 32; ++bit) {
239 uint32_t value = 1u << bit;
240 if (std::optional<MapFlags> flag = symbolizeMapFlags(value))
241 config.setMapFlagRuntimeValue(*flag, value);
242 }
243}
244
245FailureOr<LLVM::CallOp>
247 Region &globalSymbolRegion, SymbolTable &symbolTable,
248 RuntimeFunction fn, const ACCRuntimeCallConfig &config,
249 ArrayRef<Value> arguments) {
250 MLIRContext *ctx = builder.getContext();
251 LLVM::LLVMFunctionType fnTy = getRuntimeFunctionType(ctx, fn);
252 StringRef symbolName = config.getName(fn);
253
254 auto func = symbolTable.lookup<LLVM::LLVMFuncOp>(symbolName);
255 if (func) {
256 // An existing declaration with a different signature cannot be called with
257 // the arguments expected by the runtime entry point.
258 if (func.getFunctionType() != fnTy)
259 return emitError(loc) << "OpenACC runtime function '" << symbolName
260 << "' is already declared with signature "
261 << func.getFunctionType() << ", expected " << fnTy;
262 } else {
263 OpBuilder moduleBuilder =
264 OpBuilder::atBlockEnd(&globalSymbolRegion.front());
265 func = LLVM::LLVMFuncOp::create(moduleBuilder, loc, symbolName, fnTy);
266 symbolTable.insert(func);
267 }
268
269 return LLVM::CallOp::create(builder, loc, func, arguments);
270}
MLIRContext * getContext() const
Definition Builders.h:56
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:71
This class helps build Operations.
Definition Builders.h:210
static OpBuilder atBlockEnd(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to after the last operation in the block but still insid...
Definition Builders.h:249
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
Block & front()
Definition Region.h:65
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:25
Operation * lookup(StringRef name) const
Look up a symbol with the specified name, returning null if no such name exists.
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
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.
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...
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,...
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.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.