gh-143995: Eliminate redundant refcounting in the JIT from LOAD_ATTR_MODULE (GH-143996)

This commit is contained in:
AN Long 2026-01-26 03:24:44 +09:00 committed by GitHub
parent a51bf70f95
commit 6e55337f8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1163 additions and 1023 deletions

View file

@ -2452,7 +2452,7 @@ dummy_func(
unused/5 +
_PUSH_NULL_CONDITIONAL;
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr)) {
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr, o)) {
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
DEOPT_IF(Py_TYPE(owner_o)->tp_getattro != PyModule_Type.tp_getattro);
PyDictObject *dict = (PyDictObject *)((PyModuleObject *)owner_o)->md_dict;
@ -2473,12 +2473,14 @@ dummy_func(
attr = PyStackRef_FromPyObjectNew(attr_o);
#endif
STAT_INC(LOAD_ATTR, hit);
PyStackRef_CLOSE(owner);
o = owner;
DEAD(owner);
}
macro(LOAD_ATTR_MODULE) =
unused/1 +
_LOAD_ATTR_MODULE +
POP_TOP +
unused/5 +
_PUSH_NULL_CONDITIONAL;
@ -5444,6 +5446,12 @@ dummy_func(
value = PyStackRef_FromPyObjectBorrow(ptr);
}
tier2 op(_INSERT_1_LOAD_CONST_INLINE, (ptr/4, left -- res, l)) {
res = PyStackRef_FromPyObjectNew(ptr);
l = left;
INPUTS_DEAD();
}
tier2 op(_INSERT_1_LOAD_CONST_INLINE_BORROW, (ptr/4, left -- res, l)) {
res = PyStackRef_FromPyObjectBorrow(ptr);
l = left;

View file

@ -9169,11 +9169,12 @@
break;
}
case _LOAD_ATTR_MODULE_r11: {
case _LOAD_ATTR_MODULE_r12: {
CHECK_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef owner;
_PyStackRef attr;
_PyStackRef o;
_PyStackRef _stack_item_0 = _tos_cache0;
owner = _stack_item_0;
uint32_t dict_version = (uint32_t)CURRENT_OPERAND0_32();
@ -9218,18 +9219,11 @@
attr = PyStackRef_FromPyObjectNew(attr_o);
#endif
STAT_INC(LOAD_ATTR, hit);
stack_pointer[0] = attr;
stack_pointer += 1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(owner);
stack_pointer = _PyFrame_GetStackPointer(frame);
o = owner;
_tos_cache1 = o;
_tos_cache0 = attr;
_tos_cache1 = PyStackRef_ZERO_BITS;
_tos_cache2 = PyStackRef_ZERO_BITS;
SET_CURRENT_CACHED_VALUES(1);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
SET_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
@ -19078,6 +19072,63 @@
break;
}
case _INSERT_1_LOAD_CONST_INLINE_r02: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef left;
_PyStackRef res;
_PyStackRef l;
left = stack_pointer[-1];
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectNew(ptr);
l = left;
_tos_cache1 = l;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _INSERT_1_LOAD_CONST_INLINE_r12: {
CHECK_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef left;
_PyStackRef res;
_PyStackRef l;
_PyStackRef _stack_item_0 = _tos_cache0;
left = _stack_item_0;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectNew(ptr);
l = left;
_tos_cache1 = l;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _INSERT_1_LOAD_CONST_INLINE_r23: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef left;
_PyStackRef res;
_PyStackRef l;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
left = _stack_item_1;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectNew(ptr);
l = left;
_tos_cache2 = l;
_tos_cache1 = res;
_tos_cache0 = _stack_item_0;
SET_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _INSERT_1_LOAD_CONST_INLINE_BORROW_r02: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());

View file

@ -8397,6 +8397,8 @@
static_assert(INLINE_CACHE_ENTRIES_LOAD_ATTR == 9, "incorrect cache size");
_PyStackRef owner;
_PyStackRef attr;
_PyStackRef o;
_PyStackRef value;
_PyStackRef *null;
/* Skip 1 cache entry */
// _LOAD_ATTR_MODULE
@ -8440,9 +8442,14 @@
attr = PyStackRef_FromPyObjectNew(attr_o);
#endif
STAT_INC(LOAD_ATTR, hit);
o = owner;
}
// _POP_TOP
{
value = o;
stack_pointer[-1] = attr;
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(owner);
PyStackRef_XCLOSE(value);
stack_pointer = _PyFrame_GetStackPointer(frame);
}
/* Skip 5 cache entries */

View file

@ -128,7 +128,7 @@ type_watcher_callback(PyTypeObject* type)
}
static PyObject *
convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj, bool pop)
convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj, bool pop, bool insert)
{
assert(inst->opcode == _LOAD_GLOBAL_MODULE || inst->opcode == _LOAD_GLOBAL_BUILTINS || inst->opcode == _LOAD_ATTR_MODULE);
assert(PyDict_CheckExact(obj));
@ -148,15 +148,22 @@ convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj, bool pop)
if (res == NULL) {
return NULL;
}
if (_Py_IsImmortal(res)) {
inst->opcode = pop ? _POP_TOP_LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE_BORROW;
}
else {
inst->opcode = pop ? _POP_TOP_LOAD_CONST_INLINE : _LOAD_CONST_INLINE;
}
if (inst->oparg & 1) {
assert(inst[1].opcode == _PUSH_NULL_CONDITIONAL);
assert(inst[1].oparg & 1);
if (insert) {
if (_Py_IsImmortal(res)) {
inst->opcode = _INSERT_1_LOAD_CONST_INLINE_BORROW;
} else {
inst->opcode = _INSERT_1_LOAD_CONST_INLINE;
}
} else {
if (_Py_IsImmortal(res)) {
inst->opcode = pop ? _POP_TOP_LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE_BORROW;
} else {
inst->opcode = pop ? _POP_TOP_LOAD_CONST_INLINE : _LOAD_CONST_INLINE;
}
if (inst->oparg & 1) {
assert(inst[1].opcode == _PUSH_NULL_CONDITIONAL);
assert(inst[1].oparg & 1);
}
}
inst->operand0 = (uint64_t)res;
return res;

View file

@ -694,7 +694,7 @@ dummy_func(void) {
o = owner;
}
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr)) {
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr, o)) {
(void)dict_version;
(void)index;
attr = PyJitRef_NULL;
@ -706,7 +706,7 @@ dummy_func(void) {
if (watched_mutations < _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
PyDict_Watch(GLOBALS_WATCHER_ID, dict);
_Py_BloomFilter_Add(dependencies, dict);
PyObject *res = convert_global_to_const(this_instr, dict, true);
PyObject *res = convert_global_to_const(this_instr, dict, false, true);
if (res == NULL) {
attr = sym_new_not_null(ctx);
}
@ -721,6 +721,7 @@ dummy_func(void) {
/* No conversion made. We don't know what `attr` is. */
attr = sym_new_not_null(ctx);
}
o = owner;
}
op (_PUSH_NULL_CONDITIONAL, ( -- null[oparg & 1])) {
@ -1585,7 +1586,7 @@ dummy_func(void) {
ctx->builtins_watched = true;
}
if (ctx->frame->globals_checked_version != 0 && ctx->frame->globals_watched) {
cnst = convert_global_to_const(this_instr, builtins, false);
cnst = convert_global_to_const(this_instr, builtins, false, false);
}
}
if (cnst == NULL) {
@ -1624,7 +1625,7 @@ dummy_func(void) {
ctx->frame->globals_checked_version = version;
}
if (ctx->frame->globals_checked_version == version) {
cnst = convert_global_to_const(this_instr, globals, false);
cnst = convert_global_to_const(this_instr, globals, false, false);
}
}
}

View file

@ -1572,7 +1572,7 @@
ctx->frame->globals_checked_version = version;
}
if (ctx->frame->globals_checked_version == version) {
cnst = convert_global_to_const(this_instr, globals, false);
cnst = convert_global_to_const(this_instr, globals, false, false);
}
}
}
@ -1615,7 +1615,7 @@
ctx->builtins_watched = true;
}
if (ctx->frame->globals_checked_version != 0 && ctx->frame->globals_watched) {
cnst = convert_global_to_const(this_instr, builtins, false);
cnst = convert_global_to_const(this_instr, builtins, false, false);
}
}
if (cnst == NULL) {
@ -1875,6 +1875,7 @@
case _LOAD_ATTR_MODULE: {
JitOptRef owner;
JitOptRef attr;
JitOptRef o;
owner = stack_pointer[-1];
uint32_t dict_version = (uint32_t)this_instr->operand0;
uint16_t index = (uint16_t)this_instr->operand0;
@ -1890,7 +1891,7 @@
if (watched_mutations < _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
PyDict_Watch(GLOBALS_WATCHER_ID, dict);
_Py_BloomFilter_Add(dependencies, dict);
PyObject *res = convert_global_to_const(this_instr, dict, true);
PyObject *res = convert_global_to_const(this_instr, dict, false, true);
if (res == NULL) {
attr = sym_new_not_null(ctx);
}
@ -1903,7 +1904,12 @@
if (PyJitRef_IsNull(attr)) {
attr = sym_new_not_null(ctx);
}
o = owner;
CHECK_STACK_BOUNDS(1);
stack_pointer[-1] = attr;
stack_pointer[0] = o;
stack_pointer += 1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
@ -4005,6 +4011,19 @@
break;
}
case _INSERT_1_LOAD_CONST_INLINE: {
JitOptRef res;
JitOptRef l;
res = sym_new_not_null(ctx);
l = sym_new_not_null(ctx);
CHECK_STACK_BOUNDS(1);
stack_pointer[-1] = res;
stack_pointer[0] = l;
stack_pointer += 1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
case _INSERT_1_LOAD_CONST_INLINE_BORROW: {
JitOptRef res;
JitOptRef l;