MLIR 23.0.0git
LinalgInterfaces.h
Go to the documentation of this file.
1//===- LinalgInterface.h - Linalg operations interfaces -------------------===//
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// This file implements the operation interfaces for Linalg operations.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_DIALECT_LINALG_IR_LINALGINTERFACES_H_
14#define MLIR_DIALECT_LINALG_IR_LINALGINTERFACES_H_
15
17#include "mlir/IR/AffineMap.h"
19#include "mlir/IR/IRMapping.h"
27
28namespace mlir {
29namespace linalg {
30class IteratorTypeAttr;
31class LinalgOp;
32class GenericOp;
33
34namespace detail {
35/// Implementation of the method that check if given operands
36/// can be dropped, i.e. the remaining operands can compute the loop
37/// bounds of the op.
38bool canOpOperandsBeDroppedImpl(linalg::LinalgOp linalgOp,
39 ArrayRef<OpOperand *> droppedOperands);
40} // namespace detail
41
42/// Positions of a Linalg op loops that correspond to different kinds of a
43/// contraction dimension.
50
51/// Find at least 2 parallel (m and n) and 1 reduction (k) dimension candidates
52/// that form a matmul subcomputation within `linalgOp`.
53/// These dimensions are such that:
54/// 1. The m dimension is involved in an outer-product along LHS
55/// (i.e. it is a permutation on RES and LHS and does not appear in RHS).
56/// 2. The n dimension is involved in an outer-product along RHS
57/// (i.e. it is a permutation on RES and RHS and does not appear in LHS).
58/// 3. The k dimension appears as a permutation on LHS and RHS.
59/// 4. m, n and k appear only once in any given indexing.
60/// 5. Optional batch dimensions that appear in all operands are captured.
61/// This allows e.g. detecting that some contraction is embedded within
62/// `linalgOp` with some orthogonal heuristic.
63/// When multiple dimension occurrences exist that match `batch`, `m`, `n`, or
64/// `k`, indices are returned in sorted order.
65/// Returns a failure if any of `m`, `n` or `k` is empty.
66FailureOr<ContractionDimensions> inferContractionDims(LinalgOp linalgOp);
67FailureOr<ContractionDimensions>
69
70/// Checks whether `linalgOp` conforms to ContractionOpInterface.
71// TODO: embed within `isa<ContractionOpInterface>` if possible / natural.
72bool isaContractionOpInterface(LinalgOp linalgOp);
73
74/// Positions of a Linalg op loops that correspond to different kinds of a
75/// convolution dimension.
86
87/// Find at least 1 parallel (output_image) and reduction (filter_loop)
88/// dimension candidates that form a convolution subcomputation within
89/// `linalgOp`. The LHS is assumed to be the convolution input while the
90/// RHS is assumed as the filter.
91/// These dimensions are such that:
92/// 1. Optional batch dimensions that appear in the input and filter.
93/// 2. The output_image dimension is involved in a cross-correlation along LHS
94/// (i.e. it is a permutation on RES and LHS and has an associated
95/// filter_loop in RHS).
96/// 3. Optional output_channel dimension is involved in an outer-product along
97/// RHS (i.e. it is a permutation on RES and RHS and does not appear in
98/// LHS).
99/// 4. Optional input_channel dimension appears as a permutation on LHS and
100/// RHS.
101/// 5. The filter_loop dimension appears as a permutation on the RHS and
102/// represents the shape of the kernel cross-correlated along a
103/// corresponding output_image dim.
104/// 6. The input_channel dimension appears as a permutation on LHS and RHS.
105/// 7. All dimensions appear only once in any given indexing map.
106/// This allows e.g. detecting that some convolution is embedded within
107/// `linalgOp` with some orthogonal heuristic.
108///
109/// The `outputImage` and `filterLoop` arrays are ordered such that
110/// `outputImage[i]` pairs with `filterLoop[i]` based on the convolution access
111/// pattern in the input indexing map (e.g., `d0 + d2` pairs dimension 0 with
112/// dimension 2). Other dimension sets are returned in sorted order.
113///
114/// Returns a failure if `output_image` (and implicitly `filter_loop`) is empty.
115FailureOr<ConvolutionDimensions> inferConvolutionDims(LinalgOp linalgOp);
116
117/// Checks whether `linalgOp` conforms to ConvolutionOpInterface.
118/// By default, we require the `linalgOp` to have non-empty convolved dims
119/// (implicitly non-empty `output_image` and `filter_loop`).
120/// Users can loosen the constraint by setting `allowEmptyConvolvedDims` to true
121// TODO: embed within `isa<ConvolutionOpInterface>` if possible / natural.
122bool isaConvolutionOpInterface(LinalgOp linalgOp,
123 bool allowEmptyConvolvedDims = false);
124
125/// Checks whether `linalgOp` is semantically equivalent to a `linalg.copyOp`.
126bool isaCopyOpInterface(LinalgOp linalgOp);
127
128/// Checks whether `linalgOp` is semantically equivalent to a broadcast
129/// operation. Returns broadcast dimensions if true.
130std::optional<SmallVector<int64_t>> isaBroadcastOpInterface(LinalgOp linalgOp);
131
132/// Checks whether `genericOp` is semantically equivalent to a
133/// `linalg.transpose`. Returns permuted dimensions if true.
134std::optional<SmallVector<int64_t>>
135isaTransposeOpInterface(GenericOp genericOp);
136
137/// Checks whether a given `genericOp` is semantically equivalent to a single
138/// linalg elementwise unary op, e.g. `linalg.exp` or
139/// `linalg.elementwise kind=#linalg.elementwise_kind<exp>`.
140/// If `allowNonIdentityMaps` is true, operations with custom indexing maps are
141/// included in the check. Note that these operations can only be represented by
142/// the category op.
143/// A linalg.generic body could be a series of unary elementwise ops e.g.
144/// `exp(neg(x))`, such as formed by linalg op fusion. Here we restrict it to
145/// detecting cases where body is is a single computation op.
146bool isaElemwiseSingleUnaryOpInterface(GenericOp genericOp,
147 bool allowNonIdentityMaps = false);
148
149/// Checks whether `genericOp` is semantically equivalent to a single linalg
150/// elementwise binary op e.g. linalg.sub.
151bool isaElemwiseSingleBinaryOpInterface(GenericOp genericOp);
152
153/// Checks whether `genericOp` is semantically equivalent to a `linalg.fill`.
154/// Supports two patterns:
155/// 1. External: linalg.generic ins(%scalar) outs(%tensor) { yield %scalar }
156/// 2. Inlined: linalg.generic outs(%tensor) { yield %constant }
157/// Returns the scalar fill value if true.
158std::optional<Value> isaFillOpInterface(GenericOp genericOp);
159
160namespace detail {
161
162/// Returns true if the block contains a contraction of the following form:
163///
164/// %0 = <elemwise>(permutation-of(cu(block-argument-0),
165/// cu(block-argument-1)))
166/// %1 = <reduce>(permutation-of(cu(%0), cu(block-argument-2)))
167/// return-like cu(%1)
168///
169/// where <elemwise> and <reduce> are binary operations constituting a
170/// contraction (in the canonical case, <elemwise> is a multiplication and
171/// <reduce> is an addition). The name and other properties of these operations
172/// are checked by `isaPair`. All operands of all operations may be supplied
173/// through a chain of side effect-free unary operations, such as casts, which
174/// is denoted as `cu` above.
175///
176/// When the body does not contain a contraction, a more precise description of
177/// the failed precondition is send to the `errs` stream, if provided.
178bool isContractionBody(Block &block,
179 function_ref<bool(Operation *, Operation *)> isaPair,
180 llvm::raw_ostream &errs = mlir::thread_safe_nulls());
181
182/// Result of matching a Linalg generic against the predicates of it being a
183/// contraction.
184enum class MatchContractionResult;
185
186/// Checks whether `op` conforms to ContractionOpInterface and populates
187/// `dimensions` with indexes of the different kinds of dimensions when
188/// present.
191 ContractionDimensions *dimensions = nullptr);
192
193/// Returns the error message corresponding to the contraction checking return
194/// code.
196
197/// Result of matching a Linalg generic against the predicates of it being a
198/// convolution.
199enum class MatchConvolutionResult;
200
201/// Checks whether `op` conforms to ConvolutionOpInterface and populates
202/// `dimensions` with indexes of the different kinds of dimensions when
203/// present.
204/// If `allowEmptyConvolvedDims` is not set, we further checks whether the `op`
205/// contains convolved dims.
208 ConvolutionDimensions *dimensions = nullptr,
209 bool allowEmptyConvolvedDims = false);
210
211/// Returns the error message corresponding to the convolution checking return
212/// code.
214
215/// Verify that `op` conforms to ContractionOpInterface.
216LogicalResult verifyContractionInterface(Operation *op);
217
218/// Verify that `op` conforms to the ConvolutionOpInterface.
219LogicalResult verifyConvolutionInterface(Operation *op);
220
221/// Verify that `op` conforms to the FillOpInterface.
222LogicalResult verifyFillInterface(Operation *op);
223
224/// Verify that `op` conforms to the invariants of StructuredOpInterface
225LogicalResult verifyStructuredOpInterface(Operation *op);
226
227} // namespace detail
228} // namespace linalg
229} // namespace mlir
230
231#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.h.inc"
232
233/// Include the generated interface declarations.
234#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h.inc"
235
236/// Include the generated relayout interface declarations.
237#include "mlir/Dialect/Linalg/IR/RelayoutOpInterface.h.inc"
238
239#endif // MLIR_DIALECT_LINALG_IR_LINALGINTERFACES_H_
Block represents an ordered list of Operations.
Definition Block.h:33
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
MatchConvolutionResult isConvolutionInterfaceImpl(Operation *op, ConvolutionDimensions *dimensions=nullptr, bool allowEmptyConvolvedDims=false)
Checks whether op conforms to ConvolutionOpInterface and populates dimensions with indexes of the dif...
bool isContractionBody(Block &block, function_ref< bool(Operation *, Operation *)> isaPair, llvm::raw_ostream &errs=mlir::thread_safe_nulls())
Returns true if the block contains a contraction of the following form:
StringRef getMatchConvolutionMessage(MatchConvolutionResult res)
Returns the error message corresponding to the convolution checking return code.
bool canOpOperandsBeDroppedImpl(linalg::LinalgOp linalgOp, ArrayRef< OpOperand * > droppedOperands)
Implementation of the method that check if given operands can be dropped, i.e.
MatchContractionResult isContractionInterfaceImpl(Operation *op, ContractionDimensions *dimensions=nullptr)
Checks whether op conforms to ContractionOpInterface and populates dimensions with indexes of the dif...
LogicalResult verifyContractionInterface(Operation *op)
Verify that op conforms to ContractionOpInterface.
LogicalResult verifyFillInterface(Operation *op)
Verify that op conforms to the FillOpInterface.
StringRef getMatchContractionMessage(MatchContractionResult res)
Returns the error message corresponding to the contraction checking return code.
LogicalResult verifyStructuredOpInterface(Operation *op)
Verify that op conforms to the invariants of StructuredOpInterface.
LogicalResult verifyConvolutionInterface(Operation *op)
Verify that op conforms to the ConvolutionOpInterface.
std::optional< SmallVector< int64_t > > isaTransposeOpInterface(GenericOp genericOp)
Checks whether genericOp is semantically equivalent to a linalg.transpose.
bool isaCopyOpInterface(LinalgOp linalgOp)
Checks whether linalgOp is semantically equivalent to a linalg.copyOp.
FailureOr< ConvolutionDimensions > inferConvolutionDims(LinalgOp linalgOp)
Find at least 1 parallel (output_image) and reduction (filter_loop) dimension candidates that form a ...
bool isaConvolutionOpInterface(LinalgOp linalgOp, bool allowEmptyConvolvedDims=false)
Checks whether linalgOp conforms to ConvolutionOpInterface.
std::optional< SmallVector< int64_t > > isaBroadcastOpInterface(LinalgOp linalgOp)
Checks whether linalgOp is semantically equivalent to a broadcast operation.
FailureOr< ContractionDimensions > inferContractionDims(LinalgOp linalgOp)
Find at least 2 parallel (m and n) and 1 reduction (k) dimension candidates that form a matmul subcom...
bool isaContractionOpInterface(LinalgOp linalgOp)
Checks whether linalgOp conforms to ContractionOpInterface.
bool isaElemwiseSingleUnaryOpInterface(GenericOp genericOp, bool allowNonIdentityMaps=false)
Checks whether a given genericOp is semantically equivalent to a single linalg elementwise unary op,...
std::optional< Value > isaFillOpInterface(GenericOp genericOp)
Checks whether genericOp is semantically equivalent to a linalg.fill.
bool isaElemwiseSingleBinaryOpInterface(GenericOp genericOp)
Checks whether genericOp is semantically equivalent to a single linalg elementwise binary op e....
Include the generated interface declarations.
llvm::raw_ostream & thread_safe_nulls()
Returns a raw output stream that simply discards the output, but in a thread-safe manner.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147
Positions of a Linalg op loops that correspond to different kinds of a contraction dimension.
SmallVector< unsigned, 2 > batch
Positions of a Linalg op loops that correspond to different kinds of a convolution dimension.
SmallVector< unsigned, 2 > depth
SmallVector< unsigned, 2 > outputImage
SmallVector< unsigned, 2 > outputChannel
SmallVector< int64_t, 2 > dilations
SmallVector< int64_t, 2 > strides
SmallVector< unsigned, 2 > inputChannel
SmallVector< unsigned, 2 > batch
SmallVector< unsigned, 2 > filterLoop