MLIR  22.0.0git
MemOpInterfaces.cpp
Go to the documentation of this file.
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 
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 
18  auto memCastOp = cast<MemorySpaceCastOpInterface>(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>(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>(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>>>
60  ValueRange results) {
61  MemorySpaceCastOpInterface castOp =
62  MemorySpaceCastOpInterface::getIfPromotableCast(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"
IRValueT get() const
Return the current value being used by this operand.
Definition: UseDefLists.h:160
void set(IRValueT newValue)
Set the current value being used by this operand.
Definition: UseDefLists.h:163
This class represents an operand of an operation.
Definition: Value.h:257
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:267
unsigned getNumResults()
Return the number of results held by this operation.
Definition: Operation.h:404
TypeID getTypeID()
Return a unique identifier for the concrete type.
Definition: Types.h:101
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
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
FailureOr< std::optional< SmallVector< Value > > > bubbleDownInPlaceMemorySpaceCastImpl(OpOperand &operand, ValueRange results)
Tries to bubble-down inplace a MemorySpaceCastOpInterface operation referenced by operand.
LogicalResult verifyMemorySpaceCastOpInterface(Operation *op)
Attempt to verify the given memory space cast operation.
Include the generated interface declarations.