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