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