2020-05-18 13:01:54 -05:00
|
|
|
//===- ConvertLaunchFuncToGpuRuntimeCalls.cpp - MLIR GPU lowering passes --===//
|
2019-06-19 06:22:36 -07:00
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-06-19 06:22:36 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-19 06:22:36 -07:00
|
|
|
//
|
|
|
|
|
// This file implements a pass to convert gpu.launch_func op into a sequence of
|
2020-05-18 13:01:54 -05:00
|
|
|
// GPU runtime calls. As most of GPU runtimes does not have a stable published
|
|
|
|
|
// ABI, this pass uses a slim runtime layer that builds on top of the public
|
|
|
|
|
// API from GPU runtime headers.
|
2019-06-19 06:22:36 -07:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2020-05-18 13:01:54 -05:00
|
|
|
#include "mlir/Conversion/GPUCommon/GPUCommonPass.h"
|
2019-06-19 06:22:36 -07:00
|
|
|
|
2022-09-29 11:14:47 -04:00
|
|
|
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
|
2021-02-03 20:10:51 +01:00
|
|
|
#include "mlir/Conversion/AsyncToLLVM/AsyncToLLVM.h"
|
2022-02-06 15:10:03 -08:00
|
|
|
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
|
2022-03-01 14:53:41 -08:00
|
|
|
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
|
|
|
|
|
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
|
2021-07-08 18:35:18 +02:00
|
|
|
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
|
2021-07-07 11:45:27 +02:00
|
|
|
#include "mlir/Conversion/LLVMCommon/Pattern.h"
|
2021-07-08 18:35:18 +02:00
|
|
|
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
|
2021-03-18 12:59:49 -07:00
|
|
|
#include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h"
|
2021-02-24 15:02:30 +01:00
|
|
|
#include "mlir/Dialect/Async/IR/Async.h"
|
2022-06-09 21:33:41 +00:00
|
|
|
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
|
|
|
|
|
#include "mlir/Dialect/GPU/Transforms/Passes.h"
|
2019-08-19 11:00:47 -07:00
|
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
2023-01-17 23:20:11 -05:00
|
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
2019-06-19 06:22:36 -07:00
|
|
|
#include "mlir/IR/Attributes.h"
|
|
|
|
|
#include "mlir/IR/Builders.h"
|
2020-11-19 10:43:12 -08:00
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
2020-12-03 17:22:29 -08:00
|
|
|
#include "mlir/IR/BuiltinTypes.h"
|
2019-06-19 06:22:36 -07:00
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
#include "llvm/Support/Error.h"
|
2019-08-20 07:51:32 -07:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2019-06-19 06:22:36 -07:00
|
|
|
|
2022-08-31 10:16:29 +02:00
|
|
|
namespace mlir {
|
|
|
|
|
#define GEN_PASS_DEF_GPUTOLLVMCONVERSIONPASS
|
|
|
|
|
#include "mlir/Conversion/Passes.h.inc"
|
|
|
|
|
} // namespace mlir
|
|
|
|
|
|
2019-06-19 06:22:36 -07:00
|
|
|
using namespace mlir;
|
|
|
|
|
|
2020-05-18 13:01:54 -05:00
|
|
|
static constexpr const char *kGpuBinaryStorageSuffix = "_gpubin_cst";
|
2019-06-19 06:22:36 -07:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2022-08-30 22:20:36 +02:00
|
|
|
class GpuToLLVMConversionPass
|
2022-08-31 10:16:29 +02:00
|
|
|
: public impl::GpuToLLVMConversionPassBase<GpuToLLVMConversionPass> {
|
2020-08-01 15:06:25 +02:00
|
|
|
public:
|
2023-02-10 20:40:38 +01:00
|
|
|
using Base::Base;
|
2021-03-09 20:19:32 +01:00
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
// Run the dialect converter on the module.
|
|
|
|
|
void runOnOperation() override;
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-24 15:02:30 +01:00
|
|
|
struct FunctionCallBuilder {
|
2021-01-05 16:22:53 +01:00
|
|
|
FunctionCallBuilder(StringRef functionName, Type returnType,
|
|
|
|
|
ArrayRef<Type> argumentTypes)
|
2020-08-01 15:06:25 +02:00
|
|
|
: functionName(functionName),
|
2020-12-22 11:22:56 +01:00
|
|
|
functionType(LLVM::LLVMFunctionType::get(returnType, argumentTypes)) {}
|
2020-08-01 15:06:25 +02:00
|
|
|
LLVM::CallOp create(Location loc, OpBuilder &builder,
|
|
|
|
|
ArrayRef<Value> arguments) const;
|
|
|
|
|
|
|
|
|
|
StringRef functionName;
|
2020-12-22 11:22:21 +01:00
|
|
|
LLVM::LLVMFunctionType functionType;
|
2020-08-01 15:06:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename OpTy>
|
|
|
|
|
class ConvertOpToGpuRuntimeCallPattern : public ConvertOpToLLVMPattern<OpTy> {
|
|
|
|
|
public:
|
|
|
|
|
explicit ConvertOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToLLVMPattern<OpTy>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
2021-09-04 08:03:33 +02:00
|
|
|
Value getNumElements(ConversionPatternRewriter &rewriter, Location loc,
|
|
|
|
|
MemRefType type, MemRefDescriptor desc) const {
|
|
|
|
|
return type.hasStaticShape()
|
|
|
|
|
? ConvertToLLVMPattern::createIndexConstant(
|
|
|
|
|
rewriter, loc, type.getNumElements())
|
|
|
|
|
// For identity maps (verified by caller), the number of
|
|
|
|
|
// elements is stride[0] * size[0].
|
|
|
|
|
: rewriter.create<LLVM::MulOp>(loc,
|
|
|
|
|
desc.stride(rewriter, loc, 0),
|
|
|
|
|
desc.size(rewriter, loc, 0));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 12:34:26 +01:00
|
|
|
MLIRContext *context = &this->getTypeConverter()->getContext();
|
2020-08-01 15:06:25 +02:00
|
|
|
|
2021-01-05 16:22:53 +01:00
|
|
|
Type llvmVoidType = LLVM::LLVMVoidType::get(context);
|
2023-02-12 23:52:16 +01:00
|
|
|
LLVM::LLVMPointerType llvmPointerType =
|
2023-02-21 07:51:44 +01:00
|
|
|
this->getTypeConverter()->getPointerType(IntegerType::get(context, 8));
|
|
|
|
|
Type llvmPointerPointerType =
|
|
|
|
|
this->getTypeConverter()->getPointerType(llvmPointerType);
|
[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 16:19:04 +01:00
|
|
|
Type llvmInt8Type = IntegerType::get(context, 8);
|
|
|
|
|
Type llvmInt32Type = IntegerType::get(context, 32);
|
|
|
|
|
Type llvmInt64Type = IntegerType::get(context, 64);
|
|
|
|
|
Type llvmIntPtrType = IntegerType::get(
|
2020-12-03 12:34:26 +01:00
|
|
|
context, this->getTypeConverter()->getPointerBitwidth(0));
|
2020-08-01 15:06:25 +02:00
|
|
|
|
|
|
|
|
FunctionCallBuilder moduleLoadCallBuilder = {
|
|
|
|
|
"mgpuModuleLoad",
|
|
|
|
|
llvmPointerType /* void *module */,
|
|
|
|
|
{llvmPointerType /* void *cubin */}};
|
2020-10-29 08:17:27 +01:00
|
|
|
FunctionCallBuilder moduleUnloadCallBuilder = {
|
|
|
|
|
"mgpuModuleUnload", llvmVoidType, {llvmPointerType /* void *module */}};
|
2020-08-01 15:06:25 +02:00
|
|
|
FunctionCallBuilder moduleGetFunctionCallBuilder = {
|
|
|
|
|
"mgpuModuleGetFunction",
|
|
|
|
|
llvmPointerType /* void *function */,
|
|
|
|
|
{
|
|
|
|
|
llvmPointerType, /* void *module */
|
|
|
|
|
llvmPointerType /* char *name */
|
|
|
|
|
}};
|
|
|
|
|
FunctionCallBuilder launchKernelCallBuilder = {
|
|
|
|
|
"mgpuLaunchKernel",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{
|
|
|
|
|
llvmPointerType, /* void* f */
|
|
|
|
|
llvmIntPtrType, /* intptr_t gridXDim */
|
|
|
|
|
llvmIntPtrType, /* intptr_t gridyDim */
|
|
|
|
|
llvmIntPtrType, /* intptr_t gridZDim */
|
|
|
|
|
llvmIntPtrType, /* intptr_t blockXDim */
|
|
|
|
|
llvmIntPtrType, /* intptr_t blockYDim */
|
|
|
|
|
llvmIntPtrType, /* intptr_t blockZDim */
|
|
|
|
|
llvmInt32Type, /* unsigned int sharedMemBytes */
|
|
|
|
|
llvmPointerType, /* void *hstream */
|
|
|
|
|
llvmPointerPointerType, /* void **kernelParams */
|
|
|
|
|
llvmPointerPointerType /* void **extra */
|
|
|
|
|
}};
|
|
|
|
|
FunctionCallBuilder streamCreateCallBuilder = {
|
|
|
|
|
"mgpuStreamCreate", llvmPointerType /* void *stream */, {}};
|
2020-10-08 16:37:44 +02:00
|
|
|
FunctionCallBuilder streamDestroyCallBuilder = {
|
|
|
|
|
"mgpuStreamDestroy", llvmVoidType, {llvmPointerType /* void *stream */}};
|
2020-08-01 15:06:25 +02:00
|
|
|
FunctionCallBuilder streamSynchronizeCallBuilder = {
|
|
|
|
|
"mgpuStreamSynchronize",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmPointerType /* void *stream */}};
|
2020-10-08 16:37:44 +02:00
|
|
|
FunctionCallBuilder streamWaitEventCallBuilder = {
|
|
|
|
|
"mgpuStreamWaitEvent",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmPointerType /* void *stream */, llvmPointerType /* void *event */}};
|
|
|
|
|
FunctionCallBuilder eventCreateCallBuilder = {
|
|
|
|
|
"mgpuEventCreate", llvmPointerType /* void *event */, {}};
|
|
|
|
|
FunctionCallBuilder eventDestroyCallBuilder = {
|
|
|
|
|
"mgpuEventDestroy", llvmVoidType, {llvmPointerType /* void *event */}};
|
|
|
|
|
FunctionCallBuilder eventSynchronizeCallBuilder = {
|
|
|
|
|
"mgpuEventSynchronize",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmPointerType /* void *event */}};
|
|
|
|
|
FunctionCallBuilder eventRecordCallBuilder = {
|
|
|
|
|
"mgpuEventRecord",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmPointerType /* void *event */, llvmPointerType /* void *stream */}};
|
2020-08-10 10:13:57 +02:00
|
|
|
FunctionCallBuilder hostRegisterCallBuilder = {
|
|
|
|
|
"mgpuMemHostRegisterMemRef",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmIntPtrType /* intptr_t rank */,
|
|
|
|
|
llvmPointerType /* void *memrefDesc */,
|
|
|
|
|
llvmIntPtrType /* intptr_t elementSizeBytes */}};
|
2020-11-24 22:07:34 +01:00
|
|
|
FunctionCallBuilder allocCallBuilder = {
|
|
|
|
|
"mgpuMemAlloc",
|
|
|
|
|
llvmPointerType /* void * */,
|
|
|
|
|
{llvmIntPtrType /* intptr_t sizeBytes */,
|
|
|
|
|
llvmPointerType /* void *stream */}};
|
|
|
|
|
FunctionCallBuilder deallocCallBuilder = {
|
|
|
|
|
"mgpuMemFree",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmPointerType /* void *ptr */, llvmPointerType /* void *stream */}};
|
2020-12-22 17:42:59 +01:00
|
|
|
FunctionCallBuilder memcpyCallBuilder = {
|
|
|
|
|
"mgpuMemcpy",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmPointerType /* void *dst */, llvmPointerType /* void *src */,
|
|
|
|
|
llvmIntPtrType /* intptr_t sizeBytes */,
|
|
|
|
|
llvmPointerType /* void *stream */}};
|
2021-09-04 08:03:33 +02:00
|
|
|
FunctionCallBuilder memsetCallBuilder = {
|
|
|
|
|
"mgpuMemset32",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmPointerType /* void *dst */, llvmInt32Type /* unsigned int value */,
|
|
|
|
|
llvmIntPtrType /* intptr_t sizeBytes */,
|
|
|
|
|
llvmPointerType /* void *stream */}};
|
2022-02-15 20:23:44 +00:00
|
|
|
FunctionCallBuilder setDefaultDeviceCallBuilder = {
|
|
|
|
|
"mgpuSetDefaultDevice",
|
|
|
|
|
llvmVoidType,
|
|
|
|
|
{llvmInt32Type /* uint32_t devIndex */}};
|
2020-08-10 10:13:57 +02:00
|
|
|
};
|
|
|
|
|
|
2020-08-19 15:50:44 +02:00
|
|
|
/// A rewrite pattern to convert gpu.host_register operations into a GPU runtime
|
2020-08-10 10:13:57 +02:00
|
|
|
/// call. Currently it supports CUDA and ROCm (HIP).
|
|
|
|
|
class ConvertHostRegisterOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::HostRegisterOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertHostRegisterOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::HostRegisterOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::HostRegisterOp hostRegisterOp, OpAdaptor adaptor,
|
2020-08-10 10:13:57 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
2020-08-01 15:06:25 +02:00
|
|
|
};
|
|
|
|
|
|
2020-11-24 22:07:34 +01:00
|
|
|
/// A rewrite pattern to convert gpu.alloc operations into a GPU runtime
|
|
|
|
|
/// call. Currently it supports CUDA and ROCm (HIP).
|
|
|
|
|
class ConvertAllocOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::AllocOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertAllocOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::AllocOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::AllocOp allocOp, OpAdaptor adaptor,
|
2020-11-24 22:07:34 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// A rewrite pattern to convert gpu.dealloc operations into a GPU runtime
|
|
|
|
|
/// call. Currently it supports CUDA and ROCm (HIP).
|
|
|
|
|
class ConvertDeallocOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::DeallocOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertDeallocOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::DeallocOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::DeallocOp deallocOp, OpAdaptor adaptor,
|
2020-11-24 22:07:34 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-24 15:02:30 +01:00
|
|
|
class ConvertAsyncYieldToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<async::YieldOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertAsyncYieldToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<async::YieldOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(async::YieldOp yieldOp, OpAdaptor adaptor,
|
2021-02-24 15:02:30 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-21 08:24:53 +02:00
|
|
|
/// A rewrite pattern to convert gpu.wait operations into a GPU runtime
|
|
|
|
|
/// call. Currently it supports CUDA and ROCm (HIP).
|
|
|
|
|
class ConvertWaitOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::WaitOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertWaitOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::WaitOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::WaitOp waitOp, OpAdaptor adaptor,
|
2020-10-21 08:24:53 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// A rewrite pattern to convert gpu.wait async operations into a GPU runtime
|
|
|
|
|
/// call. Currently it supports CUDA and ROCm (HIP).
|
|
|
|
|
class ConvertWaitAsyncOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::WaitOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertWaitAsyncOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::WaitOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::WaitOp waitOp, OpAdaptor adaptor,
|
2020-10-21 08:24:53 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
/// A rewrite patter to convert gpu.launch_func operations into a sequence of
|
|
|
|
|
/// GPU runtime calls. Currently it supports CUDA and ROCm (HIP).
|
2019-06-19 06:22:36 -07:00
|
|
|
///
|
|
|
|
|
/// In essence, a gpu.launch_func operations gets compiled into the following
|
|
|
|
|
/// sequence of runtime calls:
|
|
|
|
|
///
|
2020-05-18 13:01:54 -05:00
|
|
|
/// * moduleLoad -- loads the module given the cubin / hsaco data
|
|
|
|
|
/// * moduleGetFunction -- gets a handle to the actual kernel function
|
|
|
|
|
/// * getStreamHelper -- initializes a new compute stream on GPU
|
|
|
|
|
/// * launchKernel -- launches the kernel on a stream
|
|
|
|
|
/// * streamSynchronize -- waits for operations on the stream to finish
|
2019-06-19 06:22:36 -07:00
|
|
|
///
|
|
|
|
|
/// Intermediate data structures are allocated on the stack.
|
2020-08-01 15:06:25 +02:00
|
|
|
class ConvertLaunchFuncOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::LaunchFuncOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertLaunchFuncOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter,
|
2022-07-11 18:29:01 +00:00
|
|
|
StringRef gpuBinaryAnnotation,
|
|
|
|
|
bool kernelBarePtrCallConv)
|
2020-08-01 15:06:25 +02:00
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::LaunchFuncOp>(typeConverter),
|
2022-07-11 18:29:01 +00:00
|
|
|
gpuBinaryAnnotation(gpuBinaryAnnotation),
|
|
|
|
|
kernelBarePtrCallConv(kernelBarePtrCallConv) {}
|
2019-06-19 06:22:36 -07:00
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
private:
|
2021-09-24 17:51:20 +00:00
|
|
|
Value generateParamsArray(gpu::LaunchFuncOp launchOp, OpAdaptor adaptor,
|
|
|
|
|
OpBuilder &builder) const;
|
2020-04-16 13:14:43 +02:00
|
|
|
Value generateKernelNameConstant(StringRef moduleName, StringRef name,
|
2020-08-01 15:06:25 +02:00
|
|
|
Location loc, OpBuilder &builder) const;
|
2019-06-19 06:22:36 -07:00
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::LaunchFuncOp launchOp, OpAdaptor adaptor,
|
2020-08-01 15:06:25 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
[mlir][gpu] Introduce mlir-rocm-runner.
Summary:
`mlir-rocm-runner` is introduced in this commit to execute GPU modules on ROCm
platform. A small wrapper to encapsulate ROCm's HIP runtime API is also inside
the commit.
Due to behavior of ROCm, raw pointers inside memrefs passed to `gpu.launch`
must be modified on the host side to properly capture the pointer values
addressable on the GPU.
LLVM MC is used to assemble AMD GCN ISA coming out from
`ConvertGPUKernelToBlobPass` to binary form, and LLD is used to produce a shared
ELF object which could be loaded by ROCm HIP runtime.
gfx900 is the default target be used right now, although it could be altered via
an option in `mlir-rocm-runner`. Future revisions may consider using ROCm Agent
Enumerator to detect the right target on the system.
Notice AMDGPU Code Object V2 is used in this revision. Future enhancements may
upgrade to AMDGPU Code Object V3.
Bitcode libraries in ROCm-Device-Libs, which implements math routines exposed in
`rocdl` dialect are not yet linked, and is left as a TODO in the logic.
Reviewers: herhut
Subscribers: mgorny, tpr, dexonsmith, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, csigg, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, frgossen, Kayjukh, jurahul, llvm-commits
Tags: #mlir, #llvm
Differential Revision: https://reviews.llvm.org/D80676
2020-05-20 16:07:49 -05:00
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
llvm::SmallString<32> gpuBinaryAnnotation;
|
2022-07-11 18:29:01 +00:00
|
|
|
bool kernelBarePtrCallConv;
|
2020-08-01 15:06:25 +02:00
|
|
|
};
|
2019-06-19 06:22:36 -07:00
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
class EraseGpuModuleOpPattern : public OpRewritePattern<gpu::GPUModuleOp> {
|
|
|
|
|
using OpRewritePattern<gpu::GPUModuleOp>::OpRewritePattern;
|
2019-10-08 04:29:58 -07:00
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
LogicalResult matchAndRewrite(gpu::GPUModuleOp op,
|
|
|
|
|
PatternRewriter &rewriter) const override {
|
2019-10-08 04:29:58 -07:00
|
|
|
// GPU kernel modules are no longer necessary since we have a global
|
2020-05-18 13:01:54 -05:00
|
|
|
// constant with the CUBIN, or HSACO data.
|
2020-08-01 15:06:25 +02:00
|
|
|
rewriter.eraseOp(op);
|
|
|
|
|
return success();
|
2019-06-19 06:22:36 -07:00
|
|
|
}
|
|
|
|
|
};
|
2020-12-22 17:42:59 +01:00
|
|
|
|
|
|
|
|
/// A rewrite pattern to convert gpu.memcpy operations into a GPU runtime
|
|
|
|
|
/// call. Currently it supports CUDA and ROCm (HIP).
|
|
|
|
|
class ConvertMemcpyOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::MemcpyOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertMemcpyOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::MemcpyOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::MemcpyOp memcpyOp, OpAdaptor adaptor,
|
2020-12-22 17:42:59 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
2021-09-04 08:03:33 +02:00
|
|
|
|
|
|
|
|
/// A rewrite pattern to convert gpu.memset operations into a GPU runtime
|
|
|
|
|
/// call. Currently it supports CUDA and ROCm (HIP).
|
|
|
|
|
class ConvertMemsetOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::MemsetOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertMemsetOpToGpuRuntimeCallPattern(LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::MemsetOp>(typeConverter) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogicalResult
|
2021-09-24 17:51:20 +00:00
|
|
|
matchAndRewrite(gpu::MemsetOp memsetOp, OpAdaptor adaptor,
|
2021-09-04 08:03:33 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
2022-02-15 20:23:44 +00:00
|
|
|
|
|
|
|
|
/// A rewrite pattern to convert gpu.set_default_device to a GPU runtime call.
|
|
|
|
|
/// Currently supports CUDA and ROCm (HIP)
|
|
|
|
|
class ConvertSetDefaultDeviceOpToGpuRuntimeCallPattern
|
|
|
|
|
: public ConvertOpToGpuRuntimeCallPattern<gpu::SetDefaultDeviceOp> {
|
|
|
|
|
public:
|
|
|
|
|
ConvertSetDefaultDeviceOpToGpuRuntimeCallPattern(
|
|
|
|
|
LLVMTypeConverter &typeConverter)
|
|
|
|
|
: ConvertOpToGpuRuntimeCallPattern<gpu::SetDefaultDeviceOp>(
|
|
|
|
|
typeConverter) {}
|
|
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
|
matchAndRewrite(gpu::SetDefaultDeviceOp op, OpAdaptor adaptor,
|
|
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
|
};
|
2020-08-01 15:06:25 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
2022-08-30 22:20:36 +02:00
|
|
|
void GpuToLLVMConversionPass::runOnOperation() {
|
2023-02-21 07:51:44 +01:00
|
|
|
LowerToLLVMOptions options(&getContext());
|
|
|
|
|
options.useOpaquePointers = useOpaquePointers;
|
|
|
|
|
|
|
|
|
|
LLVMTypeConverter converter(&getContext(), options);
|
2021-03-22 16:58:34 -07:00
|
|
|
RewritePatternSet patterns(&getContext());
|
2021-02-03 20:10:51 +01:00
|
|
|
LLVMConversionTarget target(getContext());
|
|
|
|
|
|
2021-06-14 09:14:30 +02:00
|
|
|
target.addIllegalDialect<gpu::GPUDialect>();
|
|
|
|
|
|
2022-09-29 11:14:47 -04:00
|
|
|
mlir::arith::populateArithToLLVMConversionPatterns(converter, patterns);
|
2022-02-06 15:10:03 -08:00
|
|
|
mlir::cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);
|
2021-03-18 12:59:49 -07:00
|
|
|
populateVectorToLLVMConversionPatterns(converter, patterns);
|
2023-01-24 13:05:47 +00:00
|
|
|
populateFinalizeMemRefToLLVMConversionPatterns(converter, patterns);
|
2022-03-01 14:53:41 -08:00
|
|
|
populateFuncToLLVMConversionPatterns(converter, patterns);
|
2021-03-20 16:29:41 -07:00
|
|
|
populateAsyncStructuralTypeConversionsAndLegality(converter, patterns,
|
|
|
|
|
target);
|
2022-07-11 18:29:01 +00:00
|
|
|
populateGpuToLLVMConversionPatterns(converter, patterns, gpuBinaryAnnotation,
|
|
|
|
|
kernelBarePtrCallConv);
|
2020-08-01 15:06:25 +02:00
|
|
|
|
2020-10-26 17:25:01 -07:00
|
|
|
if (failed(
|
|
|
|
|
applyPartialConversion(getOperation(), target, std::move(patterns))))
|
2020-08-01 15:06:25 +02:00
|
|
|
signalPassFailure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LLVM::CallOp FunctionCallBuilder::create(Location loc, OpBuilder &builder,
|
|
|
|
|
ArrayRef<Value> arguments) const {
|
|
|
|
|
auto module = builder.getBlock()->getParent()->getParentOfType<ModuleOp>();
|
|
|
|
|
auto function = [&] {
|
|
|
|
|
if (auto function = module.lookupSymbol<LLVM::LLVMFuncOp>(functionName))
|
|
|
|
|
return function;
|
2021-03-11 23:58:02 +00:00
|
|
|
return OpBuilder::atBlockEnd(module.getBody())
|
2020-08-01 15:06:25 +02:00
|
|
|
.create<LLVM::LLVMFuncOp>(loc, functionName, functionType);
|
|
|
|
|
}();
|
2021-07-28 10:23:06 +02:00
|
|
|
return builder.create<LLVM::CallOp>(loc, function, arguments);
|
2019-06-19 06:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-24 22:07:34 +01:00
|
|
|
// Returns whether all operands are of LLVM type.
|
|
|
|
|
static LogicalResult areAllLLVMTypes(Operation *op, ValueRange operands,
|
|
|
|
|
ConversionPatternRewriter &rewriter) {
|
|
|
|
|
if (!llvm::all_of(operands, [](Value value) {
|
2021-01-05 16:22:53 +01:00
|
|
|
return LLVM::isCompatibleType(value.getType());
|
2020-11-24 22:07:34 +01:00
|
|
|
}))
|
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
|
op, "Cannot convert if operands aren't of LLVM type.");
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static LogicalResult
|
|
|
|
|
isAsyncWithOneDependency(ConversionPatternRewriter &rewriter,
|
|
|
|
|
gpu::AsyncOpInterface op) {
|
|
|
|
|
if (op.getAsyncDependencies().size() != 1)
|
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
|
op, "Can only convert with exactly one async dependency.");
|
|
|
|
|
|
|
|
|
|
if (!op.getAsyncToken())
|
|
|
|
|
return rewriter.notifyMatchFailure(op, "Can convert only async version.");
|
|
|
|
|
|
|
|
|
|
return success();
|
2020-08-10 10:13:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LogicalResult ConvertHostRegisterOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::HostRegisterOp hostRegisterOp, OpAdaptor adaptor,
|
2020-08-10 10:13:57 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2020-11-27 21:09:13 +01:00
|
|
|
auto *op = hostRegisterOp.getOperation();
|
2021-09-24 17:51:20 +00:00
|
|
|
if (failed(areAllLLVMTypes(op, adaptor.getOperands(), rewriter)))
|
2020-11-24 22:07:34 +01:00
|
|
|
return failure();
|
2020-08-10 10:13:57 +02:00
|
|
|
|
|
|
|
|
Location loc = op->getLoc();
|
|
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
auto memRefType = hostRegisterOp.getValue().getType();
|
2020-08-10 10:13:57 +02:00
|
|
|
auto elementType = memRefType.cast<UnrankedMemRefType>().getElementType();
|
|
|
|
|
auto elementSize = getSizeInBytes(loc, elementType, rewriter);
|
|
|
|
|
|
2021-09-24 17:51:20 +00:00
|
|
|
auto arguments = getTypeConverter()->promoteOperands(
|
|
|
|
|
loc, op->getOperands(), adaptor.getOperands(), rewriter);
|
2020-08-10 10:13:57 +02:00
|
|
|
arguments.push_back(elementSize);
|
|
|
|
|
hostRegisterCallBuilder.create(loc, rewriter, arguments);
|
|
|
|
|
|
|
|
|
|
rewriter.eraseOp(op);
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 22:07:34 +01:00
|
|
|
LogicalResult ConvertAllocOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::AllocOp allocOp, OpAdaptor adaptor,
|
2020-11-24 22:07:34 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-09-09 00:04:01 +02:00
|
|
|
if (adaptor.getHostShared())
|
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
|
allocOp, "host_shared allocation is not supported");
|
|
|
|
|
|
2020-11-24 22:07:34 +01:00
|
|
|
MemRefType memRefType = allocOp.getType();
|
|
|
|
|
|
2021-09-24 17:51:20 +00:00
|
|
|
if (failed(areAllLLVMTypes(allocOp, adaptor.getOperands(), rewriter)) ||
|
2020-12-23 11:37:49 +01:00
|
|
|
!isConvertibleAndHasIdentityMaps(memRefType) ||
|
2020-11-28 13:46:43 +01:00
|
|
|
failed(isAsyncWithOneDependency(rewriter, allocOp)))
|
2020-11-24 22:07:34 +01:00
|
|
|
return failure();
|
|
|
|
|
|
2020-11-28 13:46:43 +01:00
|
|
|
auto loc = allocOp.getLoc();
|
2020-11-24 22:07:34 +01:00
|
|
|
|
|
|
|
|
// Get shape of the memref as values: static sizes are constant
|
|
|
|
|
// values and dynamic sizes are passed to 'alloc' as operands.
|
|
|
|
|
SmallVector<Value, 4> shape;
|
|
|
|
|
SmallVector<Value, 4> strides;
|
|
|
|
|
Value sizeBytes;
|
2022-09-30 12:30:41 -07:00
|
|
|
getMemRefDescriptorSizes(loc, memRefType, adaptor.getDynamicSizes(), rewriter,
|
2021-01-11 14:46:26 +01:00
|
|
|
shape, strides, sizeBytes);
|
2020-11-24 22:07:34 +01:00
|
|
|
|
|
|
|
|
// Allocate the underlying buffer and store a pointer to it in the MemRef
|
|
|
|
|
// descriptor.
|
|
|
|
|
Type elementPtrType = this->getElementPtrType(memRefType);
|
2022-09-30 12:30:41 -07:00
|
|
|
auto stream = adaptor.getAsyncDependencies().front();
|
2020-11-24 22:07:34 +01:00
|
|
|
Value allocatedPtr =
|
2022-08-11 00:34:02 -04:00
|
|
|
allocCallBuilder.create(loc, rewriter, {sizeBytes, stream}).getResult();
|
2023-02-21 07:51:44 +01:00
|
|
|
if (!getTypeConverter()->useOpaquePointers())
|
|
|
|
|
allocatedPtr =
|
|
|
|
|
rewriter.create<LLVM::BitcastOp>(loc, elementPtrType, allocatedPtr);
|
2020-11-24 22:07:34 +01:00
|
|
|
|
|
|
|
|
// No alignment.
|
|
|
|
|
Value alignedPtr = allocatedPtr;
|
|
|
|
|
|
|
|
|
|
// Create the MemRef descriptor.
|
|
|
|
|
auto memRefDescriptor = this->createMemRefDescriptor(
|
|
|
|
|
loc, memRefType, allocatedPtr, alignedPtr, shape, strides, rewriter);
|
|
|
|
|
|
2020-11-28 13:46:43 +01:00
|
|
|
rewriter.replaceOp(allocOp, {memRefDescriptor, stream});
|
2020-11-24 22:07:34 +01:00
|
|
|
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LogicalResult ConvertDeallocOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::DeallocOp deallocOp, OpAdaptor adaptor,
|
2020-11-24 22:07:34 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2021-09-24 17:51:20 +00:00
|
|
|
if (failed(areAllLLVMTypes(deallocOp, adaptor.getOperands(), rewriter)) ||
|
2020-11-28 13:46:43 +01:00
|
|
|
failed(isAsyncWithOneDependency(rewriter, deallocOp)))
|
2020-11-24 22:07:34 +01:00
|
|
|
return failure();
|
|
|
|
|
|
2020-11-28 13:46:43 +01:00
|
|
|
Location loc = deallocOp.getLoc();
|
2020-11-24 22:07:34 +01:00
|
|
|
|
|
|
|
|
Value pointer =
|
2022-09-30 12:30:41 -07:00
|
|
|
MemRefDescriptor(adaptor.getMemref()).allocatedPtr(rewriter, loc);
|
2023-02-21 07:51:44 +01:00
|
|
|
if (!getTypeConverter()->useOpaquePointers())
|
|
|
|
|
pointer = rewriter.create<LLVM::BitcastOp>(loc, llvmPointerType, pointer);
|
2022-09-30 12:30:41 -07:00
|
|
|
Value stream = adaptor.getAsyncDependencies().front();
|
2023-02-21 07:51:44 +01:00
|
|
|
deallocCallBuilder.create(loc, rewriter, {pointer, stream});
|
2020-11-24 22:07:34 +01:00
|
|
|
|
2020-11-28 13:46:43 +01:00
|
|
|
rewriter.replaceOp(deallocOp, {stream});
|
2020-11-24 22:07:34 +01:00
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 15:02:30 +01:00
|
|
|
static bool isGpuAsyncTokenType(Value value) {
|
|
|
|
|
return value.getType().isa<gpu::AsyncTokenType>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Converts !gpu.async.token operands of `async.yield` to runtime calls. The
|
|
|
|
|
// !gpu.async.token are lowered to stream within the async.execute region, but
|
|
|
|
|
// are passed as events between them. For each !gpu.async.token operand, we
|
|
|
|
|
// create an event and record it on the stream.
|
|
|
|
|
LogicalResult ConvertAsyncYieldToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
async::YieldOp yieldOp, OpAdaptor adaptor,
|
2021-02-24 15:02:30 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-10-25 18:29:53 -07:00
|
|
|
if (llvm::none_of(yieldOp.getOperands(), isGpuAsyncTokenType))
|
2021-02-24 15:02:30 +01:00
|
|
|
return rewriter.notifyMatchFailure(yieldOp, "no gpu async token operand");
|
|
|
|
|
|
|
|
|
|
Location loc = yieldOp.getLoc();
|
2021-09-24 17:51:20 +00:00
|
|
|
SmallVector<Value, 4> newOperands(adaptor.getOperands());
|
2021-02-24 15:02:30 +01:00
|
|
|
llvm::SmallDenseSet<Value> streams;
|
|
|
|
|
for (auto &operand : yieldOp->getOpOperands()) {
|
|
|
|
|
if (!isGpuAsyncTokenType(operand.get()))
|
|
|
|
|
continue;
|
|
|
|
|
auto idx = operand.getOperandNumber();
|
2021-09-24 17:51:20 +00:00
|
|
|
auto stream = adaptor.getOperands()[idx];
|
2022-08-11 00:34:02 -04:00
|
|
|
auto event = eventCreateCallBuilder.create(loc, rewriter, {}).getResult();
|
2021-02-24 15:02:30 +01:00
|
|
|
eventRecordCallBuilder.create(loc, rewriter, {event, stream});
|
|
|
|
|
newOperands[idx] = event;
|
|
|
|
|
streams.insert(stream);
|
|
|
|
|
}
|
|
|
|
|
for (auto stream : streams)
|
|
|
|
|
streamDestroyCallBuilder.create(loc, rewriter, {stream});
|
|
|
|
|
|
|
|
|
|
rewriter.updateRootInPlace(yieldOp,
|
|
|
|
|
[&] { yieldOp->setOperands(newOperands); });
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns whether `value` is the result of an LLVM::CallOp to `functionName`.
|
|
|
|
|
static bool isDefinedByCallTo(Value value, StringRef functionName) {
|
|
|
|
|
assert(value.getType().isa<LLVM::LLVMPointerType>());
|
|
|
|
|
if (auto defOp = value.getDefiningOp<LLVM::CallOp>())
|
2021-10-24 18:36:33 -07:00
|
|
|
return defOp.getCallee()->equals(functionName);
|
2021-02-24 15:02:30 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Converts `gpu.wait` to runtime calls. The converted op synchronizes the host
|
|
|
|
|
// with the stream/event operands. The operands are destroyed. That is, it
|
|
|
|
|
// assumes that it is not used afterwards or elsewhere. Otherwise we will get a
|
|
|
|
|
// runtime error. Eventually, we should guarantee this property.
|
2020-10-21 08:24:53 +02:00
|
|
|
LogicalResult ConvertWaitOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::WaitOp waitOp, OpAdaptor adaptor,
|
2020-10-21 08:24:53 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-09-30 12:30:41 -07:00
|
|
|
if (waitOp.getAsyncToken())
|
2020-11-27 21:09:13 +01:00
|
|
|
return rewriter.notifyMatchFailure(waitOp, "Cannot convert async op.");
|
2020-10-21 08:24:53 +02:00
|
|
|
|
2020-11-27 21:09:13 +01:00
|
|
|
Location loc = waitOp.getLoc();
|
2020-10-21 08:24:53 +02:00
|
|
|
|
2021-09-24 17:51:20 +00:00
|
|
|
for (auto operand : adaptor.getOperands()) {
|
2021-02-24 15:02:30 +01:00
|
|
|
if (isDefinedByCallTo(operand, streamCreateCallBuilder.functionName)) {
|
|
|
|
|
// The converted operand's definition created a stream.
|
|
|
|
|
streamSynchronizeCallBuilder.create(loc, rewriter, {operand});
|
|
|
|
|
streamDestroyCallBuilder.create(loc, rewriter, {operand});
|
|
|
|
|
} else {
|
|
|
|
|
// Otherwise the converted operand is an event. This assumes that we use
|
|
|
|
|
// events in control flow code as well.
|
|
|
|
|
eventSynchronizeCallBuilder.create(loc, rewriter, {operand});
|
|
|
|
|
eventDestroyCallBuilder.create(loc, rewriter, {operand});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-21 08:24:53 +02:00
|
|
|
|
2020-11-27 21:09:13 +01:00
|
|
|
rewriter.eraseOp(waitOp);
|
2020-10-21 08:24:53 +02:00
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 15:02:30 +01:00
|
|
|
// Converts `gpu.wait async` to runtime calls. The converted op creates a new
|
|
|
|
|
// stream that is synchronized with stream/event operands. The operands are
|
|
|
|
|
// destroyed. That is, it assumes that it is not used afterwards or elsewhere.
|
|
|
|
|
// Otherwise we will get a runtime error. Eventually, we should guarantee this
|
|
|
|
|
// property.
|
2020-10-21 08:24:53 +02:00
|
|
|
LogicalResult ConvertWaitAsyncOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::WaitOp waitOp, OpAdaptor adaptor,
|
2020-10-21 08:24:53 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-09-30 12:30:41 -07:00
|
|
|
if (!waitOp.getAsyncToken())
|
2020-11-27 21:09:13 +01:00
|
|
|
return rewriter.notifyMatchFailure(waitOp, "Can only convert async op.");
|
2020-10-21 08:24:53 +02:00
|
|
|
|
2020-11-27 21:09:13 +01:00
|
|
|
Location loc = waitOp.getLoc();
|
2020-10-21 08:24:53 +02:00
|
|
|
|
|
|
|
|
auto insertionPoint = rewriter.saveInsertionPoint();
|
|
|
|
|
SmallVector<Value, 1> events;
|
2021-09-24 17:51:20 +00:00
|
|
|
for (auto pair :
|
2022-09-30 12:30:41 -07:00
|
|
|
llvm::zip(waitOp.getAsyncDependencies(), adaptor.getOperands())) {
|
2021-02-24 15:02:30 +01:00
|
|
|
auto operand = std::get<1>(pair);
|
|
|
|
|
if (isDefinedByCallTo(operand, streamCreateCallBuilder.functionName)) {
|
|
|
|
|
// The converted operand's definition created a stream. Insert an event
|
|
|
|
|
// into the stream just after the last use of the original token operand.
|
|
|
|
|
auto *defOp = std::get<0>(pair).getDefiningOp();
|
2020-10-21 08:24:53 +02:00
|
|
|
rewriter.setInsertionPointAfter(defOp);
|
2022-08-11 00:34:02 -04:00
|
|
|
auto event = eventCreateCallBuilder.create(loc, rewriter, {}).getResult();
|
2021-02-24 15:02:30 +01:00
|
|
|
eventRecordCallBuilder.create(loc, rewriter, {event, operand});
|
|
|
|
|
events.push_back(event);
|
2020-10-21 08:24:53 +02:00
|
|
|
} else {
|
2021-02-24 15:02:30 +01:00
|
|
|
// Otherwise the converted operand is an event. This assumes that we use
|
|
|
|
|
// events in control flow code as well.
|
|
|
|
|
events.push_back(operand);
|
2020-10-21 08:24:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rewriter.restoreInsertionPoint(insertionPoint);
|
2022-08-11 00:34:02 -04:00
|
|
|
auto stream = streamCreateCallBuilder.create(loc, rewriter, {}).getResult();
|
2020-10-21 08:24:53 +02:00
|
|
|
for (auto event : events)
|
|
|
|
|
streamWaitEventCallBuilder.create(loc, rewriter, {stream, event});
|
|
|
|
|
for (auto event : events)
|
|
|
|
|
eventDestroyCallBuilder.create(loc, rewriter, {event});
|
2020-11-27 21:09:13 +01:00
|
|
|
rewriter.replaceOp(waitOp, {stream});
|
2020-10-21 08:24:53 +02:00
|
|
|
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 16:40:21 +02:00
|
|
|
// Creates a struct containing all kernel parameters on the stack and returns
|
|
|
|
|
// an array of type-erased pointers to the fields of the struct. The array can
|
|
|
|
|
// then be passed to the CUDA / ROCm (HIP) kernel launch calls.
|
2019-06-19 06:22:36 -07:00
|
|
|
// The generated code is essentially as follows:
|
|
|
|
|
//
|
2020-08-01 16:40:21 +02:00
|
|
|
// %struct = alloca(sizeof(struct { Parameters... }))
|
|
|
|
|
// %array = alloca(NumParameters * sizeof(void *))
|
|
|
|
|
// for (i : [0, NumParameters))
|
|
|
|
|
// %fieldPtr = llvm.getelementptr %struct[0, i]
|
|
|
|
|
// llvm.store parameters[i], %fieldPtr
|
|
|
|
|
// %elementPtr = llvm.getelementptr %array[i]
|
|
|
|
|
// llvm.store %fieldPtr, %elementPtr
|
2019-06-19 06:22:36 -07:00
|
|
|
// return %array
|
2020-08-01 15:06:25 +02:00
|
|
|
Value ConvertLaunchFuncOpToGpuRuntimeCallPattern::generateParamsArray(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::LaunchFuncOp launchOp, OpAdaptor adaptor, OpBuilder &builder) const {
|
2020-08-01 16:40:21 +02:00
|
|
|
auto loc = launchOp.getLoc();
|
2019-09-27 09:55:38 -07:00
|
|
|
auto numKernelOperands = launchOp.getNumKernelOperands();
|
2022-07-11 18:29:01 +00:00
|
|
|
SmallVector<Value, 4> arguments;
|
|
|
|
|
if (kernelBarePtrCallConv) {
|
|
|
|
|
// Hack the bare pointer value on just for the argument promotion
|
|
|
|
|
LLVMTypeConverter *converter = getTypeConverter();
|
|
|
|
|
LowerToLLVMOptions options = converter->getOptions();
|
|
|
|
|
LowerToLLVMOptions overrideToMatchKernelOpts = options;
|
|
|
|
|
overrideToMatchKernelOpts.useBarePtrCallConv = true;
|
|
|
|
|
converter->dangerousSetOptions(overrideToMatchKernelOpts);
|
|
|
|
|
arguments = converter->promoteOperands(
|
|
|
|
|
loc, launchOp.getOperands().take_back(numKernelOperands),
|
|
|
|
|
adaptor.getOperands().take_back(numKernelOperands), builder);
|
|
|
|
|
converter->dangerousSetOptions(options);
|
|
|
|
|
} else {
|
|
|
|
|
arguments = getTypeConverter()->promoteOperands(
|
|
|
|
|
loc, launchOp.getOperands().take_back(numKernelOperands),
|
|
|
|
|
adaptor.getOperands().take_back(numKernelOperands), builder);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 16:40:21 +02:00
|
|
|
auto numArguments = arguments.size();
|
2021-01-05 16:22:53 +01:00
|
|
|
SmallVector<Type, 4> argumentTypes;
|
2020-08-01 16:40:21 +02:00
|
|
|
argumentTypes.reserve(numArguments);
|
|
|
|
|
for (auto argument : arguments)
|
2021-01-05 16:22:53 +01:00
|
|
|
argumentTypes.push_back(argument.getType());
|
2020-12-22 11:22:56 +01:00
|
|
|
auto structType = LLVM::LLVMStructType::getNewIdentified(context, StringRef(),
|
|
|
|
|
argumentTypes);
|
2022-08-09 14:40:07 -04:00
|
|
|
auto one = builder.create<LLVM::ConstantOp>(loc, llvmInt32Type, 1);
|
2020-08-01 16:40:21 +02:00
|
|
|
auto structPtr = builder.create<LLVM::AllocaOp>(
|
2023-02-21 07:51:44 +01:00
|
|
|
loc, getTypeConverter()->getPointerType(structType), structType, one,
|
|
|
|
|
/*alignment=*/0);
|
2022-08-09 14:40:07 -04:00
|
|
|
auto arraySize =
|
|
|
|
|
builder.create<LLVM::ConstantOp>(loc, llvmInt32Type, numArguments);
|
2023-02-21 07:51:44 +01:00
|
|
|
auto arrayPtr = builder.create<LLVM::AllocaOp>(
|
|
|
|
|
loc, llvmPointerPointerType, llvmPointerType, arraySize, /*alignment=*/0);
|
2022-01-02 22:02:14 +00:00
|
|
|
for (const auto &en : llvm::enumerate(arguments)) {
|
2023-02-21 07:51:44 +01:00
|
|
|
Value fieldPtr = builder.create<LLVM::GEPOp>(
|
|
|
|
|
loc, getTypeConverter()->getPointerType(argumentTypes[en.index()]),
|
|
|
|
|
argumentTypes[en.index()], structPtr,
|
2022-07-29 01:00:22 +02:00
|
|
|
ArrayRef<LLVM::GEPArg>{0, en.index()});
|
2020-08-01 16:40:21 +02:00
|
|
|
builder.create<LLVM::StoreOp>(loc, en.value(), fieldPtr);
|
2023-02-21 07:51:44 +01:00
|
|
|
auto elementPtr = builder.create<LLVM::GEPOp>(
|
|
|
|
|
loc, llvmPointerPointerType, llvmPointerType, arrayPtr,
|
|
|
|
|
ArrayRef<LLVM::GEPArg>{en.index()});
|
|
|
|
|
if (!getTypeConverter()->useOpaquePointers())
|
|
|
|
|
fieldPtr =
|
|
|
|
|
builder.create<LLVM::BitcastOp>(loc, llvmPointerType, fieldPtr);
|
|
|
|
|
builder.create<LLVM::StoreOp>(loc, fieldPtr, elementPtr);
|
2019-06-19 06:22:36 -07:00
|
|
|
}
|
2020-08-01 16:40:21 +02:00
|
|
|
return arrayPtr;
|
2019-06-19 06:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-20 07:51:32 -07:00
|
|
|
// Generates an LLVM IR dialect global that contains the name of the given
|
|
|
|
|
// kernel function as a C string, and returns a pointer to its beginning.
|
|
|
|
|
// The code is essentially:
|
2019-06-19 06:22:36 -07:00
|
|
|
//
|
2019-08-20 07:51:32 -07:00
|
|
|
// llvm.global constant @kernel_name("function_name\00")
|
|
|
|
|
// func(...) {
|
|
|
|
|
// %0 = llvm.addressof @kernel_name
|
|
|
|
|
// %1 = llvm.constant (0 : index)
|
|
|
|
|
// %2 = llvm.getelementptr %0[%1, %1] : !llvm<"i8*">
|
|
|
|
|
// }
|
2020-08-01 15:06:25 +02:00
|
|
|
Value ConvertLaunchFuncOpToGpuRuntimeCallPattern::generateKernelNameConstant(
|
|
|
|
|
StringRef moduleName, StringRef name, Location loc,
|
|
|
|
|
OpBuilder &builder) const {
|
2019-08-20 07:51:32 -07:00
|
|
|
// Make sure the trailing zero is included in the constant.
|
2019-10-08 05:11:00 -07:00
|
|
|
std::vector<char> kernelName(name.begin(), name.end());
|
2019-08-20 07:51:32 -07:00
|
|
|
kernelName.push_back('\0');
|
|
|
|
|
|
2020-04-16 13:14:43 +02:00
|
|
|
std::string globalName =
|
|
|
|
|
std::string(llvm::formatv("{0}_{1}_kernel_name", moduleName, name));
|
2019-08-20 07:51:32 -07:00
|
|
|
return LLVM::createGlobalString(
|
|
|
|
|
loc, builder, globalName, StringRef(kernelName.data(), kernelName.size()),
|
2023-02-21 07:51:44 +01:00
|
|
|
LLVM::Linkage::Internal, getTypeConverter()->useOpaquePointers());
|
2019-06-19 06:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Emits LLVM IR to launch a kernel function. Expects the module that contains
|
2020-05-18 13:01:54 -05:00
|
|
|
// the compiled kernel function as a cubin in the 'nvvm.cubin' attribute, or a
|
|
|
|
|
// hsaco in the 'rocdl.hsaco' attribute of the kernel function in the IR.
|
2019-06-19 06:22:36 -07:00
|
|
|
//
|
2020-05-18 13:01:54 -05:00
|
|
|
// %0 = call %binarygetter
|
2020-07-28 16:29:29 +02:00
|
|
|
// %1 = call %moduleLoad(%0)
|
|
|
|
|
// %2 = <see generateKernelNameConstant>
|
|
|
|
|
// %3 = call %moduleGetFunction(%1, %2)
|
|
|
|
|
// %4 = call %streamCreate()
|
2020-08-01 15:06:25 +02:00
|
|
|
// %5 = <see generateParamsArray>
|
2020-07-28 16:29:29 +02:00
|
|
|
// call %launchKernel(%3, <launchOp operands 0..5>, 0, %4, %5, nullptr)
|
|
|
|
|
// call %streamSynchronize(%4)
|
2020-10-29 17:58:48 +01:00
|
|
|
// call %streamDestroy(%4)
|
|
|
|
|
// call %moduleUnload(%1)
|
|
|
|
|
//
|
|
|
|
|
// If the op is async, the stream corresponds to the (single) async dependency
|
|
|
|
|
// as well as the async token the op produces.
|
2020-08-01 15:06:25 +02:00
|
|
|
LogicalResult ConvertLaunchFuncOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::LaunchFuncOp launchOp, OpAdaptor adaptor,
|
2020-08-01 15:06:25 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2021-09-24 17:51:20 +00:00
|
|
|
if (failed(areAllLLVMTypes(launchOp, adaptor.getOperands(), rewriter)))
|
2020-11-24 22:07:34 +01:00
|
|
|
return failure();
|
2020-08-01 16:40:21 +02:00
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
if (launchOp.getAsyncDependencies().size() > 1)
|
2020-10-29 17:58:48 +01:00
|
|
|
return rewriter.notifyMatchFailure(
|
2020-11-27 21:09:13 +01:00
|
|
|
launchOp, "Cannot convert with more than one async dependency.");
|
2020-10-29 17:58:48 +01:00
|
|
|
|
|
|
|
|
// Fail when the synchronous version of the op has async dependencies. The
|
|
|
|
|
// lowering destroys the stream, and we do not want to check that there is no
|
|
|
|
|
// use of the stream after this op.
|
2022-09-30 12:30:41 -07:00
|
|
|
if (!launchOp.getAsyncToken() && !launchOp.getAsyncDependencies().empty())
|
2020-10-29 17:58:48 +01:00
|
|
|
return rewriter.notifyMatchFailure(
|
2020-11-27 21:09:13 +01:00
|
|
|
launchOp, "Cannot convert non-async op with async dependencies.");
|
2020-10-29 17:58:48 +01:00
|
|
|
|
2020-08-01 16:40:21 +02:00
|
|
|
Location loc = launchOp.getLoc();
|
2019-06-19 06:22:36 -07:00
|
|
|
|
2019-10-08 05:03:09 -07:00
|
|
|
// Create an LLVM global with CUBIN extracted from the kernel annotation and
|
|
|
|
|
// obtain a pointer to the first byte in it.
|
2020-08-01 16:40:21 +02:00
|
|
|
auto kernelModule = SymbolTable::lookupNearestSymbolFrom<gpu::GPUModuleOp>(
|
|
|
|
|
launchOp, launchOp.getKernelModuleName());
|
2019-10-08 04:29:58 -07:00
|
|
|
assert(kernelModule && "expected a kernel module");
|
|
|
|
|
|
2020-12-09 11:50:18 +01:00
|
|
|
auto binaryAttr =
|
|
|
|
|
kernelModule->getAttrOfType<StringAttr>(gpuBinaryAnnotation);
|
2020-05-18 13:01:54 -05:00
|
|
|
if (!binaryAttr) {
|
2019-10-08 05:11:00 -07:00
|
|
|
kernelModule.emitOpError()
|
2020-05-18 13:01:54 -05:00
|
|
|
<< "missing " << gpuBinaryAnnotation << " attribute";
|
2020-08-01 15:06:25 +02:00
|
|
|
return failure();
|
2019-06-19 06:22:36 -07:00
|
|
|
}
|
2019-10-10 01:33:33 -07:00
|
|
|
|
Create a gpu.module operation for the GPU Dialect.
Summary:
This is based on the use of code constantly checking for an attribute on
a model and instead represents the distinct operaion with a different
op. Instead, this op can be used to provide better filtering.
Reverts "Revert "[mlir] Create a gpu.module operation for the GPU Dialect.""
This reverts commit ac446302ca4145cdc89f377c0c364c29ee303be5 after
fixing internal Google issues.
This additionally updates ROCDL lowering to use the new gpu.module.
Reviewers: herhut, mravishankar, antiagainst, nicolasvasilache
Subscribers: jholewinski, mgorny, mehdi_amini, jpienaar, burmako, shauheen, csigg, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, llvm-commits, mravishankar, rriddle, antiagainst, bkramer
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72921
2020-01-17 15:18:23 +01:00
|
|
|
SmallString<128> nameBuffer(kernelModule.getName());
|
2020-05-18 13:01:54 -05:00
|
|
|
nameBuffer.append(kGpuBinaryStorageSuffix);
|
2023-02-21 07:51:44 +01:00
|
|
|
Value data = LLVM::createGlobalString(
|
|
|
|
|
loc, rewriter, nameBuffer.str(), binaryAttr.getValue(),
|
|
|
|
|
LLVM::Linkage::Internal, getTypeConverter()->useOpaquePointers());
|
2019-10-08 05:03:09 -07:00
|
|
|
|
2020-08-01 15:06:25 +02:00
|
|
|
auto module = moduleLoadCallBuilder.create(loc, rewriter, data);
|
2019-06-19 06:22:36 -07:00
|
|
|
// Get the function from the module. The name corresponds to the name of
|
|
|
|
|
// the kernel function.
|
2020-04-21 10:16:41 +00:00
|
|
|
auto kernelName = generateKernelNameConstant(
|
2021-08-29 14:22:24 -07:00
|
|
|
launchOp.getKernelModuleName().getValue(),
|
|
|
|
|
launchOp.getKernelName().getValue(), loc, rewriter);
|
2020-08-01 15:06:25 +02:00
|
|
|
auto function = moduleGetFunctionCallBuilder.create(
|
2022-08-11 00:34:02 -04:00
|
|
|
loc, rewriter, {module.getResult(), kernelName});
|
2022-08-16 21:17:03 -07:00
|
|
|
Value zero = rewriter.create<LLVM::ConstantOp>(loc, llvmInt32Type, 0);
|
2020-10-29 17:58:48 +01:00
|
|
|
Value stream =
|
2022-09-30 12:30:41 -07:00
|
|
|
adaptor.getAsyncDependencies().empty()
|
2022-08-11 00:34:02 -04:00
|
|
|
? streamCreateCallBuilder.create(loc, rewriter, {}).getResult()
|
2022-09-30 12:30:41 -07:00
|
|
|
: adaptor.getAsyncDependencies().front();
|
2020-08-01 16:40:21 +02:00
|
|
|
// Create array of pointers to kernel arguments.
|
2021-09-24 17:51:20 +00:00
|
|
|
auto kernelParams = generateParamsArray(launchOp, adaptor, rewriter);
|
2020-08-01 16:40:21 +02:00
|
|
|
auto nullpointer = rewriter.create<LLVM::NullOp>(loc, llvmPointerPointerType);
|
2022-09-30 12:30:41 -07:00
|
|
|
Value dynamicSharedMemorySize = launchOp.getDynamicSharedMemorySize()
|
|
|
|
|
? launchOp.getDynamicSharedMemorySize()
|
2021-09-14 14:13:21 +05:30
|
|
|
: zero;
|
|
|
|
|
launchKernelCallBuilder.create(
|
|
|
|
|
loc, rewriter,
|
2022-09-30 12:30:41 -07:00
|
|
|
{function.getResult(), adaptor.getGridSizeX(), adaptor.getGridSizeY(),
|
|
|
|
|
adaptor.getGridSizeZ(), adaptor.getBlockSizeX(), adaptor.getBlockSizeY(),
|
|
|
|
|
adaptor.getBlockSizeZ(), dynamicSharedMemorySize, stream, kernelParams,
|
2021-09-14 14:13:21 +05:30
|
|
|
/*extra=*/nullpointer});
|
2020-10-29 17:58:48 +01:00
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
if (launchOp.getAsyncToken()) {
|
2020-10-29 17:58:48 +01:00
|
|
|
// Async launch: make dependent ops use the same stream.
|
2020-11-27 21:09:13 +01:00
|
|
|
rewriter.replaceOp(launchOp, {stream});
|
2020-10-29 17:58:48 +01:00
|
|
|
} else {
|
|
|
|
|
// Synchronize with host and destroy stream. This must be the stream created
|
|
|
|
|
// above (with no other uses) because we check that the synchronous version
|
|
|
|
|
// does not have any async dependencies.
|
|
|
|
|
streamSynchronizeCallBuilder.create(loc, rewriter, stream);
|
|
|
|
|
streamDestroyCallBuilder.create(loc, rewriter, stream);
|
2020-11-27 21:09:13 +01:00
|
|
|
rewriter.eraseOp(launchOp);
|
2020-10-29 17:58:48 +01:00
|
|
|
}
|
2022-08-11 00:34:02 -04:00
|
|
|
moduleUnloadCallBuilder.create(loc, rewriter, module.getResult());
|
2020-08-01 15:06:25 +02:00
|
|
|
|
|
|
|
|
return success();
|
2019-06-19 06:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:52:16 +01:00
|
|
|
static Value bitAndAddrspaceCast(Location loc,
|
|
|
|
|
ConversionPatternRewriter &rewriter,
|
|
|
|
|
LLVM::LLVMPointerType destinationType,
|
|
|
|
|
Value sourcePtr,
|
|
|
|
|
LLVMTypeConverter &typeConverter) {
|
|
|
|
|
auto sourceTy = sourcePtr.getType().cast<LLVM::LLVMPointerType>();
|
|
|
|
|
if (destinationType.getAddressSpace() != sourceTy.getAddressSpace())
|
|
|
|
|
sourcePtr = rewriter.create<LLVM::AddrSpaceCastOp>(
|
|
|
|
|
loc,
|
|
|
|
|
typeConverter.getPointerType(sourceTy.getElementType(),
|
|
|
|
|
destinationType.getAddressSpace()),
|
|
|
|
|
sourcePtr);
|
|
|
|
|
|
2023-02-21 07:51:44 +01:00
|
|
|
if (typeConverter.useOpaquePointers())
|
|
|
|
|
return sourcePtr;
|
|
|
|
|
|
2023-02-12 23:52:16 +01:00
|
|
|
return rewriter.create<LLVM::BitcastOp>(loc, destinationType, sourcePtr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 17:42:59 +01:00
|
|
|
LogicalResult ConvertMemcpyOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::MemcpyOp memcpyOp, OpAdaptor adaptor,
|
2020-12-22 17:42:59 +01:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-09-30 12:30:41 -07:00
|
|
|
auto memRefType = memcpyOp.getSrc().getType().cast<MemRefType>();
|
2020-12-22 17:42:59 +01:00
|
|
|
|
2021-09-24 17:51:20 +00:00
|
|
|
if (failed(areAllLLVMTypes(memcpyOp, adaptor.getOperands(), rewriter)) ||
|
2020-12-23 11:37:49 +01:00
|
|
|
!isConvertibleAndHasIdentityMaps(memRefType) ||
|
2020-12-22 17:42:59 +01:00
|
|
|
failed(isAsyncWithOneDependency(rewriter, memcpyOp)))
|
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
|
|
auto loc = memcpyOp.getLoc();
|
|
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
MemRefDescriptor srcDesc(adaptor.getSrc());
|
2021-09-04 08:03:33 +02:00
|
|
|
Value numElements = getNumElements(rewriter, loc, memRefType, srcDesc);
|
2020-12-22 17:42:59 +01:00
|
|
|
|
|
|
|
|
Type elementPtrType = getElementPtrType(memRefType);
|
|
|
|
|
Value nullPtr = rewriter.create<LLVM::NullOp>(loc, elementPtrType);
|
2023-02-21 07:51:44 +01:00
|
|
|
Value gepPtr = rewriter.create<LLVM::GEPOp>(
|
|
|
|
|
loc, elementPtrType,
|
|
|
|
|
typeConverter->convertType(memRefType.getElementType()), nullPtr,
|
|
|
|
|
numElements);
|
2020-12-22 17:42:59 +01:00
|
|
|
auto sizeBytes =
|
|
|
|
|
rewriter.create<LLVM::PtrToIntOp>(loc, getIndexType(), gepPtr);
|
|
|
|
|
|
2023-02-12 23:52:16 +01:00
|
|
|
auto src = bitAndAddrspaceCast(loc, rewriter, llvmPointerType,
|
|
|
|
|
srcDesc.alignedPtr(rewriter, loc),
|
|
|
|
|
*getTypeConverter());
|
|
|
|
|
auto dst = bitAndAddrspaceCast(
|
|
|
|
|
loc, rewriter, llvmPointerType,
|
|
|
|
|
MemRefDescriptor(adaptor.getDst()).alignedPtr(rewriter, loc),
|
|
|
|
|
*getTypeConverter());
|
2020-12-22 17:42:59 +01:00
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
auto stream = adaptor.getAsyncDependencies().front();
|
2020-12-22 17:42:59 +01:00
|
|
|
memcpyCallBuilder.create(loc, rewriter, {dst, src, sizeBytes, stream});
|
|
|
|
|
|
|
|
|
|
rewriter.replaceOp(memcpyOp, {stream});
|
|
|
|
|
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-04 08:03:33 +02:00
|
|
|
LogicalResult ConvertMemsetOpToGpuRuntimeCallPattern::matchAndRewrite(
|
2021-09-24 17:51:20 +00:00
|
|
|
gpu::MemsetOp memsetOp, OpAdaptor adaptor,
|
2021-09-04 08:03:33 +02:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-09-30 12:30:41 -07:00
|
|
|
auto memRefType = memsetOp.getDst().getType().cast<MemRefType>();
|
2021-09-04 08:03:33 +02:00
|
|
|
|
2021-09-24 17:51:20 +00:00
|
|
|
if (failed(areAllLLVMTypes(memsetOp, adaptor.getOperands(), rewriter)) ||
|
2021-09-04 08:03:33 +02:00
|
|
|
!isConvertibleAndHasIdentityMaps(memRefType) ||
|
|
|
|
|
failed(isAsyncWithOneDependency(rewriter, memsetOp)))
|
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
|
|
auto loc = memsetOp.getLoc();
|
|
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
Type valueType = adaptor.getValue().getType();
|
2021-09-04 08:03:33 +02:00
|
|
|
if (!valueType.isIntOrFloat() || valueType.getIntOrFloatBitWidth() != 32) {
|
|
|
|
|
return rewriter.notifyMatchFailure(memsetOp,
|
|
|
|
|
"value must be a 32 bit scalar");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
MemRefDescriptor dstDesc(adaptor.getDst());
|
2021-09-04 08:03:33 +02:00
|
|
|
Value numElements = getNumElements(rewriter, loc, memRefType, dstDesc);
|
|
|
|
|
|
|
|
|
|
auto value =
|
2022-09-30 12:30:41 -07:00
|
|
|
rewriter.create<LLVM::BitcastOp>(loc, llvmInt32Type, adaptor.getValue());
|
2023-02-12 23:52:16 +01:00
|
|
|
auto dst = bitAndAddrspaceCast(loc, rewriter, llvmPointerType,
|
|
|
|
|
dstDesc.alignedPtr(rewriter, loc),
|
|
|
|
|
*getTypeConverter());
|
2021-09-04 08:03:33 +02:00
|
|
|
|
2022-09-30 12:30:41 -07:00
|
|
|
auto stream = adaptor.getAsyncDependencies().front();
|
2021-09-04 08:03:33 +02:00
|
|
|
memsetCallBuilder.create(loc, rewriter, {dst, value, numElements, stream});
|
|
|
|
|
|
|
|
|
|
rewriter.replaceOp(memsetOp, {stream});
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 20:23:44 +00:00
|
|
|
LogicalResult ConvertSetDefaultDeviceOpToGpuRuntimeCallPattern::matchAndRewrite(
|
|
|
|
|
gpu::SetDefaultDeviceOp op, OpAdaptor adaptor,
|
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
|
Location loc = op.getLoc();
|
2022-09-30 12:30:41 -07:00
|
|
|
setDefaultDeviceCallBuilder.create(loc, rewriter, {adaptor.getDevIndex()});
|
2022-02-15 20:23:44 +00:00
|
|
|
rewriter.replaceOp(op, {});
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:31:47 -07:00
|
|
|
void mlir::populateGpuToLLVMConversionPatterns(LLVMTypeConverter &converter,
|
|
|
|
|
RewritePatternSet &patterns,
|
2022-07-11 18:29:01 +00:00
|
|
|
StringRef gpuBinaryAnnotation,
|
|
|
|
|
bool kernelBarePtrCallConv) {
|
2023-02-21 07:51:44 +01:00
|
|
|
converter.addConversion([&converter](gpu::AsyncTokenType type) -> Type {
|
|
|
|
|
return converter.getPointerType(
|
|
|
|
|
IntegerType::get(&converter.getContext(), 8));
|
|
|
|
|
});
|
2021-07-28 22:31:26 +03:00
|
|
|
patterns.add<ConvertAllocOpToGpuRuntimeCallPattern,
|
|
|
|
|
ConvertDeallocOpToGpuRuntimeCallPattern,
|
|
|
|
|
ConvertHostRegisterOpToGpuRuntimeCallPattern,
|
|
|
|
|
ConvertMemcpyOpToGpuRuntimeCallPattern,
|
2021-09-04 08:03:33 +02:00
|
|
|
ConvertMemsetOpToGpuRuntimeCallPattern,
|
2022-02-15 20:23:44 +00:00
|
|
|
ConvertSetDefaultDeviceOpToGpuRuntimeCallPattern,
|
2021-07-28 22:31:26 +03:00
|
|
|
ConvertWaitAsyncOpToGpuRuntimeCallPattern,
|
|
|
|
|
ConvertWaitOpToGpuRuntimeCallPattern,
|
|
|
|
|
ConvertAsyncYieldToGpuRuntimeCallPattern>(converter);
|
2022-07-11 18:29:01 +00:00
|
|
|
patterns.add<ConvertLaunchFuncOpToGpuRuntimeCallPattern>(
|
|
|
|
|
converter, gpuBinaryAnnotation, kernelBarePtrCallConv);
|
2021-07-28 22:31:26 +03:00
|
|
|
patterns.add<EraseGpuModuleOpPattern>(&converter.getContext());
|
|
|
|
|
}
|