2020-10-23 03:20:42 +08:00
|
|
|
//===- AsyncToLLVM.cpp - Convert Async to LLVM dialect --------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/Conversion/AsyncToLLVM/AsyncToLLVM.h"
|
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
2021-07-09 00:35:18 +08:00
|
|
|
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
|
|
|
|
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
|
2020-12-24 21:08:09 +08:00
|
|
|
#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h"
|
2021-10-13 07:14:57 +08:00
|
|
|
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
|
2020-10-23 03:20:42 +08:00
|
|
|
#include "mlir/Dialect/Async/IR/Async.h"
|
|
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
2020-12-24 21:08:09 +08:00
|
|
|
#include "mlir/Dialect/StandardOps/Transforms/FuncConversions.h"
|
2020-12-23 02:35:15 +08:00
|
|
|
#include "mlir/IR/ImplicitLocOpBuilder.h"
|
2020-10-23 03:20:42 +08:00
|
|
|
#include "mlir/IR/TypeUtilities.h"
|
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
2021-05-27 04:33:38 +08:00
|
|
|
#include "llvm/ADT/TypeSwitch.h"
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "convert-async-to-llvm"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::async;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Async Runtime C API declaration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-11-20 18:42:28 +08:00
|
|
|
static constexpr const char *kAddRef = "mlirAsyncRuntimeAddRef";
|
|
|
|
static constexpr const char *kDropRef = "mlirAsyncRuntimeDropRef";
|
2020-10-23 03:20:42 +08:00
|
|
|
static constexpr const char *kCreateToken = "mlirAsyncRuntimeCreateToken";
|
2020-12-24 21:08:09 +08:00
|
|
|
static constexpr const char *kCreateValue = "mlirAsyncRuntimeCreateValue";
|
2020-11-13 19:01:52 +08:00
|
|
|
static constexpr const char *kCreateGroup = "mlirAsyncRuntimeCreateGroup";
|
2020-10-23 03:20:42 +08:00
|
|
|
static constexpr const char *kEmplaceToken = "mlirAsyncRuntimeEmplaceToken";
|
2020-12-24 21:08:09 +08:00
|
|
|
static constexpr const char *kEmplaceValue = "mlirAsyncRuntimeEmplaceValue";
|
2021-05-26 06:06:34 +08:00
|
|
|
static constexpr const char *kSetTokenError = "mlirAsyncRuntimeSetTokenError";
|
|
|
|
static constexpr const char *kSetValueError = "mlirAsyncRuntimeSetValueError";
|
|
|
|
static constexpr const char *kIsTokenError = "mlirAsyncRuntimeIsTokenError";
|
|
|
|
static constexpr const char *kIsValueError = "mlirAsyncRuntimeIsValueError";
|
2021-05-27 04:33:38 +08:00
|
|
|
static constexpr const char *kIsGroupError = "mlirAsyncRuntimeIsGroupError";
|
2020-10-23 03:20:42 +08:00
|
|
|
static constexpr const char *kAwaitToken = "mlirAsyncRuntimeAwaitToken";
|
2020-12-24 21:08:09 +08:00
|
|
|
static constexpr const char *kAwaitValue = "mlirAsyncRuntimeAwaitValue";
|
2020-11-13 19:01:52 +08:00
|
|
|
static constexpr const char *kAwaitGroup = "mlirAsyncRuntimeAwaitAllInGroup";
|
2020-10-23 03:20:42 +08:00
|
|
|
static constexpr const char *kExecute = "mlirAsyncRuntimeExecute";
|
2020-12-24 21:08:09 +08:00
|
|
|
static constexpr const char *kGetValueStorage =
|
|
|
|
"mlirAsyncRuntimeGetValueStorage";
|
2020-11-13 19:01:52 +08:00
|
|
|
static constexpr const char *kAddTokenToGroup =
|
|
|
|
"mlirAsyncRuntimeAddTokenToGroup";
|
2020-12-24 21:08:09 +08:00
|
|
|
static constexpr const char *kAwaitTokenAndExecute =
|
2020-10-23 03:20:42 +08:00
|
|
|
"mlirAsyncRuntimeAwaitTokenAndExecute";
|
2020-12-24 21:08:09 +08:00
|
|
|
static constexpr const char *kAwaitValueAndExecute =
|
|
|
|
"mlirAsyncRuntimeAwaitValueAndExecute";
|
2020-11-13 19:01:52 +08:00
|
|
|
static constexpr const char *kAwaitAllAndExecute =
|
|
|
|
"mlirAsyncRuntimeAwaitAllInGroupAndExecute";
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
namespace {
|
2020-12-24 21:08:09 +08:00
|
|
|
/// Async Runtime API function types.
|
|
|
|
///
|
|
|
|
/// Because we can't create API function signature for type parametrized
|
|
|
|
/// async.value type, we use opaque pointers (!llvm.ptr<i8>) instead. After
|
|
|
|
/// lowering all async data types become opaque pointers at runtime.
|
2020-10-23 03:20:42 +08:00
|
|
|
struct AsyncAPI {
|
2020-12-24 21:08:09 +08:00
|
|
|
// All async types are lowered to opaque i8* LLVM pointers at runtime.
|
|
|
|
static LLVM::LLVMPointerType opaquePointerType(MLIRContext *ctx) {
|
[mlir] replace LLVMIntegerType with built-in integer type
The LLVM dialect type system has been closed until now, i.e. did not support
types from other dialects inside containers. While this has had obvious
benefits of deriving from a common base class, it has led to some simple types
being almost identical with the built-in types, namely integer and floating
point types. This in turn has led to a lot of larger-scale complexity: simple
types must still be converted, numerous operations that correspond to LLVM IR
intrinsics are replicated to produce versions operating on either LLVM dialect
or built-in types leading to quasi-duplicate dialects, lowering to the LLVM
dialect is essentially required to be one-shot because of type conversion, etc.
In this light, it is reasonable to trade off some local complexity in the
internal implementation of LLVM dialect types for removing larger-scale system
complexity. Previous commits to the LLVM dialect type system have adapted the
API to support types from other dialects.
Replace LLVMIntegerType with the built-in IntegerType plus additional checks
that such types are signless (these are isolated in a utility function that
replaced `isa<LLVMType>` and in the parser). Temporarily keep the possibility
to parse `!llvm.i32` as a synonym for `i32`, but add a deprecation notice.
Reviewed By: mehdi_amini, silvas, antiagainst
Differential Revision: https://reviews.llvm.org/D94178
2021-01-06 23:19:04 +08:00
|
|
|
return LLVM::LLVMPointerType::get(IntegerType::get(ctx, 8));
|
2020-12-24 21:08:09 +08:00
|
|
|
}
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
static LLVM::LLVMTokenType tokenType(MLIRContext *ctx) {
|
|
|
|
return LLVM::LLVMTokenType::get(ctx);
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:42:28 +08:00
|
|
|
static FunctionType addOrDropRefFunctionType(MLIRContext *ctx) {
|
2020-12-24 21:08:09 +08:00
|
|
|
auto ref = opaquePointerType(ctx);
|
2021-09-27 22:06:54 +08:00
|
|
|
auto count = IntegerType::get(ctx, 64);
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {ref, count}, {});
|
2020-11-20 18:42:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
static FunctionType createTokenFunctionType(MLIRContext *ctx) {
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {}, {TokenType::get(ctx)});
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
static FunctionType createValueFunctionType(MLIRContext *ctx) {
|
2021-09-27 22:06:54 +08:00
|
|
|
auto i64 = IntegerType::get(ctx, 64);
|
2020-12-24 21:08:09 +08:00
|
|
|
auto value = opaquePointerType(ctx);
|
2021-09-27 22:06:54 +08:00
|
|
|
return FunctionType::get(ctx, {i64}, {value});
|
2020-12-24 21:08:09 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:52 +08:00
|
|
|
static FunctionType createGroupFunctionType(MLIRContext *ctx) {
|
2021-06-23 21:24:09 +08:00
|
|
|
auto i64 = IntegerType::get(ctx, 64);
|
|
|
|
return FunctionType::get(ctx, {i64}, {GroupType::get(ctx)});
|
2020-11-13 19:01:52 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
static FunctionType getValueStorageFunctionType(MLIRContext *ctx) {
|
|
|
|
auto value = opaquePointerType(ctx);
|
|
|
|
auto storage = opaquePointerType(ctx);
|
|
|
|
return FunctionType::get(ctx, {value}, {storage});
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
static FunctionType emplaceTokenFunctionType(MLIRContext *ctx) {
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {TokenType::get(ctx)}, {});
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
static FunctionType emplaceValueFunctionType(MLIRContext *ctx) {
|
|
|
|
auto value = opaquePointerType(ctx);
|
|
|
|
return FunctionType::get(ctx, {value}, {});
|
|
|
|
}
|
|
|
|
|
2021-05-26 06:06:34 +08:00
|
|
|
static FunctionType setTokenErrorFunctionType(MLIRContext *ctx) {
|
|
|
|
return FunctionType::get(ctx, {TokenType::get(ctx)}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
static FunctionType setValueErrorFunctionType(MLIRContext *ctx) {
|
|
|
|
auto value = opaquePointerType(ctx);
|
|
|
|
return FunctionType::get(ctx, {value}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
static FunctionType isTokenErrorFunctionType(MLIRContext *ctx) {
|
|
|
|
auto i1 = IntegerType::get(ctx, 1);
|
|
|
|
return FunctionType::get(ctx, {TokenType::get(ctx)}, {i1});
|
|
|
|
}
|
|
|
|
|
|
|
|
static FunctionType isValueErrorFunctionType(MLIRContext *ctx) {
|
|
|
|
auto value = opaquePointerType(ctx);
|
|
|
|
auto i1 = IntegerType::get(ctx, 1);
|
|
|
|
return FunctionType::get(ctx, {value}, {i1});
|
|
|
|
}
|
|
|
|
|
2021-05-27 04:33:38 +08:00
|
|
|
static FunctionType isGroupErrorFunctionType(MLIRContext *ctx) {
|
|
|
|
auto i1 = IntegerType::get(ctx, 1);
|
|
|
|
return FunctionType::get(ctx, {GroupType::get(ctx)}, {i1});
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
static FunctionType awaitTokenFunctionType(MLIRContext *ctx) {
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {TokenType::get(ctx)}, {});
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
static FunctionType awaitValueFunctionType(MLIRContext *ctx) {
|
|
|
|
auto value = opaquePointerType(ctx);
|
|
|
|
return FunctionType::get(ctx, {value}, {});
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:52 +08:00
|
|
|
static FunctionType awaitGroupFunctionType(MLIRContext *ctx) {
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {GroupType::get(ctx)}, {});
|
2020-11-13 19:01:52 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
static FunctionType executeFunctionType(MLIRContext *ctx) {
|
2020-12-24 21:08:09 +08:00
|
|
|
auto hdl = opaquePointerType(ctx);
|
2020-12-22 18:22:21 +08:00
|
|
|
auto resume = LLVM::LLVMPointerType::get(resumeFunctionType(ctx));
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {hdl, resume}, {});
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:52 +08:00
|
|
|
static FunctionType addTokenToGroupFunctionType(MLIRContext *ctx) {
|
2020-12-18 04:24:45 +08:00
|
|
|
auto i64 = IntegerType::get(ctx, 64);
|
|
|
|
return FunctionType::get(ctx, {TokenType::get(ctx), GroupType::get(ctx)},
|
|
|
|
{i64});
|
2020-11-13 19:01:52 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
static FunctionType awaitTokenAndExecuteFunctionType(MLIRContext *ctx) {
|
|
|
|
auto hdl = opaquePointerType(ctx);
|
2020-12-22 18:22:21 +08:00
|
|
|
auto resume = LLVM::LLVMPointerType::get(resumeFunctionType(ctx));
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {TokenType::get(ctx), hdl, resume}, {});
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
static FunctionType awaitValueAndExecuteFunctionType(MLIRContext *ctx) {
|
|
|
|
auto value = opaquePointerType(ctx);
|
|
|
|
auto hdl = opaquePointerType(ctx);
|
|
|
|
auto resume = LLVM::LLVMPointerType::get(resumeFunctionType(ctx));
|
|
|
|
return FunctionType::get(ctx, {value, hdl, resume}, {});
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:52 +08:00
|
|
|
static FunctionType awaitAllAndExecuteFunctionType(MLIRContext *ctx) {
|
2020-12-24 21:08:09 +08:00
|
|
|
auto hdl = opaquePointerType(ctx);
|
2020-12-22 18:22:21 +08:00
|
|
|
auto resume = LLVM::LLVMPointerType::get(resumeFunctionType(ctx));
|
2020-12-18 04:24:45 +08:00
|
|
|
return FunctionType::get(ctx, {GroupType::get(ctx), hdl, resume}, {});
|
2020-11-13 19:01:52 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
// Auxiliary coroutine resume intrinsic wrapper.
|
2021-01-05 23:22:53 +08:00
|
|
|
static Type resumeFunctionType(MLIRContext *ctx) {
|
2020-12-22 18:22:56 +08:00
|
|
|
auto voidTy = LLVM::LLVMVoidType::get(ctx);
|
2020-12-24 21:08:09 +08:00
|
|
|
auto i8Ptr = opaquePointerType(ctx);
|
2020-12-22 18:22:56 +08:00
|
|
|
return LLVM::LLVMFunctionType::get(voidTy, {i8Ptr}, false);
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
/// Adds Async Runtime C API declarations to the module.
|
2020-10-23 03:20:42 +08:00
|
|
|
static void addAsyncRuntimeApiDeclarations(ModuleOp module) {
|
2021-03-12 07:58:02 +08:00
|
|
|
auto builder =
|
|
|
|
ImplicitLocOpBuilder::atBlockEnd(module.getLoc(), module.getBody());
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2020-11-14 03:58:40 +08:00
|
|
|
auto addFuncDecl = [&](StringRef name, FunctionType type) {
|
|
|
|
if (module.lookupSymbol(name))
|
|
|
|
return;
|
2020-12-23 02:35:15 +08:00
|
|
|
builder.create<FuncOp>(name, type).setPrivate();
|
2020-11-14 03:58:40 +08:00
|
|
|
};
|
2020-11-13 19:01:52 +08:00
|
|
|
|
2020-11-14 03:58:40 +08:00
|
|
|
MLIRContext *ctx = module.getContext();
|
2020-11-20 18:42:28 +08:00
|
|
|
addFuncDecl(kAddRef, AsyncAPI::addOrDropRefFunctionType(ctx));
|
|
|
|
addFuncDecl(kDropRef, AsyncAPI::addOrDropRefFunctionType(ctx));
|
2020-11-14 03:58:40 +08:00
|
|
|
addFuncDecl(kCreateToken, AsyncAPI::createTokenFunctionType(ctx));
|
2020-12-24 21:08:09 +08:00
|
|
|
addFuncDecl(kCreateValue, AsyncAPI::createValueFunctionType(ctx));
|
2020-11-14 03:58:40 +08:00
|
|
|
addFuncDecl(kCreateGroup, AsyncAPI::createGroupFunctionType(ctx));
|
|
|
|
addFuncDecl(kEmplaceToken, AsyncAPI::emplaceTokenFunctionType(ctx));
|
2020-12-24 21:08:09 +08:00
|
|
|
addFuncDecl(kEmplaceValue, AsyncAPI::emplaceValueFunctionType(ctx));
|
2021-05-26 06:06:34 +08:00
|
|
|
addFuncDecl(kSetTokenError, AsyncAPI::setTokenErrorFunctionType(ctx));
|
|
|
|
addFuncDecl(kSetValueError, AsyncAPI::setValueErrorFunctionType(ctx));
|
|
|
|
addFuncDecl(kIsTokenError, AsyncAPI::isTokenErrorFunctionType(ctx));
|
|
|
|
addFuncDecl(kIsValueError, AsyncAPI::isValueErrorFunctionType(ctx));
|
2021-05-27 04:33:38 +08:00
|
|
|
addFuncDecl(kIsGroupError, AsyncAPI::isGroupErrorFunctionType(ctx));
|
2020-11-14 03:58:40 +08:00
|
|
|
addFuncDecl(kAwaitToken, AsyncAPI::awaitTokenFunctionType(ctx));
|
2020-12-24 21:08:09 +08:00
|
|
|
addFuncDecl(kAwaitValue, AsyncAPI::awaitValueFunctionType(ctx));
|
2020-11-14 03:58:40 +08:00
|
|
|
addFuncDecl(kAwaitGroup, AsyncAPI::awaitGroupFunctionType(ctx));
|
|
|
|
addFuncDecl(kExecute, AsyncAPI::executeFunctionType(ctx));
|
2020-12-24 21:08:09 +08:00
|
|
|
addFuncDecl(kGetValueStorage, AsyncAPI::getValueStorageFunctionType(ctx));
|
2020-11-14 03:58:40 +08:00
|
|
|
addFuncDecl(kAddTokenToGroup, AsyncAPI::addTokenToGroupFunctionType(ctx));
|
2020-12-24 21:08:09 +08:00
|
|
|
addFuncDecl(kAwaitTokenAndExecute,
|
|
|
|
AsyncAPI::awaitTokenAndExecuteFunctionType(ctx));
|
|
|
|
addFuncDecl(kAwaitValueAndExecute,
|
|
|
|
AsyncAPI::awaitValueAndExecuteFunctionType(ctx));
|
2020-11-20 18:42:28 +08:00
|
|
|
addFuncDecl(kAwaitAllAndExecute,
|
|
|
|
AsyncAPI::awaitAllAndExecuteFunctionType(ctx));
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-01-26 07:53:42 +08:00
|
|
|
// Add malloc/free declarations to the module.
|
2020-10-23 03:20:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-01-26 07:53:42 +08:00
|
|
|
static constexpr const char *kMalloc = "malloc";
|
|
|
|
static constexpr const char *kFree = "free";
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2020-12-23 02:35:15 +08:00
|
|
|
static void addLLVMFuncDecl(ModuleOp module, ImplicitLocOpBuilder &builder,
|
2021-01-05 23:22:53 +08:00
|
|
|
StringRef name, Type ret, ArrayRef<Type> params) {
|
2020-11-14 03:58:40 +08:00
|
|
|
if (module.lookupSymbol(name))
|
|
|
|
return;
|
2021-01-05 23:22:53 +08:00
|
|
|
Type type = LLVM::LLVMFunctionType::get(ret, params);
|
2020-12-23 02:35:15 +08:00
|
|
|
builder.create<LLVM::LLVMFuncOp>(name, type);
|
2020-11-14 03:58:40 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
/// Adds malloc/free declarations to the module.
|
|
|
|
static void addCRuntimeDeclarations(ModuleOp module) {
|
|
|
|
using namespace mlir::LLVM;
|
|
|
|
|
|
|
|
MLIRContext *ctx = module.getContext();
|
2021-03-12 07:58:02 +08:00
|
|
|
auto builder =
|
|
|
|
ImplicitLocOpBuilder::atBlockEnd(module.getLoc(), module.getBody());
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2020-12-22 18:22:56 +08:00
|
|
|
auto voidTy = LLVMVoidType::get(ctx);
|
[mlir] replace LLVMIntegerType with built-in integer type
The LLVM dialect type system has been closed until now, i.e. did not support
types from other dialects inside containers. While this has had obvious
benefits of deriving from a common base class, it has led to some simple types
being almost identical with the built-in types, namely integer and floating
point types. This in turn has led to a lot of larger-scale complexity: simple
types must still be converted, numerous operations that correspond to LLVM IR
intrinsics are replicated to produce versions operating on either LLVM dialect
or built-in types leading to quasi-duplicate dialects, lowering to the LLVM
dialect is essentially required to be one-shot because of type conversion, etc.
In this light, it is reasonable to trade off some local complexity in the
internal implementation of LLVM dialect types for removing larger-scale system
complexity. Previous commits to the LLVM dialect type system have adapted the
API to support types from other dialects.
Replace LLVMIntegerType with the built-in IntegerType plus additional checks
that such types are signless (these are isolated in a utility function that
replaced `isa<LLVMType>` and in the parser). Temporarily keep the possibility
to parse `!llvm.i32` as a synonym for `i32`, but add a deprecation notice.
Reviewed By: mehdi_amini, silvas, antiagainst
Differential Revision: https://reviews.llvm.org/D94178
2021-01-06 23:19:04 +08:00
|
|
|
auto i64 = IntegerType::get(ctx, 64);
|
|
|
|
auto i8Ptr = LLVMPointerType::get(IntegerType::get(ctx, 8));
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2020-11-14 03:58:40 +08:00
|
|
|
addLLVMFuncDecl(module, builder, kMalloc, i8Ptr, {i64});
|
|
|
|
addLLVMFuncDecl(module, builder, kFree, voidTy, {i8Ptr});
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Coroutine resume function wrapper.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static constexpr const char *kResume = "__resume";
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
/// A function that takes a coroutine handle and calls a `llvm.coro.resume`
|
|
|
|
/// intrinsics. We need this function to be able to pass it to the async
|
|
|
|
/// runtime execute API.
|
2020-10-23 03:20:42 +08:00
|
|
|
static void addResumeFunction(ModuleOp module) {
|
2021-02-02 22:42:16 +08:00
|
|
|
if (module.lookupSymbol(kResume))
|
|
|
|
return;
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
MLIRContext *ctx = module.getContext();
|
2021-03-12 07:58:02 +08:00
|
|
|
auto loc = module.getLoc();
|
|
|
|
auto moduleBuilder = ImplicitLocOpBuilder::atBlockEnd(loc, module.getBody());
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2020-12-22 18:22:56 +08:00
|
|
|
auto voidTy = LLVM::LLVMVoidType::get(ctx);
|
[mlir] replace LLVMIntegerType with built-in integer type
The LLVM dialect type system has been closed until now, i.e. did not support
types from other dialects inside containers. While this has had obvious
benefits of deriving from a common base class, it has led to some simple types
being almost identical with the built-in types, namely integer and floating
point types. This in turn has led to a lot of larger-scale complexity: simple
types must still be converted, numerous operations that correspond to LLVM IR
intrinsics are replicated to produce versions operating on either LLVM dialect
or built-in types leading to quasi-duplicate dialects, lowering to the LLVM
dialect is essentially required to be one-shot because of type conversion, etc.
In this light, it is reasonable to trade off some local complexity in the
internal implementation of LLVM dialect types for removing larger-scale system
complexity. Previous commits to the LLVM dialect type system have adapted the
API to support types from other dialects.
Replace LLVMIntegerType with the built-in IntegerType plus additional checks
that such types are signless (these are isolated in a utility function that
replaced `isa<LLVMType>` and in the parser). Temporarily keep the possibility
to parse `!llvm.i32` as a synonym for `i32`, but add a deprecation notice.
Reviewed By: mehdi_amini, silvas, antiagainst
Differential Revision: https://reviews.llvm.org/D94178
2021-01-06 23:19:04 +08:00
|
|
|
auto i8Ptr = LLVM::LLVMPointerType::get(IntegerType::get(ctx, 8));
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
auto resumeOp = moduleBuilder.create<LLVM::LLVMFuncOp>(
|
2021-03-12 07:58:02 +08:00
|
|
|
kResume, LLVM::LLVMFunctionType::get(voidTy, {i8Ptr}));
|
2020-11-14 03:58:40 +08:00
|
|
|
resumeOp.setPrivate();
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
auto *block = resumeOp.addEntryBlock();
|
2020-12-23 02:35:15 +08:00
|
|
|
auto blockBuilder = ImplicitLocOpBuilder::atBlockEnd(loc, block);
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2021-01-26 07:53:42 +08:00
|
|
|
blockBuilder.create<LLVM::CoroResumeOp>(resumeOp.getArgument(0));
|
2020-12-23 02:35:15 +08:00
|
|
|
blockBuilder.create<LLVM::ReturnOp>(ValueRange());
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert Async dialect types to LLVM types.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2020-12-24 21:08:09 +08:00
|
|
|
/// AsyncRuntimeTypeConverter only converts types from the Async dialect to
|
|
|
|
/// their runtime type (opaque pointers) and does not convert any other types.
|
2020-10-23 03:20:42 +08:00
|
|
|
class AsyncRuntimeTypeConverter : public TypeConverter {
|
|
|
|
public:
|
2020-12-24 21:08:09 +08:00
|
|
|
AsyncRuntimeTypeConverter() {
|
|
|
|
addConversion([](Type type) { return type; });
|
|
|
|
addConversion(convertAsyncTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<Type> convertAsyncTypes(Type type) {
|
|
|
|
if (type.isa<TokenType, GroupType, ValueType>())
|
|
|
|
return AsyncAPI::opaquePointerType(type.getContext());
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
if (type.isa<CoroIdType, CoroStateType>())
|
|
|
|
return AsyncAPI::tokenType(type.getContext());
|
|
|
|
if (type.isa<CoroHandleType>())
|
|
|
|
return AsyncAPI::opaquePointerType(type.getContext());
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
return llvm::None;
|
2020-10-23 03:20:42 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-01-19 02:58:34 +08:00
|
|
|
// Convert async.coro.id to @llvm.coro.id intrinsic.
|
2020-10-23 03:20:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2021-01-19 02:58:34 +08:00
|
|
|
class CoroIdOpConversion : public OpConversionPattern<CoroIdOp> {
|
2020-10-23 03:20:42 +08:00
|
|
|
public:
|
2021-01-19 02:58:34 +08:00
|
|
|
using OpConversionPattern::OpConversionPattern;
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(CoroIdOp op, OpAdaptor adaptor,
|
2020-10-23 03:20:42 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-01-19 02:58:34 +08:00
|
|
|
auto token = AsyncAPI::tokenType(op->getContext());
|
|
|
|
auto i8Ptr = AsyncAPI::opaquePointerType(op->getContext());
|
|
|
|
auto loc = op->getLoc();
|
|
|
|
|
|
|
|
// Constants for initializing coroutine frame.
|
|
|
|
auto constZero = rewriter.create<LLVM::ConstantOp>(
|
|
|
|
loc, rewriter.getI32Type(), rewriter.getI32IntegerAttr(0));
|
|
|
|
auto nullPtr = rewriter.create<LLVM::NullOp>(loc, i8Ptr);
|
|
|
|
|
|
|
|
// Get coroutine id: @llvm.coro.id.
|
2021-01-26 07:53:42 +08:00
|
|
|
rewriter.replaceOpWithNewOp<LLVM::CoroIdOp>(
|
|
|
|
op, token, ValueRange({constZero, nullPtr, nullPtr, nullPtr}));
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.coro.begin to @llvm.coro.begin intrinsic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CoroBeginOpConversion : public OpConversionPattern<CoroBeginOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(CoroBeginOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
auto i8Ptr = AsyncAPI::opaquePointerType(op->getContext());
|
|
|
|
auto loc = op->getLoc();
|
|
|
|
|
|
|
|
// Get coroutine frame size: @llvm.coro.size.i64.
|
2021-01-26 07:53:42 +08:00
|
|
|
auto coroSize =
|
|
|
|
rewriter.create<LLVM::CoroSizeOp>(loc, rewriter.getI64Type());
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
// Allocate memory for the coroutine frame.
|
|
|
|
auto coroAlloc = rewriter.create<LLVM::CallOp>(
|
2021-08-31 00:31:48 +08:00
|
|
|
loc, i8Ptr, SymbolRefAttr::get(rewriter.getContext(), kMalloc),
|
2021-01-26 07:53:42 +08:00
|
|
|
ValueRange(coroSize.getResult()));
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
// Begin a coroutine: @llvm.coro.begin.
|
2021-09-25 01:50:58 +08:00
|
|
|
auto coroId = CoroBeginOpAdaptor(adaptor.getOperands()).id();
|
2021-01-26 07:53:42 +08:00
|
|
|
rewriter.replaceOpWithNewOp<LLVM::CoroBeginOp>(
|
|
|
|
op, i8Ptr, ValueRange({coroId, coroAlloc.getResult(0)}));
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.coro.free to @llvm.coro.free intrinsic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CoroFreeOpConversion : public OpConversionPattern<CoroFreeOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(CoroFreeOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
auto i8Ptr = AsyncAPI::opaquePointerType(op->getContext());
|
|
|
|
auto loc = op->getLoc();
|
|
|
|
|
|
|
|
// Get a pointer to the coroutine frame memory: @llvm.coro.free.
|
2021-09-25 01:50:58 +08:00
|
|
|
auto coroMem =
|
|
|
|
rewriter.create<LLVM::CoroFreeOp>(loc, i8Ptr, adaptor.getOperands());
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
// Free the memory.
|
2021-08-31 00:31:48 +08:00
|
|
|
rewriter.replaceOpWithNewOp<LLVM::CallOp>(
|
|
|
|
op, TypeRange(), SymbolRefAttr::get(rewriter.getContext(), kFree),
|
|
|
|
ValueRange(coroMem.getResult()));
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.coro.end to @llvm.coro.end intrinsic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CoroEndOpConversion : public OpConversionPattern<CoroEndOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(CoroEndOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
// We are not in the block that is part of the unwind sequence.
|
|
|
|
auto constFalse = rewriter.create<LLVM::ConstantOp>(
|
|
|
|
op->getLoc(), rewriter.getI1Type(), rewriter.getBoolAttr(false));
|
|
|
|
|
|
|
|
// Mark the end of a coroutine: @llvm.coro.end.
|
2021-09-25 01:50:58 +08:00
|
|
|
auto coroHdl = adaptor.handle();
|
2021-01-26 07:53:42 +08:00
|
|
|
rewriter.create<LLVM::CoroEndOp>(op->getLoc(), rewriter.getI1Type(),
|
|
|
|
ValueRange({coroHdl, constFalse}));
|
2021-01-19 02:58:34 +08:00
|
|
|
rewriter.eraseOp(op);
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.coro.save to @llvm.coro.save intrinsic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CoroSaveOpConversion : public OpConversionPattern<CoroSaveOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(CoroSaveOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
// Save the coroutine state: @llvm.coro.save
|
2021-01-26 07:53:42 +08:00
|
|
|
rewriter.replaceOpWithNewOp<LLVM::CoroSaveOp>(
|
2021-09-25 01:50:58 +08:00
|
|
|
op, AsyncAPI::tokenType(op->getContext()), adaptor.getOperands());
|
2021-01-19 02:58:34 +08:00
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-11-20 18:42:28 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-01-19 02:58:34 +08:00
|
|
|
// Convert async.coro.suspend to @llvm.coro.suspend intrinsic.
|
2020-11-20 18:42:28 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
/// Convert async.coro.suspend to the @llvm.coro.suspend intrinsic call, and
|
|
|
|
/// branch to the appropriate block based on the return code.
|
|
|
|
///
|
|
|
|
/// Before:
|
|
|
|
///
|
|
|
|
/// ^suspended:
|
|
|
|
/// "opBefore"(...)
|
|
|
|
/// async.coro.suspend %state, ^suspend, ^resume, ^cleanup
|
|
|
|
/// ^resume:
|
|
|
|
/// "op"(...)
|
|
|
|
/// ^cleanup: ...
|
|
|
|
/// ^suspend: ...
|
|
|
|
///
|
|
|
|
/// After:
|
|
|
|
///
|
|
|
|
/// ^suspended:
|
|
|
|
/// "opBefore"(...)
|
2021-01-26 07:53:42 +08:00
|
|
|
/// %suspend = llmv.intr.coro.suspend ...
|
2021-01-19 02:58:34 +08:00
|
|
|
/// switch %suspend [-1: ^suspend, 0: ^resume, 1: ^cleanup]
|
|
|
|
/// ^resume:
|
|
|
|
/// "op"(...)
|
|
|
|
/// ^cleanup: ...
|
|
|
|
/// ^suspend: ...
|
|
|
|
///
|
|
|
|
class CoroSuspendOpConversion : public OpConversionPattern<CoroSuspendOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(CoroSuspendOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
auto i8 = rewriter.getIntegerType(8);
|
|
|
|
auto i32 = rewriter.getI32Type();
|
|
|
|
auto loc = op->getLoc();
|
|
|
|
|
|
|
|
// This is not a final suspension point.
|
|
|
|
auto constFalse = rewriter.create<LLVM::ConstantOp>(
|
|
|
|
loc, rewriter.getI1Type(), rewriter.getBoolAttr(false));
|
|
|
|
|
|
|
|
// Suspend a coroutine: @llvm.coro.suspend
|
2021-09-25 01:50:58 +08:00
|
|
|
auto coroState = adaptor.state();
|
2021-01-26 07:53:42 +08:00
|
|
|
auto coroSuspend = rewriter.create<LLVM::CoroSuspendOp>(
|
|
|
|
loc, i8, ValueRange({coroState, constFalse}));
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
// Cast return code to i32.
|
|
|
|
|
|
|
|
// After a suspension point decide if we should branch into resume, cleanup
|
|
|
|
// or suspend block of the coroutine (see @llvm.coro.suspend return code
|
|
|
|
// documentation).
|
|
|
|
llvm::SmallVector<int32_t, 2> caseValues = {0, 1};
|
|
|
|
llvm::SmallVector<Block *, 2> caseDest = {op.resumeDest(),
|
|
|
|
op.cleanupDest()};
|
|
|
|
rewriter.replaceOpWithNewOp<LLVM::SwitchOp>(
|
2021-01-26 07:53:42 +08:00
|
|
|
op, rewriter.create<LLVM::SExtOp>(loc, i32, coroSuspend.getResult()),
|
2021-01-19 02:58:34 +08:00
|
|
|
/*defaultDestination=*/op.suspendDest(),
|
|
|
|
/*defaultOperands=*/ValueRange(),
|
|
|
|
/*caseValues=*/caseValues,
|
|
|
|
/*caseDestinations=*/caseDest,
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
/*caseOperands=*/ArrayRef<ValueRange>({ValueRange(), ValueRange()}),
|
2021-01-19 02:58:34 +08:00
|
|
|
/*branchWeights=*/ArrayRef<int32_t>());
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.create to the corresponding runtime API call.
|
|
|
|
//
|
|
|
|
// To allocate storage for the async values we use getelementptr trick:
|
|
|
|
// http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeCreateOpLowering : public OpConversionPattern<RuntimeCreateOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeCreateOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
TypeConverter *converter = getTypeConverter();
|
|
|
|
Type resultType = op->getResultTypes()[0];
|
|
|
|
|
2021-06-23 21:24:09 +08:00
|
|
|
// Tokens creation maps to a simple function call.
|
|
|
|
if (resultType.isa<TokenType>()) {
|
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, kCreateToken,
|
|
|
|
converter->convertType(resultType));
|
2021-01-19 02:58:34 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// To create a value we need to compute the storage requirement.
|
|
|
|
if (auto value = resultType.dyn_cast<ValueType>()) {
|
|
|
|
// Returns the size requirements for the async value storage.
|
|
|
|
auto sizeOf = [&](ValueType valueType) -> Value {
|
|
|
|
auto loc = op->getLoc();
|
2021-09-27 22:06:54 +08:00
|
|
|
auto i64 = rewriter.getI64Type();
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
auto storedType = converter->convertType(valueType.getValueType());
|
|
|
|
auto storagePtrType = LLVM::LLVMPointerType::get(storedType);
|
|
|
|
|
|
|
|
// %Size = getelementptr %T* null, int 1
|
2021-09-27 22:06:54 +08:00
|
|
|
// %SizeI = ptrtoint %T* %Size to i64
|
2021-01-19 02:58:34 +08:00
|
|
|
auto nullPtr = rewriter.create<LLVM::NullOp>(loc, storagePtrType);
|
|
|
|
auto one = rewriter.create<LLVM::ConstantOp>(
|
2021-09-27 22:06:54 +08:00
|
|
|
loc, i64, rewriter.getI64IntegerAttr(1));
|
2021-01-19 02:58:34 +08:00
|
|
|
auto gep = rewriter.create<LLVM::GEPOp>(loc, storagePtrType, nullPtr,
|
|
|
|
one.getResult());
|
2021-09-27 22:06:54 +08:00
|
|
|
return rewriter.create<LLVM::PtrToIntOp>(loc, i64, gep);
|
2021-01-19 02:58:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, kCreateValue, resultType,
|
|
|
|
sizeOf(value));
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rewriter.notifyMatchFailure(op, "unsupported async type");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-06-23 21:24:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.create_group to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeCreateGroupOpLowering
|
|
|
|
: public OpConversionPattern<RuntimeCreateGroupOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeCreateGroupOp op, OpAdaptor adaptor,
|
2021-06-23 21:24:09 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
TypeConverter *converter = getTypeConverter();
|
2021-06-26 03:14:51 +08:00
|
|
|
Type resultType = op.getResult().getType();
|
2021-06-23 21:24:09 +08:00
|
|
|
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, kCreateGroup,
|
|
|
|
converter->convertType(resultType),
|
|
|
|
adaptor.getOperands());
|
2021-06-23 21:24:09 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.set_available to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeSetAvailableOpLowering
|
|
|
|
: public OpConversionPattern<RuntimeSetAvailableOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeSetAvailableOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-05-27 04:33:38 +08:00
|
|
|
StringRef apiFuncName =
|
|
|
|
TypeSwitch<Type, StringRef>(op.operand().getType())
|
|
|
|
.Case<TokenType>([](Type) { return kEmplaceToken; })
|
|
|
|
.Case<ValueType>([](Type) { return kEmplaceValue; });
|
|
|
|
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, apiFuncName, TypeRange(),
|
|
|
|
adaptor.getOperands());
|
2021-05-27 04:33:38 +08:00
|
|
|
|
2021-05-26 06:06:34 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2021-01-19 02:58:34 +08:00
|
|
|
|
2021-05-26 06:06:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.set_error to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-01-19 02:58:34 +08:00
|
|
|
|
2021-05-26 06:06:34 +08:00
|
|
|
namespace {
|
|
|
|
class RuntimeSetErrorOpLowering
|
|
|
|
: public OpConversionPattern<RuntimeSetErrorOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeSetErrorOp op, OpAdaptor adaptor,
|
2021-05-26 06:06:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-05-27 04:33:38 +08:00
|
|
|
StringRef apiFuncName =
|
|
|
|
TypeSwitch<Type, StringRef>(op.operand().getType())
|
|
|
|
.Case<TokenType>([](Type) { return kSetTokenError; })
|
|
|
|
.Case<ValueType>([](Type) { return kSetValueError; });
|
|
|
|
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, apiFuncName, TypeRange(),
|
|
|
|
adaptor.getOperands());
|
2021-05-27 04:33:38 +08:00
|
|
|
|
2021-05-26 06:06:34 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.is_error to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeIsErrorOpLowering : public OpConversionPattern<RuntimeIsErrorOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeIsErrorOp op, OpAdaptor adaptor,
|
2021-05-26 06:06:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-05-27 04:33:38 +08:00
|
|
|
StringRef apiFuncName =
|
|
|
|
TypeSwitch<Type, StringRef>(op.operand().getType())
|
|
|
|
.Case<TokenType>([](Type) { return kIsTokenError; })
|
|
|
|
.Case<GroupType>([](Type) { return kIsGroupError; })
|
|
|
|
.Case<ValueType>([](Type) { return kIsValueError; });
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, apiFuncName, rewriter.getI1Type(),
|
2021-09-25 01:50:58 +08:00
|
|
|
adaptor.getOperands());
|
2021-05-26 06:06:34 +08:00
|
|
|
return success();
|
2021-01-19 02:58:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.await to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeAwaitOpLowering : public OpConversionPattern<RuntimeAwaitOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeAwaitOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-05-27 04:33:38 +08:00
|
|
|
StringRef apiFuncName =
|
|
|
|
TypeSwitch<Type, StringRef>(op.operand().getType())
|
|
|
|
.Case<TokenType>([](Type) { return kAwaitToken; })
|
|
|
|
.Case<ValueType>([](Type) { return kAwaitValue; })
|
|
|
|
.Case<GroupType>([](Type) { return kAwaitGroup; });
|
2021-01-19 02:58:34 +08:00
|
|
|
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.create<CallOp>(op->getLoc(), apiFuncName, TypeRange(),
|
|
|
|
adaptor.getOperands());
|
2021-01-19 02:58:34 +08:00
|
|
|
rewriter.eraseOp(op);
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.await_and_resume to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeAwaitAndResumeOpLowering
|
|
|
|
: public OpConversionPattern<RuntimeAwaitAndResumeOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeAwaitAndResumeOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-05-27 04:33:38 +08:00
|
|
|
StringRef apiFuncName =
|
|
|
|
TypeSwitch<Type, StringRef>(op.operand().getType())
|
|
|
|
.Case<TokenType>([](Type) { return kAwaitTokenAndExecute; })
|
|
|
|
.Case<ValueType>([](Type) { return kAwaitValueAndExecute; })
|
|
|
|
.Case<GroupType>([](Type) { return kAwaitAllAndExecute; });
|
2021-01-19 02:58:34 +08:00
|
|
|
|
2021-09-25 01:50:58 +08:00
|
|
|
Value operand = adaptor.operand();
|
|
|
|
Value handle = adaptor.handle();
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
// A pointer to coroutine resume intrinsic wrapper.
|
2021-02-02 22:42:16 +08:00
|
|
|
addResumeFunction(op->getParentOfType<ModuleOp>());
|
2021-01-19 02:58:34 +08:00
|
|
|
auto resumeFnTy = AsyncAPI::resumeFunctionType(op->getContext());
|
|
|
|
auto resumePtr = rewriter.create<LLVM::AddressOfOp>(
|
|
|
|
op->getLoc(), LLVM::LLVMPointerType::get(resumeFnTy), kResume);
|
|
|
|
|
|
|
|
rewriter.create<CallOp>(op->getLoc(), apiFuncName, TypeRange(),
|
2021-10-25 09:36:33 +08:00
|
|
|
ValueRange({operand, handle, resumePtr.getRes()}));
|
2021-01-19 02:58:34 +08:00
|
|
|
rewriter.eraseOp(op);
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.resume to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeResumeOpLowering : public OpConversionPattern<RuntimeResumeOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeResumeOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
// A pointer to coroutine resume intrinsic wrapper.
|
2021-02-02 22:42:16 +08:00
|
|
|
addResumeFunction(op->getParentOfType<ModuleOp>());
|
2021-01-19 02:58:34 +08:00
|
|
|
auto resumeFnTy = AsyncAPI::resumeFunctionType(op->getContext());
|
|
|
|
auto resumePtr = rewriter.create<LLVM::AddressOfOp>(
|
|
|
|
op->getLoc(), LLVM::LLVMPointerType::get(resumeFnTy), kResume);
|
|
|
|
|
|
|
|
// Call async runtime API to execute a coroutine in the managed thread.
|
2021-09-25 01:50:58 +08:00
|
|
|
auto coroHdl = adaptor.handle();
|
2021-10-25 09:36:33 +08:00
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(
|
|
|
|
op, TypeRange(), kExecute, ValueRange({coroHdl, resumePtr.getRes()}));
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.store to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeStoreOpLowering : public OpConversionPattern<RuntimeStoreOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeStoreOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Location loc = op->getLoc();
|
|
|
|
|
|
|
|
// Get a pointer to the async value storage from the runtime.
|
|
|
|
auto i8Ptr = AsyncAPI::opaquePointerType(rewriter.getContext());
|
2021-09-25 01:50:58 +08:00
|
|
|
auto storage = adaptor.storage();
|
2021-01-19 02:58:34 +08:00
|
|
|
auto storagePtr = rewriter.create<CallOp>(loc, kGetValueStorage,
|
|
|
|
TypeRange(i8Ptr), storage);
|
|
|
|
|
|
|
|
// Cast from i8* to the LLVM pointer type.
|
|
|
|
auto valueType = op.value().getType();
|
|
|
|
auto llvmValueType = getTypeConverter()->convertType(valueType);
|
2021-01-26 18:40:43 +08:00
|
|
|
if (!llvmValueType)
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "failed to convert stored value type to LLVM type");
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
auto castedStoragePtr = rewriter.create<LLVM::BitcastOp>(
|
|
|
|
loc, LLVM::LLVMPointerType::get(llvmValueType),
|
|
|
|
storagePtr.getResult(0));
|
|
|
|
|
|
|
|
// Store the yielded value into the async value storage.
|
2021-09-25 01:50:58 +08:00
|
|
|
auto value = adaptor.value();
|
2021-01-19 02:58:34 +08:00
|
|
|
rewriter.create<LLVM::StoreOp>(loc, value, castedStoragePtr.getResult());
|
|
|
|
|
|
|
|
// Erase the original runtime store operation.
|
|
|
|
rewriter.eraseOp(op);
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.load to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeLoadOpLowering : public OpConversionPattern<RuntimeLoadOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeLoadOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Location loc = op->getLoc();
|
|
|
|
|
|
|
|
// Get a pointer to the async value storage from the runtime.
|
|
|
|
auto i8Ptr = AsyncAPI::opaquePointerType(rewriter.getContext());
|
2021-09-25 01:50:58 +08:00
|
|
|
auto storage = adaptor.storage();
|
2021-01-19 02:58:34 +08:00
|
|
|
auto storagePtr = rewriter.create<CallOp>(loc, kGetValueStorage,
|
|
|
|
TypeRange(i8Ptr), storage);
|
|
|
|
|
|
|
|
// Cast from i8* to the LLVM pointer type.
|
|
|
|
auto valueType = op.result().getType();
|
|
|
|
auto llvmValueType = getTypeConverter()->convertType(valueType);
|
2021-01-26 18:40:43 +08:00
|
|
|
if (!llvmValueType)
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "failed to convert loaded value type to LLVM type");
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
auto castedStoragePtr = rewriter.create<LLVM::BitcastOp>(
|
|
|
|
loc, LLVM::LLVMPointerType::get(llvmValueType),
|
|
|
|
storagePtr.getResult(0));
|
|
|
|
|
|
|
|
// Load from the casted pointer.
|
|
|
|
rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op, castedStoragePtr.getResult());
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.add_to_group to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeAddToGroupOpLowering
|
|
|
|
: public OpConversionPattern<RuntimeAddToGroupOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RuntimeAddToGroupOp op, OpAdaptor adaptor,
|
2021-01-19 02:58:34 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
// Currently we can only add tokens to the group.
|
|
|
|
if (!op.operand().getType().isa<TokenType>())
|
|
|
|
return rewriter.notifyMatchFailure(op, "only token type is supported");
|
|
|
|
|
|
|
|
// Replace with a runtime API function call.
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(
|
|
|
|
op, kAddTokenToGroup, rewriter.getI64Type(), adaptor.getOperands());
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Async reference counting ops lowering (`async.runtime.add_ref` and
|
|
|
|
// `async.runtime.drop_ref` to the corresponding API calls).
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2020-11-20 18:42:28 +08:00
|
|
|
template <typename RefCountingOp>
|
2021-01-19 02:58:34 +08:00
|
|
|
class RefCountingOpLowering : public OpConversionPattern<RefCountingOp> {
|
2020-11-20 18:42:28 +08:00
|
|
|
public:
|
2020-12-24 21:08:09 +08:00
|
|
|
explicit RefCountingOpLowering(TypeConverter &converter, MLIRContext *ctx,
|
|
|
|
StringRef apiFunctionName)
|
2021-01-19 02:58:34 +08:00
|
|
|
: OpConversionPattern<RefCountingOp>(converter, ctx),
|
2020-11-20 18:42:28 +08:00
|
|
|
apiFunctionName(apiFunctionName) {}
|
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(RefCountingOp op, typename RefCountingOp::Adaptor adaptor,
|
2020-11-20 18:42:28 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-10-13 07:14:57 +08:00
|
|
|
auto count = rewriter.create<arith::ConstantOp>(
|
|
|
|
op->getLoc(), rewriter.getI64Type(),
|
|
|
|
rewriter.getI64IntegerAttr(op.count()));
|
2020-11-20 18:42:28 +08:00
|
|
|
|
2021-09-25 01:50:58 +08:00
|
|
|
auto operand = adaptor.operand();
|
2020-11-20 18:42:28 +08:00
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, TypeRange(), apiFunctionName,
|
2021-01-19 02:58:34 +08:00
|
|
|
ValueRange({operand, count}));
|
2020-11-20 18:42:28 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef apiFunctionName;
|
|
|
|
};
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
class RuntimeAddRefOpLowering : public RefCountingOpLowering<RuntimeAddRefOp> {
|
2020-11-20 18:42:28 +08:00
|
|
|
public:
|
2021-01-19 02:58:34 +08:00
|
|
|
explicit RuntimeAddRefOpLowering(TypeConverter &converter, MLIRContext *ctx)
|
2020-12-24 21:08:09 +08:00
|
|
|
: RefCountingOpLowering(converter, ctx, kAddRef) {}
|
2020-11-20 18:42:28 +08:00
|
|
|
};
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
class RuntimeDropRefOpLowering
|
|
|
|
: public RefCountingOpLowering<RuntimeDropRefOp> {
|
2020-11-20 18:42:28 +08:00
|
|
|
public:
|
2021-01-19 02:58:34 +08:00
|
|
|
explicit RuntimeDropRefOpLowering(TypeConverter &converter, MLIRContext *ctx)
|
2020-12-24 21:08:09 +08:00
|
|
|
: RefCountingOpLowering(converter, ctx, kDropRef) {}
|
2020-11-20 18:42:28 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-01-19 02:58:34 +08:00
|
|
|
// Convert return operations that return async values from async regions.
|
2020-10-23 03:20:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2021-01-19 02:58:34 +08:00
|
|
|
class ReturnOpOpConversion : public OpConversionPattern<ReturnOp> {
|
2020-10-23 03:20:42 +08:00
|
|
|
public:
|
2021-01-19 02:58:34 +08:00
|
|
|
using OpConversionPattern::OpConversionPattern;
|
2020-11-13 19:01:52 +08:00
|
|
|
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(ReturnOp op, OpAdaptor adaptor,
|
2020-11-13 19:01:52 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.replaceOpWithNewOp<ReturnOp>(op, adaptor.getOperands());
|
2020-11-13 19:01:52 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ConvertAsyncToLLVMPass
|
|
|
|
: public ConvertAsyncToLLVMBase<ConvertAsyncToLLVMPass> {
|
|
|
|
void runOnOperation() override;
|
|
|
|
};
|
2021-01-19 02:58:34 +08:00
|
|
|
} // namespace
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
void ConvertAsyncToLLVMPass::runOnOperation() {
|
|
|
|
ModuleOp module = getOperation();
|
2021-01-26 18:40:43 +08:00
|
|
|
MLIRContext *ctx = module->getContext();
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2021-02-02 22:42:16 +08:00
|
|
|
// Add declarations for most functions required by the coroutines lowering.
|
|
|
|
// We delay adding the resume function until it's needed because it currently
|
|
|
|
// fails to compile unless '-O0' is specified.
|
2020-10-23 03:20:42 +08:00
|
|
|
addAsyncRuntimeApiDeclarations(module);
|
|
|
|
addCRuntimeDeclarations(module);
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
// Lower async.runtime and async.coro operations to Async Runtime API and
|
|
|
|
// LLVM coroutine intrinsics.
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
// Convert async dialect types and operations to LLVM dialect.
|
|
|
|
AsyncRuntimeTypeConverter converter;
|
2021-03-23 07:58:34 +08:00
|
|
|
RewritePatternSet patterns(ctx);
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2021-01-26 18:40:43 +08:00
|
|
|
// We use conversion to LLVM type to lower async.runtime load and store
|
|
|
|
// operations.
|
|
|
|
LLVMTypeConverter llvmConverter(ctx);
|
|
|
|
llvmConverter.addConversion(AsyncRuntimeTypeConverter::convertAsyncTypes);
|
|
|
|
|
2020-12-24 21:08:09 +08:00
|
|
|
// Convert async types in function signatures and function calls.
|
2021-03-21 07:29:41 +08:00
|
|
|
populateFuncOpTypeConversionPattern(patterns, converter);
|
|
|
|
populateCallOpTypeConversionPattern(patterns, converter);
|
2020-12-24 21:08:09 +08:00
|
|
|
|
|
|
|
// Convert return operations inside async.execute regions.
|
2021-03-23 07:58:34 +08:00
|
|
|
patterns.add<ReturnOpOpConversion>(converter, ctx);
|
2020-12-24 21:08:09 +08:00
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
// Lower async.runtime operations to the async runtime API calls.
|
2021-05-26 06:06:34 +08:00
|
|
|
patterns.add<RuntimeSetAvailableOpLowering, RuntimeSetErrorOpLowering,
|
|
|
|
RuntimeIsErrorOpLowering, RuntimeAwaitOpLowering,
|
2021-03-23 07:58:34 +08:00
|
|
|
RuntimeAwaitAndResumeOpLowering, RuntimeResumeOpLowering,
|
|
|
|
RuntimeAddToGroupOpLowering, RuntimeAddRefOpLowering,
|
|
|
|
RuntimeDropRefOpLowering>(converter, ctx);
|
2020-12-24 21:08:09 +08:00
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
// Lower async.runtime operations that rely on LLVM type converter to convert
|
|
|
|
// from async value payload type to the LLVM type.
|
2021-06-23 21:24:09 +08:00
|
|
|
patterns.add<RuntimeCreateOpLowering, RuntimeCreateGroupOpLowering,
|
|
|
|
RuntimeStoreOpLowering, RuntimeLoadOpLowering>(llvmConverter,
|
|
|
|
ctx);
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
// Lower async coroutine operations to LLVM coroutine intrinsics.
|
2021-03-23 07:58:34 +08:00
|
|
|
patterns
|
|
|
|
.add<CoroIdOpConversion, CoroBeginOpConversion, CoroFreeOpConversion,
|
|
|
|
CoroEndOpConversion, CoroSaveOpConversion, CoroSuspendOpConversion>(
|
|
|
|
converter, ctx);
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
ConversionTarget target(*ctx);
|
2021-10-13 07:14:57 +08:00
|
|
|
target
|
|
|
|
.addLegalOp<arith::ConstantOp, ConstantOp, UnrealizedConversionCastOp>();
|
2020-10-23 03:20:42 +08:00
|
|
|
target.addLegalDialect<LLVM::LLVMDialect>();
|
2020-12-24 21:08:09 +08:00
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
// All operations from Async dialect must be lowered to the runtime API and
|
|
|
|
// LLVM intrinsics calls.
|
2020-10-23 03:20:42 +08:00
|
|
|
target.addIllegalDialect<AsyncDialect>();
|
2020-12-24 21:08:09 +08:00
|
|
|
|
|
|
|
// Add dynamic legality constraints to apply conversions defined above.
|
2020-10-23 03:20:42 +08:00
|
|
|
target.addDynamicallyLegalOp<FuncOp>(
|
|
|
|
[&](FuncOp op) { return converter.isSignatureLegal(op.getType()); });
|
2020-12-24 21:08:09 +08:00
|
|
|
target.addDynamicallyLegalOp<ReturnOp>(
|
|
|
|
[&](ReturnOp op) { return converter.isLegal(op.getOperandTypes()); });
|
|
|
|
target.addDynamicallyLegalOp<CallOp>([&](CallOp op) {
|
|
|
|
return converter.isSignatureLegal(op.getCalleeType());
|
|
|
|
});
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2020-10-27 08:25:01 +08:00
|
|
|
if (failed(applyPartialConversion(module, target, std::move(patterns))))
|
2020-10-23 03:20:42 +08:00
|
|
|
signalPassFailure();
|
|
|
|
}
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Patterns for structural type conversions for the Async dialect operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-23 03:20:42 +08:00
|
|
|
|
2021-01-12 03:34:44 +08:00
|
|
|
namespace {
|
|
|
|
class ConvertExecuteOpTypes : public OpConversionPattern<ExecuteOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(ExecuteOp op, OpAdaptor adaptor,
|
2021-01-12 03:34:44 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
ExecuteOp newOp =
|
|
|
|
cast<ExecuteOp>(rewriter.cloneWithoutRegions(*op.getOperation()));
|
|
|
|
rewriter.inlineRegionBefore(op.getRegion(), newOp.getRegion(),
|
|
|
|
newOp.getRegion().end());
|
|
|
|
|
|
|
|
// Set operands and update block argument and result types.
|
2021-09-25 01:50:58 +08:00
|
|
|
newOp->setOperands(adaptor.getOperands());
|
2021-01-12 03:34:44 +08:00
|
|
|
if (failed(rewriter.convertRegionTypes(&newOp.getRegion(), *typeConverter)))
|
|
|
|
return failure();
|
|
|
|
for (auto result : newOp.getResults())
|
|
|
|
result.setType(typeConverter->convertType(result.getType()));
|
|
|
|
|
|
|
|
rewriter.replaceOp(op, newOp.getResults());
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dummy pattern to trigger the appropriate type conversion / materialization.
|
|
|
|
class ConvertAwaitOpTypes : public OpConversionPattern<AwaitOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(AwaitOp op, OpAdaptor adaptor,
|
2021-01-12 03:34:44 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.replaceOpWithNewOp<AwaitOp>(op, adaptor.getOperands().front());
|
2021-01-12 03:34:44 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dummy pattern to trigger the appropriate type conversion / materialization.
|
|
|
|
class ConvertYieldOpTypes : public OpConversionPattern<async::YieldOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
2021-09-25 01:50:58 +08:00
|
|
|
matchAndRewrite(async::YieldOp op, OpAdaptor adaptor,
|
2021-01-12 03:34:44 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-09-25 01:50:58 +08:00
|
|
|
rewriter.replaceOpWithNewOp<async::YieldOp>(op, adaptor.getOperands());
|
2021-01-12 03:34:44 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-10-23 03:20:42 +08:00
|
|
|
std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertAsyncToLLVMPass() {
|
|
|
|
return std::make_unique<ConvertAsyncToLLVMPass>();
|
|
|
|
}
|
2021-01-12 03:34:44 +08:00
|
|
|
|
|
|
|
void mlir::populateAsyncStructuralTypeConversionsAndLegality(
|
2021-03-23 07:58:34 +08:00
|
|
|
TypeConverter &typeConverter, RewritePatternSet &patterns,
|
2021-03-21 07:29:41 +08:00
|
|
|
ConversionTarget &target) {
|
2021-01-12 03:34:44 +08:00
|
|
|
typeConverter.addConversion([&](TokenType type) { return type; });
|
|
|
|
typeConverter.addConversion([&](ValueType type) {
|
2021-06-03 15:25:22 +08:00
|
|
|
Type converted = typeConverter.convertType(type.getValueType());
|
|
|
|
return converted ? ValueType::get(converted) : converted;
|
2021-01-12 03:34:44 +08:00
|
|
|
});
|
|
|
|
|
2021-03-23 07:58:34 +08:00
|
|
|
patterns.add<ConvertExecuteOpTypes, ConvertAwaitOpTypes, ConvertYieldOpTypes>(
|
|
|
|
typeConverter, patterns.getContext());
|
2021-01-12 03:34:44 +08:00
|
|
|
|
|
|
|
target.addDynamicallyLegalOp<AwaitOp, ExecuteOp, async::YieldOp>(
|
|
|
|
[&](Operation *op) { return typeConverter.isLegal(op); });
|
|
|
|
}
|