MLIR  18.0.0git
VectorTransforms.h
Go to the documentation of this file.
1 //===- VectorTransforms.h - Vector transformations as 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_VECTOR_TRANSFORMS_VECTORTRANSFORMS_H
10 #define MLIR_DIALECT_VECTOR_TRANSFORMS_VECTORTRANSFORMS_H
11 
14 
15 namespace mlir {
16 class MLIRContext;
17 class VectorTransferOpInterface;
18 class RewritePatternSet;
19 class RewriterBase;
20 
21 namespace scf {
22 class IfOp;
23 } // namespace scf
24 
25 namespace vector {
26 
27 //===----------------------------------------------------------------------===//
28 // Vector transformation options exposed as auxiliary structs.
29 //===----------------------------------------------------------------------===//
30 
31 /// Structure to control the behavior of vector transform patterns.
33  /// Option to control the lowering of vector.contract.
34  VectorContractLowering vectorContractLowering = VectorContractLowering::Dot;
36  setVectorTransformsOptions(VectorContractLowering opt) {
38  return *this;
39  }
40  /// Option to control the lowering of vector.multi_reduction.
41  VectorMultiReductionLowering vectorMultiReductionLowering =
42  VectorMultiReductionLowering::InnerParallel;
44  setVectorMultiReductionLowering(VectorMultiReductionLowering opt) {
46  return *this;
47  }
48  /// Option to control the lowering of vector.transpose.
49  VectorTransposeLowering vectorTransposeLowering =
50  VectorTransposeLowering::EltWise;
52  setVectorTransposeLowering(VectorTransposeLowering opt) {
54  return *this;
55  }
56  /// Option to control the splitting of vector transfers.
58  VectorTransformsOptions &setVectorTransferSplit(VectorTransferSplit opt) {
59  vectorTransferSplit = opt;
60  return *this;
61  }
62 
63  /// Option to control if vector.transpose can lower to a vector.shape_cast.
64  /// TODO: ATM it's not possible to lower `vector.shape_cast` to SPIR-V
65  /// and hence the need for this opt-out. Once the missing support has been
66  /// added, this option can be removed.
67  bool useShapeCast = true;
69  useShapeCast = opt;
70  return *this;
71  }
72 };
73 
74 //===----------------------------------------------------------------------===//
75 // Standalone transformations and helpers.
76 //===----------------------------------------------------------------------===//
77 
78 /// Split a vector.transfer operation into an in-bounds (i.e., no
79 /// out-of-bounds masking) fastpath and a slowpath. If `ifOp` is not null and
80 /// the result is `success, the `ifOp` points to the newly created conditional
81 /// upon function return. To accomodate for the fact that the original
82 /// vector.transfer indexing may be arbitrary and the slow path indexes
83 /// @[0...0] in the temporary buffer, the scf.if op returns a view and values
84 /// of type index. At this time, only vector.transfer_read case is
85 /// implemented.
86 ///
87 /// Example (a 2-D vector.transfer_read):
88 /// ```
89 /// %1 = vector.transfer_read %0[...], %pad : memref<A...>, vector<...>
90 /// ```
91 /// is transformed into:
92 /// ```
93 /// %1:3 = scf.if (%inBounds) {
94 /// // fastpath, direct cast
95 /// memref.cast %A: memref<A...> to compatibleMemRefType
96 /// scf.yield %view : compatibleMemRefType, index, index
97 /// } else {
98 /// // slowpath, not in-bounds vector.transfer or linalg.copy.
99 /// memref.cast %alloc: memref<B...> to compatibleMemRefType
100 /// scf.yield %4 : compatibleMemRefType, index, index
101 // }
102 /// %0 = vector.transfer_read %1#0[%1#1, %1#2] {in_bounds = [true ...
103 /// true]}
104 /// ```
105 /// where `alloc` is a top of the function alloca'ed buffer of one vector.
106 ///
107 /// Preconditions:
108 /// 1. `xferOp.permutation_map()` must be a minor identity map
109 /// 2. the rank of the `xferOp.memref()` and the rank of the
110 /// `xferOp.vector()` must be equal. This will be relaxed in the future but
111 /// requires rank-reducing subviews.
113  RewriterBase &b, VectorTransferOpInterface xferOp,
114  VectorTransformsOptions options = VectorTransformsOptions(),
115  scf::IfOp *ifOp = nullptr);
116 
117 /// Implements transfer op write to read forwarding and dead transfer write
118 /// optimizations.
119 void transferOpflowOpt(RewriterBase &rewriter, Operation *rootOp);
120 
121 /// Cast away the leading unit dim, if exists, for the given contract op.
122 /// Return success if the transformation applies; return failure otherwise.
123 LogicalResult castAwayContractionLeadingOneDim(vector::ContractionOp contractOp,
124  RewriterBase &rewriter);
125 
126 } // namespace vector
127 } // namespace mlir
128 
129 #endif // MLIR_DIALECT_VECTOR_TRANSFORMS_VECTORTRANSFORMS_H
@ None
static llvm::ManagedStatic< PassManagerOptions > options
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:399
LogicalResult castAwayContractionLeadingOneDim(vector::ContractionOp contractOp, RewriterBase &rewriter)
Cast away the leading unit dim, if exists, for the given contract op.
LogicalResult splitFullAndPartialTransfer(RewriterBase &b, VectorTransferOpInterface xferOp, VectorTransformsOptions options=VectorTransformsOptions(), scf::IfOp *ifOp=nullptr)
Split a vector.transfer operation into an in-bounds (i.e., no out-of-bounds masking) fastpath and a s...
void transferOpflowOpt(RewriterBase &rewriter, Operation *rootOp)
Implements transfer op write to read forwarding and dead transfer write optimizations.
Include the generated interface declarations.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Structure to control the behavior of vector transform patterns.
VectorTransferSplit vectorTransferSplit
Option to control the splitting of vector transfers.
VectorTransformsOptions & setVectorMultiReductionLowering(VectorMultiReductionLowering opt)
VectorMultiReductionLowering vectorMultiReductionLowering
Option to control the lowering of vector.multi_reduction.
VectorTransformsOptions & setUseShapeCast(bool opt=true)
bool useShapeCast
Option to control if vector.transpose can lower to a vector.shape_cast.
VectorTransformsOptions & setVectorTransposeLowering(VectorTransposeLowering opt)
VectorTransformsOptions & setVectorTransferSplit(VectorTransferSplit opt)
VectorTransformsOptions & setVectorTransformsOptions(VectorContractLowering opt)
VectorContractLowering vectorContractLowering
Option to control the lowering of vector.contract.
VectorTransposeLowering vectorTransposeLowering
Option to control the lowering of vector.transpose.