Skip to content
Merged
Prev Previous commit
Next Next commit
gh-148211: decompose _INSERT_1_LOAD_CONST_INLINE(_BORROW) in JIT
  • Loading branch information
NekoAsakura committed Apr 9, 2026
commit 1e94a6c33e44d8a08ddb65342924793f0563bc4b
9 changes: 5 additions & 4 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ optimize_to_bool(
JitOptContext *ctx,
JitOptRef value,
JitOptRef *result_ptr,
uint16_t prefix, uint16_t load_op, uint16_t suffix)
uint16_t prefix, uint16_t suffix)
{
if (sym_matches_type(value, &PyBool_Type)) {
ADD_OP(_NOP, 0, 0);
Expand All @@ -332,7 +332,7 @@ optimize_to_bool(
if (prefix != _NOP) {
ADD_OP(prefix, 0, 0);
}
ADD_OP(load_op, 0, (uintptr_t)load);
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
if (suffix != _NOP) {
ADD_OP(suffix, 2, 0);
}
Expand Down Expand Up @@ -382,7 +382,7 @@ eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, bool exit
static JitOptRef
lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction *this_instr,
PyTypeObject *type, PyObject *name,
uint16_t prefix, uint16_t immortal_op, uint16_t mortal_op, uint16_t suffix)
uint16_t prefix, uint16_t suffix)
{
// The cached value may be dead, so we need to do the lookup again... :(
if (type && PyType_Check(type)) {
Expand All @@ -392,7 +392,8 @@ lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction
if (prefix != _NOP) {
ADD_OP(prefix, 0, 0);
}
ADD_OP(immortal ? immortal_op : mortal_op, 0, (uintptr_t)lookup);
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE,
0, (uintptr_t)lookup);
if (suffix != _NOP) {
ADD_OP(suffix, 2, 0);
}
Expand Down
26 changes: 13 additions & 13 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ optimize_to_bool(
JitOptContext *ctx,
JitOptSymbol *value,
JitOptSymbol **result_ptr,
uint16_t prefix, uint16_t load_op, uint16_t suffix);
uint16_t prefix, uint16_t suffix);

extern void
eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, bool exit);
Expand Down Expand Up @@ -537,23 +537,23 @@ dummy_func(void) {

op(_TO_BOOL, (value -- res)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res,
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _NOP);
_POP_TOP, _NOP);
if (!already_bool) {
res = sym_new_truthiness(ctx, value, true);
}
}

op(_TO_BOOL_BOOL, (value -- value)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &value,
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _NOP);
_POP_TOP, _NOP);
if (!already_bool) {
sym_set_type(value, &PyBool_Type);
}
}

op(_TO_BOOL_INT, (value -- res, v)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res,
_NOP, _LOAD_CONST_INLINE_BORROW, _SWAP);
_NOP, _SWAP);
if (!already_bool) {
sym_set_type(value, &PyLong_Type);
res = sym_new_truthiness(ctx, value, true);
Expand All @@ -563,7 +563,7 @@ dummy_func(void) {

op(_TO_BOOL_LIST, (value -- res, v)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res,
_NOP, _LOAD_CONST_INLINE_BORROW, _SWAP);
_NOP, _SWAP);
if (!already_bool) {
res = sym_new_type(ctx, &PyBool_Type);
}
Expand All @@ -572,7 +572,7 @@ dummy_func(void) {

op(_TO_BOOL_NONE, (value -- res)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res,
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _NOP);
_POP_TOP, _NOP);
if (!already_bool) {
sym_set_const(value, Py_None);
res = sym_new_const(ctx, Py_False);
Expand All @@ -599,7 +599,7 @@ dummy_func(void) {

op(_TO_BOOL_STR, (value -- res, v)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res,
_NOP, _LOAD_CONST_INLINE_BORROW, _SWAP);
_NOP, _SWAP);
v = value;
if (!already_bool) {
res = sym_new_truthiness(ctx, value, true);
Expand Down Expand Up @@ -900,31 +900,31 @@ dummy_func(void) {
PyTypeObject *type = (PyTypeObject *)sym_get_const(ctx, owner);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _NOP);
_POP_TOP, _NOP);
}

op(_LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES, (descr/4, owner -- attr)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _NOP);
_POP_TOP, _NOP);
}

op(_LOAD_ATTR_NONDESCRIPTOR_NO_DICT, (descr/4, owner -- attr)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _NOP);
_POP_TOP, _NOP);
}

op(_LOAD_ATTR_METHOD_WITH_VALUES, (descr/4, owner -- attr, self)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
_NOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _SWAP);
_NOP, _SWAP);
self = owner;
}

Expand All @@ -933,7 +933,7 @@ dummy_func(void) {
PyTypeObject *type = sym_get_type(owner);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
_NOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _SWAP);
_NOP, _SWAP);
self = owner;
}

Expand All @@ -942,7 +942,7 @@ dummy_func(void) {
PyTypeObject *type = sym_get_type(owner);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
_NOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _SWAP);
_NOP, _SWAP);
self = owner;
}

Expand Down
24 changes: 12 additions & 12 deletions Python/optimizer_cases.c.h

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

Loading