Skip to content

Commit 061631e

Browse files
maksleventalmahesh-attarde
authored andcommitted
[MLIR][Standalone] use narrow registration instead of RegisterEverything (llvm#160469)
This PR cleans up a long-standing TODO by avoiding `MLIRPythonExtension.RegisterEverything` in the Standalone example and registering the necessary dialects explicitly instead.
1 parent 1e1e1b3 commit 061631e

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

mlir/examples/standalone/python/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ declare_mlir_python_extension(StandalonePythonSources.Pybind11Extension
3030
PRIVATE_LINK_LIBS
3131
LLVMSupport
3232
EMBED_CAPI_LINK_LIBS
33+
MLIRCAPIIR
34+
MLIRCAPIArith
35+
MLIRCAPITransforms
3336
StandaloneCAPI
3437
PYTHON_BINDINGS_LIBRARY pybind11
3538
)
@@ -42,6 +45,9 @@ declare_mlir_python_extension(StandalonePythonSources.NanobindExtension
4245
PRIVATE_LINK_LIBS
4346
LLVMSupport
4447
EMBED_CAPI_LINK_LIBS
48+
MLIRCAPIIR
49+
MLIRCAPIArith
50+
MLIRCAPITransforms
4551
StandaloneCAPI
4652
PYTHON_BINDINGS_LIBRARY nanobind
4753
)
@@ -58,9 +64,6 @@ add_mlir_python_common_capi_library(StandalonePythonCAPI
5864
RELATIVE_INSTALL_ROOT "../../../.."
5965
DECLARED_SOURCES
6066
StandalonePythonSources
61-
# TODO: Remove this in favor of showing fine grained registration once
62-
# available.
63-
MLIRPythonExtension.RegisterEverything
6467
MLIRPythonSources.Core
6568
MLIRPythonSources.Dialects.builtin
6669
)
@@ -130,9 +133,6 @@ declare_mlir_python_sources(
130133
)
131134
set(_declared_sources
132135
StandalonePythonSources
133-
# TODO: Remove this in favor of showing fine grained registration once
134-
# available.
135-
MLIRPythonExtension.RegisterEverything
136136
MLIRPythonSources.Core
137137
MLIRPythonSources.Dialects.builtin
138138
)

mlir/examples/standalone/python/StandaloneExtensionNanobind.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
#include "Standalone-c/Dialects.h"
13+
#include "mlir-c/Dialect/Arith.h"
1314
#include "mlir/Bindings/Python/Nanobind.h"
1415
#include "mlir/Bindings/Python/NanobindAdaptors.h"
1516

@@ -22,17 +23,21 @@ NB_MODULE(_standaloneDialectsNanobind, m) {
2223
auto standaloneM = m.def_submodule("standalone");
2324

2425
standaloneM.def(
25-
"register_dialect",
26+
"register_dialects",
2627
[](MlirContext context, bool load) {
27-
MlirDialectHandle handle = mlirGetDialectHandle__standalone__();
28-
mlirDialectHandleRegisterDialect(handle, context);
28+
MlirDialectHandle arithHandle = mlirGetDialectHandle__arith__();
29+
MlirDialectHandle standaloneHandle =
30+
mlirGetDialectHandle__standalone__();
31+
mlirDialectHandleRegisterDialect(arithHandle, context);
32+
mlirDialectHandleRegisterDialect(standaloneHandle, context);
2933
if (load) {
30-
mlirDialectHandleLoadDialect(handle, context);
34+
mlirDialectHandleLoadDialect(arithHandle, context);
35+
mlirDialectHandleRegisterDialect(standaloneHandle, context);
3136
}
3237
},
3338
nb::arg("context").none() = nb::none(), nb::arg("load") = true,
3439
// clang-format off
35-
nb::sig("def register_dialect(context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") ", load: bool = True) -> None")
40+
nb::sig("def register_dialects(context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") ", load: bool = True) -> None")
3641
// clang-format on
3742
);
3843
}

mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
#include "Standalone-c/Dialects.h"
13+
#include "mlir-c/Dialect/Arith.h"
1314
#include "mlir/Bindings/Python/PybindAdaptors.h"
1415

1516
using namespace mlir::python::adaptors;
@@ -21,12 +22,16 @@ PYBIND11_MODULE(_standaloneDialectsPybind11, m) {
2122
auto standaloneM = m.def_submodule("standalone");
2223

2324
standaloneM.def(
24-
"register_dialect",
25+
"register_dialects",
2526
[](MlirContext context, bool load) {
26-
MlirDialectHandle handle = mlirGetDialectHandle__standalone__();
27-
mlirDialectHandleRegisterDialect(handle, context);
27+
MlirDialectHandle arithHandle = mlirGetDialectHandle__arith__();
28+
MlirDialectHandle standaloneHandle =
29+
mlirGetDialectHandle__standalone__();
30+
mlirDialectHandleRegisterDialect(arithHandle, context);
31+
mlirDialectHandleRegisterDialect(standaloneHandle, context);
2832
if (load) {
29-
mlirDialectHandleLoadDialect(handle, context);
33+
mlirDialectHandleLoadDialect(arithHandle, context);
34+
mlirDialectHandleRegisterDialect(standaloneHandle, context);
3035
}
3136
},
3237
py::arg("context") = py::none(), py::arg("load") = true);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ add_mlir_aggregate(StandaloneCAPITestLib
66
SHARED
77
EMBED_LIBS
88
MLIRCAPIIR
9-
# TODO: Remove this in favor of showing fine grained dialect registration
10-
# (once available).
11-
MLIRCAPIRegisterEverything
9+
MLIRCAPIArith
1210
StandaloneCAPI
1311
)
1412

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,12 @@
1212
#include <stdio.h>
1313

1414
#include "Standalone-c/Dialects.h"
15+
#include "mlir-c/Dialect/Arith.h"
1516
#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-
}
2417

2518
int main(int argc, char **argv) {
2619
MlirContext ctx = mlirContextCreate();
27-
// TODO: Create the dialect handles for the builtin dialects and avoid this.
28-
// This adds dozens of MB of binary size over just the standalone dialect.
29-
registerAllUpstreamDialects(ctx);
20+
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__arith__(), ctx);
3021
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__standalone__(), ctx);
3122

3223
MlirModule module = mlirModuleCreateParse(

mlir/examples/standalone/test/python/smoketest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import sys
55
from mlir_standalone.ir import *
6-
from mlir_standalone.dialects import builtin as builtin_d
76

87
if sys.argv[1] == "pybind11":
98
from mlir_standalone.dialects import standalone_pybind11 as standalone_d
@@ -14,7 +13,7 @@
1413

1514

1615
with Context():
17-
standalone_d.register_dialect()
16+
standalone_d.register_dialects()
1817
module = Module.parse(
1918
"""
2019
%0 = arith.constant 2 : i32

mlir/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ set(MLIR_TEST_DEPENDS
126126
if(NOT MLIR_STANDALONE_BUILD)
127127
list(APPEND MLIR_TEST_DEPENDS FileCheck count not split-file yaml2obj)
128128
endif()
129+
# Examples/standalone/test.toy (vis-a-vis the standalone example) depends on these.
130+
if(LLVM_INCLUDE_EXAMPLES)
131+
list(APPEND MLIR_TEST_DEPENDS MLIRCAPIArith)
132+
endif()
129133

130134
set(MLIR_TEST_DEPENDS ${MLIR_TEST_DEPENDS}
131135
mlir-capi-pdl-test

0 commit comments

Comments
 (0)