aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Interfaces/CastInterfaces.cpp
blob: 05c872daf5dab28fbcfc3921ba5c74948d7ac15d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===- CastInterfaces.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir/Interfaces/CastInterfaces.h"

#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/BuiltinOps.h"

using namespace mlir;

//===----------------------------------------------------------------------===//
// Helper functions for CastOpInterface
//===----------------------------------------------------------------------===//

/// Attempt to fold the given cast operation.
LogicalResult
impl::foldCastInterfaceOp(Operation *op, ArrayRef<Attribute> attrOperands,
                          SmallVectorImpl<OpFoldResult> &foldResults) {
  OperandRange operands = op->getOperands();
  if (operands.empty())
    return failure();
  ResultRange results = op->getResults();

  // Check for the case where the input and output types match 1-1.
  if (operands.getTypes() == results.getTypes()) {
    foldResults.append(operands.begin(), operands.end());
    return success();
  }

  return failure();
}

/// Attempt to verify the given cast operation.
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();
}

//===----------------------------------------------------------------------===//
// External model for BuiltinDialect ops
//===----------------------------------------------------------------------===//

namespace mlir {
namespace {
// This interface cannot be implemented directly on the op because the IR build
// unit cannot depend on the Interfaces build unit.
struct UnrealizedConversionCastOpInterface
    : CastOpInterface::ExternalModel<UnrealizedConversionCastOpInterface,
                                     UnrealizedConversionCastOp> {
  static bool areCastCompatible(TypeRange inputs, TypeRange outputs) {
    // `UnrealizedConversionCastOp` is agnostic of the input/output types.
    return true;
  }
};
} // namespace
} // namespace mlir

void mlir::builtin::registerCastOpInterfaceExternalModels(
    DialectRegistry &registry) {
  registry.addExtension(+[](MLIRContext *ctx, BuiltinDialect *dialect) {
    UnrealizedConversionCastOp::attachInterface<
        UnrealizedConversionCastOpInterface>(*ctx);
  });
}

//===----------------------------------------------------------------------===//
// Table-generated class definitions
//===----------------------------------------------------------------------===//

#include "mlir/Interfaces/CastInterfaces.cpp.inc"