[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
Thu Mar 19 08:28:01 PDT 2026
================
@@ -31,12 +31,128 @@ 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
+mlirDynamicDialectGetDialect(MlirDynamicDialect dialect);
+
+//===----------------------------------------------------------------------===//
+/// Dynamic op definition creation
+//===----------------------------------------------------------------------===//
+
+/// Callback to verify a dynamic op.
+typedef MlirLogicalResult (*MlirDynamicOpVerifyFn)(MlirOperation op,
+ void *userData);
+
+/// Create a dynamic op definition with the given name and verifier 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.
+/// Both \p verifyFn and \p verifyRegionFn may be NULL, in which case
+/// verification always succeeds. \p userData is shared between both 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,
+ MlirDynamicOpVerifyFn verifyFn, MlirDynamicOpVerifyFn verifyRegionFn,
+ 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
+//===----------------------------------------------------------------------===//
+
+/// Callback to verify a dynamic type's parameters.
+typedef MlirLogicalResult (*MlirDynamicTypeVerifyFn)(
+ intptr_t nParams, MlirAttribute const *params, void *userData);
+
+/// Create a dynamic type definition with the given name and verifier.
+/// The name should be the bare type name (e.g. "mystruct"), not the
+/// dialect-qualified name. The definition is NOT yet registered with the
+/// dialect; call mlirDynamicDialectRegisterType to register it.
+/// \p verifyFn may be NULL, in which case verification always succeeds.
+/// Note: the verifier callback does not receive diagnostic context; failures
+/// are reported without a custom error message.
+/// If the definition is not registered, it must be destroyed with
+/// mlirDynamicTypeDefinitionDestroy to avoid a memory leak.
+MLIR_CAPI_EXPORTED MlirDynamicTypeDefinition mlirDynamicTypeDefinitionCreate(
+ MlirDynamicDialect dialect, MlirStringRef name,
+ MlirDynamicTypeVerifyFn verifyFn, void *userData);
----------------
PragmaTwice wrote:
ditto : )
https://github.com/llvm/llvm-project/pull/187239
More information about the Mlir-commits
mailing list