Skip to content

gh-115999: Enable free-threaded specialization of LOAD_CONST #129365

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

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,20 @@ def compare_op_str():
self.assert_specialized(compare_op_str, "COMPARE_OP_STR")
self.assert_no_opcode(compare_op_str, "COMPARE_OP")

@cpython_only
@requires_specialization_ft
def test_load_const(self):
def load_const():
def unused(): pass
# Currently, the empty tuple is immortal, and the otherwise
# unused nested function's code object is mortal. This test will
# have to use different values if either of that changes.
return ()

load_const()
self.assert_specialized(load_const, "LOAD_CONST_IMMORTAL")
self.assert_specialized(load_const, "LOAD_CONST_MORTAL")
self.assert_no_opcode(load_const, "LOAD_CONST")

if __name__ == "__main__":
unittest.main()
14 changes: 12 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,20 @@ dummy_func(
* marshalling can intern strings and make them immortal. */
PyObject *obj = GETITEM(FRAME_CO_CONSTS, oparg);
value = PyStackRef_FromPyObjectNew(obj);
#if ENABLE_SPECIALIZATION
#if ENABLE_SPECIALIZATION_FT
#ifdef Py_GIL_DISABLED
uint8_t expected = LOAD_CONST;
if (!_Py_atomic_compare_exchange_uint8(
&this_instr->op.code, &expected,
_Py_IsImmortal(obj) ? LOAD_CONST_IMMORTAL : LOAD_CONST_MORTAL)) {
// We might lose a race with instrumentation, which we don't care about.
assert(expected >= MIN_INSTRUMENTED_OPCODE);
}
#else
if (this_instr->op.code == LOAD_CONST) {
this_instr->op.code = _Py_IsImmortal(obj) ? LOAD_CONST_IMMORTAL : LOAD_CONST_MORTAL;
}
#endif
#endif
}

Expand Down Expand Up @@ -2555,7 +2565,7 @@ dummy_func(
}
OPCODE_DEFERRED_INC(COMPARE_OP);
ADVANCE_ADAPTIVE_COUNTER(this_instr[1].counter);
#endif /* ENABLE_SPECIALIZATION */
#endif /* ENABLE_SPECIALIZATION_FT */
}

op(_COMPARE_OP, (left, right -- res)) {
Expand Down
14 changes: 12 additions & 2 deletions Python/generated_cases.c.h

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

1 change: 1 addition & 0 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ def has_error_without_pop(op: parser.InstDef) -> bool:
"_Py_STR",
"_Py_TryIncrefCompare",
"_Py_TryIncrefCompareStackRef",
"_Py_atomic_compare_exchange_uint8",
"_Py_atomic_load_ptr_acquire",
"_Py_atomic_load_uintptr_relaxed",
"_Py_set_eval_breaker_bit",
Expand Down
Loading