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
11#include "mlir/IR/SymbolTable.h"
12#include "llvm/Support/ErrorHandling.h"
13
14#include <optional>
15
16using namespace mlir;
17using namespace mlir::acc;
18
20 switch (fn) {
21#define ACC_RTL(Enum, Str, ...) \
22 case RuntimeFunction::Enum: \
23 return Str;
24#include "mlir/Dialect/OpenACC/OpenACCRuntimeFunctions.def"
25 }
26 llvm_unreachable("unknown ACC runtime function");
27}
28
29LLVM::LLVMFunctionType acc::getRuntimeFunctionType(MLIRContext *ctx,
30 RuntimeFunction fn) {
31 Type Void = LLVM::LLVMVoidType::get(ctx);
32 Type Ptr = LLVM::LLVMPointerType::get(ctx);
33 Type Int32 = IntegerType::get(ctx, 32);
34 Type Int64 = IntegerType::get(ctx, 64);
35
36 switch (fn) {
37#define ACC_RTL(Enum, Str, IsVarArg, ReturnType, ...) \
38 case RuntimeFunction::Enum: \
39 return LLVM::LLVMFunctionType::get(ReturnType, \
40 ArrayRef<Type>{__VA_ARGS__}, IsVarArg);
41#include "mlir/Dialect/OpenACC/OpenACCRuntimeFunctions.def"
42 }
43 llvm_unreachable("unknown ACC runtime function");
44}
45
47 overrides[fn] = name.str();
48}
49
51 if (auto it = overrides.find(fn); it != overrides.end())
52 return it->second;
53 return getRuntimeFunctionName(fn);
54}
55
57 functionDisplayNameFn = std::move(fn);
58}
59
60std::string
61ACCRuntimeCallConfig::getFunctionDisplayName(StringRef mangledOrSymbol) const {
62 if (functionDisplayNameFn)
63 return functionDisplayNameFn(mangledOrSymbol);
64 return mangledOrSymbol.str();
65}
66
68 int64_t runtimeValue) {
69 deviceTypeRuntimeValues[type] = runtimeValue;
70}
71
73 if (auto it = deviceTypeRuntimeValues.find(type);
74 it != deviceTypeRuntimeValues.end())
75 return it->second;
76 llvm::report_fatal_error(
77 llvm::Twine("missing OpenACC runtime device-type mapping for ") +
78 stringifyDeviceType(type));
79}
80
82 asyncSyncRuntimeValue = runtimeValue;
83}
84
86 return asyncSyncRuntimeValue;
87}
88
90 asyncNoValueRuntimeValue = runtimeValue;
91}
92
94 return asyncNoValueRuntimeValue;
95}
96
98 ACCRuntimeCallConfig &config) {
99 for (uint32_t value = 0; value <= getMaxEnumValForDeviceType(); ++value)
100 if (std::optional<DeviceType> type = symbolizeDeviceType(value))
101 config.setDeviceTypeRuntimeValue(*type, value);
102}
103
104FailureOr<LLVM::CallOp>
105acc::createRuntimeCall(Location loc, OpBuilder &builder, ModuleOp module,
106 RuntimeFunction fn, const ACCRuntimeCallConfig &config,
107 ArrayRef<Value> arguments) {
108 MLIRContext *ctx = builder.getContext();
109 LLVM::LLVMFunctionType fnTy = getRuntimeFunctionType(ctx, fn);
110 StringRef symbolName = config.getName(fn);
111
112 SymbolTable symbolTable(module);
113 auto func = symbolTable.lookup<LLVM::LLVMFuncOp>(symbolName);
114 if (func) {
115 // An existing declaration with a different signature cannot be called with
116 // the arguments expected by the runtime entry point.
117 if (func.getFunctionType() != fnTy)
118 return emitError(loc) << "OpenACC runtime function '" << symbolName
119 << "' is already declared with signature "
120 << func.getFunctionType() << ", expected " << fnTy;
121 } else {
122 OpBuilder moduleBuilder = OpBuilder::atBlockEnd(module.getBody());
123 func = LLVM::LLVMFuncOp::create(moduleBuilder, loc, symbolName, fnTy);
124 }
125
126 return LLVM::CallOp::create(builder, loc, func, arguments);
127}
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:63
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
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:24
Operation * lookup(StringRef name) const
Look up a symbol with the specified name, returning null if no such name exists.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Optional overrides for OpenACC to LLVM runtime lowering.
StringRef getName(RuntimeFunction fn) const
std::string getFunctionDisplayName(StringRef mangledOrSymbol) const
void setName(RuntimeFunction fn, StringRef name)
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.
int64_t getDeviceTypeRuntimeValue(DeviceType type) const
void setFunctionDisplayNameFn(FunctionDisplayNameFn fn)
std::function< std::string(StringRef)> FunctionDisplayNameFn
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.
FailureOr< LLVM::CallOp > createRuntimeCall(Location loc, OpBuilder &builder, ModuleOp module, 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 ...
RuntimeFunction
IDs for OpenACC compiler-to-runtime entry points (__tgt_acc_*).
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.