MLIR  19.0.0git
Macros | Functions
SPIRVConversion.cpp File Reference
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.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/IR/BuiltinTypes.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
#include <functional>
#include <optional>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "mlir-spirv-conversion"
 

Functions

template<typename LabelT >
static LogicalResult checkExtensionRequirements (LabelT label, const spirv::TargetEnv &targetEnv, const spirv::SPIRVType::ExtensionArrayRefVector &candidates)
 Checks that candidates extension requirements are possible to be satisfied with the given targetEnv. More...
 
template<typename LabelT >
static LogicalResult checkCapabilityRequirements (LabelT label, const spirv::TargetEnv &targetEnv, const spirv::SPIRVType::CapabilityArrayRefVector &candidates)
 Checks that candidatescapability requirements are possible to be satisfied with the given isAllowedFn. More...
 
static bool needsExplicitLayout (spirv::StorageClass storageClass)
 Returns true if the given storageClass needs explicit layout when used in Shader environments. More...
 
static spirv::PointerType wrapInStructAndGetPointer (Type elementType, spirv::StorageClass storageClass)
 Wraps the given elementType in a struct and gets the pointer to the struct. More...
 
static spirv::ScalarType getIndexType (MLIRContext *ctx, const SPIRVConversionOptions &options)
 
static std::optional< int64_t > getTypeNumBytes (const SPIRVConversionOptions &options, Type type)
 
static Type convertScalarType (const spirv::TargetEnv &targetEnv, const SPIRVConversionOptions &options, spirv::ScalarType type, std::optional< spirv::StorageClass > storageClass={})
 Converts a scalar type to a suitable type under the given targetEnv. More...
 
static Type convertSubByteIntegerType (const SPIRVConversionOptions &options, IntegerType type)
 Converts a sub-byte integer type to i32 regardless of target environment. More...
 
static ShapedType convertIndexElementType (ShapedType type, const SPIRVConversionOptions &options)
 Returns a type with the same shape but with any index element type converted to the matching integer type. More...
 
static Type convertVectorType (const spirv::TargetEnv &targetEnv, const SPIRVConversionOptions &options, VectorType type, std::optional< spirv::StorageClass > storageClass={})
 Converts a vector type to a suitable type under the given targetEnv. More...
 
static Type convertComplexType (const spirv::TargetEnv &targetEnv, const SPIRVConversionOptions &options, ComplexType type, std::optional< spirv::StorageClass > storageClass={})
 
static Type convertTensorType (const spirv::TargetEnv &targetEnv, const SPIRVConversionOptions &options, TensorType type)
 Converts a tensor type to a suitable type under the given targetEnv. More...
 
static Type convertBoolMemrefType (const spirv::TargetEnv &targetEnv, const SPIRVConversionOptions &options, MemRefType type, spirv::StorageClass storageClass)
 
static Type convertSubByteMemrefType (const spirv::TargetEnv &targetEnv, const SPIRVConversionOptions &options, MemRefType type, spirv::StorageClass storageClass)
 
static Type convertMemrefType (const spirv::TargetEnv &targetEnv, const SPIRVConversionOptions &options, MemRefType type)
 
std::optional< ValuecastToSourceType (const spirv::TargetEnv &targetEnv, OpBuilder &builder, Type type, ValueRange inputs, Location loc)
 Converts the given inputs to the original source type considering the targetEnv's capabilities. More...
 
static spirv::GlobalVariableOp getBuiltinVariable (Block &body, spirv::BuiltIn builtin)
 
static std::string getBuiltinVarName (spirv::BuiltIn builtin, StringRef prefix, StringRef suffix)
 Gets name of global variable for a builtin. More...
 
static spirv::GlobalVariableOp getOrInsertBuiltinVariable (Block &body, Location loc, spirv::BuiltIn builtin, Type integerType, OpBuilder &builder, StringRef prefix, StringRef suffix)
 Gets or inserts a global variable for a builtin within body block. More...
 
static spirv::PointerType getPushConstantStorageType (unsigned elementCount, Builder &builder, Type indexType)
 Returns the pointer type for the push constant storage containing elementCount 32-bit integer values. More...
 
static spirv::GlobalVariableOp getPushConstantVariable (Block &body, unsigned elementCount)
 Returns the push constant varible containing elementCount 32-bit integer values in body. More...
 
static spirv::GlobalVariableOp getOrInsertPushConstantVariable (Location loc, Block &block, unsigned elementCount, OpBuilder &b, Type indexType)
 Gets or inserts a global variable for push constant storage containing elementCount 32-bit integer values in block. More...
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "mlir-spirv-conversion"

Definition at line 29 of file SPIRVConversion.cpp.

Function Documentation

◆ castToSourceType()

std::optional<Value> castToSourceType ( const spirv::TargetEnv targetEnv,
OpBuilder builder,
Type  type,
ValueRange  inputs,
Location  loc 
)

Converts the given inputs to the original source type considering the targetEnv's capabilities.

This function is meant to be used for source materialization in type converters. When the type converter needs to materialize a cast op back to some original source type, we need to check whether the original source type is supported in the target environment. If so, we can insert legal SPIR-V cast ops accordingly.

Note that in SPIR-V the capabilities for storage and compute are separate. This function is meant to handle the compute side; so it does not involve storage classes in its logic. The storage side is expected to be handled by MemRef conversion logic.

Definition at line 629 of file SPIRVConversion.cpp.

References checkCapabilityRequirements(), checkExtensionRequirements(), mlir::OpBuilder::create(), mlir::failed(), mlir::Operation::getResult(), mlir::Value::getType(), mlir::Type::isInteger(), and mlir::Type::isSignedInteger().

Referenced by mlir::SPIRVTypeConverter::SPIRVTypeConverter().

◆ checkCapabilityRequirements()

template<typename LabelT >
static LogicalResult checkCapabilityRequirements ( LabelT  label,
const spirv::TargetEnv targetEnv,
const spirv::SPIRVType::CapabilityArrayRefVector candidates 
)
static

Checks that candidatescapability requirements are possible to be satisfied with the given isAllowedFn.

candidates is a vector of vector for capability requirements following ((Capability::A OR Capability::B) AND (Capability::C OR Capability::D)) convention.

Definition at line 72 of file SPIRVConversion.cpp.

References mlir::spirv::TargetEnv::allows(), mlir::failure(), and mlir::success().

Referenced by castToSourceType().

◆ checkExtensionRequirements()

template<typename LabelT >
static LogicalResult checkExtensionRequirements ( LabelT  label,
const spirv::TargetEnv targetEnv,
const spirv::SPIRVType::ExtensionArrayRefVector candidates 
)
static

Checks that candidates extension requirements are possible to be satisfied with the given targetEnv.

candidates is a vector of vector for extension requirements following ((Extension::A OR Extension::B) AND (Extension::C OR Extension::D)) convention.

Definition at line 44 of file SPIRVConversion.cpp.

References mlir::spirv::TargetEnv::allows(), mlir::failure(), and mlir::success().

Referenced by castToSourceType().

◆ convertBoolMemrefType()

static Type convertBoolMemrefType ( const spirv::TargetEnv targetEnv,
const SPIRVConversionOptions options,
MemRefType  type,
spirv::StorageClass  storageClass 
)
static

◆ convertComplexType()

static Type convertComplexType ( const spirv::TargetEnv targetEnv,
const SPIRVConversionOptions options,
ComplexType  type,
std::optional< spirv::StorageClass >  storageClass = {} 
)
static

◆ convertIndexElementType()

static ShapedType convertIndexElementType ( ShapedType  type,
const SPIRVConversionOptions options 
)
static

Returns a type with the same shape but with any index element type converted to the matching integer type.

This is a noop when the element type is not the index type.

Definition at line 290 of file SPIRVConversion.cpp.

References getIndexType(), and options.

Referenced by convertMemrefType(), and convertTensorType().

◆ convertMemrefType()

static Type convertMemrefType ( const spirv::TargetEnv targetEnv,
const SPIRVConversionOptions options,
MemRefType  type 
)
static

◆ convertScalarType()

static Type convertScalarType ( const spirv::TargetEnv targetEnv,
const SPIRVConversionOptions options,
spirv::ScalarType  type,
std::optional< spirv::StorageClass >  storageClass = {} 
)
static

Converts a scalar type to a suitable type under the given targetEnv.

Definition at line 223 of file SPIRVConversion.cpp.

Referenced by convertBoolMemrefType(), convertMemrefType(), convertTensorType(), and mlir::SPIRVTypeConverter::SPIRVTypeConverter().

◆ convertSubByteIntegerType()

static Type convertSubByteIntegerType ( const SPIRVConversionOptions options,
IntegerType  type 
)
static

Converts a sub-byte integer type to i32 regardless of target environment.

Note that we don't recognize sub-byte types in spirv::ScalarType and use the above given that these sub-byte types are not supported at all in SPIR-V; there are no compute/storage capability for them like other supported integer types.

Definition at line 267 of file SPIRVConversion.cpp.

References mlir::get(), options, and mlir::Packed.

Referenced by convertSubByteMemrefType(), and mlir::SPIRVTypeConverter::SPIRVTypeConverter().

◆ convertSubByteMemrefType()

static Type convertSubByteMemrefType ( const spirv::TargetEnv targetEnv,
const SPIRVConversionOptions options,
MemRefType  type,
spirv::StorageClass  storageClass 
)
static

◆ convertTensorType()

static Type convertTensorType ( const spirv::TargetEnv targetEnv,
const SPIRVConversionOptions options,
TensorType  type 
)
static

Converts a tensor type to a suitable type under the given targetEnv.

Note that this is mainly for lowering constant tensors. In SPIR-V one can create composite constants with OpConstantComposite to embed relative large constant values and use OpCompositeExtract and OpCompositeInsert to manipulate, like what we do for vectors.

Definition at line 387 of file SPIRVConversion.cpp.

References convertIndexElementType(), convertScalarType(), mlir::spirv::ArrayType::get(), mlir::TensorType::getElementType(), getTypeNumBytes(), and options.

Referenced by mlir::SPIRVTypeConverter::SPIRVTypeConverter().

◆ convertVectorType()

static Type convertVectorType ( const spirv::TargetEnv targetEnv,
const SPIRVConversionOptions options,
VectorType  type,
std::optional< spirv::StorageClass >  storageClass = {} 
)
static

Converts a vector type to a suitable type under the given targetEnv.

Definition at line 301 of file SPIRVConversion.cpp.

Referenced by convertMemrefType(), and mlir::SPIRVTypeConverter::SPIRVTypeConverter().

◆ getBuiltinVariable()

static spirv::GlobalVariableOp getBuiltinVariable ( Block body,
spirv::BuiltIn  builtin 
)
static

Definition at line 820 of file SPIRVConversion.cpp.

References mlir::Block::getOps().

Referenced by getOrInsertBuiltinVariable().

◆ getBuiltinVarName()

static std::string getBuiltinVarName ( spirv::BuiltIn  builtin,
StringRef  prefix,
StringRef  suffix 
)
static

Gets name of global variable for a builtin.

Definition at line 838 of file SPIRVConversion.cpp.

Referenced by getOrInsertBuiltinVariable().

◆ getIndexType()

static spirv::ScalarType getIndexType ( MLIRContext ctx,
const SPIRVConversionOptions options 
)
static

Definition at line 121 of file SPIRVConversion.cpp.

References mlir::get(), and options.

Referenced by convertIndexElementType(), and mlir::SPIRVTypeConverter::getIndexType().

◆ getOrInsertBuiltinVariable()

static spirv::GlobalVariableOp getOrInsertBuiltinVariable ( Block body,
Location  loc,
spirv::BuiltIn  builtin,
Type  integerType,
OpBuilder builder,
StringRef  prefix,
StringRef  suffix 
)
static

Gets or inserts a global variable for a builtin within body block.

Definition at line 845 of file SPIRVConversion.cpp.

References mlir::OpBuilder::create(), mlir::emitError(), mlir::get(), mlir::spirv::PointerType::get(), getBuiltinVariable(), getBuiltinVarName(), and mlir::OpBuilder::setInsertionPointToStart().

◆ getOrInsertPushConstantVariable()

static spirv::GlobalVariableOp getOrInsertPushConstantVariable ( Location  loc,
Block block,
unsigned  elementCount,
OpBuilder b,
Type  indexType 
)
static

Gets or inserts a global variable for push constant storage containing elementCount 32-bit integer values in block.

Definition at line 944 of file SPIRVConversion.cpp.

References mlir::OpBuilder::atBlockBegin(), mlir::OpBuilder::create(), mlir::OpBuilder::getListener(), getPushConstantStorageType(), and getPushConstantVariable().

◆ getPushConstantStorageType()

static spirv::PointerType getPushConstantStorageType ( unsigned  elementCount,
Builder builder,
Type  indexType 
)
static

Returns the pointer type for the push constant storage containing elementCount 32-bit integer values.

Definition at line 908 of file SPIRVConversion.cpp.

References mlir::spirv::StructType::get(), mlir::spirv::ArrayType::get(), and mlir::spirv::PointerType::get().

Referenced by getOrInsertPushConstantVariable().

◆ getPushConstantVariable()

static spirv::GlobalVariableOp getPushConstantVariable ( Block body,
unsigned  elementCount 
)
static

Returns the push constant varible containing elementCount 32-bit integer values in body.

Returns null op if such an op does not exit.

Definition at line 919 of file SPIRVConversion.cpp.

References mlir::Block::getOps().

Referenced by getOrInsertPushConstantVariable().

◆ getTypeNumBytes()

static std::optional<int64_t> getTypeNumBytes ( const SPIRVConversionOptions options,
Type  type 
)
static

◆ needsExplicitLayout()

static bool needsExplicitLayout ( spirv::StorageClass  storageClass)
static

Returns true if the given storageClass needs explicit layout when used in Shader environments.

Definition at line 95 of file SPIRVConversion.cpp.

Referenced by convertBoolMemrefType(), convertMemrefType(), convertSubByteMemrefType(), and wrapInStructAndGetPointer().

◆ wrapInStructAndGetPointer()

static spirv::PointerType wrapInStructAndGetPointer ( Type  elementType,
spirv::StorageClass  storageClass 
)
static

Wraps the given elementType in a struct and gets the pointer to the struct.

This is used to satisfy Vulkan interface requirements.

Definition at line 110 of file SPIRVConversion.cpp.

References mlir::spirv::StructType::get(), mlir::spirv::PointerType::get(), and needsExplicitLayout().

Referenced by convertBoolMemrefType(), convertMemrefType(), and convertSubByteMemrefType().