1//===- LLVMVectorOps.cpp - LLVM dialect vector ops ------------------------===//
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 ops of op partition `Vector` of the LLVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/LLVMIR/LLVMVectorOps.h"
14
15#include "IR/LLVMOps.h"
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/DialectImplementation.h"
18#include "llvm/ADT/STLExtras.h"
19
20using namespace mlir;
21using namespace mlir::LLVM;
22
23//===----------------------------------------------------------------------===//
24// ShuffleVectorOp
25//===----------------------------------------------------------------------===//
26
27void ShuffleVectorOp::build(OpBuilder &builder, OperationState &state, Value v1,
28 Value v2, DenseI32ArrayAttr mask,
29 ArrayRef<NamedAttribute> attrs) {
30 auto containerType = v1.getType();
31 auto vType = LLVM::getVectorType(
32 elementType: cast<VectorType>(Val&: containerType).getElementType(), numElements: mask.size(),
33 isScalable: LLVM::isScalableVectorType(vectorType: containerType));
34 build(odsBuilder&: builder, odsState&: state, res: vType, v1, v2, mask);
35 state.addAttributes(newAttributes: attrs);
36}
37
38void ShuffleVectorOp::build(OpBuilder &builder, OperationState &state, Value v1,
39 Value v2, ArrayRef<int32_t> mask) {
40 build(builder, state, v1, v2, mask: builder.getDenseI32ArrayAttr(values: mask));
41}
42
43/// Build the result type of a shuffle vector operation.
44ParseResult mlir::LLVM::parseShuffleType(AsmParser &parser, Type v1Type,
45 Type &resType,
46 DenseI32ArrayAttr mask) {
47 if (!LLVM::isCompatibleVectorType(type: v1Type))
48 return parser.emitError(loc: parser.getCurrentLocation(),
49 message: "expected an LLVM compatible vector type");
50 resType =
51 LLVM::getVectorType(elementType: cast<VectorType>(Val&: v1Type).getElementType(),
52 numElements: mask.size(), isScalable: LLVM::isScalableVectorType(vectorType: v1Type));
53 return success();
54}
55
56/// Nothing to do when the result type is inferred.
57void mlir::LLVM::printShuffleType(AsmPrinter &printer, Operation *op,
58 Type v1Type, Type resType,
59 DenseI32ArrayAttr mask) {}
60
61LogicalResult ShuffleVectorOp::verify() {
62 if (LLVM::isScalableVectorType(vectorType: getV1().getType()) &&
63 llvm::any_of(Range: getMask(), P: [](int32_t v) { return v != 0; }))
64 return emitOpError(message: "expected a splat operation for scalable vectors");
65 return success();
66}
67
68// Folding for shufflevector op when v1 is single element 1D vector
69// and the mask is a single zero. OpFoldResult will be v1 in this case.
70OpFoldResult ShuffleVectorOp::fold(FoldAdaptor adaptor) {
71 // Check if operand 0 is a single element vector.
72 auto vecType = llvm::dyn_cast<VectorType>(Val: getV1().getType());
73 if (!vecType || vecType.getRank() != 1 || vecType.getNumElements() != 1)
74 return {};
75 // Check if the mask is a single zero.
76 // Note: The mask is guaranteed to be non-empty.
77 if (getMask().size() != 1 || getMask()[0] != 0)
78 return {};
79 return getV1();
80}
81
82#define GET_OP_CLASSES
83#include "mlir/Dialect/LLVMIR/LLVMVectorOps.cpp.inc"
84