Skip to content

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

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

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

Conversation

@Fidget-Spinner

@Fidget-SpinnerFidget-Spinner commented Nov 15, 2025

Copy link
Copy Markdown
Member

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 threadLib/test/test_capi/test_opt.py
Comment threadObjects/listobject.c Outdated
Comment threadPython/bytecodes.c Outdated
Comment threadPython/bytecodes.c Outdated
Comment threadPython/pylifecycle.c Outdated
Comment threadPython/pylifecycle.c Outdated
Comment threadPython/sysmodule.c Outdated
@Fidget-Spinner

Fidget-Spinner commented Nov 15, 2025

Copy link
Copy Markdown
MemberAuthor

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

@Fidget-Spinner

Fidget-Spinner commented Dec 29, 2025

Copy link
Copy Markdown
MemberAuthor

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

@tomasr8tomasr8 left a comment

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.

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

Comment threadObjects/typeobject.c
Comment threadObjects/funcobject.c Outdated
Comment threadPython/sysmodule.c
Comment threadPython/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
MemberAuthor

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-SpinnerFidget-Spinner changed the title gh-141594: A free-threaded JITgh-141594: A free-threaded JIT (Part 1)Jan 10, 2026
@github-actions

Copy link
Copy Markdown

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

@github-actionsgithub-actionsBot added the stale Stale PR or inactive for long period of time. label May 2, 2026
@merwokmerwok removed the stale Stale PR or inactive for long period of time. label Jun 15, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@Fidget-Spinner@sergey-miryanov@markshannon@corona10@efimov-mikhail@tomasr8@merwok@savannahostrowski@nazeerali4325-commits