MLIR  22.0.0git
Linalg.h
Go to the documentation of this file.
1 //===- Linalg.h - Linalg dialect --------------------------------*- 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 #ifndef MLIR_DIALECT_LINALG_IR_LINALG_H
10 #define MLIR_DIALECT_LINALG_IR_LINALG_H
11 
16 #include "mlir/IR/AffineExpr.h"
17 #include "mlir/IR/AffineMap.h"
18 #include "mlir/IR/BuiltinDialect.h"
19 #include "mlir/IR/BuiltinTypes.h"
20 #include "mlir/IR/Diagnostics.h"
21 #include "mlir/IR/Dialect.h"
23 #include "mlir/IR/TypeUtilities.h"
30 
31 #include "llvm/ADT/STLFunctionalExtras.h"
32 
33 #include <optional>
34 
35 namespace mlir {
36 namespace linalg {
37 
38 class LinalgOp;
39 
40 /// Returns the name mangled library call name to disambiguate between different
41 /// overloads at the C level. The name mangling scheme is basic and uses MLIR
42 /// type names:
43 /// 1. form a string which is the concatenation of the linalg op name with all
44 /// the operand type names, separate by underscores;
45 /// 2. drop the `linalg.` prefix, and the `<`, `>`, `?` symbols from the type.
46 /// Assumes `op` is a LinalgOp.
47 ///
48 /// Examples:
49 ///
50 /// 1. linalg.fill(%f, %A) : f32, memref<f32>
51 /// name mangles into `linalg_fill_f32_viewf32`
52 ///
53 /// 2. linalg.dot %A, %B, %C :
54 /// (memref<?xf32, stride_specification>,
55 /// memref<?xf32, stride_specification>, memref<f32>)
56 /// name mangles into `linalg_dot_viewxf32_viewxf32_viewf32`
57 ///
58 /// 3. linalg.matmul(...) :
59 /// memref<?x?xf32, stride_specification>,
60 /// memref<?x?xf32, stride_specification>,
61 /// memref<?x?xf32, stride_specification>
62 /// name mangles into `linalg_matmul_viewxxf32_viewxxf32_viewxxf32`
63 std::string generateLibraryCallName(Operation *op);
64 
65 /// Returns `num` AffineDimExpr dimensions at positions
66 /// [startIdx, startIdx + num) and increments `startIdx` to `startIdx + num`.
67 SmallVector<AffineExpr, 4> makeAffineDimExprs(unsigned num, unsigned &startIdx,
68  MLIRContext *context);
69 
70 /// Returns `maybeMap.get()` if `maybeMap` is set, otherwise returns the
71 /// symbol-less identity map of `rank`.
72 AffineMap extractOrIdentityMap(std::optional<AffineMap> maybeMap, unsigned rank,
73  MLIRContext *context);
74 
75 /// Return the vector that is the concatenation of `a` and `b`.
76 SmallVector<AffineExpr, 4> concat(ArrayRef<AffineExpr> a,
77  ArrayRef<AffineExpr> b);
78 
79 /// Create one memref::DimOp or tensor::DimOp depending on the type of `val`.
80 /// This is a polymorphic convenience function to abstract away the rank and
81 /// concrete type of `val`.
82 /// Asserts that `val` is a memref or tensor type.
83 Value createOrFoldDimOp(OpBuilder &b, Location loc, Value val, int64_t dim);
84 
85 /// Create one memref::DimOp or tensor::DimOp depending on the type of `val`.
86 /// This is a polymorphic convenience function to abstract away the rank and
87 /// concrete type of `val`.
88 /// Asserts that `val` is a memref or tensor type.
89 OpFoldResult createFoldedDimOp(OpBuilder &b, Location loc, Value val,
90  int64_t dim);
91 
92 } // namespace linalg
93 } // namespace mlir
94 
95 //===----------------------------------------------------------------------===//
96 // Linalg Dialect
97 //===----------------------------------------------------------------------===//
98 
99 #include "mlir/Dialect/Linalg/IR/LinalgOpsDialect.h.inc"
100 
101 //===----------------------------------------------------------------------===//
102 // Linalg Enums
103 //===----------------------------------------------------------------------===//
104 
105 #include "mlir/Dialect/Linalg/IR/LinalgOpsEnums.h.inc"
106 
107 namespace mlir {
108 namespace linalg {
109 
110 /// Converts the given `m` and `r` parameters to a WinogradConv2DFmr enumeration
111 /// value.
112 std::optional<WinogradConv2DFmr> getWinogradConv2DFmr(int64_t m, int64_t r);
113 
114 /// Converts the given WinogradConv2DFmr enumeration value to a pair of
115 /// m and r parameters.
116 std::pair<int64_t, int64_t> getFmrFromWinogradConv2DFmr(WinogradConv2DFmr fmr);
117 
118 } // namespace linalg
119 } // namespace mlir
120 
121 //===----------------------------------------------------------------------===//
122 // Linalg Attributes
123 //===----------------------------------------------------------------------===//
124 
125 #define GET_ATTRDEF_CLASSES
126 #include "mlir/Dialect/Linalg/IR/LinalgOpsAttrDefs.h.inc"
127 
128 //===----------------------------------------------------------------------===//
129 // Linalg Interfaces
130 //===----------------------------------------------------------------------===//
131 
133 
134 //===----------------------------------------------------------------------===//
135 // Linalg Dialect Operations
136 //===----------------------------------------------------------------------===//
137 
138 #define GET_OP_CLASSES
139 #include "mlir/Dialect/Linalg/IR/LinalgOps.h.inc"
140 
141 #define GET_OP_CLASSES
142 #include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.h.inc"
143 
144 #define GET_OP_CLASSES
145 #include "mlir/Dialect/Linalg/IR/LinalgRelayoutOps.h.inc"
146 
147 namespace mlir::linalg {
148 
149 /// Returns the outer shape in the packed domain before applying the
150 /// transposition.
151 template <typename OpTy,
152  typename = std::enable_if_t<std::is_same_v<OpTy, linalg::PackOp> ||
153  std::is_same_v<OpTy, linalg::UnPackOp>>>
154 SmallVector<int64_t> getPackedOuterShapeWithoutTransposition(OpTy packOrUnPack);
155 
156 /// Specialization of `linalg.matmul` op that has a transpose map on A
157 class MatmulTransposeAOp : public MatmulOp {
158  /// Create an affine map for a transpose-A matmul. Used only in the builders.
159  static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
160 
161 public:
162  using MatmulOp::MatmulOp;
163  static ::mlir::TypeID resolveTypeID() { return TypeID::get<MatmulOp>(); }
164 
165  /// Build a transpose A matmul.
166  static void build(OpBuilder &builder, OperationState &result,
167  ValueRange inputs, ValueRange outputs,
168  ArrayRef<NamedAttribute> attributes = {});
169 
170  static MatmulTransposeAOp create(OpBuilder &builder, Location location,
171  ValueRange inputs, ValueRange outputs,
172  ArrayRef<NamedAttribute> attributes = {});
173 
174  /// Build a transpose A matmul with a specific result type.
175  static void build(OpBuilder &builder, OperationState &result,
176  TypeRange resultTensorTypes, ValueRange inputs,
177  ValueRange outputs,
178  ArrayRef<NamedAttribute> attributes = {});
179 
180  static MatmulTransposeAOp create(OpBuilder &builder, Location location,
181  TypeRange resultTensorTypes,
182  ValueRange inputs, ValueRange outputs,
183  ArrayRef<NamedAttribute> attributes = {});
184 
185  /// Build a transpose A matmul with a specific result type and a cast type.
186  static void build(OpBuilder &builder, OperationState &result,
187  TypeRange resultTensorTypes, ValueRange inputs,
188  ValueRange outputs, Attribute cast,
189  ArrayRef<NamedAttribute> attributes = {});
190 
191  static MatmulTransposeAOp create(OpBuilder &builder, Location location,
192  TypeRange resultTensorTypes,
193  ValueRange inputs, ValueRange outputs,
194  Attribute cast,
195  ArrayRef<NamedAttribute> attributes = {});
196 
197  /// Checks if the affine map is the expected one for this operation
198  static bool isDefaultIndexingMaps(Attribute attr);
199 
200  static bool classof(Operation *op);
201 };
202 
203 /// Specialization of `linalg.matmul` op that has a transpose map on B
204 class MatmulTransposeBOp : public MatmulOp {
205  /// Create an affine map for a transpose-B matmul. Used only in the builders.
206  static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
207 
208 public:
209  using MatmulOp::MatmulOp;
210  static ::mlir::TypeID resolveTypeID() { return TypeID::get<MatmulOp>(); }
211 
212  /// Build a transpose B matmul.
213  static void build(OpBuilder &builder, OperationState &result,
214  ValueRange inputs, ValueRange outputs,
215  ArrayRef<NamedAttribute> attributes = {});
216 
217  static MatmulTransposeBOp create(OpBuilder &builder, Location location,
218  ValueRange inputs, ValueRange outputs,
219  ArrayRef<NamedAttribute> attributes = {});
220 
221  /// Build a transpose B matmul with a specific result type.
222  static void build(OpBuilder &builder, OperationState &result,
223  TypeRange resultTensorTypes, ValueRange inputs,
224  ValueRange outputs,
225  ArrayRef<NamedAttribute> attributes = {});
226 
227  static MatmulTransposeBOp create(OpBuilder &builder, Location location,
228  TypeRange resultTensorTypes,
229  ValueRange inputs, ValueRange outputs,
230  ArrayRef<NamedAttribute> attributes = {});
231 
232  /// Build a transpose B matmul with a specific result type and a cast type.
233  static void build(OpBuilder &builder, OperationState &result,
234  TypeRange resultTensorTypes, ValueRange inputs,
235  ValueRange outputs, Attribute cast,
236  ArrayRef<NamedAttribute> attributes = {});
237 
238  static MatmulTransposeBOp create(OpBuilder &builder, Location location,
239  TypeRange resultTensorTypes,
240  ValueRange inputs, ValueRange outputs,
241  Attribute cast,
242  ArrayRef<NamedAttribute> attributes = {});
243 
244  /// Checks if the affine map is the expected one for this operation
245  static bool isDefaultIndexingMaps(Attribute attr);
246 
247  static bool classof(Operation *op);
248 };
249 
250 /// Specialization of `linalg.batch_matmul` op that has a transpose map on A
252  /// Create an affine map for a transpose-A batch_matmul. Used only in the
253  /// builders.
254  static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
255 
256 public:
257  using BatchMatmulOp::BatchMatmulOp;
258  static ::mlir::TypeID resolveTypeID() { return TypeID::get<BatchMatmulOp>(); }
259 
260  /// Build a transpose A matmul.
261  static void build(OpBuilder &builder, OperationState &result,
262  ValueRange inputs, ValueRange outputs,
263  ArrayRef<NamedAttribute> attributes = {});
264 
265  static BatchMatmulTransposeAOp
266  create(OpBuilder &builder, Location location, ValueRange inputs,
267  ValueRange outputs, ArrayRef<NamedAttribute> attributes = {});
268 
269  /// Build a transpose A matmul with a specific result type.
270  static void build(OpBuilder &builder, OperationState &result,
271  TypeRange resultTensorTypes, ValueRange inputs,
272  ValueRange outputs,
273  ArrayRef<NamedAttribute> attributes = {});
274 
275  static BatchMatmulTransposeAOp
276  create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
277  ValueRange inputs, ValueRange outputs,
278  ArrayRef<NamedAttribute> attributes = {});
279 
280  /// Build a transpose A matmul with a specific result type and a cast type.
281  static void build(OpBuilder &builder, OperationState &result,
282  TypeRange resultTensorTypes, ValueRange inputs,
283  ValueRange outputs, Attribute cast,
284  ArrayRef<NamedAttribute> attributes = {});
285 
286  static BatchMatmulTransposeAOp
287  create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
288  ValueRange inputs, ValueRange outputs, Attribute cast,
289  ArrayRef<NamedAttribute> attributes = {});
290 
291  /// Checks if the affine map is the expected one for this operation
292  static bool isDefaultIndexingMaps(Attribute attr);
293 
294  static bool classof(Operation *op);
295 };
296 
297 /// Specialization of `linalg.batch_matmul` op that has a transpose map on B
299  /// Create an affine map for a transpose-B batch_matmul. Used only in the
300  /// builders.
301  static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
302 
303 public:
304  using BatchMatmulOp::BatchMatmulOp;
305  static ::mlir::TypeID resolveTypeID() { return TypeID::get<BatchMatmulOp>(); }
306 
307  /// Build a transpose B matmul.
308  static void build(OpBuilder &builder, OperationState &result,
309  ValueRange inputs, ValueRange outputs,
310  ArrayRef<NamedAttribute> attributes = {});
311 
312  static BatchMatmulTransposeBOp
313  create(OpBuilder &builder, Location location, ValueRange inputs,
314  ValueRange outputs, ArrayRef<NamedAttribute> attributes = {});
315 
316  /// Build a transpose B matmul with a specific result type.
317  static void build(OpBuilder &builder, OperationState &result,
318  TypeRange resultTensorTypes, ValueRange inputs,
319  ValueRange outputs,
320  ArrayRef<NamedAttribute> attributes = {});
321 
322  static BatchMatmulTransposeBOp
323  create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
324  ValueRange inputs, ValueRange outputs,
325  ArrayRef<NamedAttribute> attributes = {});
326 
327  /// Build a transpose B matmul with a specific result type and a cast type.
328  static void build(OpBuilder &builder, OperationState &result,
329  TypeRange resultTensorTypes, ValueRange inputs,
330  ValueRange outputs, Attribute cast,
331  ArrayRef<NamedAttribute> attributes = {});
332 
333  static BatchMatmulTransposeBOp
334  create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
335  ValueRange inputs, ValueRange outputs, Attribute cast,
336  ArrayRef<NamedAttribute> attributes = {});
337 
338  /// Checks if the affine map is the expected one for this operation
339  static bool isDefaultIndexingMaps(Attribute attr);
340 
341  static bool classof(Operation *op);
342 };
343 
344 } // namespace mlir::linalg
345 
346 #endif // MLIR_DIALECT_LINALG_IR_LINALG_H
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
This class helps build Operations.
Definition: Builders.h:207
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
Specialization of linalg.batch_matmul op that has a transpose map on A.
Definition: Linalg.h:251
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
Definition: LinalgOps.cpp:4158
static bool classof(Operation *op)
Definition: LinalgOps.cpp:4234
static BatchMatmulTransposeAOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Definition: LinalgOps.cpp:4181
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose A matmul.
Definition: LinalgOps.cpp:4172
Specialization of linalg.batch_matmul op that has a transpose map on B.
Definition: Linalg.h:298
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose B matmul.
Definition: LinalgOps.cpp:4265
static bool classof(Operation *op)
Definition: LinalgOps.cpp:4327
static BatchMatmulTransposeBOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Definition: LinalgOps.cpp:4274
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
Definition: LinalgOps.cpp:4251
Specialization of linalg.matmul op that has a transpose map on A.
Definition: Linalg.h:157
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
Definition: LinalgOps.cpp:3970
static MatmulTransposeAOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Definition: LinalgOps.cpp:3993
::mlir::TypeID resolveTypeID()
Definition: Linalg.h:163
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose A matmul.
Definition: LinalgOps.cpp:3984
static bool classof(Operation *op)
Definition: LinalgOps.cpp:4047
Specialization of linalg.matmul op that has a transpose map on B.
Definition: Linalg.h:204
::mlir::TypeID resolveTypeID()
Definition: Linalg.h:210
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose B matmul.
Definition: LinalgOps.cpp:4078
static MatmulTransposeBOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Definition: LinalgOps.cpp:4087
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
Definition: LinalgOps.cpp:4064
static bool classof(Operation *op)
Definition: LinalgOps.cpp:4141
SmallVector< AffineExpr, 4 > concat(ArrayRef< AffineExpr > a, ArrayRef< AffineExpr > b)
Return the vector that is the concatenation of a and b.
Definition: LinalgOps.cpp:2465
OpFoldResult createFoldedDimOp(OpBuilder &b, Location loc, Value val, int64_t dim)
Create one memref::DimOp or tensor::DimOp depending on the type of val.
Definition: LinalgOps.cpp:104
std::string generateLibraryCallName(Operation *op)
Returns the name mangled library call name to disambiguate between different overloads at the C level...
Definition: LinalgOps.cpp:2506
AffineMap extractOrIdentityMap(std::optional< AffineMap > maybeMap, unsigned rank, MLIRContext *context)
Returns maybeMap.get() if maybeMap is set, otherwise returns the symbol-less identity map of rank.
Definition: LinalgOps.cpp:2445
SmallVector< AffineExpr, 4 > makeAffineDimExprs(unsigned num, unsigned &startIdx, MLIRContext *context)
Returns num AffineDimExpr dimensions at positions [startIdx, startIdx + num) and increments startIdx ...
Definition: LinalgOps.cpp:2456
Value createOrFoldDimOp(OpBuilder &b, Location loc, Value val, int64_t dim)
Create one memref::DimOp or tensor::DimOp depending on the type of val.
Definition: LinalgOps.cpp:95
std::pair< int64_t, int64_t > getFmrFromWinogradConv2DFmr(WinogradConv2DFmr fmr)
Converts the given WinogradConv2DFmr enumeration value to a pair of m and r parameters.
Definition: LinalgOps.cpp:3748
std::optional< WinogradConv2DFmr > getWinogradConv2DFmr(int64_t m, int64_t r)
Converts the given m and r parameters to a WinogradConv2DFmr enumeration value.
Definition: LinalgOps.cpp:3738
SmallVector< int64_t > getPackedOuterShapeWithoutTransposition(OpTy packOrUnPack)
Returns the outer shape in the packed domain before applying the transposition.
Definition: LinalgOps.cpp:4959
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.