MLIR 22.0.0git
Transforms.h
Go to the documentation of this file.
1//===- Transforms.h - Tensor Transformation Patterns ------------*- 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_TENSOR_TRANSFORMS_TRANSFORMS_H
10#define MLIR_DIALECT_TENSOR_TRANSFORMS_TRANSFORMS_H
11
15
16namespace mlir {
17
18struct TilingResult;
19
20namespace tensor {
21
22//===----------------------------------------------------------------------===//
23// Patterns
24//===----------------------------------------------------------------------===//
25
26/// Method to swap an `tensor.extract_slice` with its producer when the
27/// producer implements the `TilingInterface`. The pattern itself does not
28/// provide a mechanism to control where the application happens. With use of
29/// transform dialect that control is done within the transform dialect. Other
30/// use cases can inherit from this pattern and add necessary controls.
31FailureOr<TilingResult> replaceExtractSliceWithTiledProducer(
32 OpBuilder &builder, tensor::ExtractSliceOp sliceOp, OpResult producerOp);
33
34/// Method to swap `tensor.insert_slice`s with their consumers when the
35/// consumer implements the `TilingInterface`. The size of `sliceOps` and
36/// `consumerOperands` is expected to be the same. Every entry in
37/// `consumerOperands` represents a use of the the corresponding
38/// entry in `sliceOps` in the consumer. All entries of `consumerOperands` is
39/// expected to be uses in the same consumer.
40FailureOr<TilingResult>
42 ArrayRef<tensor::InsertSliceOp> sliceOps,
43 ArrayRef<OpOperand *> consumerOperands);
44
45//===----------------------------------------------------------------------===//
46// Populate functions.
47//===----------------------------------------------------------------------===//
48
49/// Appends patterns for folding tensor subset ops into consumer load/store
50/// ops into `patterns`. (This includes patterns for folding tensor subset ops
51/// into vector transfer ops.)
52void populateFoldTensorSubsetOpPatterns(RewritePatternSet &patterns);
53
54/// Appends patterns for folding tensor subset ops into vector transfer ops.
56 RewritePatternSet &patterns);
57
58/// Collects patterns to merge consecutive tensor.insert_slice/extract_slice
59/// into one. These patterns are in this separate entry point because the
60/// bufferization is sensitive to IR structure, particularly those
61/// tensor.extract_slice and tensor.insert_slice ops for creating the slices.
63 RewritePatternSet &patterns);
64
65/// Appends patterns that are used to bubble up tensor.extract slice op above
66/// its producer. When used as cleanup patterns of tile and fuse, enables fusing
67/// the producer with the consumer even if the producer does not implement the
68/// tiling interface.
70
71/// Populates `patterns` with patterns that drop redundant tensor.insert_slice
72/// rank expansions.
74 RewritePatternSet &patterns);
75
76/// Populates `patterns` with patterns that fold `tensor.expand_shape` and
77/// `tensor.collapse_shape` into other ops.
79
80/// Populates `patterns` with patterns that bubble up `tensor.expand_shape`
81/// through `tensor.collapse_shape` ops.
82void populateBubbleUpExpandShapePatterns(RewritePatternSet &patterns);
83
84/// Populates `patterns` with patterns that fold tensor.empty with its
85/// consumers.
86///
87/// If `singleUseOnly` is set to "true", only tensor.empty ops with a single
88/// use are folded.
89void populateFoldTensorEmptyPatterns(RewritePatternSet &patterns,
90 bool foldSingleUseOnly = false);
91
92/// Populates `patterns` with patterns that decompose `tensor.concat` into
93/// `tensor.empty` of a tensor of the concatenated size, followed by a chain
94/// of `tensor.insert_slice` operations on the inputs. This is intended to be
95/// used as a fallback tensor -> tensor lowering that decomposes concat such
96/// that it can be bufferized into a sequence of copies.
97void populateDecomposeTensorConcatPatterns(RewritePatternSet &patterns);
98
99using ControlFoldFn = std::function<bool(OpOperand *)>;
100
101/// Populates `patterns` with patterns that replace tensor ops (such as
102/// tensor.generate) with constants when possible.
104 const ControlFoldFn &controlFn);
105
106//===----------------------------------------------------------------------===//
107// Transform helpers
108//===----------------------------------------------------------------------===//
109
110/// Build a new tensor::PadOp with low/high padding that is independent of all
111/// given independencies. If the op is already independent of all
112/// independencies, the same PadOp result is returned.
113///
114/// Failure indicates the no suitable upper bound for low/high padding could be
115/// found.
116///
117/// Example:
118/// scf.for %iv = %lb to %ub step %step {
119/// %high = affine.apply affine_map<(d0)[s0] -> (s0 - d0)> (%i)[%ub]
120/// %p = tensor.pad %t low[5] high[%high] ...
121/// ...
122/// }
123///
124/// The function builds IR such as:
125/// %high_new = affine.apply affine_map<()[s0, s1] -> (-s0 + s1)> ()[%lb, %ub]
126/// %p_hoistable = tensor.pad %t low[5] high[%high_new]
127/// %dim = tensor.dim %t, %c0
128/// %size = affine.apply affine_map<(d0)[s0, s1] -> (-d0 + s0 + s1 + 5)>
129/// (%iv)[%ub, %dim]
130/// %slice = tensor.extract_slice %p_hoistable [0] [%size] [1]
131///
132/// The slice is returned.
133FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::PadOp padOp,
134 ValueRange independencies);
135
136/// Build a new tensor::EmptyOp who's dynamic sizes are independent of all
137/// given independencies. If the op is already independent of all
138/// independencies, the same EmptyOp result is returned.
139///
140/// Failure indicates the no suitable upper bound for the dynamic sizes could be
141/// found.
142FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::EmptyOp emptyOp,
143 ValueRange independencies);
144
145/// Computes the offsets, sizes, and strides needed to build a collapsed
146/// `sliceOp`. The dimensions to collapse are specified by `reassociation`.
147///
148/// This fails when the specified collapse cannot be represented by a valid
149/// ExtractSliceOp.
150LogicalResult
151getCollapsedExtractSliceInfo(OpBuilder &b, tensor::ExtractSliceOp sliceOp,
152 ArrayRef<ReassociationIndices> reassociation,
153 SmallVectorImpl<OpFoldResult> &collapsedOffsets,
154 SmallVectorImpl<OpFoldResult> &collapsedSizes,
155 SmallVectorImpl<OpFoldResult> &collapsedStrides);
156
157/// Computes the offsets, sizes, and strides needed to build an expanded
158/// `sliceOp`. The dimensions to expand are specified by `reassociation` and
159/// `expandedShape`.
160///
161/// This fails when the specified expansion cannot be represented by a valid
162/// ExtractSliceOp.
163LogicalResult
164getExpandedExtractSliceInfo(OpBuilder &b, tensor::ExtractSliceOp sliceOp,
165 ArrayRef<ReassociationIndices> reassociation,
166 ArrayRef<int64_t> expandedShape,
167 SmallVectorImpl<OpFoldResult> &expandedOffsets,
168 SmallVectorImpl<OpFoldResult> &expandedSizes,
169 SmallVectorImpl<OpFoldResult> &expandedStrides);
170
171} // namespace tensor
172} // namespace mlir
173
174#endif // MLIR_DIALECT_TENSOR_TRANSFORMS_TRANSFORMS_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
This class helps build Operations.
Definition Builders.h:207
This class represents an operand of an operation.
Definition Value.h:257
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
LogicalResult getCollapsedExtractSliceInfo(OpBuilder &b, tensor::ExtractSliceOp sliceOp, ArrayRef< ReassociationIndices > reassociation, SmallVectorImpl< OpFoldResult > &collapsedOffsets, SmallVectorImpl< OpFoldResult > &collapsedSizes, SmallVectorImpl< OpFoldResult > &collapsedStrides)
Computes the offsets, sizes, and strides needed to build a collapsed sliceOp.
void populateFoldTensorEmptyPatterns(RewritePatternSet &patterns, bool foldSingleUseOnly=false)
Populates patterns with patterns that fold tensor.empty with its consumers.
FailureOr< TilingResult > replaceExtractSliceWithTiledProducer(OpBuilder &builder, tensor::ExtractSliceOp sliceOp, OpResult producerOp)
Method to swap an tensor.extract_slice with its producer when the producer implements the TilingInter...
FailureOr< TilingResult > replaceInsertSlicesWithTiledConsumer(OpBuilder &builder, ArrayRef< tensor::InsertSliceOp > sliceOps, ArrayRef< OpOperand * > consumerOperands)
Method to swap tensor.insert_slices with their consumers when the consumer implements the TilingInter...
void populateMergeConsecutiveInsertExtractSlicePatterns(RewritePatternSet &patterns)
Collects patterns to merge consecutive tensor.insert_slice/extract_slice into one.
void populateDecomposeTensorConcatPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that decompose tensor.concat into tensor.empty of a tensor of the co...
void populateFoldTensorSubsetOpPatterns(RewritePatternSet &patterns)
Appends patterns for folding tensor subset ops into consumer load/store ops into patterns.
void populateReassociativeReshapeFoldingPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that fold tensor.expand_shape and tensor.collapse_shape into other o...
void populateBubbleUpExtractSliceOpPatterns(RewritePatternSet &patterns)
Appends patterns that are used to bubble up tensor.extract slice op above its producer.
std::function< bool(OpOperand *)> ControlFoldFn
Definition Transforms.h:99
void populateBubbleUpExpandShapePatterns(RewritePatternSet &patterns)
Populates patterns with patterns that bubble up tensor.expand_shape through tensor....
void populateDropRedundantInsertSliceRankExpansionPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that drop redundant tensor.insert_slice rank expansions.
FailureOr< Value > buildIndependentOp(OpBuilder &b, tensor::PadOp padOp, ValueRange independencies)
Build a new tensor::PadOp with low/high padding that is independent of all given independencies.
void populateRewriteAsConstantPatterns(RewritePatternSet &patterns, const ControlFoldFn &controlFn)
Populates patterns with patterns that replace tensor ops (such as tensor.generate) with constants whe...
void populateFoldTensorSubsetIntoVectorTransferPatterns(RewritePatternSet &patterns)
Appends patterns for folding tensor subset ops into vector transfer ops.
LogicalResult getExpandedExtractSliceInfo(OpBuilder &b, tensor::ExtractSliceOp sliceOp, ArrayRef< ReassociationIndices > reassociation, ArrayRef< int64_t > expandedShape, SmallVectorImpl< OpFoldResult > &expandedOffsets, SmallVectorImpl< OpFoldResult > &expandedSizes, SmallVectorImpl< OpFoldResult > &expandedStrides)
Computes the offsets, sizes, and strides needed to build an expanded sliceOp.
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
Container for result values of tiling.