xref: /llvm-project/mlir/include/mlir/CAPI/Registration.h (revision 5e83a5b4752da6631d79c446f21e5d128b5c5495)
1 //===- Registration.h - C API Registration implementation  ------*- C++ -*-===//
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 
9 #ifndef MLIR_CAPI_REGISTRATION_H
10 #define MLIR_CAPI_REGISTRATION_H
11 
12 #include "mlir-c/IR.h"
13 #include "mlir/CAPI/IR.h"
14 #include "mlir/CAPI/Support.h"
15 
16 //===----------------------------------------------------------------------===//
17 // Corrolary to MLIR_DECLARE_CAPI_DIALECT_REGISTRATION that defines an impl.
18 // Takes the same name passed to the above and the fully qualified class name
19 // of the dialect class.
20 //===----------------------------------------------------------------------===//
21 
22 /// Hooks for dynamic discovery of dialects.
23 typedef void (*MlirDialectRegistryInsertDialectHook)(
24     MlirDialectRegistry registry);
25 typedef MlirDialect (*MlirContextLoadDialectHook)(MlirContext context);
26 typedef MlirStringRef (*MlirDialectGetNamespaceHook)();
27 
28 /// Structure of dialect registration hooks.
29 struct MlirDialectRegistrationHooks {
30   MlirDialectRegistryInsertDialectHook insertHook;
31   MlirContextLoadDialectHook loadHook;
32   MlirDialectGetNamespaceHook getNamespaceHook;
33 };
34 typedef struct MlirDialectRegistrationHooks MlirDialectRegistrationHooks;
35 
36 #define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName)      \
37   static void mlirDialectRegistryInsert##Name##Dialect(                        \
38       MlirDialectRegistry registry) {                                          \
39     unwrap(registry)->insert<ClassName>();                                     \
40   }                                                                            \
41   static MlirDialect mlirContextLoad##Name##Dialect(MlirContext context) {     \
42     return wrap(unwrap(context)->getOrLoadDialect<ClassName>());               \
43   }                                                                            \
44   static MlirStringRef mlir##Name##DialectGetNamespace() {                     \
45     return wrap(ClassName::getDialectNamespace());                             \
46   }                                                                            \
47   MlirDialectHandle mlirGetDialectHandle__##Namespace##__() {                  \
48     static MlirDialectRegistrationHooks hooks = {                              \
49         mlirDialectRegistryInsert##Name##Dialect,                              \
50         mlirContextLoad##Name##Dialect, mlir##Name##DialectGetNamespace};      \
51     return MlirDialectHandle{&hooks};                                          \
52   }
53 
54 #endif // MLIR_CAPI_REGISTRATION_H
55