From 30c64db9f69e450c05b69939661a5baad79f2bba Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 30 Apr 2024 16:29:58 -0700 Subject: [PATCH 1/2] Warn when building the JIT for an experimental platform --- Tools/jit/_targets.py | 8 +++++++- Tools/jit/build.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Tools/jit/_targets.py b/Tools/jit/_targets.py index 274d17bcf38deb..d1be1e46ccfe34 100644 --- a/Tools/jit/_targets.py +++ b/Tools/jit/_targets.py @@ -40,6 +40,7 @@ class _Target(typing.Generic[_S, _R]): args: typing.Sequence[str] = () ghccc: bool = False prefix: str = "" + stable: bool = False debug: bool = False force: bool = False verbose: bool = False @@ -188,6 +189,11 @@ async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]: def build(self, out: pathlib.Path, *, comment: str = "") -> None: """Build jit_stencils.h in the given directory.""" + if not self.stable: + warning = f"JIT support for {self.triple} is still experimental!" + request = "Please report any issues you encounter.".center(len(warning)) + outline = "=" * len(warning) + print("\n".join(["", outline, warning, request, outline, ""])) digest = f"// {self._compute_digest(out)}\n" jit_stencils = out / "jit_stencils.h" if ( @@ -478,7 +484,7 @@ def _handle_relocation( return _stencils.Hole(offset, kind, value, symbol, addend) -def get_target(host: str) -> _COFF | _ELF | _MachO: +def target(host: str) -> _COFF | _ELF | _MachO: """Build a _Target for the given host "triple" and options.""" # ghccc currently crashes Clang when combined with musttail on aarch64. :( if re.fullmatch(r"aarch64-apple-darwin.*", host): diff --git a/Tools/jit/build.py b/Tools/jit/build.py index 4d4ace14ebf26c..6897e7bba0ebf5 100644 --- a/Tools/jit/build.py +++ b/Tools/jit/build.py @@ -10,7 +10,7 @@ comment = f"$ {shlex.join([sys.executable] + sys.argv)}" parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( - "target", type=_targets.get_target, help="a PEP 11 target triple to compile for" + "target", type=_targets.target, help="a PEP 11 target triple to compile for" ) parser.add_argument( "-d", "--debug", action="store_true", help="compile for a debug build of Python" From b97473f1a37f715ee1229a804ecdc2e90bb7d891 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 1 May 2024 09:24:26 -0700 Subject: [PATCH 2/2] Linting, type-checking, and formatting --- Tools/jit/_llvm.py | 1 + Tools/jit/_schema.py | 1 + Tools/jit/_stencils.py | 8 ++++---- Tools/jit/_targets.py | 45 ++++++++++++++++++++++-------------------- Tools/jit/_writer.py | 1 + Tools/jit/build.py | 6 +++--- 6 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Tools/jit/_llvm.py b/Tools/jit/_llvm.py index 74a048ccbfcc58..45bd69ff861b56 100644 --- a/Tools/jit/_llvm.py +++ b/Tools/jit/_llvm.py @@ -1,4 +1,5 @@ """Utilities for invoking LLVM tools.""" + import asyncio import functools import os diff --git a/Tools/jit/_schema.py b/Tools/jit/_schema.py index 6aef887e12475b..228fc389584dd7 100644 --- a/Tools/jit/_schema.py +++ b/Tools/jit/_schema.py @@ -1,4 +1,5 @@ """Schema for the JSON produced by llvm-readobj --elf-output-style=JSON.""" + import typing HoleKind: typing.TypeAlias = typing.Literal[ diff --git a/Tools/jit/_stencils.py b/Tools/jit/_stencils.py index f8ecffcf3ddda2..9feceb45388d05 100644 --- a/Tools/jit/_stencils.py +++ b/Tools/jit/_stencils.py @@ -1,4 +1,5 @@ """Core data structures for compiled code templates.""" + import dataclasses import enum import sys @@ -29,7 +30,7 @@ class HoleValue(enum.Enum): OPARG = enum.auto() # The current uop's operand on 64-bit platforms (exposed as _JIT_OPERAND): OPERAND = enum.auto() - # The current uop's operand on 32-bit platforms (exposed as _JIT_OPERAND_HI and _JIT_OPERAND_LO): + # The current uop's operand on 32-bit platforms (exposed as _JIT_OPERAND_HI/LO): OPERAND_HI = enum.auto() OPERAND_LO = enum.auto() # The current uop's target (exposed as _JIT_TARGET): @@ -203,9 +204,8 @@ def process_relocations(self, *, alignment: int = 1) -> None: """Fix up all GOT and internal relocations for this stencil group.""" for hole in self.code.holes.copy(): if ( - hole.kind in { - "R_AARCH64_CALL26", "R_AARCH64_JUMP26", "ARM64_RELOC_BRANCH26" - } + hole.kind + in {"R_AARCH64_CALL26", "R_AARCH64_JUMP26", "ARM64_RELOC_BRANCH26"} and hole.value is HoleValue.ZERO ): self.code.pad(alignment) diff --git a/Tools/jit/_targets.py b/Tools/jit/_targets.py index d1be1e46ccfe34..23bb18947f80ea 100644 --- a/Tools/jit/_targets.py +++ b/Tools/jit/_targets.py @@ -1,4 +1,5 @@ """Target-specific code generation, parsing, and processing.""" + import asyncio import dataclasses import hashlib @@ -42,7 +43,6 @@ class _Target(typing.Generic[_S, _R]): prefix: str = "" stable: bool = False debug: bool = False - force: bool = False verbose: bool = False def _compute_digest(self, out: pathlib.Path) -> str: @@ -187,7 +187,9 @@ async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]: tasks.append(group.create_task(coro, name=opname)) return {task.get_name(): task.result() for task in tasks} - def build(self, out: pathlib.Path, *, comment: str = "") -> None: + def build( + self, out: pathlib.Path, *, comment: str = "", force: bool = False + ) -> None: """Build jit_stencils.h in the given directory.""" if not self.stable: warning = f"JIT support for {self.triple} is still experimental!" @@ -197,7 +199,7 @@ def build(self, out: pathlib.Path, *, comment: str = "") -> None: digest = f"// {self._compute_digest(out)}\n" jit_stencils = out / "jit_stencils.h" if ( - not self.force + not force and jit_stencils.exists() and jit_stencils.read_text().startswith(digest) ): @@ -456,9 +458,7 @@ def _handle_relocation( } | { "Offset": offset, "Symbol": {"Name": s}, - "Type": { - "Name": "X86_64_RELOC_BRANCH" | "X86_64_RELOC_SIGNED" as kind - }, + "Type": {"Name": "X86_64_RELOC_BRANCH" | "X86_64_RELOC_SIGNED" as kind}, }: offset += base s = s.removeprefix(self.prefix) @@ -484,26 +484,29 @@ def _handle_relocation( return _stencils.Hole(offset, kind, value, symbol, addend) -def target(host: str) -> _COFF | _ELF | _MachO: +def get_target(host: str) -> _COFF | _ELF | _MachO: """Build a _Target for the given host "triple" and options.""" # ghccc currently crashes Clang when combined with musttail on aarch64. :( + target: _COFF | _ELF | _MachO if re.fullmatch(r"aarch64-apple-darwin.*", host): - return _MachO(host, alignment=8, prefix="_") - if re.fullmatch(r"aarch64-pc-windows-msvc", host): + target = _MachO(host, alignment=8, prefix="_") + elif re.fullmatch(r"aarch64-pc-windows-msvc", host): args = ["-fms-runtime-lib=dll"] - return _COFF(host, alignment=8, args=args) - if re.fullmatch(r"aarch64-.*-linux-gnu", host): + target = _COFF(host, alignment=8, args=args) + elif re.fullmatch(r"aarch64-.*-linux-gnu", host): args = ["-fpic"] - return _ELF(host, alignment=8, args=args) - if re.fullmatch(r"i686-pc-windows-msvc", host): + target = _ELF(host, alignment=8, args=args) + elif re.fullmatch(r"i686-pc-windows-msvc", host): args = ["-DPy_NO_ENABLE_SHARED"] - return _COFF(host, args=args, ghccc=True, prefix="_") - if re.fullmatch(r"x86_64-apple-darwin.*", host): - return _MachO(host, ghccc=True, prefix="_") - if re.fullmatch(r"x86_64-pc-windows-msvc", host): + target = _COFF(host, args=args, ghccc=True, prefix="_") + elif re.fullmatch(r"x86_64-apple-darwin.*", host): + target = _MachO(host, ghccc=True, prefix="_") + elif re.fullmatch(r"x86_64-pc-windows-msvc", host): args = ["-fms-runtime-lib=dll"] - return _COFF(host, args=args, ghccc=True) - if re.fullmatch(r"x86_64-.*-linux-gnu", host): + target = _COFF(host, args=args, ghccc=True) + elif re.fullmatch(r"x86_64-.*-linux-gnu", host): args = ["-fpic"] - return _ELF(host, args=args, ghccc=True) - raise ValueError(host) + target = _ELF(host, args=args, ghccc=True) + else: + raise ValueError(host) + return target diff --git a/Tools/jit/_writer.py b/Tools/jit/_writer.py index 6b36d8a9c66a3f..ccd67850c37787 100644 --- a/Tools/jit/_writer.py +++ b/Tools/jit/_writer.py @@ -1,4 +1,5 @@ """Utilities for writing StencilGroups out to a C header file.""" + import typing import _schema diff --git a/Tools/jit/build.py b/Tools/jit/build.py index 6897e7bba0ebf5..4a23c6f0afa74a 100644 --- a/Tools/jit/build.py +++ b/Tools/jit/build.py @@ -1,4 +1,5 @@ """Build an experimental just-in-time compiler for CPython.""" + import argparse import pathlib import shlex @@ -10,7 +11,7 @@ comment = f"$ {shlex.join([sys.executable] + sys.argv)}" parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( - "target", type=_targets.target, help="a PEP 11 target triple to compile for" + "target", type=_targets.get_target, help="a PEP 11 target triple to compile for" ) parser.add_argument( "-d", "--debug", action="store_true", help="compile for a debug build of Python" @@ -23,6 +24,5 @@ ) args = parser.parse_args() args.target.debug = args.debug - args.target.force = args.force args.target.verbose = args.verbose - args.target.build(pathlib.Path.cwd(), comment=comment) + args.target.build(pathlib.Path.cwd(), comment=comment, force=args.force)