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