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.
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.
28 lit_shell_env
= os
.environ
.get("LIT_USE_INTERNAL_SHELL")
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.
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
):
66 for prefix
in ["", "lib"]:
68 config
.llvm_shlib_dir
, f
"{prefix}{name}{config.llvm_shlib_ext}"
70 if os
.path
.isfile(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
85 if not "asan" in config
.available_features
or not "Darwin" in config
.host_os
:
87 # Find the asan rt lib
89 subprocess
.check_output([config
.host_cc
.strip(), "-print-resource-dir"])
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")
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
):
115 # Find the "real" python binary.
117 subprocess
.check_output(
119 config
.python_executable
,
121 os
.path
.dirname(os
.path
.realpath(__file__
)),
122 "get_darwin_real_python.py",
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.
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
143 os
.remove(copied_python
)
144 os
.symlink(real_python
, copied_python
)
146 # The copied Python works.
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
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
]
176 "mlir-capi-execution-engine-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",
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",
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
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
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
)
276 ToolSubst("%PYTHON", python_executable
, unresolved
="ignore"),
280 if "MLIR_OPT_CHECK_IR_ROUNDTRIP" in os
.environ
:
283 ToolSubst("mlir-opt", "mlir-opt --verify-roundtrip", unresolved
="fatal"),
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(
306 os
.path
.join(config
.mlir_obj_root
, "python_packages", "mlir_core"),
307 os
.path
.join(config
.mlir_obj_root
, "python_packages", "mlir_test"),
312 if config
.enable_assertions
:
313 config
.available_features
.add("asserts")
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
:
325 mlir_cpu_runner_cmd
= subprocess
.Popen(
326 [mlir_cpu_runner_exe
, "--host-supports-" + feature_name
],
327 stdout
=subprocess
.PIPE
,
330 print("could not exec mlir-cpu-runner")
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")