cpython/Lib
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
..
__phello__
_pyrepl gh-141415: Remove unused variables and comment in _pyrepl.windows_console.py (#141416) 2025-11-11 14:27:21 -05:00
asyncio gh-103847: fix cancellation safety of asyncio.create_subprocess_exec (#140805) 2025-11-12 10:47:38 +05:30
collections gh-138682: Add symmetric difference to Counter (gh-138766) 2025-09-26 00:04:49 -05:00
compression gh-132983: Split `_zstd_set_c_parameters` (#133921) 2025-05-28 14:45:08 +00:00
concurrent gh-139462: Make the ProcessPoolExecutor BrokenProcessPool exception report which child process terminated (GH-139486) 2025-11-11 22:09:58 +00:00
ctypes gh-140041: Fix import of ctypes on Android and Cygwin when ABI flags are present (#140178) 2025-10-16 05:40:39 +08:00
curses gh-133575: eliminate legacy checks in Lib/curses/__init__.py (#133576) 2025-05-07 20:28:32 +02:00
dbm gh-135386: Fix "unable to open database file" errors on readonly DB (GH-135566) 2025-08-22 14:11:59 +03:00
email gh-136702: Deprecate passing non-ascii *encoding* (str) to encodings.normalize_encoding (#140030) 2025-11-09 13:37:34 +01:00
encodings gh-136702: Deprecate passing non-ascii *encoding* (str) to encodings.normalize_encoding (#140030) 2025-11-09 13:37:34 +01:00
ensurepip gh-140874: Upgrade bundled pip to 25.3 (GH-140876) 2025-11-01 10:25:19 +00:00
html gh-137836: Support more RAWTEXT and PLAINTEXT elements in HTMLParser (GH-137837) 2025-10-31 17:44:02 +02:00
http gh-139434: Update selected RFC 2822 references to RFC 5322 (#139435) 2025-11-04 14:46:07 -05:00
idlelib Minor fixes to idle.rst and regenerate help.html (#140037) 2025-11-06 03:21:02 -05:00
importlib gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
json gh-140793: Improve documentatation and tests for the ensure_ascii option in the json module (GH-140906) 2025-11-08 12:07:27 +02:00
logging gh-138162: Fix logging.LoggerAdapter with merge_extra=True and without the extra argument (GH-140511) 2025-10-30 12:52:02 +02:00
multiprocessing gh-98896: resource_tracker: use json&base64 to allow arbitrary shared memory names (GH-138473) 2025-11-12 13:35:01 +01:00
pathlib GH-139174: Prepare pathlib.Path.info for new methods (part 2) (#140155) 2025-10-18 02:13:25 +01:00
profiling gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
pydoc_data Python 3.15.0a1 2025-10-14 13:46:08 +03:00
re gh-140797: Forbid capturing groups in re.Scanner lexicon patterns (GH-140944) 2025-11-04 12:54:28 +02:00
site-packages
sqlite3 gh-133390: sqlite3 CLI completion for tables, columns, indices, triggers, views, functions, schemata (GH-136101) 2025-10-24 08:26:36 +02:00
string GH-132661: Add `string.templatelib.convert()` (#135217) 2025-07-15 11:56:42 +02:00
sysconfig Replace obsolete platforms with more recent examples (#132455) 2025-10-10 05:38:13 +00:00
test gh-139109: A new tracing JIT compiler frontend for CPython (GH-140310) 2025-11-13 18:08:32 +00:00
tkinter gh-140481: Improve error message when trying to iterate a Tk widget, image or font (GH-140501) 2025-10-30 13:11:56 +02:00
tomllib gh-133117: Enable stricter mypy checks for tomllib (#133206) 2025-05-03 16:57:09 +03:00
turtledemo
unittest gh-137716: Fix double period in AttributeError message for invalid mock assertions (#137717) 2025-08-14 21:26:02 +00:00
urllib gh-140691: urllib.request: Close FTP control socket if data socket can't connect (GH-140835) 2025-11-05 11:52:11 +01:00
venv gh-133951: Remove lib64->lib symlink in venv creation (#137139) 2025-10-04 14:55:17 +01:00
wsgiref gh-133810: remove http.server.CGIHTTPRequestHandler and --cgi flag (#133811) 2025-05-17 09:58:16 +02:00
xml gh-135640: Adds more type checking to ElementTree (GH-135643) 2025-07-03 10:48:47 +03:00
xmlrpc gh-136839: Refactor simple dict.update calls (#136811) 2025-07-19 10:12:10 -07:00
zipfile gh-139700: Check consistency of the zip64 end of central directory record (GH-139702) 2025-10-07 20:15:26 +03:00
zoneinfo gh-137976: Explicitly exclude localtime from available_timezones (#138012) 2025-09-18 17:32:14 +01:00
__future__.py
__hello__.py
_aix_support.py
_android_support.py Make Android streams respect the unbuffered (-u) option (#138806) 2025-09-18 11:41:21 +01:00
_apple_support.py
_ast_unparse.py gh-138774: use value to ast.unparse code when str is None in ast.Interpolation (#139415) 2025-10-23 13:56:05 +00:00
_collections_abc.py gh-118803: Make ByteString deprecations louder; remove ByteString from typing.__all__ and collections.abc.__all__ (#139127) 2025-09-18 18:58:16 +00:00
_colorize.py GH-139946: Colorize error and warning messages in argparse (#140695) 2025-11-04 16:31:35 +00:00
_compat_pickle.py gh-133810: remove http.server.CGIHTTPRequestHandler and --cgi flag (#133811) 2025-05-17 09:58:16 +02:00
_ios_support.py
_markupbase.py
_opcode_metadata.py GH-134282: Always borrow references LOAD_CONST (GH-134284) 2025-05-20 11:24:11 -04:00
_osx_support.py
_py_abc.py
_py_warnings.py gh-140691: urllib.request: Close FTP control socket if data socket can't connect (GH-140835) 2025-11-05 11:52:11 +01:00
_pydatetime.py gh-97517: Add documentation links to datetime strftime/strptime docstrings (#138559) 2025-09-15 19:50:46 +01:00
_pydecimal.py gh-76007: Deprecate __version__ attribute in decimal (#140302) 2025-10-26 12:01:04 +01:00
_pyio.py gh-140607: Validate returned byte count in RawIOBase.read (#140611) 2025-10-27 18:06:46 +00:00
_pylong.py
_sitebuiltins.py
_strptime.py gh-81148: Eliminate unnecessary check in _strptime when determining AM/PM (#13428) 2025-09-19 10:23:12 +00:00
_threading_local.py gh-107006: Move threading.local docstring to docs (#131840) 2025-05-05 15:00:15 +03:00
_weakrefset.py
abc.py
annotationlib.py gh-138151: Fix annotationlib handling of multiple nonlocals (#138164) 2025-11-03 07:22:32 -08:00
antigravity.py
argparse.py GH-139946: Colorize error and warning messages in argparse (#140695) 2025-11-04 16:31:35 +00:00
ast.py gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
base64.py gh-138775: fix handle python -m base64 stdin correct with EOF signal (GH-138776) 2025-11-11 23:47:57 +00:00
bdb.py gh-136980: Remove unused C tracing code in bdb (#136981) 2025-07-24 09:58:54 +09:00
bisect.py
bz2.py gh-132983: Introduce compression package and move _compression module (GH-133018) 2025-04-27 14:41:30 -07:00
calendar.py gh-140212: Add html for year-month option in Calendar (#140230) 2025-10-31 17:28:53 +02:00
cmd.py gh-133363: Fix Cmd completion for lines beginning with ! (#133364) 2025-05-03 22:50:37 -04:00
code.py gh-135103: Remove an unused local variable in Lib/code.py (GH-135104) 2025-06-04 13:57:31 +09:00
codecs.py gh-52876: Implement missing parameter in codecs.StreamReaderWriter functions (#136498) 2025-07-10 17:42:14 +02:00
codeop.py gh-132449: Improve syntax error messages for keywords with typos (#132450) 2025-04-22 11:01:55 +02:00
colorsys.py
compileall.py gh-130645: Add color to stdlib argparse CLIs (gh-133380) 2025-05-05 19:46:46 +02:00
configparser.py gh-65697: Improved error msg for configparser key validation (#135527) 2025-06-15 12:13:19 -04:00
contextlib.py
contextvars.py
copy.py gh-132657: improve deepcopy and copy scaling on free-threading (#138429) 2025-09-04 13:20:23 +05:30
copyreg.py gh-132882: Fix copying of unions with members that do not support __or__ (#132883) 2025-04-24 16:49:09 +00:00
cProfile.py gh-138122: Implement PEP 799 (#138142) 2025-08-27 17:52:50 +01:00
csv.py gh-137627: Make csv.Sniffer.sniff() delimiter detection 1.6x faster (#137628) 2025-10-23 15:28:29 +03:00
dataclasses.py gh-137530: generate an __annotate__ function for dataclasses __init__ (GH-137711) 2025-11-10 14:57:11 +01:00
datetime.py
decimal.py gh-76007: Deprecate __version__ attribute in decimal (#140302) 2025-10-26 12:01:04 +01:00
difflib.py gh-95953: Add a css class to changed lines of difflib.HtmlDiff make_table (#139226) 2025-09-22 13:19:37 +00:00
dis.py gh-130645: Add color to stdlib argparse CLIs (gh-133380) 2025-05-05 19:46:46 +02:00
doctest.py gh-136914: Fix support of cached functions and properties in DocTest's lineno computation (GH-136930) 2025-07-25 12:46:12 +03:00
enum.py gh-140766: [Enum] add show_flag_values and bin to enum.__all__ (GH-140765) 2025-10-30 10:32:55 -07:00
filecmp.py
fileinput.py
fnmatch.py gh-133306: Use \z instead of \Z in fnmatch.translate() and glob.translate() (GH-133338) 2025-05-03 17:58:21 +03:00
fractions.py gh-87790: support thousands separators for formatting fractional part of Fraction (#132204) 2025-07-07 11:16:31 +03:00
ftplib.py
functools.py gh-140873: Add support of non-descriptor callables in functools.singledispatchmethod() (GH-140884) 2025-11-13 19:48:52 +02:00
genericpath.py gh-71189: Support all-but-last mode in os.path.realpath() (GH-117562) 2025-07-30 10:19:19 +03:00
getopt.py
getpass.py gh-138514: getpass: restrict echo_char to a single ASCII character (#138591) 2025-09-16 16:21:55 +02:00
gettext.py
glob.py docs: be clearer that glob results are unordered (#140184) 2025-10-19 16:16:35 -04:00
graphlib.py
gzip.py Remove some dead code from gzip and tarfile (#138123) 2025-08-25 16:23:47 +03:00
hashlib.py gh-136565: use SHA-256 for hashlib.__doc__ example instead of MD5 (#138157) 2025-08-26 10:38:53 +00:00
heapq.py Remove unnecessary slice in heapq.py (gh-139087) 2025-09-18 00:52:46 -05:00
hmac.py gh-136912: fix handling of OverflowError in hmac.digest (#136917) 2025-07-26 08:22:06 +00:00
imaplib.py gh-76007: Deprecate __version__ attribute in imaplib (#140299) 2025-10-20 15:20:44 +03:00
inspect.py gh-131116: Fix inspect.getdoc() to work with cached_property objects (GH-131165) 2025-11-12 10:07:21 +00:00
io.py gh-132952: Speed up startup by importing _io instead of io (#132957) 2025-04-28 08:38:56 -07:00
ipaddress.py gh-76007: Deprecate __version__ attribute (#138675) 2025-09-29 12:03:23 +03:00
keyword.py
linecache.py gh-140228: Avoid making unnecessary syscalls in linecache for frozen modules (#140377) 2025-10-28 23:06:22 -07:00
locale.py gh-137729: Fix support for locales with @-modifiers (GH-137253) 2025-08-18 10:11:15 +03:00
lzma.py gh-132983: Introduce compression package and move _compression module (GH-133018) 2025-04-27 14:41:30 -07:00
mailbox.py gh-140808: Remove __class_getitem__ from mailbox._ProxyFile (#140838) 2025-11-02 13:56:59 -08:00
mimetypes.py gh-87710: Update mime type for `.ai` (#141239) 2025-11-12 16:22:01 +02:00
modulefinder.py gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
netrc.py gh-135823: improve error message in netrc security checks (#135827) 2025-06-23 12:49:27 +02:00
ntpath.py gh-136065: Fix quadratic complexity in os.path.expandvars() (GH-134952) 2025-10-31 14:49:51 +01:00
nturl2path.py
numbers.py gh-122450: Expand documentation for `Rational and Fraction` (#136800) 2025-08-04 02:15:59 +00:00
opcode.py gh-131738: optimize builtin any/all/tuple calls with a generator expression arg (#131737) 2025-03-28 10:35:20 +00:00
operator.py
optparse.py gh-76007: Deprecate __version__ attribute (#138675) 2025-09-29 12:03:23 +03:00
os.py gh-120057: add os.reload_environ to __all__ (#140763) 2025-10-29 21:21:26 +00:00
pdb.py Remove redundant sys.exit(2) call in pdb CLI (#139948) 2025-11-02 18:47:18 -08:00
pickle.py gh-76007: Deprecate __version__ attribute (#138675) 2025-09-29 12:03:23 +03:00
pickletools.py gh-139646: fix typo in pickletools error message (#139647) 2025-10-06 10:52:45 +00:00
pkgutil.py
platform.py gh-76007: Deprecate __version__ attribute (#138675) 2025-09-29 12:03:23 +03:00
plistlib.py
poplib.py
posixpath.py gh-136065: Fix quadratic complexity in os.path.expandvars() (GH-134952) 2025-10-31 14:49:51 +01:00
pprint.py GH-90117: Check for list and tuple before MappingView in pprint (GH-135779) 2025-06-24 14:41:41 -07:00
profile.py gh-138122: Implement PEP 799 (#138142) 2025-08-27 17:52:50 +01:00
pstats.py gh-140137: Handle empty collections in profiling.sampling (#140154) 2025-10-15 14:59:12 +01:00
pty.py
py_compile.py gh-130645: Add color to stdlib argparse CLIs (gh-133380) 2025-05-05 19:46:46 +02:00
pyclbr.py
pydoc.py gh-132686: Add parameters inherit_class_doc and fallback_to_class_doc for inspect.getdoc() (GH-132691) 2025-11-12 00:01:25 +02:00
queue.py Fix Queue.shutdown docs for condition to unblock a join (gh-137088) 2025-07-25 07:56:28 -06:00
quopri.py
random.py Minor edit: Move comments closer to the code they describe (gh-136477) 2025-07-09 10:23:46 -07:00
reprlib.py gh-135487: fix reprlib.Repr.repr_int when given very large integers (#135506) 2025-06-24 11:09:46 +00:00
rlcompleter.py
runpy.py gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
sched.py
secrets.py
selectors.py
shelve.py gh-99631: Add custom loads and dumps support for the shelve module (#118065) 2025-07-12 14:27:32 +02:00
shlex.py gh-138804: Check type in shlex.quote (GH-138809) 2025-09-12 14:26:21 -04:00
shutil.py gh-132983: Add missing references to Zstandard in shutil docstrings (GH-136617) 2025-07-23 18:09:53 +00:00
signal.py
site.py gh-138993: Dedent credits text (#138994) 2025-10-14 11:15:17 +03:00
smtplib.py gh-139434: Update selected RFC 2822 references to RFC 5322 (#139435) 2025-11-04 14:46:07 -05:00
socket.py gh-99813: Start using SSL_sendfile when available (#99907) 2025-07-12 12:42:35 +00:00
socketserver.py gh-76007: Deprecate __version__ attribute (#138675) 2025-09-29 12:03:23 +03:00
ssl.py gh-88046: remove impossible conditional import for _ssl.RAND_egd (#139648) 2025-10-09 11:14:36 +02:00
stat.py gh-83714: Implement os.statx() function (#139178) 2025-10-15 13:44:08 +00:00
statistics.py gh-132893: Minor edits to the statistics module PR (gh-133106) 2025-04-28 23:05:37 -05:00
stringprep.py
struct.py
subprocess.py GH-133711: Enable UTF-8 mode by default (PEP 686) (#133712) 2025-07-15 10:45:41 +01:00
symtable.py gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00
tabnanny.py gh-76007: Deprecate __version__ attribute (#138675) 2025-09-29 12:03:23 +03:00
tarfile.py gh-57911: Sanitize symlink targets in tarfile on win32 (GH-138309) 2025-09-05 16:19:47 +02:00
tempfile.py gh-136156: Allow using linkat() with TemporaryFile (#136281) 2025-07-08 18:39:47 +02:00
textwrap.py gh-139065: Fix trailing space before long word in textwrap (GH-139070) 2025-10-10 16:29:18 +03:00
this.py
threading.py gh-114827: clarify threading.Event.wait timeout behavior (#114834) 2025-10-14 11:34:53 +03:00
timeit.py gh-139374: colorize traceback when using timeit command-line interface (#139375) 2025-09-28 11:49:18 +00:00
token.py gh-131507: Add support for syntax highlighting in PyREPL (GH-133247) 2025-05-02 20:22:31 +02:00
tokenize.py gh-63161: Fix tokenize.detect_encoding() (GH-139446) 2025-10-20 20:08:47 +03:00
trace.py gh-130645: Add color to stdlib argparse CLIs (gh-133380) 2025-05-05 19:46:46 +02:00
traceback.py gh-139707: Better ModuleNotFoundError message for missing stdlib modules (GH-140219) 2025-10-21 09:12:04 +02:00
tracemalloc.py
tty.py
turtle.py gh-138772: Fix and improve documentation for turtle color functions (GH-139325) 2025-10-13 18:32:16 +03:00
types.py gh-136492: Add FrameLocalsProxyType to types (GH-136546) 2025-07-20 20:49:00 +02:00
typing.py gh-133601: Remove deprecated typing.no_type_check_decorator (#133602) 2025-10-20 21:10:44 +00:00
uuid.py gh-76760: test that uuid.uuid1() sets the version field (#139033) 2025-09-17 13:31:51 +00:00
warnings.py gh-128384: Use a context variable for warnings.catch_warnings (gh-130010) 2025-04-09 16:18:54 -07:00
wave.py gh-75593: Add support of bytes and path-like paths in wave.open() (GH-140951) 2025-11-12 10:20:55 +02:00
weakref.py
webbrowser.py gh-130645: Add color to stdlib argparse CLIs (gh-133380) 2025-05-05 19:46:46 +02:00
zipapp.py gh-130645: Add color to stdlib argparse CLIs (gh-133380) 2025-05-05 19:46:46 +02:00
zipimport.py gh-135801: Add the module parameter to compile() etc (GH-139652) 2025-11-13 13:21:32 +02:00