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
9#include "mlir/Dialect/OpenACC/OpenACCRuntimeUtils.h"
10
11#include "mlir/Dialect/LLVMIR/LLVMCallOps.h"
12#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
13#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
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
23ACCRuntimeCallConfig::ACCRuntimeCallConfig() {
24 populateDialectIdentityDeviceTypeMapping(config&: *this);
25 populateDialectIdentityMapFlagsMapping(config&: *this);
26}
27
28StringRef acc::getRuntimeFunctionName(RuntimeFunction fn) {
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(context: ctx);
42 Type Int32 = IntegerType::get(context: ctx, width: 32);
43 Type Int64 = IntegerType::get(context: ctx, width: 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
55StringRef acc::getDataDescriptorName(DataDescriptor desc) {
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
65DataDescKind acc::getDataDescriptorKind(DataDescriptor desc) {
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(context: ctx, width: 8);
79 Type Int32 = IntegerType::get(context: ctx, width: 32);
80 Type Int64 = IntegerType::get(context: ctx, width: 64);
81 Type Ptr = LLVM::LLVMPointerType::get(context: 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(context: ctx, width: 32);
101 Type Int64 = IntegerType::get(context: ctx, width: 64);
102 Type Ptr = LLVM::LLVMPointerType::get(context: ctx);
103 Type Int64x3 = LLVM::LLVMArrayType::get(elementType: Int64, numElements: 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(context: ctx, types: fields);
108}
109
110void ACCRuntimeCallConfig::setName(RuntimeFunction fn, StringRef name) {
111 overrides[fn] = name.str();
112}
113
114StringRef ACCRuntimeCallConfig::getName(RuntimeFunction fn) const {
115 if (auto it = overrides.find(Val: fn); it != overrides.end())
116 return it->second;
117 return getRuntimeFunctionName(fn);
118}
119
120void ACCRuntimeCallConfig::setFunctionDisplayNameFn(FunctionDisplayNameFn fn) {
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
131void ACCRuntimeCallConfig::setDeviceTypeRuntimeValue(DeviceType type,
132 int64_t runtimeValue) {
133 deviceTypeRuntimeValues[type] = runtimeValue;
134}
135
136int64_t ACCRuntimeCallConfig::getDeviceTypeRuntimeValue(DeviceType type) const {
137 if (auto it = deviceTypeRuntimeValues.find(Val: type);
138 it != deviceTypeRuntimeValues.end())
139 return it->second;
140 llvm::report_fatal_error(
141 reason: llvm::Twine("missing OpenACC runtime device-type mapping for ") +
142 stringifyDeviceType(type));
143}
144
145void ACCRuntimeCallConfig::setMapFlagRuntimeValue(MapFlags flag,
146 int64_t runtimeValue) {
147 mapFlagRuntimeValues[flag] = runtimeValue;
148}
149
150int64_t ACCRuntimeCallConfig::getMapFlagsRuntimeValue(MapFlags flags) const {
151 int64_t runtimeValue = 0;
152 for (unsigned bit = 0; bit != 32; ++bit) {
153 auto flag = static_cast<MapFlags>(1u << bit);
154 if (!bitEnumContainsAny(bits: flags, bit: flag))
155 continue;
156 auto it = mapFlagRuntimeValues.find(Val: flag);
157 if (it == mapFlagRuntimeValues.end())
158 llvm::report_fatal_error(
159 reason: llvm::Twine("missing OpenACC runtime map-flag mapping for ") +
160 stringifyMapFlags(flag));
161 runtimeValue |= it->second;
162 }
163 return runtimeValue;
164}
165
166void ACCRuntimeCallConfig::setMapFlagsPostProcessFn(MapFlagsPostProcessFn fn) {
167 mapFlagsPostProcessFn = std::move(fn);
168}
169
170MapFlags ACCRuntimeCallConfig::postProcessMapFlags(Operation *mapOp,
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(val: runtimeValue);
182 rendered += " / 0x";
183 rendered += llvm::Twine::utohexstr(Val: runtimeValue).str();
184 rendered += ")";
185 return rendered;
186}
187
188void ACCRuntimeCallConfig::setAsyncSyncRuntimeValue(int64_t runtimeValue) {
189 asyncSyncRuntimeValue = runtimeValue;
190}
191
192int64_t ACCRuntimeCallConfig::getAsyncSyncRuntimeValue() const {
193 return asyncSyncRuntimeValue;
194}
195
196void ACCRuntimeCallConfig::setAsyncNoValueRuntimeValue(int64_t runtimeValue) {
197 asyncNoValueRuntimeValue = runtimeValue;
198}
199
200int64_t ACCRuntimeCallConfig::getAsyncNoValueRuntimeValue() const {
201 return asyncNoValueRuntimeValue;
202}
203
204void ACCRuntimeCallConfig::setDeclareBinaryDescriptorFn(
205 DeclareBinaryDescriptorFn fn) {
206 declareBinaryDescriptorFn = std::move(fn);
207}
208
209Value ACCRuntimeCallConfig::createDeclareBinaryDescriptor(
210 Location loc, OpBuilder &builder) const {
211 if (declareBinaryDescriptorFn)
212 return declareBinaryDescriptorFn(loc, builder);
213 return LLVM::ZeroOp::create(builder, location: loc,
214 res: LLVM::LLVMPointerType::get(context: builder.getContext()));
215}
216
217void ACCRuntimeCallConfig::setKernelBinaryDescriptorFn(
218 KernelBinaryDescriptorFn fn) {
219 kernelBinaryDescriptorFn = std::move(fn);
220}
221
222Value ACCRuntimeCallConfig::createKernelBinaryDescriptor(
223 Location loc, OpBuilder &builder) const {
224 if (kernelBinaryDescriptorFn)
225 return kernelBinaryDescriptorFn(loc, builder);
226 return LLVM::ZeroOp::create(builder, location: loc,
227 res: LLVM::LLVMPointerType::get(context: builder.getContext()));
228}
229
230void acc::populateDialectIdentityDeviceTypeMapping(
231 ACCRuntimeCallConfig &config) {
232 for (uint32_t value = 0; value <= getMaxEnumValForDeviceType(); ++value)
233 if (std::optional<DeviceType> type = symbolizeDeviceType(value))
234 config.setDeviceTypeRuntimeValue(type: *type, runtimeValue: value);
235}
236
237void acc::populateDialectIdentityMapFlagsMapping(ACCRuntimeCallConfig &config) {
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: *flag, runtimeValue: value);
242 }
243}
244
245FailureOr<LLVM::CallOp>
246acc::createRuntimeCall(Location loc, OpBuilder &builder,
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>(name: 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(block: &globalSymbolRegion.front());
265 func = LLVM::LLVMFuncOp::create(builder&: moduleBuilder, location: loc, name: symbolName, type: fnTy);
266 symbolTable.insert(symbol: func);
267 }
268
269 return LLVM::CallOp::create(builder, location: loc, func, args: arguments);
270}
271