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
13#include "mlir/Dialect/LLVMIR/LLVMConversionOps.h"
14
15#include "IR/LLVMOps.h"
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/DialectImplementation.h"
18#include "mlir/Interfaces/DataLayoutInterfaces.h"
19#include "mlir/Interfaces/MemorySlotInterfaces.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>(op: *this); }
69
70OpFoldResult LLVM::ZExtOp::fold(FoldAdaptor adaptor) {
71 auto arg = dyn_cast_or_null<IntegerAttr>(Val: adaptor.getArg());
72 if (!arg)
73 return {};
74
75 size_t targetSize = cast<IntegerType>(Val: getType()).getWidth();
76 return IntegerAttr::get(type: getType(), value: arg.getValue().zext(width: targetSize));
77}
78
79//===----------------------------------------------------------------------===//
80// SExtOp
81//===----------------------------------------------------------------------===//
82
83LogicalResult SExtOp::verify() { return verifyExtOp<SExtOp>(op: *this); }
84
85//===----------------------------------------------------------------------===//
86// Folder and verifier for LLVM::BitcastOp
87//===----------------------------------------------------------------------===//
88
89/// Folds a cast op that can be chained.
90template <typename T>
91static OpFoldResult foldChainableCast(T castOp,
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(castOp: *this, adaptor);
109}
110
111LogicalResult LLVM::BitcastOp::verify() {
112 Type srcElemType = extractVectorElementType(type: getArg().getType());
113 Type dstElemType = extractVectorElementType(type: 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>(Val: srcElemType) || isa<LLVMByteType>(Val: dstElemType))
118 return success();
119
120 auto resultType = llvm::dyn_cast<LLVMPointerType>(Val&: dstElemType);
121 auto sourceType = llvm::dyn_cast<LLVMPointerType>(Val&: 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(message: "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(message: "cannot cast pointer to vector of pointers");
137
138 if (!isVector(getResult().getType()) && isVector(getArg().getType()))
139 return emitOpError(message: "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(message: "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>(Val: extractVectorElementType(type: getArg().getType()));
153 auto integerType = cast<IntegerType>(Val: extractVectorElementType(type: getType()));
154
155 auto dataLayout = DataLayout::closest(op: *this);
156 std::optional<unsigned> width = dataLayout.getTypeIndexBitwidth(t: pointerType);
157 assert(width && "pointers always return an index bitwidth");
158 if (width != integerType.getWidth())
159 return emitOpError(message: "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(castOp: *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(op: *this, newBlockingUses);
181}
182
183DeletionKind LLVM::BitcastOp::removeBlockingUses(
184 const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
185 return DeletionKind::Delete;
186}
187
188bool LLVM::AddrSpaceCastOp::canUsesBeRemoved(
189 const SmallPtrSetImpl<OpOperand *> &blockingUses,
190 SmallVectorImpl<OpOperand *> &newBlockingUses,
191 const DataLayout &dataLayout) {
192 return forwardToUsers(op: *this, newBlockingUses);
193}
194
195DeletionKind LLVM::AddrSpaceCastOp::removeBlockingUses(
196 const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
197 return DeletionKind::Delete;
198}
199
200#define GET_OP_CLASSES
201#include "mlir/Dialect/LLVMIR/LLVMConversionOps.cpp.inc"
202