MLIR  16.0.0git
Macros | Functions | Variables
SPIRVToLLVM.cpp File Reference
#include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVM.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.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/Utils/LayoutUtils.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FormatVariadic.h"
+ Include dependency graph for SPIRVToLLVM.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "spirv-to-llvm-pattern"
 
#define DISPATCH(functionControl, llvmAttr)
 

Functions

static bool isSignedIntegerOrVector (Type type)
 Returns true if the given type is a signed integer or vector type. More...
 
static bool isUnsignedIntegerOrVector (Type type)
 Returns true if the given type is an unsigned integer or vector type. More...
 
static unsigned getBitWidth (Type type)
 Returns the bit width of integer, float or vector of float or integer values. More...
 
static unsigned getLLVMTypeBitWidth (Type type)
 Returns the bit width of LLVMType integer or vector. More...
 
static IntegerAttr minusOneIntegerAttribute (Type type, Builder builder)
 Creates IntegerAttribute with all bits set for given type. More...
 
static Value createConstantAllBitsSet (Location loc, Type srcType, Type dstType, PatternRewriter &rewriter)
 Creates llvm.mlir.constant with all bits set for the given type. More...
 
static Value createFPConstant (Location loc, Type srcType, Type dstType, PatternRewriter &rewriter, double value)
 Creates llvm.mlir.constant with a floating-point scalar or vector value. More...
 
static Value optionallyTruncateOrExtend (Location loc, Value value, Type llvmType, PatternRewriter &rewriter)
 Utility function for bitfield ops: More...
 
static Value broadcast (Location loc, Value toBroadcast, unsigned numElements, LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter)
 Broadcasts the value to vector with numElements number of elements. More...
 
static Value optionallyBroadcast (Location loc, Value value, Type srcType, LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter)
 Broadcasts the value. If srcType is a scalar, the value remains unchanged. More...
 
static Value processCountOrOffset (Location loc, Value value, Type srcType, Type dstType, LLVMTypeConverter &converter, ConversionPatternRewriter &rewriter)
 Utility function for bitfield ops: BitFieldInsert, BitFieldSExtract and BitFieldUExtract. More...
 
static Optional< TypeconvertStructTypeWithOffset (spirv::StructType type, LLVMTypeConverter &converter)
 Converts SPIR-V struct with a regular (according to VulkanLayoutUtils) offset to LLVM struct. More...
 
static Type convertStructTypePacked (spirv::StructType type, LLVMTypeConverter &converter)
 Converts SPIR-V struct with no offset to packed LLVM struct. More...
 
static Value createI32ConstantOf (Location loc, PatternRewriter &rewriter, unsigned value)
 Creates LLVM dialect constant with the given value. More...
 
static LogicalResult replaceWithLoadOrStore (Operation *op, ValueRange operands, ConversionPatternRewriter &rewriter, LLVMTypeConverter &typeConverter, unsigned alignment, bool isVolatile, bool isNonTemporal)
 Utility for spirv.Load and spirv.Store conversion. More...
 
static Optional< TypeconvertArrayType (spirv::ArrayType type, TypeConverter &converter)
 Converts SPIR-V array type to LLVM array. More...
 
static Type convertPointerType (spirv::PointerType type, TypeConverter &converter)
 Converts SPIR-V pointer type to LLVM pointer. More...
 
static Optional< TypeconvertRuntimeArrayType (spirv::RuntimeArrayType type, TypeConverter &converter)
 Converts SPIR-V runtime array to LLVM array. More...
 
static Optional< TypeconvertStructType (spirv::StructType type, LLVMTypeConverter &converter)
 Converts SPIR-V struct to LLVM struct. More...
 

Variables

static constexpr StringRef kBinding = "binding"
 Hook for descriptor set and binding number encoding. More...
 
static constexpr StringRef kDescriptorSet = "descriptor_set"
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "spirv-to-llvm-pattern"

Definition at line 28 of file SPIRVToLLVM.cpp.

◆ DISPATCH

#define DISPATCH (   functionControl,
  llvmAttr 
)
Value:
case functionControl: \
newFuncOp->setAttr("passthrough", ArrayAttr::get(context, {llvmAttr})); \
break;

Referenced by convertStructType().

Function Documentation

◆ broadcast()

static Value broadcast ( Location  loc,
Value  toBroadcast,
unsigned  numElements,
LLVMTypeConverter typeConverter,
ConversionPatternRewriter rewriter 
)
static

◆ convertArrayType()

static Optional<Type> convertArrayType ( spirv::ArrayType  type,
TypeConverter converter 
)
static

Converts SPIR-V array type to LLVM array.

Natural stride (according to VulkanLayoutUtils) is also mapped to LLVM array. This has to be respected when converting ops that manipulate array types.

Definition at line 250 of file SPIRVToLLVM.cpp.

References mlir::Type::cast(), mlir::TypeConverter::convertType(), mlir::LLVM::LLVMArrayType::get(), mlir::spirv::ArrayType::getArrayStride(), mlir::spirv::ArrayType::getElementType(), mlir::spirv::ArrayType::getNumElements(), and getSizeInBytes().

Referenced by mlir::populateSPIRVToLLVMTypeConversion().

◆ convertPointerType()

static Type convertPointerType ( spirv::PointerType  type,
TypeConverter converter 
)
static

Converts SPIR-V pointer type to LLVM pointer.

Pointer's storage class is not modelled at the moment.

Definition at line 265 of file SPIRVToLLVM.cpp.

References mlir::TypeConverter::convertType(), mlir::LLVM::LLVMPointerType::get(), and mlir::spirv::PointerType::getPointeeType().

Referenced by mlir::populateSPIRVToLLVMTypeConversion().

◆ convertRuntimeArrayType()

static Optional<Type> convertRuntimeArrayType ( spirv::RuntimeArrayType  type,
TypeConverter converter 
)
static

Converts SPIR-V runtime array to LLVM array.

Since LLVM allows indexing over the bounds, the runtime array is converted to a 0-sized LLVM array. There is no modelling of array stride at the moment.

Definition at line 274 of file SPIRVToLLVM.cpp.

References mlir::TypeConverter::convertType(), mlir::LLVM::LLVMArrayType::get(), mlir::spirv::RuntimeArrayType::getArrayStride(), and mlir::spirv::RuntimeArrayType::getElementType().

Referenced by mlir::populateSPIRVToLLVMTypeConversion().

◆ convertStructType()

static Optional<Type> convertStructType ( spirv::StructType  type,
LLVMTypeConverter converter 
)
static

Converts SPIR-V struct to LLVM struct.

There is no support of structs with member decorations. Also, only natural offset is supported.

Definition at line 284 of file SPIRVToLLVM.cpp.

References mlir::Block::begin(), mlir::Location::cast(), mlir::Value::cast(), mlir::LLVM::convertArrayToIndices(), mlir::ConversionPatternRewriter::convertRegionTypes(), convertStructTypePacked(), convertStructTypeWithOffset(), mlir::OpBuilder::create(), mlir::OpBuilder::createBlock(), createConstantAllBitsSet(), createFPConstant(), createI32ConstantOf(), DISPATCH, mlir::Operation::emitError(), mlir::ConversionPatternRewriter::eraseBlock(), mlir::ConversionPatternRewriter::eraseOp(), mlir::failed(), mlir::failure(), mlir::LLVM::LLVMArrayType::get(), mlir::DenseElementsAttr::get(), mlir::Block::getArguments(), getBitWidth(), mlir::OpBuilder::getBlock(), mlir::Builder::getContext(), getElementType(), mlir::Builder::getI32IntegerAttr(), mlir::Builder::getI32Type(), mlir::OpBuilder::getInsertionBlock(), mlir::OpBuilder::getInsertionPoint(), mlir::Builder::getIntegerAttr(), mlir::Builder::getIntegerType(), mlir::LLVM::LLVMStructType::getLiteral(), mlir::spirv::StructType::getMemberDecorations(), getNumElements(), mlir::Operation::getOperands(), mlir::Block::getOperations(), mlir::Block::getTerminator(), mlir::spirv::StructType::hasOffset(), mlir::ConversionPatternRewriter::inlineRegionBefore(), mlir::Value::isa(), mlir::Type::isa(), isSignedIntegerOrVector(), isUnsignedIntegerOrVector(), minusOneIntegerAttribute(), None, processCountOrOffset(), mlir::ConversionPatternRewriter::replaceOp(), mlir::RewriterBase::replaceOpWithNewOp(), replaceWithLoadOrStore(), mlir::OpBuilder::setInsertionPoint(), mlir::OpBuilder::setInsertionPointAfter(), mlir::OpBuilder::setInsertionPointToEnd(), mlir::OpBuilder::setInsertionPointToStart(), mlir::ConversionPatternRewriter::splitBlock(), mlir::success(), and value.

Referenced by mlir::populateSPIRVToLLVMTypeConversion().

◆ convertStructTypePacked()

static Type convertStructTypePacked ( spirv::StructType  type,
LLVMTypeConverter converter 
)
static

Converts SPIR-V struct with no offset to packed LLVM struct.

Definition at line 202 of file SPIRVToLLVM.cpp.

References mlir::TypeConverter::convertType(), mlir::spirv::StructType::getElementTypes(), and mlir::LLVM::LLVMStructType::getLiteral().

Referenced by convertStructType().

◆ convertStructTypeWithOffset()

static Optional<Type> convertStructTypeWithOffset ( spirv::StructType  type,
LLVMTypeConverter converter 
)
static

Converts SPIR-V struct with a regular (according to VulkanLayoutUtils) offset to LLVM struct.

Otherwise, the conversion is not supported.

Definition at line 188 of file SPIRVToLLVM.cpp.

References mlir::TypeConverter::convertType(), mlir::VulkanLayoutUtils::decorateType(), mlir::spirv::StructType::getElementTypes(), and mlir::LLVM::LLVMStructType::getLiteral().

Referenced by convertStructType().

◆ createConstantAllBitsSet()

static Value createConstantAllBitsSet ( Location  loc,
Type  srcType,
Type  dstType,
PatternRewriter rewriter 
)
static

Creates llvm.mlir.constant with all bits set for the given type.

Definition at line 86 of file SPIRVToLLVM.cpp.

References mlir::Type::cast(), mlir::OpBuilder::create(), mlir::DenseElementsAttr::get(), mlir::Type::isa(), and minusOneIntegerAttribute().

Referenced by convertStructType().

◆ createFPConstant()

static Value createFPConstant ( Location  loc,
Type  srcType,
Type  dstType,
PatternRewriter rewriter,
double  value 
)
static

Creates llvm.mlir.constant with a floating-point scalar or vector value.

Definition at line 99 of file SPIRVToLLVM.cpp.

References mlir::Value::cast(), mlir::Type::cast(), mlir::OpBuilder::create(), mlir::Type::dyn_cast(), mlir::DenseElementsAttr::get(), and mlir::Builder::getFloatAttr().

Referenced by convertStructType().

◆ createI32ConstantOf()

static Value createI32ConstantOf ( Location  loc,
PatternRewriter rewriter,
unsigned  value 
)
static

Creates LLVM dialect constant with the given value.

Definition at line 213 of file SPIRVToLLVM.cpp.

References mlir::OpBuilder::create(), mlir::Builder::getContext(), mlir::Builder::getI32Type(), mlir::Builder::getIntegerAttr(), and value.

Referenced by convertStructType().

◆ getBitWidth()

static unsigned getBitWidth ( Type  type)
static

◆ getLLVMTypeBitWidth()

static unsigned getLLVMTypeBitWidth ( Type  type)
static

Returns the bit width of LLVMType integer or vector.

Definition at line 68 of file SPIRVToLLVM.cpp.

References mlir::LLVM::getVectorElementType(), and mlir::LLVM::isCompatibleVectorType().

Referenced by optionallyTruncateOrExtend().

◆ isSignedIntegerOrVector()

static bool isSignedIntegerOrVector ( Type  type)
static

Returns true if the given type is a signed integer or vector type.

Definition at line 37 of file SPIRVToLLVM.cpp.

References mlir::Type::dyn_cast(), and mlir::Type::isSignedInteger().

Referenced by convertStructType().

◆ isUnsignedIntegerOrVector()

static bool isUnsignedIntegerOrVector ( Type  type)
static

Returns true if the given type is an unsigned integer or vector type.

Definition at line 46 of file SPIRVToLLVM.cpp.

References mlir::Type::dyn_cast(), and mlir::Type::isUnsignedInteger().

Referenced by convertStructType().

◆ minusOneIntegerAttribute()

static IntegerAttr minusOneIntegerAttribute ( Type  type,
Builder  builder 
)
static

Creates IntegerAttribute with all bits set for given type.

Definition at line 76 of file SPIRVToLLVM.cpp.

References mlir::Type::cast(), mlir::Type::dyn_cast(), and mlir::Builder::getIntegerAttr().

Referenced by convertStructType(), and createConstantAllBitsSet().

◆ optionallyBroadcast()

static Value optionallyBroadcast ( Location  loc,
Value  value,
Type  srcType,
LLVMTypeConverter typeConverter,
ConversionPatternRewriter rewriter 
)
static

Broadcasts the value. If srcType is a scalar, the value remains unchanged.

Definition at line 157 of file SPIRVToLLVM.cpp.

References broadcast(), mlir::Type::dyn_cast(), value, and vectorType().

Referenced by processCountOrOffset().

◆ optionallyTruncateOrExtend()

static Value optionallyTruncateOrExtend ( Location  loc,
Value  value,
Type  llvmType,
PatternRewriter rewriter 
)
static

Utility function for bitfield ops:

  • BitFieldInsert
  • BitFieldSExtract
  • BitFieldUExtract Truncates or extends the value. If the bitwidth of the value is the same as llvmType bitwidth, the value remains unchanged.

Definition at line 119 of file SPIRVToLLVM.cpp.

References mlir::OpBuilder::create(), getBitWidth(), getLLVMTypeBitWidth(), mlir::Value::getType(), mlir::LLVM::isCompatibleType(), and value.

Referenced by processCountOrOffset().

◆ processCountOrOffset()

static Value processCountOrOffset ( Location  loc,
Value  value,
Type  srcType,
Type  dstType,
LLVMTypeConverter converter,
ConversionPatternRewriter rewriter 
)
static

Utility function for bitfield ops: BitFieldInsert, BitFieldSExtract and BitFieldUExtract.

Broadcast Offset and Count to match the type of Base. If Base is of a vector type, construct a vector that has:

  • same number of elements as Base
  • each element has the type that is the same as the type of Offset or Count
  • each element has the same value as Offset or Count Then cast Offset and Count if their bit width is different from Base bit width.

Definition at line 177 of file SPIRVToLLVM.cpp.

References optionallyBroadcast(), and optionallyTruncateOrExtend().

Referenced by convertStructType().

◆ replaceWithLoadOrStore()

static LogicalResult replaceWithLoadOrStore ( Operation op,
ValueRange  operands,
ConversionPatternRewriter rewriter,
LLVMTypeConverter typeConverter,
unsigned  alignment,
bool  isVolatile,
bool  isNonTemporal 
)
static

Utility for spirv.Load and spirv.Store conversion.

Definition at line 221 of file SPIRVToLLVM.cpp.

References mlir::TypeConverter::convertType(), mlir::failure(), mlir::RewriterBase::replaceOpWithNewOp(), and mlir::success().

Referenced by convertStructType().

Variable Documentation

◆ kBinding

constexpr StringRef kBinding = "binding"
static

Hook for descriptor set and binding number encoding.

Definition at line 1576 of file SPIRVToLLVM.cpp.

Referenced by mlir::encodeBindAttribute().

◆ kDescriptorSet

constexpr StringRef kDescriptorSet = "descriptor_set"
static

Definition at line 1577 of file SPIRVToLLVM.cpp.

Referenced by mlir::encodeBindAttribute().