Skip to content

Commit 5e83a5b

Browse files
[mlir] Overhaul C/Python registration APIs to properly scope registration/loading activities.
Since the very first commits, the Python and C MLIR APIs have had mis-placed registration/load functionality for dialects, extensions, etc. This was done pragmatically in order to get bootstrapped and then just grew in. Downstreams largely bypass and do their own thing by providing various APIs to register things they need. Meanwhile, the C++ APIs have stabilized around this and it would make sense to follow suit. The thing we have observed in canonical usage by downstreams is that each downstream tends to have native entry points that configure its installation to its preferences with one-stop APIs. This patch leans in to this approach with `RegisterEverything.h` and `mlir._mlir_libs._mlirRegisterEverything` being the one-stop entry points for the "upstream packages". The `_mlir_libs.__init__.py` now allows customization of the environment and Context by adding "initialization modules" to the `_mlir_libs` package. If present, `_mlirRegisterEverything` is treated as such a module. Others can be added by downstreams by adding a `_site_initialize_{i}.py` module, where '{i}' is a number starting with zero. The number will be incremented and corresponding module loaded until one is not found. Initialization modules can: * Perform load time customization to the global environment (i.e. registering passes, hooks, etc). * Define a `register_dialects(registry: DialectRegistry)` function that can extend the `DialectRegistry` that will be used to bootstrap the `Context`. * Define a `context_init_hook(context: Context)` function that will be added to a list of callbacks which will be invoked after dialect registration during `Context` initialization. Note that the `MLIRPythonExtension.RegisterEverything` is not included by default when building a downstream (its corresponding behavior was prior). For downstreams which need the default MLIR initialization to take place, they must add this back in to their Python CMake build just like they add their own components (i.e. to `add_mlir_python_common_capi_library` and `add_mlir_python_modules`). It is perfectly valid to not do this, in which case, only the things explicitly depended on and initialized by downstreams will be built/packaged. If the downstream has not been set up for this, it is recommended to simply add this back for the time being and pay the build time/package size cost. CMake changes: * `MLIRCAPIRegistration` -> `MLIRCAPIRegisterEverything` (renamed to signify what it does and force an evaluation: a number of places were incidentally linking this very expensive target) * `MLIRPythonSoure.Passes` removed (without replacement: just drop) * `MLIRPythonExtension.AllPassesRegistration` removed (without replacement: just drop) * `MLIRPythonExtension.Conversions` removed (without replacement: just drop) * `MLIRPythonExtension.Transforms` removed (without replacement: just drop) Header changes: * `mlir-c/Registration.h` is deleted. Dialect registration functionality is now in `IR.h`. Registration of upstream features are in `mlir-c/RegisterEverything.h`. When updating MLIR and a couple of downstreams, I found that proper usage was commingled so required making a choice vs just blind S&R. Python APIs removed: * mlir.transforms and mlir.conversions (previously only had an __init__.py which indirectly triggered `mlirRegisterTransformsPasses()` and `mlirRegisterConversionPasses()` respectively). Downstream impact: Remove these imports if present (they now happen as part of default initialization). * mlir._mlir_libs._all_passes_registration, mlir._mlir_libs._mlirTransforms, mlir._mlir_libs._mlirConversions. Downstream impact: None expected (these were internally used). C-APIs changed: * mlirRegisterAllDialects(MlirContext) now takes an MlirDialectRegistry instead. It also used to trigger loading of all dialects, which was already marked with a TODO to remove -- it no longer does, and for direct use, dialects must be explicitly loaded. Downstream impact: Direct C-API users must ensure that needed dialects are loaded or call `mlirContextLoadAllAvailableDialects(MlirContext)` to emulate the prior behavior. Also see the `ir.c` test case (e.g. ` mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("func"));`). * mlirDialectHandle* APIs were moved from Registration.h (which now is restricted to just global/upstream registration) to IR.h, arguably where it should have been. Downstream impact: include correct header (likely already doing so). C-APIs added: * mlirContextLoadAllAvailableDialects(MlirContext): Corresponds to C++ API with the same purpose. Python APIs added: * mlir.ir.DialectRegistry: Mapping for an MlirDialectRegistry. * mlir.ir.Context.append_dialect_registry(MlirDialectRegistry) * mlir.ir.Context.load_all_available_dialects() * mlir._mlir_libs._mlirAllRegistration: New native extension that exposes a `register_dialects(MlirDialectRegistry)` entry point and performs all upstream pass/conversion/transforms registration on init. In this first step, we eagerly load this as part of the __init__.py and use it to monkey patch the Context to emulate prior behavior. * Type caster and capsule support for MlirDialectRegistry This should make it possible to build downstream Python dialects that only depend on a subset of MLIR. See: #56037 Here is an example PR, minimally adapting IREE to these changes: https://github.com/iree-org/iree/pull/9638/files In this situation, IREE is opting to not link everything, since it is already configuring the Context to its liking. For projects that would just like to not think about it and pull in everything, add `MLIRPythonExtension.RegisterEverything` to the list of Python sources getting built, and the old behavior will continue. Reviewed By: mehdi_amini, ftynse Differential Revision: https://reviews.llvm.org/D128593
1 parent 8477bc6 commit 5e83a5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+425
-309
lines changed

mlir/examples/standalone/include/Standalone-c/Dialects.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef STANDALONE_C_DIALECTS_H
1010
#define STANDALONE_C_DIALECTS_H
1111

12-
#include "mlir-c/Registration.h"
12+
#include "mlir-c/IR.h"
1313

1414
#ifdef __cplusplus
1515
extern "C" {

mlir/examples/standalone/python/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ add_mlir_python_common_capi_library(StandalonePythonCAPI
4040
RELATIVE_INSTALL_ROOT "../../../.."
4141
DECLARED_SOURCES
4242
StandalonePythonSources
43+
# TODO: Remove this in favor of showing fine grained registration once
44+
# available.
45+
MLIRPythonExtension.RegisterEverything
4346
MLIRPythonSources.Core
4447
)
4548

@@ -52,6 +55,9 @@ add_mlir_python_modules(StandalonePythonModules
5255
INSTALL_PREFIX "python_packages/standalone/mlir_standalone"
5356
DECLARED_SOURCES
5457
StandalonePythonSources
58+
# TODO: Remove this in favor of showing fine grained registration once
59+
# available.
60+
MLIRPythonExtension.RegisterEverything
5561
MLIRPythonSources
5662
COMMON_CAPI_LINK_LIBS
5763
StandalonePythonCAPI

mlir/examples/standalone/test/CAPI/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ add_mlir_aggregate(StandaloneCAPITestLib
66
SHARED
77
EMBED_LIBS
88
MLIRCAPIIR
9-
MLIRCAPIRegistration
9+
# TODO: Remove this in favor of showing fine grained dialect registration
10+
# (once available).
11+
MLIRCAPIRegisterEverything
1012
StandaloneCAPI
1113
)
1214

mlir/examples/standalone/test/CAPI/standalone-capi-test.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111

1212
#include <stdio.h>
1313

14-
#include "mlir-c/IR.h"
1514
#include "Standalone-c/Dialects.h"
15+
#include "mlir-c/IR.h"
16+
#include "mlir-c/RegisterEverything.h"
17+
18+
static void registerAllUpstreamDialects(MlirContext ctx) {
19+
MlirDialectRegistry registry = mlirDialectRegistryCreate();
20+
mlirRegisterAllDialects(registry);
21+
mlirContextAppendDialectRegistry(ctx, registry);
22+
mlirDialectRegistryDestroy(registry);
23+
}
1624

1725
int main(int argc, char **argv) {
1826
MlirContext ctx = mlirContextCreate();
1927
// TODO: Create the dialect handles for the builtin dialects and avoid this.
2028
// This adds dozens of MB of binary size over just the standalone dialect.
21-
mlirRegisterAllDialects(ctx);
29+
registerAllUpstreamDialects(ctx);
2230
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__standalone__(), ctx);
2331

2432
MlirModule module = mlirModuleCreateParse(

mlir/include/mlir-c/Bindings/Python/Interop.h

+24
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute._CAPIPtr")
6565
#define MLIR_PYTHON_CAPSULE_CONTEXT \
6666
MAKE_MLIR_PYTHON_QUALNAME("ir.Context._CAPIPtr")
67+
#define MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY \
68+
MAKE_MLIR_PYTHON_QUALNAME("ir.DialectRegistry._CAPIPtr")
6769
#define MLIR_PYTHON_CAPSULE_EXECUTION_ENGINE \
6870
MAKE_MLIR_PYTHON_QUALNAME("execution_engine.ExecutionEngine._CAPIPtr")
6971
#define MLIR_PYTHON_CAPSULE_INTEGER_SET \
@@ -172,6 +174,28 @@ static inline MlirContext mlirPythonCapsuleToContext(PyObject *capsule) {
172174
return context;
173175
}
174176

177+
/** Creates a capsule object encapsulating the raw C-API MlirDialectRegistry.
178+
* The returned capsule does not extend or affect ownership of any Python
179+
* objects that reference the context in any way.
180+
*/
181+
static inline PyObject *
182+
mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry) {
183+
return PyCapsule_New(registry.ptr, MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY,
184+
NULL);
185+
}
186+
187+
/** Extracts an MlirDialectRegistry from a capsule as produced from
188+
* mlirPythonDialectRegistryToCapsule. If the capsule is not of the right type,
189+
* then a null context is returned (as checked via mlirContextIsNull). In such a
190+
* case, the Python APIs will have already set an error. */
191+
static inline MlirDialectRegistry
192+
mlirPythonCapsuleToDialectRegistry(PyObject *capsule) {
193+
void *ptr =
194+
PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY);
195+
MlirDialectRegistry registry = {ptr};
196+
return registry;
197+
}
198+
175199
/** Creates a capsule object encapsulating the raw C-API MlirLocation.
176200
* The returned capsule does not extend or affect ownership of any Python
177201
* objects that reference the location in any way. */

mlir/include/mlir-c/Dialect/Async.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef MLIR_C_DIALECT_ASYNC_H
1111
#define MLIR_C_DIALECT_ASYNC_H
1212

13-
#include "mlir-c/Registration.h"
13+
#include "mlir-c/IR.h"
1414
#include "mlir-c/Support.h"
1515

1616
#ifdef __cplusplus

mlir/include/mlir-c/Dialect/ControlFlow.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef MLIR_C_DIALECT_CONTROLFLOW_H
1111
#define MLIR_C_DIALECT_CONTROLFLOW_H
1212

13-
#include "mlir-c/Registration.h"
13+
#include "mlir-c/IR.h"
1414

1515
#ifdef __cplusplus
1616
extern "C" {

mlir/include/mlir-c/Dialect/Func.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef MLIR_C_DIALECT_FUNC_H
1919
#define MLIR_C_DIALECT_FUNC_H
2020

21-
#include "mlir-c/Registration.h"
21+
#include "mlir-c/IR.h"
2222

2323
#ifdef __cplusplus
2424
extern "C" {

mlir/include/mlir-c/Dialect/GPU.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef MLIR_C_DIALECT_GPU_H
1111
#define MLIR_C_DIALECT_GPU_H
1212

13-
#include "mlir-c/Registration.h"
13+
#include "mlir-c/IR.h"
1414
#include "mlir-c/Support.h"
1515

1616
#ifdef __cplusplus

mlir/include/mlir-c/Dialect/LLVM.h

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define MLIR_C_DIALECT_LLVM_H
1212

1313
#include "mlir-c/IR.h"
14-
#include "mlir-c/Registration.h"
1514

1615
#ifdef __cplusplus
1716
extern "C" {

mlir/include/mlir-c/Dialect/Linalg.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef MLIR_C_DIALECT_LINALG_H
1111
#define MLIR_C_DIALECT_LINALG_H
1212

13-
#include "mlir-c/Registration.h"
13+
#include "mlir-c/IR.h"
1414
#include "mlir-c/Support.h"
1515

1616
#ifdef __cplusplus

mlir/include/mlir-c/Dialect/PDL.h

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define MLIR_C_DIALECT_PDL_H
1212

1313
#include "mlir-c/IR.h"
14-
#include "mlir-c/Registration.h"
1514

1615
#ifdef __cplusplus
1716
extern "C" {

mlir/include/mlir-c/Dialect/Quant.h

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define MLIR_C_DIALECT_QUANT_H
1212

1313
#include "mlir-c/IR.h"
14-
#include "mlir-c/Registration.h"
1514

1615
#ifdef __cplusplus
1716
extern "C" {

mlir/include/mlir-c/Dialect/SCF.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef MLIR_C_DIALECT_SCF_H
1111
#define MLIR_C_DIALECT_SCF_H
1212

13-
#include "mlir-c/Registration.h"
13+
#include "mlir-c/IR.h"
1414

1515
#ifdef __cplusplus
1616
extern "C" {

mlir/include/mlir-c/Dialect/Shape.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef MLIR_C_DIALECT_SHAPE_H
1111
#define MLIR_C_DIALECT_SHAPE_H
1212

13-
#include "mlir-c/Registration.h"
13+
#include "mlir-c/IR.h"
1414

1515
#ifdef __cplusplus
1616
extern "C" {

mlir/include/mlir-c/Dialect/SparseTensor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define MLIR_C_DIALECT_SPARSETENSOR_H
1212

1313
#include "mlir-c/AffineMap.h"
14-
#include "mlir-c/Registration.h"
14+
#include "mlir-c/IR.h"
1515

1616
#ifdef __cplusplus
1717
extern "C" {

mlir/include/mlir-c/Dialect/Tensor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef MLIR_C_DIALECT_TENSOR_H
1111
#define MLIR_C_DIALECT_TENSOR_H
1212

13-
#include "mlir-c/Registration.h"
13+
#include "mlir-c/IR.h"
1414

1515
#ifdef __cplusplus
1616
extern "C" {

mlir/include/mlir-c/IR.h

+46
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
130130
MLIR_CAPI_EXPORTED void mlirContextEnableMultithreading(MlirContext context,
131131
bool enable);
132132

133+
/// Eagerly loads all available dialects registered with a context, making
134+
/// them available for use for IR construction.
135+
MLIR_CAPI_EXPORTED void
136+
mlirContextLoadAllAvailableDialects(MlirContext context);
137+
133138
/// Returns whether the given fully-qualified operation (i.e.
134139
/// 'dialect.operation') is registered with the context. This will return true
135140
/// if the dialect is loaded and the operation is registered within the
@@ -157,6 +162,47 @@ MLIR_CAPI_EXPORTED bool mlirDialectEqual(MlirDialect dialect1,
157162
/// Returns the namespace of the given dialect.
158163
MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect);
159164

165+
//===----------------------------------------------------------------------===//
166+
// DialectHandle API.
167+
// Registration entry-points for each dialect are declared using the common
168+
// MLIR_DECLARE_DIALECT_REGISTRATION_CAPI macro, which takes the dialect
169+
// API name (i.e. "Func", "Tensor", "Linalg") and namespace (i.e. "func",
170+
// "tensor", "linalg"). The following declarations are produced:
171+
//
172+
// /// Gets the above hook methods in struct form for a dialect by namespace.
173+
// /// This is intended to facilitate dynamic lookup and registration of
174+
// /// dialects via a plugin facility based on shared library symbol lookup.
175+
// const MlirDialectHandle *mlirGetDialectHandle__{NAMESPACE}__();
176+
//
177+
// This is done via a common macro to facilitate future expansion to
178+
// registration schemes.
179+
//===----------------------------------------------------------------------===//
180+
181+
struct MlirDialectHandle {
182+
const void *ptr;
183+
};
184+
typedef struct MlirDialectHandle MlirDialectHandle;
185+
186+
#define MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Name, Namespace) \
187+
MLIR_CAPI_EXPORTED MlirDialectHandle mlirGetDialectHandle__##Namespace##__()
188+
189+
/// Returns the namespace associated with the provided dialect handle.
190+
MLIR_CAPI_EXPORTED
191+
MlirStringRef mlirDialectHandleGetNamespace(MlirDialectHandle);
192+
193+
/// Inserts the dialect associated with the provided dialect handle into the
194+
/// provided dialect registry
195+
MLIR_CAPI_EXPORTED void mlirDialectHandleInsertDialect(MlirDialectHandle,
196+
MlirDialectRegistry);
197+
198+
/// Registers the dialect associated with the provided dialect handle.
199+
MLIR_CAPI_EXPORTED void mlirDialectHandleRegisterDialect(MlirDialectHandle,
200+
MlirContext);
201+
202+
/// Loads the dialect associated with the provided dialect handle.
203+
MLIR_CAPI_EXPORTED MlirDialect mlirDialectHandleLoadDialect(MlirDialectHandle,
204+
MlirContext);
205+
160206
//===----------------------------------------------------------------------===//
161207
// DialectRegistry API.
162208
//===----------------------------------------------------------------------===//

mlir/include/mlir-c/Pass.h

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#define MLIR_C_PASS_H
1616

1717
#include "mlir-c/IR.h"
18-
#include "mlir-c/Registration.h"
1918
#include "mlir-c/Support.h"
2019

2120
#ifdef __cplusplus
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- mlir-c/RegisterEverything.h - Register all MLIR entities --*- C -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
// This header contains registration entry points for MLIR upstream dialects
10+
// and passes. Downstream projects typically will not want to use this unless
11+
// if they don't care about binary size or build bloat and just wish access
12+
// to the entire set of upstream facilities. For those that do care, they
13+
// should use registration functions specific to their project.
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef MLIR_C_REGISTER_EVERYTHING_H
17+
#define MLIR_C_REGISTER_EVERYTHING_H
18+
19+
#include "mlir-c/IR.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
/// Appends all upstream dialects and extensions to the dialect registry.
26+
MLIR_CAPI_EXPORTED void mlirRegisterAllDialects(MlirDialectRegistry registry);
27+
28+
/// Register all translations to LLVM IR for dialects that can support it.
29+
MLIR_CAPI_EXPORTED void mlirRegisterAllLLVMTranslations(MlirContext context);
30+
31+
/// Register all compiler passes of MLIR.
32+
MLIR_CAPI_EXPORTED void mlirRegisterAllPasses();
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif // MLIR_C_REGISTER_EVERYTHING_H

mlir/include/mlir-c/Registration.h

-75
This file was deleted.

0 commit comments

Comments
 (0)