8000 GH-114809: Add support for macOS multi-arch builds with the JIT enabl… · python/cpython@26c0248 · GitHub
[go: up one dir, main page]

Skip to content

Commit 26c0248

Browse files
savannahostrowskironaldoussorenbrandtbucher
authored
GH-114809: Add support for macOS multi-arch builds with the JIT enabled (#131751)
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com> Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
1 parent 2b67db7 commit 26c0248

File tree

7 files changed

+152
-102
lines changed
8000

7 files changed

+152
-102
lines changed

.github/workflows/jit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ jobs:
113113
find /usr/local/bin -lname '*/Library/Frameworks/Python.framework/*' -delete
114114
brew install llvm@${{ matrix.llvm }}
115115
export SDKROOT="$(xcrun --show-sdk-path)"
116-
./configure --enable-experimental-jit ${{ matrix.debug && '--with-pydebug' || '' }}
116+
./configure --enable-experimental-jit --enable-universalsdk --with-universal-archs=universal2 ${{ matrix.debug && '--with-pydebug' || '' }}
117117
make all --jobs 4
118118
./python.exe -m test --multiprocess 0 --timeout 4500 --verbose2 --verbose3
119119

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Tools/unicode/data/
138138
# hendrikmuhs/ccache-action@v1
139139
/.ccache
140140
/cross-build/
141-
/jit_stencils.h
141+
/jit_stencils*.h
142142
/platform
143143
/profile-clean-stamp
144144
/profile-run-stamp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for macOS multi-arch builds with the JIT enabled

Tools/jit/_targets.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
CPYTHON = TOOLS.parent
2626
PYTHON_EXECUTOR_CASES_C_H = CPYTHON / "Python" / "executor_cases.c.h"
2727
TOOLS_JIT_TEMPLATE_C = TOOLS_JIT / "template.c"
28+
ASYNCIO_RUNNER = asyncio.Runner()
2829

2930
_S = typing.TypeVar("_S", _schema.COFFSection, _schema.ELFSection, _schema.MachOSection)
3031
_R = typing.TypeVar(
@@ -35,6 +36,7 @@
3536
@dataclasses.dataclass
3637
class _Target(typing.Generic[_S, _R]):
3738
triple: str
39+
condition: str
3840
_: dataclasses.KW_ONLY
3941
alignment: int = 1
4042
args: typing.Sequence[str] = ()
@@ -188,7 +190,12 @@ async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:
188190
return stencil_groups
189191

190192
def build(
191-
self, out: pathlib.Path, *, comment: str = "", force: bool = False
193+
self,
194+
out: pathlib.Path,
195+
*,
196+
comment: str = "",
197+
force: bool = False,
198+
stencils_h: str = "jit_stencils.h",
192199
) -> None:
193200
"""Build jit_stencils.h in the given directory."""
194201
if not self.stable:
@@ -197,14 +204,14 @@ def build(
197204
outline = "=" * len(warning)
198205
print("\n".join(["", outline, warning, request, outline, ""]))
199206
digest = f"// {self._compute_digest(out)}\n"
200-
jit_stencils = out / "jit_stencils.h"
207+
jit_stencils = out / stencils_h
201208
if (
202209
not force
203210
and jit_stencils.exists()
204211
and jit_stencils.read_text().startswith(digest)
205212
):
206213
return
207-
stencil_groups = asyncio.run(self._build_stencils())
214+
stencil_groups = ASYNCIO_RUNNER.run(self._build_stencils())
208215
jit_stencils_new = out / "jit_stencils.h.new"
209216
try:
210217
with jit_stencils_new.open("w") as file:
@@ -512,33 +519,40 @@ def get_target(host: str) -> _COFF | _ELF | _MachO:
512519
"""Build a _Target for the given host "triple" and options."""
513520
target: _COFF | _ELF | _MachO
514521
if re.fullmatch(r"aarch64-apple-darwin.*", host):
515-
target = _MachO(host, alignment=8, prefix="_")
522+
condition = "defined(__aarch64__) && defined(__APPLE__)"
523+
target = _MachO(host, condition, alignment=8, prefix="_")
516524
elif re.fullmatch(r"aarch64-pc-windows-msvc", host):
517525
args = ["-fms-runtime-lib=dll", "-fplt"]
518-
target = _COFF(host, alignment=8, args=args)
526+
condition = "defined(_M_ARM64)"
527+
target = _COFF(host, condition, alignment=8, args=args)
519528
elif re.fullmatch(r"aarch64-.*-linux-gnu", host):
520529
args = [
521530
"-fpic",
522531
# On aarch64 Linux, intrinsics were being emitted and this flag
523532
# was required to disable them.
524533
"-mno-outline-atomics",
525534
]
526-
target = _ELF(host, alignment=8, args=args)
535+
condition = "defined(__aarch64__) && defined(__linux__)"
536+
target = _ELF(host, condition, alignment=8, args=args)
527537
elif re.fullmatch(r"i686-pc-windows-msvc", host):
528538
args = [
529539
"-DPy_NO_ENABLE_SHARED",
530540
# __attribute__((preserve_none)) is not supported
531541
"-Wno-ignored-attributes",
532542
]
533-
target = _COFF(host, args=args, prefix="_")
543+
condition = "defined(_M_IX86)"
544+
target = _COFF(host, condition, args=args, prefix="_")
534545
elif re.fullmatch(r"x86_64-apple-darwin.*", host):
535-
target = _MachO(host, prefix="_")
546+
condition = "defined(__x86_64__) && defined(__APPLE__)"
547+
target = _MachO(host, condition, prefix="_")
536548
elif re.fullmatch(r"x86_64-pc-windows-msvc", host):
537549
args = ["-fms-runtime-lib=dll"]
538-
target = _COFF(host, args=args)
550+
condition = "defined(_M_X64)"
551+
target = _COFF(host, condition, args=args)
539552
elif re.fullmatch(r"x86_64-.*-linux-gnu", host):
540553
args = ["-fno-pic", "-mcmodel=medium", "-mlarge-data-threshold=0"]
541-
target = _ELF(host, args=args)
554+
condition = "defined(__x86_64__) && defined(__linux__)"
555+
target = _ELF(host, condition, args=args)
542556
else:
543557
raise ValueError(host)
544558
return target

Tools/jit/build.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
comment = f"$ {shlex.join([pathlib.Path(sys.executable).name] + sys.argv)}"
1212
parser = argparse.ArgumentParser(description=__doc__)
1313
parser.add_argument(
14-
"target", type=_targets.get_target, help="a PEP 11 target triple to compile for"
14+
"target",
15+
nargs="+",
16+
type=_targets.get_target,
17+
help="a PEP 11 target triple to compile for",
1518
)
1619
parser.add_argument(
1720
"-d", "--debug", action="store_true", help="compile for a debug build of Python"
@@ -23,6 +26,22 @@
2326
"-v", "--verbose", action="store_true", help="echo commands as they are run"
2427
)
2528
args = parser.parse_args()
26-
args.target.debug = args.debug
27-
args.target.verbose = args.verbose
28-
args.target.build(pathlib.Path.cwd(), comment=comment, force=args.force)
29+
for target in args.target:
30+
target.debug = args.debug
31+
target.force = args.force
32+
target.verbose = args.verbose
33+
target.build(
34+
pathlib.Path.cwd(),
35+
comment=comment,
36+
stencils_h=f"jit_stencils-{target.triple}.h",
37+
force=args.force,
38+
)
39+
40+
with open("jit_stencils.h", "w") as fp:
41+
for idx, target in enumerate(args.target):
42+
fp.write(f"#{'if' if idx == 0 else 'elif'} {target.condition}\n")
43+
fp.write(f'#include "jit_stencils-{target.triple}.h"\n')
44+
45+
fp.write("#else\n")
46+
fp.write('#error "unexpected target"\n')
47+
fp.write("#endif\n")

configure

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

0 commit comments

Comments
 (0)
0