#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
#include "mlir/Dialect/SPIRV/IR/TargetAndABI.h"
#include "mlir/Dialect/Utils/IndexingUtils.h"
#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/OneToNTypeConversion.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
#include <functional>
#include <optional>
#define DEBUG_TYPE "mlir-spirv-conversion"
using namespace mlir;
namespace {
static int getComputeVectorSize(int64_t size) {
for (int i : {4, 3, 2}) {
if (size % i == 0)
return i;
}
return 1;
}
static std::optional<SmallVector<int64_t>> getTargetShape(VectorType vecType) {
LLVM_DEBUG(llvm::dbgs() << "Get target shape\n");
if (vecType.isScalable()) {
LLVM_DEBUG(llvm::dbgs()
<< "--scalable vectors are not supported -> BAIL\n");
return std::nullopt;
}
SmallVector<int64_t> unrollShape = llvm::to_vector<4>(vecType.getShape());
std::optional<SmallVector<int64_t>> targetShape =
SmallVector<int64_t>(1, getComputeVectorSize(vecType.getShape().back()));
if (!targetShape) {
LLVM_DEBUG(llvm::dbgs() << "--no unrolling target shape defined\n");
return std::nullopt;
}
auto maybeShapeRatio = computeShapeRatio(unrollShape, *targetShape);
if (!maybeShapeRatio) {
LLVM_DEBUG(llvm::dbgs()
<< "--could not compute integral shape ratio -> BAIL\n");
return std::nullopt;
}
if (llvm::all_of(*maybeShapeRatio, [](int64_t v) { return v == 1; })) {
LLVM_DEBUG(llvm::dbgs() << "--no unrolling needed -> SKIP\n");
return std::nullopt;
}
LLVM_DEBUG(llvm::dbgs()
<< "--found an integral shape ratio to unroll to -> SUCCESS\n");
return targetShape;
}
template <typename LabelT>
static LogicalResult checkExtensionRequirements(
LabelT label, const spirv::TargetEnv &targetEnv,
const spirv::SPIRVType::ExtensionArrayRefVector &candidates) {
for (const auto &ors : candidates) {
if (targetEnv.allows(ors))
continue;
LLVM_DEBUG({
SmallVector<StringRef> extStrings;
for (spirv::Extension ext : ors)
extStrings.push_back(spirv::stringifyExtension(ext));
llvm::dbgs() << label << " illegal: requires at least one extension in ["
<< llvm::join(extStrings, ", ")
<< "] but none allowed in target environment\n";
});
return failure();
}
return success();
}
template <typename LabelT>
static LogicalResult checkCapabilityRequirements(
LabelT label, const spirv::TargetEnv &targetEnv,
const spirv::SPIRVType::CapabilityArrayRefVector &candidates) {
for (const auto &ors : candidates) {
if (targetEnv.allows(ors))
continue;
LLVM_DEBUG({
SmallVector<StringRef> capStrings;
for (spirv::Capability cap : ors)
capStrings.push_back(spirv::stringifyCapability(cap));
llvm::dbgs() << label << " illegal: requires at least one capability in ["
<< llvm::join(capStrings, ", ")
<< "] but none allowed in target environment\n";
});
return failure();
}
return success();
}
static bool needsExplicitLayout(spirv::StorageClass storageClass) {
switch (storageClass) {
case spirv::StorageClass::PhysicalStorageBuffer:
case spirv::StorageClass::PushConstant:
case spirv::StorageClass::StorageBuffer:
case spirv::StorageClass::Uniform:
return true;
default:
return false;
}
}
static spirv::PointerType
wrapInStructAndGetPointer(Type elementType, spirv::StorageClass storageClass) {
auto structType = needsExplicitLayout(storageClass)
? spirv::StructType::get(elementType, 0)
: spirv::StructType::get(elementType);
return spirv::PointerType::get(structType, storageClass);
}
static spirv::ScalarType getIndexType(MLIRContext *ctx,
const SPIRVConversionOptions &options) {
return cast<spirv::ScalarType>(
IntegerType::get(ctx, options.use64bitIndex ? 64 : 32));
}
static std::optional<int64_t>
getTypeNumBytes(const SPIRVConversionOptions &options, Type type) {
if (isa<spirv::ScalarType>(type)) {
auto bitWidth = type.getIntOrFloatBitWidth();
if (bitWidth == 1)
return std::nullopt;
return bitWidth / 8;
}
if (auto complexType = dyn_cast<ComplexType>(type)) {
auto elementSize = getTypeNumBytes(options, complexType.getElementType());
if (!elementSize)
return std::nullopt;
return 2 * *elementSize;
}
if (auto vecType = dyn_cast<VectorType>(type)) {
auto elementSize = getTypeNumBytes(options, vecType.getElementType());
if (!elementSize)
return std::nullopt;
return vecType.getNumElements() * *elementSize;
}
if (auto memRefType = dyn_cast<MemRefType>(type)) {
int64_t offset;
SmallVector<int64_t, 4> strides;
if (!memRefType.hasStaticShape() ||
failed(getStridesAndOffset(memRefType, strides, offset)))
return std::nullopt;
auto elementSize = getTypeNumBytes(options, memRefType.getElementType());
if (!elementSize)
return std::nullopt;
if (memRefType.getRank() == 0)
return elementSize;
auto dims = memRefType.getShape();
if (llvm::is_contained(dims, ShapedType::kDynamic) ||
ShapedType::isDynamic(offset) ||
llvm::is_contained(strides, ShapedType::kDynamic))
return std::nullopt;
int64_t memrefSize = -1;
for (const auto &shape : enumerate(dims))
memrefSize = std::max(memrefSize, shape.value() * strides[shape.index()]);
return (offset + memrefSize) * *elementSize;
}
if (auto tensorType = dyn_cast<TensorType>(type)) {
if (!tensorType.hasStaticShape())
return std::nullopt;
auto elementSize = getTypeNumBytes(options, tensorType.getElementType());
if (!elementSize)
return std::nullopt;
int64_t size = *elementSize;
for (auto shape : tensorType.getShape())
size *= shape;
return size;
}
return std::nullopt;
}
static Type
convertScalarType(const spirv::TargetEnv &targetEnv,
const SPIRVConversionOptions &options, spirv::ScalarType type,
std::optional<spirv::StorageClass> storageClass = {}) {
SmallVector<ArrayRef<spirv::Extension>, 1> extensions;
SmallVector<ArrayRef<spirv::Capability>, 2> capabilities;
type.getExtensions(extensions, storageClass);
type.getCapabilities(capabilities, storageClass);
if (succeeded(checkCapabilityRequirements(type, targetEnv, capabilities)) &&
succeeded(checkExtensionRequirements(type, targetEnv, extensions)))
return type;
if (!options.emulateLT32BitScalarTypes)
return nullptr;
if (type.getIntOrFloatBitWidth() > 32) {
LLVM_DEBUG(llvm::dbgs()
<< type
<< " not converted to 32-bit for SPIR-V to avoid truncation\n");
return nullptr;
}
if (auto floatType = dyn_cast<FloatType>(type)) {
LLVM_DEBUG(llvm::dbgs() << type << " converted to 32-bit for SPIR-V\n");
return Builder(targetEnv.getContext()).getF32Type();
}
auto intType = cast<IntegerType>(type);
LLVM_DEBUG(llvm::dbgs() << type << " converted to 32-bit for SPIR-V\n");
return IntegerType::get(targetEnv.getContext(), 32,
intType.getSignedness());
}
static Type convertSubByteIntegerType(const SPIRVConversionOptions &options,
IntegerType type) {
if (options.subByteTypeStorage != SPIRVSubByteTypeStorage::Packed) {
LLVM_DEBUG(llvm::dbgs() << "unsupported sub-byte storage kind\n");
return nullptr;
}
if (!llvm::isPowerOf2_32(type.getWidth())) {
LLVM_DEBUG(llvm::dbgs()
<< "unsupported non-power-of-two bitwidth in sub-byte" << type
<< "\n");
return nullptr;
}
LLVM_DEBUG(llvm::dbgs() << type << " converted to 32-bit for SPIR-V\n");
return IntegerType::get(type.getContext(), 32,
type.getSignedness());
}
static ShapedType
convertIndexElementType(ShapedType type,
const SPIRVConversionOptions &options) {
Type indexType = dyn_cast<IndexType>(type.getElementType());
if (!indexType)
return type;
return type.clone(getIndexType(type.getContext(), options));
}
static Type
convertVectorType(const spirv::TargetEnv &targetEnv,
const SPIRVConversionOptions &options, VectorType type,
std::optional<spirv::StorageClass> storageClass = {}) {
type = cast<VectorType>(convertIndexElementType(type, options));
auto scalarType = dyn_cast_or_null<spirv::ScalarType>(type.getElementType());
if (!scalarType) {
auto intType = dyn_cast<IntegerType>(type.getElementType());
if (!intType) {
LLVM_DEBUG(llvm::dbgs()
<< type
<< " illegal: cannot convert non-scalar element type\n");
return nullptr;
}
Type elementType = convertSubByteIntegerType(options, intType);
if (type.getRank() <= 1 && type.getNumElements() == 1)
return elementType;
if (type.getNumElements() > 4) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: > 4-element unimplemented\n");
return nullptr;
}
return VectorType::get(type.getShape(), elementType);
}
if (type.getRank() <= 1 && type.getNumElements() == 1)
return convertScalarType(targetEnv, options, scalarType, storageClass);
if (!spirv::CompositeType::isValid(type)) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: not a valid composite type\n");
return nullptr;
}
SmallVector<ArrayRef<spirv::Extension>, 1> extensions;
SmallVector<ArrayRef<spirv::Capability>, 2> capabilities;
cast<spirv::CompositeType>(type).getExtensions(extensions, storageClass);
cast<spirv::CompositeType>(type).getCapabilities(capabilities, storageClass);
if (succeeded(checkCapabilityRequirements(type, targetEnv, capabilities)) &&
succeeded(checkExtensionRequirements(type, targetEnv, extensions)))
return type;
auto elementType =
convertScalarType(targetEnv, options, scalarType, storageClass);
if (elementType)
return VectorType::get(type.getShape(), elementType);
return nullptr;
}
static Type
convertComplexType(const spirv::TargetEnv &targetEnv,
const SPIRVConversionOptions &options, ComplexType type,
std::optional<spirv::StorageClass> storageClass = {}) {
auto scalarType = dyn_cast_or_null<spirv::ScalarType>(type.getElementType());
if (!scalarType) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot convert non-scalar element type\n");
return nullptr;
}
auto elementType =
convertScalarType(targetEnv, options, scalarType, storageClass);
if (!elementType)
return nullptr;
if (elementType != type.getElementType()) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: complex type emulation unsupported\n");
return nullptr;
}
return VectorType::get(2, elementType);
}
static Type convertTensorType(const spirv::TargetEnv &targetEnv,
const SPIRVConversionOptions &options,
TensorType type) {
if (!type.hasStaticShape()) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: dynamic shape unimplemented\n");
return nullptr;
}
type = cast<TensorType>(convertIndexElementType(type, options));
auto scalarType = dyn_cast_or_null<spirv::ScalarType>(type.getElementType());
if (!scalarType) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot convert non-scalar element type\n");
return nullptr;
}
std::optional<int64_t> scalarSize = getTypeNumBytes(options, scalarType);
std::optional<int64_t> tensorSize = getTypeNumBytes(options, type);
if (!scalarSize || !tensorSize) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot deduce element count\n");
return nullptr;
}
int64_t arrayElemCount = *tensorSize / *scalarSize;
if (arrayElemCount == 0) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot handle zero-element tensors\n");
return nullptr;
}
Type arrayElemType = convertScalarType(targetEnv, options, scalarType);
if (!arrayElemType)
return nullptr;
std::optional<int64_t> arrayElemSize =
getTypeNumBytes(options, arrayElemType);
if (!arrayElemSize) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot deduce converted element size\n");
return nullptr;
}
return spirv::ArrayType::get(arrayElemType, arrayElemCount);
}
static Type convertBoolMemrefType(const spirv::TargetEnv &targetEnv,
const SPIRVConversionOptions &options,
MemRefType type,
spirv::StorageClass storageClass) {
unsigned numBoolBits = options.boolNumBits;
if (numBoolBits != 8) {
LLVM_DEBUG(llvm::dbgs()
<< "using non-8-bit storage for bool types unimplemented");
return nullptr;
}
auto elementType = dyn_cast<spirv::ScalarType>(
IntegerType::get(type.getContext(), numBoolBits));
if (!elementType)
return nullptr;
Type arrayElemType =
convertScalarType(targetEnv, options, elementType, storageClass);
if (!arrayElemType)
return nullptr;
std::optional<int64_t> arrayElemSize =
getTypeNumBytes(options, arrayElemType);
if (!arrayElemSize) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot deduce converted element size\n");
return nullptr;
}
if (!type.hasStaticShape()) {
if (targetEnv.allows(spirv::Capability::Kernel))
return spirv::PointerType::get(arrayElemType, storageClass);
int64_t stride = needsExplicitLayout(storageClass) ? *arrayElemSize : 0;
auto arrayType = spirv::RuntimeArrayType::get(arrayElemType, stride);
return wrapInStructAndGetPointer(arrayType, storageClass);
}
if (type.getNumElements() == 0) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: zero-element memrefs are not supported\n");
return nullptr;
}
int64_t memrefSize = llvm::divideCeil(type.getNumElements() * numBoolBits, 8);
int64_t arrayElemCount = llvm::divideCeil(memrefSize, *arrayElemSize);
int64_t stride = needsExplicitLayout(storageClass) ? *arrayElemSize : 0;
auto arrayType = spirv::ArrayType::get(arrayElemType, arrayElemCount, stride);
if (targetEnv.allows(spirv::Capability::Kernel))
return spirv::PointerType::get(arrayType, storageClass);
return wrapInStructAndGetPointer(arrayType, storageClass);
}
static Type convertSubByteMemrefType(const spirv::TargetEnv &targetEnv,
const SPIRVConversionOptions &options,
MemRefType type,
spirv::StorageClass storageClass) {
IntegerType elementType = cast<IntegerType>(type.getElementType());
Type arrayElemType = convertSubByteIntegerType(options, elementType);
if (!arrayElemType)
return nullptr;
int64_t arrayElemSize = *getTypeNumBytes(options, arrayElemType);
if (!type.hasStaticShape()) {
if (targetEnv.allows(spirv::Capability::Kernel))
return spirv::PointerType::get(arrayElemType, storageClass);
int64_t stride = needsExplicitLayout(storageClass) ? arrayElemSize : 0;
auto arrayType = spirv::RuntimeArrayType::get(arrayElemType, stride);
return wrapInStructAndGetPointer(arrayType, storageClass);
}
if (type.getNumElements() == 0) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: zero-element memrefs are not supported\n");
return nullptr;
}
int64_t memrefSize =
llvm::divideCeil(type.getNumElements() * elementType.getWidth(), 8);
int64_t arrayElemCount = llvm::divideCeil(memrefSize, arrayElemSize);
int64_t stride = needsExplicitLayout(storageClass) ? arrayElemSize : 0;
auto arrayType = spirv::ArrayType::get(arrayElemType, arrayElemCount, stride);
if (targetEnv.allows(spirv::Capability::Kernel))
return spirv::PointerType::get(arrayType, storageClass);
return wrapInStructAndGetPointer(arrayType, storageClass);
}
static Type convertMemrefType(const spirv::TargetEnv &targetEnv,
const SPIRVConversionOptions &options,
MemRefType type) {
auto attr = dyn_cast_or_null<spirv::StorageClassAttr>(type.getMemorySpace());
if (!attr) {
LLVM_DEBUG(
llvm::dbgs()
<< type
<< " illegal: expected memory space to be a SPIR-V storage class "
"attribute; please use MemorySpaceToStorageClassConverter to map "
"numeric memory spaces beforehand\n");
return nullptr;
}
spirv::StorageClass storageClass = attr.getValue();
if (isa<IntegerType>(type.getElementType())) {
if (type.getElementTypeBitWidth() == 1)
return convertBoolMemrefType(targetEnv, options, type, storageClass);
if (type.getElementTypeBitWidth() < 8)
return convertSubByteMemrefType(targetEnv, options, type, storageClass);
}
Type arrayElemType;
Type elementType = type.getElementType();
if (auto vecType = dyn_cast<VectorType>(elementType)) {
arrayElemType =
convertVectorType(targetEnv, options, vecType, storageClass);
} else if (auto complexType = dyn_cast<ComplexType>(elementType)) {
arrayElemType =
convertComplexType(targetEnv, options, complexType, storageClass);
} else if (auto scalarType = dyn_cast<spirv::ScalarType>(elementType)) {
arrayElemType =
convertScalarType(targetEnv, options, scalarType, storageClass);
} else if (auto indexType = dyn_cast<IndexType>(elementType)) {
type = cast<MemRefType>(convertIndexElementType(type, options));
arrayElemType = type.getElementType();
} else {
LLVM_DEBUG(
llvm::dbgs()
<< type
<< " unhandled: can only convert scalar or vector element type\n");
return nullptr;
}
if (!arrayElemType)
return nullptr;
std::optional<int64_t> arrayElemSize =
getTypeNumBytes(options, arrayElemType);
if (!arrayElemSize) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot deduce converted element size\n");
return nullptr;
}
if (!type.hasStaticShape()) {
if (targetEnv.allows(spirv::Capability::Kernel))
return spirv::PointerType::get(arrayElemType, storageClass);
int64_t stride = needsExplicitLayout(storageClass) ? *arrayElemSize : 0;
auto arrayType = spirv::RuntimeArrayType::get(arrayElemType, stride);
return wrapInStructAndGetPointer(arrayType, storageClass);
}
std::optional<int64_t> memrefSize = getTypeNumBytes(options, type);
if (!memrefSize) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: cannot deduce element count\n");
return nullptr;
}
if (*memrefSize == 0) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: zero-element memrefs are not supported\n");
return nullptr;
}
int64_t arrayElemCount = llvm::divideCeil(*memrefSize, *arrayElemSize);
int64_t stride = needsExplicitLayout(storageClass) ? *arrayElemSize : 0;
auto arrayType = spirv::ArrayType::get(arrayElemType, arrayElemCount, stride);
if (targetEnv.allows(spirv::Capability::Kernel))
return spirv::PointerType::get(arrayType, storageClass);
return wrapInStructAndGetPointer(arrayType, storageClass);
}
static std::optional<Value> castToSourceType(const spirv::TargetEnv &targetEnv,
OpBuilder &builder, Type type,
ValueRange inputs, Location loc) {
if (inputs.size() != 1) {
auto castOp = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
return castOp.getResult(0);
}
Value input = inputs.front();
if (!isa<IntegerType>(type)) {
auto castOp = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
return castOp.getResult(0);
}
auto inputType = cast<IntegerType>(input.getType());
auto scalarType = dyn_cast<spirv::ScalarType>(type);
if (!scalarType) {
auto castOp = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
return castOp.getResult(0);
}
if (inputType.getIntOrFloatBitWidth() < scalarType.getIntOrFloatBitWidth()) {
auto castOp = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
return castOp.getResult(0);
}
if (type.isInteger(1)) {
Value one = spirv::ConstantOp::getOne(inputType, loc, builder);
return builder.create<spirv::IEqualOp>(loc, input, one);
}
SmallVector<ArrayRef<spirv::Extension>, 1> exts;
SmallVector<ArrayRef<spirv::Capability>, 2> caps;
scalarType.getExtensions(exts);
scalarType.getCapabilities(caps);
if (failed(checkCapabilityRequirements(type, targetEnv, caps)) ||
failed(checkExtensionRequirements(type, targetEnv, exts))) {
auto castOp = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
return castOp.getResult(0);
}
if (type.isSignedInteger()) {
return builder.create<spirv::SConvertOp>(loc, type, input);
}
return builder.create<spirv::UConvertOp>(loc, type, input);
}
static spirv::GlobalVariableOp getBuiltinVariable(Block &body,
spirv::BuiltIn builtin) {
for (auto varOp : body.getOps<spirv::GlobalVariableOp>()) {
if (auto builtinAttr = varOp->getAttrOfType<StringAttr>(
spirv::SPIRVDialect::getAttributeName(
spirv::Decoration::BuiltIn))) {
auto varBuiltIn = spirv::symbolizeBuiltIn(builtinAttr.getValue());
if (varBuiltIn && *varBuiltIn == builtin) {
return varOp;
}
}
}
return nullptr;
}
std::string getBuiltinVarName(spirv::BuiltIn builtin, StringRef prefix,
StringRef suffix) {
return Twine(prefix).concat(stringifyBuiltIn(builtin)).concat(suffix).str();
}
static spirv::GlobalVariableOp
getOrInsertBuiltinVariable(Block &body, Location loc, spirv::BuiltIn builtin,
Type integerType, OpBuilder &builder,
StringRef prefix, StringRef suffix) {
if (auto varOp = getBuiltinVariable(body, builtin))
return varOp;
OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToStart(&body);
spirv::GlobalVariableOp newVarOp;
switch (builtin) {
case spirv::BuiltIn::NumWorkgroups:
case spirv::BuiltIn::WorkgroupSize:
case spirv::BuiltIn::WorkgroupId:
case spirv::BuiltIn::LocalInvocationId:
case spirv::BuiltIn::GlobalInvocationId: {
auto ptrType = spirv::PointerType::get(VectorType::get({3}, integerType),
spirv::StorageClass::Input);
std::string name = getBuiltinVarName(builtin, prefix, suffix);
newVarOp =
builder.create<spirv::GlobalVariableOp>(loc, ptrType, name, builtin);
break;
}
case spirv::BuiltIn::SubgroupId:
case spirv::BuiltIn::NumSubgroups:
case spirv::BuiltIn::SubgroupSize: {
auto ptrType =
spirv::PointerType::get(integerType, spirv::StorageClass::Input);
std::string name = getBuiltinVarName(builtin, prefix, suffix);
newVarOp =
builder.create<spirv::GlobalVariableOp>(loc, ptrType, name, builtin);
break;
}
default:
emitError(loc, "unimplemented builtin variable generation for ")
<< stringifyBuiltIn(builtin);
}
return newVarOp;
}
static spirv::PointerType getPushConstantStorageType(unsigned elementCount,
Builder &builder,
Type indexType) {
auto arrayType = spirv::ArrayType::get(indexType, elementCount,
4);
auto structType = spirv::StructType::get({arrayType}, 0);
return spirv::PointerType::get(structType, spirv::StorageClass::PushConstant);
}
static spirv::GlobalVariableOp getPushConstantVariable(Block &body,
unsigned elementCount) {
for (auto varOp : body.getOps<spirv::GlobalVariableOp>()) {
auto ptrType = dyn_cast<spirv::PointerType>(varOp.getType());
if (!ptrType)
continue;
if (ptrType.getStorageClass() == spirv::StorageClass::PushConstant) {
auto numElements = cast<spirv::ArrayType>(
cast<spirv::StructType>(ptrType.getPointeeType())
.getElementType(0))
.getNumElements();
if (numElements == elementCount)
return varOp;
}
}
return nullptr;
}
static spirv::GlobalVariableOp
getOrInsertPushConstantVariable(Location loc, Block &block,
unsigned elementCount, OpBuilder &b,
Type indexType) {
if (auto varOp = getPushConstantVariable(block, elementCount))
return varOp;
auto builder = OpBuilder::atBlockBegin(&block, b.getListener());
auto type = getPushConstantStorageType(elementCount, builder, indexType);
const char *name = "__push_constant_var__";
return builder.create<spirv::GlobalVariableOp>(loc, type, name,
nullptr);
}
struct FuncOpConversion final : OpConversionPattern<func::FuncOp> {
using OpConversionPattern<func::FuncOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
FunctionType fnType = funcOp.getFunctionType();
if (fnType.getNumResults() > 1)
return failure();
TypeConverter::SignatureConversion signatureConverter(
fnType.getNumInputs());
for (const auto &argType : enumerate(fnType.getInputs())) {
auto convertedType = getTypeConverter()->convertType(argType.value());
if (!convertedType)
return failure();
signatureConverter.addInputs(argType.index(), convertedType);
}
Type resultType;
if (fnType.getNumResults() == 1) {
resultType = getTypeConverter()->convertType(fnType.getResult(0));
if (!resultType)
return failure();
}
auto newFuncOp = rewriter.create<spirv::FuncOp>(
funcOp.getLoc(), funcOp.getName(),
rewriter.getFunctionType(signatureConverter.getConvertedTypes(),
resultType ? TypeRange(resultType)
: TypeRange()));
for (const auto &namedAttr : funcOp->getAttrs()) {
if (namedAttr.getName() != funcOp.getFunctionTypeAttrName() &&
namedAttr.getName() != SymbolTable::getSymbolAttrName())
newFuncOp->setAttr(namedAttr.getName(), namedAttr.getValue());
}
rewriter.inlineRegionBefore(funcOp.getBody(), newFuncOp.getBody(),
newFuncOp.end());
if (failed(rewriter.convertRegionTypes(
&newFuncOp.getBody(), *getTypeConverter(), &signatureConverter)))
return failure();
rewriter.eraseOp(funcOp);
return success();
}
};
struct FuncOpVectorUnroll final : OpRewritePattern<func::FuncOp> {
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(func::FuncOp funcOp,
PatternRewriter &rewriter) const override {
FunctionType fnType = funcOp.getFunctionType();
if (funcOp.isDeclaration()) {
LLVM_DEBUG(llvm::dbgs()
<< fnType << " illegal: declarations are unsupported\n");
return failure();
}
auto newFuncOp = rewriter.create<func::FuncOp>(funcOp.getLoc(),
funcOp.getName(), fnType);
rewriter.inlineRegionBefore(funcOp.getBody(), newFuncOp.getBody(),
newFuncOp.end());
Location loc = newFuncOp.getBody().getLoc();
Block &entryBlock = newFuncOp.getBlocks().front();
OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(&entryBlock);
OneToNTypeMapping oneToNTypeMapping(fnType.getInputs());
SmallVector<size_t> unrolledInputNums;
size_t newInputNo = 0;
llvm::SmallDenseMap<Operation *, size_t> tmpOps;
size_t newOpCount = 0;
for (auto [origInputNo, origType] : enumerate(fnType.getInputs())) {
auto origVecType = dyn_cast<VectorType>(origType);
if (!origVecType) {
Value result = rewriter.create<arith::ConstantOp>(
loc, origType, rewriter.getZeroAttr(origType));
rewriter.replaceAllUsesWith(newFuncOp.getArgument(origInputNo), result);
tmpOps.insert({result.getDefiningOp(), newInputNo});
oneToNTypeMapping.addInputs(origInputNo, origType);
++newInputNo;
++newOpCount;
continue;
}
auto targetShape = getTargetShape(origVecType);
if (!targetShape) {
Value result = rewriter.create<arith::ConstantOp>(
loc, origType, rewriter.getZeroAttr(origType));
rewriter.replaceAllUsesWith(newFuncOp.getArgument(origInputNo), result);
tmpOps.insert({result.getDefiningOp(), newInputNo});
oneToNTypeMapping.addInputs(origInputNo, origType);
++newInputNo;
++newOpCount;
continue;
}
VectorType unrolledType =
VectorType::get(*targetShape, origVecType.getElementType());
auto originalShape =
llvm::to_vector_of<int64_t, 4>(origVecType.getShape());
Value result = rewriter.create<arith::ConstantOp>(
loc, origVecType, rewriter.getZeroAttr(origVecType));
++newOpCount;
Value dummy = rewriter.create<arith::ConstantOp>(
loc, unrolledType, rewriter.getZeroAttr(unrolledType));
++newOpCount;
SmallVector<int64_t> strides(targetShape->size(), 1);
SmallVector<Type> newTypes;
for (SmallVector<int64_t> offsets :
StaticTileOffsetRange(originalShape, *targetShape)) {
result = rewriter.create<vector::InsertStridedSliceOp>(
loc, dummy, result, offsets, strides);
newTypes.push_back(unrolledType);
unrolledInputNums.push_back(newInputNo);
++newInputNo;
++newOpCount;
}
rewriter.replaceAllUsesWith(newFuncOp.getArgument(origInputNo), result);
oneToNTypeMapping.addInputs(origInputNo, newTypes);
}
auto convertedTypes = oneToNTypeMapping.getConvertedTypes();
auto newFnType = fnType.clone(convertedTypes, fnType.getResults());
rewriter.modifyOpInPlace(newFuncOp,
[&] { newFuncOp.setFunctionType(newFnType); });
entryBlock.eraseArguments(0, fnType.getNumInputs());
SmallVector<Location> locs(convertedTypes.size(), newFuncOp.getLoc());
entryBlock.addArguments(convertedTypes, locs);
size_t unrolledInputIdx = 0;
for (auto [count, op] : enumerate(entryBlock.getOperations())) {
Operation &curOp = op;
for (auto [operandIdx, operandVal] : llvm::enumerate(op.getOperands())) {
Operation *operandOp = operandVal.getDefiningOp();
if (auto it = tmpOps.find(operandOp); it != tmpOps.end()) {
size_t idx = operandIdx;
rewriter.modifyOpInPlace(&curOp, [&curOp, &newFuncOp, it, idx] {
curOp.setOperand(idx, newFuncOp.getArgument(it->second));
});
}
}
if (count >= newOpCount)
continue;
if (auto vecOp = dyn_cast<vector::InsertStridedSliceOp>(op)) {
size_t unrolledInputNo = unrolledInputNums[unrolledInputIdx];
rewriter.modifyOpInPlace(&curOp, [&] {
curOp.setOperand(0, newFuncOp.getArgument(unrolledInputNo));
});
++unrolledInputIdx;
}
}
rewriter.eraseOp(funcOp);
return success();
}
};
struct ReturnOpVectorUnroll final : OpRewritePattern<func::ReturnOp> {
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(func::ReturnOp returnOp,
PatternRewriter &rewriter) const override {
auto funcOp = dyn_cast<func::FuncOp>(returnOp->getParentOp());
if (!funcOp)
return failure();
FunctionType fnType = funcOp.getFunctionType();
OneToNTypeMapping oneToNTypeMapping(fnType.getResults());
Location loc = returnOp.getLoc();
SmallVector<Value> newOperands;
for (auto [origResultNo, origType] : enumerate(fnType.getResults())) {
auto origVecType = dyn_cast<VectorType>(origType);
if (!origVecType) {
oneToNTypeMapping.addInputs(origResultNo, origType);
newOperands.push_back(returnOp.getOperand(origResultNo));
continue;
}
auto targetShape = getTargetShape(origVecType);
if (!targetShape) {
oneToNTypeMapping.addInputs(origResultNo, origType);
newOperands.push_back(returnOp.getOperand(origResultNo));
continue;
}
VectorType unrolledType =
VectorType::get(*targetShape, origVecType.getElementType());
auto originalShape =
llvm::to_vector_of<int64_t, 4>(origVecType.getShape());
SmallVector<int64_t> strides(targetShape->size(), 1);
SmallVector<Type> newTypes;
Value returnValue = returnOp.getOperand(origResultNo);
for (SmallVector<int64_t> offsets :
StaticTileOffsetRange(originalShape, *targetShape)) {
Value result = rewriter.create<vector::ExtractStridedSliceOp>(
loc, returnValue, offsets, *targetShape, strides);
newOperands.push_back(result);
newTypes.push_back(unrolledType);
}
oneToNTypeMapping.addInputs(origResultNo, newTypes);
}
auto newFnType =
FunctionType::get(rewriter.getContext(), TypeRange(fnType.getInputs()),
TypeRange(oneToNTypeMapping.getConvertedTypes()));
rewriter.modifyOpInPlace(funcOp,
[&] { funcOp.setFunctionType(newFnType); });
rewriter.replaceOp(returnOp,
rewriter.create<func::ReturnOp>(loc, newOperands));
return success();
}
};
}
Value mlir::spirv::getBuiltinVariableValue(Operation *op,
spirv::BuiltIn builtin,
Type integerType, OpBuilder &builder,
StringRef prefix, StringRef suffix) {
Operation *parent = SymbolTable::getNearestSymbolTable(op->getParentOp());
if (!parent) {
op->emitError("expected operation to be within a module-like op");
return nullptr;
}
spirv::GlobalVariableOp varOp =
getOrInsertBuiltinVariable(*parent->getRegion(0).begin(), op->getLoc(),
builtin, integerType, builder, prefix, suffix);
Value ptr = builder.create<spirv::AddressOfOp>(op->getLoc(), varOp);
return builder.create<spirv::LoadOp>(op->getLoc(), ptr);
}
Value spirv::getPushConstantValue(Operation *op, unsigned elementCount,
unsigned offset, Type integerType,
OpBuilder &builder) {
Location loc = op->getLoc();
Operation *parent = SymbolTable::getNearestSymbolTable(op->getParentOp());
if (!parent) {
op->emitError("expected operation to be within a module-like op");
return nullptr;
}
spirv::GlobalVariableOp varOp = getOrInsertPushConstantVariable(
loc, parent->getRegion(0).front(), elementCount, builder, integerType);
Value zeroOp = spirv::ConstantOp::getZero(integerType, loc, builder);
Value offsetOp = builder.create<spirv::ConstantOp>(
loc, integerType, builder.getI32IntegerAttr(offset));
auto addrOp = builder.create<spirv::AddressOfOp>(loc, varOp);
auto acOp = builder.create<spirv::AccessChainOp>(
loc, addrOp, llvm::ArrayRef({zeroOp, offsetOp}));
return builder.create<spirv::LoadOp>(loc, acOp);
}
Value mlir::spirv::linearizeIndex(ValueRange indices, ArrayRef<int64_t> strides,
int64_t offset, Type integerType,
Location loc, OpBuilder &builder) {
assert(indices.size() == strides.size() &&
"must provide indices for all dimensions");
Value linearizedIndex = builder.createOrFold<spirv::ConstantOp>(
loc, integerType, IntegerAttr::get(integerType, offset));
for (const auto &index : llvm::enumerate(indices)) {
Value strideVal = builder.createOrFold<spirv::ConstantOp>(
loc, integerType,
IntegerAttr::get(integerType, strides[index.index()]));
Value update =
builder.createOrFold<spirv::IMulOp>(loc, index.value(), strideVal);
linearizedIndex =
builder.createOrFold<spirv::IAddOp>(loc, update, linearizedIndex);
}
return linearizedIndex;
}
Value mlir::spirv::getVulkanElementPtr(const SPIRVTypeConverter &typeConverter,
MemRefType baseType, Value basePtr,
ValueRange indices, Location loc,
OpBuilder &builder) {
int64_t offset;
SmallVector<int64_t, 4> strides;
if (failed(getStridesAndOffset(baseType, strides, offset)) ||
llvm::is_contained(strides, ShapedType::kDynamic) ||
ShapedType::isDynamic(offset)) {
return nullptr;
}
auto indexType = typeConverter.getIndexType();
SmallVector<Value, 2> linearizedIndices;
auto zero = spirv::ConstantOp::getZero(indexType, loc, builder);
linearizedIndices.push_back(zero);
if (baseType.getRank() == 0) {
linearizedIndices.push_back(zero);
} else {
linearizedIndices.push_back(
linearizeIndex(indices, strides, offset, indexType, loc, builder));
}
return builder.create<spirv::AccessChainOp>(loc, basePtr, linearizedIndices);
}
Value mlir::spirv::getOpenCLElementPtr(const SPIRVTypeConverter &typeConverter,
MemRefType baseType, Value basePtr,
ValueRange indices, Location loc,
OpBuilder &builder) {
int64_t offset;
SmallVector<int64_t, 4> strides;
if (failed(getStridesAndOffset(baseType, strides, offset)) ||
llvm::is_contained(strides, ShapedType::kDynamic) ||
ShapedType::isDynamic(offset)) {
return nullptr;
}
auto indexType = typeConverter.getIndexType();
SmallVector<Value, 2> linearizedIndices;
Value linearIndex;
if (baseType.getRank() == 0) {
linearIndex = spirv::ConstantOp::getZero(indexType, loc, builder);
} else {
linearIndex =
linearizeIndex(indices, strides, offset, indexType, loc, builder);
}
Type pointeeType =
cast<spirv::PointerType>(basePtr.getType()).getPointeeType();
if (isa<spirv::ArrayType>(pointeeType)) {
linearizedIndices.push_back(linearIndex);
return builder.create<spirv::AccessChainOp>(loc, basePtr,
linearizedIndices);
}
return builder.create<spirv::PtrAccessChainOp>(loc, basePtr, linearIndex,
linearizedIndices);
}
Value mlir::spirv::getElementPtr(const SPIRVTypeConverter &typeConverter,
MemRefType baseType, Value basePtr,
ValueRange indices, Location loc,
OpBuilder &builder) {
if (typeConverter.allows(spirv::Capability::Kernel)) {
return getOpenCLElementPtr(typeConverter, baseType, basePtr, indices, loc,
builder);
}
return getVulkanElementPtr(typeConverter, baseType, basePtr, indices, loc,
builder);
}
SPIRVTypeConverter::SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr,
const SPIRVConversionOptions &options)
: targetEnv(targetAttr), options(options) {
addConversion([](spirv::SPIRVType type) { return type; });
addConversion([this](IndexType ) { return getIndexType(); });
addConversion([this](IntegerType intType) -> std::optional<Type> {
if (auto scalarType = dyn_cast<spirv::ScalarType>(intType))
return convertScalarType(this->targetEnv, this->options, scalarType);
if (intType.getWidth() < 8)
return convertSubByteIntegerType(this->options, intType);
return Type();
});
addConversion([this](FloatType floatType) -> std::optional<Type> {
if (auto scalarType = dyn_cast<spirv::ScalarType>(floatType))
return convertScalarType(this->targetEnv, this->options, scalarType);
return Type();
});
addConversion([this](ComplexType complexType) {
return convertComplexType(this->targetEnv, this->options, complexType);
});
addConversion([this](VectorType vectorType) {
return convertVectorType(this->targetEnv, this->options, vectorType);
});
addConversion([this](TensorType tensorType) {
return convertTensorType(this->targetEnv, this->options, tensorType);
});
addConversion([this](MemRefType memRefType) {
return convertMemrefType(this->targetEnv, this->options, memRefType);
});
addSourceMaterialization(
[this](OpBuilder &builder, Type type, ValueRange inputs, Location loc) {
return castToSourceType(this->targetEnv, builder, type, inputs, loc);
});
addTargetMaterialization([](OpBuilder &builder, Type type, ValueRange inputs,
Location loc) {
auto cast = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
return std::optional<Value>(cast.getResult(0));
});
}
Type SPIRVTypeConverter::getIndexType() const {
return ::getIndexType(getContext(), options);
}
MLIRContext *SPIRVTypeConverter::getContext() const {
return targetEnv.getAttr().getContext();
}
bool SPIRVTypeConverter::allows(spirv::Capability capability) const {
return targetEnv.allows(capability);
}
std::unique_ptr<SPIRVConversionTarget>
SPIRVConversionTarget::get(spirv::TargetEnvAttr targetAttr) {
std::unique_ptr<SPIRVConversionTarget> target(
new SPIRVConversionTarget(targetAttr));
SPIRVConversionTarget *targetPtr = target.get();
target->addDynamicallyLegalDialect<spirv::SPIRVDialect>(
[targetPtr](Operation *op) { return targetPtr->isLegalOp(op); });
return target;
}
SPIRVConversionTarget::SPIRVConversionTarget(spirv::TargetEnvAttr targetAttr)
: ConversionTarget(*targetAttr.getContext()), targetEnv(targetAttr) {}
bool SPIRVConversionTarget::isLegalOp(Operation *op) {
if (auto minVersionIfx = dyn_cast<spirv::QueryMinVersionInterface>(op)) {
std::optional<spirv::Version> minVersion = minVersionIfx.getMinVersion();
if (minVersion && *minVersion > this->targetEnv.getVersion()) {
LLVM_DEBUG(llvm::dbgs()
<< op->getName() << " illegal: requiring min version "
<< spirv::stringifyVersion(*minVersion) << "\n");
return false;
}
}
if (auto maxVersionIfx = dyn_cast<spirv::QueryMaxVersionInterface>(op)) {
std::optional<spirv::Version> maxVersion = maxVersionIfx.getMaxVersion();
if (maxVersion && *maxVersion < this->targetEnv.getVersion()) {
LLVM_DEBUG(llvm::dbgs()
<< op->getName() << " illegal: requiring max version "
<< spirv::stringifyVersion(*maxVersion) << "\n");
return false;
}
}
if (auto extensions = dyn_cast<spirv::QueryExtensionInterface>(op))
if (failed(checkExtensionRequirements(op->getName(), this->targetEnv,
extensions.getExtensions())))
return false;
if (auto capabilities = dyn_cast<spirv::QueryCapabilityInterface>(op))
if (failed(checkCapabilityRequirements(op->getName(), this->targetEnv,
capabilities.getCapabilities())))
return false;
SmallVector<Type, 4> valueTypes;
valueTypes.append(op->operand_type_begin(), op->operand_type_end());
valueTypes.append(op->result_type_begin(), op->result_type_end());
if (llvm::any_of(valueTypes,
[](Type t) { return !isa<spirv::SPIRVType>(t); }))
return false;
if (auto globalVar = dyn_cast<spirv::GlobalVariableOp>(op))
valueTypes.push_back(globalVar.getType());
SmallVector<ArrayRef<spirv::Extension>, 4> typeExtensions;
SmallVector<ArrayRef<spirv::Capability>, 8> typeCapabilities;
for (Type valueType : valueTypes) {
typeExtensions.clear();
cast<spirv::SPIRVType>(valueType).getExtensions(typeExtensions);
if (failed(checkExtensionRequirements(op->getName(), this->targetEnv,
typeExtensions)))
return false;
typeCapabilities.clear();
cast<spirv::SPIRVType>(valueType).getCapabilities(typeCapabilities);
if (failed(checkCapabilityRequirements(op->getName(), this->targetEnv,
typeCapabilities)))
return false;
}
return true;
}
void mlir::populateBuiltinFuncToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
RewritePatternSet &patterns) {
patterns.add<FuncOpConversion>(typeConverter, patterns.getContext());
}
void mlir::populateFuncOpVectorRewritePatterns(RewritePatternSet &patterns) {
patterns.add<FuncOpVectorUnroll>(patterns.getContext());
}
void mlir::populateReturnOpVectorRewritePatterns(RewritePatternSet &patterns) {
patterns.add<ReturnOpVectorUnroll>(patterns.getContext());
}