MLIR 24.0.0git
LLVMVectorOps.cpp
Go to the documentation of this file.
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
14
15#include "IR/LLVMOps.h"
16#include "mlir/IR/Builders.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,
30 auto containerType = v1.getType();
31 auto vType = LLVM::getVectorType(
32 cast<VectorType>(containerType).getElementType(), mask.size(),
33 LLVM::isScalableVectorType(containerType));
34 build(builder, state, vType, v1, v2, mask);
35 state.addAttributes(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, builder.getDenseI32ArrayAttr(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) {
48 return parser.emitError(parser.getCurrentLocation(),
49 "expected an LLVM compatible vector type");
50 resType =
51 LLVM::getVectorType(cast<VectorType>(v1Type).getElementType(),
52 mask.size(), LLVM::isScalableVectorType(v1Type));
53 return success();
54}
55
56/// Nothing to do when the result type is inferred.
58 Type v1Type, Type resType,
59 DenseI32ArrayAttr mask) {}
60
61LogicalResult ShuffleVectorOp::verify() {
62 if (LLVM::isScalableVectorType(getV1().getType()) &&
63 llvm::any_of(getMask(), [](int32_t v) { return v != 0; }))
64 return emitOpError("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>(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"
return success()
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
Definition SPIRVOps.cpp:230
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
This base class exposes generic asm printer hooks, usable across the various derived printers.
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition Builders.cpp:171
This class helps build Operations.
Definition Builders.h:210
This class represents a single result from folding an operation.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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
Type getType() const
Return the type of this value.
Definition Value.h:105
Type getVectorType(Type elementType, unsigned numElements, bool isScalable=false)
Creates an LLVM dialect-compatible vector type with the given element type and length.
bool isScalableVectorType(Type vectorType)
Returns whether a vector type is scalable or not.
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
mlir::ParseResult parseShuffleType(mlir::AsmParser &parser, mlir::Type v1Type, mlir::Type &resType, mlir::DenseI32ArrayAttr mask)
Build the result type of a shuffle vector operation.
void printShuffleType(mlir::AsmPrinter &printer, mlir::Operation *op, mlir::Type v1Type, mlir::Type resType, mlir::DenseI32ArrayAttr mask)
Nothing to do when the result type is inferred.
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
This represents an operation in an abstracted form, suitable for use with the builder APIs.
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.