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
9#include "mlir/Conversion/LLVMCommon/VectorPattern.h"
10#include "mlir/Dialect/LLVMIR/LLVMAggregateOps.h"
11#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
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).
19LLVM::detail::NDVectorTypeInfo
20LLVM::detail::extractNDVectorTypeInfo(VectorType vectorType,
21 const LLVMTypeConverter &converter) {
22 assert(vectorType.getRank() > 1 && "expected >1D vector type");
23 NDVectorTypeInfo info;
24 info.llvmNDVectorTy = converter.convertType(t: vectorType);
25 if (!info.llvmNDVectorTy || !LLVM::isCompatibleType(type: info.llvmNDVectorTy)) {
26 info.llvmNDVectorTy = nullptr;
27 return info;
28 }
29 info.arraySizes.reserve(N: vectorType.getRank() - 1);
30 auto llvmTy = info.llvmNDVectorTy;
31 while (isa<LLVM::LLVMArrayType>(Val: llvmTy)) {
32 info.arraySizes.push_back(
33 Elt: cast<LLVM::LLVMArrayType>(Val&: llvmTy).getNumElements());
34 llvmTy = cast<LLVM::LLVMArrayType>(Val&: llvmTy).getElementType();
35 }
36 if (!LLVM::isCompatibleVectorType(type: llvmTy))
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).
49SmallVector<int64_t, 4> LLVM::detail::getCoordinates(ArrayRef<int64_t> basis,
50 unsigned linearIndex) {
51 SmallVector<int64_t, 4> res;
52 res.reserve(N: basis.size());
53 for (unsigned basisElement : llvm::reverse(C&: basis)) {
54 res.push_back(Elt: linearIndex % basisElement);
55 linearIndex = linearIndex / basisElement;
56 }
57 if (linearIndex > 0)
58 return {};
59 std::reverse(first: res.begin(), last: 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.
65void LLVM::detail::nDVectorIterate(const LLVM::detail::NDVectorTypeInfo &info,
66 OpBuilder &builder,
67 function_ref<void(ArrayRef<int64_t>)> fun) {
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(basis: 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
81LogicalResult LLVM::detail::handleMultidimensionalVectors(
82 Operation *op, ValueRange operands, const LLVMTypeConverter &typeConverter,
83 std::function<Value(Type, ValueRange)> createOperand,
84 ConversionPatternRewriter &rewriter) {
85 auto resultNDVectorType = cast<VectorType>(Val: op->getResult(idx: 0).getType());
86 auto resultTypeInfo =
87 extractNDVectorTypeInfo(vectorType: resultNDVectorType, converter: typeConverter);
88 auto result1DVectorTy = resultTypeInfo.llvm1DVectorTy;
89 auto resultNDVectoryTy = resultTypeInfo.llvmNDVectorTy;
90 auto loc = op->getLoc();
91 Value desc = LLVM::PoisonOp::create(builder&: rewriter, location: loc, res: resultNDVectoryTy);
92 nDVectorIterate(info: resultTypeInfo, builder&: rewriter, fun: [&](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(First&: operands)) {
97 extractedOperands.push_back(Elt: LLVM::ExtractValueOp::create(
98 builder&: rewriter, location: loc, container: operand.value(), position));
99 }
100 Value newVal = createOperand(result1DVectorTy, extractedOperands);
101 desc = LLVM::InsertValueOp::create(builder&: rewriter, location: loc, container: desc, value: newVal, position);
102 });
103 rewriter.replaceOp(op, newValues: desc);
104 return success();
105}
106
107LogicalResult LLVM::detail::vectorOneToOneRewrite(
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(Range: operands.getTypes(), P: isCompatibleType))
116 return failure();
117
118 auto llvmNDVectorTy = operands[0].getType();
119 if (!isa<LLVM::LLVMArrayType>(Val: 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(bytes: targetOp),
125 operands, llvm1DVectorTy, targetAttrs);
126 state.propertiesAttr = propertiesAttr;
127 Operation *newOp = rewriter.create(state);
128 return newOp->getResult(idx: 0);
129 };
130
131 return handleMultidimensionalVectors(op, operands, typeConverter, createOperand: callback,
132 rewriter);
133}
134