MLIR  20.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 /// Populates `patterns` with patterns that drop redundant tensor.insert_slice
62 /// rank expansions.
64  RewritePatternSet &patterns);
65 
66 /// Populates `patterns` with patterns that fold `tensor.expand_shape` and
67 /// `tensor.collapse_shape` into other ops.
68 void populateReassociativeReshapeFoldingPatterns(RewritePatternSet &patterns);
69 
70 /// Populates `patterns` with patterns that fold tensor.empty with its
71 /// consumers.
72 ///
73 /// If `singleUseOnly` is set to "true", only tensor.empty ops with a single
74 /// use are folded.
75 void populateFoldTensorEmptyPatterns(RewritePatternSet &patterns,
76  bool foldSingleUseOnly = false);
77 
78 /// Populates `patterns` with patterns that decompose `tensor.concat` into
79 /// `tensor.empty` of a tensor of the concatenated size, followed by a chain
80 /// of `tensor.insert_slice` operations on the inputs. This is intended to be
81 /// used as a fallback tensor -> tensor lowering that decomposes concat such
82 /// that it can be bufferized into a sequence of copies.
83 void populateDecomposeTensorConcatPatterns(RewritePatternSet &patterns);
84 
85 /// Populates `patterns` with patterns that simplify `tensor.pack` and
86 /// `tensor.unpack` operations.
87 void populateSimplifyPackAndUnpackPatterns(RewritePatternSet &patterns);
88 
89 /// Populates `patterns` with patterns that fold operations like `tensor.pad`
90 /// and `tensor.extract_slice` into `tensor.pack` and `tensor.unpack` operations
91 /// respectively.
92 void populateFoldIntoPackAndUnpackPatterns(RewritePatternSet &patterns);
93 
94 using ControlFoldFn = std::function<bool(OpOperand *)>;
95 
96 /// Populates `patterns` with patterns that replace tensor ops (such as
97 /// tensor.generate) with constants when possible.
99  const ControlFoldFn &controlFn);
100 
101 //===----------------------------------------------------------------------===//
102 // Transform helpers
103 //===----------------------------------------------------------------------===//
104 
105 /// Build a new tensor::PadOp with low/high padding that is independent of all
106 /// given independencies. If the op is already independent of all
107 /// independencies, the same PadOp result is returned.
108 ///
109 /// Failure indicates the no suitable upper bound for low/high padding could be
110 /// found.
111 ///
112 /// Example:
113 /// scf.for %iv = %lb to %ub step %step {
114 /// %high = affine.apply affine_map<(d0)[s0] -> (s0 - d0)> (%i)[%ub]
115 /// %p = tensor.pad %t low[5] high[%high] ...
116 /// ...
117 /// }
118 ///
119 /// The function builds IR such as:
120 /// %high_new = affine.apply affine_map<()[s0, s1] -> (-s0 + s1)> ()[%lb, %ub]
121 /// %p_hoistable = tensor.pad %t low[5] high[%high_new]
122 /// %dim = tensor.dim %t, %c0
123 /// %size = affine.apply affine_map<(d0)[s0, s1] -> (-d0 + s0 + s1 + 5)>
124 /// (%iv)[%ub, %dim]
125 /// %slice = tensor.extract_slice %p_hoistable [0] [%size] [1]
126 ///
127 /// The slice is returned.
128 FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::PadOp padOp,
129  ValueRange independencies);
130 
131 /// Build a new tensor::EmptyOp who's dynamic sizes are independent of all
132 /// given independencies. If the op is already independent of all
133 /// independencies, the same EmptyOp result is returned.
134 ///
135 /// Failure indicates the no suitable upper bound for the dynamic sizes could be
136 /// found.
137 FailureOr<Value> buildIndependentOp(OpBuilder &b, tensor::EmptyOp emptyOp,
138  ValueRange independencies);
139 
140 } // namespace tensor
141 } // namespace mlir
142 
143 #endif // MLIR_DIALECT_TENSOR_TRANSFORMS_TRANSFORMS_H
This class helps build Operations.
Definition: Builders.h:210
This class represents an operand of an operation.
Definition: Value.h:267
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
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 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 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:94
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.
void populateSimplifyPackAndUnpackPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that simplify tensor.pack and tensor.unpack operations.
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.