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"
|
2020-12-24 21:08:09 +08:00
|
|
|
#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.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"
|
|
|
|
|
|
|
|
#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";
|
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);
|
2020-12-18 04:24:45 +08:00
|
|
|
auto count = IntegerType::get(ctx, 32);
|
|
|
|
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) {
|
|
|
|
auto i32 = IntegerType::get(ctx, 32);
|
|
|
|
auto value = opaquePointerType(ctx);
|
|
|
|
return FunctionType::get(ctx, {i32}, {value});
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:52 +08:00
|
|
|
static FunctionType createGroupFunctionType(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-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}, {});
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-23 02:35:15 +08:00
|
|
|
auto builder = ImplicitLocOpBuilder::atBlockTerminator(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));
|
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();
|
2020-12-23 02:35:15 +08:00
|
|
|
ImplicitLocOpBuilder builder(module.getLoc(),
|
|
|
|
module.getBody()->getTerminator());
|
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) {
|
|
|
|
MLIRContext *ctx = module.getContext();
|
|
|
|
|
|
|
|
OpBuilder moduleBuilder(module.getBody()->getTerminator());
|
|
|
|
Location loc = module.getLoc();
|
|
|
|
|
|
|
|
if (module.lookupSymbol(kResume))
|
|
|
|
return;
|
|
|
|
|
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>(
|
2020-12-22 18:22:56 +08:00
|
|
|
loc, 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-01-19 02:58:34 +08:00
|
|
|
matchAndRewrite(CoroIdOp op, ArrayRef<Value> operands,
|
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
|
|
|
|
matchAndRewrite(CoroBeginOp op, ArrayRef<Value> operands,
|
|
|
|
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>(
|
|
|
|
loc, i8Ptr, rewriter.getSymbolRefAttr(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.
|
|
|
|
auto coroId = CoroBeginOpAdaptor(operands).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
|
|
|
|
matchAndRewrite(CoroFreeOp op, ArrayRef<Value> operands,
|
|
|
|
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-01-26 07:53:42 +08:00
|
|
|
auto coroMem = rewriter.create<LLVM::CoroFreeOp>(loc, i8Ptr, operands);
|
2021-01-19 02:58:34 +08:00
|
|
|
|
|
|
|
// Free the memory.
|
|
|
|
rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, TypeRange(),
|
|
|
|
rewriter.getSymbolRefAttr(kFree),
|
2021-01-26 07:53:42 +08:00
|
|
|
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
|
|
|
|
matchAndRewrite(CoroEndOp op, ArrayRef<Value> operands,
|
|
|
|
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.
|
|
|
|
auto coroHdl = CoroEndOpAdaptor(operands).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
|
|
|
|
matchAndRewrite(CoroSaveOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
// Save the coroutine state: @llvm.coro.save
|
2021-01-26 07:53:42 +08:00
|
|
|
rewriter.replaceOpWithNewOp<LLVM::CoroSaveOp>(
|
|
|
|
op, AsyncAPI::tokenType(op->getContext()), operands);
|
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
|
|
|
|
matchAndRewrite(CoroSuspendOp op, ArrayRef<Value> operands,
|
|
|
|
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
|
|
|
|
auto coroState = CoroSuspendOpAdaptor(operands).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,
|
|
|
|
/*caseOperands=*/ArrayRef<ValueRange>(),
|
|
|
|
/*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
|
|
|
|
matchAndRewrite(RuntimeCreateOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
TypeConverter *converter = getTypeConverter();
|
|
|
|
Type resultType = op->getResultTypes()[0];
|
|
|
|
|
|
|
|
// Tokens and Groups lowered to function calls without arguments.
|
|
|
|
if (resultType.isa<TokenType>() || resultType.isa<GroupType>()) {
|
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(
|
|
|
|
op, resultType.isa<TokenType>() ? kCreateToken : kCreateGroup,
|
|
|
|
converter->convertType(resultType));
|
|
|
|
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();
|
|
|
|
auto i32 = rewriter.getI32Type();
|
|
|
|
|
|
|
|
auto storedType = converter->convertType(valueType.getValueType());
|
|
|
|
auto storagePtrType = LLVM::LLVMPointerType::get(storedType);
|
|
|
|
|
|
|
|
// %Size = getelementptr %T* null, int 1
|
|
|
|
// %SizeI = ptrtoint %T* %Size to i32
|
|
|
|
auto nullPtr = rewriter.create<LLVM::NullOp>(loc, storagePtrType);
|
|
|
|
auto one = rewriter.create<LLVM::ConstantOp>(
|
|
|
|
loc, i32, rewriter.getI32IntegerAttr(1));
|
|
|
|
auto gep = rewriter.create<LLVM::GEPOp>(loc, storagePtrType, nullPtr,
|
|
|
|
one.getResult());
|
|
|
|
return rewriter.create<LLVM::PtrToIntOp>(loc, i32, gep);
|
|
|
|
};
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, kCreateValue, resultType,
|
|
|
|
sizeOf(value));
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rewriter.notifyMatchFailure(op, "unsupported async type");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.set_available to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeSetAvailableOpLowering
|
|
|
|
: public OpConversionPattern<RuntimeSetAvailableOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(RuntimeSetAvailableOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Type operandType = op.operand().getType();
|
|
|
|
|
|
|
|
if (operandType.isa<TokenType>() || operandType.isa<ValueType>()) {
|
|
|
|
rewriter.create<CallOp>(op->getLoc(),
|
|
|
|
operandType.isa<TokenType>() ? kEmplaceToken
|
|
|
|
: kEmplaceValue,
|
|
|
|
TypeRange(), operands);
|
|
|
|
rewriter.eraseOp(op);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rewriter.notifyMatchFailure(op, "unsupported async type");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.await to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeAwaitOpLowering : public OpConversionPattern<RuntimeAwaitOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(RuntimeAwaitOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Type operandType = op.operand().getType();
|
|
|
|
|
|
|
|
StringRef apiFuncName;
|
|
|
|
if (operandType.isa<TokenType>())
|
|
|
|
apiFuncName = kAwaitToken;
|
|
|
|
else if (operandType.isa<ValueType>())
|
|
|
|
apiFuncName = kAwaitValue;
|
|
|
|
else if (operandType.isa<GroupType>())
|
|
|
|
apiFuncName = kAwaitGroup;
|
|
|
|
else
|
|
|
|
return rewriter.notifyMatchFailure(op, "unsupported async type");
|
|
|
|
|
|
|
|
rewriter.create<CallOp>(op->getLoc(), apiFuncName, TypeRange(), operands);
|
|
|
|
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
|
|
|
|
matchAndRewrite(RuntimeAwaitAndResumeOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Type operandType = op.operand().getType();
|
|
|
|
|
|
|
|
StringRef apiFuncName;
|
|
|
|
if (operandType.isa<TokenType>())
|
|
|
|
apiFuncName = kAwaitTokenAndExecute;
|
|
|
|
else if (operandType.isa<ValueType>())
|
|
|
|
apiFuncName = kAwaitValueAndExecute;
|
|
|
|
else if (operandType.isa<GroupType>())
|
|
|
|
apiFuncName = kAwaitAllAndExecute;
|
|
|
|
else
|
|
|
|
return rewriter.notifyMatchFailure(op, "unsupported async type");
|
|
|
|
|
|
|
|
Value operand = RuntimeAwaitAndResumeOpAdaptor(operands).operand();
|
|
|
|
Value handle = RuntimeAwaitAndResumeOpAdaptor(operands).handle();
|
|
|
|
|
|
|
|
// A pointer to coroutine resume intrinsic wrapper.
|
|
|
|
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(),
|
|
|
|
ValueRange({operand, handle, resumePtr.res()}));
|
|
|
|
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
|
|
|
|
matchAndRewrite(RuntimeResumeOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
// A pointer to coroutine resume intrinsic wrapper.
|
|
|
|
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.
|
|
|
|
auto coroHdl = RuntimeResumeOpAdaptor(operands).handle();
|
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, TypeRange(), kExecute,
|
|
|
|
ValueRange({coroHdl, resumePtr.res()}));
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Convert async.runtime.store to the corresponding runtime API call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RuntimeStoreOpLowering : public OpConversionPattern<RuntimeStoreOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(RuntimeStoreOp op, ArrayRef<Value> operands,
|
|
|
|
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());
|
|
|
|
auto storage = RuntimeStoreOpAdaptor(operands).storage();
|
|
|
|
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.
|
|
|
|
auto value = RuntimeStoreOpAdaptor(operands).value();
|
|
|
|
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
|
|
|
|
matchAndRewrite(RuntimeLoadOp op, ArrayRef<Value> operands,
|
|
|
|
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());
|
|
|
|
auto storage = RuntimeLoadOpAdaptor(operands).storage();
|
|
|
|
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
|
|
|
|
matchAndRewrite(RuntimeAddToGroupOp op, ArrayRef<Value> operands,
|
|
|
|
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.
|
|
|
|
rewriter.replaceOpWithNewOp<CallOp>(op, kAddTokenToGroup,
|
|
|
|
rewriter.getI64Type(), operands);
|
|
|
|
|
|
|
|
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-01-19 02:58:34 +08:00
|
|
|
matchAndRewrite(RefCountingOp op, ArrayRef<Value> operands,
|
2020-11-20 18:42:28 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-01-19 02:58:34 +08:00
|
|
|
auto count =
|
|
|
|
rewriter.create<ConstantOp>(op->getLoc(), rewriter.getI32Type(),
|
|
|
|
rewriter.getI32IntegerAttr(op.count()));
|
2020-11-20 18:42:28 +08:00
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
auto operand = typename RefCountingOp::Adaptor(operands).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-01-19 02:58:34 +08:00
|
|
|
matchAndRewrite(ReturnOp op, ArrayRef<Value> operands,
|
2020-11-13 19:01:52 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2021-01-19 02:58:34 +08:00
|
|
|
rewriter.replaceOpWithNewOp<ReturnOp>(op, operands);
|
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
|
|
|
|
|
|
|
// Add declarations for all functions required by the coroutines lowering.
|
|
|
|
addResumeFunction(module);
|
|
|
|
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;
|
|
|
|
OwningRewritePatternList patterns;
|
|
|
|
|
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.
|
2020-10-23 03:20:42 +08:00
|
|
|
populateFuncOpTypeConversionPattern(patterns, ctx, converter);
|
2020-12-24 21:08:09 +08:00
|
|
|
populateCallOpTypeConversionPattern(patterns, ctx, converter);
|
|
|
|
|
|
|
|
// Convert return operations inside async.execute regions.
|
|
|
|
patterns.insert<ReturnOpOpConversion>(converter, ctx);
|
|
|
|
|
2021-01-19 02:58:34 +08:00
|
|
|
// Lower async.runtime operations to the async runtime API calls.
|
|
|
|
patterns.insert<RuntimeSetAvailableOpLowering, RuntimeAwaitOpLowering,
|
|
|
|
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.
|
|
|
|
patterns.insert<RuntimeCreateOpLowering, RuntimeStoreOpLowering,
|
|
|
|
RuntimeLoadOpLowering>(llvmConverter, ctx);
|
|
|
|
|
|
|
|
// Lower async coroutine operations to LLVM coroutine intrinsics.
|
|
|
|
patterns.insert<CoroIdOpConversion, CoroBeginOpConversion,
|
|
|
|
CoroFreeOpConversion, CoroEndOpConversion,
|
|
|
|
CoroSaveOpConversion, CoroSuspendOpConversion>(converter,
|
|
|
|
ctx);
|
2020-10-23 03:20:42 +08:00
|
|
|
|
|
|
|
ConversionTarget target(*ctx);
|
2020-11-20 18:42:28 +08:00
|
|
|
target.addLegalOp<ConstantOp>();
|
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
|
|
|
|
matchAndRewrite(ExecuteOp op, ArrayRef<Value> operands,
|
|
|
|
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.
|
|
|
|
newOp->setOperands(operands);
|
|
|
|
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
|
|
|
|
matchAndRewrite(AwaitOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
rewriter.replaceOpWithNewOp<AwaitOp>(op, operands.front());
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dummy pattern to trigger the appropriate type conversion / materialization.
|
|
|
|
class ConvertYieldOpTypes : public OpConversionPattern<async::YieldOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(async::YieldOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
rewriter.replaceOpWithNewOp<async::YieldOp>(op, operands);
|
|
|
|
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(
|
|
|
|
MLIRContext *context, TypeConverter &typeConverter,
|
|
|
|
OwningRewritePatternList &patterns, ConversionTarget &target) {
|
|
|
|
typeConverter.addConversion([&](TokenType type) { return type; });
|
|
|
|
typeConverter.addConversion([&](ValueType type) {
|
|
|
|
return ValueType::get(typeConverter.convertType(type.getValueType()));
|
|
|
|
});
|
|
|
|
|
|
|
|
patterns
|
|
|
|
.insert<ConvertExecuteOpTypes, ConvertAwaitOpTypes, ConvertYieldOpTypes>(
|
|
|
|
typeConverter, context);
|
|
|
|
|
|
|
|
target.addDynamicallyLegalOp<AwaitOp, ExecuteOp, async::YieldOp>(
|
|
|
|
[&](Operation *op) { return typeConverter.isLegal(op); });
|
|
|
|
}
|