MLIR  17.0.0git
Interfaces.cpp
Go to the documentation of this file.
1 //===- Interfaces.cpp - C Interface for MLIR Interfaces -------------------===//
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-c/Interfaces.h"
10 
11 #include "mlir/CAPI/IR.h"
12 #include "mlir/CAPI/Support.h"
13 #include "mlir/CAPI/Wrap.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include <optional>
17 
18 using namespace mlir;
19 
20 bool mlirOperationImplementsInterface(MlirOperation operation,
21  MlirTypeID interfaceTypeID) {
22  std::optional<RegisteredOperationName> info =
23  unwrap(operation)->getRegisteredInfo();
24  return info && info->hasInterface(unwrap(interfaceTypeID));
25 }
26 
28  MlirContext context,
29  MlirTypeID interfaceTypeID) {
30  std::optional<RegisteredOperationName> info = RegisteredOperationName::lookup(
31  StringRef(operationName.data, operationName.length), unwrap(context));
32  return info && info->hasInterface(unwrap(interfaceTypeID));
33 }
34 
36  return wrap(InferTypeOpInterface::getInterfaceID());
37 }
38 
40  MlirStringRef opName, MlirContext context, MlirLocation location,
41  intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
42  intptr_t nRegions, MlirRegion *regions, MlirTypesCallback callback,
43  void *userData) {
44  StringRef name(opName.data, opName.length);
45  std::optional<RegisteredOperationName> info =
47  if (!info)
48  return mlirLogicalResultFailure();
49 
50  std::optional<Location> maybeLocation;
51  if (!mlirLocationIsNull(location))
52  maybeLocation = unwrap(location);
53  SmallVector<Value> unwrappedOperands;
54  (void)unwrapList(nOperands, operands, unwrappedOperands);
55  DictionaryAttr attributeDict;
56  if (!mlirAttributeIsNull(attributes))
57  attributeDict = unwrap(attributes).cast<DictionaryAttr>();
58 
59  // Create a vector of unique pointers to regions and make sure they are not
60  // deleted when exiting the scope. This is a hack caused by C++ API expecting
61  // an list of unique pointers to regions (without ownership transfer
62  // semantics) and C API making ownership transfer explicit.
63  SmallVector<std::unique_ptr<Region>> unwrappedRegions;
64  unwrappedRegions.reserve(nRegions);
65  for (intptr_t i = 0; i < nRegions; ++i)
66  unwrappedRegions.emplace_back(unwrap(*(regions + i)));
67  auto cleaner = llvm::make_scope_exit([&]() {
68  for (auto &region : unwrappedRegions)
69  region.release();
70  });
71 
72  SmallVector<Type> inferredTypes;
73  if (failed(info->getInterface<InferTypeOpInterface>()->inferReturnTypes(
74  unwrap(context), maybeLocation, unwrappedOperands, attributeDict,
75  unwrappedRegions, inferredTypes)))
76  return mlirLogicalResultFailure();
77 
78  SmallVector<MlirType> wrappedInferredTypes;
79  wrappedInferredTypes.reserve(inferredTypes.size());
80  for (Type t : inferredTypes)
81  wrappedInferredTypes.push_back(wrap(t));
82  callback(wrappedInferredTypes.size(), wrappedInferredTypes.data(), userData);
83  return mlirLogicalResultSuccess();
84 }
MlirLogicalResult mlirInferTypeOpInterfaceInferReturnTypes(MlirStringRef opName, MlirContext context, MlirLocation location, intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, intptr_t nRegions, MlirRegion *regions, MlirTypesCallback callback, void *userData)
Infers the return types of the operation identified by its canonical given the arguments that will be...
Definition: Interfaces.cpp:39
bool mlirOperationImplementsInterface(MlirOperation operation, MlirTypeID interfaceTypeID)
Returns true if the given operation implements an interface identified by its TypeID.
Definition: Interfaces.cpp:20
bool mlirOperationImplementsInterfaceStatic(MlirStringRef operationName, MlirContext context, MlirTypeID interfaceTypeID)
Returns true if the operation identified by its canonical string name implements the interface identi...
Definition: Interfaces.cpp:27
MlirTypeID mlirInferTypeOpInterfaceTypeID()
Returns the interface TypeID of the InferTypeOpInterface.
Definition: Interfaces.cpp:35
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Definition: Wrap.h:40
static std::optional< RegisteredOperationName > lookup(StringRef name, MLIRContext *ctx)
Lookup the registered operation information for the given operation.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
static bool mlirAttributeIsNull(MlirAttribute attr)
Checks whether an attribute is null.
Definition: IR.h:824
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
Definition: IR.h:263
void(* MlirTypesCallback)(intptr_t, MlirType *, void *)
These callbacks are used to return multiple types from functions while transferring ownership to the ...
Definition: Interfaces.h:51
static MlirLogicalResult mlirLogicalResultFailure(void)
Creates a logical result representing a failure.
Definition: Support.h:136
static MlirLogicalResult mlirLogicalResultSuccess(void)
Creates a logical result representing a success.
Definition: Support.h:130
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
Include the generated interface declarations.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
A logical result value, essentially a boolean with named states.
Definition: Support.h:114
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:71
const char * data
Pointer to the first symbol.
Definition: Support.h:72
size_t length
Length of the fragment.
Definition: Support.h:73