2020-11-05 02:08:34 +08:00
|
|
|
//===-- mlir-c/Registration.h - Registration functions for MLIR ---*- 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 20:36:16 +08:00
|
|
|
|
|
|
|
#ifndef MLIR_C_REGISTRATION_H
|
|
|
|
#define MLIR_C_REGISTRATION_H
|
|
|
|
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
#include "mlir-c/IR.h"
|
|
|
|
|
2020-08-05 20:36:16 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-01-10 11:01:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dialect registration declarations.
|
|
|
|
// Registration entry-points for each dialect are declared using the common
|
|
|
|
// MLIR_DECLARE_DIALECT_REGISTRATION_CAPI macro, which takes the dialect
|
|
|
|
// API name (i.e. "Standard", "Tensor", "Linalg") and namespace (i.e. "std",
|
|
|
|
// "tensor", "linalg"). The following declarations are produced:
|
|
|
|
//
|
|
|
|
// /// Gets the above hook methods in struct form for a dialect by namespace.
|
|
|
|
// /// This is intended to facilitate dynamic lookup and registration of
|
|
|
|
// /// dialects via a plugin facility based on shared library symbol lookup.
|
2021-02-10 01:00:22 +08:00
|
|
|
// const MlirDialectHandle *mlirGetDialectHandle__{NAMESPACE}__();
|
2021-01-10 11:01:39 +08:00
|
|
|
//
|
|
|
|
// This is done via a common macro to facilitate future expansion to
|
|
|
|
// registration schemes.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-02-10 01:00:22 +08:00
|
|
|
struct MlirDialectHandle {
|
|
|
|
const void *ptr;
|
|
|
|
};
|
|
|
|
typedef struct MlirDialectHandle MlirDialectHandle;
|
|
|
|
|
2021-01-10 11:01:39 +08:00
|
|
|
#define MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Name, Namespace) \
|
2021-02-10 01:00:22 +08:00
|
|
|
MLIR_CAPI_EXPORTED MlirDialectHandle mlirGetDialectHandle__##Namespace##__()
|
2021-01-10 11:01:39 +08:00
|
|
|
|
2021-02-10 01:00:22 +08:00
|
|
|
/// Returns the namespace associated with the provided dialect handle.
|
|
|
|
MLIR_CAPI_EXPORTED
|
|
|
|
MlirStringRef mlirDialectHandleGetNamespace(MlirDialectHandle);
|
2021-01-10 11:01:39 +08:00
|
|
|
|
2021-02-10 01:00:22 +08:00
|
|
|
/// Registers the dialect associated with the provided dialect handle.
|
|
|
|
MLIR_CAPI_EXPORTED void mlirDialectHandleRegisterDialect(MlirDialectHandle,
|
|
|
|
MlirContext);
|
|
|
|
|
|
|
|
/// Loads the dialect associated with the provided dialect handle.
|
|
|
|
MLIR_CAPI_EXPORTED MlirDialect mlirDialectHandleLoadDialect(MlirDialectHandle,
|
|
|
|
MlirContext);
|
2021-01-10 11:01:39 +08:00
|
|
|
|
|
|
|
/// Registers all dialects known to core MLIR with the provided Context.
|
|
|
|
/// This is needed before creating IR for these Dialects.
|
|
|
|
/// TODO: Remove this function once the real registration API is finished.
|
2020-11-09 08:49:36 +08:00
|
|
|
MLIR_CAPI_EXPORTED void mlirRegisterAllDialects(MlirContext context);
|
2020-08-05 20:36:16 +08:00
|
|
|
|
2021-02-14 03:23:06 +08:00
|
|
|
/// Register all translations to LLVM IR for dialects that can support it.
|
|
|
|
MLIR_CAPI_EXPORTED void mlirRegisterAllLLVMTranslations(MlirContext context);
|
|
|
|
|
2020-08-05 20:36:16 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // MLIR_C_REGISTRATION_H
|