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
21extern "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
34DEFINE_C_API_STRUCT(MlirDynamicOpTrait, void);
35DEFINE_C_API_STRUCT(MlirDynamicTypeDefinition, void);
36DEFINE_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.
45MLIR_CAPI_EXPORTED bool
46mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait,
47 MlirStringRef opName, MlirContext context);
48
49/// Get the dynamic op trait that indicates the operation is a terminator.
50MLIR_CAPI_EXPORTED MlirDynamicOpTrait
51mlirDynamicOpTraitIsTerminatorCreate(void);
52
53/// Get the type ID of the dynamic op trait that indicates the operation is a
54/// terminator.
55MLIR_CAPI_EXPORTED MlirTypeID mlirDynamicOpTraitIsTerminatorGetTypeID(void);
56
57/// Get the dynamic op trait that indicates regions are isolated from above.
58MLIR_CAPI_EXPORTED MlirDynamicOpTrait
59mlirDynamicOpTraitIsIsolatedFromAboveCreate(void);
60
61/// Get the type ID of the dynamic op trait that indicates regions are isolated
62/// from above.
63MLIR_CAPI_EXPORTED MlirTypeID
64mlirDynamicOpTraitIsIsolatedFromAboveGetTypeID(void);
65
66/// Get the dynamic op trait that indicates regions have no terminator.
67MLIR_CAPI_EXPORTED MlirDynamicOpTrait
68mlirDynamicOpTraitNoTerminatorCreate(void);
69
70/// Get the type ID of the dynamic op trait that indicates regions have no
71/// terminator.
72MLIR_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.
76MLIR_CAPI_EXPORTED MlirDynamicOpTrait
77mlirDynamicOpTraitRecursiveMemoryEffectsCreate(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.
81MLIR_CAPI_EXPORTED MlirTypeID
82mlirDynamicOpTraitRecursiveMemoryEffectsGetTypeID(void);
83
84/// Destroy the dynamic op trait.
85MLIR_CAPI_EXPORTED void
86mlirDynamicOpTraitDestroy(MlirDynamicOpTrait dynamicOpTrait);
87
88typedef 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.
102MLIR_CAPI_EXPORTED MlirDynamicOpTrait mlirDynamicOpTraitCreate(
103 MlirTypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData);
104
105/// Check if the given dialect is an extensible dialect.
106MLIR_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.
110MLIR_CAPI_EXPORTED MlirDynamicTypeDefinition
111mlirExtensibleDialectLookupTypeDefinition(MlirDialect dialect,
112 MlirStringRef typeName);
113
114/// Check if the given type is a dynamic type.
115MLIR_CAPI_EXPORTED bool mlirTypeIsADynamicType(MlirType type);
116
117/// Get a dynamic type by instantiating the given type definition with the
118/// provided attributes.
119MLIR_CAPI_EXPORTED MlirType mlirDynamicTypeGet(
120 MlirDynamicTypeDefinition typeDef, MlirAttribute *attrs, intptr_t numAttrs);
121
122/// Get the number of parameters in the given dynamic type.
123MLIR_CAPI_EXPORTED intptr_t mlirDynamicTypeGetNumParams(MlirType type);
124
125/// Get the parameter at the given index in the provided dynamic type.
126MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicTypeGetParam(MlirType type,
127 intptr_t index);
128
129/// Get the type definition of the given dynamic type.
130MLIR_CAPI_EXPORTED MlirDynamicTypeDefinition
131mlirDynamicTypeGetTypeDef(MlirType type);
132
133/// Get the type ID of a dynamic type definition.
134MLIR_CAPI_EXPORTED MlirTypeID
135mlirDynamicTypeDefinitionGetTypeID(MlirDynamicTypeDefinition typeDef);
136
137/// Get the name of the given dynamic type definition.
138MLIR_CAPI_EXPORTED MlirStringRef
139mlirDynamicTypeDefinitionGetName(MlirDynamicTypeDefinition typeDef);
140
141/// Get the dialect that the given dynamic type definition belongs to.
142MLIR_CAPI_EXPORTED MlirDialect
143mlirDynamicTypeDefinitionGetDialect(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.
147MLIR_CAPI_EXPORTED MlirDynamicAttrDefinition
148mlirExtensibleDialectLookupAttrDefinition(MlirDialect dialect,
149 MlirStringRef attrName);
150
151/// Check if the given attribute is a dynamic attribute.
152MLIR_CAPI_EXPORTED bool mlirAttributeIsADynamicAttr(MlirAttribute attr);
153
154/// Get a dynamic attribute by instantiating the given attribute definition with
155/// the provided attributes.
156MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicAttrGet(
157 MlirDynamicAttrDefinition attrDef, MlirAttribute *attrs, intptr_t numAttrs);
158
159/// Get the number of parameters in the given dynamic attribute.
160MLIR_CAPI_EXPORTED intptr_t mlirDynamicAttrGetNumParams(MlirAttribute attr);
161
162/// Get the parameter at the given index in the provided dynamic attribute.
163MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicAttrGetParam(MlirAttribute attr,
164 intptr_t index);
165
166/// Get the attribute definition of the given dynamic attribute.
167MLIR_CAPI_EXPORTED MlirDynamicAttrDefinition
168mlirDynamicAttrGetAttrDef(MlirAttribute attr);
169
170/// Get the type ID of a dynamic attribute definition.
171MLIR_CAPI_EXPORTED MlirTypeID
172mlirDynamicAttrDefinitionGetTypeID(MlirDynamicAttrDefinition attrDef);
173
174/// Get the name of the given dynamic attribute definition.
175MLIR_CAPI_EXPORTED MlirStringRef
176mlirDynamicAttrDefinitionGetName(MlirDynamicAttrDefinition attrDef);
177
178/// Get the dialect that the given dynamic attribute definition belongs to.
179MLIR_CAPI_EXPORTED MlirDialect
180mlirDynamicAttrDefinitionGetDialect(MlirDynamicAttrDefinition attrDef);
181
182#ifdef __cplusplus
183}
184#endif
185
186#endif // MLIR_C_EXTENSIBLEDIALECT_H
187