summaryrefslogtreecommitdiffstats
path: root/Python/optimizer.c
Commit message (Collapse)AuthorAgeFilesLines
* GH-135379: Top of stack caching for the JIT. (GH-135465)Mark Shannon2025-12-111-51/+132
| | | | Uses three registers to cache values at the top of the evaluation stack This significantly reduces memory traffic for smaller, more common uops.
* gh-137007: Track executor before any possible deallocations (GH-137016)Ken Jin2025-12-101-1/+4
|
* gh-141976: Protect against non-progressing specializations in tracing JIT ↵Ken Jin2025-12-101-0/+19
| | | | (GH-141989)
* gh-139109: JIT _EXIT_TRACE to ENTER_EXECUTOR rather than _DEOPT (GH-141573)Ken Jin2025-11-151-3/+3
|
* gh-139109: A new tracing JIT compiler frontend for CPython (GH-140310)Ken Jin2025-11-131-484/+581
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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`.
* gh-140936: Fix JIT assertion crash at finalization if some generator is ↵Mikhail Efimov2025-11-121-1/+7
| | | | alive (GH-140969)
* gh-138050: [WIP] JIT - Streamline MAKE_WARM - move coldness check to ↵alm2025-10-271-0/+9
| | | | executor creation (GH-138240)
* Correct a simple NULL-check in `optimizer.c`'s `uop_item()` (GH-140069)Maurycy Pawłowski-Wieroński2025-10-141-1/+1
|
* gh-111489: Remove _PyTuple_FromArray() alias (#139973)Victor Stinner2025-10-111-1/+1
| | | | Replace _PyTuple_FromArray() with PyTuple_FromArray(). Remove pycore_tuple.h includes.
* gh-133171: Re-enable JUMP_BACKWARD to free-threading build (gh-137800)Donghee Na2025-09-241-0/+4
|
* GH-138378: Move globals-to-consts pass into main optimizer pass (GH-138379)Mark Shannon2025-09-181-1/+1
|
* gh-137838: Move _PyUOpInstruction buffer to PyInterpreterState (gh-138918)Donghee Na2025-09-171-2/+16
|
* gh-137838: Fix JIT trace buffer overrun by increasing possible exit stubs ↵Donghee Na2025-09-091-5/+8
| | | | (gh-138177)
* GH-137959: Replace shim code in jitted code with a single trampoline ↵Mark Shannon2025-08-211-2/+0
| | | | function. (GH-137961)
* GH-137573: mark _PyOptimizer_Optimize as no inline (GH-137731)Sachin Shah2025-08-141-1/+2
| | | | Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
* GH-132532: Add new DSL macros to better declare semantics of exits at ends ↵Mark Shannon2025-08-091-5/+16
| | | | of instructions/uops. (GH-137098)
* GH-136410: Faster side exits by using a cold exit stub (GH-136411)Mark Shannon2025-08-011-6/+59
|
* GH-135379: Support limited scalar replacement for replicated uops in the JIT ↵Mark Shannon2025-06-171-2/+2
| | | | | | code generator. (GH-135563) * Use it to support efficient specializations of COPY and SWAP in the JIT.
* GH-133231: Changes to executor management to support proposed `sys._jit` ↵Mark Shannon2025-05-041-4/+62
| | | | | | | | 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.
* GH-131726: Split up _CHECK_VALIDITY_AND_SET_IP (GH-131810)Brandt Bucher2025-04-011-7/+5
|
* gh-111178: fix UBSan failures for `_PyExecutorObject` (#131610)Bénédikt Tran2025-03-241-15/+21
|
* gh-131238: Add missing pycore_function.h includes for JIT compiler (#131571)Victor Stinner2025-03-211-0/+1
|
* GH-131498: Remove conditional stack effects (GH-131499)Mark Shannon2025-03-201-5/+5
| | | * Adds some missing #includes
* gh-115999: Add free-threaded specialization for FOR_ITER (#128798)T. Wouters2025-03-121-0/+1
| | | | 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.)
* GH-130296: Avoid stack transients in four instructions. (GH-130310)Mark Shannon2025-02-281-5/+22
| | | | | | | | | * 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
* GH-129715: Don't project traces that return to an unknown caller (GH-130024)Brandt Bucher2025-02-121-1/+1
|
* gh-100239: replace BINARY_SUBSCR & family by BINARY_OP with oparg NB_SUBSCR ↵Irit Katriel2025-02-071-1/+1
| | | | (#129700)
* GH-129715: Remove _DYNAMIC_EXIT (GH-129716)Brandt Bucher2025-02-071-13/+5
|
* GH-128682: Spill the stack pointer in labels, as well as instructions ↵Mark Shannon2025-02-041-1/+2
| | | | (GH-129618)
* GH-128682: Make `PyStackRef_CLOSE` escaping. (GH-129404)Mark Shannon2025-02-031-1/+0
|
* GH-126599: Remove the PyOptimizer API (GH-129194)Brandt Bucher2025-01-291-90/+7
|
* GH-128914: Remove all but one conditional stack effects (GH-129226)Mark Shannon2025-01-271-1/+2
| | | | | | | | | | | | | * Remove all 'if (0)' and 'if (1)' conditional stack effects * Use array instead of conditional for BUILD_SLICE args * Refactor LOAD_GLOBAL to use a common conditional uop * Remove conditional stack effects from LOAD_ATTR specializations * Replace conditional stack effects in LOAD_ATTR with a 0 or 1 sized array. * Remove conditional stack effects from CALL_FUNCTION_EX
* Revert "GH-128914: Remove conditional stack effects from `bytecodes.c` and ↵Sam Gross2025-01-231-1/+4
| | | | | | | the code generators (GH-128918)" (GH-129202) The commit introduced a ~2.5-3% regression in the free threading build. This reverts commit ab61d3f4303d14a413bc9ae6557c730ffdf7579e.
* GH-128914: Remove conditional stack effects from `bytecodes.c` and the code ↵Mark Shannon2025-01-201-4/+1
| | | | generators (GH-128918)
* GH-126599: Remove the "counter" optimizer/executor (GH-126853)Xuanteng Huang2025-01-161-99/+0
|
* GH-128375: Better instrument for `FOR_ITER` (GH-128445)Mark Shannon2025-01-061-3/+9
|
* GH-126833: Dumps graphviz representation of executor graph. (GH-126880)Mark Shannon2024-12-131-1/+135
|
* gh-115999: Specialize `LOAD_GLOBAL` in free-threaded builds (#126607)mpage2024-11-211-2/+12
| | | | | | | | | | | | | | Enable specialization of LOAD_GLOBAL in free-threaded builds. Thread-safety of specialization in free-threaded builds is provided by the following: A critical section is held on both the globals and builtins objects during specialization. This ensures we get an atomic view of both builtins and globals during specialization. Generation of new keys versions is made atomic in free-threaded builds. Existing helpers are used to atomically modify the opcode. Thread-safety of specialized instructions in free-threaded builds is provided by the following: Relaxed atomics are used when loading and storing dict keys versions. This avoids potential data races as the dict keys versions are read without holding the dictionary's per-object lock in version guards. Dicts keys objects are passed from keys version guards to the downstream uops. This ensures that we are loading from the correct offset in the keys object. Once a unicode key has been stored in a keys object for a combined dictionary in free-threaded builds, the offset that it is stored in will never be reused for a different key. Once the version guard passes, we know that we are reading from the correct offset. The dictionary read fast-path is used to read values from the dictionary once we know the correct offset.
* gh-120619: Strength reduce function guards, support 2-operand uop forms ↵Ken Jin2024-11-091-11/+11
| | | | | (GH-124846) Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
* GH-118093: Don't lose confidence when tracing through 100% biased branches ↵Brandt Bucher2024-10-021-4/+2
| | | | (GH-124813)
* GH-123516: Improve JIT memory consumption by invalidating cold executors ↵Savannah Ostrowski2024-09-271-0/+42
| | | | | (GH-124443) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
* gh-123923: Defer refcounting for `f_funcobj` in `_PyInterpreterFrame` (#124026)Sam Gross2024-09-241-1/+1
| | | | | | Use a `_PyStackRef` and defer the reference to `f_funcobj` when possible. This avoids some reference count contention in the common case of executing the same code object from multiple threads concurrently in the free-threaded build.
* GH-118093: Specialize `CALL_KW` (GH-123006)Mark Shannon2024-08-161-1/+1
|
* GH-122390: Replace `_Py_GetbaseOpcode` with `_Py_GetBaseCodeUnit` (GH-122942)Mark Shannon2024-08-131-17/+2
|
* GH-118093: Handle some polymorphism before requiring progress in tier two ↵Brandt Bucher2024-08-121-32/+53
| | | | (GH-122843)
* GH-118095: Add tier two support for BINARY_SUBSCR_GETITEM (GH-120793)Mark Shannon2024-08-011-5/+7
|
* Replace PyObject_Del with PyObject_Free (#122453)Victor Stinner2024-08-011-1/+1
| | | | PyObject_Del() is just a alias to PyObject_Free() kept for backward compatibility. Use directly PyObject_Free() instead.
* GH-118093: Improve handling of short and mid-loop traces (GH-122252)Brandt Bucher2024-07-291-33/+31
|
* GH-122294: Burn in the addresses of side exits (GH-122295)Brandt Bucher2024-07-261-4/+6
|
* GH-118093: Add tier two support for BINARY_OP_INPLACE_ADD_UNICODE (GH-122253)Brandt Bucher2024-07-251-0/+9
|