xref: /llvm-project/mlir/lib/Conversion/LLVMCommon/VectorPattern.cpp (revision e553ac4d8148291914526f4f66f09e362ce0a63f)
1684dfe8aSAlex Zinenko //===- VectorPattern.cpp - Vector conversion pattern to the LLVM dialect --===//
2684dfe8aSAlex Zinenko //
3684dfe8aSAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4684dfe8aSAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
5684dfe8aSAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6684dfe8aSAlex Zinenko //
7684dfe8aSAlex Zinenko //===----------------------------------------------------------------------===//
8684dfe8aSAlex Zinenko 
9684dfe8aSAlex Zinenko #include "mlir/Conversion/LLVMCommon/VectorPattern.h"
10684dfe8aSAlex Zinenko #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
11684dfe8aSAlex Zinenko 
12684dfe8aSAlex Zinenko using namespace mlir;
13684dfe8aSAlex Zinenko 
14684dfe8aSAlex Zinenko // For >1-D vector types, extracts the necessary information to iterate over all
15684dfe8aSAlex Zinenko // 1-D subvectors in the underlying llrepresentation of the n-D vector
16684dfe8aSAlex Zinenko // Iterates on the llvm array type until we hit a non-array type (which is
17684dfe8aSAlex Zinenko // asserted to be an llvm vector type).
18684dfe8aSAlex Zinenko LLVM::detail::NDVectorTypeInfo
extractNDVectorTypeInfo(VectorType vectorType,const LLVMTypeConverter & converter)19684dfe8aSAlex Zinenko LLVM::detail::extractNDVectorTypeInfo(VectorType vectorType,
20ce254598SMatthias Springer                                       const LLVMTypeConverter &converter) {
21684dfe8aSAlex Zinenko   assert(vectorType.getRank() > 1 && "expected >1D vector type");
22684dfe8aSAlex Zinenko   NDVectorTypeInfo info;
23684dfe8aSAlex Zinenko   info.llvmNDVectorTy = converter.convertType(vectorType);
24684dfe8aSAlex Zinenko   if (!info.llvmNDVectorTy || !LLVM::isCompatibleType(info.llvmNDVectorTy)) {
25684dfe8aSAlex Zinenko     info.llvmNDVectorTy = nullptr;
26684dfe8aSAlex Zinenko     return info;
27684dfe8aSAlex Zinenko   }
28684dfe8aSAlex Zinenko   info.arraySizes.reserve(vectorType.getRank() - 1);
29684dfe8aSAlex Zinenko   auto llvmTy = info.llvmNDVectorTy;
305550c821STres Popp   while (isa<LLVM::LLVMArrayType>(llvmTy)) {
31684dfe8aSAlex Zinenko     info.arraySizes.push_back(
325550c821STres Popp         cast<LLVM::LLVMArrayType>(llvmTy).getNumElements());
335550c821STres Popp     llvmTy = cast<LLVM::LLVMArrayType>(llvmTy).getElementType();
34684dfe8aSAlex Zinenko   }
35684dfe8aSAlex Zinenko   if (!LLVM::isCompatibleVectorType(llvmTy))
36684dfe8aSAlex Zinenko     return info;
37684dfe8aSAlex Zinenko   info.llvm1DVectorTy = llvmTy;
38684dfe8aSAlex Zinenko   return info;
39684dfe8aSAlex Zinenko }
40684dfe8aSAlex Zinenko 
41684dfe8aSAlex Zinenko // Express `linearIndex` in terms of coordinates of `basis`.
42684dfe8aSAlex Zinenko // Returns the empty vector when linearIndex is out of the range [0, P] where
43684dfe8aSAlex Zinenko // P is the product of all the basis coordinates.
44684dfe8aSAlex Zinenko //
45684dfe8aSAlex Zinenko // Prerequisites:
46684dfe8aSAlex Zinenko //   Basis is an array of nonnegative integers (signed type inherited from
47684dfe8aSAlex Zinenko //   vector shape type).
getCoordinates(ArrayRef<int64_t> basis,unsigned linearIndex)48684dfe8aSAlex Zinenko SmallVector<int64_t, 4> LLVM::detail::getCoordinates(ArrayRef<int64_t> basis,
49684dfe8aSAlex Zinenko                                                      unsigned linearIndex) {
50684dfe8aSAlex Zinenko   SmallVector<int64_t, 4> res;
51684dfe8aSAlex Zinenko   res.reserve(basis.size());
52684dfe8aSAlex Zinenko   for (unsigned basisElement : llvm::reverse(basis)) {
53684dfe8aSAlex Zinenko     res.push_back(linearIndex % basisElement);
54684dfe8aSAlex Zinenko     linearIndex = linearIndex / basisElement;
55684dfe8aSAlex Zinenko   }
56684dfe8aSAlex Zinenko   if (linearIndex > 0)
57684dfe8aSAlex Zinenko     return {};
58684dfe8aSAlex Zinenko   std::reverse(res.begin(), res.end());
59684dfe8aSAlex Zinenko   return res;
60684dfe8aSAlex Zinenko }
61684dfe8aSAlex Zinenko 
62684dfe8aSAlex Zinenko // Iterate of linear index, convert to coords space and insert splatted 1-D
63684dfe8aSAlex Zinenko // vector in each position.
nDVectorIterate(const LLVM::detail::NDVectorTypeInfo & info,OpBuilder & builder,function_ref<void (ArrayRef<int64_t>)> fun)64684dfe8aSAlex Zinenko void LLVM::detail::nDVectorIterate(const LLVM::detail::NDVectorTypeInfo &info,
65684dfe8aSAlex Zinenko                                    OpBuilder &builder,
665c5af910SJeff Niu                                    function_ref<void(ArrayRef<int64_t>)> fun) {
67684dfe8aSAlex Zinenko   unsigned ub = 1;
68684dfe8aSAlex Zinenko   for (auto s : info.arraySizes)
69684dfe8aSAlex Zinenko     ub *= s;
70684dfe8aSAlex Zinenko   for (unsigned linearIndex = 0; linearIndex < ub; ++linearIndex) {
71684dfe8aSAlex Zinenko     auto coords = getCoordinates(info.arraySizes, linearIndex);
72684dfe8aSAlex Zinenko     // Linear index is out of bounds, we are done.
73684dfe8aSAlex Zinenko     if (coords.empty())
74684dfe8aSAlex Zinenko       break;
75684dfe8aSAlex Zinenko     assert(coords.size() == info.arraySizes.size());
765c5af910SJeff Niu     fun(coords);
77684dfe8aSAlex Zinenko   }
78684dfe8aSAlex Zinenko }
79684dfe8aSAlex Zinenko 
handleMultidimensionalVectors(Operation * op,ValueRange operands,const LLVMTypeConverter & typeConverter,std::function<Value (Type,ValueRange)> createOperand,ConversionPatternRewriter & rewriter)80684dfe8aSAlex Zinenko LogicalResult LLVM::detail::handleMultidimensionalVectors(
81ce254598SMatthias Springer     Operation *op, ValueRange operands, const LLVMTypeConverter &typeConverter,
82684dfe8aSAlex Zinenko     std::function<Value(Type, ValueRange)> createOperand,
83684dfe8aSAlex Zinenko     ConversionPatternRewriter &rewriter) {
845550c821STres Popp   auto resultNDVectorType = cast<VectorType>(op->getResult(0).getType());
85684dfe8aSAlex Zinenko   auto resultTypeInfo =
86684dfe8aSAlex Zinenko       extractNDVectorTypeInfo(resultNDVectorType, typeConverter);
87684dfe8aSAlex Zinenko   auto result1DVectorTy = resultTypeInfo.llvm1DVectorTy;
88684dfe8aSAlex Zinenko   auto resultNDVectoryTy = resultTypeInfo.llvmNDVectorTy;
89684dfe8aSAlex Zinenko   auto loc = op->getLoc();
90684dfe8aSAlex Zinenko   Value desc = rewriter.create<LLVM::UndefOp>(loc, resultNDVectoryTy);
915c5af910SJeff Niu   nDVectorIterate(resultTypeInfo, rewriter, [&](ArrayRef<int64_t> position) {
92684dfe8aSAlex Zinenko     // For this unrolled `position` corresponding to the `linearIndex`^th
93684dfe8aSAlex Zinenko     // element, extract operand vectors
94684dfe8aSAlex Zinenko     SmallVector<Value, 4> extractedOperands;
95e4853be2SMehdi Amini     for (const auto &operand : llvm::enumerate(operands)) {
96684dfe8aSAlex Zinenko       extractedOperands.push_back(rewriter.create<LLVM::ExtractValueOp>(
975c5af910SJeff Niu           loc, operand.value(), position));
98684dfe8aSAlex Zinenko     }
99684dfe8aSAlex Zinenko     Value newVal = createOperand(result1DVectorTy, extractedOperands);
1005c5af910SJeff Niu     desc = rewriter.create<LLVM::InsertValueOp>(loc, desc, newVal, position);
101684dfe8aSAlex Zinenko   });
102684dfe8aSAlex Zinenko   rewriter.replaceOp(op, desc);
103684dfe8aSAlex Zinenko   return success();
104684dfe8aSAlex Zinenko }
105684dfe8aSAlex Zinenko 
vectorOneToOneRewrite(Operation * op,StringRef targetOp,ValueRange operands,ArrayRef<NamedAttribute> targetAttrs,const LLVMTypeConverter & typeConverter,ConversionPatternRewriter & rewriter,IntegerOverflowFlags overflowFlags)106*e553ac4dSJeff Niu LogicalResult LLVM::detail::vectorOneToOneRewrite(
107*e553ac4dSJeff Niu     Operation *op, StringRef targetOp, ValueRange operands,
108ce254598SMatthias Springer     ArrayRef<NamedAttribute> targetAttrs,
109*e553ac4dSJeff Niu     const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter,
110*e553ac4dSJeff Niu     IntegerOverflowFlags overflowFlags) {
111684dfe8aSAlex Zinenko   assert(!operands.empty());
112684dfe8aSAlex Zinenko 
113684dfe8aSAlex Zinenko   // Cannot convert ops if their operands are not of LLVM type.
114380a1b20SKazu Hirata   if (!llvm::all_of(operands.getTypes(), isCompatibleType))
115684dfe8aSAlex Zinenko     return failure();
116684dfe8aSAlex Zinenko 
117684dfe8aSAlex Zinenko   auto llvmNDVectorTy = operands[0].getType();
1185550c821STres Popp   if (!isa<LLVM::LLVMArrayType>(llvmNDVectorTy))
119b56e65d3SJeremy Furtek     return oneToOneRewrite(op, targetOp, operands, targetAttrs, typeConverter,
120*e553ac4dSJeff Niu                            rewriter, overflowFlags);
121684dfe8aSAlex Zinenko 
122*e553ac4dSJeff Niu   auto callback = [op, targetOp, targetAttrs, overflowFlags,
123*e553ac4dSJeff Niu                    &rewriter](Type llvm1DVectorTy, ValueRange operands) {
124*e553ac4dSJeff Niu     Operation *newOp =
125*e553ac4dSJeff Niu         rewriter.create(op->getLoc(), rewriter.getStringAttr(targetOp),
126*e553ac4dSJeff Niu                         operands, llvm1DVectorTy, targetAttrs);
127*e553ac4dSJeff Niu     LLVM::detail::setNativeProperties(newOp, overflowFlags);
128*e553ac4dSJeff Niu     return newOp->getResult(0);
129684dfe8aSAlex Zinenko   };
130684dfe8aSAlex Zinenko 
131684dfe8aSAlex Zinenko   return handleMultidimensionalVectors(op, operands, typeConverter, callback,
132684dfe8aSAlex Zinenko                                        rewriter);
133684dfe8aSAlex Zinenko }
134