MLIR  18.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.
65 
66 /// Checks whether `linalgOp` conforms to ContractionOpInterface.
67 // TODO: embed within `isa<ContractionOpInterface>` if possible / natural.
68 bool isaContractionOpInterface(LinalgOp linalgOp);
69 
70 /// Positions of a Linalg op loops that correspond to different kinds of a
71 /// convolution dimension.
81 };
82 
83 /// Find at least 1 parallel (output_image) and reduction (filter_loop)
84 /// dimension candidates that form a convolution subcomputation within
85 /// `linalgOp`. The LHS is assumed to be the convolution input while the
86 /// RHS is assumed as the filter.
87 /// These dimensions are such that:
88 /// 1. Optional batch dimensions that appear in the input and filter.
89 /// 2. The output_image dimension is involved in a cross-correlation along LHS
90 /// (i.e. it is a permutation on RES and LHS and has an associated
91 /// filter_loop in RHS).
92 /// 3. Optional output_channel dimension is involved in an outer-product along
93 /// RHS (i.e. it is a permutation on RES and RHS and does not appear in
94 /// LHS).
95 /// 4. Optional input_channel dimension appears as a permutation on LHS and
96 /// RHS.
97 /// 5. The filter_loop dimension appears as a permutation on the RHS and
98 /// represents the shape of the kernel cross-correlated along a
99 /// corresponding output_image dim.
100 /// 6. The input_channel dimension appears as a permutation on LHS and RHS.
101 /// 7. All dimensions appear only once in any given indexing map.
102 /// This allows e.g. detecting that some convolution is embedded within
103 /// `linalgOp` with some orthogonal heuristic.
104 /// When multiple dimension occurrences exist that match any classification
105 /// indices are returned in sorted order.
106 /// Returns a failure if `output_image` (and implicitly `filter_loop`) is empty.
108 
109 /// Checks whether `linalgOp` conforms to ConvolutionOpInterface.
110 // TODO: embed within `isa<ConvolutionOpInterface>` if possible / natural.
111 bool isaConvolutionOpInterface(LinalgOp linalgOp);
112 
113 /// Checks whether `linalgOp` is semantically equivalent to a `linalg.copyOp`.
114 bool isaCopyOpInterface(LinalgOp linalgOp);
115 
116 namespace detail {
117 
118 /// Returns true if the block contains a contraction of the following form:
119 ///
120 /// %0 = <elemwise>(permutation-of(cu(block-argument-0),
121 /// cu(block-argument-1)))
122 /// %1 = <reduce>(permutation-of(cu(%0), cu(block-argument-2)))
123 /// return-like cu(%1)
124 ///
125 /// where <elemwise> and <reduce> are binary operations constituting a
126 /// contraction (in the canonical case, <elemwise> is a multiplication and
127 /// <reduce> is an addition). The name and other properties of these operations
128 /// are checked by `isaPair`. All operands of all operations may be supplied
129 /// through a chain of side effect-free unary operations, such as casts, which
130 /// is denoted as `cu` above.
131 ///
132 /// When the body does not contain a contraction, a more precise description of
133 /// the failed precondition is send to the `errs` stream, if provided.
134 bool isContractionBody(Block &block,
135  function_ref<bool(Operation *, Operation *)> isaPair,
136  llvm::raw_ostream &errs = mlir::thread_safe_nulls());
137 
138 /// Result of matching a Linalg generic against the predicates of it being a
139 /// contraction.
140 enum class MatchContractionResult;
141 
142 /// Checks whether `op` conforms to ContractionOpInterface and populates
143 /// `dimensions` with indexes of the different kinds of dimensions when
144 /// present.
147  ContractionDimensions *dimensions = nullptr);
148 
149 /// Returns the error message corresponding to the contraction checking return
150 /// code.
152 
153 /// Result of matching a Linalg generic against the predicates of it being a
154 /// convolution.
155 enum class MatchConvolutionResult;
156 
157 /// Checks whether `op` conforms to ConvolutionOpInterface and populates
158 /// `dimensions` with indexes of the different kinds of dimensions when
159 /// present.
162  ConvolutionDimensions *dimensions = nullptr);
163 
164 /// Returns the error message corresponding to the convolution checking return
165 /// code.
167 
168 /// Verify that `op` conforms to ContractionOpInterface.
170 
171 /// Verify that `op` conforms to the ConvolutionOpInterface.
173 
174 /// Verify that `op` conforms to the FillOpInterface.
176 
177 /// Verify that `op` conforms to the invariants of StructuredOpInterface
179 
180 } // namespace detail
181 } // namespace linalg
182 } // namespace mlir
183 
184 #include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.h.inc"
185 
186 /// Include the generated interface declarations.
187 #include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h.inc"
188 
189 #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