MLIR  19.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"
14 
15 namespace mlir {
16 
17 struct TilingResult;
18 
19 namespace tensor {
20 
21 //===----------------------------------------------------------------------===//
22 // Patterns
23 //===----------------------------------------------------------------------===//
24 
25 /// Pattern to swap an `tensor.extract_slice` with its producer when the
26 /// producer implements the `TilingInterface`. The pattern itself does not
27 /// provide a mechanism to control where the application happens. With use of
28 /// transform dialect that control is done within the transform dialect. Other
29 /// use cases can inherit from this pattern and add necessary controls.
30 FailureOr<TilingResult> replaceExtractSliceWithTiledProducer(
31  OpBuilder &builder, tensor::ExtractSliceOp sliceOp, OpResult producerOp);
32 
33 //===----------------------------------------------------------------------===//
34 // Populate functions.
35 //===----------------------------------------------------------------------===//
36 
37 /// Appends patterns for folding tensor subset ops into consumer load/store
38 /// ops into `patterns`. (This includes patterns for folding tensor subset ops
39 /// into vector transfer ops.)
40 void populateFoldTensorSubsetOpPatterns(RewritePatternSet &patterns);
41 
42 /// Appends patterns for folding tensor subset ops into vector transfer ops.
44  RewritePatternSet &patterns);
45 
46 /// Collects patterns to merge consecutive tensor.insert_slice/extract_slice
47 /// into one. These patterns are in this separate entry point because the
48 /// bufferization is sensitive to IR structure, particularly those
49 /// tensor.extract_slice and tensor.insert_slice ops for creating the slices.
51  RewritePatternSet &patterns);
52 
53 /// Populates `patterns` with patterns that drop redundant tensor.insert_slice
54 /// rank expansions.
56  RewritePatternSet &patterns);
57 
58 /// Populates `patterns` with patterns that fold `tensor.expand_shape` and
59 /// `tensor.collapse_shape` into other ops.
60 void populateReassociativeReshapeFoldingPatterns(RewritePatternSet &patterns);
61 
62 /// Populates `patterns` with patterns that fold tensor.empty with
63 /// tensor.[extract_slice|expand_shape|collapse_shape].
64 ///
65 /// If `singleUseOnly` is set to "true", only tensor.empty ops with a single
66 /// use are folded.
67 void populateFoldTensorEmptyPatterns(RewritePatternSet &patterns,
68  bool foldSingleUseOnly = false);
69 
70 /// Populates `patterns` with patterns that decompose `tensor.concat` into
71 /// `tensor.empty` of a tensor of the concatenated size, followed by a chain
72 /// of `tensor.insert_slice` operations on the inputs. This is intended to be
73 /// used as a fallback tensor -> tensor lowering that decomposes concat such
74 /// that it can be bufferized into a sequence of copies.
75 void populateDecomposeTensorConcatPatterns(RewritePatternSet &patterns);
76 
77 /// Populates `patterns` with patterns that simplify `tensor.pack` and
78 /// `tensor.unpack` operations.
79 void populateSimplifyPackAndUnpackPatterns(RewritePatternSet &patterns);
80 
81 /// Populates `patterns` with patterns that fold operations like `tensor.pad`
82 /// and `tensor.extract_slice` into `tensor.pack` and `tensor.unpack` operations
83 /// respectively.
84 void populateFoldIntoPackAndUnpackPatterns(RewritePatternSet &patterns);
85 
86 /// Populates `patterns` with patterns that replace tensor ops (such as
87 /// tensor.generate) with constants when possible.
88 void populateRewriteAsConstantPatterns(RewritePatternSet &patterns);
89 
90 //===----------------------------------------------------------------------===//
91 // Transform helpers
92 //===----------------------------------------------------------------------===//
93 
94 /// Build a new tensor::PadOp with low/high padding that is independent of all
95 /// given independencies. If the op is already independent of all
96 /// independencies, the same PadOp result is returned.
97 ///
98 /// Failure indicates the no suitable upper bound for low/high padding could be
99 /// found.
100 ///
101 /// Example:
102 /// scf.for %iv = %lb to %ub step %step {
103 /// %high = affine.apply affine_map<(d0)[s0] -> (s0 - d0)> (%i)[%ub]
104 /// %p = tensor.pad %t low[5] high[%high] ...
105 /// ...
106 /// }
107 ///
108 /// The function builds IR such as:
109 /// %high_new = affine.apply affine_map<()[s0, s1] -> (-s0 + s1)> ()[%lb, %ub]
110 /// %p_hoistable = tensor.pad %t low[5] high[%high_new]
111 /// %dim = tensor.dim %t, %c0
112 /// %size = affine.apply affine_map<(d0)[s0, s1] -> (-d0 + s0 + s1 + 5)>
113 /// (%iv)[%ub, %dim]
114 /// %slice = tensor.extract_slice %p_hoistable [0] [%size] [1]
115 ///
116 /// The slice is returned.
117 FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::PadOp padOp,
118  ValueRange independencies);
119 
120 /// Build a new tensor::EmptyOp who's dynamic sizes are independent of all
121 /// given independencies. If the op is already independent of all
122 /// independencies, the same EmptyOp result is returned.
123 ///
124 /// Failure indicates the no suitable upper bound for the dynamic sizes could be
125 /// found.
126 FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::EmptyOp emptyOp,
127  ValueRange independencies);
128 
129 } // namespace tensor
130 } // namespace mlir
131 
132 #endif // MLIR_DIALECT_TENSOR_TRANSFORMS_TRANSFORMS_H
void populateFoldIntoPackAndUnpackPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that fold operations like tensor.pad and tensor.extract_slice into t...
void populateFoldTensorEmptyPatterns(RewritePatternSet &patterns, bool foldSingleUseOnly=false)
Populates patterns with patterns that fold tensor.empty with tensor.
FailureOr< TilingResult > replaceExtractSliceWithTiledProducer(OpBuilder &builder, tensor::ExtractSliceOp sliceOp, OpResult producerOp)
Pattern to swap an tensor.extract_slice with its producer when the producer implements the TilingInte...
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 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 populateFoldTensorSubsetIntoVectorTransferPatterns(RewritePatternSet &patterns)
Appends patterns for folding tensor subset ops into vector transfer ops.
void populateRewriteAsConstantPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that replace tensor ops (such as tensor.generate) with constants whe...
void populateSimplifyPackAndUnpackPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that simplify tensor.pack and tensor.unpack operations.
Include the generated interface declarations.