| 1 | //===-- mlir-c/ExtensibleDialect.h - Extensible dialect APIs -----*- C -*-====// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM |
| 4 | // Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header provides APIs for extensible dialects. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef MLIR_C_EXTENSIBLEDIALECT_H |
| 15 | #define MLIR_C_EXTENSIBLEDIALECT_H |
| 16 | |
| 17 | #include "mlir-c/IR.h" |
| 18 | #include "mlir-c/Support.h" |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | /// Opaque type declarations (see mlir-c/IR.h for more details). |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #define DEFINE_C_API_STRUCT(name, storage) \ |
| 29 | struct name { \ |
| 30 | storage *ptr; \ |
| 31 | }; \ |
| 32 | typedef struct name name |
| 33 | |
| 34 | DEFINE_C_API_STRUCT(MlirDynamicOpTrait, void); |
| 35 | DEFINE_C_API_STRUCT(MlirDynamicTypeDefinition, void); |
| 36 | DEFINE_C_API_STRUCT(MlirDynamicAttrDefinition, void); |
| 37 | |
| 38 | #undef DEFINE_C_API_STRUCT |
| 39 | |
| 40 | /// Attach a dynamic op trait to the given operation name. |
| 41 | /// Note that the operation name must be modeled by dynamic dialect and must be |
| 42 | /// registered. |
| 43 | /// The ownership of the trait will be transferred to the operation name |
| 44 | /// after this call. |
| 45 | MLIR_CAPI_EXPORTED bool |
| 46 | mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait, |
| 47 | MlirStringRef opName, MlirContext context); |
| 48 | |
| 49 | /// Get the dynamic op trait that indicates the operation is a terminator. |
| 50 | MLIR_CAPI_EXPORTED MlirDynamicOpTrait |
| 51 | mlirDynamicOpTraitIsTerminatorCreate(void); |
| 52 | |
| 53 | /// Get the type ID of the dynamic op trait that indicates the operation is a |
| 54 | /// terminator. |
| 55 | MLIR_CAPI_EXPORTED MlirTypeID mlirDynamicOpTraitIsTerminatorGetTypeID(void); |
| 56 | |
| 57 | /// Get the dynamic op trait that indicates regions are isolated from above. |
| 58 | MLIR_CAPI_EXPORTED MlirDynamicOpTrait |
| 59 | mlirDynamicOpTraitIsIsolatedFromAboveCreate(void); |
| 60 | |
| 61 | /// Get the type ID of the dynamic op trait that indicates regions are isolated |
| 62 | /// from above. |
| 63 | MLIR_CAPI_EXPORTED MlirTypeID |
| 64 | mlirDynamicOpTraitIsIsolatedFromAboveGetTypeID(void); |
| 65 | |
| 66 | /// Get the dynamic op trait that indicates regions have no terminator. |
| 67 | MLIR_CAPI_EXPORTED MlirDynamicOpTrait |
| 68 | mlirDynamicOpTraitNoTerminatorCreate(void); |
| 69 | |
| 70 | /// Get the type ID of the dynamic op trait that indicates regions have no |
| 71 | /// terminator. |
| 72 | MLIR_CAPI_EXPORTED MlirTypeID mlirDynamicOpTraitNoTerminatorGetTypeID(void); |
| 73 | |
| 74 | /// Get the dynamic op trait that indicates memory effects of an operation |
| 75 | /// includes the effects of operations nested within its regions. |
| 76 | MLIR_CAPI_EXPORTED MlirDynamicOpTrait |
| 77 | mlirDynamicOpTraitRecursiveMemoryEffectsCreate(void); |
| 78 | |
| 79 | /// Get the type ID of the dynamic op trait that indicates memory effects of an |
| 80 | /// operation includes the effects of operations nested within its regions. |
| 81 | MLIR_CAPI_EXPORTED MlirTypeID |
| 82 | mlirDynamicOpTraitRecursiveMemoryEffectsGetTypeID(void); |
| 83 | |
| 84 | /// Destroy the dynamic op trait. |
| 85 | MLIR_CAPI_EXPORTED void |
| 86 | mlirDynamicOpTraitDestroy(MlirDynamicOpTrait dynamicOpTrait); |
| 87 | |
| 88 | typedef struct { |
| 89 | /// Optional constructor for the user data. |
| 90 | /// Set to nullptr to disable it. |
| 91 | void (*construct)(void *userData); |
| 92 | /// Optional destructor for the user data. |
| 93 | /// Set to nullptr to disable it. |
| 94 | void (*destruct)(void *userData); |
| 95 | /// The callback function to verify the operation. |
| 96 | MlirLogicalResult (*verifyTrait)(MlirOperation op, void *userData); |
| 97 | /// The callback function to verify the operation with access to regions. |
| 98 | MlirLogicalResult (*verifyRegionTrait)(MlirOperation op, void *userData); |
| 99 | } MlirDynamicOpTraitCallbacks; |
| 100 | |
| 101 | /// Create a custom dynamic op trait with the given type ID and callbacks. |
| 102 | MLIR_CAPI_EXPORTED MlirDynamicOpTrait mlirDynamicOpTraitCreate( |
| 103 | MlirTypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData); |
| 104 | |
| 105 | /// Check if the given dialect is an extensible dialect. |
| 106 | MLIR_CAPI_EXPORTED bool mlirDialectIsAExtensibleDialect(MlirDialect dialect); |
| 107 | |
| 108 | /// Look up a registered type definition by type name in the given dialect. |
| 109 | /// Note that the dialect must be an extensible dialect. |
| 110 | MLIR_CAPI_EXPORTED MlirDynamicTypeDefinition |
| 111 | mlirExtensibleDialectLookupTypeDefinition(MlirDialect dialect, |
| 112 | MlirStringRef typeName); |
| 113 | |
| 114 | /// Check if the given type is a dynamic type. |
| 115 | MLIR_CAPI_EXPORTED bool mlirTypeIsADynamicType(MlirType type); |
| 116 | |
| 117 | /// Get a dynamic type by instantiating the given type definition with the |
| 118 | /// provided attributes. |
| 119 | MLIR_CAPI_EXPORTED MlirType mlirDynamicTypeGet( |
| 120 | MlirDynamicTypeDefinition typeDef, MlirAttribute *attrs, intptr_t numAttrs); |
| 121 | |
| 122 | /// Get the number of parameters in the given dynamic type. |
| 123 | MLIR_CAPI_EXPORTED intptr_t mlirDynamicTypeGetNumParams(MlirType type); |
| 124 | |
| 125 | /// Get the parameter at the given index in the provided dynamic type. |
| 126 | MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicTypeGetParam(MlirType type, |
| 127 | intptr_t index); |
| 128 | |
| 129 | /// Get the type definition of the given dynamic type. |
| 130 | MLIR_CAPI_EXPORTED MlirDynamicTypeDefinition |
| 131 | mlirDynamicTypeGetTypeDef(MlirType type); |
| 132 | |
| 133 | /// Get the type ID of a dynamic type definition. |
| 134 | MLIR_CAPI_EXPORTED MlirTypeID |
| 135 | mlirDynamicTypeDefinitionGetTypeID(MlirDynamicTypeDefinition typeDef); |
| 136 | |
| 137 | /// Get the name of the given dynamic type definition. |
| 138 | MLIR_CAPI_EXPORTED MlirStringRef |
| 139 | mlirDynamicTypeDefinitionGetName(MlirDynamicTypeDefinition typeDef); |
| 140 | |
| 141 | /// Get the dialect that the given dynamic type definition belongs to. |
| 142 | MLIR_CAPI_EXPORTED MlirDialect |
| 143 | mlirDynamicTypeDefinitionGetDialect(MlirDynamicTypeDefinition typeDef); |
| 144 | |
| 145 | /// Look up a registered attribute definition by attribute name in the given |
| 146 | /// dialect. Note that the dialect must be an extensible dialect. |
| 147 | MLIR_CAPI_EXPORTED MlirDynamicAttrDefinition |
| 148 | mlirExtensibleDialectLookupAttrDefinition(MlirDialect dialect, |
| 149 | MlirStringRef attrName); |
| 150 | |
| 151 | /// Check if the given attribute is a dynamic attribute. |
| 152 | MLIR_CAPI_EXPORTED bool mlirAttributeIsADynamicAttr(MlirAttribute attr); |
| 153 | |
| 154 | /// Get a dynamic attribute by instantiating the given attribute definition with |
| 155 | /// the provided attributes. |
| 156 | MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicAttrGet( |
| 157 | MlirDynamicAttrDefinition attrDef, MlirAttribute *attrs, intptr_t numAttrs); |
| 158 | |
| 159 | /// Get the number of parameters in the given dynamic attribute. |
| 160 | MLIR_CAPI_EXPORTED intptr_t mlirDynamicAttrGetNumParams(MlirAttribute attr); |
| 161 | |
| 162 | /// Get the parameter at the given index in the provided dynamic attribute. |
| 163 | MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicAttrGetParam(MlirAttribute attr, |
| 164 | intptr_t index); |
| 165 | |
| 166 | /// Get the attribute definition of the given dynamic attribute. |
| 167 | MLIR_CAPI_EXPORTED MlirDynamicAttrDefinition |
| 168 | mlirDynamicAttrGetAttrDef(MlirAttribute attr); |
| 169 | |
| 170 | /// Get the type ID of a dynamic attribute definition. |
| 171 | MLIR_CAPI_EXPORTED MlirTypeID |
| 172 | mlirDynamicAttrDefinitionGetTypeID(MlirDynamicAttrDefinition attrDef); |
| 173 | |
| 174 | /// Get the name of the given dynamic attribute definition. |
| 175 | MLIR_CAPI_EXPORTED MlirStringRef |
| 176 | mlirDynamicAttrDefinitionGetName(MlirDynamicAttrDefinition attrDef); |
| 177 | |
| 178 | /// Get the dialect that the given dynamic attribute definition belongs to. |
| 179 | MLIR_CAPI_EXPORTED MlirDialect |
| 180 | mlirDynamicAttrDefinitionGetDialect(MlirDynamicAttrDefinition attrDef); |
| 181 | |
| 182 | #ifdef __cplusplus |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | #endif // MLIR_C_EXTENSIBLEDIALECT_H |
| 187 | |