| 1 | //===- ExtensibleDialect - C API for MLIR Extensible Dialect --------------===// |
| 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/ExtensibleDialect.h" |
| 10 | #include "mlir/CAPI/IR.h" |
| 11 | #include "mlir/CAPI/Support.h" |
| 12 | #include "mlir/IR/ExtensibleDialect.h" |
| 13 | #include "mlir/IR/OperationSupport.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | DEFINE_C_API_PTR_METHODS(MlirDynamicOpTrait, DynamicOpTrait) |
| 18 | DEFINE_C_API_PTR_METHODS(MlirDynamicTypeDefinition, DynamicTypeDefinition) |
| 19 | DEFINE_C_API_PTR_METHODS(MlirDynamicAttrDefinition, DynamicAttrDefinition) |
| 20 | |
| 21 | bool mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait, |
| 22 | MlirStringRef opName, MlirContext context) { |
| 23 | std::optional<RegisteredOperationName> opNameFound = |
| 24 | RegisteredOperationName::lookup(name: unwrap(ref: opName), ctx: unwrap(c: context)); |
| 25 | assert(opNameFound && "operation name must be registered in the context" ); |
| 26 | |
| 27 | // The original getImpl() is protected, so we create a small helper struct |
| 28 | // here. |
| 29 | struct RegisteredOperationNameWithImpl : RegisteredOperationName { |
| 30 | Impl *getImpl() { return RegisteredOperationName::getImpl(); } |
| 31 | }; |
| 32 | OperationName::Impl *impl = |
| 33 | static_cast<RegisteredOperationNameWithImpl &>(*opNameFound).getImpl(); |
| 34 | |
| 35 | std::unique_ptr<DynamicOpTrait> trait(unwrap(c: dynamicOpTrait)); |
| 36 | // TODO: we should enable llvm-style RTTI for `OperationName::Impl` and check |
| 37 | // whether the `impl` is a `DynamicOpDefinition` here. |
| 38 | return static_cast<DynamicOpDefinition *>(impl)->addTrait(trait: std::move(trait)); |
| 39 | } |
| 40 | |
| 41 | MlirDynamicOpTrait mlirDynamicOpTraitIsTerminatorCreate() { |
| 42 | return wrap(cpp: new DynamicOpTraits::IsTerminator()); |
| 43 | } |
| 44 | |
| 45 | MlirTypeID mlirDynamicOpTraitIsTerminatorGetTypeID() { |
| 46 | return wrap(cpp: DynamicOpTraits::IsTerminator::getStaticTypeID()); |
| 47 | } |
| 48 | |
| 49 | MlirDynamicOpTrait mlirDynamicOpTraitNoTerminatorCreate() { |
| 50 | return wrap(cpp: new DynamicOpTraits::NoTerminator()); |
| 51 | } |
| 52 | |
| 53 | MlirTypeID mlirDynamicOpTraitNoTerminatorGetTypeID() { |
| 54 | return wrap(cpp: DynamicOpTraits::NoTerminator::getStaticTypeID()); |
| 55 | } |
| 56 | |
| 57 | namespace mlir::DynamicOpTraits { |
| 58 | |
| 59 | class RecursiveMemoryEffects |
| 60 | : public DynamicOpTraitImpl<OpTrait::HasRecursiveMemoryEffects> {}; |
| 61 | |
| 62 | } // namespace mlir::DynamicOpTraits |
| 63 | |
| 64 | MlirDynamicOpTrait mlirDynamicOpTraitRecursiveMemoryEffectsCreate(void) { |
| 65 | return wrap(cpp: new DynamicOpTraits::RecursiveMemoryEffects()); |
| 66 | } |
| 67 | |
| 68 | MlirTypeID mlirDynamicOpTraitRecursiveMemoryEffectsGetTypeID(void) { |
| 69 | return wrap(cpp: DynamicOpTraits::RecursiveMemoryEffects::getStaticTypeID()); |
| 70 | } |
| 71 | |
| 72 | MlirDynamicOpTrait mlirDynamicOpTraitIsIsolatedFromAboveCreate() { |
| 73 | return wrap(cpp: new DynamicOpTraits::IsIsolatedFromAbove()); |
| 74 | } |
| 75 | |
| 76 | MlirTypeID mlirDynamicOpTraitIsIsolatedFromAboveGetTypeID() { |
| 77 | return wrap(cpp: DynamicOpTraits::IsIsolatedFromAbove::getStaticTypeID()); |
| 78 | } |
| 79 | |
| 80 | void mlirDynamicOpTraitDestroy(MlirDynamicOpTrait dynamicOpTrait) { |
| 81 | delete unwrap(c: dynamicOpTrait); |
| 82 | } |
| 83 | |
| 84 | namespace mlir { |
| 85 | |
| 86 | class ExternalDynamicOpTrait : public DynamicOpTrait { |
| 87 | public: |
| 88 | ExternalDynamicOpTrait(TypeID typeID, MlirDynamicOpTraitCallbacks callbacks, |
| 89 | void *userData) |
| 90 | : typeID(typeID), callbacks(callbacks), userData(userData) { |
| 91 | if (callbacks.construct) |
| 92 | callbacks.construct(userData); |
| 93 | } |
| 94 | ~ExternalDynamicOpTrait() { |
| 95 | if (callbacks.destruct) |
| 96 | callbacks.destruct(userData); |
| 97 | } |
| 98 | |
| 99 | LogicalResult verifyTrait(Operation *op) const override { |
| 100 | return unwrap(res: callbacks.verifyTrait(wrap(cpp: op), userData)); |
| 101 | }; |
| 102 | LogicalResult verifyRegionTrait(Operation *op) const override { |
| 103 | return unwrap(res: callbacks.verifyRegionTrait(wrap(cpp: op), userData)); |
| 104 | }; |
| 105 | |
| 106 | TypeID getTypeID() const override { return typeID; }; |
| 107 | |
| 108 | private: |
| 109 | TypeID typeID; |
| 110 | MlirDynamicOpTraitCallbacks callbacks; |
| 111 | void *userData; |
| 112 | }; |
| 113 | |
| 114 | } // namespace mlir |
| 115 | |
| 116 | MlirDynamicOpTrait mlirDynamicOpTraitCreate( |
| 117 | MlirTypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData) { |
| 118 | return wrap( |
| 119 | cpp: new mlir::ExternalDynamicOpTrait(unwrap(c: typeID), callbacks, userData)); |
| 120 | } |
| 121 | |
| 122 | bool mlirDialectIsAExtensibleDialect(MlirDialect dialect) { |
| 123 | return llvm::isa<mlir::ExtensibleDialect>(Val: unwrap(c: dialect)); |
| 124 | } |
| 125 | |
| 126 | MlirDynamicTypeDefinition |
| 127 | mlirExtensibleDialectLookupTypeDefinition(MlirDialect dialect, |
| 128 | MlirStringRef typeName) { |
| 129 | return wrap(cpp: llvm::cast<mlir::ExtensibleDialect>(Val: unwrap(c: dialect)) |
| 130 | ->lookupTypeDefinition(name: unwrap(ref: typeName))); |
| 131 | } |
| 132 | |
| 133 | bool mlirTypeIsADynamicType(MlirType type) { |
| 134 | return llvm::isa<mlir::DynamicType>(Val: unwrap(c: type)); |
| 135 | } |
| 136 | |
| 137 | MlirTypeID mlirDynamicTypeGetTypeID() { |
| 138 | return wrap(cpp: mlir::DynamicType::getTypeID()); |
| 139 | } |
| 140 | |
| 141 | MlirType mlirDynamicTypeGet(MlirDynamicTypeDefinition typeDef, |
| 142 | MlirAttribute *attrs, intptr_t numAttrs) { |
| 143 | llvm::SmallVector<mlir::Attribute> attributes; |
| 144 | attributes.reserve(N: numAttrs); |
| 145 | for (intptr_t i = 0; i < numAttrs; ++i) |
| 146 | attributes.push_back(Elt: unwrap(c: attrs[i])); |
| 147 | |
| 148 | return wrap(cpp: mlir::DynamicType::get(typeDef: unwrap(c: typeDef), params: attributes)); |
| 149 | } |
| 150 | |
| 151 | intptr_t mlirDynamicTypeGetNumParams(MlirType type) { |
| 152 | return llvm::cast<mlir::DynamicType>(Val: unwrap(c: type)).getParams().size(); |
| 153 | } |
| 154 | |
| 155 | MlirAttribute mlirDynamicTypeGetParam(MlirType type, intptr_t index) { |
| 156 | return wrap(cpp: llvm::cast<mlir::DynamicType>(Val: unwrap(c: type)).getParams()[index]); |
| 157 | } |
| 158 | |
| 159 | MlirDynamicTypeDefinition mlirDynamicTypeGetTypeDef(MlirType type) { |
| 160 | return wrap(cpp: llvm::cast<mlir::DynamicType>(Val: unwrap(c: type)).getTypeDef()); |
| 161 | } |
| 162 | |
| 163 | MlirTypeID |
| 164 | mlirDynamicTypeDefinitionGetTypeID(MlirDynamicTypeDefinition typeDef) { |
| 165 | return wrap(cpp: unwrap(c: typeDef)->getTypeID()); |
| 166 | } |
| 167 | |
| 168 | MlirStringRef |
| 169 | mlirDynamicTypeDefinitionGetName(MlirDynamicTypeDefinition typeDef) { |
| 170 | return wrap(ref: unwrap(c: typeDef)->getName()); |
| 171 | } |
| 172 | |
| 173 | MlirDialect |
| 174 | mlirDynamicTypeDefinitionGetDialect(MlirDynamicTypeDefinition typeDef) { |
| 175 | return wrap(cpp: unwrap(c: typeDef)->getDialect()); |
| 176 | } |
| 177 | |
| 178 | MlirDynamicAttrDefinition |
| 179 | mlirExtensibleDialectLookupAttrDefinition(MlirDialect dialect, |
| 180 | MlirStringRef attrName) { |
| 181 | return wrap(cpp: llvm::cast<mlir::ExtensibleDialect>(Val: unwrap(c: dialect)) |
| 182 | ->lookupAttrDefinition(name: unwrap(ref: attrName))); |
| 183 | } |
| 184 | |
| 185 | bool mlirAttributeIsADynamicAttr(MlirAttribute attr) { |
| 186 | return llvm::isa<mlir::DynamicAttr>(Val: unwrap(c: attr)); |
| 187 | } |
| 188 | |
| 189 | MlirTypeID mlirDynamicAttrGetTypeID(void) { |
| 190 | return wrap(cpp: mlir::DynamicAttr::getTypeID()); |
| 191 | } |
| 192 | |
| 193 | MlirAttribute mlirDynamicAttrGet(MlirDynamicAttrDefinition attrDef, |
| 194 | MlirAttribute *attrs, intptr_t numAttrs) { |
| 195 | llvm::SmallVector<mlir::Attribute> attributes; |
| 196 | attributes.reserve(N: numAttrs); |
| 197 | for (intptr_t i = 0; i < numAttrs; ++i) |
| 198 | attributes.push_back(Elt: unwrap(c: attrs[i])); |
| 199 | |
| 200 | return wrap(cpp: mlir::DynamicAttr::get(attrDef: unwrap(c: attrDef), params: attributes)); |
| 201 | } |
| 202 | |
| 203 | intptr_t mlirDynamicAttrGetNumParams(MlirAttribute attr) { |
| 204 | return llvm::cast<mlir::DynamicAttr>(Val: unwrap(c: attr)).getParams().size(); |
| 205 | } |
| 206 | |
| 207 | MlirAttribute mlirDynamicAttrGetParam(MlirAttribute attr, intptr_t index) { |
| 208 | return wrap(cpp: llvm::cast<mlir::DynamicAttr>(Val: unwrap(c: attr)).getParams()[index]); |
| 209 | } |
| 210 | |
| 211 | MlirDynamicAttrDefinition mlirDynamicAttrGetAttrDef(MlirAttribute attr) { |
| 212 | return wrap(cpp: llvm::cast<mlir::DynamicAttr>(Val: unwrap(c: attr)).getAttrDef()); |
| 213 | } |
| 214 | |
| 215 | MlirTypeID |
| 216 | mlirDynamicAttrDefinitionGetTypeID(MlirDynamicAttrDefinition attrDef) { |
| 217 | return wrap(cpp: unwrap(c: attrDef)->getTypeID()); |
| 218 | } |
| 219 | |
| 220 | MlirStringRef |
| 221 | mlirDynamicAttrDefinitionGetName(MlirDynamicAttrDefinition attrDef) { |
| 222 | return wrap(ref: unwrap(c: attrDef)->getName()); |
| 223 | } |
| 224 | |
| 225 | MlirDialect |
| 226 | mlirDynamicAttrDefinitionGetDialect(MlirDynamicAttrDefinition attrDef) { |
| 227 | return wrap(cpp: unwrap(c: attrDef)->getDialect()); |
| 228 | } |
| 229 | |