summaryrefslogtreecommitdiffstats
path: root/Python/ceval.c
Commit message (Collapse)AuthorAgeFilesLines
* GH-135379: Top of stack caching for the JIT. (GH-135465)Mark Shannon2025-12-111-7/+36
| | | | Uses three registers to cache values at the top of the evaluation stack This significantly reduces memory traffic for smaller, more common uops.
* gh-138122: Don't sample partial frame chains (#141912)Pablo Galindo Salgado2025-12-071-0/+3
|
* gh-138122: Implement frame caching in RemoteUnwinder to reduce memory reads ↵Pablo Galindo Salgado2025-12-061-0/+10
| | | | | | | | | | | (#142137) This PR implements frame caching in the RemoteUnwinder class to significantly reduce memory reads when profiling remote processes with deep call stacks. When cache_frames=True, the unwinder stores the frame chain from each sample and reuses unchanged portions in subsequent samples. Since most profiling samples capture similar call stacks (especially the parent frames), this optimization avoids repeatedly reading the same frame data from the target process. The implementation adds a last_profiled_frame field to the thread state that tracks where the previous sample stopped. On the next sample, if the current frame chain reaches this marker, the cached frames from that point onward are reused instead of being re-read from remote memory. The sampling profiler now enables frame caching by default.
* gh-142168: explicitly initialize `stack_array` in `_PyEval_Vector` and ↵Kir Chou2025-12-061-2/+2
| | | | | `_PyEvalFramePushAndInit_Ex` (#142192) Co-authored-by: Kir Chou <note351@hotmail.com>
* GH-139757: Fix reference leaks introduced in GH-140800 (GH-142257)Mark Shannon2025-12-041-2/+4
|
* GH-141794: Limit size of generated machine code. (GH-142228)Mark Shannon2025-12-031-1/+283
| | | | | | * Factor out bodies of the largest uops, to reduce jit code size. * Factor out common assert, also reducing jit code size. * Limit size of jitted code for a single executor to 1MB.
* gh-142217: Remove internal _Py_Identifier functions (#142219)Victor Stinner2025-12-031-6/+0
| | | | | | | | | | | | | | | | Remove internal functions: * _PyDict_ContainsId() * _PyDict_DelItemId() * _PyDict_GetItemIdWithError() * _PyDict_SetItemId() * _PyEval_GetBuiltinId() * _PyObject_CallMethodIdNoArgs() * _PyObject_CallMethodIdObjArgs() * _PyObject_CallMethodIdOneArg() * _PyObject_VectorcallMethodId() * _PyUnicode_EqualToASCIIId() These functions were not exported and so no usable outside CPython.
* GH-139653: Only raise an exception (or fatal error) when the stack pointer ↵Mark Shannon2025-11-191-7/+21
| | | | | | | | is about to overflow the stack. (GH-141711) Only raises if the stack pointer is both below the limit *and* above the stack base. This prevents false positives for user-space threads, as the stack pointer will be outside those bounds if the stack has been swapped.
* GH-139109: Support switch/case dispatch with the tracing interpreter. ↵Mark Shannon2025-11-181-0/+4
| | | | (GH-141703)
* GH-139914: Handle stack growth direction on HPPA (GH-140028)Stefano Rivera2025-11-171-3/+40
| | | | | | Adapted from a patch for Python 3.14 submitted to the Debian BTS by John https://bugs.debian.org/1105111#20 Co-authored-by: John David Anglin <dave.anglin@bell.net>
* gh-139653: Remove assertions in _Py_InitializeRecursionLimits() (#141551)Victor Stinner2025-11-141-7/+0
| | | | These checks were invalid and failed randomly on FreeBSD and Alpine Linux.
* gh-139109: A new tracing JIT compiler frontend for CPython (GH-140310)Ken Jin2025-11-131-6/+49
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-139653: Add PyUnstable_ThreadState_SetStackProtection() (#139668)Victor Stinner2025-11-131-7/+70
| | | | | | | | Add PyUnstable_ThreadState_SetStackProtection() and PyUnstable_ThreadState_ResetStackProtection() functions to set the stack base address and stack size of a Python thread state. Co-authored-by: Petr Viktorin <encukou@gmail.com>
* gh-140530: fix a reference leak in an error path for `raise exc from cause` ↵Bénédikt Tran2025-11-091-0/+1
| | | | | | (#140908) Fix a reference leak in `raise E from T` when `T` is an exception subtype for which `T.__new__` does not return an exception instance.
* gh-140373: Correctly emit `PY_UNWIND` event when generator is closed (GH-140767)Mikhail Efimov2025-10-311-0/+4
|
* gh-136327: Fix inconsistent ``TypeError`` messages regarding invalid values ↵Tapeline2025-10-241-20/+7
| | | | after * and ** (#136395)
* GH-139193: Fix dump_stack when PYTHON_LLTRACE=4 (GH-139384)Sergey Miryanov2025-10-221-0/+4
|
* 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-139291: Fix C stack limits by factoring out finding hardware stack limits ↵Mark Shannon2025-10-071-28/+32
| | | | (GH-139294)
* gh-139231: Fix estimation of available stack size for recursion limit on ↵Rok Mandeljc2025-09-241-0/+7
| | | | | macOS (GH-139232) Use `pthread_get_stackaddr_np()` and `pthread_get_stacksize_np()` to determine the stack address and size.
* gh-139109: Dynamic opcode targets (GH-139111)Ken Jin2025-09-181-4/+5
| | | | Make opcode targets table dynamic
* gh-135755: Make Py_TAIL_CALL_INTERP macro private (#138981)Victor Stinner2025-09-181-5/+5
| | | Rename Py_TAIL_CALL_INTERP to _Py_TAIL_CALL_INTERP.
* GH-137959: Replace shim code in jitted code with a single trampoline ↵Mark Shannon2025-08-211-28/+35
| | | | function. (GH-137961)
* gh-137400: Fix thread-safety issues when profiling all threads (gh-137518)Sam Gross2025-08-131-30/+8
| | | | | | | | | | There were a few thread-safety issues when profiling or tracing all threads via PyEval_SetProfileAllThreads or PyEval_SetTraceAllThreads: * The loop over thread states could crash if a thread exits concurrently (in both the free threading and default build) * The modification of `c_profilefunc` and `c_tracefunc` wasn't thread-safe on the free threading build.
* GH-132532: Add new DSL macros to better declare semantics of exits at ends ↵Mark Shannon2025-08-091-0/+1
| | | | of instructions/uops. (GH-137098)
* GH-136410: Faster side exits by using a cold exit stub (GH-136411)Mark Shannon2025-08-011-1/+1
|
* gh-131338: Disable computed stack limit checks on non-glibc linux (#134336)R. David Murray2025-07-281-1/+5
| | | | Co-authored-by: Kumar Aditya <kumaraditya@python.org> Co-authored-by: Victor Stinner <vstinner@python.org>
* gh-134043: use `_PyObject_GetMethodStackRef` in pattern matching (#136356)Kumar Aditya2025-07-081-4/+6
|
* gh-130396: Move PYOS_LOG2_STACK_MARGIN to internal headers (#135928)Victor Stinner2025-07-011-7/+7
| | | | | | Move PYOS_LOG2_STACK_MARGIN, PYOS_STACK_MARGIN, PYOS_STACK_MARGIN_BYTES and PYOS_STACK_MARGIN_SHIFT macros to pycore_pythonrun.h internal header. Add underscore (_) prefix to the names to make them private. Rename _PYOS to _PyOS.
* gh-135755: Move SPECIAL_ constants to a private header (GH-135922)Petr Viktorin2025-06-251-1/+1
| | | Macros without a `Py`/`_Py` prefix should not be defined in public headers.
* gh-135443: Sometimes Fall Back to __main__.__dict__ For Globals (gh-135491)Eric Snow2025-06-161-3/+116
| | | | | | | | | | | | | | | | | For several builtin functions, we now fall back to __main__.__dict__ for the globals when there is no current frame and _PyInterpreterState_IsRunningMain() returns true. This allows those functions to be run with Interpreter.call(). The affected builtins: * exec() * eval() * globals() * locals() * vars() * dir() We take a similar approach with "stateless" functions, which don't use any global variables.
* GH-132554: Fix tier2 `FOR_ITER` implementation and optimizations (GH-135137)Mark Shannon2025-06-051-2/+26
|
* gh-135161: Remove redundant NULL check for 'exc' after dereference in ↵rialbat2025-06-051-1/+1
| | | | ceval.c (#135162)
* gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)Serhiy Storchaka2025-05-281-1/+1
| | | | | Add functions PySys_GetAttr(), PySys_GetAttrString(), PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
* gh-127266: avoid data races when updating type slots (gh-133177)Neil Schemenauer2025-05-281-0/+14
| | | | | | In the free-threaded build, avoid data races caused by updating type slots or type flags after the type was initially created. For those (typically rare) cases, use the stop-the-world mechanism. Remove the use of atomics when reading or writing type flags.
* GH-132554: "Virtual" iterators (GH-132555)Mark Shannon2025-05-271-0/+20
| | | | | | * FOR_ITER now pushes either the iterator and NULL or leaves the iterable and pushes tagged zero * NEXT_ITER uses the tagged int as the index into the sequence or, if TOS is NULL, iterates as before.
* GH-130397: remove special-casing of C stack depth for WASI (#134469)Brett Cannon2025-05-221-3/+0
| | | | | Removed special-casing for WASI when setting C stack depth limits. Since WASI has its own C stack checking this isn't a security risk. Also disabled some tests that stopped passing. They all happened to have already been disabled under Emscripten.
* GH-133231: Changes to executor management to support proposed `sys._jit` ↵Mark Shannon2025-05-041-19/+31
| | | | | | | | 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.
* Remove duplicate includes: Python/{bytecodes,ceval,optimizer_analysis}.c ↵Adam Turner2025-05-011-1/+0
| | | | (#132622)
* GH-124715: Move trashcan mechanism into `Py_Dealloc` (GH-132280)Mark Shannon2025-04-301-6/+0
|
* gh-132661: Implement PEP 750 (#132662)Lysandros Nikolaou2025-04-301-0/+2
| | | | | | | | | | | | | 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>
* GH-132508: Use tagged integers on the evaluation stack for the last ↵Mark Shannon2025-04-291-0/+4
| | | | instruction offset (GH-132545)
* Revert gh-127266: avoid data races when updating type slots (gh-131174) ↵Neil Schemenauer2025-04-291-14/+0
| | | | | (gh-133129) This is triggering deadlocks in test_opcache. See GH-133130 for stack trace.
* gh-127266: avoid data races when updating type slots (gh-131174)Neil Schemenauer2025-04-281-0/+14
| | | | | | | | In the free-threaded build, avoid data races caused by updating type slots or type flags after the type was initially created. For those (typically rare) cases, use the stop-the-world mechanism. Remove the use of atomics when reading or writing type flags. The use of atomics is not sufficient to avoid races (since flags are sometimes read without a lock and without atomics) and are no longer required.
* gh-132758: Fix tail call and pystats builds (GH-132759)Ken Jin2025-04-231-2/+10
|
* gh-128398: improve error messages when incorrectly using `with` and `async ↵Bénédikt Tran2025-04-191-8/+66
| | | | | | | with` (#132218) Improve the error message with a suggestion when an object supporting the synchronous (resp. asynchronous) context manager protocol is entered using `async with` (resp. `with`) instead of `with` (resp. `async with`).
* gh-129987: Selectively re-enable SLP autovectorization of ↵T. Wouters2025-04-151-4/+8
| | | | | | | | _PyEval_EvalFrameDefault (#132530) Only disable SLP autovectorization of `_PyEval_EvalFrameDefault` on newer GCCs, as the optimization bug seems to exist only on GCC 12 and later, and before GCC 9 disabling the optimization has a dramatic performance impact.
* gh-131624: Fix posix_spawn tests failing on NetBSD with stack limit ↵Furkan Onder2025-04-131-1/+1
| | | | | | assertions (GH-131625) Fix recursive limit assertions on NetBSD for posix_spawn.
* gh-132386: Fix a crash when passing a dict subclass to `exec` (GH-132412)Tomas R.2025-04-111-0/+2
| | | | | * Fix crash when passing a dict subclass to exec * Add news entry
* GH-131296: Suppress "unused label" warning for clang-cl closer to actual ↵Chris Eibl2025-04-101-2/+2
| | | | occurrence (GH-131900)