Debian Codegraph @

Tools/jit/_targets.py

raw
1 """Target-specific code generation, parsing, and processing."""
2
3 import asyncio
4 import dataclasses
5 import hashlib
6 import json
7 import os
8 import pathlib
9 import re
10 import sys
11 import tempfile
12 import typing
13
14 import _llvm
15 import _schema
16 import _stencils
17 import _writer
18
19 if sys.version_info < (3, 11):
20 raise RuntimeError("Building the JIT compiler requires Python 3.11 or newer!")
21
22 TOOLS_JIT_BUILD = pathlib.Path(__file__).resolve()
23 TOOLS_JIT = TOOLS_JIT_BUILD.parent
24 TOOLS = TOOLS_JIT.parent
25 CPYTHON = TOOLS.parent
26 PYTHON_EXECUTOR_CASES_C_H = CPYTHON / "Python" / "executor_cases.c.h"
27 TOOLS_JIT_TEMPLATE_C = TOOLS_JIT / "template.c"
28
29
30 _S = typing.TypeVar("_S", _schema.COFFSection, _schema.ELFSection, _schema.MachOSection)
31 _R = typing.TypeVar(
32 "_R", _schema.COFFRelocation, _schema.ELFRelocation, _schema.MachORelocation
33 )
34
35
36 @dataclasses.dataclass
37 class _Target(typing.Generic[_S, _R]):
38 triple: str
39 _: dataclasses.KW_ONLY
40 alignment: int = 1
41 args: typing.Sequence[str] = ()
42 ghccc: bool = False
43 prefix: str = ""
44 stable: bool = False
45 debug: bool = False
46 verbose: bool = False
47
48 def _compute_digest(self, out: pathlib.Path) -> str:
49 hasher = hashlib.sha256()
50 hasher.update(self.triple.encode())
51 hasher.update(self.debug.to_bytes())
52 # These dependencies are also reflected in _JITSources in regen.targets:
53 hasher.update(PYTHON_EXECUTOR_CASES_C_H.read_bytes())
54 hasher.update((out / "pyconfig.h").read_bytes())
55 for dirpath, _, filenames in sorted(os.walk(TOOLS_JIT)):
56 # Exclude cache files from digest computation to ensure reproducible builds.
57 if dirpath.endswith("__pycache__"):
58 continue
59 for filename in sorted(filenames):
60 hasher.update(pathlib.Path(dirpath, filename).read_bytes())
61 return hasher.hexdigest()
62
63 async def _parse(self, path: pathlib.Path) -> _stencils.StencilGroup:
64 group = _stencils.StencilGroup()
65 args = ["--disassemble", "--reloc", f"{path}"]
66 output = await _llvm.maybe_run("llvm-objdump", args, echo=self.verbose)
67 if output is not None:
68 # Make sure that full paths don't leak out (for reproducibility):
69 long, short = str(path), str(path.name)
70 group.code.disassembly.extend(
71 line.expandtabs().strip().replace(long, short)
72 for line in output.splitlines()
73 if not line.isspace()
74 )
75 args = [
76 "--elf-output-style=JSON",
77 "--expand-relocs",
78 # "--pretty-print",
79 "--section-data",
80 "--section-relocations",
81 "--section-symbols",
82 "--sections",
83 f"{path}",
84 ]
85 output = await _llvm.run("llvm-readobj", args, echo=self.verbose)
86 # --elf-output-style=JSON is only *slightly* broken on Mach-O...
87 output = output.replace("PrivateExtern\n", "\n")
88 output = output.replace("Extern\n", "\n")
89 # ...and also COFF:
90 output = output[output.index("[", 1, None) :]
91 output = output[: output.rindex("]", None, -1) + 1]
92 sections: list[dict[typing.Literal["Section"], _S]] = json.loads(output)
93 for wrapped_section in sections:
94 self._handle_section(wrapped_section["Section"], group)
95 # The trampoline's entry point is just named "_ENTRY", since on some
96 # platforms we later assume that any function starting with "_JIT_" uses
97 # the GHC calling convention:
98 entry_symbol = "_JIT_ENTRY" if "_JIT_ENTRY" in group.symbols else "_ENTRY"
99 assert group.symbols[entry_symbol] == (_stencils.HoleValue.CODE, 0)
100 if group.data.body:
101 line = f"0: {str(bytes(group.data.body)).removeprefix('b')}"
102 group.data.disassembly.append(line)
103 group.process_relocations(alignment=self.alignment)
104 return group
105
106 def _handle_section(self, section: _S, group: _stencils.StencilGroup) -> None:
107 raise NotImplementedError(type(self))
108
109 def _handle_relocation(
110 self, base: int, relocation: _R, raw: bytearray
111 ) -> _stencils.Hole:
112 raise NotImplementedError(type(self))
113
114 async def _compile(
115 self, opname: str, c: pathlib.Path, tempdir: pathlib.Path
116 ) -> _stencils.StencilGroup:
117 # "Compile" the trampoline to an empty stencil group if it's not needed:
118 if opname == "trampoline" and not self.ghccc:
119 return _stencils.StencilGroup()
120 o = tempdir / f"{opname}.o"
121 args = [
122 f"--target={self.triple}",
123 "-DPy_BUILD_CORE_MODULE",
124 "-D_DEBUG" if self.debug else "-DNDEBUG",
125 f"-D_JIT_OPCODE={opname}",
126 "-D_PyJIT_ACTIVE",
127 "-D_Py_JIT",
128 "-I.",
129 f"-I{CPYTHON / 'Include'}",
130 f"-I{CPYTHON / 'Include' / 'internal'}",
131 f"-I{CPYTHON / 'Include' / 'internal' / 'mimalloc'}",
132 f"-I{CPYTHON / 'Python'}",
133 "-O3",
134 "-c",
135 # This debug info isn't necessary, and bloats out the JIT'ed code.
136 # We *may* be able to re-enable this, process it, and JIT it for a
137 # nicer debugging experience... but that needs a lot more research:
138 "-fno-asynchronous-unwind-tables",
139 # Don't call built-in functions that we can't find or patch:
140 "-fno-builtin",
141 # Emit relaxable 64-bit calls/jumps, so we don't have to worry about
142 # about emitting in-range trampolines for out-of-range targets.
143 # We can probably remove this and emit trampolines in the future:
144 "-fno-plt",
145 # Don't call stack-smashing canaries that we can't find or patch:
146 "-fno-stack-protector",
147 "-std=c11",
148 *self.args,
149 ]
150 if self.ghccc:
151 # This is a bit of an ugly workaround, but it makes the code much
152 # smaller and faster, so it's worth it. We want to use the GHC
153 # calling convention, but Clang doesn't support it. So, we *first*
154 # compile the code to LLVM IR, perform some text replacements on the
155 # IR to change the calling convention(!), and then compile *that*.
156 # Once we have access to Clang 19, we can get rid of this and use
157 # __attribute__((preserve_none)) directly in the C code instead:
158 ll = tempdir / f"{opname}.ll"
159 args_ll = args + [
160 # -fomit-frame-pointer is necessary because the GHC calling
161 # convention uses RBP to pass arguments:
162 "-S",
163 "-emit-llvm",
164 "-fomit-frame-pointer",
165 "-o",
166 f"{ll}",
167 f"{c}",
168 ]
169 await _llvm.run("clang", args_ll, echo=self.verbose)
170 ir = ll.read_text()
171 # This handles declarations, definitions, and calls to named symbols
172 # starting with "_JIT_":
173 ir = re.sub(
174 r"(((noalias|nonnull|noundef) )*ptr @_JIT_\w+\()", r"ghccc \1", ir
175 )
176 # This handles calls to anonymous callees, since anything with
177 # "musttail" needs to use the same calling convention:
178 ir = ir.replace("musttail call", "musttail call ghccc")
179 # Sometimes *both* replacements happen at the same site, so fix it:
180 ir = ir.replace("ghccc ghccc", "ghccc")
181 ll.write_text(ir)
182 args_o = args + ["-Wno-unused-command-line-argument", "-o", f"{o}", f"{ll}"]
183 else:
184 args_o = args + ["-o", f"{o}", f"{c}"]
185 await _llvm.run("clang", args_o, echo=self.verbose)
186 return await self._parse(o)
187
188 async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:
189 generated_cases = PYTHON_EXECUTOR_CASES_C_H.read_text()
190 opnames = sorted(re.findall(r"\n {8}case (\w+): \{\n", generated_cases))
191 tasks = []
192 with tempfile.TemporaryDirectory() as tempdir:
193 work = pathlib.Path(tempdir).resolve()
194 async with asyncio.TaskGroup() as group:
195 coro = self._compile("trampoline", TOOLS_JIT / "trampoline.c", work)
196 tasks.append(group.create_task(coro, name="trampoline"))
197 for opname in opnames:
198 coro = self._compile(opname, TOOLS_JIT_TEMPLATE_C, work)
199 tasks.append(group.create_task(coro, name=opname))
200 return {task.get_name(): task.result() for task in tasks}
201
202 def build(
203 self, out: pathlib.Path, *, comment: str = "", force: bool = False
204 ) -> None:
205 """Build jit_stencils.h in the given directory."""
206 if not self.stable:
207 warning = f"JIT support for {self.triple} is still experimental!"
208 request = "Please report any issues you encounter.".center(len(warning))
209 outline = "=" * len(warning)
210 print("\n".join(["", outline, warning, request, outline, ""]))
211 digest = f"// {self._compute_digest(out)}\n"
212 jit_stencils = out / "jit_stencils.h"
213 if (
214 not force
215 and jit_stencils.exists()
216 and jit_stencils.read_text().startswith(digest)
217 ):
218 return
219 stencil_groups = asyncio.run(self._build_stencils())
220 jit_stencils_new = out / "jit_stencils.h.new"
221 try:
222 with jit_stencils_new.open("w") as file:
223 file.write(digest)
224 if comment:
225 file.write(f"// {comment}\n")
226 file.write("\n")
227 for line in _writer.dump(stencil_groups):
228 file.write(f"{line}\n")
229 try:
230 jit_stencils_new.replace(jit_stencils)
231 except FileNotFoundError:
232 # another process probably already moved the file
233 if not jit_stencils.is_file():
234 raise
235 finally:
236 jit_stencils_new.unlink(missing_ok=True)
237
238
239 class _COFF(
240 _Target[_schema.COFFSection, _schema.COFFRelocation]
241 ): # pylint: disable = too-few-public-methods
242 def _handle_section(
243 self, section: _schema.COFFSection, group: _stencils.StencilGroup
244 ) -> None:
245 flags = {flag["Name"] for flag in section["Characteristics"]["Flags"]}
246 if "SectionData" in section:
247 section_data_bytes = section["SectionData"]["Bytes"]
248 else:
249 # Zeroed BSS data, seen with printf debugging calls:
250 section_data_bytes = [0] * section["RawDataSize"]
251 if "IMAGE_SCN_MEM_EXECUTE" in flags:
252 value = _stencils.HoleValue.CODE
253 stencil = group.code
254 elif "IMAGE_SCN_MEM_READ" in flags:
255 value = _stencils.HoleValue.DATA
256 stencil = group.data
257 else:
258 return
259 base = len(stencil.body)
260 group.symbols[section["Number"]] = value, base
261 stencil.body.extend(section_data_bytes)
262 for wrapped_symbol in section["Symbols"]:
263 symbol = wrapped_symbol["Symbol"]
264 offset = base + symbol["Value"]
265 name = symbol["Name"]
266 name = name.removeprefix(self.prefix)
267 if name not in group.symbols:
268 group.symbols[name] = value, offset
269 for wrapped_relocation in section["Relocations"]:
270 relocation = wrapped_relocation["Relocation"]
271 hole = self._handle_relocation(base, relocation, stencil.body)
272 stencil.holes.append(hole)
273
274 def _unwrap_dllimport(self, name: str) -> tuple[_stencils.HoleValue, str | None]:
275 if name.startswith("__imp_"):
276 name = name.removeprefix("__imp_")
277 name = name.removeprefix(self.prefix)
278 return _stencils.HoleValue.GOT, name
279 name = name.removeprefix(self.prefix)
280 return _stencils.symbol_to_value(name)
281
282 def _handle_relocation(
283 self, base: int, relocation: _schema.COFFRelocation, raw: bytearray
284 ) -> _stencils.Hole:
285 match relocation:
286 case {
287 "Offset": offset,
288 "Symbol": s,
289 "Type": {"Name": "IMAGE_REL_I386_DIR32" as kind},
290 }:
291 offset += base
292 value, symbol = self._unwrap_dllimport(s)
293 addend = int.from_bytes(raw[offset : offset + 4], "little")
294 case {
295 "Offset": offset,
296 "Symbol": s,
297 "Type": {
298 "Name": "IMAGE_REL_AMD64_REL32" | "IMAGE_REL_I386_REL32" as kind
299 },
300 }:
301 offset += base
302 value, symbol = self._unwrap_dllimport(s)
303 addend = (
304 int.from_bytes(raw[offset : offset + 4], "little", signed=True) - 4
305 )
306 case {
307 "Offset": offset,
308 "Symbol": s,
309 "Type": {
310 "Name": "IMAGE_REL_ARM64_BRANCH26"
311 | "IMAGE_REL_ARM64_PAGEBASE_REL21"
312 | "IMAGE_REL_ARM64_PAGEOFFSET_12A"
313 | "IMAGE_REL_ARM64_PAGEOFFSET_12L" as kind
314 },
315 }:
316 offset += base
317 value, symbol = self._unwrap_dllimport(s)
318 addend = 0
319 case _:
320 raise NotImplementedError(relocation)
321 return _stencils.Hole(offset, kind, value, symbol, addend)
322
323
324 class _ELF(
325 _Target[_schema.ELFSection, _schema.ELFRelocation]
326 ): # pylint: disable = too-few-public-methods
327 def _handle_section(
328 self, section: _schema.ELFSection, group: _stencils.StencilGroup
329 ) -> None:
330 section_type = section["Type"]["Name"]
331 flags = {flag["Name"] for flag in section["Flags"]["Flags"]}
332 if section_type == "SHT_RELA":
333 assert "SHF_INFO_LINK" in flags, flags
334 assert not section["Symbols"]
335 maybe_symbol = group.symbols.get(section["Info"])
336 if maybe_symbol is None:
337 # These are relocations for a section we're not emitting. Skip:
338 return
339 value, base = maybe_symbol
340 if value is _stencils.HoleValue.CODE:
341 stencil = group.code
342 else:
343 assert value is _stencils.HoleValue.DATA
344 stencil = group.data
345 for wrapped_relocation in section["Relocations"]:
346 relocation = wrapped_relocation["Relocation"]
347 hole = self._handle_relocation(base, relocation, stencil.body)
348 stencil.holes.append(hole)
349 elif section_type == "SHT_PROGBITS":
350 if "SHF_ALLOC" not in flags:
351 return
352 if "SHF_EXECINSTR" in flags:
353 value = _stencils.HoleValue.CODE
354 stencil = group.code
355 else:
356 value = _stencils.HoleValue.DATA
357 stencil = group.data
358 group.symbols[section["Index"]] = value, len(stencil.body)
359 for wrapped_symbol in section["Symbols"]:
360 symbol = wrapped_symbol["Symbol"]
361 offset = len(stencil.body) + symbol["Value"]
362 name = symbol["Name"]["Name"]
363 name = name.removeprefix(self.prefix)
364 group.symbols[name] = value, offset
365 stencil.body.extend(section["SectionData"]["Bytes"])
366 assert not section["Relocations"]
367 else:
368 assert section_type in {
369 "SHT_GROUP",
370 "SHT_LLVM_ADDRSIG",
371 "SHT_NOTE",
372 "SHT_NULL",
373 "SHT_STRTAB",
374 "SHT_SYMTAB",
375 }, section_type
376
377 def _handle_relocation(
378 self, base: int, relocation: _schema.ELFRelocation, raw: bytearray
379 ) -> _stencils.Hole:
380 symbol: str | None
381 match relocation:
382 case {
383 "Addend": addend,
384 "Offset": offset,
385 "Symbol": {"Name": s},
386 "Type": {
387 "Name": "R_AARCH64_ADR_GOT_PAGE"
388 | "R_AARCH64_LD64_GOT_LO12_NC"
389 | "R_X86_64_GOTPCREL"
390 | "R_X86_64_GOTPCRELX"
391 | "R_X86_64_REX_GOTPCRELX" as kind
392 },
393 }:
394 offset += base
395 s = s.removeprefix(self.prefix)
396 value, symbol = _stencils.HoleValue.GOT, s
397 case {
398 "Addend": addend,
399 "Offset": offset,
400 "Symbol": {"Name": s},
401 "Type": {"Name": kind},
402 }:
403 offset += base
404 s = s.removeprefix(self.prefix)
405 value, symbol = _stencils.symbol_to_value(s)
406 case _:
407 raise NotImplementedError(relocation)
408 return _stencils.Hole(offset, kind, value, symbol, addend)
409
410
411 class _MachO(
412 _Target[_schema.MachOSection, _schema.MachORelocation]
413 ): # pylint: disable = too-few-public-methods
414 def _handle_section(
415 self, section: _schema.MachOSection, group: _stencils.StencilGroup
416 ) -> None:
417 assert section["Address"] >= len(group.code.body)
418 assert "SectionData" in section
419 flags = {flag["Name"] for flag in section["Attributes"]["Flags"]}
420 name = section["Name"]["Value"]
421 name = name.removeprefix(self.prefix)
422 if "Debug" in flags:
423 return
424 if "SomeInstructions" in flags:
425 value = _stencils.HoleValue.CODE
426 stencil = group.code
427 start_address = 0
428 group.symbols[name] = value, section["Address"] - start_address
429 else:
430 value = _stencils.HoleValue.DATA
431 stencil = group.data
432 start_address = len(group.code.body)
433 group.symbols[name] = value, len(group.code.body)
434 base = section["Address"] - start_address
435 group.symbols[section["Index"]] = value, base
436 stencil.body.extend(
437 [0] * (section["Address"] - len(group.code.body) - len(group.data.body))
438 )
439 stencil.body.extend(section["SectionData"]["Bytes"])
440 assert "Symbols" in section
441 for wrapped_symbol in section["Symbols"]:
442 symbol = wrapped_symbol["Symbol"]
443 offset = symbol["Value"] - start_address
444 name = symbol["Name"]["Name"]
445 name = name.removeprefix(self.prefix)
446 group.symbols[name] = value, offset
447 assert "Relocations" in section
448 for wrapped_relocation in section["Relocations"]:
449 relocation = wrapped_relocation["Relocation"]
450 hole = self._handle_relocation(base, relocation, stencil.body)
451 stencil.holes.append(hole)
452
453 def _handle_relocation(
454 self, base: int, relocation: _schema.MachORelocation, raw: bytearray
455 ) -> _stencils.Hole:
456 symbol: str | None
457 match relocation:
458 case {
459 "Offset": offset,
460 "Symbol": {"Name": s},
461 "Type": {
462 "Name": "ARM64_RELOC_GOT_LOAD_PAGE21"
463 | "ARM64_RELOC_GOT_LOAD_PAGEOFF12" as kind
464 },
465 }:
466 offset += base
467 s = s.removeprefix(self.prefix)
468 value, symbol = _stencils.HoleValue.GOT, s
469 addend = 0
470 case {
471 "Offset": offset,
472 "Symbol": {"Name": s},
473 "Type": {"Name": "X86_64_RELOC_GOT" | "X86_64_RELOC_GOT_LOAD" as kind},
474 }:
475 offset += base
476 s = s.removeprefix(self.prefix)
477 value, symbol = _stencils.HoleValue.GOT, s
478 addend = (
479 int.from_bytes(raw[offset : offset + 4], "little", signed=True) - 4
480 )
481 case {
482 "Offset": offset,
483 "Section": {"Name": s},
484 "Type": {"Name": "X86_64_RELOC_SIGNED" as kind},
485 } | {
486 "Offset": offset,
487 "Symbol": {"Name": s},
488 "Type": {"Name": "X86_64_RELOC_BRANCH" | "X86_64_RELOC_SIGNED" as kind},
489 }:
490 offset += base
491 s = s.removeprefix(self.prefix)
492 value, symbol = _stencils.symbol_to_value(s)
493 addend = (
494 int.from_bytes(raw[offset : offset + 4], "little", signed=True) - 4
495 )
496 case {
497 "Offset": offset,
498 "Section": {"Name": s},
499 "Type": {"Name": kind},
500 } | {
501 "Offset": offset,
502 "Symbol": {"Name": s},
503 "Type": {"Name": kind},
504 }:
505 offset += base
506 s = s.removeprefix(self.prefix)
507 value, symbol = _stencils.symbol_to_value(s)
508 addend = 0
509 case _:
510 raise NotImplementedError(relocation)
511 return _stencils.Hole(offset, kind, value, symbol, addend)
512
513
514 def get_target(host: str) -> _COFF | _ELF | _MachO:
515 """Build a _Target for the given host "triple" and options."""
516 # ghccc currently crashes Clang when combined with musttail on aarch64. :(
517 target: _COFF | _ELF | _MachO
518 if re.fullmatch(r"aarch64-apple-darwin.*", host):
519 target = _MachO(host, alignment=8, prefix="_")
520 elif re.fullmatch(r"aarch64-pc-windows-msvc", host):
521 args = ["-fms-runtime-lib=dll"]
522 target = _COFF(host, alignment=8, args=args)
523 elif re.fullmatch(r"aarch64-.*-linux-gnu", host):
524 args = [
525 "-fpic",
526 # On aarch64 Linux, intrinsics were being emitted and this flag
527 # was required to disable them.
528 "-mno-outline-atomics",
529 ]
530 target = _ELF(host, alignment=8, args=args)
531 elif re.fullmatch(r"i686-pc-windows-msvc", host):
532 args = ["-DPy_NO_ENABLE_SHARED"]
533 target = _COFF(host, args=args, ghccc=True, prefix="_")
534 elif re.fullmatch(r"x86_64-apple-darwin.*", host):
535 target = _MachO(host, ghccc=True, prefix="_")
536 elif re.fullmatch(r"x86_64-pc-windows-msvc", host):
537 args = ["-fms-runtime-lib=dll"]
538 target = _COFF(host, args=args, ghccc=True)
539 elif re.fullmatch(r"x86_64-.*-linux-gnu", host):
540 args = ["-fpic"]
541 target = _ELF(host, args=args, ghccc=True)
542 else:
543 raise ValueError(host)
544 return target