-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gh-115999: Implement thread-local bytecode and enable specialization …
…for `BINARY_OP` (#123926) Each thread specializes a thread-local copy of the bytecode, created on the first RESUME, in free-threaded builds. All copies of the bytecode for a code object are stored in the co_tlbc array on the code object. Threads reserve a globally unique index identifying its copy of the bytecode in all co_tlbc arrays at thread creation and release the index at thread destruction. The first entry in every co_tlbc array always points to the "main" copy of the bytecode that is stored at the end of the code object. This ensures that no bytecode is copied for programs that do not use threads. Thread-local bytecode can be disabled at runtime by providing either -X tlbc=0 or PYTHON_TLBC=0. Disabling thread-local bytecode also disables specialization. Concurrent modifications to the bytecode made by the specializing interpreter and instrumentation use atomics, with specialization taking care not to overwrite an instruction that was instrumented concurrently.
- Loading branch information
Showing
44 changed files
with
1,509 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef Py_INTERNAL_INDEX_POOL_H | ||
#define Py_INTERNAL_INDEX_POOL_H | ||
|
||
#include "Python.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_BUILD_CORE | ||
# error "this header requires Py_BUILD_CORE define" | ||
#endif | ||
|
||
#ifdef Py_GIL_DISABLED | ||
|
||
// This contains code for allocating unique indices in an array. It is used by | ||
// the free-threaded build to assign each thread a globally unique index into | ||
// each code object's thread-local bytecode array. | ||
|
||
// A min-heap of indices | ||
typedef struct _PyIndexHeap { | ||
int32_t *values; | ||
|
||
// Number of items stored in values | ||
Py_ssize_t size; | ||
|
||
// Maximum number of items that can be stored in values | ||
Py_ssize_t capacity; | ||
} _PyIndexHeap; | ||
|
||
// An unbounded pool of indices. Indices are allocated starting from 0. They | ||
// may be released back to the pool once they are no longer in use. | ||
typedef struct _PyIndexPool { | ||
PyMutex mutex; | ||
|
||
// Min heap of indices available for allocation | ||
_PyIndexHeap free_indices; | ||
|
||
// Next index to allocate if no free indices are available | ||
int32_t next_index; | ||
} _PyIndexPool; | ||
|
||
// Allocate the smallest available index. Returns -1 on error. | ||
extern int32_t _PyIndexPool_AllocIndex(_PyIndexPool *indices); | ||
|
||
// Release `index` back to the pool | ||
extern void _PyIndexPool_FreeIndex(_PyIndexPool *indices, int32_t index); | ||
|
||
extern void _PyIndexPool_Fini(_PyIndexPool *indices); | ||
|
||
#endif // Py_GIL_DISABLED | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif // !Py_INTERNAL_INDEX_POOL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.