mirror of
https://github.com/python/cpython.git
synced 2025-08-25 03:04:55 +00:00
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.
This commit is contained in:
parent
e5a4b402ae
commit
2e95c5ba3b
44 changed files with 1510 additions and 255 deletions
|
@ -77,6 +77,10 @@ def _managed_dict_offset():
|
|||
else:
|
||||
return -3 * _sizeof_void_p()
|
||||
|
||||
def _interp_frame_has_tlbc_index():
|
||||
interp_frame = gdb.lookup_type("_PyInterpreterFrame")
|
||||
return any(field.name == "tlbc_index" for field in interp_frame.fields())
|
||||
|
||||
|
||||
Py_TPFLAGS_INLINE_VALUES = (1 << 2)
|
||||
Py_TPFLAGS_MANAGED_DICT = (1 << 4)
|
||||
|
@ -105,6 +109,8 @@ FRAME_INFO_OPTIMIZED_OUT = '(frame information optimized out)'
|
|||
UNABLE_READ_INFO_PYTHON_FRAME = 'Unable to read information on python frame'
|
||||
EVALFRAME = '_PyEval_EvalFrameDefault'
|
||||
|
||||
INTERP_FRAME_HAS_TLBC_INDEX = _interp_frame_has_tlbc_index()
|
||||
|
||||
class NullPyObjectPtr(RuntimeError):
|
||||
pass
|
||||
|
||||
|
@ -693,6 +699,16 @@ def parse_location_table(firstlineno, linetable):
|
|||
yield addr, end_addr, line
|
||||
addr = end_addr
|
||||
|
||||
|
||||
class PyCodeArrayPtr:
|
||||
def __init__(self, gdbval):
|
||||
self._gdbval = gdbval
|
||||
|
||||
def get_entry(self, index):
|
||||
assert (index >= 0) and (index < self._gdbval["size"])
|
||||
return self._gdbval["entries"][index]
|
||||
|
||||
|
||||
class PyCodeObjectPtr(PyObjectPtr):
|
||||
"""
|
||||
Class wrapping a gdb.Value that's a PyCodeObject* i.e. a <code> instance
|
||||
|
@ -1085,7 +1101,12 @@ class PyFramePtr:
|
|||
def _f_lasti(self):
|
||||
codeunit_p = gdb.lookup_type("_Py_CODEUNIT").pointer()
|
||||
instr_ptr = self._gdbval["instr_ptr"]
|
||||
first_instr = self._f_code().field("co_code_adaptive").cast(codeunit_p)
|
||||
if INTERP_FRAME_HAS_TLBC_INDEX:
|
||||
tlbc_index = self._gdbval["tlbc_index"]
|
||||
code_arr = PyCodeArrayPtr(self._f_code().field("co_tlbc"))
|
||||
first_instr = code_arr.get_entry(tlbc_index).cast(codeunit_p)
|
||||
else:
|
||||
first_instr = self._f_code().field("co_code_adaptive").cast(codeunit_p)
|
||||
return int(instr_ptr - first_instr)
|
||||
|
||||
def is_shim(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue