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/// linalgelementwise unary op. e.g. linalg.exp.
139/// A linalg.generic body could be a series of unary elementwise ops e.g.
140/// `exp(neg(x))`, such as formed by linalg op fusion. Here we restrict it to
141/// detecting cases where body is is a single computation op.
142bool isaElemwiseSingleUnaryOpInterface(GenericOp genericOp);
143
144/// Checks whether `genericOp` is semantically equivalent to a single linalg
145/// elementwise binary op e.g. linalg.sub.
146bool isaElemwiseSingleBinaryOpInterface(GenericOp genericOp);
147
148/// Checks whether `genericOp` is semantically equivalent to a `linalg.fill`.
149/// Supports two patterns:
150/// 1. External: linalg.generic ins(%scalar) outs(%tensor) { yield %scalar }
151/// 2. Inlined: linalg.generic outs(%tensor) { yield %constant }
152/// Returns the scalar fill value if true.
153std::optional<Value> isaFillOpInterface(GenericOp genericOp);
154
155namespace detail {
156
157/// Returns true if the block contains a contraction of the following form:
158///
159/// %0 = <elemwise>(permutation-of(cu(block-argument-0),
160/// cu(block-argument-1)))
161/// %1 = <reduce>(permutation-of(cu(%0), cu(block-argument-2)))
162/// return-like cu(%1)
163///
164/// where <elemwise> and <reduce> are binary operations constituting a
165/// contraction (in the canonical case, <elemwise> is a multiplication and
166/// <reduce> is an addition). The name and other properties of these operations
167/// are checked by `isaPair`. All operands of all operations may be supplied
168/// through a chain of side effect-free unary operations, such as casts, which
169/// is denoted as `cu` above.
170///
171/// When the body does not contain a contraction, a more precise description of
172/// the failed precondition is send to the `errs` stream, if provided.
173bool isContractionBody(Block &block,
174 function_ref<bool(Operation *, Operation *)> isaPair,
175 llvm::raw_ostream &errs = mlir::thread_safe_nulls());
176
177/// Result of matching a Linalg generic against the predicates of it being a
178/// contraction.
179enum class MatchContractionResult;
180
181/// Checks whether `op` conforms to ContractionOpInterface and populates
182/// `dimensions` with indexes of the different kinds of dimensions when
183/// present.
186 ContractionDimensions *dimensions = nullptr);
187
188/// Returns the error message corresponding to the contraction checking return
189/// code.
191
192/// Result of matching a Linalg generic against the predicates of it being a
193/// convolution.
194enum class MatchConvolutionResult;
195
196/// Checks whether `op` conforms to ConvolutionOpInterface and populates
197/// `dimensions` with indexes of the different kinds of dimensions when
198/// present.
199/// If `allowEmptyConvolvedDims` is not set, we further checks whether the `op`
200/// contains convolved dims.
203 ConvolutionDimensions *dimensions = nullptr,
204 bool allowEmptyConvolvedDims = false);
205
206/// Returns the error message corresponding to the convolution checking return
207/// code.
209
210/// Verify that `op` conforms to ContractionOpInterface.
211LogicalResult verifyContractionInterface(Operation *op);
212
213/// Verify that `op` conforms to the ConvolutionOpInterface.
214LogicalResult verifyConvolutionInterface(Operation *op);
215
216/// Verify that `op` conforms to the FillOpInterface.
217LogicalResult verifyFillInterface(Operation *op);
218
219/// Verify that `op` conforms to the invariants of StructuredOpInterface
220LogicalResult verifyStructuredOpInterface(Operation *op);
221
222} // namespace detail
223} // namespace linalg
224} // namespace mlir
225
226#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.h.inc"
227
228/// Include the generated interface declarations.
229#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h.inc"
230
231/// Include the generated relayout interface declarations.
232#include "mlir/Dialect/Linalg/IR/RelayoutOpInterface.h.inc"
233
234#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 isaElemwiseSingleUnaryOpInterface(GenericOp genericOp)
Checks whether a given genericOp is semantically equivalent to a single linalgelementwise unary op.
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.
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:144
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