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.
21#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
22#include "mlir/Dialect/GPU/IR/GPUDialectDecl.h"
23#include "mlir/Dialect/SCF/IR/DeviceMappingInterface.h"
24#include "mlir/IR/BuiltinTypes.h"
25#include "mlir/Interfaces/FunctionInterfaces.h"
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().
31void registerAttributeStorage(MLIRContext *context);
32} // namespace detail
33
34class AsyncTokenType
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
43class NamedBarrierType
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.
53struct MMAMatrixStorageType : public TypeStorage {
54 MMAMatrixStorageType(unsigned numDims, const int64_t *dimShapes,
55 Type elementType, StringRef operand)
56 : dimShapes(dimShapes), numDims(numDims), elementType(elementType),
57 operand(operand) {}
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.
66 static MMAMatrixStorageType *construct(TypeStorageAllocator &allocator,
67 const KeyTy &key) {
68 ArrayRef<int64_t> shape = allocator.copyInto(elements: std::get<0>(t: key));
69 StringRef operand = allocator.copyInto(str: std::get<2>(t: key));
70
71 return new (allocator.allocate<MMAMatrixStorageType>())
72 MMAMatrixStorageType(shape.size(), shape.data(), std::get<1>(t: key),
73 operand);
74 }
75
76 ArrayRef<int64_t> getShape() const {
77 return ArrayRef<int64_t>(dimShapes, numDims);
78 }
79
80 StringRef getOperand() const { return operand; }
81
82 /// Reference to the shape of the MMA matrix.
83 const int64_t *dimShapes;
84
85 /// Number of dimensions in the MMA matrix.
86 unsigned numDims;
87
88 /// Element type of elements held in the MMA matrix.
89 Type elementType;
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.
130class MMAMatrixType
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.
144 static MMAMatrixType getChecked(function_ref<InFlightDiagnostic()> emitError,
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
154 verifyInvariants(function_ref<InFlightDiagnostic()> emitError,
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.
162 ArrayRef<int64_t> getShape() const;
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.
174enum class SparseHandleKind { SpMat, DnTensor, SpGEMMOp };
175
176class SparseDnTensorHandleType
177 : public Type::TypeBase<SparseDnTensorHandleType, Type, TypeStorage> {
178public:
179 using Base =
180 Type::TypeBase<SparseDnTensorHandleType, Type, TypeStorage>::Base;
181 using Base::Base;
182
183 static constexpr StringLiteral name = "gpu.sparse.dntensor_handle";
184};
185
186class SparseSpMatHandleType
187 : public Type::TypeBase<SparseSpMatHandleType, Type, TypeStorage> {
188public:
189 using Base = Type::TypeBase<SparseSpMatHandleType, Type, TypeStorage>::Base;
190 using Base::Base;
191
192 static constexpr StringLiteral name = "gpu.sparse.spmat_handle";
193};
194
195class SparseSpGEMMOpHandleType
196 : public Type::TypeBase<SparseSpGEMMOpHandleType, Type, TypeStorage> {
197public:
198 using Base =
199 Type::TypeBase<SparseSpGEMMOpHandleType, Type, TypeStorage>::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
210