MLIR 24.0.0git
GPUBase.h
Go to the documentation of this file.
1//===- GPUBase.h - GPU types, attributes, and enums -------------*- 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 declares GPU types, attributes, and enums independently of GPU
10// operations.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_DIALECT_GPU_IR_GPUBASE_H
15#define MLIR_DIALECT_GPU_IR_GPUBASE_H
16
17// These representations can be inspected without linking GPU operations.
18// Constructing or parsing GPU types and attributes still requires loading
19// GPUDialect, which owns and registers them as before.
20// Attribute methods that build IR are provided by MLIRGPUDialect.
26
27namespace mlir::gpu {
28namespace detail {
29/// Register storage whose definitions are private to MLIRGPUBase. Attribute
30/// metadata and interfaces are registered by GPUDialect::initialize().
32} // namespace detail
33
35 : public Type::TypeBase<AsyncTokenType, Type, TypeStorage> {
36public:
37 // Used for generic hooks in TypeBase.
38 using Base::Base;
39
40 static constexpr StringLiteral name = "gpu.async_token";
41};
42
44 : public Type::TypeBase<NamedBarrierType, Type, TypeStorage> {
45public:
46 using Base::Base;
47
48 static constexpr StringLiteral name = "gpu.named_barrier";
49};
50
51/// MMAMatrixType storage and uniquing. Array is uniqued based on its shape
52/// and type.
58
59 /// The hash key for uniquing.
60 using KeyTy = std::tuple<ArrayRef<int64_t>, Type, StringRef>;
61 bool operator==(const KeyTy &key) const {
62 return key == KeyTy(getShape(), elementType, operand);
63 }
64
65 /// Construction.
67 const KeyTy &key) {
68 ArrayRef<int64_t> shape = allocator.copyInto(std::get<0>(key));
69 StringRef operand = allocator.copyInto(std::get<2>(key));
70
71 return new (allocator.allocate<MMAMatrixStorageType>())
72 MMAMatrixStorageType(shape.size(), shape.data(), std::get<1>(key),
73 operand);
74 }
75
79
80 StringRef getOperand() const { return operand; }
81
82 /// Reference to the shape of the MMA matrix.
84
85 /// Number of dimensions in the MMA matrix.
86 unsigned numDims;
87
88 /// Element type of elements held in the MMA matrix.
90
91 /// MMA operand that this MMAMatrix holds. The general form of operation this
92 /// type supports is given by the equation C += A*B. This field specifies
93 /// which operand in the given equation is held by this type. The valid values
94 /// are "AOp", "BOp" and "COp".
95 StringRef operand;
96};
97
98/// MMAMatrix represents a matrix held by a subgroup for matrix-matrix multiply
99/// accumulate operations. MMAMatrices are taken as direct operands by these
100/// operations and are also produced as results. These matrices are meant to
101/// reside in the registers. A limited number of pointwise operations can be
102/// performed on these matrices, i.e., operations which operate uniformly on
103/// all the elements in the matrix and do not change the order of matrix
104/// elements. The above conditions exist because the layout of matrix elements
105/// inside the matrix is opaque i.e., the elements may be present in the
106/// matrix in any order. The general usage of this type is shown as follows:-
107///
108/// %0 = gpu.subgroup_mma_load_matrix %arg0[%c0, %c0] {leadDimension = 16 :
109/// index} : memref<16x16xf16> -> !gpu.mma_matrix<16x16xf16, "AOp">
110///
111/// The MMAMatrixType describes the shape of the matrix being loaded and the
112/// operand being loaded too. The operand needs to be specified to aid the
113/// lowering of this type to dialects such as NVVM where each workitem may
114/// hold different amount of elements depending on the elementType of the
115/// matrix. For e.g., Each workitem holds 4 vector<2xf16>s for f16 data type
116/// and 8 f32s for f32 data type of MMAMatrix. Some other instances of usage
117/// are:-
118///
119/// %3 = gpu.subgroup_mma_compute %0, %1, %2 :
120/// !gpu.mma_matrix<16x16xf16, "AOp">, !gpu.mma_matrix<16x16xf16, "BOp">
121/// -> !gpu.mma_matrix<16x16xf32, "COp">
122///
123///
124/// gpu.subgroup_mma_store_matrix %3, %arg22[%c0, %c0] {leadDimension = 16
125/// : index}: !gpu.mma_matrix<16x16xf32, "COp">, memref<16x16xf32>
126///
127/// MMA matrices may be memref elements on targets that support storing them,
128/// for example SPIRV can represent arrays of MMA matrices.
129// TODO: consider moving this to ODS.
131 : public Type::TypeBase<MMAMatrixType, Type, MMAMatrixStorageType,
132 MemRefElementTypeInterface::Trait> {
133public:
134 using Base::Base;
135
136 static constexpr StringLiteral name = "gpu.mma_matrix";
137
138 /// Get MMAMatrixType and verify construction Invariants.
139 static MMAMatrixType get(ArrayRef<int64_t> shape, Type elementType,
140 StringRef operand);
141
142 /// Get MMAMatrixType at a particular location and verify construction
143 /// Invariants.
145 ArrayRef<int64_t> shape, Type elementType,
146 StringRef operand);
147
148 /// Check if a type is valid a MMAMatrixType elementType.
149 static bool isValidElementType(Type elementType);
150
151 /// Verify that shape and elementType are actually allowed for the
152 /// MMAMatrixType.
153 static LogicalResult
155 ArrayRef<int64_t> shape, Type elementType,
156 StringRef operand);
157
158 /// Get number of dims.
159 unsigned getNumDims() const;
160
161 /// Get shape of the matrix.
163
164 /// Get elementType of a single element.
165 Type getElementType() const;
166
167 /// The general form of operation this type supports is given by the equation
168 /// C += A*B. This function returns which operand in the given equation is
169 /// held by this type. String returned can be one of"AOp", "BOp" and "COp".
170 StringRef getOperand() const;
171};
172
173// Handle types for sparse.
175
177 : public Type::TypeBase<SparseDnTensorHandleType, Type, TypeStorage> {
178public:
179 using Base =
181 using Base::Base;
182
183 static constexpr StringLiteral name = "gpu.sparse.dntensor_handle";
184};
185
187 : public Type::TypeBase<SparseSpMatHandleType, Type, TypeStorage> {
188public:
190 using Base::Base;
191
192 static constexpr StringLiteral name = "gpu.sparse.spmat_handle";
193};
194
196 : public Type::TypeBase<SparseSpGEMMOpHandleType, Type, TypeStorage> {
197public:
198 using Base =
200 using Base::Base;
201
202 static constexpr StringLiteral name = "gpu.sparse.spgemmop_handle";
203};
204} // namespace mlir::gpu
205
206#define GET_ATTRDEF_CLASSES
207#include "mlir/Dialect/GPU/IR/GPUOpsAttributes.h.inc"
208
209#endif // MLIR_DIALECT_GPU_IR_GPUBASE_H
This class represents a diagnostic that is inflight and set to be reported.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
ArrayRef< T > copyInto(ArrayRef< T > elements)
Copy the specified array of elements into memory managed by our bump pointer allocator.
T * allocate()
Allocate an instance of the provided type.
TypeStorage()
This constructor is used by derived classes as part of the TypeUniquer.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
detail::StorageUserBase< ConcreteType, BaseType, StorageType, detail::TypeUniquer, Traits... > TypeBase
Utility class for implementing types.
Definition Types.h:79
StorageUserBase< ConcreteType, BaseType, StorageType, detail::TypeUniquer, Traits... > Base
static constexpr StringLiteral name
Definition GPUBase.h:40
MMAMatrix represents a matrix held by a subgroup for matrix-matrix multiply accumulate operations.
Definition GPUBase.h:132
ArrayRef< int64_t > getShape() const
Get shape of the matrix.
Definition GPUBase.cpp:129
static MMAMatrixType get(ArrayRef< int64_t > shape, Type elementType, StringRef operand)
Get MMAMatrixType and verify construction Invariants.
Definition GPUBase.cpp:114
Type getElementType() const
Get elementType of a single element.
Definition GPUBase.cpp:133
static bool isValidElementType(Type elementType)
Check if a type is valid a MMAMatrixType elementType.
Definition GPUBase.cpp:137
static LogicalResult verifyInvariants(function_ref< InFlightDiagnostic()> emitError, ArrayRef< int64_t > shape, Type elementType, StringRef operand)
Verify that shape and elementType are actually allowed for the MMAMatrixType.
Definition GPUBase.cpp:144
StringRef getOperand() const
The general form of operation this type supports is given by the equation C += A*B.
Definition GPUBase.cpp:135
static MMAMatrixType getChecked(function_ref< InFlightDiagnostic()> emitError, ArrayRef< int64_t > shape, Type elementType, StringRef operand)
Get MMAMatrixType at a particular location and verify construction Invariants.
Definition GPUBase.cpp:120
static constexpr StringLiteral name
Definition GPUBase.h:136
unsigned getNumDims() const
Get number of dims.
Definition GPUBase.cpp:127
static constexpr StringLiteral name
Definition GPUBase.h:48
Type::TypeBase< SparseDnTensorHandleType, Type, TypeStorage >::Base Base
Definition GPUBase.h:179
static constexpr StringLiteral name
Definition GPUBase.h:183
static constexpr StringLiteral name
Definition GPUBase.h:202
Type::TypeBase< SparseSpGEMMOpHandleType, Type, TypeStorage >::Base Base
Definition GPUBase.h:198
Type::TypeBase< SparseSpMatHandleType, Type, TypeStorage >::Base Base
Definition GPUBase.h:189
static constexpr StringLiteral name
Definition GPUBase.h:192
void registerAttributeStorage(MLIRContext *context)
Register storage whose definitions are private to MLIRGPUBase.
Definition GPUBase.cpp:502
SparseHandleKind
Definition GPUBase.h:174
StorageUniquer::StorageAllocator TypeStorageAllocator
This is a utility allocator used to allocate memory for instances of derived Types.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
StringRef operand
MMA operand that this MMAMatrix holds.
Definition GPUBase.h:95
static MMAMatrixStorageType * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition GPUBase.h:66
unsigned numDims
Number of dimensions in the MMA matrix.
Definition GPUBase.h:86
ArrayRef< int64_t > getShape() const
Definition GPUBase.h:76
const int64_t * dimShapes
Reference to the shape of the MMA matrix.
Definition GPUBase.h:83
StringRef getOperand() const
Definition GPUBase.h:80
MMAMatrixStorageType(unsigned numDims, const int64_t *dimShapes, Type elementType, StringRef operand)
Definition GPUBase.h:54
Type elementType
Element type of elements held in the MMA matrix.
Definition GPUBase.h:89
std::tuple< ArrayRef< int64_t >, Type, StringRef > KeyTy
The hash key for uniquing.
Definition GPUBase.h:60
bool operator==(const KeyTy &key) const
Definition GPUBase.h:61