2020-10-23 22:33:53 +08:00
|
|
|
//===- ConvertLaunchFuncToLLVMCalls.cpp - MLIR GPU launch to LLVM pass ----===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements passes to convert `gpu.launch_func` op into a sequence
|
|
|
|
// of LLVM calls that emulate the host and device sides.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
2020-12-24 03:32:31 +08:00
|
|
|
#include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVM.h"
|
|
|
|
#include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVMPass.h"
|
2020-10-23 22:33:53 +08:00
|
|
|
#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h"
|
|
|
|
#include "mlir/Dialect/GPU/GPUDialect.h"
|
|
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
2020-12-17 23:55:45 +08:00
|
|
|
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
|
2020-10-23 22:33:53 +08:00
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
2020-11-20 02:43:12 +08:00
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
2020-10-23 22:33:53 +08:00
|
|
|
#include "mlir/IR/SymbolTable.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2021-04-21 22:54:44 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2020-10-23 22:33:53 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
static constexpr const char kSPIRVModule[] = "__spv__";
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Returns the string name of the `DescriptorSet` decoration.
|
|
|
|
static std::string descriptorSetName() {
|
|
|
|
return llvm::convertToSnakeFromCamelCase(
|
|
|
|
stringifyDecoration(spirv::Decoration::DescriptorSet));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the string name of the `Binding` decoration.
|
|
|
|
static std::string bindingName() {
|
|
|
|
return llvm::convertToSnakeFromCamelCase(
|
|
|
|
stringifyDecoration(spirv::Decoration::Binding));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates the index of the kernel's operand that is represented by the
|
|
|
|
/// given global variable with the `bind` attribute. We assume that the index of
|
|
|
|
/// each kernel's operand is mapped to (descriptorSet, binding) by the map:
|
|
|
|
/// i -> (0, i)
|
|
|
|
/// which is implemented under `LowerABIAttributesPass`.
|
|
|
|
static unsigned calculateGlobalIndex(spirv::GlobalVariableOp op) {
|
2020-12-09 18:50:18 +08:00
|
|
|
IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName());
|
2020-10-23 22:33:53 +08:00
|
|
|
return binding.getInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copies the given number of bytes from src to dst pointers.
|
|
|
|
static void copy(Location loc, Value dst, Value src, Value size,
|
|
|
|
OpBuilder &builder) {
|
|
|
|
MLIRContext *context = builder.getContext();
|
[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 llvmI1Type = IntegerType::get(context, 1);
|
2020-10-23 22:33:53 +08:00
|
|
|
Value isVolatile = builder.create<LLVM::ConstantOp>(
|
|
|
|
loc, llvmI1Type, builder.getBoolAttr(false));
|
|
|
|
builder.create<LLVM::MemcpyOp>(loc, dst, src, size, isVolatile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encodes the binding and descriptor set numbers into a new symbolic name.
|
|
|
|
/// The name is specified by
|
|
|
|
/// {kernel_module_name}_{variable_name}_descriptor_set{ds}_binding{b}
|
|
|
|
/// to avoid symbolic conflicts, where 'ds' and 'b' are descriptor set and
|
|
|
|
/// binding numbers.
|
|
|
|
static std::string
|
|
|
|
createGlobalVariableWithBindName(spirv::GlobalVariableOp op,
|
|
|
|
StringRef kernelModuleName) {
|
|
|
|
IntegerAttr descriptorSet =
|
2020-12-09 18:50:18 +08:00
|
|
|
op->getAttrOfType<IntegerAttr>(descriptorSetName());
|
|
|
|
IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName());
|
2020-10-23 22:33:53 +08:00
|
|
|
return llvm::formatv("{0}_{1}_descriptor_set{2}_binding{3}",
|
|
|
|
kernelModuleName.str(), op.sym_name().str(),
|
|
|
|
std::to_string(descriptorSet.getInt()),
|
|
|
|
std::to_string(binding.getInt()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the given global variable has both a descriptor set number
|
|
|
|
/// and a binding number.
|
|
|
|
static bool hasDescriptorSetAndBinding(spirv::GlobalVariableOp op) {
|
|
|
|
IntegerAttr descriptorSet =
|
2020-12-09 18:50:18 +08:00
|
|
|
op->getAttrOfType<IntegerAttr>(descriptorSetName());
|
|
|
|
IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName());
|
2020-10-23 22:33:53 +08:00
|
|
|
return descriptorSet && binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fills `globalVariableMap` with SPIR-V global variables that represent kernel
|
|
|
|
/// arguments from the given SPIR-V module. We assume that the module contains a
|
2021-03-05 05:17:12 +08:00
|
|
|
/// single entry point function. Hence, all `spv.GlobalVariable`s with a bind
|
2020-10-23 22:33:53 +08:00
|
|
|
/// attribute are kernel arguments.
|
|
|
|
static LogicalResult getKernelGlobalVariables(
|
|
|
|
spirv::ModuleOp module,
|
|
|
|
DenseMap<uint32_t, spirv::GlobalVariableOp> &globalVariableMap) {
|
|
|
|
auto entryPoints = module.getOps<spirv::EntryPointOp>();
|
|
|
|
if (!llvm::hasSingleElement(entryPoints)) {
|
|
|
|
return module.emitError(
|
|
|
|
"The module must contain exactly one entry point function");
|
|
|
|
}
|
|
|
|
auto globalVariables = module.getOps<spirv::GlobalVariableOp>();
|
|
|
|
for (auto globalOp : globalVariables) {
|
|
|
|
if (hasDescriptorSetAndBinding(globalOp))
|
|
|
|
globalVariableMap[calculateGlobalIndex(globalOp)] = globalOp;
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encodes the SPIR-V module's symbolic name into the name of the entry point
|
|
|
|
/// function.
|
|
|
|
static LogicalResult encodeKernelName(spirv::ModuleOp module) {
|
|
|
|
StringRef spvModuleName = module.sym_name().getValue();
|
|
|
|
// We already know that the module contains exactly one entry point function
|
|
|
|
// based on `getKernelGlobalVariables()` call. Update this function's name
|
|
|
|
// to:
|
|
|
|
// {spv_module_name}_{function_name}
|
|
|
|
auto entryPoint = *module.getOps<spirv::EntryPointOp>().begin();
|
|
|
|
StringRef funcName = entryPoint.fn();
|
|
|
|
auto funcOp = module.lookupSymbol<spirv::FuncOp>(funcName);
|
|
|
|
std::string newFuncName = spvModuleName.str() + "_" + funcName.str();
|
|
|
|
if (failed(SymbolTable::replaceAllSymbolUses(funcOp, newFuncName, module)))
|
|
|
|
return failure();
|
|
|
|
SymbolTable::setSymbolName(funcOp, newFuncName);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Conversion patterns
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Structure to group information about the variables being copied.
|
|
|
|
struct CopyInfo {
|
|
|
|
Value dst;
|
|
|
|
Value src;
|
|
|
|
Value size;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// This pattern emulates a call to the kernel in LLVM dialect. For that, we
|
|
|
|
/// copy the data to the global variable (emulating device side), call the
|
|
|
|
/// kernel as a normal void LLVM function, and copy the data back (emulating the
|
|
|
|
/// host side).
|
|
|
|
class GPULaunchLowering : public ConvertOpToLLVMPattern<gpu::LaunchFuncOp> {
|
|
|
|
using ConvertOpToLLVMPattern<gpu::LaunchFuncOp>::ConvertOpToLLVMPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
2020-11-28 04:09:13 +08:00
|
|
|
matchAndRewrite(gpu::LaunchFuncOp launchOp, ArrayRef<Value> operands,
|
2020-10-23 22:33:53 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2020-11-28 04:09:13 +08:00
|
|
|
auto *op = launchOp.getOperation();
|
2020-10-23 22:33:53 +08:00
|
|
|
MLIRContext *context = rewriter.getContext();
|
2020-12-09 18:50:18 +08:00
|
|
|
auto module = launchOp->getParentOfType<ModuleOp>();
|
2020-10-23 22:33:53 +08:00
|
|
|
|
|
|
|
// Get the SPIR-V module that represents the gpu kernel module. The module
|
|
|
|
// is named:
|
|
|
|
// __spv__{kernel_module_name}
|
|
|
|
// based on GPU to SPIR-V conversion.
|
|
|
|
StringRef kernelModuleName = launchOp.getKernelModuleName();
|
|
|
|
std::string spvModuleName = kSPIRVModule + kernelModuleName.str();
|
|
|
|
auto spvModule = module.lookupSymbol<spirv::ModuleOp>(spvModuleName);
|
|
|
|
if (!spvModule) {
|
|
|
|
return launchOp.emitOpError("SPIR-V kernel module '")
|
|
|
|
<< spvModuleName << "' is not found";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare kernel function in the main module so that it later can be linked
|
|
|
|
// with its definition from the kernel module. We know that the kernel
|
|
|
|
// function would have no arguments and the data is passed via global
|
|
|
|
// variables. The name of the kernel will be
|
|
|
|
// {spv_module_name}_{kernel_function_name}
|
|
|
|
// to avoid symbolic name conflicts.
|
|
|
|
StringRef kernelFuncName = launchOp.getKernelName();
|
|
|
|
std::string newKernelFuncName = spvModuleName + "_" + kernelFuncName.str();
|
|
|
|
auto kernelFunc = module.lookupSymbol<LLVM::LLVMFuncOp>(newKernelFuncName);
|
|
|
|
if (!kernelFunc) {
|
|
|
|
OpBuilder::InsertionGuard guard(rewriter);
|
|
|
|
rewriter.setInsertionPointToStart(module.getBody());
|
|
|
|
kernelFunc = rewriter.create<LLVM::LLVMFuncOp>(
|
|
|
|
rewriter.getUnknownLoc(), newKernelFuncName,
|
2020-12-22 18:22:56 +08:00
|
|
|
LLVM::LLVMFunctionType::get(LLVM::LLVMVoidType::get(context),
|
2021-01-05 23:22:53 +08:00
|
|
|
ArrayRef<Type>()));
|
2020-10-23 22:33:53 +08:00
|
|
|
rewriter.setInsertionPoint(launchOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all global variables associated with the kernel operands.
|
|
|
|
DenseMap<uint32_t, spirv::GlobalVariableOp> globalVariableMap;
|
|
|
|
if (failed(getKernelGlobalVariables(spvModule, globalVariableMap)))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Traverse kernel operands that were converted to MemRefDescriptors. For
|
|
|
|
// each operand, create a global variable and copy data from operand to it.
|
|
|
|
Location loc = launchOp.getLoc();
|
|
|
|
SmallVector<CopyInfo, 4> copyInfo;
|
|
|
|
auto numKernelOperands = launchOp.getNumKernelOperands();
|
|
|
|
auto kernelOperands = operands.take_back(numKernelOperands);
|
|
|
|
for (auto operand : llvm::enumerate(kernelOperands)) {
|
2021-01-08 01:09:48 +08:00
|
|
|
// Check if the kernel's operand is a ranked memref.
|
2020-10-23 22:33:53 +08:00
|
|
|
auto memRefType = launchOp.getKernelOperand(operand.index())
|
|
|
|
.getType()
|
|
|
|
.dyn_cast<MemRefType>();
|
|
|
|
if (!memRefType)
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Calculate the size of the memref and get the pointer to the allocated
|
|
|
|
// buffer.
|
|
|
|
SmallVector<Value, 4> sizes;
|
2020-11-18 01:00:55 +08:00
|
|
|
SmallVector<Value, 4> strides;
|
|
|
|
Value sizeBytes;
|
2021-01-11 21:46:26 +08:00
|
|
|
getMemRefDescriptorSizes(loc, memRefType, {}, rewriter, sizes, strides,
|
|
|
|
sizeBytes);
|
2020-10-23 22:33:53 +08:00
|
|
|
MemRefDescriptor descriptor(operand.value());
|
|
|
|
Value src = descriptor.allocatedPtr(rewriter, loc);
|
|
|
|
|
|
|
|
// Get the global variable in the SPIR-V module that is associated with
|
|
|
|
// the kernel operand. Construct its new name and create a corresponding
|
|
|
|
// LLVM dialect global variable.
|
|
|
|
spirv::GlobalVariableOp spirvGlobal = globalVariableMap[operand.index()];
|
|
|
|
auto pointeeType =
|
|
|
|
spirvGlobal.type().cast<spirv::PointerType>().getPointeeType();
|
2020-12-03 19:34:26 +08:00
|
|
|
auto dstGlobalType = typeConverter->convertType(pointeeType);
|
2020-10-23 22:33:53 +08:00
|
|
|
if (!dstGlobalType)
|
|
|
|
return failure();
|
|
|
|
std::string name =
|
|
|
|
createGlobalVariableWithBindName(spirvGlobal, spvModuleName);
|
|
|
|
// Check if this variable has already been created.
|
|
|
|
auto dstGlobal = module.lookupSymbol<LLVM::GlobalOp>(name);
|
|
|
|
if (!dstGlobal) {
|
|
|
|
OpBuilder::InsertionGuard guard(rewriter);
|
|
|
|
rewriter.setInsertionPointToStart(module.getBody());
|
|
|
|
dstGlobal = rewriter.create<LLVM::GlobalOp>(
|
2021-01-05 23:22:53 +08:00
|
|
|
loc, dstGlobalType,
|
2020-10-23 22:33:53 +08:00
|
|
|
/*isConstant=*/false, LLVM::Linkage::Linkonce, name, Attribute());
|
|
|
|
rewriter.setInsertionPoint(launchOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the data from src operand pointer to dst global variable. Save
|
|
|
|
// src, dst and size so that we can copy data back after emulating the
|
|
|
|
// kernel call.
|
|
|
|
Value dst = rewriter.create<LLVM::AddressOfOp>(loc, dstGlobal);
|
2020-11-18 01:00:55 +08:00
|
|
|
copy(loc, dst, src, sizeBytes, rewriter);
|
2020-10-23 22:33:53 +08:00
|
|
|
|
|
|
|
CopyInfo info;
|
|
|
|
info.dst = dst;
|
|
|
|
info.src = src;
|
2020-11-18 01:00:55 +08:00
|
|
|
info.size = sizeBytes;
|
2020-10-23 22:33:53 +08:00
|
|
|
copyInfo.push_back(info);
|
|
|
|
}
|
|
|
|
// Create a call to the kernel and copy the data back.
|
|
|
|
rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, kernelFunc,
|
|
|
|
ArrayRef<Value>());
|
|
|
|
for (CopyInfo info : copyInfo)
|
|
|
|
copy(loc, info.src, info.dst, info.size, rewriter);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class LowerHostCodeToLLVM
|
|
|
|
: public LowerHostCodeToLLVMBase<LowerHostCodeToLLVM> {
|
|
|
|
public:
|
|
|
|
void runOnOperation() override {
|
|
|
|
ModuleOp module = getOperation();
|
|
|
|
|
|
|
|
// Erase the GPU module.
|
|
|
|
for (auto gpuModule :
|
|
|
|
llvm::make_early_inc_range(module.getOps<gpu::GPUModuleOp>()))
|
|
|
|
gpuModule.erase();
|
|
|
|
|
|
|
|
// Specify options to lower Standard to LLVM and pull in the conversion
|
|
|
|
// patterns.
|
2021-03-22 21:58:26 +08:00
|
|
|
LowerToLLVMOptions options(module.getContext());
|
|
|
|
options.emitCWrappers = true;
|
2020-10-23 22:33:53 +08:00
|
|
|
auto *context = module.getContext();
|
2021-03-23 07:58:34 +08:00
|
|
|
RewritePatternSet patterns(context);
|
2020-10-23 22:33:53 +08:00
|
|
|
LLVMTypeConverter typeConverter(context, options);
|
|
|
|
populateStdToLLVMConversionPatterns(typeConverter, patterns);
|
2021-03-23 07:58:34 +08:00
|
|
|
patterns.add<GPULaunchLowering>(typeConverter);
|
2020-10-23 22:33:53 +08:00
|
|
|
|
|
|
|
// Pull in SPIR-V type conversion patterns to convert SPIR-V global
|
|
|
|
// variable's type to LLVM dialect type.
|
|
|
|
populateSPIRVToLLVMTypeConversion(typeConverter);
|
|
|
|
|
|
|
|
ConversionTarget target(*context);
|
|
|
|
target.addLegalDialect<LLVM::LLVMDialect>();
|
2020-10-27 08:25:01 +08:00
|
|
|
if (failed(applyPartialConversion(module, target, std::move(patterns))))
|
2020-10-23 22:33:53 +08:00
|
|
|
signalPassFailure();
|
|
|
|
|
|
|
|
// Finally, modify the kernel function in SPIR-V modules to avoid symbolic
|
|
|
|
// conflicts.
|
|
|
|
for (auto spvModule : module.getOps<spirv::ModuleOp>())
|
2021-02-05 06:57:50 +08:00
|
|
|
(void)encodeKernelName(spvModule);
|
2020-10-23 22:33:53 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
|
|
|
|
mlir::createLowerHostCodeToLLVMPass() {
|
|
|
|
return std::make_unique<LowerHostCodeToLLVM>();
|
|
|
|
}
|