MLIR  20.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"
32 #include "llvm/ADT/SetVector.h"
33 #include "llvm/ADT/StringExtras.h"
34 
35 // Pull in all enum type definitions and utility function declarations.
36 #include "mlir/Dialect/Vector/IR/VectorEnums.h.inc"
37 
38 #define GET_ATTRDEF_CLASSES
39 #include "mlir/Dialect/Vector/IR/VectorAttributes.h.inc"
40 
41 namespace mlir {
42 class MLIRContext;
43 class RewritePatternSet;
44 
45 namespace arith {
46 enum class AtomicRMWKind : uint64_t;
47 } // namespace arith
48 
49 namespace vector {
50 class ContractionOp;
51 class TransferReadOp;
52 class TransferWriteOp;
53 class VectorDialect;
54 
55 namespace detail {
56 struct BitmaskEnumStorage;
57 } // namespace detail
58 
59 /// Default callback to build a region with a 'vector.yield' terminator with no
60 /// arguments.
61 void buildTerminatedBody(OpBuilder &builder, Location loc);
62 
63 /// Return whether `srcType` can be broadcast to `dstVectorType` under the
64 /// semantics of the `vector.broadcast` op.
66  Success = 0,
67  SourceRankHigher = 1,
70 };
72 isBroadcastableTo(Type srcType, VectorType dstVectorType,
73  std::pair<int, int> *mismatchingDims = nullptr);
74 
75 /// Collect a set of vector-to-vector canonicalization patterns.
77  PatternBenefit benefit = 1);
78 
79 /// Collect a set of patterns that fold arithmetic extension on floating point
80 /// into vector contract for the backends with native support.
82 
83 /// Collect a set of patterns that fold elementwise op on vectors to the vector
84 /// dialect.
86 
87 /// Returns the integer type required for subscripts in the vector dialect.
88 IntegerType getVectorSubscriptType(Builder &builder);
89 
90 /// Returns an integer array attribute containing the given values using
91 /// the integer type required for subscripts in the vector dialect.
93 
94 /// Returns the value obtained by reducing the vector into a scalar using the
95 /// operation kind associated with a binary AtomicRMWKind op.
96 Value getVectorReductionOp(arith::AtomicRMWKind op, OpBuilder &builder,
97  Location loc, Value vector);
98 
99 /// Build the default minor identity map suitable for a vector transfer. This
100 /// also handles the case memref<... x vector<...>> -> vector<...> in which the
101 /// rank of the identity map must take the vector element type into account.
102 AffineMap getTransferMinorIdentityMap(ShapedType shapedType,
103  VectorType vectorType);
104 
105 /// Return true if the transfer_write fully writes the data accessed by the
106 /// transfer_read.
107 bool checkSameValueRAW(TransferWriteOp defWrite, TransferReadOp read);
108 
109 /// Return true if the write op fully over-write the priorWrite transfer_write
110 /// op.
111 bool checkSameValueWAW(TransferWriteOp write, TransferWriteOp priorWrite);
112 
113 /// Return true if we can prove that the transfer operations access disjoint
114 /// memory, without requring the accessed tensor/memref to be the same.
115 ///
116 /// If `testDynamicValueUsingBounds` is true, tries to test dynamic values
117 /// via ValueBoundsOpInterface.
118 bool isDisjointTransferIndices(VectorTransferOpInterface transferA,
119  VectorTransferOpInterface transferB,
120  bool testDynamicValueUsingBounds = false);
121 
122 /// Return true if we can prove that the transfer operations access disjoint
123 /// memory, requiring the operations to access the same tensor/memref.
124 ///
125 /// If `testDynamicValueUsingBounds` is true, tries to test dynamic values
126 /// via ValueBoundsOpInterface.
127 bool isDisjointTransferSet(VectorTransferOpInterface transferA,
128  VectorTransferOpInterface transferB,
129  bool testDynamicValueUsingBounds = false);
130 
131 /// Returns the result value of reducing two scalar/vector values with the
132 /// corresponding arith operation.
133 Value makeArithReduction(OpBuilder &b, Location loc, CombiningKind kind,
134  Value v1, Value acc,
135  arith::FastMathFlagsAttr fastmath = nullptr,
136  Value mask = nullptr);
137 
138 /// Returns true if `attr` has "parallel" iterator type semantics.
139 inline bool isParallelIterator(Attribute attr) {
140  return cast<IteratorTypeAttr>(attr).getValue() == IteratorType::parallel;
141 }
142 
143 /// Returns true if `attr` has "reduction" iterator type semantics.
144 inline bool isReductionIterator(Attribute attr) {
145  return cast<IteratorTypeAttr>(attr).getValue() == IteratorType::reduction;
146 }
147 
148 /// Returns the integer numbers in `values`. `values` are expected to be
149 /// constant operations.
151 
152 /// Returns the integer numbers in `foldResults`. `foldResults` are expected to
153 /// be constant operations.
155 
156 /// Convert `foldResults` into Values. Integer attributes are converted to
157 /// constant op.
159  ArrayRef<OpFoldResult> foldResults);
160 
161 /// Returns the constant index ops in `values`. `values` are expected to be
162 /// constant operations.
165 
166 //===----------------------------------------------------------------------===//
167 // Vector Masking Utilities
168 //===----------------------------------------------------------------------===//
169 
170 /// Infers the mask type for a transfer op given its vector type and
171 /// permutation map. The mask in a transfer op operation applies to the
172 /// tensor/buffer part of it and its type should match the vector shape
173 /// *before* any permutation or broadcasting. For example,
174 ///
175 /// vecType = vector<1x2x3xf32>, permMap = affine_map<(d0, d1, d2) -> (d1, d0)>
176 ///
177 /// Has inferred mask type:
178 ///
179 /// maskType = vector<2x1xi1>
180 VectorType inferTransferOpMaskType(VectorType vecType, AffineMap permMap);
181 
182 /// Create the vector.yield-ended region of a vector.mask op with `maskableOp`
183 /// as masked operation.
184 void createMaskOpRegion(OpBuilder &builder, Operation *maskableOp);
185 
186 /// Creates a vector.mask operation around a maskable operation. Returns the
187 /// vector.mask operation if the mask provided is valid. Otherwise, returns the
188 /// maskable operation itself.
189 Operation *maskOperation(OpBuilder &builder, Operation *maskableOp, Value mask,
190  Value passthru = Value());
191 
192 /// Creates a vector select operation that picks values from `newValue` or
193 /// `passthru` for each result vector lane based on `mask`. This utility is used
194 /// to propagate the pass-thru value for masked-out or expeculatively executed
195 /// lanes. VP intrinsics do not support pass-thru values and every mask-out lane
196 /// is set to poison. LLVM backends are usually able to match op + select
197 /// patterns and fold them into a native target instructions.
198 Value selectPassthru(OpBuilder &builder, Value mask, Value newValue,
199  Value passthru);
200 
201 } // namespace vector
202 } // namespace mlir
203 
204 #define GET_OP_CLASSES
205 #include "mlir/Dialect/Vector/IR/VectorDialect.h.inc"
206 #include "mlir/Dialect/Vector/IR/VectorOps.h.inc"
207 
208 #endif // MLIR_DIALECT_VECTOR_IR_VECTOROPS_H
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:50
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
This class helps build Operations.
Definition: Builders.h:210
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:432
bool isReductionIterator(Attribute attr)
Returns true if attr has "reduction" iterator type semantics.
Definition: VectorOps.h:144
void buildTerminatedBody(OpBuilder &builder, Location loc)
Default callback to build a region with a 'vector.yield' terminator with no arguments.
Definition: VectorOps.cpp:126
SmallVector< arith::ConstantIndexOp > getAsConstantIndexOps(ArrayRef< Value > values)
Returns the constant index ops in values.
void populateElementwiseToVectorOpsPatterns(RewritePatternSet &patterns)
Collect a set of patterns that fold elementwise op on vectors to the vector dialect.
AffineMap getTransferMinorIdentityMap(ShapedType shapedType, VectorType vectorType)
Build the default minor identity map suitable for a vector transfer.
Definition: VectorOps.cpp:154
bool checkSameValueRAW(TransferWriteOp defWrite, TransferReadOp read)
Return true if the transfer_write fully writes the data accessed by the transfer_read.
Operation * maskOperation(OpBuilder &builder, Operation *maskableOp, Value mask, Value passthru=Value())
Creates a vector.mask operation around a maskable operation.
VectorType inferTransferOpMaskType(VectorType vecType, AffineMap permMap)
Infers the mask type for a transfer op given its vector type and permutation map.
Definition: VectorOps.cpp:3988
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:220
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:284
BroadcastableToResult isBroadcastableTo(Type srcType, VectorType dstVectorType, std::pair< int, int > *mismatchingDims=nullptr)
Definition: VectorOps.cpp:2376
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:139
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:339
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:609
BroadcastableToResult
Return whether srcType can be broadcast to dstVectorType under the semantics of the vector....
Definition: VectorOps.h:65
IntegerType getVectorSubscriptType(Builder &builder)
Returns the integer type required for subscripts in the vector dialect.
Definition: VectorOps.cpp:428
SmallVector< int64_t > getAsIntegers(ArrayRef< OpFoldResult > foldResults)
Returns the integer numbers in foldResults.
Definition: VectorOps.cpp:327
Include the generated interface declarations.