Skip to content

gh-141594: A free-threaded JIT (Part 1)#141595

Open
Fidget-Spinner wants to merge 62 commits intopython:mainfrom
Fidget-Spinner:jit_ft
Open

gh-141594: A free-threaded JIT (Part 1)#141595
Fidget-Spinner wants to merge 62 commits intopython:mainfrom
Fidget-Spinner:jit_ft

Conversation

@Fidget-Spinner
Copy link
Copy Markdown
Member

@Fidget-Spinner Fidget-Spinner commented Nov 15, 2025

This PR gives the JIT free-threading support. It is only on for single-threaded code, and turns off automatically on multi-threaded code. All JIT features are turned on, including the optimizer. All tests pass on my system, including TSAN as of 527aac1 usng the FT suppression file, except for the usual spurious race conditions already in existing CPython.

Benchmark results are good. Overall speedup of 5% faster on FT+JIT vs just FT for x86-64 https://github.com/facebookexperimental/free-threading-benchmarking/blob/main/results/bm-20260110-3.15.0a3%2B-5d987e8-JIT%2CNOGIL/bm-20260110-vultr-x86_64-Fidget%252dSpinner-jit_ft-3.15.0a3%2B-5d987e8-vs-base.md

9-10% speedup for macOS AArch64 for FT+JIT vs FT https://github.com/facebookexperimental/free-threading-benchmarking/blob/main/results/bm-20260110-3.15.0a3%2B-5d987e8-JIT%2CNOGIL/bm-20260110-macm4pro-arm64-Fidget%252dSpinner-jit_ft-3.15.0a3%2B-5d987e8-vs-base.md

Design:
Creation of >1 threads cause global invalidation of all executors and disables JIT. Once we drop to 1 thread, JIT is re-enabled.

Two versions of stencils are required --- one for max single threaded perf, one for multi-threaded safety. This just implements the first part (max single threaded perf) stencils.

All with FT+PGO+LTO=thin+TC+JIT + pyperf system tune:

Mean +- std dev: [nogil-float-lock] 69.9 ms +- 1.5 ms -> [nogil-float-nolock] 54.9 ms +- 0.8 ms: 1.27x faster
Mean +- std dev: [nogil-nbody-lock] 139 ms +- 1 ms -> [nogil-nbody-nolock] 115 ms +- 3 ms: 1.21x faster
Mean +- std dev: [nogil-richards-lock] 37.3 ms +- 0.4 ms -> [nogil-richards-nolock] 35.8 ms +- 0.3 ms: 1.04x faster 
Mean +- std dev: [nogil-deltablue-lock] 3.50 ms +- 0.15 ms -> [nogil-deltablue-nolock] 3.36 ms +- 0.14 ms: 1.04x faster

This is on a platform where locking/atomics are somewhat slow (i7-12700h). I removed the lock removal code for this PR to reduce the diff.

Comment thread Lib/test/test_capi/test_opt.py
Comment thread Objects/listobject.c Outdated
Comment thread Python/bytecodes.c Outdated
Comment thread Python/bytecodes.c Outdated
Comment thread Python/pylifecycle.c Outdated
Comment thread Python/pylifecycle.c Outdated
Comment thread Python/sysmodule.c Outdated
@Fidget-Spinner
Copy link
Copy Markdown
Member Author

Fidget-Spinner commented Nov 15, 2025

@sergey-miryanov in the future could you please bundle up your reviews and send them in one review instead of multiple reviews?

@Fidget-Spinner
Copy link
Copy Markdown
Member Author

Fidget-Spinner commented Dec 29, 2025

Latest results for macOS FT+JIT are incredibly good: 8-10% faster on pyperformance versus just FT-only.

https://github.com/facebookexperimental/free-threading-benchmarking/tree/main/results/bm-20251228-3.15.0a3%2B-0678505-JIT%2CNOGIL

Copy link
Copy Markdown
Member

@tomasr8 tomasr8 left a comment

Choose a reason for hiding this comment

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

Not an expert in this area, but everything looks reasonable. I just have a couple of comments/questions as usual :)

Comment thread Objects/typeobject.c
Comment thread Objects/funcobject.c Outdated
Comment thread Python/sysmodule.c
Comment thread Python/pystate.c
Comment on lines +1578 to +1579
// There's more than one thread. In FT mode,
// disable the JIT completely for now.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you think it's feasible to add a test that ensures the JIT is properly disabled and then re-enabled?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added a test, thanks!

@Fidget-Spinner Fidget-Spinner changed the title gh-141594: A free-threaded JIT gh-141594: A free-threaded JIT (Part 1) Jan 10, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 2, 2026

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants