Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 4cdf6d7

Browse files
authored
[mlir][python] enable registering dialects with the default Context (#72488)
1 parent 9356e17 commit 4cdf6d7

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

mlir/python/mlir/_mlir_libs/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,28 @@ def get_include_dirs() -> Sequence[str]:
5656
#
5757
# This facility allows downstreams to customize Context creation to their
5858
# needs.
59+
60+
_dialect_registry = None
61+
62+
63+
def get_dialect_registry():
64+
global _dialect_registry
65+
66+
if _dialect_registry is None:
67+
from ._mlir import ir
68+
69+
_dialect_registry = ir.DialectRegistry()
70+
71+
return _dialect_registry
72+
73+
5974
def _site_initialize():
6075
import importlib
6176
import itertools
6277
import logging
6378
from ._mlir import ir
6479

6580
logger = logging.getLogger(__name__)
66-
registry = ir.DialectRegistry()
6781
post_init_hooks = []
6882
disable_multithreading = False
6983

@@ -84,7 +98,7 @@ def process_initializer_module(module_name):
8498
logger.debug("Initializing MLIR with module: %s", module_name)
8599
if hasattr(m, "register_dialects"):
86100
logger.debug("Registering dialects from initializer %r", m)
87-
m.register_dialects(registry)
101+
m.register_dialects(get_dialect_registry())
88102
if hasattr(m, "context_init_hook"):
89103
logger.debug("Adding context init hook from %r", m)
90104
post_init_hooks.append(m.context_init_hook)
@@ -110,7 +124,7 @@ def process_initializer_module(module_name):
110124
class Context(ir._BaseContext):
111125
def __init__(self, *args, **kwargs):
112126
super().__init__(*args, **kwargs)
113-
self.append_dialect_registry(registry)
127+
self.append_dialect_registry(get_dialect_registry())
114128
for hook in post_init_hooks:
115129
hook(self)
116130
if not disable_multithreading:

mlir/python/mlir/dialects/python_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212

1313

14-
def register_python_test_dialect(context, load=True):
14+
def register_python_test_dialect(registry):
1515
from .._mlir_libs import _mlirPythonTest
1616

17-
_mlirPythonTest.register_python_test_dialect(context, load)
17+
_mlirPythonTest.register_dialect(registry)

mlir/python/mlir/ir.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ._mlir_libs._mlir.ir import *
66
from ._mlir_libs._mlir.ir import _GlobalDebug
77
from ._mlir_libs._mlir import register_type_caster, register_value_caster
8+
from ._mlir_libs import get_dialect_registry
89

910

1011
# Convenience decorator for registering user-friendly Attribute builders.

0 commit comments

Comments
 (0)