[Mlir-commits] [mlir] [mlir][CAPI] Add C API for creating dynamic dialects, ops, types, and attrs (PR #187239)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Mar 20 06:58:12 PDT 2026


================
@@ -31,12 +31,149 @@ extern "C" {
   };                                                                           \
   typedef struct name name
 
+DEFINE_C_API_STRUCT(MlirDynamicDialect, void);
+DEFINE_C_API_STRUCT(MlirDynamicOpDefinition, void);
 DEFINE_C_API_STRUCT(MlirDynamicOpTrait, void);
 DEFINE_C_API_STRUCT(MlirDynamicTypeDefinition, void);
 DEFINE_C_API_STRUCT(MlirDynamicAttrDefinition, void);
 
 #undef DEFINE_C_API_STRUCT
 
+//===----------------------------------------------------------------------===//
+/// Dynamic dialect creation
+//===----------------------------------------------------------------------===//
+
+/// Create a new dynamic dialect with the given name and register it with the
+/// context. If a dialect with the same name already exists, returns the
+/// existing one. The context takes ownership of the dialect. The returned
+/// handle is valid as long as the context is alive and must not be freed.
+MLIR_CAPI_EXPORTED MlirDynamicDialect
+mlirDynamicDialectCreate(MlirContext ctx, MlirStringRef name);
+
+/// Get the underlying MlirDialect from a MlirDynamicDialect.
+MLIR_CAPI_EXPORTED MlirDialect
+mlirDynamicDialectAsDialect(MlirDynamicDialect dialect);
+
+//===----------------------------------------------------------------------===//
+/// Dynamic op definition creation
+//===----------------------------------------------------------------------===//
+
+/// Callbacks for a dynamic op definition. All fields may be NULL, in which
+/// case the corresponding action is a no-op / always succeeds.
+typedef struct {
+  /// Optional constructor for the user data.
+  void (*construct)(void *userData);
+  /// Optional destructor for the user data.
+  void (*destruct)(void *userData);
+  /// The callback function to verify the operation.
+  MlirLogicalResult (*verify)(MlirOperation op, void *userData);
+  /// The callback function to verify the operation with access to regions.
+  MlirLogicalResult (*verifyRegion)(MlirOperation op, void *userData);
+} MlirDynamicOpDefinitionCallbacks;
+
+/// Create a dynamic op definition with the given name and callbacks.
+/// The name should be the bare op name (e.g. "constant"), not the
+/// dialect-qualified name. The definition is NOT yet registered with the
+/// dialect; call mlirDynamicDialectRegisterOp to register it.
+/// \p userData is shared between all callbacks.
+/// If the definition is not registered, it must be destroyed with
+/// mlirDynamicOpDefinitionDestroy to avoid a memory leak.
+MLIR_CAPI_EXPORTED MlirDynamicOpDefinition mlirDynamicOpDefinitionCreate(
+    MlirDynamicDialect dialect, MlirStringRef name,
+    MlirDynamicOpDefinitionCallbacks callbacks, void *userData);
+
+/// Destroy a dynamic op definition that was not registered with a dialect.
+MLIR_CAPI_EXPORTED void
+mlirDynamicOpDefinitionDestroy(MlirDynamicOpDefinition opDef);
+
+/// Register a dynamic op definition with its parent dialect.
+/// This transfers ownership of the definition to the dialect.
+/// After this call, ops with this name can be created using
+/// mlirOperationCreate with the dialect-qualified name.
+MLIR_CAPI_EXPORTED void
+mlirDynamicDialectRegisterOp(MlirDynamicDialect dialect,
+                             MlirDynamicOpDefinition opDef);
+
+//===----------------------------------------------------------------------===//
+/// Dynamic type definition creation
+//===----------------------------------------------------------------------===//
+
+/// Callbacks for a dynamic type definition. All fields may be NULL, in which
+/// case the corresponding action is a no-op / always succeeds.
+typedef struct {
+  /// Optional constructor for the user data.
+  void (*construct)(void *userData);
+  /// Optional destructor for the user data.
+  void (*destruct)(void *userData);
----------------
PragmaTwice wrote:

It seems that the `construct` and `destruct` will not get called in the current implementation. If you cannot figure out how to make them work. You can just remove these two field for now and I'll add them back later.

https://github.com/llvm/llvm-project/pull/187239


More information about the Mlir-commits mailing list