Skip to content

Commit 1ea7ced

Browse files
authored
[mlir][py] Enable disabling loading all registered (#117643)
There is a pending todo about always eagerly loading or not. Make this behavior optional and give the control to the user in a backwards compatible manner. This is made optional as there were arguments for both forms, kept it in form that is backwards compatible.
1 parent 32432a6 commit 1ea7ced

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

mlir/python/mlir/_mlir_libs/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,16 @@ def _site_initialize():
8080
logger = logging.getLogger(__name__)
8181
post_init_hooks = []
8282
disable_multithreading = False
83+
# This flag disables eagerly loading all dialects. Eagerly loading is often
84+
# not the desired behavior (see
85+
# https://github.com/llvm/llvm-project/issues/56037), and the logic is that
86+
# if any module has this attribute set, then we don't load all (e.g., it's
87+
# being used in a solution where the loading is controlled).
88+
disable_load_all_available_dialects = False
8389

8490
def process_initializer_module(module_name):
8591
nonlocal disable_multithreading
92+
nonlocal disable_load_all_available_dialects
8693
try:
8794
m = importlib.import_module(f".{module_name}", __name__)
8895
except ModuleNotFoundError:
@@ -107,6 +114,8 @@ def process_initializer_module(module_name):
107114
if bool(m.disable_multithreading):
108115
logger.debug("Disabling multi-threading for context")
109116
disable_multithreading = True
117+
if hasattr(m, "disable_load_all_available_dialects"):
118+
disable_load_all_available_dialects = True
110119
return True
111120

112121
# If _mlirRegisterEverything is built, then include it as an initializer
@@ -130,10 +139,8 @@ def __init__(self, *args, **kwargs):
130139
hook(self)
131140
if not disable_multithreading:
132141
self.enable_multithreading(True)
133-
# TODO: There is some debate about whether we should eagerly load
134-
# all dialects. It is being done here in order to preserve existing
135-
# behavior. See: https://github.com/llvm/llvm-project/issues/56037
136-
self.load_all_available_dialects()
142+
if not disable_load_all_available_dialects:
143+
self.load_all_available_dialects()
137144
if init_module:
138145
logger.debug(
139146
"Registering translations from initializer %r", init_module

0 commit comments

Comments
 (0)