Commit Graph

192 Commits

Author SHA1 Message Date
Donghee Na 794f758cd8 gh-141504: Refactor policy object into a single opt_config (gh-143644) 2026-01-15 09:53:00 +09:00
Mohammad Miadh Angkad 499706b843 GH-143842: Make optimizer color table static (GH-143846) 2026-01-14 20:37:13 +00:00
Ken Jin e370c8db52 gh-143123: Protect against recursive tracer calls/finalization (GH-143126)
* Stronger check for recursive traces

* Add a stop_tracing field

* Stop early when tracing exceptions
2026-01-14 12:23:14 +00:00
Mark Shannon 6db952eae9 GH-143613: Add colours and some more edges to executor visualization graph (GH-143809) 2026-01-14 11:34:58 +00:00
Hai Zhu 94dbce1397 gh-138050: Use cold flag instead of warm flag in MAKE_WARM (GH-143827) 2026-01-14 10:27:33 +00:00
Nadeshiko Manju e2f0160026 gh-143604: Hold strong reference to executor during JIT tracing (GH-143646)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2026-01-10 11:15:48 +00:00
Ken Jin e0fb278064 gh-143421: Allocate all JIT state in one go (GH-143626) 2026-01-09 19:00:49 +00:00
Ken Jin b852236b26 gh-143421: Lazily allocate tracer code and opt buffers (GH-143597) 2026-01-09 16:56:19 +00:00
Hai Zhu aeb3403563 gh-143421: Move JitOptContext from stack allocation to per-thread heap allocation (GH-143536)
* move JitOptContext to _PyThreadStateImpl
* make _PyUOpInstruction buffer a part of _PyThreadStateImpl

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2026-01-08 19:38:21 +00:00
Donghee Na 12283f6373 gh-141504: Factor out tracing and optimization heuristics into a single object (gh-143381) 2026-01-03 15:22:14 +00:00
Ken Jin 6cb245d260 gh-143183: Link trace to side exits, rather than stop (GH-143268) 2025-12-29 15:10:42 +00:00
Ken Jin daa9aa4c0a gh-143183: Rewind stop tracing to previous target (GH-143187)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-12-28 22:12:31 +00:00
Ken Jin cf6758ff9e gh-143092: Make CALL_LIST_APPEND and BINARY_OP_INPLACE_ADD_UNICODE normal instructions (GH-143124)
These super instructions need many special cases in the interpreter, specializer, and JIT. It's best we convert them to normal instructions.
2025-12-24 22:03:00 +00:00
Mark Shannon 20aeb3a463 GH-143026: Fix assertion error in executor management. (GH-143104) 2025-12-23 17:19:34 +00:00
Ken Jin 665d2807a0 gh-139109: Add terminator to JIT code when halting due to invalid dependencies (#143033)
* Add terminator to JIT code when  halting due to invalid dependencies

* 📜🤖 Added by blurb_it.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2025-12-22 20:54:47 +00:00
Chris Eibl 700e9fad70 GH-142513: fix missing return in executor_clear (GH-143073)
fix missing return in executor_clear
2025-12-22 17:10:52 +00:00
Ken Jin 9ded3dd4e9 gh-142476: Fix Windows crashing with JIT (GH-143021)
Fix Windows crashing with JIT
2025-12-22 14:57:13 +00:00
Shamil e2a7db7175 gh-142476: fix memory leak when creating JIT executors (GH-142492) 2025-12-19 19:07:11 +00:00
Mark Shannon e4058d7cb1 GH-142513: Reimplement executor management (GH-142931)
* Invalidating an executor does not cause arbitrary code to run
* Executors are only freed at safe points
2025-12-18 16:43:44 +00:00
Ken Jin fba4584ffc gh-142849: Fix segfault in executor_to_gv (GH-142885)
Fix segfault in `executor_to_gv`
2025-12-17 17:05:21 +00:00
Ken Jin 89729f2ef7 gh-142543: Mark tracer functions as Py_NO_INLINE (GH-142846) 2025-12-17 00:12:32 +00:00
Mark Shannon 92d4aeafd5 GH-142629: JIT: Fix out of bounds memory read in lltrace (GH-142821)
JIT: Fix out of bounds memory read in lltrace
2025-12-16 19:57:15 +00:00
Mark Shannon 469f191a85 GH-135379: Top of stack caching for the JIT. (GH-135465)
Uses three registers to cache values at the top of the evaluation stack
This significantly reduces memory traffic for smaller, more common uops.
2025-12-11 10:32:52 +00:00
Ken Jin 97e19014dd gh-137007: Track executor before any possible deallocations (GH-137016) 2025-12-11 05:09:56 +08:00
Ken Jin ebf3427615 gh-141976: Protect against non-progressing specializations in tracing JIT (GH-141989) 2025-12-10 19:39:11 +00:00
Ken Jin ed73c909f2 gh-139109: JIT _EXIT_TRACE to ENTER_EXECUTOR rather than _DEOPT (GH-141573) 2025-11-15 20:19:41 +00:00
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
Mikhail Efimov 35ed3e4ced gh-140936: Fix JIT assertion crash at finalization if some generator is alive (GH-140969) 2025-11-12 19:04:02 +00:00
alm 1753ccb432 gh-138050: [WIP] JIT - Streamline MAKE_WARM - move coldness check to executor creation (GH-138240) 2025-10-27 16:37:37 +00:00
Maurycy Pawłowski-Wieroński 3490a99046 Correct a simple NULL-check in optimizer.c's uop_item() (GH-140069) 2025-10-14 16:18:20 +01:00
Victor Stinner 166cdaa6fb gh-111489: Remove _PyTuple_FromArray() alias (#139973)
Replace _PyTuple_FromArray() with PyTuple_FromArray().
Remove pycore_tuple.h includes.
2025-10-11 22:58:14 +02:00
Donghee Na c4f21d7c7c gh-133171: Re-enable JUMP_BACKWARD to free-threading build (gh-137800) 2025-09-24 14:19:17 +09:00
Mark Shannon 3b83257366 GH-138378: Move globals-to-consts pass into main optimizer pass (GH-138379) 2025-09-18 10:09:59 +01:00
Donghee Na d873fb42f3 gh-137838: Move _PyUOpInstruction buffer to PyInterpreterState (gh-138918) 2025-09-17 18:50:16 +01:00
Donghee Na 5edfe55acf gh-137838: Fix JIT trace buffer overrun by increasing possible exit stubs (gh-138177) 2025-09-09 09:51:08 +09: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
Sachin Shah c9d7065188 GH-137573: mark _PyOptimizer_Optimize as no inline (GH-137731)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2025-08-14 08:00:00 +00:00
Mark Shannon af15e1d13e GH-132532: Add new DSL macros to better declare semantics of exits at ends of instructions/uops. (GH-137098) 2025-08-09 15:41:28 +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
Mark Shannon 8dd8b5c2f0 GH-135379: Support limited scalar replacement for replicated uops in the JIT code generator. (GH-135563)
* Use it to support efficient specializations of COPY and SWAP in the JIT.
2025-06-17 13:43:09 +01: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 3a8cefba0b GH-131726: Split up _CHECK_VALIDITY_AND_SET_IP (GH-131810) 2025-04-01 16:55:05 -07:00
Bénédikt Tran f65be097ed gh-111178: fix UBSan failures for _PyExecutorObject (#131610) 2025-03-24 10:53:23 +01: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
T. Wouters de2f7da77d gh-115999: Add free-threaded specialization for FOR_ITER (#128798)
Add free-threaded versions of existing specialization for FOR_ITER (list, tuples, fast range iterators and generators), without significantly affecting their thread-safety. (Iterating over shared lists/tuples/ranges should be fine like before. Reusing iterators between threads is not fine, like before. Sharing generators between threads is a recipe for significant crashes, like before.)
2025-03-12 16:21:46 +01:00
Mark Shannon 54965f3fb2 GH-130296: Avoid stack transients in four instructions. (GH-130310)
* Combine _GUARD_GLOBALS_VERSION_PUSH_KEYS and _LOAD_GLOBAL_MODULE_FROM_KEYS into _LOAD_GLOBAL_MODULE

* Combine _GUARD_BUILTINS_VERSION_PUSH_KEYS and _LOAD_GLOBAL_BUILTINS_FROM_KEYS into _LOAD_GLOBAL_BUILTINS

* Combine _CHECK_ATTR_MODULE_PUSH_KEYS and _LOAD_ATTR_MODULE_FROM_KEYS into _LOAD_ATTR_MODULE

* Remove stack transient in LOAD_ATTR_WITH_HINT
2025-02-28 18:00:38 +00:00
Brandt Bucher 11bb08e4ec GH-129715: Don't project traces that return to an unknown caller (GH-130024) 2025-02-12 10:16:43 -08:00
Irit Katriel a1417b211f gh-100239: replace BINARY_SUBSCR & family by BINARY_OP with oparg NB_SUBSCR (#129700) 2025-02-07 22:39:54 +00:00
Brandt Bucher 5fa7e1b7fd GH-129715: Remove _DYNAMIC_EXIT (GH-129716) 2025-02-07 11:41:17 -08:00