Skip to content

GH-114809: Add support for macOS multi-arch builds with the JIT enabled #131751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
efd5863
First draft of supporting fat builds on macOS with the experimental JIT
ronaldoussoren Feb 20, 2024
4f493cd
merge conflict
savannahostrowski Sep 25, 2024
a55268e
fix up merge
savannahostrowski Sep 25, 2024
bce7980
add fix for CI?;
savannahostrowski Sep 25, 2024
b606b48
Fix up to make generic
savannahostrowski Sep 25, 2024
a382a44
Fix up conditions
savannahostrowski Sep 27, 2024
ff3d363
Save state before merge
savannahostrowski Mar 26, 2025
3bc2820
Merge main and fix conflicts
savannahostrowski Mar 26, 2025
348f2b6
Fix event loop binding error for _CORES in FAT builds
savannahostrowski Mar 26, 2025
4b31a30
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 26, 2025
4564bc4
Try using weakref?
savannahostrowski Mar 26, 2025
2d381ca
Merge branch 'og_fat_build' of https://github.com/savannahostrowski/c…
savannahostrowski Mar 26, 2025
49f86a1
Remove weakref and lazily instantiate cores
savannahostrowski Mar 27, 2025
467ffe1
use Runner
savannahostrowski Apr 3, 2025
fd858bc
Merge branch 'main' into og_fat_build
savannahostrowski Apr 3, 2025
58649ff
Remove conditional and fix indentation in stencils
savannahostrowski Apr 11, 2025
182612c
Merge branch 'og_fat_build' of https://github.com/savannahostrowski/c…
savannahostrowski Apr 11, 2025
30a4d99
Merge branch 'main' into og_fat_build
savannahostrowski Apr 11, 2025
b172a41
CI updates
savannahostrowski Apr 11, 2025
18e77f2
Merge branch 'og_fat_build' of https://github.com/savannahostrowski/c…
savannahostrowski Apr 11, 2025
de3f896
Remove extra space
savannahostrowski Apr 11, 2025
7c22761
Fix condition typo
savannahostrowski Apr 11, 2025
c54250b
undo
savannahostrowski Apr 11, 2025
c21573f
Update jit.yml
savannahostrowski Apr 11, 2025
4332f10
Merge branch 'main' into og_fat_build
savannahostrowski Apr 16, 2025
dd5f196
Merge branch 'main' into og_fat_build
brandtbucher Apr 28, 2025
cdc4bfb
Merge branch 'main' into og_fat_build
savannahostrowski Apr 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/jit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
find /usr/local/bin -lname '*/Library/Frameworks/Python.framework/*' -delete
brew install llvm@${{ matrix.llvm }}
export SDKROOT="$(xcrun --show-sdk-path)"
./configure --enable-experimental-jit ${{ matrix.debug && '--with-pydebug' || '' }}
./configure --enable-experimental-jit --enable-universalsdk --with-universal-archs=universal2 ${{ matrix.debug && '--with-pydebug' || '' }}
make all --jobs 4
./python.exe -m test --multiprocess 0 --timeout 4500 --verbose2 --verbose3

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Tools/unicode/data/
# hendrikmuhs/ccache-action@v1
/.ccache
/cross-build/
/jit_stencils.h
/jit_stencils*.h
/platform
/profile-clean-stamp
/profile-run-stamp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for macOS multi-arch builds with the JIT enabled
34 changes: 24 additions & 10 deletions Tools/jit/_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CPYTHON = TOOLS.parent
PYTHON_EXECUTOR_CASES_C_H = CPYTHON / "Python" / "executor_cases.c.h"
TOOLS_JIT_TEMPLATE_C = TOOLS_JIT / "template.c"
ASYNCIO_RUNNER = asyncio.Runner()

_S = typing.TypeVar("_S", _schema.COFFSection, _schema.ELFSection, _schema.MachOSection)
_R = typing.TypeVar(
Expand All @@ -35,6 +36,7 @@
@dataclasses.dataclass
class _Target(typing.Generic[_S, _R]):
triple: str
condition: str
_: dataclasses.KW_ONLY
alignment: int = 1
args: typing.Sequence[str] = ()
Expand Down Expand Up @@ -188,7 +190,12 @@ async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:
return stencil_groups

def build(
self, out: pathlib.Path, *, comment: str = "", force: bool = False
self,
out: pathlib.Path,
*,
comment: str = "",
force: bool = False,
stencils_h: str = "jit_stencils.h",
) -> None:
"""Build jit_stencils.h in the given directory."""
if not self.stable:
Expand All @@ -197,14 +204,14 @@ def build(
outline = "=" * len(warning)
print("\n".join(["", outline, warning, request, outline, ""]))
digest = f"// {self._compute_digest(out)}\n"
jit_stencils = out / "jit_stencils.h"
jit_stencils = out / stencils_h
if (
not force
and jit_stencils.exists()
and jit_stencils.read_text().startswith(digest)
):
return
stencil_groups = asyncio.run(self._build_stencils())
stencil_groups = ASYNCIO_RUNNER.run(self._build_stencils())
jit_stencils_new = out / "jit_stencils.h.new"
try:
with jit_stencils_new.open("w") as file:
Expand Down Expand Up @@ -512,33 +519,40 @@ def get_target(host: str) -> _COFF | _ELF | _MachO:
"""Build a _Target for the given host "triple" and options."""
target: _COFF | _ELF | _MachO
if re.fullmatch(r"aarch64-apple-darwin.*", host):
target = _MachO(host, alignment=8, prefix="_")
condition = "defined(__aarch64__) && defined(__APPLE__)"
target = _MachO(host, condition, alignment=8, prefix="_")
elif re.fullmatch(r"aarch64-pc-windows-msvc", host):
args = ["-fms-runtime-lib=dll", "-fplt"]
target = _COFF(host, alignment=8, args=args)
condition = "defined(_M_ARM64)"
target = _COFF(host, condition, alignment=8, args=args)
elif re.fullmatch(r"aarch64-.*-linux-gnu", host):
args = [
"-fpic",
# On aarch64 Linux, intrinsics were being emitted and this flag
# was required to disable them.
"-mno-outline-atomics",
]
target = _ELF(host, alignment=8, args=args)
condition = "defined(__aarch64__) && defined(__linux__)"
target = _ELF(host, condition, alignment=8, args=args)
elif re.fullmatch(r"i686-pc-windows-msvc", host):
args = [
"-DPy_NO_ENABLE_SHARED",
# __attribute__((preserve_none)) is not supported
"-Wno-ignored-attributes",
]
target = _COFF(host, args=args, prefix="_")
condition = "defined(_M_IX86)"
target = _COFF(host, condition, args=args, prefix="_")
elif re.fullmatch(r"x86_64-apple-darwin.*", host):
target = _MachO(host, prefix="_")
condition = "defined(__x86_64__) && defined(__APPLE__)"
target = _MachO(host, condition, prefix="_")
elif re.fullmatch(r"x86_64-pc-windows-msvc", host):
args = ["-fms-runtime-lib=dll"]
target = _COFF(host, args=args)
condition = "defined(_M_X64)"
target = _COFF(host, condition, args=args)
elif re.fullmatch(r"x86_64-.*-linux-gnu", host):
args = ["-fno-pic", "-mcmodel=medium", "-mlarge-data-threshold=0"]
target = _ELF(host, args=args)
condition = "defined(__x86_64__) && defined(__linux__)"
target = _ELF(host, condition, args=args)
else:
raise ValueError(host)
return target
27 changes: 23 additions & 4 deletions Tools/jit/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
comment = f"$ {shlex.join([pathlib.Path(sys.executable).name] + 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",
nargs="+",
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"
Expand All @@ -23,6 +26,22 @@
"-v", "--verbose", action="store_true", help="echo commands as they are run"
)
args = parser.parse_args()
args.target.debug = args.debug
args.target.verbose = args.verbose
args.target.build(pathlib.Path.cwd(), comment=comment, force=args.force)
for target in args.target:
target.debug = args.debug
target.force = args.force
target.verbose = args.verbose
target.build(
pathlib.Path.cwd(),
comment=comment,
stencils_h=f"jit_stencils-{target.triple}.h",
force=args.force,
)

with open("jit_stencils.h", "w") as fp:
for idx, target in enumerate(args.target):
fp.write(f"#{'if' if idx == 0 else 'elif'} {target.condition}\n")
fp.write(f'#include "jit_stencils-{target.triple}.h"\n')

fp.write("#else\n")
fp.write('#error "unexpected target"\n')
fp.write("#endif\n")
110 changes: 59 additions & 51 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading