1 //===- CastInterfaces.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Interfaces/CastInterfaces.h"
11 #include "mlir/IR/BuiltinDialect.h"
12 #include "mlir/IR/BuiltinOps.h"
16 //===----------------------------------------------------------------------===//
17 // Helper functions for CastOpInterface
18 //===----------------------------------------------------------------------===//
20 /// Attempt to fold the given cast operation.
22 impl::foldCastInterfaceOp(Operation
*op
, ArrayRef
<Attribute
> attrOperands
,
23 SmallVectorImpl
<OpFoldResult
> &foldResults
) {
24 OperandRange operands
= op
->getOperands();
27 ResultRange results
= op
->getResults();
29 // Check for the case where the input and output types match 1-1.
30 if (operands
.getTypes() == results
.getTypes()) {
31 foldResults
.append(operands
.begin(), operands
.end());
38 /// Attempt to verify the given cast operation.
39 LogicalResult
impl::verifyCastInterfaceOp(Operation
*op
) {
40 auto resultTypes
= op
->getResultTypes();
41 if (resultTypes
.empty())
42 return op
->emitOpError()
43 << "expected at least one result for cast operation";
45 auto operandTypes
= op
->getOperandTypes();
46 if (!cast
<CastOpInterface
>(op
).areCastCompatible(operandTypes
, resultTypes
)) {
47 InFlightDiagnostic diag
= op
->emitOpError("operand type");
48 if (operandTypes
.empty())
50 else if (llvm::size(operandTypes
) == 1)
51 diag
<< " " << *operandTypes
.begin();
53 diag
<< "s " << operandTypes
;
54 return diag
<< " and result type" << (resultTypes
.size() == 1 ? " " : "s ")
55 << resultTypes
<< " are cast incompatible";
61 //===----------------------------------------------------------------------===//
62 // External model for BuiltinDialect ops
63 //===----------------------------------------------------------------------===//
67 // This interface cannot be implemented directly on the op because the IR build
68 // unit cannot depend on the Interfaces build unit.
69 struct UnrealizedConversionCastOpInterface
70 : CastOpInterface::ExternalModel
<UnrealizedConversionCastOpInterface
,
71 UnrealizedConversionCastOp
> {
72 static bool areCastCompatible(TypeRange inputs
, TypeRange outputs
) {
73 // `UnrealizedConversionCastOp` is agnostic of the input/output types.
80 void mlir::builtin::registerCastOpInterfaceExternalModels(
81 DialectRegistry
®istry
) {
82 registry
.addExtension(+[](MLIRContext
*ctx
, BuiltinDialect
*dialect
) {
83 UnrealizedConversionCastOp::attachInterface
<
84 UnrealizedConversionCastOpInterface
>(*ctx
);
88 //===----------------------------------------------------------------------===//
89 // Table-generated class definitions
90 //===----------------------------------------------------------------------===//
92 #include "mlir/Interfaces/CastInterfaces.cpp.inc"