MLIR  19.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"
18 #include "mlir/IR/BuiltinTypes.h"
19 #include "mlir/IR/IRMapping.h"
21 #include "mlir/IR/OpDefinition.h"
26 
27 namespace mlir {
28 namespace linalg {
29 class IteratorTypeAttr;
30 class LinalgOp;
31 
32 namespace detail {
33 /// Implementation of the method that check if given operands
34 /// can be dropped, i.e. the remaining operands can compute the loop
35 /// bounds of the op.
36 bool canOpOperandsBeDroppedImpl(linalg::LinalgOp linalgOp,
37  ArrayRef<OpOperand *> droppedOperands);
38 } // namespace detail
39 
40 /// Positions of a Linalg op loops that correspond to different kinds of a
41 /// contraction dimension.
47 };
48 
49 /// Find at least 2 parallel (m and n) and 1 reduction (k) dimension candidates
50 /// that form a matmul subcomputation within `linalgOp`.
51 /// These dimensions are such that:
52 /// 1. The m dimension is involved in an outer-product along LHS
53 /// (i.e. it is a permutation on RES and LHS and does not appear in RHS).
54 /// 2. The n dimension is involved in an outer-product along RHS
55 /// (i.e. it is a permutation on RES and RHS and does not appear in LHS).
56 /// 3. The k dimension appears as a permutation on LHS and RHS.
57 /// 4. m, n and k appear only once in any given indexing.
58 /// 5. Optional batch dimensions that appear in all operands are captured.
59 /// This allows e.g. detecting that some contraction is embedded within
60 /// `linalgOp` with some orthogonal heuristic.
61 /// When multiple dimension occurrences exist that match `batch`, `m`, `n`, or
62 /// `k`, indices are returned in sorted order.
63 /// Returns a failure if any of `m`, `n` or `k` is empty.
67 
68 /// Checks whether `linalgOp` conforms to ContractionOpInterface.
69 // TODO: embed within `isa<ContractionOpInterface>` if possible / natural.
70 bool isaContractionOpInterface(LinalgOp linalgOp);
71 
72 /// Positions of a Linalg op loops that correspond to different kinds of a
73 /// convolution dimension.
83 };
84 
85 /// Find at least 1 parallel (output_image) and reduction (filter_loop)
86 /// dimension candidates that form a convolution subcomputation within
87 /// `linalgOp`. The LHS is assumed to be the convolution input while the
88 /// RHS is assumed as the filter.
89 /// These dimensions are such that:
90 /// 1. Optional batch dimensions that appear in the input and filter.
91 /// 2. The output_image dimension is involved in a cross-correlation along LHS
92 /// (i.e. it is a permutation on RES and LHS and has an associated
93 /// filter_loop in RHS).
94 /// 3. Optional output_channel dimension is involved in an outer-product along
95 /// RHS (i.e. it is a permutation on RES and RHS and does not appear in
96 /// LHS).
97 /// 4. Optional input_channel dimension appears as a permutation on LHS and
98 /// RHS.
99 /// 5. The filter_loop dimension appears as a permutation on the RHS and
100 /// represents the shape of the kernel cross-correlated along a
101 /// corresponding output_image dim.
102 /// 6. The input_channel dimension appears as a permutation on LHS and RHS.
103 /// 7. All dimensions appear only once in any given indexing map.
104 /// This allows e.g. detecting that some convolution is embedded within
105 /// `linalgOp` with some orthogonal heuristic.
106 /// When multiple dimension occurrences exist that match any classification
107 /// indices are returned in sorted order.
108 /// Returns a failure if `output_image` (and implicitly `filter_loop`) is empty.
110 
111 /// Checks whether `linalgOp` conforms to ConvolutionOpInterface.
112 // TODO: embed within `isa<ConvolutionOpInterface>` if possible / natural.
113 bool isaConvolutionOpInterface(LinalgOp linalgOp);
114 
115 /// Checks whether `linalgOp` is semantically equivalent to a `linalg.copyOp`.
116 bool isaCopyOpInterface(LinalgOp linalgOp);
117 
118 namespace detail {
119 
120 /// Returns true if the block contains a contraction of the following form:
121 ///
122 /// %0 = <elemwise>(permutation-of(cu(block-argument-0),
123 /// cu(block-argument-1)))
124 /// %1 = <reduce>(permutation-of(cu(%0), cu(block-argument-2)))
125 /// return-like cu(%1)
126 ///
127 /// where <elemwise> and <reduce> are binary operations constituting a
128 /// contraction (in the canonical case, <elemwise> is a multiplication and
129 /// <reduce> is an addition). The name and other properties of these operations
130 /// are checked by `isaPair`. All operands of all operations may be supplied
131 /// through a chain of side effect-free unary operations, such as casts, which
132 /// is denoted as `cu` above.
133 ///
134 /// When the body does not contain a contraction, a more precise description of
135 /// the failed precondition is send to the `errs` stream, if provided.
136 bool isContractionBody(Block &block,
137  function_ref<bool(Operation *, Operation *)> isaPair,
138  llvm::raw_ostream &errs = mlir::thread_safe_nulls());
139 
140 /// Result of matching a Linalg generic against the predicates of it being a
141 /// contraction.
142 enum class MatchContractionResult;
143 
144 /// Checks whether `op` conforms to ContractionOpInterface and populates
145 /// `dimensions` with indexes of the different kinds of dimensions when
146 /// present.
149  ContractionDimensions *dimensions = nullptr);
150 
151 /// Returns the error message corresponding to the contraction checking return
152 /// code.
154 
155 /// Result of matching a Linalg generic against the predicates of it being a
156 /// convolution.
157 enum class MatchConvolutionResult;
158 
159 /// Checks whether `op` conforms to ConvolutionOpInterface and populates
160 /// `dimensions` with indexes of the different kinds of dimensions when
161 /// present.
164  ConvolutionDimensions *dimensions = nullptr);
165 
166 /// Returns the error message corresponding to the convolution checking return
167 /// code.
169 
170 /// Verify that `op` conforms to ContractionOpInterface.
172 
173 /// Verify that `op` conforms to the ConvolutionOpInterface.
175 
176 /// Verify that `op` conforms to the FillOpInterface.
178 
179 /// Verify that `op` conforms to the invariants of StructuredOpInterface
181 
182 } // namespace detail
183 } // namespace linalg
184 } // namespace mlir
185 
186 #include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.h.inc"
187 
188 /// Include the generated interface declarations.
189 #include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h.inc"
190 
191 #endif // MLIR_DIALECT_LINALG_IR_LINALGINTERFACES_H_
Block represents an ordered list of Operations.
Definition: Block.h:30
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
MatchConvolutionResult isConvolutionInterfaceImpl(Operation *op, ConvolutionDimensions *dimensions=nullptr)
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.
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 ...
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 isaConvolutionOpInterface(LinalgOp linalgOp)
Checks whether linalgOp conforms to ConvolutionOpInterface.
bool isaContractionOpInterface(LinalgOp linalgOp)
Checks whether linalgOp conforms to ContractionOpInterface.
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.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Positions of a Linalg op loops that correspond to different kinds of a contraction dimension.
SmallVector< unsigned, 2 > batch
SmallVector< unsigned, 2 > m
SmallVector< unsigned, 2 > n
SmallVector< unsigned, 2 > k
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