Skip to content

Commit

Permalink
GH-113464: Display a warning when building the JIT (GH-118481)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandtbucher authored May 1, 2024
1 parent 39981fd commit 424438b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
1 change: 1 addition & 0 deletions Tools/jit/_llvm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utilities for invoking LLVM tools."""

import asyncio
import functools
import os
Expand Down
1 change: 1 addition & 0 deletions Tools/jit/_schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Schema for the JSON produced by llvm-readobj --elf-output-style=JSON."""

import typing

HoleKind: typing.TypeAlias = typing.Literal[
Expand Down
8 changes: 4 additions & 4 deletions Tools/jit/_stencils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Core data structures for compiled code templates."""

import dataclasses
import enum
import sys
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 29 additions & 20 deletions Tools/jit/_targets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Target-specific code generation, parsing, and processing."""

import asyncio
import dataclasses
import hashlib
Expand Down Expand Up @@ -40,8 +41,8 @@ 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

def _compute_digest(self, out: pathlib.Path) -> str:
Expand Down Expand Up @@ -186,12 +187,19 @@ 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!"
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 (
not self.force
not force
and jit_stencils.exists()
and jit_stencils.read_text().startswith(digest)
):
Expand Down Expand Up @@ -450,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)
Expand Down Expand Up @@ -481,23 +487,26 @@ def _handle_relocation(
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
1 change: 1 addition & 0 deletions Tools/jit/_writer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utilities for writing StencilGroups out to a C header file."""

import typing

import _schema
Expand Down
4 changes: 2 additions & 2 deletions Tools/jit/build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Build an experimental just-in-time compiler for CPython."""

import argparse
import pathlib
import shlex
Expand All @@ -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)

0 comments on commit 424438b

Please sign in to comment.