Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
776a1e1
Assign threads indices into bytecode copies
mpage Aug 15, 2024
2b40870
Replace most usage of PyCode_CODE
mpage Aug 27, 2024
344d7ad
Get bytecode copying working
mpage Aug 20, 2024
f203d00
Refactor remove_tools
mpage Aug 30, 2024
82b456a
Refactor remove_line_tools
mpage Aug 30, 2024
b021704
Instrument thread-local bytecode
mpage Sep 1, 2024
aea69c5
Use locks for instrumentation
mpage Sep 3, 2024
552277d
Add ifdef guards for each specialization family
mpage Sep 3, 2024
50a6089
Specialize BINARY_OP
mpage Sep 4, 2024
3f1d941
Limit the amount of memory consumed by bytecode copies
mpage Sep 6, 2024
7d2eb27
Make thread-local bytecode limits user configurable
mpage Sep 7, 2024
d5476b9
Fix a few data races when (de)instrumenting opcodes
mpage Sep 8, 2024
e3b367a
Make branch taken recording thread-safe
mpage Sep 8, 2024
b2375bf
Lock thread-local bytecode when specializing
mpage Sep 9, 2024
2707f8e
Load bytecode on RESUME_CHECK
mpage Sep 9, 2024
3fdcb28
Load tlbc on generator.throw()
mpage Sep 9, 2024
4a55ce5
Use tlbc instead of thread_local_bytecode
mpage Sep 9, 2024
8b3ff60
Use tlbc everywhere
mpage Sep 9, 2024
862afa1
Explicitly manage tlbc state
mpage Sep 9, 2024
0b4d952
Refactor API for fetching tlbc
mpage Sep 9, 2024
7795e99
Add unit tests
mpage Sep 10, 2024
693a4cc
Fix initconfig in default build
mpage Sep 10, 2024
b43531e
Fix instrumentation in default build
mpage Sep 10, 2024
9025f43
Synchronize bytecode modifications between specialization and instrum…
mpage Sep 10, 2024
c44c7d9
Add a high-level comment
mpage Sep 10, 2024
e2a6656
Fix unused variable warning in default build
mpage Sep 10, 2024
e6513d1
Fix test_config in free-threaded builds
mpage Sep 10, 2024
a18396f
Fix formatting
mpage Sep 10, 2024
81fe1a2
Remove comment
mpage Sep 10, 2024
837645e
Fix data race in _PyInstruction_GetLength
mpage Sep 10, 2024
f13e132
Fix tier2 optimizer
mpage Sep 11, 2024
942f628
Use __VA_ARGS__ for macros
mpage Sep 11, 2024
66cb24d
Update vcxproj files to include newly added files
mpage Sep 11, 2024
ad12bd4
Mark unused params
mpage Sep 11, 2024
1bbbbbc
Keep tier2 and the JIT disabled in free-threaded builds
mpage Sep 12, 2024
e63e403
Only allow enabling/disabling tlbc
mpage Sep 13, 2024
8b97771
Update libpython for gdb
mpage Sep 13, 2024
d34adeb
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Sep 13, 2024
6d4fe73
Handle out of memory errors
mpage Sep 13, 2024
c2d8693
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Sep 17, 2024
b104782
Fix warnings on windows
mpage Sep 17, 2024
deb5216
Fix another warning
mpage Sep 18, 2024
2f11cc7
Ugh actually fix it
mpage Sep 18, 2024
04f1ac3
Add high-level comment about index pools
mpage Sep 25, 2024
aa330b1
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Sep 25, 2024
7dfd1ca
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Sep 26, 2024
7c9da24
Exclude tlbc from refleak counts
mpage Sep 27, 2024
dd144d0
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Sep 28, 2024
ad180d1
Regen files
mpage Sep 28, 2024
95d2264
Move `get_tlbc_blocks` into the sys module
mpage Sep 30, 2024
b6380de
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Sep 30, 2024
adb59ef
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 5, 2024
39c947d
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 10, 2024
2cc5830
Work around `this_instr` now being const
mpage Oct 11, 2024
96ec126
Make RESUME_CHECK cheaper
mpage Oct 11, 2024
5ecebd9
Pass tstate to _PyCode_GetTLBCFast
mpage Oct 11, 2024
815b2fe
Rename test_tlbc.py to test_thread_local_bytecode.py
mpage Oct 11, 2024
fb90d23
Remove per-family defines for specialization
mpage Oct 11, 2024
4e42414
Replace bytecode pointer with tlbc_index
mpage Oct 13, 2024
814e4ca
Add a test verifying that we clean up tlbc when the code object is de…
mpage Oct 14, 2024
ba3930a
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 14, 2024
cb8a774
Fix indentation
mpage Oct 14, 2024
0f8a55b
Clarify comment
mpage Oct 14, 2024
70ce0fe
Fix TSAN
mpage Oct 14, 2024
f512353
Add test for cleaning up tlbc in correct place, not old emacs buffer
mpage Oct 14, 2024
4be2b1f
Remove test_tlbc.py
mpage Oct 14, 2024
61c7aa9
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 17, 2024
ab6222c
Use int32_t instead of Py_ssize_t for tlbc indices
mpage Oct 17, 2024
6bbb220
Use _PyCode_CODE instead of PyFrame_GetBytecode in super_init_without…
mpage Oct 17, 2024
4580e3c
Update comment
mpage Oct 17, 2024
b992f44
Consolidate _PyCode_{Quicken,DisableSpecialization} into _PyCode_Init…
mpage Oct 17, 2024
4c040d3
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 18, 2024
5b7658c
Fix incorrect types
mpage Oct 18, 2024
bec5bce
Add command-line tests for enabling TLBC
mpage Oct 18, 2024
c9054b7
Update libpython.py for tlbc_index
mpage Oct 18, 2024
1a48ab2
Avoid special casing in _PyEval_GetExecutableCode
mpage Oct 19, 2024
b16ae5f
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 19, 2024
176b24e
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 23, 2024
c107495
Clear TLBC when other caches are cleared
mpage Oct 23, 2024
07f9140
Remove _get_tlbc_blocks
mpage Oct 24, 2024
4cbe237
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Oct 30, 2024
38ff315
Rename _PyCode_InitCounters back to _PyCode_Quicken
mpage Oct 30, 2024
338f7e5
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Nov 4, 2024
bcd1bb2
Merge branch 'main' into gh-115999-thread-local-bytecode
mpage Nov 4, 2024
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
Prev Previous commit
Next Next commit
Get bytecode copying working
  • Loading branch information
mpage committed Sep 10, 2024
commit 344d7adb8a8f9eafad15c8864d39c1cb825beae3
15 changes: 15 additions & 0 deletions Include/cpython/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ typedef struct {
uint8_t *per_instruction_tools;
} _PyCoMonitoringData;

#ifdef Py_GIL_DISABLED
/* Each thread specializes a thread-local copy of the bytecode in free-threaded
* builds. These copies are stored on the code object in a `_PyCodeArray`.
*/
typedef struct {
Py_ssize_t size;
char *entries[];
} _PyCodeArray;

#define _PyCode_DEF_THREAD_LOCAL_BYTECODE() _PyCodeArray *co_specialized_code;
#else
#define _PyCode_DEF_THREAD_LOCAL_BYTECODE()
#endif

// To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
// defined in this macro:
#define _PyCode_DEF(SIZE) { \
Expand Down Expand Up @@ -133,6 +147,7 @@ typedef struct {
Type is a void* to keep the format private in codeobject.c to force \
people to go through the proper APIs. */ \
void *co_extra; \
_PyCode_DEF_THREAD_LOCAL_BYTECODE() \
Comment thread
Yhg1s marked this conversation as resolved.
char co_code_adaptive[(SIZE)]; \
}

Expand Down
20 changes: 20 additions & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,26 @@ struct _PyCode8 _PyCode_DEF(8);
PyAPI_DATA(const struct _PyCode8) _Py_InitCleanup;

#ifdef Py_GIL_DISABLED

extern _Py_CODEUNIT *_PyCode_CreateSpecializableCode(PyCodeObject *co);
/* Return bytecode that should be executed.
* Will not return NULL, but may disable specialization, in which case the
* returned bytecode should not be specialized.
*
* XXX - This is a confusing contract.
*/
static inline _Py_CODEUNIT *
_PyCode_GetSpecializableCode(PyCodeObject *co)
{
_PyCodeArray *code = _Py_atomic_load_ptr_acquire(&co->co_specialized_code);
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *) PyThreadState_GET();
Comment thread
mpage marked this conversation as resolved.
Outdated
Py_ssize_t idx = tstate->specialized_code_index;
if (idx < code->size && code->entries[idx] != NULL) {
// XXX - Do we need to worry about alignment here?
return (_Py_CODEUNIT *) code->entries[idx];
}
return _PyCode_CreateSpecializableCode(co);
}
extern int _Py_ReserveSpecializedCodeIndex(PyInterpreterState *interp);
extern void _Py_ClearSpecializedCodeIndex(_PyThreadStateImpl *tstate);
#endif
Expand Down
9 changes: 9 additions & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ typedef struct _PyInterpreterFrame {
PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
_Py_CODEUNIT *instr_ptr; /* Instruction currently executing (or about to begin) */
#ifdef Py_GIL_DISABLED
Comment thread
Yhg1s marked this conversation as resolved.
_Py_CODEUNIT *bytecode;
#endif
_PyStackRef *stackpointer;
uint16_t return_offset; /* Only relevant during a function call */
char owner;
Expand All @@ -84,7 +87,11 @@ static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) {
}

static inline _Py_CODEUNIT *_PyFrame_GetBytecode(_PyInterpreterFrame *f) {
#ifdef Py_GIL_DISABLED
return f->bytecode;
#else
return _PyCode_CODE(_PyFrame_GetCode(f));
#endif
}

static inline _PyStackRef *_PyFrame_Stackbase(_PyInterpreterFrame *f) {
Expand Down Expand Up @@ -167,6 +174,7 @@ _PyFrame_Initialize(
}

#ifdef Py_GIL_DISABLED
frame->bytecode = frame->instr_ptr;
// On GIL disabled, we walk the entire stack in GC. Since stacktop
// is not always in sync with the real stack pointer, we have
// no choice but to traverse the entire stack.
Expand Down Expand Up @@ -339,6 +347,7 @@ _PyFrame_PushTrampolineUnchecked(PyThreadState *tstate, PyCodeObject *code, int
frame->return_offset = 0;

#ifdef Py_GIL_DISABLED
frame->bytecode = frame->instr_ptr;
assert(code->co_nlocalsplus == 0);
for (int i = 0; i < code->co_stacksize; i++) {
frame->localsplus[i] = PyStackRef_NULL;
Expand Down
103 changes: 52 additions & 51 deletions Include/internal/pycore_uop_ids.h

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

81 changes: 79 additions & 2 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
#include "pycore_object.h" // _PyObject_SetDeferredRefcount
#include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches
#include "pycore_opcode_utils.h" // RESUME_AT_FUNC_START
#include "pycore_pymem.h" // _PyMem_FreeDelayed
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "clinic/codeobject.c.h"

#define INITIAL_SPECIALIZED_CODE_SIZE 16
Comment thread
mpage marked this conversation as resolved.

static const char *
code_event_name(PyCodeEvent event) {
switch (event) {
Expand Down Expand Up @@ -450,7 +453,7 @@ _PyCode_Validate(struct _PyCodeConstructor *con)

extern void _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size);

static void
static int
init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
{
int nlocalsplus = (int)PyTuple_GET_SIZE(con->localsplusnames);
Expand Down Expand Up @@ -513,6 +516,12 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)

memcpy(_PyCode_CODE(co), PyBytes_AS_STRING(con->code),
PyBytes_GET_SIZE(con->code));
#ifdef Py_GIL_DISABLED
// XXX - initialize code array
co->co_specialized_code = PyMem_Calloc(1, sizeof(_PyCodeArray) + sizeof(void*) * INITIAL_SPECIALIZED_CODE_SIZE);
co->co_specialized_code->size = INITIAL_SPECIALIZED_CODE_SIZE;
co->co_specialized_code->entries[0] = (char *) _PyCode_CODE(co);
#endif
int entry_point = 0;
while (entry_point < Py_SIZE(co) &&
_PyCode_CODE(co)[entry_point].op.code != RESUME) {
Expand All @@ -521,6 +530,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
co->_co_firsttraceable = entry_point;
_PyCode_Quicken(_PyCode_CODE(co), Py_SIZE(co));
notify_code_watchers(PY_CODE_EVENT_CREATE, co);
return 0;
}

static int
Expand Down Expand Up @@ -675,7 +685,12 @@ _PyCode_New(struct _PyCodeConstructor *con)
PyErr_NoMemory();
return NULL;
}
init_code(co, con);

if (init_code(co, con) < 0) {
Py_DECREF(co);
return NULL;
}

#ifdef Py_GIL_DISABLED
_PyObject_SetDeferredRefcount((PyObject *)co);
_PyObject_GC_TRACK(co);
Expand Down Expand Up @@ -1871,6 +1886,15 @@ code_dealloc(PyCodeObject *co)
PyObject_ClearWeakRefs((PyObject*)co);
}
free_monitoring_data(co->_co_monitoring);
#ifdef Py_GIL_DISABLED
// The first element always points to the bytecode that follows the fixed
// part of the code object, which will be freed when the code object is
// freed.
for (Py_ssize_t i = 1; i < co->co_specialized_code->size; i++) {
PyMem_Free(co->co_specialized_code->entries[i]);
}
PyMem_Free(co->co_specialized_code);
#endif
PyObject_Free(co);
}

Expand Down Expand Up @@ -2637,3 +2661,56 @@ _PyCode_Fini(PyInterpreterState *interp)
_PyIndexPool_Fini(&interp->specialized_code_indices);
#endif
}

#ifdef Py_GIL_DISABLED

static void
copy_code(_Py_CODEUNIT *dst, _Py_CODEUNIT *src, Py_ssize_t nbytes)
{
int code_len = Py_SIZE(co);
_Py_CODEUNIT *dst_bytecode = (_Py_CODEUNIT *) dst->bytecode;
for (int i = 0; i < code_len; i += _PyInstruction_GetLength(co, i)) {
dst_bytecode[i] = _Py_GetBaseCodeUnit(co, i);
}
_PyCode_Quicken(dst_bytecode, code_len);
}

static _Py_CODEUNIT *
create_specializable_code_lock_held(PyCodeObject *co)
{
_PyCodeArray *spec_code = co->co_specialized_code;
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *) PyThreadState_GET();
Py_ssize_t idx = tstate->specialized_code_index;
if (idx >= spec_code->size) {
Py_ssize_t new_size = spec_code->size * 2;
_PyCodeArray *new_spec_code = PyMem_Calloc(sizeof(_PyCodeArray) + sizeof(char*) * new_size, 1);
if (new_spec_code == NULL) {
PyErr_NoMemory();
return NULL;
}
new_spec_code->size = new_size;
memcpy(new_spec_code->entries, spec_code->entries, spec_code->size * sizeof(char*));
_Py_atomic_store_ptr_release(&co->co_specialized_code, new_spec_code);
_PyMem_FreeDelayed(spec_code);
spec_code = new_spec_code;
}
spec_code->entries[idx] = PyMem_Malloc(_PyCode_NBYTES(co));
if (spec_code->entries[idx] == NULL) {
PyErr_NoMemory();
return NULL;
}
copy_code((_Py_CODEUNIT *) spec_code->entries[idx], _PyCode_CODE(co), _PyCode_NBYTES(co));
return (_Py_CODEUNIT *) spec_code->entries[idx];
}

_Py_CODEUNIT *
_PyCode_CreateSpecializableCode(PyCodeObject *co)
{
_Py_CODEUNIT *result;
Py_BEGIN_CRITICAL_SECTION(co);
result = create_specializable_code_lock_held(co);
Py_END_CRITICAL_SECTION();
return result;
}

#endif
Loading