cpython/Modules
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
..
_ctypes Remove unreachable break statements in _ctypes_test.c (#140585) 2025-10-25 14:59:12 +00:00
_decimal gh-111389: replace deprecated occurrences of _PyHASH_* macros (#141236) 2025-11-09 15:14:08 +01:00
_hacl gh-140120: Refresh HACL* to fix an hmac memory leak (GH-140188) 2025-10-16 05:25:51 +00:00
_io gh-141311: Avoid assertion in BytesIO.readinto() (GH-141333) 2025-11-12 11:57:05 +02:00
_multiprocessing gh-138342: Move _PyObject_VisitType() to the internal C API (#139734) 2025-10-08 12:10:58 +02:00
_sqlite gh-139327: consolidate sqlite3_finalize and sqlite3_reset usages (GH-139329) 2025-10-15 15:18:07 +02:00
_sre gh-140979: Fix off-by-one error in the RE code validator (GH-140984) 2025-11-04 17:49:44 +02:00
_ssl gh-137583: Only lock the SSL context, not the SSL socket (GH-137588) 2025-08-10 14:47:11 +00:00
_testcapi gh-140550: Initial implementation of PEP 793 – PyModExport (GH-140556) 2025-11-05 12:31:42 +01:00
_testinternalcapi Remove internal _PyTime_AsLong() function (#141053) 2025-11-05 18:37:06 +01:00
_testlimitedcapi gh-132657: add regression test for PySet_Contains with unhashable type (#141411) 2025-11-12 20:50:08 +05:30
_xxtestfuzz gh-131253: free-threaded build support for pystats (gh-137189) 2025-11-03 11:36:37 -08:00
_zstd gh-139877: Use PyBytesWriter in pycore_blocks_output_buffer.h (#139976) 2025-10-14 10:03:55 -07:00
cjkcodecs gh-101828: Fix jisx0213 codecs removing null characters (gh-139340) 2025-10-14 22:55:00 +09:00
clinic gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
expat gh-139312: Update bundled libexpat to 2.7.3 (GH-139319) 2025-09-26 19:44:36 -07:00
_abc.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
_asynciomodule.c gh-141376: Rename _AsyncioDebug to _Py_AsyncioDebug (GH-141391) 2025-11-12 09:42:56 +01:00
_bisectmodule.c GH-135763: AC: Use `Py_ssize_t(allow_negative=False)` (#138394) 2025-09-02 21:29:05 +01:00
_bz2module.c gh-116738: Use PyMutex for bz2 module (gh-140555) 2025-10-27 09:52:30 -04:00
_codecsmodule.c gh-55531: Implement normalize_encoding in C (#136643) 2025-10-30 15:31:47 +01:00
_collectionsmodule.c gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614) 2025-06-30 11:14:31 +00:00
_csv.c gh-137986: Fix and improve the csv functions docstrings (GH-137987) 2025-08-24 11:13:31 +03:00
_curses_panel.c gh-116946: add Py_TPFLAGS_IMMUTABLETYPE to several internal types (#138582) 2025-09-11 09:56:20 +02:00
_cursesmodule.c gh-129813, PEP 782: Use PyBytesWriter in _curses (#138920) 2025-09-15 16:24:34 +02:00
_datetimemodule.c gh-139774: use relaxed atomics for datetime hashes (#139775) 2025-10-08 19:49:54 +05:30
_dbmmodule.c gh-116738: Add critical section to dbm/gdbm context manager (gh-140391) 2025-10-22 11:16:28 -04:00
_elementtree.c gh-140061: Use _PyObject_IsUniquelyReferenced() to check if objects are uniquely referenced (gh-140062) 2025-10-15 09:48:21 -04:00
_functoolsmodule.c GH-140590: Fix setstate for functools.partial C-module (GH-140671) 2025-10-28 10:28:32 +01:00
_gdbmmodule.c gh-116738: Add critical section to dbm/gdbm context manager (gh-140391) 2025-10-22 11:16:28 -04:00
_hashopenssl.c gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl (#138922) 2025-09-15 18:41:43 +02:00
_heapqmodule.c Fix comments for heapq.siftup_max (#135359) 2025-07-03 15:21:41 +05:30
_interpchannelsmodule.c gh-140306: Fix memory leaks in cross-interpreter data handling (GH-140307) 2025-10-19 22:24:28 +03:00
_interpqueuesmodule.c gh-140306: Fix memory leaks in cross-interpreter data handling (GH-140307) 2025-10-19 22:24:28 +03:00
_interpreters_common.h gh-132775: Use _PyObject_GetXIData (With Fallback) (gh-134440) 2025-05-22 06:50:06 -06:00
_interpretersmodule.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
_json.c gh-137821: Improve Argument Clinic definitions in the `_json` module (#140780) 2025-10-30 10:32:08 +00:00
_localemodule.c gh-130567: Remove optimistic allocation in locale.strxfrm() (GH-137143) 2025-10-16 09:54:41 +02:00
_lsprof.c Remove internal _PyTime_AsLong() function (#141053) 2025-11-05 18:37:06 +01:00
_lzmamodule.c gh-116738: use PyMutex in lzma module (#140711) 2025-11-12 02:01:55 +05:30
_math.h gh-122681: remove m_atan2()/c_atan2() helpers (#122715) 2024-08-17 13:48:16 +05:30
_opcode.c gh-135755: Move SPECIAL_ constants to a private header (GH-135922) 2025-06-25 13:03:05 +02:00
_operator.c gh-131238: Add explicit includes to pycore headers (#131257) 2025-03-17 12:32:43 +01:00
_pickle.c gh-129813, PEP 782: Use PyBytesWriter in pickle and struct (#138833) 2025-09-13 18:26:49 +02:00
_posixsubprocess.c fix comment reference from man 7 signal to man 7 signal-safety (#138554) 2025-09-06 18:40:15 +00:00
_queuemodule.c gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614) 2025-06-30 11:14:31 +00:00
_randommodule.c Fix a compiler warning in _randommodule.c (#141058) 2025-11-05 18:00:32 +00:00
_remote_debugging_module.c Fix compiler warnings in remote debugging (#141060) 2025-11-05 20:18:45 +01:00
_scproxy.c gh-116322: Add Py_mod_gil module slot (#116882) 2024-05-03 11:30:55 -04:00
_ssl.c gh-139929: fix incorrect OpenSSL version-based guard in _ssl.c (GH-139945) 2025-10-11 10:34:08 -07:00
_ssl.h GH-103092: isolate _ssl (#104725) 2023-05-22 06:14:48 +05:30
_ssl_data_35.h gh-139573: Update OpenSSL in CI (GH-139577) 2025-10-04 19:43:17 -05:00
_ssl_data_111.h gh-131423: Update OpenSSL data to 3.4.1 on Linux (#131618) 2025-04-25 10:26:58 +02:00
_ssl_data_300.h gh-127330: Update for OpenSSL 3.4 & document+improve the update process (GH-127331) 2024-11-28 13:29:27 +01:00
_ssl_data_340.h gh-131423: Update OpenSSL data to 3.4.1 on Linux (#131618) 2025-04-25 10:26:58 +02:00
_stat.c gh-108765: fix comment about macro definitions in _stat.c post GH-108854 (#136027) 2025-06-27 15:15:11 +00:00
_statisticsmodule.c gh-116322: Add Py_mod_gil module slot (#116882) 2024-05-03 11:30:55 -04:00
_struct.c gh-140260: fix data race in _struct module initialization with subinterpreters (#140909) 2025-11-13 16:31:31 +05:30
_suggestions.c gh-116738: Make _suggestions module thread-safe (gh-140321) 2025-10-22 09:14:48 +09:00
_sysconfig.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
_testbuffer.c gh-136300: Modify C tests to conform to PEP-737 (GH-136301) 2025-07-11 15:18:35 +02:00
_testcapi_feature_macros.inc gh-91325: Skip Stable ABI checks with Py_TRACE_REFS special build (GH-92046) 2024-01-29 16:45:31 +01:00
_testcapimodule.c gh-140550: Initial implementation of PEP 793 – PyModExport (GH-140556) 2025-11-05 12:31:42 +01:00
_testclinic.c gh-111489: Remove _PyTuple_FromArray() alias (#139973) 2025-10-11 22:58:14 +02:00
_testclinic_limited.c gh-116322: Rename PyModule_ExperimentalSetGIL to PyUnstable_Module_SetGIL (GH-118645) 2024-05-06 18:59:36 +02:00
_testimportmultiple.c gh-116322: Add Py_mod_gil module slot (#116882) 2024-05-03 11:30:55 -04:00
_testinternalcapi.c gh-139109: A new tracing JIT compiler frontend for CPython (GH-140310) 2025-11-13 18:08:32 +00:00
_testlimitedcapi.c gh-111495: Add PyFile tests (#129449) 2025-01-30 18:05:32 +01:00
_testmultiphase.c gh-140550: Initial implementation of PEP 793 – PyModExport (GH-140556) 2025-11-05 12:31:42 +01:00
_testsinglephase.c gh-140550: Initial implementation of PEP 793 – PyModExport (GH-140556) 2025-11-05 12:31:42 +01:00
_threadmodule.c GH-116946: revert eliminate the need for the GC in the _thread.lock and _thread.RLock (#141448) 2025-11-12 07:21:43 +00:00
_tkinter.c gh-124111: Fix TCL 9 thread detection (GH-128103) 2025-11-12 22:16:58 +00:00
_tracemalloc.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
_typesmodule.c gh-136492: Add FrameLocalsProxyType to types (GH-136546) 2025-07-20 20:49:00 +02:00
_typingmodule.c gh-105499: Merge typing.Union and types.UnionType (#105511) 2025-03-04 11:44:19 -08:00
_uuidmodule.c gh-132710: only use stable _uuid.generate_time_safe() to deduce MAC address (#132901) 2025-05-26 10:56:31 +02:00
_weakref.c gh-120974: Make _asyncio._leave_task atomic in the free-threaded build (#122139) 2024-07-23 17:06:03 +00:00
_winapi.c gh-129813, PEP 782: Use PyBytesWriter in _winapi.PeekNamedPipe() (#138930) 2025-09-15 21:25:47 +01:00
_zoneinfo.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
addrinfo.h gh-114917: add support for AI_NUMERICSERV in getaddrinfo emulation (#114918) 2025-03-18 11:26:51 +01:00
arraymodule.c gh-140474: Fix memory leak in array.array (GH-140478) 2025-10-23 05:49:27 -04:00
atexitmodule.c gh-140080: Clear atexit callbacks when memory allocation fails during finalization (GH-140103) 2025-10-15 09:49:55 -04:00
binascii.c gh-129813, PEP 782: Use PyBytesWriter in binascii (#138825) 2025-09-13 18:25:16 +02:00
blake2module.c gh-138342: Move _PyObject_VisitType() to the internal C API (#139734) 2025-10-08 12:10:58 +02:00
cmathmodule.c gh-141004: soft-deprecate Py_INFINITY macro (#141033) 2025-11-12 13:44:49 +01:00
config.c.in gh-128384: Use a context variable for warnings.catch_warnings (gh-130010) 2025-04-09 16:18:54 -07:00
errnomodule.c gh-126585: Add EHWPOISON error code (#126586) 2024-12-03 12:45:50 +00:00
faulthandler.c gh-137725: Convert faulthandler to Argument Clinic (#137726) 2025-08-16 15:16:04 +02:00
fcntlmodule.c gh-140615: Update docstrings in the fcntl module (GH-140619) 2025-11-08 12:17:59 +02:00
gc_weakref.txt GH-135552: Make the GC clear weakrefs later (GH-136189) 2025-08-07 16:32:17 -07:00
gcmodule.c gh-111489: Remove _PyTuple_FromArray() alias (#139973) 2025-10-11 22:58:14 +02:00
getaddrinfo.c gh-114917: Fix typo in getaddrinfo emulation (#131413) 2025-03-18 14:31:13 +01:00
getbuildinfo.c gh-100388: Change undefined __DATE__ to the Unix epoch (#100389) 2025-03-03 14:59:46 +01:00
getnameinfo.c Replace K&R function declarations with ANSI declarations (#114818) 2025-03-11 13:06:36 +01:00
getpath.c GH-131296: Avoid invalid signed char comparison in getpath.c assertion (GH-131594) 2025-04-22 14:06:30 +01:00
getpath.py gh-120037: Disable user site packages when a ._pth file is used (GH-137428) 2025-08-19 21:28:28 +01:00
getpath_noop.c bpo-45582: Port getpath[p].c to Python (GH-29041) 2021-12-03 00:08:42 +00:00
grpmodule.c gh-116738: Make grp module thread-safe (#135434) 2025-07-14 11:18:41 -07:00
hashlib.h gh-131876: Revert "gh-131876: extract _hashlib helpers into a separate directory (#136995) (#137307) 2025-08-01 10:45:40 -07:00
hmacmodule.c gh-116946: remove unnecessary gc from immutable types (#139073) 2025-10-01 13:15:58 +05:30
itertoolsmodule.c gh-140061: Use _PyObject_IsUniquelyReferenced() to check if objects are uniquely referenced (gh-140062) 2025-10-15 09:48:21 -04:00
ld_so_aix.in Issue #10656: Fix out-of-tree building on AIX 2016-11-20 07:56:37 +00:00
main.c gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035) 2025-05-28 20:11:09 +03:00
makesetup gh-116943: Ensure makesetup does not choke on missing EOL in Setup.* files (#139100) 2025-09-18 09:25:01 +00:00
makexp_aix bpo-42087: Remove support for AIX 5.3 and below (GH-22830) 2020-11-16 16:16:10 +01:00
mathintegermodule.c gh-81313: Add the math.integer module (PEP-791) (GH-133909) 2025-10-31 16:13:43 +02:00
mathmodule.c gh-141004: soft-deprecate Py_INFINITY macro (#141033) 2025-11-12 13:44:49 +01:00
md5module.c gh-138342: Move _PyObject_VisitType() to the internal C API (#139734) 2025-10-08 12:10:58 +02:00
mmapmodule.c gh-116738: make mmap module thread-safe (#139237) 2025-10-09 12:00:47 +05:30
overlapped.c GH-131296: Remove unused variable from overlapped.c (GH-131590) 2025-04-16 22:44:57 +01:00
posixmodule.c gh-137109: refactor warning about threads when forking (#141438) 2025-11-12 18:15:16 -08:00
posixmodule.h gh-85283: Convert grp extension to the limited C API (#116611) 2024-03-12 00:46:53 +00:00
pwdmodule.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
pyexpat.c gh-140593: Fix a memory leak in function my_ElementDeclHandler of pyexpat (#140602) 2025-10-26 13:45:32 +00:00
readline.c gh-140398: fix memory leaks in readline module when PySys_Audit fails (#140400) 2025-10-21 13:24:34 +05:30
README
resource.c gh-137512: Add new constants in the resource module (GH-137513) 2025-08-19 08:52:45 +02:00
rotatingtree.c gh-116181: Remove Py_BUILD_CORE_BUILTIN and Py_BUILD_CORE_MODULE in rotatingtree.c (#121260) 2024-07-03 13:05:05 +05:30
rotatingtree.h bpo-32150: Expand tabs to spaces in C files. (#4583) 2017-11-28 17:56:10 +02:00
selectmodule.c gh-67795: Accept any real numbers as timestamp and timeout (GH-139224) 2025-09-23 21:31:42 +03:00
Setup gh-81313: Add the math.integer module (PEP-791) (GH-133909) 2025-10-31 16:13:43 +02:00
Setup.bootstrap.in gh-136421: Load _datetime static types during interpreter initialization (GH-136583) 2025-07-21 13:47:26 -04:00
Setup.stdlib.in gh-140550: Initial implementation of PEP 793 – PyModExport (GH-140556) 2025-11-05 12:31:42 +01:00
sha1module.c gh-138342: Move _PyObject_VisitType() to the internal C API (#139734) 2025-10-08 12:10:58 +02:00
sha2module.c gh-138342: Move _PyObject_VisitType() to the internal C API (#139734) 2025-10-08 12:10:58 +02:00
sha3module.c gh-138342: Move _PyObject_VisitType() to the internal C API (#139734) 2025-10-08 12:10:58 +02:00
signalmodule.c gh-67795: Accept any real numbers as timestamp and timeout (GH-139224) 2025-09-23 21:31:42 +03:00
socketmodule.c gh-100218: correctly set errno when socket.if_{nametoindex,indextoname} raise OSError (#140905) 2025-11-09 13:45:38 +01:00
socketmodule.h gh-86819: Add ISO-TP CAN socket constants (#23794) 2025-09-17 17:18:04 +00:00
symtablemodule.c gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
syslogmodule.c gh-116738: Make syslog module thread-safe (#136760) 2025-07-21 09:24:42 -07:00
termios.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
timemodule.c Remove internal _PyTime_AsLong() function (#141053) 2025-11-05 18:37:06 +01:00
tkappinit.c gh-103538: Remove unused TK_AQUA code (GH-103539) 2023-05-10 18:53:13 +00:00
tkinter.h gh-103532: Remove TKINTER_PROTECT_LOADTK code (GH-103535) 2023-04-14 09:04:16 -05:00
unicodedata.c gh-129117: Add unicodedata.isxidstart() function (#140269) 2025-10-30 10:18:12 +00:00
unicodedata_db.h closes gh-138706: update Unicode to 17.0.0 (#138719) 2025-09-11 09:58:39 -07:00
unicodename_db.h closes gh-138706: update Unicode to 17.0.0 (#138719) 2025-09-11 09:58:39 -07:00
winreparse.h bpo-31512: Add non-elevated symlink support for Windows (GH-3652) 2019-04-09 11:19:46 -07:00
xxlimited.c gh-134160: Add more comments for the xxlimited module (GH-140214) 2025-10-27 11:39:42 +01:00
xxlimited_35.c gh-111178: Fix function signatures for multiple tests (#131496) 2025-03-20 12:27:03 +01:00
xxmodule.c gh-122040: reword Modules/xxmodule.c module-level comment (#132201) 2025-04-07 08:03:03 +00:00
xxsubtype.c gh-111178: fix UBSan failures in Modules/xx*.c (GH-129797) 2025-02-25 13:02:32 +01:00
zlibmodule.c gh-76007: Deprecate zlib.__version__ attribute (#140130) 2025-10-15 13:18:48 +02:00

Source files for standard library extension modules,
and former extension modules that are now builtin modules.