93 Commits

Author SHA1 Message Date
Ken Jin
4fa80ce74c gh-139109: A new tracing JIT compiler frontend for CPython (GH-140310)
This PR changes the current JIT model from trace projection to trace recording. Benchmarking: better pyperformance (about 1.7% overall) geomean versus current https://raw.githubusercontent.com/facebookexperimental/free-threading-benchmarking/refs/heads/main/results/bm-20251108-3.15.0a1%2B-7e2bc1d-JIT/bm-20251108-vultr-x86_64-Fidget%252dSpinner-tracing_jit-3.15.0a1%2B-7e2bc1d-vs-base.svg, 100% faster Richards on the most improved benchmark versus the current JIT. Slowdown of about 10-15% on the worst benchmark versus the current JIT. **Note: the fastest version isn't the one merged, as it relies on fixing bugs in the specializing interpreter, which is left to another PR**. The speedup in the merged version is about 1.1%. https://raw.githubusercontent.com/facebookexperimental/free-threading-benchmarking/refs/heads/main/results/bm-20251112-3.15.0a1%2B-f8a764a-JIT/bm-20251112-vultr-x86_64-Fidget%252dSpinner-tracing_jit-3.15.0a1%2B-f8a764a-vs-base.svg

Stats: 50% more uops executed, 30% more traces entered the last time we ran them. It also suggests our trace lengths for a real trace recording JIT are too short, as a lot of trace too long aborts https://github.com/facebookexperimental/free-threading-benchmarking/blob/main/results/bm-20251023-3.15.0a1%2B-eb73378-CLANG%2CJIT/bm-20251023-vultr-x86_64-Fidget%252dSpinner-tracing_jit-3.15.0a1%2B-eb73378-pystats-vs-base.md .

This new JIT frontend is already able to record/execute significantly more instructions than the previous JIT frontend. In this PR, we are now able to record through custom dunders, simple object creation, generators, etc. None of these were done by the old JIT frontend. Some custom dunders uops were discovered to be broken as part of this work gh-140277

The optimizer stack space check is disabled, as it's no longer valid to deal with underflow.

Pros:
* Ignoring the generated tracer code as it's automatically created, this is only additional 1k lines of code. The maintenance burden is handled by the DSL and code generator.
* `optimizer.c` is now significantly simpler, as we don't have to do strange things to recover the bytecode from a trace.
* The new JIT frontend is able to handle a lot more control-flow than the old one.
* Tracing is very low overhead. We use the tail calling interpreter/computed goto interpreter to switch between tracing mode and non-tracing mode. I call this mechanism dual dispatch, as we have two dispatch tables dispatching to each other. Specialization is still enabled while tracing.
* Better handling of polymorphism. We leverage the specializing interpreter for this.

Cons:
* (For now) requires tail calling interpreter or computed gotos. This means no Windows JIT for now :(. Not to fret, tail calling is coming soon to Windows though https://github.com/python/cpython/pull/139962

Design:
* After each instruction, the `record_previous_inst` function/label is executed. This does as the name suggests.
* The tracing interpreter lowers bytecode to uops directly so that it can obtain "fresh" values at the point of lowering.
* The tracing version behaves nearly identical to the normal interpreter, in fact it even has specialization! This allows it to run without much of a slowdown when tracing. The actual cost of tracing is only a function call and writes to memory.
* The tracing interpreter uses the specializing interpreter's deopt to naturally form the side exit chains. This allows it to side exit chain effectively, without repeating much code. We force a re-specializing when tracing a deopt.
* The tracing interpreter can even handle goto errors/exceptions, but I chose to disable them for now as it's not tested.
* Because we do not share interpreter dispatch, there is should be no significant slowdown to the original specializing interpreter on tailcall and computed got with JIT disabled. With JIT enabled, there might be a slowdown in the form of the JIT trying to trace.
* Things that could have dynamic instruction pointer effects are guarded on. The guard deopts to a new instruction --- `_DYNAMIC_EXIT`.
2025-11-13 18:08:32 +00:00
Savannah Ostrowski
1f381a579c Add details about JIT build infrastructure and updating dependencies to Tools/jit (#141167) 2025-11-12 18:26:50 +00:00
Savannah Ostrowski
d162c42790 GH-140479: Update JIT builds to use LLVM 21 (#140973) 2025-11-12 18:09:25 +00:00
Savannah Ostrowski
42d0140860 GH-136895: Fixes for pulling LLVM as a release artifact (#141002) 2025-11-06 11:58:01 -08:00
Savannah Ostrowski
57f4d09a6f JIT: Fix compiler warning from visibility attribute in typedef (#139981) 2025-11-03 19:46:53 +00:00
Savannah Ostrowski
4e2ff4ac4c GH-136895: Update JIT builds to use LLVM 20 (#140329)
Co-authored-by: Emma Harper Smith <emma@emmatyping.dev>
2025-11-03 10:01:44 -08:00
Ken Jin
f701f98052 gh-140312: Set lltrace on JIT debug builds (GH-140313)
Co-authored-by: Mark Shannon <mark@hotpy.org>
2025-11-01 16:22:59 +00:00
Mark Shannon
61e759c2ee GH-135904: JIT compiler: Support 19 bit branch instructions on AArch64 for Mach-O. (GH-140453)
* Insert labels into assembly for custom relocation during stencil creation.
2025-10-23 16:45:57 +01:00
Mark Shannon
f1883852ed GH-135904: Implement assembler optimization for AArch64. (GH-139855) 2025-10-17 11:26:17 +01:00
danigm
96c59a6e42 gh-138497: Support LLVM_VERSION configuration via env (#138498)
Co-authored-by: Savannah Ostrowski <savannah@python.org>
2025-10-07 07:54:31 -07:00
Savannah Bailey
f0d8583303 GH-139040: Add Dev Container instructions in JIT README (#139041) 2025-09-22 09:34:02 -07:00
Savannah Bailey
85c1ef6477 GH-137218: Update make for JIT stencils (#137265) 2025-09-19 10:36:42 +01:00
Brandt Bucher
55e29a6100 GH-129805: Clean up some changes from GH-129806 (GH-133540) 2025-09-16 11:29:09 +01:00
alm
d3d94e0ed7 gh-138061: Exclude __pycache__ directory from the computed digest in the JIT stencils (#138131)
Exclude the __pycache__ directory when generating the digest in the JIT stencils
2025-08-30 22:21:25 +01:00
Mark Shannon
a8d9d94784 GH-137959: Replace shim code in jitted code with a single trampoline function. (GH-137961) 2025-08-21 10:40:53 +01:00
Mark Shannon
e7b55f564d GH-136410: Faster side exits by using a cold exit stub (GH-136411) 2025-08-01 16:26:07 +01:00
Brandt Bucher
3d8c38f6db GH-135904: Improve the JIT's performance on macOS (GH-136528) 2025-07-14 10:14:20 -07:00
Brandt Bucher
c49dc3bd0f GH-115802: Optimize JIT stencils for size (GH-136393) 2025-07-09 12:11:28 -07:00
Brandt Bucher
0e5d096130 GH-135904: Optimize the JIT's assembly control flow (GH-135905) 2025-06-27 08:20:51 -07:00
Zanie Blue
2b0c684e07 GH-134273: Allow setting JIT compiler flags at build time with CFLAGS_JIT (GH134276) 2025-06-12 16:11:08 -07:00
Brandt Bucher
7ad90463df GH-133779: Fix finding pyconfig.h on Windows JIT builds (GH-134349) 2025-05-20 12:32:26 -04:00
Brandt Bucher
57c9810ba4 GH-113464: Add the JIT to What's New (GH-133486) 2025-05-05 20:06:41 -07:00
Mark Shannon
ac7d5ba96e GH-133231: Changes to executor management to support proposed sys._jit module (GH-133287)
* Track the current executor, not the previous one, on the thread-state. 

* Batch executors for deallocation to avoid having to constantly incref executors; this is an ad-hoc form of deferred reference counting.
2025-05-04 10:05:35 +01:00
Brandt Bucher
bfcbb28223 GH-113464: Get LLVM from cpython-bin-deps on Windows (GH-133278) 2025-05-02 11:17:15 -07:00
Savannah Ostrowski
26c0248b54 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>
2025-04-30 11:03:57 -07:00
Lysandros Nikolaou
60202609a2 gh-132661: Implement PEP 750 (#132662)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Wingy <git@wingysam.xyz>
Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
Co-authored-by: Dave Peck <davepeck@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Paul Everitt <pauleveritt@me.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
2025-04-30 11:46:41 +02:00
Mark Shannon
ccf1b0b1c1 GH-132508: Use tagged integers on the evaluation stack for the last instruction offset (GH-132545) 2025-04-29 18:00:35 +01:00
sobolevn
87b1ea016b Update pre-commit repos (#132792) 2025-04-22 18:32:19 +03:00
Bojun Ren
a26a301f8b GH-130673: Gracefully handle missing sections in JIT build (GH-130906) 2025-03-25 16:35:39 -07:00
Diego Russo
ea0453ee97 GH-130887: Always remove trailing jumps in AArch64 JIT stencils (GH-131042) 2025-03-25 10:15:36 -07:00
Victor Stinner
49fb75c676 gh-131238: Add missing pycore_function.h includes for JIT compiler (#131571) 2025-03-21 23:37:49 +00:00
Mark Shannon
7ebd71ee14 GH-131498: Remove conditional stack effects (GH-131499)
* Adds some missing #includes
2025-03-20 15:39:38 +00:00
Diego Russo
02de9cb9a8 gh-129964: Fix JIT crash on Windows on Arm (GH-130882) 2025-03-05 12:40:50 -08:00
Brandt Bucher
5d8db36bbb GH-115802: JIT using the "medium" code model on x86_64-unknown-linux-gnu (GH-130097) 2025-03-05 11:54:33 -08:00
sobolevn
422f8e9e02 gh-129805: Fix bytes annotation in Tools/jit (#129806) 2025-02-17 10:55:30 +03:00
Brandt Bucher
05e89c34bd GH-115869: Don't JIT zeroed bytes (GH-130023) 2025-02-13 10:51:03 -08:00
Brandt Bucher
70e387c990 GH-129709: Clean up tier two (GH-129710) 2025-02-07 09:52:49 -08:00
Brandt Bucher
fbaa6c8ff0 GH-129763: Remove the LLTRACE macro (GH-129764) 2025-02-07 08:49:51 -08:00
Ken Jin
cb640b659e gh-128563: A new tail-calling interpreter (GH-128718)
Co-authored-by: Garrett Gu <garrettgu777@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2025-02-06 23:21:57 +08:00
Stan Ulbrych
652f66ac38 gh-129438: Update `--enable-experimental-jit` section with install requirements (#129450)
Add a note to the `JIT` docs that building CPython with `JIT` requires Python 3.11 or newer.

Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
2025-01-30 12:16:05 +02:00
Diego Russo
7c5a6f67c7 Enable native AArch64 Ubuntu CI jobs (#127584)
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
2024-12-04 14:12:06 -08:00
Brandt Bucher
17c16aea66 GH-115869: Make jit_stencils.h reproducible (GH-127166) 2024-11-24 14:42:50 -08:00
Ken Jin
6293d00e72 gh-120619: Strength reduce function guards, support 2-operand uop forms (GH-124846)
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
2024-11-09 11:35:33 +08:00
Savannah Ostrowski
c3a12ae13e GH-125911: Rename big trampoline to "shim" (GH-126339) 2024-11-05 15:26:46 -08:00
Peter Bierma
1371295e67 gh-126366: Fix crash if __iter__ raises an exception during yield from (#126369) 2024-11-05 15:26:36 +05:30
Savannah Ostrowski
c29bbe2101 GH-125498: Update JIT builds to use LLVM 19 and preserve_none (GH-125499) 2024-10-30 12:03:31 -07:00
Michael Droettboom
c8fd4b12e3 gh-125207: Fix MSVC 1935 build with JIT (#125209)
* gh-125207: Use {0} array initializers

* Simplify, as suggested in PR

* Revert change to explicitly specify length
2024-10-18 15:51:29 -04:00
Diego Russo
b85923a0fe GH-119726: Deduplicate AArch64 trampolines within a trace (GH-123872) 2024-10-02 12:07:20 -07:00
Savannah Ostrowski
6737333ac5 GH-124820: Move -mno-outline-atomics flag to aarch64 Linux JIT builds only (#124821) 2024-10-02 11:40:59 +02:00
Savannah Ostrowski
65f1237098 GH-123516: Improve JIT memory consumption by invalidating cold executors (GH-124443)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-09-27 00:35:42 +00:00