MLIR 23.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// TODO: consider moving this to ODS.
139 : public Type::TypeBase<MMAMatrixType, Type, MMAMatrixStorageType> {
140public:
141 using Base::Base;
142
143 static constexpr StringLiteral name = "gpu.mma_matrix";
144
145 /// Get MMAMatrixType and verify construction Invariants.
146 static MMAMatrixType get(ArrayRef<int64_t> shape, Type elementType,
147 StringRef operand);
148
149 /// Get MMAMatrixType at a particular location and verify construction
150 /// Invariants.
152 ArrayRef<int64_t> shape, Type elementType,
153 StringRef operand);
154
155 /// Check if a type is valid a MMAMatrixType elementType.
156 static bool isValidElementType(Type elementType);
157
158 /// Verify that shape and elementType are actually allowed for the
159 /// MMAMatrixType.
160 static LogicalResult
162 ArrayRef<int64_t> shape, Type elementType,
163 StringRef operand);
164
165 /// Get number of dims.
166 unsigned getNumDims() const;
167
168 /// Get shape of the matrix.
170
171 /// Get elementType of a single element.
172 Type getElementType() const;
173
174 /// The general form of operation this type supports is given by the equation
175 /// C += A*B. This function returns which operand in the given equation is
176 /// held by this type. String returned can be one of"AOp", "BOp" and "COp".
177 StringRef getOperand() const;
178};
179
180// Adds a `gpu.async.token` to the front of the argument list.
181void addAsyncDependency(Operation *op, Value token);
182
183// Handle types for sparse.
185
187 : public Type::TypeBase<SparseDnTensorHandleType, Type, TypeStorage> {
188public:
189 using Base =
191 using Base::Base;
192
193 static constexpr StringLiteral name = "gpu.sparse.dntensor_handle";
194};
195
197 : public Type::TypeBase<SparseSpMatHandleType, Type, TypeStorage> {
198public:
200 using Base::Base;
201
202 static constexpr StringLiteral name = "gpu.sparse.spmat_handle";
203};
204
206 : public Type::TypeBase<SparseSpGEMMOpHandleType, Type, TypeStorage> {
207public:
208 using Base =
210 using Base::Base;
211
212 static constexpr StringLiteral name = "gpu.sparse.spgemmop_handle";
213};
214
215} // namespace gpu
216} // namespace mlir
217
218#include "mlir/Dialect/GPU/IR/GPUOpsEnums.h.inc"
219
220#include "mlir/Dialect/GPU/IR/GPUOpsDialect.h.inc"
221
222#include "mlir/Dialect/GPU/IR/GPUOpInterfaces.h.inc"
223
225
226#define GET_ATTRDEF_CLASSES
227#include "mlir/Dialect/GPU/IR/GPUOpsAttributes.h.inc"
228
229#define GET_OP_CLASSES
230#include "mlir/Dialect/GPU/IR/GPUOps.h.inc"
231
232namespace mlir::gpu {
233/// Retrieve the constant bounds for a given dimension and dimension kind
234/// from the context surrounding `op`, if known, and return them. This will
235/// check the bounds on an enclosing `gpu.launch`, an enclosing `gpu.func`, and
236/// any `gpu.known_*_size` on other function-like operations, in that order.
237std::optional<uint32_t>
238getKnownDimensionSizeAround(Operation *op, DimensionKind kind, Dimension dim);
239} // namespace mlir::gpu
240#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:139
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:143
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:189
static constexpr StringLiteral name
Definition GPUDialect.h:193
static constexpr StringLiteral name
Definition GPUDialect.h:212
Type::TypeBase< SparseSpGEMMOpHandleType, Type, TypeStorage >::Base Base
Definition GPUDialect.h:208
Type::TypeBase< SparseSpMatHandleType, Type, TypeStorage >::Base Base
Definition GPUDialect.h:199
static constexpr StringLiteral name
Definition GPUDialect.h:202
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