Commit graph

197 commits

Author SHA1 Message Date
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
Hai Zhu
cc48bf0fde
gh-134584: Eliminate redundant refcounting from _BINARY_OP_SUBSCR_TUPLE_INT (GH-143094) 2025-12-23 21:47:12 +00:00
Tomas R.
25c294b6ea
gh-134584: Eliminate redundant refcounting from _CALL_TYPE_1 (GH-135818) 2025-12-23 17:01:10 +00:00
Hai Zhu
5b5ee3c4bf
gh-134584: Eliminate redundant refcounting from _LOAD_ATTR_WITH_HINT (GH-143062)
Eliminate redundant refcounting from _LOAD_ATTR_WITH_HINT
2025-12-23 00:28:08 +00:00
Ken Jin
6b4bc6e6a2
gh-134584: JIT: Borrow references for immortal promoted globals (GH-142921)
JIT: Borrow references for immortal promoted globals
2025-12-19 19:06:34 +00:00
Ken Jin
786f464c74
gh-142961: Fix constant folding len(tuple) in JIT (GH-142963) 2025-12-19 17:43:36 +00:00
Savannah Ostrowski
1391ee664c
GH-134584: Remove redundant refcount for BINARY_OP_SUBSCR_STR_INT (#142844) 2025-12-18 21:29:54 +00:00
LloydZ
33d94abafd
gh-134584: Eliminate redundant refcounting from _BINARY_OP_SUBSCR_LIST_INT (GH-142926) 2025-12-18 18:25:36 +00:00
Donghee Na
71a7cb8887
gh-134584: Remove redundant refcount from _BINARY_OP_ADD_UNICODE (gh-142825) 2025-12-18 21:33:18 +09:00
Nadeshiko Manju
4345253981
gh-134584: Eliminate redundant refcounting from _STORE_ATTR_WITH_HINT (GH-142767)
Signed-off-by: Manjusaka <me@manjusaka.me>
2025-12-16 22:21:04 +00:00
Nadeshiko Manju
6ee51a36b3
gh-134584: Eliminate redundant refcounting from _LOAD_ATTR_INSTANCE_VALUE (GH-142769)
Signed-off-by: Manjusaka <me@manjusaka.me>
2025-12-16 20:39:20 +00:00
Savannah Ostrowski
bef63d2fb8
GH-134584: Remove redundant refcount from _STORE_ATTR_SLOT (#142729) 2025-12-15 15:18:44 -08:00
Ken Jin
790a46a449
gh-134584: JIT: Eliminate redundant refcount ops for X_INT (GH-142765) 2025-12-15 22:22:18 +00:00
Nadeshiko Manju
059316ade6
gh-134584: Eliminate redundant refcounting from _STORE_ATTR_INSTANCE_VALUE (GH-142759)
Signed-off-by: Manjusaka <me@manjusaka.me>
2025-12-15 20:17:12 +00:00
Ken Jin
0ac4e6c6cd
gh-134584: Remove custom float decref ops (GH-142576) 2025-12-15 19:38:58 +00:00
Donghee Na
abaaeee6a0
gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_DICT (GH-142712)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2025-12-15 16:42:32 +00:00
Nadeshiko Manju
872ab51f32
gh-134584: Eliminate redundant refcounting from _CALL_LIST_APPEND (GH-142711)
Signed-off-by: Manjusaka <me@manjusaka.me>
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2025-12-15 15:49:36 +00:00
Donghee Na
04da416e6b
gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_LIST_INT (gh-142703) 2025-12-14 12:26:00 +00:00
PuQing
a2a400af1e
gh-134584: Eliminate redundant refcounting from _CALL_BUILTION_O (GH-142695)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2025-12-14 10:28:02 +00:00
Nadeshiko Manju
a154c9ed4e
gh-134584: Eliminate redundant refcounting from _CALL_STR_1 (GH-136070)
Signed-off-by: Manjusaka <me@manjusaka.me>
2025-12-14 09:33:05 +00:00
Ken Jin
e02a35c365
gh-134584: Cleanups for GH-135860 (GH-142604) 2025-12-13 14:38:10 +00:00
Ken Jin
a3a611b042
gh-134584: Revert partially GH-135860 (GH-142620) 2025-12-12 14:04:11 +00:00
Donghee Na
a27538540e
gh-134584: Eliminate redundant refcounting from `_CALL_LEN` (gh-136104) 2025-12-11 15:24:34 +00:00
Noam Cohen
a78f43b001
gh-134584: Eliminate redundant refcounting from _CALL_TUPLE_1 (GH-135860) 2025-12-11 14:31:28 +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
97f0a1f203
gh-142276: Watch attribute loads when promoting JIT constants (GH-142303)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Savannah Ostrowski <savannah@python.org>
2025-12-08 18:03:15 +00:00
Ken Jin
b3bf212898
gh-141976: Check stack bounds in JIT optimizer (GH-142201) 2025-12-04 20:28:08 +00:00
Mark Shannon
62423c9c36
GH-141794: Limit size of generated machine code. (GH-142228)
* 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.
2025-12-03 17:43:35 +00:00
Mark Shannon
b420f6be53
GH-139109: Support switch/case dispatch with the tracing interpreter. (GH-141703) 2025-11-18 13:31:48 +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
Mark Shannon
3b83257366
GH-138378: Move globals-to-consts pass into main optimizer pass (GH-138379) 2025-09-18 10:09:59 +01:00
Savannah Bailey
9c9a0f7da7
GH-132732: Use pure op machinery to optimize various instructions with _POP_TOP and _POP_TWO (#137577) 2025-09-15 16:29:45 +01:00
Ken Jin
2402f84665
gh-138431: JIT Optimizer --- Fix round-tripping references for str and tuple (GH-138458)
Co-authored-by: Mark Shannon <9448417+markshannon@users.noreply.github.com>
2025-09-04 02:05:06 +08:00
AN Long
1ff2cbbac8
gh-137136: Suppress build warnings when build on Windows with --experimental-jit-interpreter (GH-137137) 2025-09-03 15:42:26 +01: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
Savannah Bailey
f7c380ef67
GH-132732: Use pure op machinery to optimize COMPARE_OP_INT/FLOAT/STR (#137062)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2025-07-25 19:02:04 -07:00
Ken Jin
b3308973e3
gh-136183: Deal with escapes in JIT optimizer's constant evaluator (GH-136184) 2025-07-02 14:08:25 +08:00
Ken Jin
e0d6500b2d
gh-136125: Use _PyObject_GetMethodStackRef for LOAD_ATTR (GH-136127) 2025-07-02 01:20:46 +08:00
Ken Jin
695ab61351
gh-132732: Automatically constant evaluate pure operations (GH-132733)
This adds a "macro" to the optimizer DSL called "REPLACE_OPCODE_IF_EVALUATES_PURE", which allows automatically constant evaluating a bytecode body if certain inputs have no side effects upon evaluations (such as ints, strings, and floats).


Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
2025-06-27 19:37:44 +08:00
Noam Cohen
bda121862e
gh-131798: Optimize _UNARY_NEGATIVE (GH-135223) 2025-06-24 03:42:09 +08:00
Ken Jin
569fc6870f
gh-134584: Specialize POP_TOP by reference and type in JIT (GH-135761) 2025-06-24 00:57:14 +08:00
Tomas R.
61532b4bc7
gh-131798: JIT: Optimize _CALL_LEN when the length is known (#135260)
* Add news entry

* Optimize _CALL_LEN

* Simplify tests
2025-06-20 18:21:39 +02:00
Ken Jin
b53b0c14da
gh-135608: Add a null check for attribute promotion to fix a JIT crash (GH-135613)
Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
2025-06-20 14:33:35 +08:00
Mark Shannon
9731dd2c8d
GH-135379: Specialize int operations for compact ints only (GH-135668) 2025-06-19 11:10:29 +01:00
Ken Jin
fba5dded6d
gh-134584: Decref elimination for float ops in the JIT (GH-134588)
This PR adds a PyJitRef API to the JIT's optimizer that mimics the _PyStackRef API. This allows it to track references and their stack lifetimes properly. Thus opening up the doorway to refcount elimination in the JIT.
2025-06-17 23:25:53 +08:00
Nadeshiko Manju
667a86e076
gh-131798: JIT: replace _CHECK_METHOD_VERSION with _CHECK_FUNCTION_VERSION_INLINE (GH-135022)
Signed-off-by: Manjusaka <me@manjusaka.me>
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
2025-06-16 13:25:50 +08:00
Ken Jin
7b15873ed0
gh-135474: Specialize arithmetic only on compact ints (GH-135479)
Specialize arithmetic only on compact ints. This also makes int operations non-escaping in the JIT and in tier 1.
2025-06-14 17:13:32 +08:00
Mark Shannon
c87b5b2cb6
GH-135379: Remove types from stack items in code generator. (GH-135384)
* Make casts explicit in the instruction definitions
2025-06-11 15:52:25 +01:00
Noam Cohen
b150b6aca7
gh-131798: Optimize _UNARY_INVERT (GH-135222) 2025-06-09 18:33:18 +08:00