[mlir][py] Enable loading only specified dialects during creation. (#121421)
[llvm-project.git] / mlir / test / lit.cfg.py
blobf162f9a00efa7c7630a22b011314c4b1ea14318b
1 # -*- Python -*-
3 import os
4 import platform
5 import re
6 import shutil
7 import subprocess
8 import tempfile
10 import lit.formats
11 import lit.util
13 from lit.llvm import llvm_config
14 from lit.llvm.subst import ToolSubst
15 from lit.llvm.subst import FindTool
17 # Configuration file for the 'lit' test runner.
19 # name: The name of this test suite.
20 config.name = "MLIR"
22 # TODO: Consolidate the logic for turning on the internal shell by default for all LLVM test suites.
23 # See https://github.com/llvm/llvm-project/issues/106636 for more details.
25 # We prefer the lit internal shell which provides a better user experience on failures
26 # unless the user explicitly disables it with LIT_USE_INTERNAL_SHELL=0 env var.
27 use_lit_shell = True
28 lit_shell_env = os.environ.get("LIT_USE_INTERNAL_SHELL")
29 if lit_shell_env:
30 use_lit_shell = lit.util.pythonize_bool(lit_shell_env)
32 config.test_format = lit.formats.ShTest(execute_external=not use_lit_shell)
34 # suffixes: A list of file extensions to treat as test files.
35 config.suffixes = [
36 ".td",
37 ".mlir",
38 ".toy",
39 ".ll",
40 ".tc",
41 ".py",
42 ".yaml",
43 ".test",
44 ".pdll",
45 ".c",
48 # test_source_root: The root path where tests are located.
49 config.test_source_root = os.path.dirname(__file__)
51 # test_exec_root: The root path where tests should be run.
52 config.test_exec_root = os.path.join(config.mlir_obj_root, "test")
54 config.substitutions.append(("%PATH%", config.environment["PATH"]))
55 config.substitutions.append(("%shlibext", config.llvm_shlib_ext))
56 config.substitutions.append(("%llvm_src_root", config.llvm_src_root))
57 config.substitutions.append(("%mlir_src_root", config.mlir_src_root))
58 config.substitutions.append(("%host_cxx", config.host_cxx))
59 config.substitutions.append(("%host_cc", config.host_cc))
62 # Searches for a runtime library with the given name and returns the found path.
63 # Correctly handles the platforms shared library directory and naming conventions.
64 def find_runtime(name):
65 path = ""
66 for prefix in ["", "lib"]:
67 path = os.path.join(
68 config.llvm_shlib_dir, f"{prefix}{name}{config.llvm_shlib_ext}"
70 if os.path.isfile(path):
71 break
72 return path
75 # Searches for a runtime library with the given name and returns a tool
76 # substitution of the same name and the found path.
77 def add_runtime(name):
78 return ToolSubst(f"%{name}", find_runtime(name))
81 # Provide the path to asan runtime lib 'libclang_rt.asan_osx_dynamic.dylib' if
82 # available. This is darwin specific since it's currently only needed on darwin.
83 # Stolen from llvm/test/lit.cfg.py with a few modifications
84 def get_asan_rtlib():
85 if not "asan" in config.available_features or not "Darwin" in config.host_os:
86 return ""
87 # Find the asan rt lib
88 resource_dir = (
89 subprocess.check_output([config.host_cc.strip(), "-print-resource-dir"])
90 .decode("utf-8")
91 .strip()
93 return os.path.join(
94 resource_dir, "lib", "darwin", "libclang_rt.asan_osx_dynamic.dylib"
98 # On macOS, we can't do the DYLD_INSERT_LIBRARIES trick with a shim python
99 # binary as the ASan interceptors get loaded too late. Also, when SIP is
100 # enabled, we can't inject libraries into system binaries at all, so we need a
101 # copy of the "real" python to work with.
102 # Stolen from lldb/test/API/lit.cfg.py with a few modifications
103 def find_real_python_interpreter():
104 # If we're running in a virtual environment, we have to copy Python into
105 # the virtual environment for it to work.
106 if sys.prefix != sys.base_prefix:
107 copied_python = os.path.join(sys.prefix, "bin", "copied-python")
108 else:
109 copied_python = os.path.join(config.lldb_build_directory, "copied-python")
111 # Avoid doing any work if we already copied the binary.
112 if os.path.isfile(copied_python):
113 return copied_python
115 # Find the "real" python binary.
116 real_python = (
117 subprocess.check_output(
119 config.python_executable,
120 os.path.join(
121 os.path.dirname(os.path.realpath(__file__)),
122 "get_darwin_real_python.py",
126 .decode("utf-8")
127 .strip()
130 shutil.copy(real_python, copied_python)
132 # Now make sure the copied Python works. The Python in Xcode has a relative
133 # RPATH and cannot be copied.
134 try:
135 # We don't care about the output, just make sure it runs.
136 subprocess.check_call([copied_python, "-V"])
137 except subprocess.CalledProcessError:
138 # The copied Python didn't work. Assume we're dealing with the Python
139 # interpreter in Xcode. Given that this is not a system binary SIP
140 # won't prevent us form injecting the interceptors, but when running in
141 # a virtual environment, we can't use it directly. Create a symlink
142 # instead.
143 os.remove(copied_python)
144 os.symlink(real_python, copied_python)
146 # The copied Python works.
147 return copied_python
150 llvm_config.with_system_environment(["HOME", "INCLUDE", "LIB", "TMP", "TEMP"])
152 llvm_config.use_default_substitutions()
154 # excludes: A list of directories to exclude from the testsuite. The 'Inputs'
155 # subdirectories contain auxiliary inputs for various tests in their parent
156 # directories.
157 config.excludes = [
158 "Inputs",
159 "CMakeLists.txt",
160 "README.txt",
161 "LICENSE.txt",
162 "lit.cfg.py",
163 "lit.site.cfg.py",
164 "get_darwin_real_python.py",
167 # Tweak the PATH to include the tools dir.
168 llvm_config.with_environment("PATH", config.mlir_tools_dir, append_path=True)
169 llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True)
171 tool_dirs = [config.mlir_tools_dir, config.llvm_tools_dir]
172 tools = [
173 "mlir-tblgen",
174 "mlir-translate",
175 "mlir-lsp-server",
176 "mlir-capi-execution-engine-test",
177 "mlir-capi-ir-test",
178 "mlir-capi-irdl-test",
179 "mlir-capi-llvm-test",
180 "mlir-capi-pass-test",
181 "mlir-capi-pdl-test",
182 "mlir-capi-quant-test",
183 "mlir-capi-rewrite-test",
184 "mlir-capi-sparse-tensor-test",
185 "mlir-capi-transform-test",
186 "mlir-capi-transform-interpreter-test",
187 "mlir-capi-translation-test",
188 "mlir-cpu-runner",
189 add_runtime("mlir_runner_utils"),
190 add_runtime("mlir_c_runner_utils"),
191 add_runtime("mlir_async_runtime"),
192 add_runtime("mlir_float16_utils"),
193 "mlir-linalg-ods-yaml-gen",
194 "mlir-reduce",
195 "mlir-pdll",
196 "not",
199 if config.enable_vulkan_runner:
200 tools.extend([add_runtime("vulkan-runtime-wrappers")])
202 if config.enable_rocm_runner:
203 tools.extend([add_runtime("mlir_rocm_runtime")])
205 if config.enable_cuda_runner:
206 tools.extend([add_runtime("mlir_cuda_runtime")])
208 if config.enable_sycl_runner:
209 tools.extend([add_runtime("mlir_sycl_runtime")])
211 if config.enable_spirv_cpu_runner:
212 tools.extend([add_runtime("mlir_spirv_cpu_runtime")])
214 if config.mlir_run_arm_sve_tests or config.mlir_run_arm_sme_tests:
215 tools.extend([add_runtime("mlir_arm_runner_utils")])
217 if config.mlir_run_arm_sme_tests:
218 config.substitutions.append(
220 "%arm_sme_abi_shlib",
221 # Use passed Arm SME ABI routines, if not present default to stubs.
222 config.arm_sme_abi_routines_shlib or find_runtime("mlir_arm_sme_abi_stubs"),
226 # The following tools are optional
227 tools.extend(
229 ToolSubst("toyc-ch1", unresolved="ignore"),
230 ToolSubst("toyc-ch2", unresolved="ignore"),
231 ToolSubst("toyc-ch3", unresolved="ignore"),
232 ToolSubst("toyc-ch4", unresolved="ignore"),
233 ToolSubst("toyc-ch5", unresolved="ignore"),
234 ToolSubst("toyc-ch6", unresolved="ignore"),
235 ToolSubst("toyc-ch7", unresolved="ignore"),
236 ToolSubst("transform-opt-ch2", unresolved="ignore"),
237 ToolSubst("transform-opt-ch3", unresolved="ignore"),
238 ToolSubst("transform-opt-ch4", unresolved="ignore"),
239 ToolSubst("mlir-transform-opt", unresolved="ignore"),
240 ToolSubst("%mlir_lib_dir", config.mlir_lib_dir, unresolved="ignore"),
241 ToolSubst("%mlir_src_dir", config.mlir_src_root, unresolved="ignore"),
245 python_executable = config.python_executable
246 # Python configuration with sanitizer requires some magic preloading. This will only work on clang/linux/darwin.
247 # TODO: detect Windows situation (or mark these tests as unsupported on these platforms).
248 if "asan" in config.available_features:
249 if "Linux" in config.host_os:
250 python_executable = f"LD_PRELOAD=$({config.host_cxx} -print-file-name=libclang_rt.asan-{config.host_arch}.so) {config.python_executable}"
251 if "Darwin" in config.host_os:
252 # Ensure we use a non-shim Python executable, for the `DYLD_INSERT_LIBRARIES`
253 # env variable to take effect
254 real_python_executable = find_real_python_interpreter()
255 if real_python_executable:
256 python_executable = real_python_executable
257 lit_config.note(
258 "Using {} instead of {}".format(
259 python_executable, config.python_executable
263 asan_rtlib = get_asan_rtlib()
264 lit_config.note("Using ASan rtlib {}".format(asan_rtlib))
265 config.environment["MallocNanoZone"] = "0"
266 config.environment["ASAN_OPTIONS"] = "detect_stack_use_after_return=1"
267 config.environment["DYLD_INSERT_LIBRARIES"] = asan_rtlib
270 # On Windows the path to python could contains spaces in which case it needs to be provided in quotes.
271 # This is the equivalent of how %python is setup in llvm/utils/lit/lit/llvm/config.py.
272 elif "Windows" in config.host_os:
273 python_executable = '"%s"' % (python_executable)
274 tools.extend(
276 ToolSubst("%PYTHON", python_executable, unresolved="ignore"),
280 if "MLIR_OPT_CHECK_IR_ROUNDTRIP" in os.environ:
281 tools.extend(
283 ToolSubst("mlir-opt", "mlir-opt --verify-roundtrip", unresolved="fatal"),
286 else:
287 tools.extend(["mlir-opt"])
289 llvm_config.add_tool_substitutions(tools, tool_dirs)
292 # FileCheck -enable-var-scope is enabled by default in MLIR test
293 # This option avoids to accidentally reuse variable across -LABEL match,
294 # it can be explicitly opted-in by prefixing the variable name with $
295 config.environment["FILECHECK_OPTS"] = "-enable-var-scope --allow-unused-prefixes=false"
297 # Add the python path for both the source and binary tree.
298 # Note that presently, the python sources come from the source tree and the
299 # binaries come from the build tree. This should be unified to the build tree
300 # by copying/linking sources to build.
301 if config.enable_bindings_python:
302 config.environment["PYTHONPATH"] = os.getenv("MLIR_LIT_PYTHONPATH", "")
303 llvm_config.with_environment(
304 "PYTHONPATH",
306 os.path.join(config.mlir_obj_root, "python_packages", "mlir_core"),
307 os.path.join(config.mlir_obj_root, "python_packages", "mlir_test"),
309 append_path=True,
312 if config.enable_assertions:
313 config.available_features.add("asserts")
314 else:
315 config.available_features.add("noasserts")
318 def have_host_jit_feature_support(feature_name):
319 mlir_cpu_runner_exe = lit.util.which("mlir-cpu-runner", config.mlir_tools_dir)
321 if not mlir_cpu_runner_exe:
322 return False
324 try:
325 mlir_cpu_runner_cmd = subprocess.Popen(
326 [mlir_cpu_runner_exe, "--host-supports-" + feature_name],
327 stdout=subprocess.PIPE,
329 except OSError:
330 print("could not exec mlir-cpu-runner")
331 return False
333 mlir_cpu_runner_out = mlir_cpu_runner_cmd.stdout.read().decode("ascii")
334 mlir_cpu_runner_cmd.wait()
336 return "true" in mlir_cpu_runner_out
339 if have_host_jit_feature_support("jit"):
340 config.available_features.add("host-supports-jit")
342 if config.run_nvptx_tests:
343 config.available_features.add("host-supports-nvptx")
345 if config.run_rocm_tests:
346 config.available_features.add("host-supports-amdgpu")
348 if config.arm_emulator_executable:
349 config.available_features.add("arm-emulator")