Skip to content

GH-135379: Top of stack caching for the JIT. #135465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
579b758
Tier 2 TOS caching. Work in progress
markshannon Jun 11, 2025
489e510
Tier 2 TOS caching, working for interpreter.
markshannon Jun 13, 2025
f603929
Get JIT working
markshannon Jun 13, 2025
cf1d7ab
Fix tool to support 3.11
markshannon Jun 13, 2025
efd7a0a
Add news
markshannon Jun 13, 2025
bb4e6b9
int arithmetic doesn't escape
markshannon Jun 13, 2025
e976b9b
Repair stats
markshannon Jun 13, 2025
11de93e
Add missing type annotation
markshannon Jun 13, 2025
4698695
Pacify mypy
markshannon Jun 13, 2025
33837a7
Add type annotation
markshannon Jun 13, 2025
8bb12ef
Avoid overflow gathering stats
markshannon Jun 13, 2025
920e6de
Reduce spilling
markshannon Jun 13, 2025
45e1abd
Merge branch 'main' into tier-2-tos-caching
markshannon Jun 16, 2025
3d72871
Merge branch 'main' into tier-2-tos-caching
markshannon Jun 17, 2025
0240115
Merge branch 'main' into tier-2-tos-caching
markshannon Jun 19, 2025
2850d72
Improve heuristics for stack caching
markshannon Jun 19, 2025
1c291f1
Merge branch 'main' into tier-2-tos-caching
markshannon Jun 20, 2025
ba2331a
Add news
markshannon Jun 20, 2025
cbee8d2
Fix uop execution stats
markshannon Jun 23, 2025
40988a0
Address review comments
markshannon Jun 23, 2025
76030e9
Address code review
markshannon Jun 26, 2025
991bbea
Merge branch 'main' into tier-2-tos-caching
markshannon Jun 26, 2025
51d4342
Remove blank line
markshannon Jun 26, 2025
9438c19
Zero out unused registers to reduce register pressure
markshannon Jul 21, 2025
53a50eb
Merge branch 'main' into tier-2-tos-caching
markshannon Jul 21, 2025
a4de1bf
Avoid spilling around DEOPT_IF
markshannon Jul 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/cpython/pystats.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# error "this header file must not be included directly"
#endif

#define PYSTATS_MAX_UOP_ID 1024
#define PYSTATS_MAX_UOP_ID 1500

#define SPECIALIZATION_FAILURE_KINDS 60

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C" {

#ifdef _Py_JIT

typedef _Py_CODEUNIT *(*jit_func)(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer, PyThreadState *tstate);
typedef _Py_CODEUNIT *(*jit_func)(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer, PyThreadState *tstate, _PyStackRef _tos_cache0, _PyStackRef _tos_cache1, _PyStackRef _tos_cache2);

int _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction *trace, size_t length);
void _PyJIT_Free(_PyExecutorObject *executor);
Expand Down
44 changes: 22 additions & 22 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
#define JIT_CLEANUP_THRESHOLD 100000

// This is the length of the trace we project initially.
#define UOP_MAX_TRACE_LENGTH 800
#define UOP_MAX_TRACE_LENGTH 1600

#define TRACE_STACK_SIZE 5

Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_stackref.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ static const _PyStackRef PyStackRef_ERROR = { .index = 2 };
#define PyStackRef_False ((_PyStackRef){ .index = 6 })
#define PyStackRef_True ((_PyStackRef){ .index = 8 })

#define PyStackRef_ZERO_BITS PyStackRef_NULL


#define INITIAL_STACKREF_INDEX 10

static inline int
Expand Down Expand Up @@ -264,6 +267,10 @@ PyStackRef_IsNullOrInt(_PyStackRef ref);

static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID };

/* For use in the JIT to clear an unused value.
* PyStackRef_ZERO_BITS has no meaning and should not be used other than by the JIT. */
static const _PyStackRef PyStackRef_ZERO_BITS = { .bits = 0 };

/* Wrap a pointer in a stack ref.
* The resulting stack reference is not safe and should only be used
* in the interpreter to pass values from one uop to another.
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C" {
#define GC_STAT_ADD(gen, name, n) do { if (_Py_stats) _Py_stats->gc_stats[(gen)].name += (n); } while (0)
#define OPT_STAT_INC(name) do { if (_Py_stats) _Py_stats->optimization_stats.name++; } while (0)
#define OPT_STAT_ADD(name, n) do { if (_Py_stats) _Py_stats->optimization_stats.name += (n); } while (0)
#define UOP_STAT_INC(opname, name) do { if (_Py_stats) { assert(opname < 512); _Py_stats->optimization_stats.opcode[opname].name++; } } while (0)
#define UOP_STAT_INC(opname, name) do { if (_Py_stats) { _Py_stats->optimization_stats.opcode[opname].name++; } } while (0)
#define UOP_PAIR_INC(uopcode, lastuop) \
do { \
if (lastuop && _Py_stats) { \
Expand Down
Loading
Loading