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