MLIR  22.0.0git
VectorOps.h
Go to the documentation of this file.
1 //===- VectorOps.h - MLIR Vector Dialect Operations -------------*- 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 // This file defines the Vector dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_VECTOR_IR_VECTOROPS_H
14 #define MLIR_DIALECT_VECTOR_IR_VECTOROPS_H
15 
20 #include "mlir/IR/AffineMap.h"
21 #include "mlir/IR/Attributes.h"
22 #include "mlir/IR/BuiltinTypes.h"
23 #include "mlir/IR/Dialect.h"
24 #include "mlir/IR/OpDefinition.h"
25 #include "mlir/IR/PatternMatch.h"
34 #include "llvm/ADT/SetVector.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/Support/Alignment.h"
37 
38 // Pull in all enum type definitions and utility function declarations.
39 #include "mlir/Dialect/Vector/IR/VectorEnums.h.inc"
40 
41 #define GET_ATTRDEF_CLASSES
42 #include "mlir/Dialect/Vector/IR/VectorAttributes.h.inc"
43 
44 namespace mlir {
45 class MLIRContext;
46 class RewritePatternSet;
47 
48 namespace arith {
49 enum class AtomicRMWKind : uint64_t;
50 } // namespace arith
51 
52 namespace vector {
53 class ContractionOp;
54 class TransferReadOp;
55 class TransferWriteOp;
56 class VectorDialect;
57 
58 namespace detail {
59 struct BitmaskEnumStorage;
60 } // namespace detail
61 
62 /// Predefined constant_mask kinds.
63 enum class ConstantMaskKind { AllFalse = 0, AllTrue };
64 
65 /// Default callback to build a region with a 'vector.yield' terminator with no
66 /// arguments.
67 void buildTerminatedBody(OpBuilder &builder, Location loc);
68 
69 /// Return whether `srcType` can be broadcast to `dstVectorType` under the
70 /// semantics of the `vector.broadcast` op.
72  Success = 0,
73  SourceRankHigher = 1,
76 };
77 
78 struct VectorDim {
79  int64_t dim;
80  bool isScalable;
81 };
83 isBroadcastableTo(Type srcType, VectorType dstVectorType,
84  std::pair<VectorDim, VectorDim> *mismatchingDims = nullptr);
85 
86 /// Collect a set of vector-to-vector canonicalization patterns.
88  PatternBenefit benefit = 1);
89 
90 /// Collect a set of patterns that fold arithmetic extension on floating point
91 /// into vector contract for the backends with native support.
93 
94 /// Collect a set of patterns that fold elementwise op on vectors to the vector
95 /// dialect.
97 
98 /// Returns the integer type required for subscripts in the vector dialect.
99 IntegerType getVectorSubscriptType(Builder &builder);
100 
101 /// Returns an integer array attribute containing the given values using
102 /// the integer type required for subscripts in the vector dialect.
103 ArrayAttr getVectorSubscriptAttr(Builder &b, ArrayRef<int64_t> values);
104 
105 /// Returns the value obtained by reducing the vector into a scalar using the
106 /// operation kind associated with a binary AtomicRMWKind op.
107 Value getVectorReductionOp(arith::AtomicRMWKind op, OpBuilder &builder,
108  Location loc, Value vector);
109 
110 /// Build the default minor identity map suitable for a vector transfer. This
111 /// also handles the case memref<... x vector<...>> -> vector<...> in which the
112 /// rank of the identity map must take the vector element type into account.
113 AffineMap getTransferMinorIdentityMap(ShapedType shapedType,
114  VectorType vectorType);
115 
116 /// Return true if the transfer_write fully writes the data accessed by the
117 /// transfer_read.
118 bool checkSameValueRAW(TransferWriteOp defWrite, TransferReadOp read);
119 
120 /// Return true if the write op fully over-write the priorWrite transfer_write
121 /// op.
122 bool checkSameValueWAW(TransferWriteOp write, TransferWriteOp priorWrite);
123 
124 /// Return true if we can prove that the transfer operations access disjoint
125 /// memory, without requring the accessed tensor/memref to be the same.
126 ///
127 /// If `testDynamicValueUsingBounds` is true, tries to test dynamic values
128 /// via ValueBoundsOpInterface.
129 bool isDisjointTransferIndices(VectorTransferOpInterface transferA,
130  VectorTransferOpInterface transferB,
131  bool testDynamicValueUsingBounds = false);
132 
133 /// Return true if we can prove that the transfer operations access disjoint
134 /// memory, requiring the operations to access the same tensor/memref.
135 ///
136 /// If `testDynamicValueUsingBounds` is true, tries to test dynamic values
137 /// via ValueBoundsOpInterface.
138 bool isDisjointTransferSet(VectorTransferOpInterface transferA,
139  VectorTransferOpInterface transferB,
140  bool testDynamicValueUsingBounds = false);
141 
142 /// Returns the result value of reducing two scalar/vector values with the
143 /// corresponding arith operation.
145  Value v1, Value acc,
146  arith::FastMathFlagsAttr fastmath = nullptr,
147  Value mask = nullptr);
148 
149 /// Returns true if `attr` has "parallel" iterator type semantics.
150 inline bool isParallelIterator(Attribute attr) {
151  return cast<IteratorTypeAttr>(attr).getValue() == IteratorType::parallel;
152 }
153 
154 /// Returns true if `attr` has "reduction" iterator type semantics.
155 inline bool isReductionIterator(Attribute attr) {
156  return cast<IteratorTypeAttr>(attr).getValue() == IteratorType::reduction;
157 }
158 
159 /// Returns the integer numbers in `values`. `values` are expected to be
160 /// constant operations.
162 
163 /// Returns the integer numbers in `foldResults`. `foldResults` are expected to
164 /// be constant operations.
166 
167 /// Convert `foldResults` into Values. Integer attributes are converted to
168 /// constant op.
170  ArrayRef<OpFoldResult> foldResults);
171 
172 /// If `value` is a constant multiple of `vector.vscale` (e.g. `%cst *
173 /// vector.vscale`), return the multiplier (`%cst`). Otherwise, return
174 /// `std::nullopt`.
175 std::optional<int64_t> getConstantVscaleMultiplier(Value value);
176 
177 //===----------------------------------------------------------------------===//
178 // Vector Masking Utilities
179 //===----------------------------------------------------------------------===//
180 
181 /// Infers the mask type for a transfer op given its vector type and
182 /// permutation map. The mask in a transfer op operation applies to the
183 /// tensor/buffer part of it and its type should match the vector shape
184 /// *before* any permutation or broadcasting. For example,
185 ///
186 /// vecType = vector<1x2x3xf32>, permMap = affine_map<(d0, d1, d2) -> (d1, d0)>
187 ///
188 /// Has inferred mask type:
189 ///
190 /// maskType = vector<2x1xi1>
191 VectorType inferTransferOpMaskType(VectorType vecType, AffineMap permMap);
192 
193 /// Create the vector.yield-ended region of a vector.mask op with `maskableOp`
194 /// as masked operation.
195 void createMaskOpRegion(OpBuilder &builder, Operation *maskableOp);
196 
197 /// Creates a vector.mask operation around a maskable operation. Returns the
198 /// vector.mask operation if the mask provided is valid. Otherwise, returns the
199 /// maskable operation itself.
200 Operation *maskOperation(OpBuilder &builder, Operation *maskableOp, Value mask,
201  Value passthru = Value());
202 
203 /// Creates a vector select operation that picks values from `newValue` or
204 /// `passthru` for each result vector lane based on `mask`. This utility is used
205 /// to propagate the pass-thru value for masked-out or expeculatively executed
206 /// lanes. VP intrinsics do not support pass-thru values and every mask-out lane
207 /// is set to poison. LLVM backends are usually able to match op + select
208 /// patterns and fold them into a native target instructions.
209 Value selectPassthru(OpBuilder &builder, Value mask, Value newValue,
210  Value passthru);
211 
212 } // namespace vector
213 } // namespace mlir
214 
215 #define GET_OP_CLASSES
216 #include "mlir/Dialect/Vector/IR/VectorDialect.h.inc"
217 #include "mlir/Dialect/Vector/IR/VectorOps.h.inc"
218 
219 #endif // MLIR_DIALECT_VECTOR_IR_VECTOROPS_H
union mlir::linalg::@1252::ArityGroupAndKind::Kind kind
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition: AffineMap.h:46
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:51
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
This class helps build Operations.
Definition: Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
Definition: PatternMatch.h:34
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Value makeArithReduction(OpBuilder &b, Location loc, CombiningKind kind, Value v1, Value acc, arith::FastMathFlagsAttr fastmath=nullptr, Value mask=nullptr)
Returns the result value of reducing two scalar/vector values with the corresponding arith operation.
ArrayAttr getVectorSubscriptAttr(Builder &b, ArrayRef< int64_t > values)
Returns an integer array attribute containing the given values using the integer type required for su...
Definition: VectorOps.cpp:508
bool isReductionIterator(Attribute attr)
Returns true if attr has "reduction" iterator type semantics.
Definition: VectorOps.h:155
void buildTerminatedBody(OpBuilder &builder, Location loc)
Default callback to build a region with a 'vector.yield' terminator with no arguments.
Definition: VectorOps.cpp:127
void populateElementwiseToVectorOpsPatterns(RewritePatternSet &patterns)
Collect a set of patterns that fold elementwise op on vectors to the vector dialect.
std::optional< int64_t > getConstantVscaleMultiplier(Value value)
If value is a constant multiple of vector.vscale (e.g.
Definition: VectorOps.cpp:385
AffineMap getTransferMinorIdentityMap(ShapedType shapedType, VectorType vectorType)
Build the default minor identity map suitable for a vector transfer.
Definition: VectorOps.cpp:189
bool checkSameValueRAW(TransferWriteOp defWrite, TransferReadOp read)
Return true if the transfer_write fully writes the data accessed by the transfer_read.
ConstantMaskKind
Predefined constant_mask kinds.
Definition: VectorOps.h:63
Operation * maskOperation(OpBuilder &builder, Operation *maskableOp, Value mask, Value passthru=Value())
Creates a vector.mask operation around a maskable operation.
BroadcastableToResult isBroadcastableTo(Type srcType, VectorType dstVectorType, std::pair< VectorDim, VectorDim > *mismatchingDims=nullptr)
Definition: VectorOps.cpp:2916
VectorType inferTransferOpMaskType(VectorType vecType, AffineMap permMap)
Infers the mask type for a transfer op given its vector type and permutation map.
Definition: VectorOps.cpp:4815
Value selectPassthru(OpBuilder &builder, Value mask, Value newValue, Value passthru)
Creates a vector select operation that picks values from newValue or passthru for each result vector ...
bool isDisjointTransferIndices(VectorTransferOpInterface transferA, VectorTransferOpInterface transferB, bool testDynamicValueUsingBounds=false)
Return true if we can prove that the transfer operations access disjoint memory, without requring the...
Definition: VectorOps.cpp:251
bool isDisjointTransferSet(VectorTransferOpInterface transferA, VectorTransferOpInterface transferB, bool testDynamicValueUsingBounds=false)
Return true if we can prove that the transfer operations access disjoint memory, requiring the operat...
Definition: VectorOps.cpp:315
bool checkSameValueWAW(TransferWriteOp write, TransferWriteOp priorWrite)
Return true if the write op fully over-write the priorWrite transfer_write op.
bool isParallelIterator(Attribute attr)
Returns true if attr has "parallel" iterator type semantics.
Definition: VectorOps.h:150
void populateFoldArithExtensionPatterns(RewritePatternSet &patterns)
Collect a set of patterns that fold arithmetic extension on floating point into vector contract for t...
void populateVectorToVectorCanonicalizationPatterns(RewritePatternSet &patterns, PatternBenefit benefit=1)
Collect a set of vector-to-vector canonicalization patterns.
void createMaskOpRegion(OpBuilder &builder, Operation *maskableOp)
Create the vector.yield-ended region of a vector.mask op with maskableOp as masked operation.
SmallVector< Value > getAsValues(OpBuilder &builder, Location loc, ArrayRef< OpFoldResult > foldResults)
Convert foldResults into Values.
Definition: VectorOps.cpp:370
Value getVectorReductionOp(arith::AtomicRMWKind op, OpBuilder &builder, Location loc, Value vector)
Returns the value obtained by reducing the vector into a scalar using the operation kind associated w...
Definition: VectorOps.cpp:684
BroadcastableToResult
Return whether srcType can be broadcast to dstVectorType under the semantics of the vector....
Definition: VectorOps.h:71
IntegerType getVectorSubscriptType(Builder &builder)
Returns the integer type required for subscripts in the vector dialect.
Definition: VectorOps.cpp:504
SmallVector< int64_t > getAsIntegers(ArrayRef< OpFoldResult > foldResults)
Returns the integer numbers in foldResults.
Definition: VectorOps.cpp:358
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns