MLIR  16.0.0git
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 for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "mlir-c/Interfaces.h"
11 #include "mlir/CAPI/IR.h"
12 #include "mlir/CAPI/Support.h"
13 #include "mlir/CAPI/Wrap.h"
15 #include "llvm/ADT/ScopeExit.h"
17 using namespace mlir;
19 bool mlirOperationImplementsInterface(MlirOperation operation,
20  MlirTypeID interfaceTypeID) {
22  unwrap(operation)->getRegisteredInfo();
23  return info && info->hasInterface(unwrap(interfaceTypeID));
24 }
27  MlirContext context,
28  MlirTypeID interfaceTypeID) {
30  StringRef(, operationName.length), unwrap(context));
31  return info && info->hasInterface(unwrap(interfaceTypeID));
32 }
35  return wrap(InferTypeOpInterface::getInterfaceID());
36 }
39  MlirStringRef opName, MlirContext context, MlirLocation location,
40  intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
41  intptr_t nRegions, MlirRegion *regions, MlirTypesCallback callback,
42  void *userData) {
43  StringRef name(, opName.length);
46  if (!info)
47  return mlirLogicalResultFailure();
49  llvm::Optional<Location> maybeLocation;
50  if (!mlirLocationIsNull(location))
51  maybeLocation = unwrap(location);
52  SmallVector<Value> unwrappedOperands;
53  (void)unwrapList(nOperands, operands, unwrappedOperands);
54  DictionaryAttr attributeDict;
55  if (!mlirAttributeIsNull(attributes))
56  attributeDict = unwrap(attributes).cast<DictionaryAttr>();
58  // Create a vector of unique pointers to regions and make sure they are not
59  // deleted when exiting the scope. This is a hack caused by C++ API expecting
60  // an list of unique pointers to regions (without ownership transfer
61  // semantics) and C API making ownership transfer explicit.
62  SmallVector<std::unique_ptr<Region>> unwrappedRegions;
63  unwrappedRegions.reserve(nRegions);
64  for (intptr_t i = 0; i < nRegions; ++i)
65  unwrappedRegions.emplace_back(unwrap(*(regions + i)));
66  auto cleaner = llvm::make_scope_exit([&]() {
67  for (auto &region : unwrappedRegions)
68  region.release();
69  });
71  SmallVector<Type> inferredTypes;
72  if (failed(info->getInterface<InferTypeOpInterface>()->inferReturnTypes(
73  unwrap(context), maybeLocation, unwrappedOperands, attributeDict,
74  unwrappedRegions, inferredTypes)))
75  return mlirLogicalResultFailure();
77  SmallVector<MlirType> wrappedInferredTypes;
78  wrappedInferredTypes.reserve(inferredTypes.size());
79  for (Type t : inferredTypes)
80  wrappedInferredTypes.push_back(wrap(t));
81  callback(wrappedInferredTypes.size(),, userData);
82  return mlirLogicalResultSuccess();
83 }
Include the generated interface declarations.
const char * data
Pointer to the first symbol.
Definition: Support.h:72
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:26
static MlirLogicalResult mlirLogicalResultSuccess()
Creates a logical result representing a success.
Definition: Support.h:130
static bool mlirAttributeIsNull(MlirAttribute attr)
Checks whether an attribute is null.
Definition: IR.h:776
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
Definition: LogicalResult.h:72
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:38
static MlirLogicalResult mlirLogicalResultFailure()
Creates a logical result representing a failure.
Definition: Support.h:136
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Definition: Wrap.h:40
bool mlirOperationImplementsInterface(MlirOperation operation, MlirTypeID interfaceTypeID)
Returns true if the given operation implements an interface identified by its TypeID.
Definition: Interfaces.cpp:19
A logical result value, essentially a boolean with named states.
Definition: Support.h:114
size_t length
Length of the fragment.
Definition: Support.h:73
static 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
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:71
MlirTypeID mlirInferTypeOpInterfaceTypeID()
Returns the interface TypeID of the InferTypeOpInterface.
Definition: Interfaces.cpp:34
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
Definition: IR.h:253
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