Debian Codegraph @

Tools/jit/_llvm.py

raw
1 """Utilities for invoking LLVM tools."""
2
3 import asyncio
4 import functools
5 import os
6 import re
7 import shlex
8 import subprocess
9 import typing
10
11 _LLVM_VERSION = 18
12 _LLVM_VERSION_PATTERN = re.compile(
13 rf"(?<!Apple )(LLVM|clang) version\s+{_LLVM_VERSION}\.\d+\.\d+\S*\s+"
14 )
15
16 _P = typing.ParamSpec("_P")
17 _R = typing.TypeVar("_R")
18 _C = typing.Callable[_P, typing.Awaitable[_R]]
19
20
21 def _async_cache(f: _C[_P, _R]) -> _C[_P, _R]:
22 cache = {}
23 lock = asyncio.Lock()
24
25 @functools.wraps(f)
26 async def wrapper(
27 *args: _P.args, **kwargs: _P.kwargs # pylint: disable = no-member
28 ) -> _R:
29 async with lock:
30 if args not in cache:
31 cache[args] = await f(*args, **kwargs)
32 return cache[args]
33
34 return wrapper
35
36
37 _CORES = asyncio.BoundedSemaphore(os.cpu_count() or 1)
38
39
40 async def _run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str | None:
41 command = [tool, *args]
42 async with _CORES:
43 if echo:
44 print(shlex.join(command))
45 try:
46 process = await asyncio.create_subprocess_exec(
47 *command, stdout=subprocess.PIPE
48 )
49 except FileNotFoundError:
50 return None
51 out, _ = await process.communicate()
52 if process.returncode:
53 raise RuntimeError(f"{tool} exited with return code {process.returncode}")
54 return out.decode()
55
56
57 @_async_cache
58 async def _check_tool_version(name: str, *, echo: bool = False) -> bool:
59 output = await _run(name, ["--version"], echo=echo)
60 return bool(output and _LLVM_VERSION_PATTERN.search(output))
61
62
63 @_async_cache
64 async def _get_brew_llvm_prefix(*, echo: bool = False) -> str | None:
65 output = await _run("brew", ["--prefix", f"llvm@{_LLVM_VERSION}"], echo=echo)
66 return output and output.removesuffix("\n")
67
68
69 @_async_cache
70 async def _find_tool(tool: str, *, echo: bool = False) -> str | None:
71 # Unversioned executables:
72 path = tool
73 if await _check_tool_version(path, echo=echo):
74 return path
75 # Versioned executables:
76 path = f"{tool}-{_LLVM_VERSION}"
77 if await _check_tool_version(path, echo=echo):
78 return path
79 # Homebrew-installed executables:
80 prefix = await _get_brew_llvm_prefix(echo=echo)
81 if prefix is not None:
82 path = os.path.join(prefix, "bin", tool)
83 if await _check_tool_version(path, echo=echo):
84 return path
85 # Nothing found:
86 return None
87
88
89 async def maybe_run(
90 tool: str, args: typing.Iterable[str], echo: bool = False
91 ) -> str | None:
92 """Run an LLVM tool if it can be found. Otherwise, return None."""
93 path = await _find_tool(tool, echo=echo)
94 return path and await _run(path, args, echo=echo)
95
96
97 async def run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str:
98 """Run an LLVM tool if it can be found. Otherwise, raise RuntimeError."""
99 output = await maybe_run(tool, args, echo=echo)
100 if output is None:
101 raise RuntimeError(f"Can't find {tool}-{_LLVM_VERSION}!")
102 return output