MLIR  18.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"
21 #include "mlir/IR/BuiltinTypes.h"
22 #include "mlir/IR/Dialect.h"
23 #include "mlir/IR/OpDefinition.h"
25 #include "mlir/IR/SymbolTable.h"
30 #include "llvm/ADT/STLExtras.h"
31 
32 namespace mlir {
33 namespace gpu {
34 
35 /// Utility class for the GPU dialect to represent triples of `Value`s
36 /// accessible through `.x`, `.y`, and `.z` similarly to CUDA notation.
37 struct KernelDim3 {
41 };
42 
44  : public Type::TypeBase<AsyncTokenType, Type, TypeStorage> {
45 public:
46  // Used for generic hooks in TypeBase.
47  using Base::Base;
48 };
49 
50 /// MMAMatrixType storage and uniquing. Array is uniqued based on its shape
51 /// and type.
53  MMAMatrixStorageType(unsigned numDims, const int64_t *dimShapes,
54  Type elementType, StringRef operand)
56  operand(operand) {}
57 
58  /// The hash key for uniquing.
59  using KeyTy = std::tuple<ArrayRef<int64_t>, Type, StringRef>;
60  bool operator==(const KeyTy &key) const {
61  return key == KeyTy(getShape(), elementType, operand);
62  }
63 
64  /// Construction.
66  const KeyTy &key) {
67  ArrayRef<int64_t> shape = allocator.copyInto(std::get<0>(key));
68  StringRef operand = allocator.copyInto(std::get<2>(key));
69 
70  return new (allocator.allocate<MMAMatrixStorageType>())
71  MMAMatrixStorageType(shape.size(), shape.data(), std::get<1>(key),
72  operand);
73  }
74 
77  }
78 
79  StringRef getOperand() const { return operand; }
80 
81  /// Reference to the shape of the MMA matrix.
82  const int64_t *dimShapes;
83 
84  /// Number of dimensions in the MMA matrix.
85  unsigned numDims;
86 
87  /// Element type of elements held in the MMA matrix.
89 
90  /// MMA operand that this MMAMatrix holds. The general form of operation this
91  /// type supports is given by the equation C += A*B. This field specifies
92  /// which operand in the given equation is held by this type. The valid values
93  /// are "AOp", "BOp" and "COp".
94  StringRef operand;
95 };
96 
97 /// MMAMatrix represents a matrix held by a subgroup for matrix-matrix multiply
98 /// accumulate operations. MMAMatrices are taken as direct operands by these
99 /// operations and are also produced as results. These matrices are meant to
100 /// reside in the registers. A limited number of pointwise operations can be
101 /// performed on these matrices, i.e., operations which operate uniformly on
102 /// all the elements in the matrix and do not change the order of matrix
103 /// elements. The above conditions exist because the layout of matrix elements
104 /// inside the matrix is opaque i.e., the elements may be present in the
105 /// matrix in any order. The general usage of this type is shown as follows:-
106 ///
107 /// %0 = gpu.subgroup_mma_load_matrix %arg0[%c0, %c0] {leadDimension = 16 :
108 /// index} : memref<16x16xf16> -> !gpu.mma_matrix<16x16xf16, "AOp">
109 ///
110 /// The MMAMatrixType describes the shape of the matrix being loaded and the
111 /// operand being loaded too. The operand needs to be specified to aid the
112 /// lowering of this type to dialects such as NVVM where each workitem may
113 /// hold different amount of elements depending on the elementType of the
114 /// matrix. For e.g., Each workitem holds 4 vector<2xf16>s for f16 data type
115 /// and 8 f32s for f32 data type of MMAMatrix. Some other instances of usage
116 /// are:-
117 ///
118 /// %3 = gpu.subgroup_mma_compute %0, %1, %2 :
119 /// !gpu.mma_matrix<16x16xf16, "AOp">, !gpu.mma_matrix<16x16xf16, "BOp">
120 /// -> !gpu.mma_matrix<16x16xf32, "COp">
121 ///
122 ///
123 /// gpu.subgroup_mma_store_matrix %3, %arg22[%c0, %c0] {leadDimension = 16
124 /// : index}: !gpu.mma_matrix<16x16xf32, "COp">, memref<16x16xf32>
125 // TODO: consider moving this to ODS.
127  : public Type::TypeBase<MMAMatrixType, Type, MMAMatrixStorageType> {
128 public:
129  using Base::Base;
130 
131  /// Get MMAMatrixType and verify construction Invariants.
132  static MMAMatrixType get(ArrayRef<int64_t> shape, Type elementType,
133  StringRef operand);
134 
135  /// Get MMAMatrixType at a particular location and verify construction
136  /// Invariants.
138  ArrayRef<int64_t> shape, Type elementType,
139  StringRef operand);
140 
141  /// Check if a type is valid a MMAMatrixType elementType.
142  static bool isValidElementType(Type elementType);
143 
144  /// Verify that shape and elementType are actually allowed for the
145  /// MMAMatrixType.
147  ArrayRef<int64_t> shape, Type elementType,
148  StringRef operand);
149 
150  /// Get number of dims.
151  unsigned getNumDims() const;
152 
153  /// Get shape of the matrix.
154  ArrayRef<int64_t> getShape() const;
155 
156  /// Get elementType of a single element.
157  Type getElementType() const;
158 
159  /// The general form of operation this type supports is given by the equation
160  /// C += A*B. This function returns which operand in the given equation is
161  /// held by this type. String returned can be one of"AOp", "BOp" and "COp".
162  StringRef getOperand() const;
163 };
164 
165 // Adds a `gpu.async.token` to the front of the argument list.
166 void addAsyncDependency(Operation *op, Value token);
167 
168 // Handle types for sparse.
170 
171 template <SparseHandleKind K>
173  : public Type::TypeBase<SparseHandleType<K>, Type, TypeStorage> {
174 public:
175  using Base =
177  using Base::Base;
178 };
179 
183 
184 } // namespace gpu
185 } // namespace mlir
186 
187 #include "mlir/Dialect/GPU/IR/GPUOpsEnums.h.inc"
188 
189 #include "mlir/Dialect/GPU/IR/GPUOpsDialect.h.inc"
190 
191 #include "mlir/Dialect/GPU/IR/GPUOpInterfaces.h.inc"
192 
194 
195 #define GET_ATTRDEF_CLASSES
196 #include "mlir/Dialect/GPU/IR/GPUOpsAttributes.h.inc"
197 
198 #define GET_OP_CLASSES
199 #include "mlir/Dialect/GPU/IR/GPUOps.h.inc"
200 
201 #endif // MLIR_DIALECT_GPU_IR_GPUDIALECT_H
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:308
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This is a utility allocator used to allocate memory for instances of derived types.
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.
Base storage class appearing in a Type.
Definition: TypeSupport.h:153
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
Utility class for implementing users of storage classes uniqued by a StorageUniquer.
MMAMatrix represents a matrix held by a subgroup for matrix-matrix multiply accumulate operations.
Definition: GPUDialect.h:127
ArrayRef< int64_t > getShape() const
Get shape of the matrix.
Definition: GPUDialect.cpp:131
static MMAMatrixType get(ArrayRef< int64_t > shape, Type elementType, StringRef operand)
Get MMAMatrixType and verify construction Invariants.
Definition: GPUDialect.cpp:116
Type getElementType() const
Get elementType of a single element.
Definition: GPUDialect.cpp:135
static bool isValidElementType(Type elementType)
Check if a type is valid a MMAMatrixType elementType.
Definition: GPUDialect.cpp:139
StringRef getOperand() const
The general form of operation this type supports is given by the equation C += A*B.
Definition: GPUDialect.cpp:137
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: GPUDialect.cpp:122
static LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, ArrayRef< int64_t > shape, Type elementType, StringRef operand)
Verify that shape and elementType are actually allowed for the MMAMatrixType.
Definition: GPUDialect.cpp:146
unsigned getNumDims() const
Get number of dims.
Definition: GPUDialect.cpp:129
typename Type::TypeBase< SparseHandleType< K >, Type, TypeStorage >::Base Base
Definition: GPUDialect.h:176
void addAsyncDependency(Operation *op, Value token)
Definition: GPUDialect.cpp:594
This header declares functions that assist transformations in the MemRef dialect.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Utility class for the GPU dialect to represent triples of Values accessible through ....
Definition: GPUDialect.h:37
MMAMatrixType storage and uniquing.
Definition: GPUDialect.h:52
StringRef operand
MMA operand that this MMAMatrix holds.
Definition: GPUDialect.h:94
ArrayRef< int64_t > getShape() const
Definition: GPUDialect.h:75
static MMAMatrixStorageType * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition: GPUDialect.h:65
unsigned numDims
Number of dimensions in the MMA matrix.
Definition: GPUDialect.h:85
const int64_t * dimShapes
Reference to the shape of the MMA matrix.
Definition: GPUDialect.h:82
StringRef getOperand() const
Definition: GPUDialect.h:79
MMAMatrixStorageType(unsigned numDims, const int64_t *dimShapes, Type elementType, StringRef operand)
Definition: GPUDialect.h:53
Type elementType
Element type of elements held in the MMA matrix.
Definition: GPUDialect.h:88
std::tuple< ArrayRef< int64_t >, Type, StringRef > KeyTy
The hash key for uniquing.
Definition: GPUDialect.h:59
bool operator==(const KeyTy &key) const
Definition: GPUDialect.h:60