Skip to content

Conversation

@mweinelt
Copy link
Member

Things done

WIP JIT support.

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: python Python is a high-level, general-purpose programming language. label Oct 15, 2024
@mweinelt
Copy link
Member Author

mweinelt commented Oct 15, 2024

Currently stuck on this error. Also happens with python312 instead of python3Minimal.

python3>   + Exception Group Traceback (most recent call last):
python3>   |   File "/build/Python-3.13.0/./Tools/jit/build.py", line 28, in <module>
python3>   |     args.target.build(pathlib.Path.cwd(), comment=comment, force=args.force)
python3>   |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 214, in build
python3>   |     stencil_groups = asyncio.run(self._build_stencils())
python3>   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/runners.py", line 194, in run
python3>   |     return runner.run(main)
python3>   |            ^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/runners.py", line 118, in run
python3>   |     return self._loop.run_until_complete(task)
python3>   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
python3>   |     return future.result()
python3>   |            ^^^^^^^^^^^^^^^
python3>   |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 189, in _build_stencils
python3>   |     async with asyncio.TaskGroup() as group:
python3>   |                ^^^^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/taskgroups.py", line 145, in __aexit__
python3>   |     raise me from None
python3>   | ExceptionGroup: unhandled errors in a TaskGroup (2 sub-exceptions)
python3>   +-+---------------- 1 ----------------
python3>     | Traceback (most recent call last):
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 181, in _compile
python3>     |     return await self._parse(o)
python3>     |            ^^^^^^^^^^^^^^^^^^^^
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 89, in _parse
python3>     |     self._handle_section(wrapped_section["Section"], group)
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 330, in _handle_section
python3>     |     value, base = group.symbols[section["Info"]]
python3>     |                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
python3>     | KeyError: 6
python3>     +---------------- 2 ----------------
python3>     | Traceback (most recent call last):
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 181, in _compile
python3>     |     return await self._parse(o)
python3>     |            ^^^^^^^^^^^^^^^^^^^^
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 89, in _parse
python3>     |     self._handle_section(wrapped_section["Section"], group)
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 330, in _handle_section
python3>     |     value, base = group.symbols[section["Info"]]
python3>     |                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
python3>     | KeyError: 6
python3>     +------------------------------------
python3> Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x7ffff6d66d40>
python3> Traceback (most recent call last):
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_subprocess.py", line 126, in __del__
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_subprocess.py", line 104, in close
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/unix_events.py", line 568, in close
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/unix_events.py", line 592, in _close
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_events.py", line 795, in call_soon
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_events.py", line 541, in _check_closed
python3> RuntimeError: Event loop is closed
python3> make: *** [Makefile:3027: jit_stencils.h] Error 1

@mweinelt mweinelt requested a review from natsukium October 15, 2024 12:49
@mweinelt mweinelt linked an issue Oct 15, 2024 that may be closed by this pull request
@rennsax
Copy link
Member

rennsax commented Dec 24, 2024

Currently stuck on this error. Also happens with python312 instead of python3Minimal.

python3>   + Exception Group Traceback (most recent call last):
python3>   |   File "/build/Python-3.13.0/./Tools/jit/build.py", line 28, in <module>
python3>   |     args.target.build(pathlib.Path.cwd(), comment=comment, force=args.force)
python3>   |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 214, in build
python3>   |     stencil_groups = asyncio.run(self._build_stencils())
python3>   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/runners.py", line 194, in run
python3>   |     return runner.run(main)
python3>   |            ^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/runners.py", line 118, in run
python3>   |     return self._loop.run_until_complete(task)
python3>   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
python3>   |     return future.result()
python3>   |            ^^^^^^^^^^^^^^^
python3>   |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 189, in _build_stencils
python3>   |     async with asyncio.TaskGroup() as group:
python3>   |                ^^^^^^^^^^^^^^^^^^^
python3>   |   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/taskgroups.py", line 145, in __aexit__
python3>   |     raise me from None
python3>   | ExceptionGroup: unhandled errors in a TaskGroup (2 sub-exceptions)
python3>   +-+---------------- 1 ----------------
python3>     | Traceback (most recent call last):
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 181, in _compile
python3>     |     return await self._parse(o)
python3>     |            ^^^^^^^^^^^^^^^^^^^^
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 89, in _parse
python3>     |     self._handle_section(wrapped_section["Section"], group)
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 330, in _handle_section
python3>     |     value, base = group.symbols[section["Info"]]
python3>     |                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
python3>     | KeyError: 6
python3>     +---------------- 2 ----------------
python3>     | Traceback (most recent call last):
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 181, in _compile
python3>     |     return await self._parse(o)
python3>     |            ^^^^^^^^^^^^^^^^^^^^
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 89, in _parse
python3>     |     self._handle_section(wrapped_section["Section"], group)
python3>     |   File "/build/Python-3.13.0/Tools/jit/_targets.py", line 330, in _handle_section
python3>     |     value, base = group.symbols[section["Info"]]
python3>     |                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
python3>     | KeyError: 6
python3>     +------------------------------------
python3> Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x7ffff6d66d40>
python3> Traceback (most recent call last):
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_subprocess.py", line 126, in __del__
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_subprocess.py", line 104, in close
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/unix_events.py", line 568, in close
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/unix_events.py", line 592, in _close
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_events.py", line 795, in call_soon
python3>   File "/nix/store/wv9yil24n76q102qwfqjrn735ra7158d-python3-minimal-3.12.6/lib/python3.12/asyncio/base_events.py", line 541, in _check_closed
python3> RuntimeError: Event loop is closed
python3> make: *** [Makefile:3027: jit_stencils.h] Error 1

Same error here on x86_64-linux. There may be some bugs when the JIT builder tries to parse ELF objects.

JIT compilation just works on aarch64-darwin, with minor fix I may post later.


Edit:

This derivation is buildable on aarch64-darwin on my MacBook M1 Pro with macOS 15 Sequoia.

# Python 3.13.1 with JIT compiler.
with import (builtins.fetchTarball "https://github.com/NixOS/nixpkgs/tarball/890e5f24567d6d940b14241c8e05cb28129e3c59") { };
let
  llvmPackages = llvmPackages_18;
in
python313.overrideAttrs (
  final: prev: {
    nativeBuildInputs = [
      llvmPackages.libllvm      # this must be prepended
      llvmPackages.clang
    ] ++ prev.nativeBuildInputs;

    env = prev.env // {
      PYTHON_FOR_REGEN = lib.getExe pkgsBuildBuild.python3Minimal;
    };

    configureFlags = prev.configureFlags ++ [ "--enable-experimental-jit=yes-off" ];
  }
)

@@ -161,6 +167,9 @@ let
pythonOnBuildForHost
] ++ optionals (stdenv.cc.isClang && (!stdenv.hostPlatform.useAndroidPrebuilt or false) && (enableLTO || enableOptimizations)) [
stdenv.cc.cc.libllvm.out
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To adapt for darwin system, this should be modified because it overrides llvm at the last of nativeBuildInputs. Python 3.13.1 tries to find llvm-readobj-18 and libllvm.out defaults to 16, which is not acceptable.

A possible solution is adding a condition && (!jitSupport), but this is a little ugly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to support this if it pins a specific llvm version. We have 19 on master for both darwin and linux in the meantime.

@rennsax
Copy link
Member

rennsax commented Mar 3, 2025

I've posted an issue to CPython: python/cpython#130673

@rennsax
Copy link
Member

rennsax commented Mar 10, 2025

This PR probably fixes our problem and it almost reaches the final stage.

@mweinelt
Copy link
Member Author

mweinelt commented Mar 15, 2025

This PR probably fixes our problem and it almost reaches the final stage.

Picked and confirmed it builds on x86_64-linux and aarch64-darwin for python314FreeThreading. Python 3.13.2 still pins to llvm 18, and I think we may just want to skip it.

Unfortunately I had no luck confirming that the JIT actually works.

@rennsax
Copy link
Member

rennsax commented Mar 26, 2025

Python 3.13.2 still pins to llvm 18, and I think we may just want to skip it.

CPython 3.14 will pin to LLVM 19. Since LLVM is only used for building the JIT compiler (in fact, a header file jit_stencils.h), maybe we can separate the building process? For example, when building the header file, use LLVM 18 for CPython 3.13 (or LLVM 19 for CPython 3.14). When building other parts, use the default version.

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Apr 2, 2025
@rennsax
Copy link
Member

rennsax commented May 8, 2025

This patch is backported (python/cpython#131749). So configureFlags = prev.configureFlags ++ [ "--enable-experimental-jit=yes-off" ]; and adding LLVM to nativeBuildInputs should just work.

@mweinelt
Copy link
Member Author

And in 3.13.3, which is on master. Unfortunately I'm currently a bit swamped.

@ofborg ofborg bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label May 11, 2025
@mweinelt
Copy link
Member Author

mweinelt commented May 11, 2025

Rebased and updated to use llvm/clang 18. Please give it a whirl, if you can.

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Jun 18, 2025
@mweinelt
Copy link
Member Author

mweinelt commented Nov 1, 2025

Rebased.

@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 11.by: package-maintainer This PR was created by a maintainer of all the package it changes. and removed 2.status: merge conflict This PR has merge conflicts with the target branch labels Nov 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: python Python is a high-level, general-purpose programming language. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 11.by: package-maintainer This PR was created by a maintainer of all the package it changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build failure: python313 --enable-experimental-jit

4 participants