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