| 1 | //===- MemOpInterfaces.cpp - Memory operation interfaces ---------*- C++-*-===// |
|---|---|
| 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 | #include "mlir/Interfaces/MemOpInterfaces.h" |
| 10 | #include "mlir/IR/Attributes.h" |
| 11 | #include "mlir/IR/Builders.h" |
| 12 | #include "mlir/IR/BuiltinTypes.h" |
| 13 | #include "mlir/IR/Value.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | LogicalResult mlir::detail::verifyMemorySpaceCastOpInterface(Operation *op) { |
| 18 | auto memCastOp = cast<MemorySpaceCastOpInterface>(Val: op); |
| 19 | |
| 20 | // Verify that the source and target pointers are valid |
| 21 | Value sourcePtr = memCastOp.getSourcePtr(); |
| 22 | Value targetPtr = memCastOp.getTargetPtr(); |
| 23 | |
| 24 | if (!sourcePtr || !targetPtr) { |
| 25 | return op->emitError() |
| 26 | << "memory space cast op must have valid source and target pointers"; |
| 27 | } |
| 28 | |
| 29 | if (sourcePtr.getType().getTypeID() != targetPtr.getType().getTypeID()) { |
| 30 | return op->emitError() |
| 31 | << "expected source and target types of the same kind"; |
| 32 | } |
| 33 | |
| 34 | // Verify the Types are of `PtrLikeTypeInterface` type. |
| 35 | auto sourceType = dyn_cast<PtrLikeTypeInterface>(Val: sourcePtr.getType()); |
| 36 | if (!sourceType) { |
| 37 | return op->emitError() |
| 38 | << "source type must implement `PtrLikeTypeInterface`, but got: " |
| 39 | << sourcePtr.getType(); |
| 40 | } |
| 41 | |
| 42 | auto targetType = dyn_cast<PtrLikeTypeInterface>(Val: targetPtr.getType()); |
| 43 | if (!targetType) { |
| 44 | return op->emitError() |
| 45 | << "target type must implement `PtrLikeTypeInterface`, but got: " |
| 46 | << targetPtr.getType(); |
| 47 | } |
| 48 | |
| 49 | // Verify that the operation has exactly one result |
| 50 | if (op->getNumResults() != 1) { |
| 51 | return op->emitError() |
| 52 | << "memory space cast op must have exactly one result"; |
| 53 | } |
| 54 | |
| 55 | return success(); |
| 56 | } |
| 57 | |
| 58 | FailureOr<std::optional<SmallVector<Value>>> |
| 59 | mlir::detail::bubbleDownInPlaceMemorySpaceCastImpl(OpOperand &operand, |
| 60 | ValueRange results) { |
| 61 | MemorySpaceCastOpInterface castOp = |
| 62 | MemorySpaceCastOpInterface::getIfPromotableCast(value: operand.get()); |
| 63 | |
| 64 | // Bail if the src is not valid. |
| 65 | if (!castOp) |
| 66 | return failure(); |
| 67 | |
| 68 | // Modify the op. |
| 69 | operand.set(castOp.getSourcePtr()); |
| 70 | return std::optional<SmallVector<Value>>(); |
| 71 | } |
| 72 | |
| 73 | #include "mlir/Interfaces/MemOpInterfaces.cpp.inc" |
| 74 |