MLIR 24.0.0git
ExtensibleDialect.cpp
Go to the documentation of this file.
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
10#include "mlir/CAPI/IR.h"
11#include "mlir/CAPI/Support.h"
14
15using namespace mlir;
16
18DEFINE_C_API_PTR_METHODS(MlirDynamicTypeDefinition, DynamicTypeDefinition)
19DEFINE_C_API_PTR_METHODS(MlirDynamicAttrDefinition, DynamicAttrDefinition)
20
21bool mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait,
22 MlirStringRef opName, MlirContext context) {
23 std::optional<RegisteredOperationName> opNameFound =
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 };
33 static_cast<RegisteredOperationNameWithImpl &>(*opNameFound).getImpl();
34
35 std::unique_ptr<DynamicOpTrait> trait(unwrap(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(std::move(trait));
39}
40
43}
44
48
51}
52
56
57namespace mlir::DynamicOpTraits {
58
60 : public DynamicOpTraitImpl<OpTrait::HasRecursiveMemoryEffects> {};
61
62} // namespace mlir::DynamicOpTraits
63
67
71
75
79
80void mlirDynamicOpTraitDestroy(MlirDynamicOpTrait dynamicOpTrait) {
81 delete unwrap(dynamicOpTrait);
82}
83
84namespace mlir {
85
87public:
89 void *userData)
90 : typeID(typeID), callbacks(callbacks), userData(userData) {
91 if (callbacks.construct)
92 callbacks.construct(userData);
93 }
95 if (callbacks.destruct)
96 callbacks.destruct(userData);
97 }
98
99 LogicalResult verifyTrait(Operation *op) const override {
100 return unwrap(callbacks.verifyTrait(wrap(op), userData));
101 };
102 LogicalResult verifyRegionTrait(Operation *op) const override {
103 return unwrap(callbacks.verifyRegionTrait(wrap(op), userData));
104 };
105
106 TypeID getTypeID() const override { return typeID; };
107
108private:
109 TypeID typeID;
111 void *userData;
112};
113
114} // namespace mlir
115
116MlirDynamicOpTrait mlirDynamicOpTraitCreate(
117 MlirTypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData) {
118 return wrap(
119 new mlir::ExternalDynamicOpTrait(unwrap(typeID), callbacks, userData));
120}
121
122bool mlirDialectIsAExtensibleDialect(MlirDialect dialect) {
123 return llvm::isa<mlir::ExtensibleDialect>(unwrap(dialect));
124}
125
126MlirDynamicTypeDefinition
128 MlirStringRef typeName) {
129 return wrap(llvm::cast<mlir::ExtensibleDialect>(unwrap(dialect))
130 ->lookupTypeDefinition(unwrap(typeName)));
131}
132
133bool mlirTypeIsADynamicType(MlirType type) {
134 return llvm::isa<mlir::DynamicType>(unwrap(type));
135}
136
140
141MlirType mlirDynamicTypeGet(MlirDynamicTypeDefinition typeDef,
142 MlirAttribute *attrs, intptr_t numAttrs) {
144 attributes.reserve(numAttrs);
145 for (intptr_t i = 0; i < numAttrs; ++i)
146 attributes.push_back(unwrap(attrs[i]));
147
148 return wrap(mlir::DynamicType::get(unwrap(typeDef), attributes));
149}
150
152 return llvm::cast<mlir::DynamicType>(unwrap(type)).getParams().size();
153}
154
155MlirAttribute mlirDynamicTypeGetParam(MlirType type, intptr_t index) {
156 return wrap(llvm::cast<mlir::DynamicType>(unwrap(type)).getParams()[index]);
157}
158
159MlirDynamicTypeDefinition mlirDynamicTypeGetTypeDef(MlirType type) {
160 return wrap(llvm::cast<mlir::DynamicType>(unwrap(type)).getTypeDef());
161}
162
163MlirTypeID
164mlirDynamicTypeDefinitionGetTypeID(MlirDynamicTypeDefinition typeDef) {
165 return wrap(unwrap(typeDef)->getTypeID());
166}
167
169mlirDynamicTypeDefinitionGetName(MlirDynamicTypeDefinition typeDef) {
170 return wrap(unwrap(typeDef)->getName());
171}
172
173MlirDialect
174mlirDynamicTypeDefinitionGetDialect(MlirDynamicTypeDefinition typeDef) {
175 return wrap(unwrap(typeDef)->getDialect());
176}
177
178MlirDynamicAttrDefinition
180 MlirStringRef attrName) {
181 return wrap(llvm::cast<mlir::ExtensibleDialect>(unwrap(dialect))
182 ->lookupAttrDefinition(unwrap(attrName)));
183}
184
185bool mlirAttributeIsADynamicAttr(MlirAttribute attr) {
186 return llvm::isa<mlir::DynamicAttr>(unwrap(attr));
187}
188
189MlirTypeID mlirDynamicAttrGetTypeID(void) {
191}
192
193MlirAttribute mlirDynamicAttrGet(MlirDynamicAttrDefinition attrDef,
194 MlirAttribute *attrs, intptr_t numAttrs) {
196 attributes.reserve(numAttrs);
197 for (intptr_t i = 0; i < numAttrs; ++i)
198 attributes.push_back(unwrap(attrs[i]));
199
200 return wrap(mlir::DynamicAttr::get(unwrap(attrDef), attributes));
201}
202
204 return llvm::cast<mlir::DynamicAttr>(unwrap(attr)).getParams().size();
205}
206
207MlirAttribute mlirDynamicAttrGetParam(MlirAttribute attr, intptr_t index) {
208 return wrap(llvm::cast<mlir::DynamicAttr>(unwrap(attr)).getParams()[index]);
209}
210
211MlirDynamicAttrDefinition mlirDynamicAttrGetAttrDef(MlirAttribute attr) {
212 return wrap(llvm::cast<mlir::DynamicAttr>(unwrap(attr)).getAttrDef());
213}
214
215MlirTypeID
216mlirDynamicAttrDefinitionGetTypeID(MlirDynamicAttrDefinition attrDef) {
217 return wrap(unwrap(attrDef)->getTypeID());
218}
219
221mlirDynamicAttrDefinitionGetName(MlirDynamicAttrDefinition attrDef) {
222 return wrap(unwrap(attrDef)->getName());
223}
224
225MlirDialect
226mlirDynamicAttrDefinitionGetDialect(MlirDynamicAttrDefinition attrDef) {
227 return wrap(unwrap(attrDef)->getDialect());
228}
bool mlirTypeIsADynamicType(MlirType type)
Check if the given type is a dynamic type.
MlirDialect mlirDynamicTypeDefinitionGetDialect(MlirDynamicTypeDefinition typeDef)
Get the dialect that the given dynamic type definition belongs to.
MlirDynamicTypeDefinition mlirDynamicTypeGetTypeDef(MlirType type)
Get the type definition of the given dynamic type.
MlirType mlirDynamicTypeGet(MlirDynamicTypeDefinition typeDef, MlirAttribute *attrs, intptr_t numAttrs)
Get a dynamic type by instantiating the given type definition with the provided attributes.
bool mlirDialectIsAExtensibleDialect(MlirDialect dialect)
Check if the given dialect is an extensible dialect.
MlirTypeID mlirDynamicOpTraitRecursiveMemoryEffectsGetTypeID(void)
Get the type ID of the dynamic op trait that indicates memory effects of an operation includes the ef...
MlirDynamicOpTrait mlirDynamicOpTraitCreate(MlirTypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData)
Create a custom dynamic op trait with the given type ID and callbacks.
MlirDynamicAttrDefinition mlirDynamicAttrGetAttrDef(MlirAttribute attr)
Get the attribute definition of the given dynamic attribute.
MlirDynamicTypeDefinition mlirExtensibleDialectLookupTypeDefinition(MlirDialect dialect, MlirStringRef typeName)
Look up a registered type definition by type name in the given dialect.
MlirDynamicOpTrait mlirDynamicOpTraitIsTerminatorCreate()
Get the dynamic op trait that indicates the operation is a terminator.
MlirStringRef mlirDynamicAttrDefinitionGetName(MlirDynamicAttrDefinition attrDef)
Get the name of the given dynamic attribute definition.
MlirDynamicOpTrait mlirDynamicOpTraitIsIsolatedFromAboveCreate()
Get the dynamic op trait that indicates regions are isolated from above.
void mlirDynamicOpTraitDestroy(MlirDynamicOpTrait dynamicOpTrait)
Destroy the dynamic op trait.
MlirTypeID mlirDynamicOpTraitIsTerminatorGetTypeID()
Get the type ID of the dynamic op trait that indicates the operation is a terminator.
MlirAttribute mlirDynamicAttrGet(MlirDynamicAttrDefinition attrDef, MlirAttribute *attrs, intptr_t numAttrs)
Get a dynamic attribute by instantiating the given attribute definition with the provided attributes.
MlirTypeID mlirDynamicAttrDefinitionGetTypeID(MlirDynamicAttrDefinition attrDef)
Get the type ID of a dynamic attribute definition.
MlirDynamicOpTrait mlirDynamicOpTraitRecursiveMemoryEffectsCreate(void)
Get the dynamic op trait that indicates memory effects of an operation includes the effects of operat...
MlirDynamicAttrDefinition mlirExtensibleDialectLookupAttrDefinition(MlirDialect dialect, MlirStringRef attrName)
Look up a registered attribute definition by attribute name in the given dialect.
MlirDialect mlirDynamicAttrDefinitionGetDialect(MlirDynamicAttrDefinition attrDef)
Get the dialect that the given dynamic attribute definition belongs to.
MlirTypeID mlirDynamicTypeDefinitionGetTypeID(MlirDynamicTypeDefinition typeDef)
Get the type ID of a dynamic type definition.
intptr_t mlirDynamicTypeGetNumParams(MlirType type)
Get the number of parameters in the given dynamic type.
MlirTypeID mlirDynamicTypeGetTypeID()
MlirStringRef mlirDynamicTypeDefinitionGetName(MlirDynamicTypeDefinition typeDef)
Get the name of the given dynamic type definition.
MlirAttribute mlirDynamicAttrGetParam(MlirAttribute attr, intptr_t index)
Get the parameter at the given index in the provided dynamic attribute.
MlirAttribute mlirDynamicTypeGetParam(MlirType type, intptr_t index)
Get the parameter at the given index in the provided dynamic type.
intptr_t mlirDynamicAttrGetNumParams(MlirAttribute attr)
Get the number of parameters in the given dynamic attribute.
MlirTypeID mlirDynamicOpTraitIsIsolatedFromAboveGetTypeID()
Get the type ID of the dynamic op trait that indicates regions are isolated from above.
bool mlirAttributeIsADynamicAttr(MlirAttribute attr)
Check if the given attribute is a dynamic attribute.
MlirTypeID mlirDynamicAttrGetTypeID(void)
MlirDynamicOpTrait mlirDynamicOpTraitNoTerminatorCreate()
Get the dynamic op trait that indicates regions have no terminator.
MlirTypeID mlirDynamicOpTraitNoTerminatorGetTypeID()
Get the type ID of the dynamic op trait that indicates regions have no terminator.
#define DEFINE_C_API_PTR_METHODS(name, cpptype)
Definition Wrap.h:25
The definition of a dynamic attribute.
static DynamicAttr get(DynamicAttrDefinition *attrDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic attribute given a dynamic attribute definition and attribute paramete...
The definition of a dynamic op.
Base class of traits for dynamic-defined operations.
The definition of a dynamic type.
static DynamicType get(DynamicTypeDefinition *typeDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic type given a dynamic type definition and type parameters.
ExternalDynamicOpTrait(TypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData)
TypeID getTypeID() const override
Returns the TypeID of the trait.
LogicalResult verifyRegionTrait(Operation *op) const override
LogicalResult verifyTrait(Operation *op) const override
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
This is a "type erased" representation of a registered operation.
static std::optional< RegisteredOperationName > lookup(StringRef name, MLIRContext *ctx)
Lookup the registered operation information for the given operation.
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
MLIR_CAPI_EXPORTED bool mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait, MlirStringRef opName, MlirContext context)
Attach a dynamic op trait to the given operation name.
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78