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/// Specialization of `linalg.matmul` op that has a transpose map on A
152 /// Create an affine map for a transpose-A matmul. Used only in the builders.
153 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
154
155public:
156 using MatmulOp::MatmulOp;
157 static ::mlir::TypeID resolveTypeID() { return TypeID::get<MatmulOp>(); }
158
159 /// Build a transpose A matmul.
160 static void build(OpBuilder &builder, OperationState &result,
161 ValueRange inputs, ValueRange outputs,
162 ArrayRef<NamedAttribute> attributes = {});
163
164 static MatmulTransposeAOp create(OpBuilder &builder, Location location,
165 ValueRange inputs, ValueRange outputs,
166 ArrayRef<NamedAttribute> attributes = {});
167
168 /// Build a transpose A matmul with a specific result type.
169 static void build(OpBuilder &builder, OperationState &result,
170 TypeRange resultTensorTypes, ValueRange inputs,
171 ValueRange outputs,
172 ArrayRef<NamedAttribute> attributes = {});
173
174 static MatmulTransposeAOp create(OpBuilder &builder, Location location,
175 TypeRange resultTensorTypes,
176 ValueRange inputs, ValueRange outputs,
177 ArrayRef<NamedAttribute> attributes = {});
178
179 /// Build a transpose A matmul with a specific result type and a cast type.
180 static void build(OpBuilder &builder, OperationState &result,
181 TypeRange resultTensorTypes, ValueRange inputs,
182 ValueRange outputs, Attribute cast,
183 ArrayRef<NamedAttribute> attributes = {});
184
185 static MatmulTransposeAOp create(OpBuilder &builder, Location location,
186 TypeRange resultTensorTypes,
187 ValueRange inputs, ValueRange outputs,
188 Attribute cast,
189 ArrayRef<NamedAttribute> attributes = {});
190
191 /// Checks if the affine map is the expected one for this operation
192 static bool isDefaultIndexingMaps(Attribute attr);
193
194 static bool classof(Operation *op);
195};
196
197/// Specialization of `linalg.matmul` op that has a transpose map on B
199 /// Create an affine map for a transpose-B matmul. Used only in the builders.
200 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
201
202public:
203 using MatmulOp::MatmulOp;
204 static ::mlir::TypeID resolveTypeID() { return TypeID::get<MatmulOp>(); }
205
206 /// Build a transpose B matmul.
207 static void build(OpBuilder &builder, OperationState &result,
208 ValueRange inputs, ValueRange outputs,
209 ArrayRef<NamedAttribute> attributes = {});
210
211 static MatmulTransposeBOp create(OpBuilder &builder, Location location,
212 ValueRange inputs, ValueRange outputs,
213 ArrayRef<NamedAttribute> attributes = {});
214
215 /// Build a transpose B matmul with a specific result type.
216 static void build(OpBuilder &builder, OperationState &result,
217 TypeRange resultTensorTypes, ValueRange inputs,
218 ValueRange outputs,
219 ArrayRef<NamedAttribute> attributes = {});
220
221 static MatmulTransposeBOp create(OpBuilder &builder, Location location,
222 TypeRange resultTensorTypes,
223 ValueRange inputs, ValueRange outputs,
224 ArrayRef<NamedAttribute> attributes = {});
225
226 /// Build a transpose B matmul with a specific result type and a cast type.
227 static void build(OpBuilder &builder, OperationState &result,
228 TypeRange resultTensorTypes, ValueRange inputs,
229 ValueRange outputs, Attribute cast,
230 ArrayRef<NamedAttribute> attributes = {});
231
232 static MatmulTransposeBOp create(OpBuilder &builder, Location location,
233 TypeRange resultTensorTypes,
234 ValueRange inputs, ValueRange outputs,
235 Attribute cast,
236 ArrayRef<NamedAttribute> attributes = {});
237
238 /// Checks if the affine map is the expected one for this operation
239 static bool isDefaultIndexingMaps(Attribute attr);
240
241 static bool classof(Operation *op);
242};
243
244/// Specialization of `linalg.batch_matmul` op that has a transpose map on A
246 /// Create an affine map for a transpose-A batch_matmul. Used only in the
247 /// builders.
248 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
249
250public:
251 using BatchMatmulOp::BatchMatmulOp;
252 static ::mlir::TypeID resolveTypeID() { return TypeID::get<BatchMatmulOp>(); }
253
254 /// Build a transpose A matmul.
255 static void build(OpBuilder &builder, OperationState &result,
256 ValueRange inputs, ValueRange outputs,
257 ArrayRef<NamedAttribute> attributes = {});
258
259 static BatchMatmulTransposeAOp
260 create(OpBuilder &builder, Location location, ValueRange inputs,
261 ValueRange outputs, ArrayRef<NamedAttribute> attributes = {});
262
263 /// Build a transpose A matmul with a specific result type.
264 static void build(OpBuilder &builder, OperationState &result,
265 TypeRange resultTensorTypes, ValueRange inputs,
266 ValueRange outputs,
267 ArrayRef<NamedAttribute> attributes = {});
268
269 static BatchMatmulTransposeAOp
270 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
271 ValueRange inputs, ValueRange outputs,
272 ArrayRef<NamedAttribute> attributes = {});
273
274 /// Build a transpose A matmul with a specific result type and a cast type.
275 static void build(OpBuilder &builder, OperationState &result,
276 TypeRange resultTensorTypes, ValueRange inputs,
277 ValueRange outputs, Attribute cast,
278 ArrayRef<NamedAttribute> attributes = {});
279
280 static BatchMatmulTransposeAOp
281 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
282 ValueRange inputs, ValueRange outputs, Attribute cast,
283 ArrayRef<NamedAttribute> attributes = {});
284
285 /// Checks if the affine map is the expected one for this operation
286 static bool isDefaultIndexingMaps(Attribute attr);
287
288 static bool classof(Operation *op);
289};
290
291/// Specialization of `linalg.batch_matmul` op that has a transpose map on B
293 /// Create an affine map for a transpose-B batch_matmul. Used only in the
294 /// builders.
295 static SmallVector<AffineMap> getDefaultIndexingMaps(OpBuilder &builder);
296
297public:
298 using BatchMatmulOp::BatchMatmulOp;
299 static ::mlir::TypeID resolveTypeID() { return TypeID::get<BatchMatmulOp>(); }
300
301 /// Build a transpose B matmul.
302 static void build(OpBuilder &builder, OperationState &result,
303 ValueRange inputs, ValueRange outputs,
304 ArrayRef<NamedAttribute> attributes = {});
305
306 static BatchMatmulTransposeBOp
307 create(OpBuilder &builder, Location location, ValueRange inputs,
308 ValueRange outputs, ArrayRef<NamedAttribute> attributes = {});
309
310 /// Build a transpose B matmul with a specific result type.
311 static void build(OpBuilder &builder, OperationState &result,
312 TypeRange resultTensorTypes, ValueRange inputs,
313 ValueRange outputs,
314 ArrayRef<NamedAttribute> attributes = {});
315
316 static BatchMatmulTransposeBOp
317 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
318 ValueRange inputs, ValueRange outputs,
319 ArrayRef<NamedAttribute> attributes = {});
320
321 /// Build a transpose B matmul with a specific result type and a cast type.
322 static void build(OpBuilder &builder, OperationState &result,
323 TypeRange resultTensorTypes, ValueRange inputs,
324 ValueRange outputs, Attribute cast,
325 ArrayRef<NamedAttribute> attributes = {});
326
327 static BatchMatmulTransposeBOp
328 create(OpBuilder &builder, Location location, TypeRange resultTensorTypes,
329 ValueRange inputs, ValueRange outputs, Attribute cast,
330 ArrayRef<NamedAttribute> attributes = {});
331
332 /// Checks if the affine map is the expected one for this operation
333 static bool isDefaultIndexingMaps(Attribute attr);
334
335 static bool classof(Operation *op);
336};
337
338} // namespace mlir::linalg
339
340#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:245
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:292
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:151
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:157
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:198
::mlir::TypeID resolveTypeID()
Definition Linalg.h:204
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.
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.