MLIR 24.0.0git
LLVMConversionOps.cpp
Go to the documentation of this file.
1//===- LLVMConversionOps.cpp - LLVM dialect conversion 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 `Conversion` of the LLVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "IR/LLVMOps.h"
16#include "mlir/IR/Builders.h"
20
21#include <optional>
22
23using namespace mlir;
24using namespace mlir::LLVM;
25
26//===----------------------------------------------------------------------===//
27// Verifier for extension ops
28//===----------------------------------------------------------------------===//
29
30/// Verifies that the given extension operation operates on consistent scalars
31/// or vectors, and that the target width is larger than the input width.
32template <class ExtOp>
33static LogicalResult verifyExtOp(ExtOp op) {
34 IntegerType inputType, outputType;
35 if (isCompatibleVectorType(op.getArg().getType())) {
36 if (!isCompatibleVectorType(op.getResult().getType()))
37 return op.emitError(
38 "input type is a vector but output type is an integer");
39 if (getVectorNumElements(op.getArg().getType()) !=
40 getVectorNumElements(op.getResult().getType()))
41 return op.emitError("input and output vectors are of incompatible shape");
42 // Because this is a CastOp, the element of vectors is guaranteed to be an
43 // integer.
44 inputType = cast<IntegerType>(
45 cast<VectorType>(op.getArg().getType()).getElementType());
46 outputType = cast<IntegerType>(
47 cast<VectorType>(op.getResult().getType()).getElementType());
48 } else {
49 // Because this is a CastOp and arg is not a vector, arg is guaranteed to be
50 // an integer.
51 inputType = cast<IntegerType>(op.getArg().getType());
52 outputType = dyn_cast<IntegerType>(op.getResult().getType());
53 if (!outputType)
54 return op.emitError(
55 "input type is an integer but output type is a vector");
56 }
57
58 if (outputType.getWidth() <= inputType.getWidth())
59 return op.emitError("integer width of the output type is smaller or "
60 "equal to the integer width of the input type");
61 return success();
62}
63
64//===----------------------------------------------------------------------===//
65// ZExtOp
66//===----------------------------------------------------------------------===//
67
68LogicalResult ZExtOp::verify() { return verifyExtOp<ZExtOp>(*this); }
69
70OpFoldResult LLVM::ZExtOp::fold(FoldAdaptor adaptor) {
71 auto arg = dyn_cast_or_null<IntegerAttr>(adaptor.getArg());
72 if (!arg)
73 return {};
74
75 size_t targetSize = cast<IntegerType>(getType()).getWidth();
76 return IntegerAttr::get(getType(), arg.getValue().zext(targetSize));
77}
78
79//===----------------------------------------------------------------------===//
80// SExtOp
81//===----------------------------------------------------------------------===//
82
83LogicalResult SExtOp::verify() { return verifyExtOp<SExtOp>(*this); }
84
85//===----------------------------------------------------------------------===//
86// Folder and verifier for LLVM::BitcastOp
87//===----------------------------------------------------------------------===//
88
89/// Folds a cast op that can be chained.
90template <typename T>
92 typename T::FoldAdaptor adaptor) {
93 // cast(x : T0, T0) -> x
94 if (castOp.getArg().getType() == castOp.getType())
95 return castOp.getArg();
96 if (auto prev = castOp.getArg().template getDefiningOp<T>()) {
97 // cast(cast(x : T0, T1), T0) -> x
98 if (prev.getArg().getType() == castOp.getType())
99 return prev.getArg();
100 // cast(cast(x : T0, T1), T2) -> cast(x: T0, T2)
101 castOp.getArgMutable().set(prev.getArg());
102 return Value{castOp};
103 }
104 return {};
105}
106
107OpFoldResult LLVM::BitcastOp::fold(FoldAdaptor adaptor) {
108 return foldChainableCast(*this, adaptor);
109}
110
111LogicalResult LLVM::BitcastOp::verify() {
112 Type srcElemType = extractVectorElementType(getArg().getType());
113 Type dstElemType = extractVectorElementType(getResult().getType());
114
115 // TODO: 'bitcast' requires result and operand type to be identical in size.
116 // Byte types may be cast from/to any type pointer constraints.
117 if (isa<LLVMByteType>(srcElemType) || isa<LLVMByteType>(dstElemType))
118 return success();
119
120 auto resultType = llvm::dyn_cast<LLVMPointerType>(dstElemType);
121 auto sourceType = llvm::dyn_cast<LLVMPointerType>(srcElemType);
122
123 // If one of the types is a pointer (or vector of pointers), then
124 // both source and result type have to be pointers.
125 if (static_cast<bool>(resultType) != static_cast<bool>(sourceType))
126 return emitOpError("can only cast pointers from and to pointers");
127
128 if (!resultType)
129 return success();
130
131 auto isVector = llvm::IsaPred<VectorType>;
132
133 // Due to bitcast requiring both operands to be of the same size, it is not
134 // possible for only one of the two to be a pointer of vectors.
135 if (isVector(getResult().getType()) && !isVector(getArg().getType()))
136 return emitOpError("cannot cast pointer to vector of pointers");
137
138 if (!isVector(getResult().getType()) && isVector(getArg().getType()))
139 return emitOpError("cannot cast vector of pointers to pointer");
140
141 // Bitcast cannot cast between pointers of different address spaces.
142 // 'llvm.addrspacecast' must be used for this purpose instead.
143 if (resultType.getAddressSpace() != sourceType.getAddressSpace())
144 return emitOpError("cannot cast pointers of different address spaces, "
145 "use 'llvm.addrspacecast' instead");
146
147 return success();
148}
149
150LogicalResult LLVM::PtrToAddrOp::verify() {
151 auto pointerType =
152 cast<LLVM::LLVMPointerType>(extractVectorElementType(getArg().getType()));
153 auto integerType = cast<IntegerType>(extractVectorElementType(getType()));
154
155 auto dataLayout = DataLayout::closest(*this);
156 std::optional<unsigned> width = dataLayout.getTypeIndexBitwidth(pointerType);
157 assert(width && "pointers always return an index bitwidth");
158 if (width != integerType.getWidth())
159 return emitOpError("bit-width of integer result type ")
160 << integerType << " must match the pointer bitwidth (" << *width
161 << ") specified in the datalayout";
162
163 return success();
164}
165
166//===----------------------------------------------------------------------===//
167// Folder for LLVM::AddrSpaceCastOp
168//===----------------------------------------------------------------------===//
169
170OpFoldResult LLVM::AddrSpaceCastOp::fold(FoldAdaptor adaptor) {
171 return foldChainableCast(*this, adaptor);
172}
173
174Value LLVM::AddrSpaceCastOp::getViewSource() { return getArg(); }
175
176bool LLVM::BitcastOp::canUsesBeRemoved(
177 const SmallPtrSetImpl<OpOperand *> &blockingUses,
178 SmallVectorImpl<OpOperand *> &newBlockingUses,
179 const DataLayout &dataLayout) {
180 return forwardToUsers(*this, newBlockingUses);
181}
182
183DeletionKind LLVM::BitcastOp::removeBlockingUses(
184 const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
186}
187
188bool LLVM::AddrSpaceCastOp::canUsesBeRemoved(
189 const SmallPtrSetImpl<OpOperand *> &blockingUses,
190 SmallVectorImpl<OpOperand *> &newBlockingUses,
191 const DataLayout &dataLayout) {
192 return forwardToUsers(*this, newBlockingUses);
193}
194
195DeletionKind LLVM::AddrSpaceCastOp::removeBlockingUses(
196 const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
198}
199
200#define GET_OP_CLASSES
201#include "mlir/Dialect/LLVMIR/LLVMConversionOps.cpp.inc"
return success()
static OpFoldResult foldChainableCast(T castOp, typename T::FoldAdaptor adaptor)
Folds a cast op that can be chained.
static LogicalResult verifyExtOp(ExtOp op)
Verifies that the given extension operation operates on consistent scalars or vectors,...
The main mechanism for performing data layout queries.
static DataLayout closest(Operation *op)
Returns the layout of the closest parent operation carrying layout info.
This class helps build Operations.
Definition Builders.h:210
This class represents a single result from folding an operation.
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 extractVectorElementType(Type type)
Returns the elemental type of any LLVM-compatible vector type or self.
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
bool forwardToUsers(Operation *op, SmallVectorImpl< OpOperand * > &newBlockingUses)
Conditions the deletion of the operation to the removal of all its uses.
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
DeletionKind
Returned by operation promotion logic requesting the deletion of an operation.
@ Delete
Delete the operation after promotion.