blob: ac909d1dd9da7ee11b6df1a8177da020a25fe283 [file] [log] [blame]
//===- Registration.h - C API Registration implementation ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_CAPI_REGISTRATION_H
#define MLIR_CAPI_REGISTRATION_H
#include "mlir-c/IR.h"
#include "mlir-c/Registration.h"
#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Support.h"
//===----------------------------------------------------------------------===//
// Corrolary to MLIR_DECLARE_CAPI_DIALECT_REGISTRATION that defines an impl.
// Takes the same name passed to the above and the fully qualified class name
// of the dialect class.
//===----------------------------------------------------------------------===//
/// Hooks for dynamic discovery of dialects.
typedef void (*MlirContextRegisterDialectHook)(MlirContext context);
typedef MlirDialect (*MlirContextLoadDialectHook)(MlirContext context);
typedef MlirStringRef (*MlirDialectGetNamespaceHook)();
/// Structure of dialect registration hooks.
struct MlirDialectRegistrationHooks {
MlirContextRegisterDialectHook registerHook;
MlirContextLoadDialectHook loadHook;
MlirDialectGetNamespaceHook getNamespaceHook;
};
typedef struct MlirDialectRegistrationHooks MlirDialectRegistrationHooks;
#define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName) \
static void mlirContextRegister##Name##Dialect(MlirContext context) { \
mlir::DialectRegistry registry; \
registry.insert<ClassName>(); \
unwrap(context)->appendDialectRegistry(registry); \
} \
static MlirDialect mlirContextLoad##Name##Dialect(MlirContext context) { \
return wrap(unwrap(context)->getOrLoadDialect<ClassName>()); \
} \
static MlirStringRef mlir##Name##DialectGetNamespace() { \
return wrap(ClassName::getDialectNamespace()); \
} \
MlirDialectHandle mlirGetDialectHandle__##Namespace##__() { \
static MlirDialectRegistrationHooks hooks = { \
mlirContextRegister##Name##Dialect, mlirContextLoad##Name##Dialect, \
mlir##Name##DialectGetNamespace}; \
return MlirDialectHandle{&hooks}; \
}
#endif // MLIR_CAPI_REGISTRATION_H