MLIR 24.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
17#include "mlir/IR/AffineExpr.h"
18#include "mlir/IR/AffineMap.h"
21#include "mlir/IR/Diagnostics.h"
30
31#include "llvm/ADT/STLFunctionalExtras.h"
32
33#include <optional>
34
35namespace mlir {
36namespace linalg {
37
38class 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`
63std::string generateLibraryCallName(Operation *op);
64
65/// Returns `num` AffineDimExpr dimensions at positions
66/// [startIdx, startIdx + num) and increments `startIdx` to `startIdx + num`.
67SmallVector<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`.
72AffineMap extractOrIdentityMap(std::optional<AffineMap> maybeMap, unsigned rank,
73 MLIRContext *context);
74
75/// Return the vector that is the concatenation of `a` and `b`.
76SmallVector<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.
83Value 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.
89OpFoldResult createFoldedDimOp(OpBuilder &b, Location loc, Value val,
90 int64_t dim);
91
92} // namespace linalg
93} // namespace mlir
94
95//===----------------------------------------------------------------------===//
96// Linalg Enums
97//===----------------------------------------------------------------------===//
98
99#include "mlir/Dialect/Linalg/IR/LinalgOpsEnums.h.inc"
100
101namespace mlir {
102namespace linalg {
103
104/// Converts the given `m` and `r` parameters to a WinogradConv2DFmr enumeration
105/// value.
106std::optional<WinogradConv2DFmr> getWinogradConv2DFmr(int64_t m, int64_t r);
107
108/// Converts the given WinogradConv2DFmr enumeration value to a pair of
109/// m and r parameters.
110std::pair<int64_t, int64_t> getFmrFromWinogradConv2DFmr(WinogradConv2DFmr fmr);
111
112} // namespace linalg
113} // namespace mlir
114
115//===----------------------------------------------------------------------===//
116// Linalg Attributes
117//===----------------------------------------------------------------------===//
118
119#define GET_ATTRDEF_CLASSES
120#include "mlir/Dialect/Linalg/IR/LinalgOpsAttrDefs.h.inc"
121
122//===----------------------------------------------------------------------===//
123// Linalg Interfaces
124//===----------------------------------------------------------------------===//
125
127
128//===----------------------------------------------------------------------===//
129// Linalg Dialect Operations
130//===----------------------------------------------------------------------===//
131
132#define GET_OP_CLASSES
133#include "mlir/Dialect/Linalg/IR/LinalgOps.h.inc"
134
135#define GET_OP_CLASSES
136#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.h.inc"
137
138#define GET_OP_CLASSES
139#include "mlir/Dialect/Linalg/IR/LinalgRelayoutOps.h.inc"
140
141namespace mlir::linalg {
142
143/// Returns the outer shape in the packed domain before applying the
144/// transposition.
145template <typename OpTy,
146 typename = std::enable_if_t<std::is_same_v<OpTy, linalg::PackOp> ||
147 std::is_same_v<OpTy, linalg::UnPackOp>>>
148SmallVector<int64_t> getPackedOuterShapeWithoutTransposition(OpTy packOrUnPack);
149
150/// Elementwise Arity and Kind groups.
152 // The enum class {Unary, Binary, Ternary, ..}
153 ElementwiseArityGroup arityGroup;
154
155 // The kind (e.g. `exp` or `add`) belonging to the arity group.
156 union Kind {
157 UnaryFn unaryFn;
158 BinaryFn binaryFn;
159 TernaryFn ternaryFn;
160 } kind;
161};
162ArityGroupAndKind getArityGroupAndKind(ElementwiseKind kind);
163
164/// Specialization of `linalg.matmul` op that has a transpose map on A
166 /// Create an affine map for a transpose-A matmul. Used only in the builders.
167 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
168
169public:
170 using MatmulOp::MatmulOp;
171 static ::mlir::TypeID resolveTypeID() { return TypeID::get<MatmulOp>(); }
172
173 /// Build a transpose A matmul.
174 static void build(OpBuilder &builder, OperationState &result,
175 ValueRange inputs, ValueRange outputs,
176 ArrayRef<NamedAttribute> attributes = {});
177
178 static MatmulTransposeAOp create(OpBuilder &builder, Location location,
179 ValueRange inputs, ValueRange outputs,
180 ArrayRef<NamedAttribute> attributes = {});
181
182 /// Build a transpose A matmul with a specific result type.
183 static void build(OpBuilder &builder, OperationState &result,
184 TypeRange resultTensorTypes, ValueRange inputs,
185 ValueRange outputs,
186 ArrayRef<NamedAttribute> attributes = {});
187
188 static MatmulTransposeAOp create(OpBuilder &builder, Location location,
189 TypeRange resultTensorTypes,
190 ValueRange inputs, ValueRange outputs,
191 ArrayRef<NamedAttribute> attributes = {});
192
193 /// Build a transpose A matmul with a specific result type and a cast type.
194 static void build(OpBuilder &builder, OperationState &result,
195 TypeRange resultTensorTypes, ValueRange inputs,
196 ValueRange outputs, Attribute cast,
197 ArrayRef<NamedAttribute> attributes = {});
198
199 static MatmulTransposeAOp create(OpBuilder &builder, Location location,
200 TypeRange resultTensorTypes,
201 ValueRange inputs, ValueRange outputs,
202 Attribute cast,
203 ArrayRef<NamedAttribute> attributes = {});
204
205 /// Checks if the affine map is the expected one for this operation
206 static bool isDefaultIndexingMaps(Attribute attr);
207
208 static bool classof(Operation *op);
209};
210
211/// Specialization of `linalg.matmul` op that has a transpose map on B
213 /// Create an affine map for a transpose-B matmul. Used only in the builders.
214 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
215
216public:
217 using MatmulOp::MatmulOp;
218 static ::mlir::TypeID resolveTypeID() { return TypeID::get<MatmulOp>(); }
219
220 /// Build a transpose B matmul.
221 static void build(OpBuilder &builder, OperationState &result,
222 ValueRange inputs, ValueRange outputs,
223 ArrayRef<NamedAttribute> attributes = {});
224
225 static MatmulTransposeBOp create(OpBuilder &builder, Location location,
226 ValueRange inputs, ValueRange outputs,
227 ArrayRef<NamedAttribute> attributes = {});
228
229 /// Build a transpose B matmul with a specific result type.
230 static void build(OpBuilder &builder, OperationState &result,
231 TypeRange resultTensorTypes, ValueRange inputs,
232 ValueRange outputs,
233 ArrayRef<NamedAttribute> attributes = {});
234
235 static MatmulTransposeBOp create(OpBuilder &builder, Location location,
236 TypeRange resultTensorTypes,
237 ValueRange inputs, ValueRange outputs,
238 ArrayRef<NamedAttribute> attributes = {});
239
240 /// Build a transpose B matmul with a specific result type and a cast type.
241 static void build(OpBuilder &builder, OperationState &result,
242 TypeRange resultTensorTypes, ValueRange inputs,
243 ValueRange outputs, Attribute cast,
244 ArrayRef<NamedAttribute> attributes = {});
245
246 static MatmulTransposeBOp create(OpBuilder &builder, Location location,
247 TypeRange resultTensorTypes,
248 ValueRange inputs, ValueRange outputs,
249 Attribute cast,
250 ArrayRef<NamedAttribute> attributes = {});
251
252 /// Checks if the affine map is the expected one for this operation
253 static bool isDefaultIndexingMaps(Attribute attr);
254
255 static bool classof(Operation *op);
256};
257
258/// Specialization of `linalg.batch_matmul` op that has a transpose map on A
260 /// Create an affine map for a transpose-A batch_matmul. Used only in the
261 /// builders.
262 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
263
264public:
265 using BatchMatmulOp::BatchMatmulOp;
266 static ::mlir::TypeID resolveTypeID() { return TypeID::get<BatchMatmulOp>(); }
267
268 /// Build a transpose A matmul.
269 static void build(OpBuilder &builder, OperationState &result,
270 ValueRange inputs, ValueRange outputs,
271 ArrayRef<NamedAttribute> attributes = {});
272
273 static BatchMatmulTransposeAOp
274 create(OpBuilder &builder, Location location, ValueRange inputs,
275 ValueRange outputs, ArrayRef<NamedAttribute> attributes = {});
276
277 /// Build a transpose A matmul with a specific result type.
278 static void build(OpBuilder &builder, OperationState &result,
279 TypeRange resultTensorTypes, ValueRange inputs,
280 ValueRange outputs,
281 ArrayRef<NamedAttribute> attributes = {});
282
283 static BatchMatmulTransposeAOp
284 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
285 ValueRange inputs, ValueRange outputs,
286 ArrayRef<NamedAttribute> attributes = {});
287
288 /// Build a transpose A matmul with a specific result type and a cast type.
289 static void build(OpBuilder &builder, OperationState &result,
290 TypeRange resultTensorTypes, ValueRange inputs,
291 ValueRange outputs, Attribute cast,
292 ArrayRef<NamedAttribute> attributes = {});
293
294 static BatchMatmulTransposeAOp
295 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
296 ValueRange inputs, ValueRange outputs, Attribute cast,
297 ArrayRef<NamedAttribute> attributes = {});
298
299 /// Checks if the affine map is the expected one for this operation
300 static bool isDefaultIndexingMaps(Attribute attr);
301
302 static bool classof(Operation *op);
303};
304
305/// Specialization of `linalg.batch_matmul` op that has a transpose map on B
307 /// Create an affine map for a transpose-B batch_matmul. Used only in the
308 /// builders.
309 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
310
311public:
312 using BatchMatmulOp::BatchMatmulOp;
313 static ::mlir::TypeID resolveTypeID() { return TypeID::get<BatchMatmulOp>(); }
314
315 /// Build a transpose B matmul.
316 static void build(OpBuilder &builder, OperationState &result,
317 ValueRange inputs, ValueRange outputs,
318 ArrayRef<NamedAttribute> attributes = {});
319
320 static BatchMatmulTransposeBOp
321 create(OpBuilder &builder, Location location, ValueRange inputs,
322 ValueRange outputs, ArrayRef<NamedAttribute> attributes = {});
323
324 /// Build a transpose B matmul with a specific result type.
325 static void build(OpBuilder &builder, OperationState &result,
326 TypeRange resultTensorTypes, ValueRange inputs,
327 ValueRange outputs,
328 ArrayRef<NamedAttribute> attributes = {});
329
330 static BatchMatmulTransposeBOp
331 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
332 ValueRange inputs, ValueRange outputs,
333 ArrayRef<NamedAttribute> attributes = {});
334
335 /// Build a transpose B matmul with a specific result type and a cast type.
336 static void build(OpBuilder &builder, OperationState &result,
337 TypeRange resultTensorTypes, ValueRange inputs,
338 ValueRange outputs, Attribute cast,
339 ArrayRef<NamedAttribute> attributes = {});
340
341 static BatchMatmulTransposeBOp
342 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
343 ValueRange inputs, ValueRange outputs, Attribute cast,
344 ArrayRef<NamedAttribute> attributes = {});
345
346 /// Checks if the affine map is the expected one for this operation
347 static bool isDefaultIndexingMaps(Attribute attr);
348
349 static bool classof(Operation *op);
350};
351
352} // namespace mlir::linalg
353
354#endif // MLIR_DIALECT_LINALG_IR_LINALG_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
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:210
static TypeID get()
Construct a type info object for the given type T.
Definition TypeID.h:245
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:389
Specialization of linalg.batch_matmul op that has a transpose map on A.
Definition Linalg.h:259
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
static bool classof(Operation *op)
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose A matmul.
static BatchMatmulTransposeAOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Specialization of linalg.batch_matmul op that has a transpose map on B.
Definition Linalg.h:306
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose B matmul.
static bool classof(Operation *op)
static BatchMatmulTransposeBOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
Specialization of linalg.matmul op that has a transpose map on A.
Definition Linalg.h:165
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
static MatmulTransposeAOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
::mlir::TypeID resolveTypeID()
Definition Linalg.h:171
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose A matmul.
static bool classof(Operation *op)
Specialization of linalg.matmul op that has a transpose map on B.
Definition Linalg.h:212
::mlir::TypeID resolveTypeID()
Definition Linalg.h:218
static void build(OpBuilder &builder, OperationState &result, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
Build a transpose B matmul.
static MatmulTransposeBOp create(OpBuilder &builder, Location location, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes={})
static bool isDefaultIndexingMaps(Attribute attr)
Checks if the affine map is the expected one for this operation.
static bool classof(Operation *op)
SmallVector< AffineExpr, 4 > concat(ArrayRef< AffineExpr > a, ArrayRef< AffineExpr > b)
Return the vector that is the concatenation of a and b.
OpFoldResult createFoldedDimOp(OpBuilder &b, Location loc, Value val, int64_t dim)
Create one memref::DimOp or tensor::DimOp depending on the type of val.
std::string generateLibraryCallName(Operation *op)
Returns the name mangled library call name to disambiguate between different overloads at the C level...
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.
SmallVector< AffineExpr, 4 > makeAffineDimExprs(unsigned num, unsigned &startIdx, MLIRContext *context)
Returns num AffineDimExpr dimensions at positions [startIdx, startIdx + num) and increments startIdx ...
Value createOrFoldDimOp(OpBuilder &b, Location loc, Value val, int64_t dim)
Create one memref::DimOp or tensor::DimOp depending on the type of val.
std::pair< int64_t, int64_t > getFmrFromWinogradConv2DFmr(WinogradConv2DFmr fmr)
Converts the given WinogradConv2DFmr enumeration value to a pair of m and r parameters.
ArityGroupAndKind getArityGroupAndKind(ElementwiseKind kind)
std::optional< WinogradConv2DFmr > getWinogradConv2DFmr(int64_t m, int64_t r)
Converts the given m and r parameters to a WinogradConv2DFmr enumeration value.
SmallVector< int64_t > getPackedOuterShapeWithoutTransposition(OpTy packOrUnPack)
Returns the outer shape in the packed domain before applying the transposition.
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
Elementwise Arity and Kind groups.
Definition Linalg.h:151
ElementwiseArityGroup arityGroup
Definition Linalg.h:153