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