Skip to content

Commit efd5863

Browse files
First draft of supporting fat builds on macOS with the experimental JIT
1 parent 57d31ec commit efd5863

File tree

4 files changed

+106
-62
lines changed

4 files changed

+106
-62
lines changed

Tools/jit/_targets.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
PYTHON_EXECUTOR_CASES_C_H = CPYTHON / "Python" / "executor_cases.c.h"
2626
TOOLS_JIT_TEMPLATE_C = TOOLS_JIT / "template.c"
2727

28+
ASYNCIO_RUNNER=asyncio.Runner()
29+
2830

2931
_S = typing.TypeVar("_S", _schema.COFFSection, _schema.ELFSection, _schema.MachOSection)
3032
_R = typing.TypeVar(
@@ -106,6 +108,7 @@ async def _compile(
106108
o = tempdir / f"{opname}.o"
107109
args = [
108110
f"--target={self.triple}",
111+
"-isysroot", "/Users/ronald/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk",
109112
"-DPy_BUILD_CORE",
110113
"-D_DEBUG" if self.debug else "-DNDEBUG",
111114
f"-D_JIT_OPCODE={opname}",
@@ -152,17 +155,17 @@ async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:
152155
tasks.append(group.create_task(coro, name=opname))
153156
return {task.get_name(): task.result() for task in tasks}
154157

155-
def build(self, out: pathlib.Path, *, comment: str = "") -> None:
158+
def build(self, out: pathlib.Path, *, comment: str = "", stencils_h: str = "jit_stencils.h") -> None:
156159
"""Build jit_stencils.h in the given directory."""
157160
digest = f"// {self._compute_digest(out)}\n"
158-
jit_stencils = out / "jit_stencils.h"
161+
jit_stencils = out / stencils_h
159162
if (
160163
not self.force
161164
and jit_stencils.exists()
162165
and jit_stencils.read_text().startswith(digest)
163166
):
164167
return
165-
stencil_groups = asyncio.run(self._build_stencils())
168+
stencil_groups = ASYNCIO_RUNNER.run(self._build_stencils())
166169
with jit_stencils.open("w") as file:
167170
file.write(digest)
168171
if comment:

Tools/jit/build.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
comment = f"$ {shlex.join([sys.executable] + sys.argv)}"
1111
parser = argparse.ArgumentParser(description=__doc__)
1212
parser.add_argument(
13-
"target", type=_targets.get_target, help="a PEP 11 target triple to compile for"
13+
"target", nargs="+", type=_targets.get_target, help="a PEP 11 target triple to compile for"
1414
)
1515
parser.add_argument(
1616
"-d", "--debug", action="store_true", help="compile for a debug build of Python"
@@ -22,7 +22,30 @@
2222
"-v", "--verbose", action="store_true", help="echo commands as they are run"
2323
)
2424
args = parser.parse_args()
25-
args.target.debug = args.debug
26-
args.target.force = args.force
27-
args.target.verbose = args.verbose
28-
args.target.build(pathlib.Path.cwd(), comment=comment)
25+
26+
if len(args.target) == -1:
27+
args.target.debug = args.debug
28+
args.target.force = args.force
29+
args.target.verbose = args.verbose
30+
args.target.build(pathlib.Path.cwd(), comment=comment)
31+
32+
else:
33+
# Multiple triples specified, assume this is a macOS multiarchitecture build
34+
# - Generate multiple stencil headers
35+
# - Generate a helper header that include sthe stencils for the current
36+
# architecture.
37+
for target in args.target:
38+
target.debug = args.debug
39+
target.force = args.force
40+
target.verbose = args.verbose
41+
target.build(pathlib.Path.cwd(), comment=comment, stencils_h=f"jit_stencils-{target.triple}.h")
42+
43+
with open("jit_stencils.h", "w") as fp:
44+
for idx, target in enumerate(args.target):
45+
cpu, _, _ = target.triple.partition("-")
46+
fp.write(f"#{'if' if idx == 0 else 'elif'} defined(__{cpu}__)\n")
47+
fp.write(f'# include "jit_stencils-{target.triple}.h"\n')
48+
49+
fp.write("#else\n")
50+
fp.write('# error "unexpected cpu type"\n')
51+
fp.write("#endif\n")

configure

Lines changed: 42 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,27 +1621,6 @@ else
16211621
AC_MSG_RESULT([no])
16221622
fi
16231623

1624-
# Check for --enable-experimental-jit:
1625-
AC_MSG_CHECKING([for --enable-experimental-jit])
1626-
AC_ARG_ENABLE([experimental-jit],
1627-
[AS_HELP_STRING([--enable-experimental-jit],
1628-
[build the experimental just-in-time compiler (default is no)])],
1629-
[],
1630-
[enable_experimental_jit=no])
1631-
AS_VAR_IF([enable_experimental_jit],
1632-
[no],
1633-
[],
1634-
[AS_VAR_APPEND([CFLAGS_NODIST], [" -D_Py_JIT"])
1635-
AS_VAR_SET([REGEN_JIT_COMMAND],
1636-
["\$(PYTHON_FOR_REGEN) \$(srcdir)/Tools/jit/build.py $host"])
1637-
AS_VAR_SET([JIT_STENCILS_H], ["jit_stencils.h"])
1638-
AS_VAR_IF([Py_DEBUG],
1639-
[true],
1640-
[AS_VAR_APPEND([REGEN_JIT_COMMAND], [" --debug"])],
1641-
[])])
1642-
AC_SUBST([REGEN_JIT_COMMAND])
1643-
AC_SUBST([JIT_STENCILS_H])
1644-
AC_MSG_RESULT([$enable_experimental_jit])
16451624

16461625
# Enable optimization flags
16471626
AC_SUBST([DEF_MAKE_ALL_RULE])
@@ -2436,42 +2415,50 @@ yes)
24362415
UNIVERSAL_ARCH_FLAGS="-arch ppc -arch i386"
24372416
LIPO_32BIT_FLAGS=""
24382417
ARCH_RUN_32BIT=""
2418+
ARCH_TRIPPLES=`echo {ppc,i386}-apple-darwin`
24392419
;;
24402420
64-bit)
24412421
UNIVERSAL_ARCH_FLAGS="-arch ppc64 -arch x86_64"
24422422
LIPO_32BIT_FLAGS=""
24432423
ARCH_RUN_32BIT="true"
2424+
ARCH_TRIPPLES=`echo {ppc64,x86_64}-apple-darwin`
24442425
;;
24452426
all)
24462427
UNIVERSAL_ARCH_FLAGS="-arch i386 -arch ppc -arch ppc64 -arch x86_64"
24472428
LIPO_32BIT_FLAGS="-extract ppc7400 -extract i386"
24482429
ARCH_RUN_32BIT="/usr/bin/arch -i386 -ppc"
2430+
ARCH_TRIPPLES=`echo {i386,ppc,ppc64,x86_64}-apple-darwin`
24492431
;;
24502432
universal2)
24512433
UNIVERSAL_ARCH_FLAGS="-arch arm64 -arch x86_64"
24522434
LIPO_32BIT_FLAGS=""
24532435
LIPO_INTEL64_FLAGS="-extract x86_64"
24542436
ARCH_RUN_32BIT="true"
2437+
ARCH_TRIPPLES=`echo {aarch64,x86_64}-apple-darwin`
24552438
;;
24562439
intel)
24572440
UNIVERSAL_ARCH_FLAGS="-arch i386 -arch x86_64"
24582441
LIPO_32BIT_FLAGS="-extract i386"
24592442
ARCH_RUN_32BIT="/usr/bin/arch -i386"
2443+
ARCH_TRIPPLES=`echo {i386,x86_64}-apple-darwin`
24602444
;;
24612445
intel-32)
24622446
UNIVERSAL_ARCH_FLAGS="-arch i386"
24632447
LIPO_32BIT_FLAGS=""
24642448
ARCH_RUN_32BIT=""
2449+
ARCH_TRIPPLES=i386-apple-darwin
24652450
;;
24662451
intel-64)
24672452
UNIVERSAL_ARCH_FLAGS="-arch x86_64"
24682453
LIPO_32BIT_FLAGS=""
24692454
ARCH_RUN_32BIT="true"
2455+
ARCH_TRIPPLES=x86_64-apple-darwin
24702456
;;
24712457
3-way)
24722458
UNIVERSAL_ARCH_FLAGS="-arch i386 -arch ppc -arch x86_64"
24732459
LIPO_32BIT_FLAGS="-extract ppc7400 -extract i386"
24742460
ARCH_RUN_32BIT="/usr/bin/arch -i386 -ppc"
2461+
ARCH_TRIPPLES=`echo {i386,ppc,x86_64}-apple-darwin`
24752462
;;
24762463
*)
24772464
AC_MSG_ERROR([proper usage is --with-universal-arch=universal2|32-bit|64-bit|all|intel|3-way])
@@ -2565,6 +2552,28 @@ yes)
25652552
;;
25662553
esac
25672554

2555+
# Check for --enable-experimental-jit:
2556+
AC_MSG_CHECKING([for --enable-experimental-jit])
2557+
AC_ARG_ENABLE([experimental-jit],
2558+
[AS_HELP_STRING([--enable-experimental-jit],
2559+
[build the experimental just-in-time compiler (default is no)])],
2560+
[],
2561+
[enable_experimental_jit=no])
2562+
AS_VAR_IF([enable_experimental_jit],
2563+
[no],
2564+
[],
2565+
[AS_VAR_APPEND([CFLAGS_NODIST], [" -D_Py_JIT"])
2566+
AS_VAR_SET([REGEN_JIT_COMMAND],
2567+
["\$(PYTHON_FOR_REGEN) \$(srcdir)/Tools/jit/build.py ${ARCH_TRIPPLES:-$host}"])
2568+
AS_VAR_SET([JIT_STENCILS_H], ["jit_stencils.h"])
2569+
AS_VAR_IF([Py_DEBUG],
2570+
[true],
2571+
[AS_VAR_APPEND([REGEN_JIT_COMMAND], [" --debug"])],
2572+
[])])
2573+
AC_SUBST([REGEN_JIT_COMMAND])
2574+
AC_SUBST([JIT_STENCILS_H])
2575+
AC_MSG_RESULT([$enable_experimental_jit])
2576+
25682577
case "$CC_BASENAME" in
25692578
*mpicc*)
25702579
CFLAGS_NODIST="$CFLAGS_NODIST"

0 commit comments

Comments
 (0)