llvm-project
255 строк · 8.2 Кб
1# -*- Python -*-
2
3import os
4import platform
5import re
6import subprocess
7import tempfile
8
9import lit.formats
10import lit.util
11
12from lit.llvm import llvm_config
13from lit.llvm.subst import ToolSubst
14from lit.llvm.subst import FindTool
15
16# Configuration file for the 'lit' test runner.
17
18# name: The name of this test suite.
19config.name = "MLIR"
20
21# We prefer the lit internal shell which provides a better user experience on failures
22# unless the user explicitly disables it with LIT_USE_INTERNAL_SHELL=0 env var.
23use_lit_shell = True
24lit_shell_env = os.environ.get("LIT_USE_INTERNAL_SHELL")
25if lit_shell_env:
26use_lit_shell = not lit.util.pythonize_bool(lit_shell_env)
27
28config.test_format = lit.formats.ShTest(execute_external=not use_lit_shell)
29
30# suffixes: A list of file extensions to treat as test files.
31config.suffixes = [
32".td",
33".mlir",
34".toy",
35".ll",
36".tc",
37".py",
38".yaml",
39".test",
40".pdll",
41".c",
42]
43
44# test_source_root: The root path where tests are located.
45config.test_source_root = os.path.dirname(__file__)
46
47# test_exec_root: The root path where tests should be run.
48config.test_exec_root = os.path.join(config.mlir_obj_root, "test")
49
50config.substitutions.append(("%PATH%", config.environment["PATH"]))
51config.substitutions.append(("%shlibext", config.llvm_shlib_ext))
52config.substitutions.append(("%mlir_src_root", config.mlir_src_root))
53config.substitutions.append(("%host_cxx", config.host_cxx))
54config.substitutions.append(("%host_cc", config.host_cc))
55
56
57# Searches for a runtime library with the given name and returns the found path.
58# Correctly handles the platforms shared library directory and naming conventions.
59def find_runtime(name):
60path = ""
61for prefix in ["", "lib"]:
62path = os.path.join(
63config.llvm_shlib_dir, f"{prefix}{name}{config.llvm_shlib_ext}"
64)
65if os.path.isfile(path):
66break
67return path
68
69
70# Searches for a runtime library with the given name and returns a tool
71# substitution of the same name and the found path.
72def add_runtime(name):
73return ToolSubst(f"%{name}", find_runtime(name))
74
75
76llvm_config.with_system_environment(["HOME", "INCLUDE", "LIB", "TMP", "TEMP"])
77
78llvm_config.use_default_substitutions()
79
80# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
81# subdirectories contain auxiliary inputs for various tests in their parent
82# directories.
83config.excludes = [
84"Inputs",
85"CMakeLists.txt",
86"README.txt",
87"LICENSE.txt",
88"lit.cfg.py",
89"lit.site.cfg.py",
90]
91
92# Tweak the PATH to include the tools dir.
93llvm_config.with_environment("PATH", config.mlir_tools_dir, append_path=True)
94llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True)
95
96tool_dirs = [config.mlir_tools_dir, config.llvm_tools_dir]
97tools = [
98"mlir-tblgen",
99"mlir-translate",
100"mlir-lsp-server",
101"mlir-capi-execution-engine-test",
102"mlir-capi-ir-test",
103"mlir-capi-irdl-test",
104"mlir-capi-llvm-test",
105"mlir-capi-pass-test",
106"mlir-capi-pdl-test",
107"mlir-capi-quant-test",
108"mlir-capi-sparse-tensor-test",
109"mlir-capi-transform-test",
110"mlir-capi-transform-interpreter-test",
111"mlir-capi-translation-test",
112"mlir-cpu-runner",
113add_runtime("mlir_runner_utils"),
114add_runtime("mlir_c_runner_utils"),
115add_runtime("mlir_async_runtime"),
116add_runtime("mlir_float16_utils"),
117"mlir-linalg-ods-yaml-gen",
118"mlir-reduce",
119"mlir-pdll",
120"not",
121]
122
123if config.enable_spirv_cpu_runner:
124tools.extend(
125["mlir-spirv-cpu-runner", add_runtime("mlir_test_spirv_cpu_runner_c_wrappers")]
126)
127
128if config.enable_vulkan_runner:
129tools.extend([add_runtime("vulkan-runtime-wrappers")])
130
131if config.enable_rocm_runner:
132tools.extend([add_runtime("mlir_rocm_runtime")])
133
134if config.enable_cuda_runner:
135tools.extend([add_runtime("mlir_cuda_runtime")])
136
137if config.enable_sycl_runner:
138tools.extend([add_runtime("mlir_sycl_runtime")])
139
140if config.mlir_run_arm_sve_tests or config.mlir_run_arm_sme_tests:
141tools.extend([add_runtime("mlir_arm_runner_utils")])
142
143if config.mlir_run_arm_sme_tests:
144config.substitutions.append(
145(
146"%arm_sme_abi_shlib",
147# Use passed Arm SME ABI routines, if not present default to stubs.
148config.arm_sme_abi_routines_shlib or find_runtime("mlir_arm_sme_abi_stubs"),
149)
150)
151
152# The following tools are optional
153tools.extend(
154[
155ToolSubst("toyc-ch1", unresolved="ignore"),
156ToolSubst("toyc-ch2", unresolved="ignore"),
157ToolSubst("toyc-ch3", unresolved="ignore"),
158ToolSubst("toyc-ch4", unresolved="ignore"),
159ToolSubst("toyc-ch5", unresolved="ignore"),
160ToolSubst("toyc-ch6", unresolved="ignore"),
161ToolSubst("toyc-ch7", unresolved="ignore"),
162ToolSubst("transform-opt-ch2", unresolved="ignore"),
163ToolSubst("transform-opt-ch3", unresolved="ignore"),
164ToolSubst("transform-opt-ch4", unresolved="ignore"),
165ToolSubst("mlir-transform-opt", unresolved="ignore"),
166ToolSubst("%mlir_lib_dir", config.mlir_lib_dir, unresolved="ignore"),
167ToolSubst("%mlir_src_dir", config.mlir_src_root, unresolved="ignore"),
168]
169)
170
171python_executable = config.python_executable
172# Python configuration with sanitizer requires some magic preloading. This will only work on clang/linux.
173# TODO: detect Darwin/Windows situation (or mark these tests as unsupported on these platforms).
174if "asan" in config.available_features and "Linux" in config.host_os:
175python_executable = f"LD_PRELOAD=$({config.host_cxx} -print-file-name=libclang_rt.asan-{config.host_arch}.so) {config.python_executable}"
176# On Windows the path to python could contains spaces in which case it needs to be provided in quotes.
177# This is the equivalent of how %python is setup in llvm/utils/lit/lit/llvm/config.py.
178elif "Windows" in config.host_os:
179python_executable = '"%s"' % (python_executable)
180tools.extend(
181[
182ToolSubst("%PYTHON", python_executable, unresolved="ignore"),
183]
184)
185
186if "MLIR_OPT_CHECK_IR_ROUNDTRIP" in os.environ:
187tools.extend(
188[
189ToolSubst("mlir-opt", "mlir-opt --verify-roundtrip", unresolved="fatal"),
190]
191)
192else:
193tools.extend(["mlir-opt"])
194
195llvm_config.add_tool_substitutions(tools, tool_dirs)
196
197
198# FileCheck -enable-var-scope is enabled by default in MLIR test
199# This option avoids to accidentally reuse variable across -LABEL match,
200# it can be explicitly opted-in by prefixing the variable name with $
201config.environment["FILECHECK_OPTS"] = "-enable-var-scope --allow-unused-prefixes=false"
202
203# Add the python path for both the source and binary tree.
204# Note that presently, the python sources come from the source tree and the
205# binaries come from the build tree. This should be unified to the build tree
206# by copying/linking sources to build.
207if config.enable_bindings_python:
208config.environment["PYTHONPATH"] = os.getenv("MLIR_LIT_PYTHONPATH", "")
209llvm_config.with_environment(
210"PYTHONPATH",
211[
212os.path.join(config.mlir_obj_root, "python_packages", "mlir_core"),
213os.path.join(config.mlir_obj_root, "python_packages", "mlir_test"),
214],
215append_path=True,
216)
217
218if config.enable_assertions:
219config.available_features.add("asserts")
220else:
221config.available_features.add("noasserts")
222
223
224def have_host_jit_feature_support(feature_name):
225mlir_cpu_runner_exe = lit.util.which("mlir-cpu-runner", config.mlir_tools_dir)
226
227if not mlir_cpu_runner_exe:
228return False
229
230try:
231mlir_cpu_runner_cmd = subprocess.Popen(
232[mlir_cpu_runner_exe, "--host-supports-" + feature_name],
233stdout=subprocess.PIPE,
234)
235except OSError:
236print("could not exec mlir-cpu-runner")
237return False
238
239mlir_cpu_runner_out = mlir_cpu_runner_cmd.stdout.read().decode("ascii")
240mlir_cpu_runner_cmd.wait()
241
242return "true" in mlir_cpu_runner_out
243
244
245if have_host_jit_feature_support("jit"):
246config.available_features.add("host-supports-jit")
247
248if config.run_nvptx_tests:
249config.available_features.add("host-supports-nvptx")
250
251if config.run_rocm_tests:
252config.available_features.add("host-supports-amdgpu")
253
254if config.arm_emulator_executable:
255config.available_features.add("arm-emulator")
256