MLIR 24.0.0git
GPUDialect.h
Go to the documentation of this file.
1//===- GPUDialect.h - MLIR Dialect for GPU Kernels --------------*- 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 the GPU kernel-related operations and puts them in the
10// corresponding dialect.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_DIALECT_GPU_IR_GPUDIALECT_H
15#define MLIR_DIALECT_GPU_IR_GPUDIALECT_H
16
20#include "mlir/IR/Builders.h"
22#include "mlir/IR/Dialect.h"
26#include "mlir/IR/SymbolTable.h"
32#include "llvm/ADT/STLExtras.h"
33
34namespace mlir {
35namespace gpu {
36
37/// Utility class for the GPU dialect to represent triples of `Value`s
38/// accessible through `.x`, `.y`, and `.z` similarly to CUDA notation.
44
46 : public Type::TypeBase<AsyncTokenType, Type, TypeStorage> {
47public:
48 // Used for generic hooks in TypeBase.
49 using Base::Base;
50
51 static constexpr StringLiteral name = "gpu.async_token";
52};
53
55 : public Type::TypeBase<NamedBarrierType, Type, TypeStorage> {
56public:
57 using Base::Base;
58
59 static constexpr StringLiteral name = "gpu.named_barrier";
60};
61
62/// MMAMatrixType storage and uniquing. Array is uniqued based on its shape
63/// and type.
69
70 /// The hash key for uniquing.
71 using KeyTy = std::tuple<ArrayRef<int64_t>, Type, StringRef>;
72 bool operator==(const KeyTy &key) const {
73 return key == KeyTy(getShape(), elementType, operand);
74 }
75
76 /// Construction.
78 const KeyTy &key) {
79 ArrayRef<int64_t> shape = allocator.copyInto(std::get<0>(key));
80 StringRef operand = allocator.copyInto(std::get<2>(key));
81
82 return new (allocator.allocate<MMAMatrixStorageType>())
83 MMAMatrixStorageType(shape.size(), shape.data(), std::get<1>(key),
84 operand);
85 }
86
90
91 StringRef getOperand() const { return operand; }
92
93 /// Reference to the shape of the MMA matrix.
95
96 /// Number of dimensions in the MMA matrix.
97 unsigned numDims;
98
99 /// Element type of elements held in the MMA matrix.
101
102 /// MMA operand that this MMAMatrix holds. The general form of operation this
103 /// type supports is given by the equation C += A*B. This field specifies
104 /// which operand in the given equation is held by this type. The valid values
105 /// are "AOp", "BOp" and "COp".
106 StringRef operand;
107};
108
109/// MMAMatrix represents a matrix held by a subgroup for matrix-matrix multiply
110/// accumulate operations. MMAMatrices are taken as direct operands by these
111/// operations and are also produced as results. These matrices are meant to
112/// reside in the registers. A limited number of pointwise operations can be
113/// performed on these matrices, i.e., operations which operate uniformly on
114/// all the elements in the matrix and do not change the order of matrix
115/// elements. The above conditions exist because the layout of matrix elements
116/// inside the matrix is opaque i.e., the elements may be present in the
117/// matrix in any order. The general usage of this type is shown as follows:-
118///
119/// %0 = gpu.subgroup_mma_load_matrix %arg0[%c0, %c0] {leadDimension = 16 :
120/// index} : memref<16x16xf16> -> !gpu.mma_matrix<16x16xf16, "AOp">
121///
122/// The MMAMatrixType describes the shape of the matrix being loaded and the
123/// operand being loaded too. The operand needs to be specified to aid the
124/// lowering of this type to dialects such as NVVM where each workitem may
125/// hold different amount of elements depending on the elementType of the
126/// matrix. For e.g., Each workitem holds 4 vector<2xf16>s for f16 data type
127/// and 8 f32s for f32 data type of MMAMatrix. Some other instances of usage
128/// are:-
129///
130/// %3 = gpu.subgroup_mma_compute %0, %1, %2 :
131/// !gpu.mma_matrix<16x16xf16, "AOp">, !gpu.mma_matrix<16x16xf16, "BOp">
132/// -> !gpu.mma_matrix<16x16xf32, "COp">
133///
134///
135/// gpu.subgroup_mma_store_matrix %3, %arg22[%c0, %c0] {leadDimension = 16
136/// : index}: !gpu.mma_matrix<16x16xf32, "COp">, memref<16x16xf32>
137///
138/// MMA matrices may be memref elements on targets that support storing them,
139/// for example SPIRV can represent arrays of MMA matrices.
140// TODO: consider moving this to ODS.
142 : public Type::TypeBase<MMAMatrixType, Type, MMAMatrixStorageType,
143 MemRefElementTypeInterface::Trait> {
144public:
145 using Base::Base;
146
147 static constexpr StringLiteral name = "gpu.mma_matrix";
148
149 /// Get MMAMatrixType and verify construction Invariants.
150 static MMAMatrixType get(ArrayRef<int64_t> shape, Type elementType,
151 StringRef operand);
152
153 /// Get MMAMatrixType at a particular location and verify construction
154 /// Invariants.
156 ArrayRef<int64_t> shape, Type elementType,
157 StringRef operand);
158
159 /// Check if a type is valid a MMAMatrixType elementType.
160 static bool isValidElementType(Type elementType);
161
162 /// Verify that shape and elementType are actually allowed for the
163 /// MMAMatrixType.
164 static LogicalResult
166 ArrayRef<int64_t> shape, Type elementType,
167 StringRef operand);
168
169 /// Get number of dims.
170 unsigned getNumDims() const;
171
172 /// Get shape of the matrix.
174
175 /// Get elementType of a single element.
176 Type getElementType() const;
177
178 /// The general form of operation this type supports is given by the equation
179 /// C += A*B. This function returns which operand in the given equation is
180 /// held by this type. String returned can be one of"AOp", "BOp" and "COp".
181 StringRef getOperand() const;
182};
183
184// Adds a `gpu.async.token` to the front of the argument list.
185void addAsyncDependency(Operation *op, Value token);
186
187// Handle types for sparse.
189
191 : public Type::TypeBase<SparseDnTensorHandleType, Type, TypeStorage> {
192public:
193 using Base =
195 using Base::Base;
196
197 static constexpr StringLiteral name = "gpu.sparse.dntensor_handle";
198};
199
201 : public Type::TypeBase<SparseSpMatHandleType, Type, TypeStorage> {
202public:
204 using Base::Base;
205
206 static constexpr StringLiteral name = "gpu.sparse.spmat_handle";
207};
208
210 : public Type::TypeBase<SparseSpGEMMOpHandleType, Type, TypeStorage> {
211public:
212 using Base =
214 using Base::Base;
215
216 static constexpr StringLiteral name = "gpu.sparse.spgemmop_handle";
217};
218
219} // namespace gpu
220} // namespace mlir
221
222#include "mlir/Dialect/GPU/IR/GPUOpsEnums.h.inc"
223
224#include "mlir/Dialect/GPU/IR/GPUOpsDialect.h.inc"
225
226#include "mlir/Dialect/GPU/IR/GPUOpInterfaces.h.inc"
227
229
230#define GET_ATTRDEF_CLASSES
231#include "mlir/Dialect/GPU/IR/GPUOpsAttributes.h.inc"
232
233#define GET_OP_CLASSES
234#include "mlir/Dialect/GPU/IR/GPUOps.h.inc"
235
236namespace mlir::gpu {
237/// Retrieve the constant bounds for a given dimension and dimension kind
238/// from the context surrounding `op`, if known, and return them. This will
239/// check the bounds on an enclosing `gpu.launch`, an enclosing `gpu.func`, and
240/// any `gpu.known_*_size` on other function-like operations, in that order.
241std::optional<uint32_t>
242getKnownDimensionSizeAround(Operation *op, DimensionKind kind, Dimension dim);
243} // namespace mlir::gpu
244#endif // MLIR_DIALECT_GPU_IR_GPUDIALECT_H
This class represents a diagnostic that is inflight and set to be reported.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
StorageUserBase< ConcreteType, BaseType, StorageType, detail::TypeUniquer, Traits... > Base
static constexpr StringLiteral name
Definition GPUDialect.h:51
MMAMatrix represents a matrix held by a subgroup for matrix-matrix multiply accumulate operations.
Definition GPUDialect.h:143
ArrayRef< int64_t > getShape() const
Get shape of the matrix.
static MMAMatrixType get(ArrayRef< int64_t > shape, Type elementType, StringRef operand)
Get MMAMatrixType and verify construction Invariants.
Type getElementType() const
Get elementType of a single element.
static bool isValidElementType(Type elementType)
Check if a type is valid a MMAMatrixType elementType.
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.
StringRef getOperand() const
The general form of operation this type supports is given by the equation C += A*B.
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.
static constexpr StringLiteral name
Definition GPUDialect.h:147
unsigned getNumDims() const
Get number of dims.
static constexpr StringLiteral name
Definition GPUDialect.h:59
Type::TypeBase< SparseDnTensorHandleType, Type, TypeStorage >::Base Base
Definition GPUDialect.h:193
static constexpr StringLiteral name
Definition GPUDialect.h:197
static constexpr StringLiteral name
Definition GPUDialect.h:216
Type::TypeBase< SparseSpGEMMOpHandleType, Type, TypeStorage >::Base Base
Definition GPUDialect.h:212
Type::TypeBase< SparseSpMatHandleType, Type, TypeStorage >::Base Base
Definition GPUDialect.h:203
static constexpr StringLiteral name
Definition GPUDialect.h:206
std::optional< uint32_t > getKnownDimensionSizeAround(Operation *op, DimensionKind kind, Dimension dim)
Retrieve the constant bounds for a given dimension and dimension kind from the context surrounding op...
void addAsyncDependency(Operation *op, Value token)
Include the generated interface declarations.
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:147
Utility class for the GPU dialect to represent triples of Values accessible through ....
Definition GPUDialect.h:39
StringRef operand
MMA operand that this MMAMatrix holds.
Definition GPUDialect.h:106
static MMAMatrixStorageType * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition GPUDialect.h:77
unsigned numDims
Number of dimensions in the MMA matrix.
Definition GPUDialect.h:97
ArrayRef< int64_t > getShape() const
Definition GPUDialect.h:87
const int64_t * dimShapes
Reference to the shape of the MMA matrix.
Definition GPUDialect.h:94
StringRef getOperand() const
Definition GPUDialect.h:91
MMAMatrixStorageType(unsigned numDims, const int64_t *dimShapes, Type elementType, StringRef operand)
Definition GPUDialect.h:65
Type elementType
Element type of elements held in the MMA matrix.
Definition GPUDialect.h:100
std::tuple< ArrayRef< int64_t >, Type, StringRef > KeyTy
The hash key for uniquing.
Definition GPUDialect.h:71
bool operator==(const KeyTy &key) const
Definition GPUDialect.h:72