Skip to content

Commit 49e0dfb

Browse files
committed
move append_dialect_search_prefix test
1 parent 07aeb5d commit 49e0dfb

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

mlir/lib/Bindings/Python/MainModule.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ PYBIND11_MODULE(_mlir, m) {
3636
self.getDialectSearchPrefixes().push_back(std::move(moduleName));
3737
},
3838
"module_name"_a)
39+
.def(
40+
"_check_dialect_module_loaded",
41+
[](PyGlobals &self, const std::string &dialectNamespace) {
42+
return self.loadDialectModule(dialectNamespace);
43+
},
44+
"dialect_namespace"_a)
3945
.def("_register_dialect_impl", &PyGlobals::registerDialectImpl,
4046
"dialect_namespace"_a, "dialect_class"_a,
4147
"Testing hook for directly registering a dialect")

mlir/python/mlir/_mlir_libs/_mlir/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class _Globals:
77
def _register_dialect_impl(self, dialect_namespace: str, dialect_class: type) -> None: ...
88
def _register_operation_impl(self, operation_name: str, operation_class: type) -> None: ...
99
def append_dialect_search_prefix(self, module_name: str) -> None: ...
10+
def _check_dialect_module_loaded(self, dialect_namespace: str) -> bool: ...
1011

1112
def register_dialect(dialect_class: type) -> object: ...
1213
def register_operation(dialect_class: type) -> object: ...
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# The purpose of this empty dialect module is to enable successfully loading the "custom" dialect.
2+
# Without this file here (and a corresponding _cext.globals.append_dialect_search_prefix("custom_dialect")),
3+
# PyGlobals::loadDialectModule would search and fail to find the "custom" dialect for each Operation.create("custom.op")
4+
# (amongst other things).

mlir/test/python/ir/dialects.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# RUN: %PYTHON %s | FileCheck %s
22

33
import gc
4+
import sys
45
from mlir.ir import *
6+
from mlir.dialects._ods_common import _cext
57

68

79
def run(f):
@@ -104,3 +106,18 @@ def testIsRegisteredOperation():
104106
print(f"cf.cond_br: {ctx.is_registered_operation('cf.cond_br')}")
105107
# CHECK: func.not_existing: False
106108
print(f"func.not_existing: {ctx.is_registered_operation('func.not_existing')}")
109+
110+
111+
# CHECK-LABEL: TEST: testAppendPrefixSearchPath
112+
@run
113+
def testAppendPrefixSearchPath():
114+
ctx = Context()
115+
ctx.allow_unregistered_dialects = True
116+
with Location.unknown(ctx):
117+
assert not _cext.globals._check_dialect_module_loaded("custom")
118+
Operation.create("custom.op")
119+
assert not _cext.globals._check_dialect_module_loaded("custom")
120+
121+
sys.path.append(".")
122+
_cext.globals.append_dialect_search_prefix("custom_dialect")
123+
assert _cext.globals._check_dialect_module_loaded("custom")

mlir/test/python/ir/insertion_point.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
# RUN: %PYTHON %s | FileCheck %s
22

33
import gc
4-
import io
5-
import itertools
6-
import sys
7-
84
from mlir.ir import *
9-
from mlir.dialects._ods_common import _cext
10-
11-
sys.path.append(".")
12-
_cext.globals.append_dialect_search_prefix("custom_dialect")
135

146

157
def run(f):

0 commit comments

Comments
 (0)