#include "mlir/Interfaces/CastInterfaces.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/BuiltinOps.h"
using namespace mlir;
LogicalResult
impl::foldCastInterfaceOp(Operation *op, ArrayRef<Attribute> attrOperands,
SmallVectorImpl<OpFoldResult> &foldResults) {
OperandRange operands = op->getOperands();
if (operands.empty())
return failure();
ResultRange results = op->getResults();
if (operands.getTypes() == results.getTypes()) {
foldResults.append(operands.begin(), operands.end());
return success();
}
return failure();
}
LogicalResult impl::verifyCastInterfaceOp(Operation *op) {
auto resultTypes = op->getResultTypes();
if (resultTypes.empty())
return op->emitOpError()
<< "expected at least one result for cast operation";
auto operandTypes = op->getOperandTypes();
if (!cast<CastOpInterface>(op).areCastCompatible(operandTypes, resultTypes)) {
InFlightDiagnostic diag = op->emitOpError("operand type");
if (operandTypes.empty())
diag << "s []";
else if (llvm::size(operandTypes) == 1)
diag << " " << *operandTypes.begin();
else
diag << "s " << operandTypes;
return diag << " and result type" << (resultTypes.size() == 1 ? " " : "s ")
<< resultTypes << " are cast incompatible";
}
return success();
}
namespace mlir {
namespace {
struct UnrealizedConversionCastOpInterface
: CastOpInterface::ExternalModel<UnrealizedConversionCastOpInterface,
UnrealizedConversionCastOp> {
static bool areCastCompatible(TypeRange inputs, TypeRange outputs) {
return true;
}
};
}
}
void mlir::builtin::registerCastOpInterfaceExternalModels(
DialectRegistry ®istry) {
registry.addExtension(+[](MLIRContext *ctx, BuiltinDialect *dialect) {
UnrealizedConversionCastOp::attachInterface<
UnrealizedConversionCastOpInterface>(*ctx);
});
}
#include "mlir/Interfaces/CastInterfaces.cpp.inc"