MLIR  21.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 
13 #include "mlir/IR/PatternMatch.h"
15 
16 namespace mlir {
17 
18 struct TilingResult;
19 
20 namespace 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.
31 FailureOr<TilingResult> replaceExtractSliceWithTiledProducer(
32  OpBuilder &builder, tensor::ExtractSliceOp sliceOp, OpResult producerOp);
33 
34 /// Method to swap an `tensor.insert_slice` with its consumer when the
35 /// consumer implements the `TilingInterface`.
36 FailureOr<TilingResult>
37 replaceInsertSliceWithTiledConsumer(OpBuilder &builder,
38  OffsetSizeAndStrideOpInterface sliceOp,
39  OpOperand &consumerOp);
40 
41 //===----------------------------------------------------------------------===//
42 // Populate functions.
43 //===----------------------------------------------------------------------===//
44 
45 /// Appends patterns for folding tensor subset ops into consumer load/store
46 /// ops into `patterns`. (This includes patterns for folding tensor subset ops
47 /// into vector transfer ops.)
48 void populateFoldTensorSubsetOpPatterns(RewritePatternSet &patterns);
49 
50 /// Appends patterns for folding tensor subset ops into vector transfer ops.
52  RewritePatternSet &patterns);
53 
54 /// Collects patterns to merge consecutive tensor.insert_slice/extract_slice
55 /// into one. These patterns are in this separate entry point because the
56 /// bufferization is sensitive to IR structure, particularly those
57 /// tensor.extract_slice and tensor.insert_slice ops for creating the slices.
59  RewritePatternSet &patterns);
60 
61 /// Appends patterns that are used to bubble up tensor.extract slice op above
62 /// its producer. When used as cleanup patterns of tile and fuse, enables fusing
63 /// the producer with the consumer even if the producer does not implement the
64 /// tiling interface.
65 void populateBubbleUpExtractSliceOpPatterns(RewritePatternSet &patterns);
66 
67 /// Populates `patterns` with patterns that drop redundant tensor.insert_slice
68 /// rank expansions.
70  RewritePatternSet &patterns);
71 
72 /// Populates `patterns` with patterns that fold `tensor.expand_shape` and
73 /// `tensor.collapse_shape` into other ops.
75 
76 /// Populates `patterns` with patterns that bubble up `tensor.expand_shape`
77 /// through `tensor.collapse_shape` ops.
78 void populateBubbleUpExpandShapePatterns(RewritePatternSet &patterns);
79 
80 /// Populates `patterns` with patterns that fold tensor.empty with its
81 /// consumers.
82 ///
83 /// If `singleUseOnly` is set to "true", only tensor.empty ops with a single
84 /// use are folded.
85 void populateFoldTensorEmptyPatterns(RewritePatternSet &patterns,
86  bool foldSingleUseOnly = false);
87 
88 /// Populates `patterns` with patterns that decompose `tensor.concat` into
89 /// `tensor.empty` of a tensor of the concatenated size, followed by a chain
90 /// of `tensor.insert_slice` operations on the inputs. This is intended to be
91 /// used as a fallback tensor -> tensor lowering that decomposes concat such
92 /// that it can be bufferized into a sequence of copies.
93 void populateDecomposeTensorConcatPatterns(RewritePatternSet &patterns);
94 
95 using ControlFoldFn = std::function<bool(OpOperand *)>;
96 
97 /// Populates `patterns` with patterns that replace tensor ops (such as
98 /// tensor.generate) with constants when possible.
100  const ControlFoldFn &controlFn);
101 
102 //===----------------------------------------------------------------------===//
103 // Transform helpers
104 //===----------------------------------------------------------------------===//
105 
106 /// Build a new tensor::PadOp with low/high padding that is independent of all
107 /// given independencies. If the op is already independent of all
108 /// independencies, the same PadOp result is returned.
109 ///
110 /// Failure indicates the no suitable upper bound for low/high padding could be
111 /// found.
112 ///
113 /// Example:
114 /// scf.for %iv = %lb to %ub step %step {
115 /// %high = affine.apply affine_map<(d0)[s0] -> (s0 - d0)> (%i)[%ub]
116 /// %p = tensor.pad %t low[5] high[%high] ...
117 /// ...
118 /// }
119 ///
120 /// The function builds IR such as:
121 /// %high_new = affine.apply affine_map<()[s0, s1] -> (-s0 + s1)> ()[%lb, %ub]
122 /// %p_hoistable = tensor.pad %t low[5] high[%high_new]
123 /// %dim = tensor.dim %t, %c0
124 /// %size = affine.apply affine_map<(d0)[s0, s1] -> (-d0 + s0 + s1 + 5)>
125 /// (%iv)[%ub, %dim]
126 /// %slice = tensor.extract_slice %p_hoistable [0] [%size] [1]
127 ///
128 /// The slice is returned.
129 FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::PadOp padOp,
130  ValueRange independencies);
131 
132 /// Build a new tensor::EmptyOp who's dynamic sizes are independent of all
133 /// given independencies. If the op is already independent of all
134 /// independencies, the same EmptyOp result is returned.
135 ///
136 /// Failure indicates the no suitable upper bound for the dynamic sizes could be
137 /// found.
138 FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::EmptyOp emptyOp,
139  ValueRange independencies);
140 
141 } // namespace tensor
142 } // namespace mlir
143 
144 #endif // MLIR_DIALECT_TENSOR_TRANSFORMS_TRANSFORMS_H
This class helps build Operations.
Definition: Builders.h:205
This class represents an operand of an operation.
Definition: Value.h:243
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
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...
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.
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.
std::function< bool(OpOperand *)> ControlFoldFn
Definition: Transforms.h:95
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.
FailureOr< TilingResult > replaceInsertSliceWithTiledConsumer(OpBuilder &builder, OffsetSizeAndStrideOpInterface sliceOp, OpOperand &consumerOp)
Method to swap an tensor.insert_slice with its consumer when the consumer implements the TilingInterf...
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns