MLIR 22.0.0git
LayoutUtils.h
Go to the documentation of this file.
1//===-- LayoutUtils.h - Vulkan Layout Util functions ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines utilities used to get alignment and layout information for
10// types in SPIR-V dialect.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_DIALECT_SPIRV_UTILS_LAYOUTUTILS_H_
15#define MLIR_DIALECT_SPIRV_UTILS_LAYOUTUTILS_H_
16
17#include <cstdint>
18
19namespace mlir {
20class Type;
21class VectorType;
22
23namespace spirv {
24class ArrayType;
26class StructType;
27class MatrixType;
28} // namespace spirv
29
30/// According to the Vulkan spec "15.6.4. Offset and Stride Assignment":
31/// "There are different alignment requirements depending on the specific
32/// resources and on the features enabled on the device."
33///
34/// There are 3 types of alignment: scalar, base, extended.
35/// See the spec for details.
36///
37/// Note: Even if scalar alignment is supported, it is generally more
38/// performant to use the base alignment. So here the calculation is based on
39/// base alignment.
40///
41/// The memory layout must obey the following rules:
42/// 1. The Offset decoration of any member must be a multiple of its alignment.
43/// 2. Any ArrayStride or MatrixStride decoration must be a multiple of the
44/// alignment of the array or matrix as defined above.
45///
46/// According to the SPIR-V spec:
47/// "The ArrayStride, MatrixStride, and Offset decorations must be large
48/// enough to hold the size of the objects they affect (that is, specifying
49/// overlap is invalid)."
51public:
52 using Size = uint64_t;
53
54 /// Returns a new StructType with layout decoration.
56
57 /// Checks whether a type is legal in terms of Vulkan layout info
58 /// decoration. A type is dynamically illegal if it's a composite type in the
59 /// StorageBuffer, PhysicalStorageBuffer, Uniform, and PushConstant Storage
60 /// Classes without layout information.
61 static bool isLegalType(Type type);
62
63private:
64 /// Returns a new type with layout decoration. Assigns the type size in bytes
65 /// to the `size`. Assigns the type alignment in bytes to the `alignment`.
66 static Type decorateType(Type type, Size &size, Size &alignment);
67
68 static Type decorateType(VectorType vectorType, Size &size, Size &alignment);
69 static Type decorateType(spirv::ArrayType arrayType, Size &size,
70 Size &alignment);
71 static Type decorateType(spirv::MatrixType matrixType, Size &size,
72 Size &alignment);
73 static Type decorateType(spirv::RuntimeArrayType arrayType, Size &alignment);
75 Size &size, Size &alignment);
76
77 /// Calculates the alignment for the given scalar type.
78 static Size getScalarTypeAlignment(Type scalarType);
79};
80
81} // namespace mlir
82
83#endif // MLIR_DIALECT_SPIRV_UTILS_LAYOUTUTILS_H_
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
According to the Vulkan spec "15.6.4. Offset and Stride Assignment": "There are different alignment r...
Definition LayoutUtils.h:50
static bool isLegalType(Type type)
Checks whether a type is legal in terms of Vulkan layout info decoration.
static spirv::StructType decorateType(spirv::StructType structType)
Returns a new StructType with layout decoration.
SPIR-V struct type.
Definition SPIRVTypes.h:251
Include the generated interface declarations.