diff options
author | mpage <mpage@cs.stanford.edu> | 2024-11-04 19:13:32 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-04 19:13:32 (GMT) |
commit | 2e95c5ba3bf7e5004c7e2304afda4a8f8e2443a7 (patch) | |
tree | de32ac52ed5ffcb9460dfc062effc6b4b662ee5d /Tools/gdb | |
parent | e5a4b402ae55f5eeeb44d3e7bc3f3ec39b249846 (diff) | |
download | cpython-2e95c5ba3bf7e5004c7e2304afda4a8f8e2443a7.zip cpython-2e95c5ba3bf7e5004c7e2304afda4a8f8e2443a7.tar.gz cpython-2e95c5ba3bf7e5004c7e2304afda4a8f8e2443a7.tar.bz2 |
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.
Diffstat (limited to 'Tools/gdb')
-rwxr-xr-x | Tools/gdb/libpython.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/Tools/gdb/libpython.py b/Tools/gdb/libpython.py index 946af4b..ed25415 100755 --- a/Tools/gdb/libpython.py +++ b/Tools/gdb/libpython.py @@ -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): |