MLIR 24.0.0git
VectorPattern.cpp
Go to the documentation of this file.
1//===- VectorPattern.cpp - Vector conversion pattern to the LLVM dialect --===//
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
12
13using namespace mlir;
14
15// For >1-D vector types, extracts the necessary information to iterate over all
16// 1-D subvectors in the underlying llrepresentation of the n-D vector
17// Iterates on the llvm array type until we hit a non-array type (which is
18// asserted to be an llvm vector type).
21 const LLVMTypeConverter &converter) {
22 assert(vectorType.getRank() > 1 && "expected >1D vector type");
24 info.llvmNDVectorTy = converter.convertType(vectorType);
26 info.llvmNDVectorTy = nullptr;
27 return info;
28 }
29 info.arraySizes.reserve(vectorType.getRank() - 1);
30 auto llvmTy = info.llvmNDVectorTy;
31 while (isa<LLVM::LLVMArrayType>(llvmTy)) {
32 info.arraySizes.push_back(
33 cast<LLVM::LLVMArrayType>(llvmTy).getNumElements());
34 llvmTy = cast<LLVM::LLVMArrayType>(llvmTy).getElementType();
35 }
37 return info;
38 info.llvm1DVectorTy = llvmTy;
39 return info;
40}
41
42// Express `linearIndex` in terms of coordinates of `basis`.
43// Returns the empty vector when linearIndex is out of the range [0, P] where
44// P is the product of all the basis coordinates.
45//
46// Prerequisites:
47// Basis is an array of nonnegative integers (signed type inherited from
48// vector shape type).
50 unsigned linearIndex) {
52 res.reserve(basis.size());
53 for (unsigned basisElement : llvm::reverse(basis)) {
54 res.push_back(linearIndex % basisElement);
55 linearIndex = linearIndex / basisElement;
56 }
57 if (linearIndex > 0)
58 return {};
59 std::reverse(res.begin(), res.end());
60 return res;
61}
62
63// Iterate of linear index, convert to coords space and insert splatted 1-D
64// vector in each position.
66 OpBuilder &builder,
68 unsigned ub = 1;
69 for (auto s : info.arraySizes)
70 ub *= s;
71 for (unsigned linearIndex = 0; linearIndex < ub; ++linearIndex) {
72 auto coords = getCoordinates(info.arraySizes, linearIndex);
73 // Linear index is out of bounds, we are done.
74 if (coords.empty())
75 break;
76 assert(coords.size() == info.arraySizes.size());
77 fun(coords);
78 }
79}
80
82 Operation *op, ValueRange operands, const LLVMTypeConverter &typeConverter,
83 std::function<Value(Type, ValueRange)> createOperand,
84 ConversionPatternRewriter &rewriter) {
85 auto resultNDVectorType = cast<VectorType>(op->getResult(0).getType());
86 auto resultTypeInfo =
87 extractNDVectorTypeInfo(resultNDVectorType, typeConverter);
88 auto result1DVectorTy = resultTypeInfo.llvm1DVectorTy;
89 auto resultNDVectoryTy = resultTypeInfo.llvmNDVectorTy;
90 auto loc = op->getLoc();
91 Value desc = LLVM::PoisonOp::create(rewriter, loc, resultNDVectoryTy);
92 nDVectorIterate(resultTypeInfo, rewriter, [&](ArrayRef<int64_t> position) {
93 // For this unrolled `position` corresponding to the `linearIndex`^th
94 // element, extract operand vectors
95 SmallVector<Value, 4> extractedOperands;
96 for (const auto &operand : llvm::enumerate(operands)) {
97 extractedOperands.push_back(LLVM::ExtractValueOp::create(
98 rewriter, loc, operand.value(), position));
99 }
100 Value newVal = createOperand(result1DVectorTy, extractedOperands);
101 desc = LLVM::InsertValueOp::create(rewriter, loc, desc, newVal, position);
102 });
103 rewriter.replaceOp(op, desc);
104 return success();
105}
106
108 Operation *op, StringRef targetOp, ValueRange operands,
109 ArrayRef<NamedAttribute> targetAttrs, Attribute propertiesAttr,
110 const LLVMTypeConverter &typeConverter,
111 ConversionPatternRewriter &rewriter) {
112 assert(!operands.empty());
113
114 // Cannot convert ops if their operands are not of LLVM type.
115 if (!llvm::all_of(operands.getTypes(), isCompatibleType))
116 return failure();
117
118 auto llvmNDVectorTy = operands[0].getType();
119 if (!isa<LLVM::LLVMArrayType>(llvmNDVectorTy))
120 return oneToOneRewrite(op, targetOp, operands, targetAttrs, propertiesAttr,
121 typeConverter, rewriter);
122 auto callback = [op, targetOp, targetAttrs, propertiesAttr,
123 &rewriter](Type llvm1DVectorTy, ValueRange operands) {
124 OperationState state(op->getLoc(), rewriter.getStringAttr(targetOp),
125 operands, llvm1DVectorTy, targetAttrs);
126 state.propertiesAttr = propertiesAttr;
127 Operation *newOp = rewriter.create(state);
128 return newOp->getResult(0);
129 };
130
131 return handleMultidimensionalVectors(op, operands, typeConverter, callback,
132 rewriter);
133}
return success()
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
static Value handleMultidimensionalVectors(ImplicitLocOpBuilder &builder, ValueRange operands, int64_t vectorWidth, llvm::function_ref< Value(ValueRange)> compute)
Attributes are known-constant values of operations.
Definition Attributes.h:25
Conversion from types to the LLVM IR dialect.
This class helps build Operations.
Definition Builders.h:210
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:434
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:392
type_range getType() const
type_range getTypes() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
LogicalResult vectorOneToOneRewrite(Operation *op, StringRef targetOp, ValueRange operands, ArrayRef< NamedAttribute > targetAttrs, Attribute propertiesAttr, const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter)
LogicalResult handleMultidimensionalVectors(Operation *op, ValueRange operands, const LLVMTypeConverter &typeConverter, std::function< Value(Type, ValueRange)> createOperand, ConversionPatternRewriter &rewriter)
void nDVectorIterate(const NDVectorTypeInfo &info, OpBuilder &builder, function_ref< void(ArrayRef< int64_t >)> fun)
LogicalResult oneToOneRewrite(Operation *op, StringRef targetOp, ValueRange operands, ArrayRef< NamedAttribute > targetAttrs, Attribute propertiesAttr, const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter)
Replaces the given operation "op" with a new operation of type "targetOp" and given operands.
Definition Pattern.cpp:326
NDVectorTypeInfo extractNDVectorTypeInfo(VectorType vectorType, const LLVMTypeConverter &converter)
SmallVector< int64_t, 4 > getCoordinates(ArrayRef< int64_t > basis, unsigned linearIndex)
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
SmallVector< int64_t, 4 > arraySizes
This represents an operation in an abstracted form, suitable for use with the builder APIs.
Attribute propertiesAttr
This Attribute is used to opaquely construct the properties of the operation.