Skip to content
Merged
Prev Previous commit
Next Next commit
Merge branch 'main' into gh-148211/decompose-insert-1
  • Loading branch information
NekoAsakura committed Apr 9, 2026
commit fbf9c07175bb2c2b9cad7d7ffd24fe047fe50583
29 changes: 12 additions & 17 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ optimize_to_bool(
JitOptContext *ctx,
JitOptRef value,
JitOptRef *result_ptr,
uint16_t prefix, uint16_t load_op)
uint16_t prefix, uint16_t load_op, uint16_t suffix)
{
if (sym_matches_type(value, &PyBool_Type)) {
ADD_OP(_NOP, 0, 0);
Expand All @@ -328,14 +328,13 @@ optimize_to_bool(
int truthiness = sym_truthiness(ctx, value);
if (truthiness >= 0) {
PyObject *load = truthiness ? Py_True : Py_False;
if (insert_mode) {
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
ADD_OP(_SWAP, 2, 0);
} else {
ADD_OP(_POP_TOP, 0, 0);
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
if (prefix != _NOP) {
ADD_OP(prefix, 0, 0);
}
ADD_OP(load_op, 0, (uintptr_t)load);
if (suffix != _NOP) {
ADD_OP(suffix, 2, 0);
}
*result_ptr = sym_new_const(ctx, load);
return 1;
}
Expand Down Expand Up @@ -382,24 +381,20 @@ 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 prefix, uint16_t immortal_op, uint16_t mortal_op, uint16_t suffix)
Comment thread
NekoAsakura marked this conversation as resolved.
Outdated
{
// The cached value may be dead, so we need to do the lookup again... :(
if (type && PyType_Check(type)) {
PyObject *lookup = _PyType_Lookup(type, name);
if (lookup) {
bool immortal = _Py_IsImmortal(lookup) || (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE);
if (pop) {
ADD_OP(_POP_TOP, 0, 0);
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE,
0, (uintptr_t)lookup);
}
else {
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE,
0, (uintptr_t)lookup);
ADD_OP(_SWAP, 2, 0);
if (prefix != _NOP) {
ADD_OP(prefix, 0, 0);
}
ADD_OP(immortal ? immortal_op : mortal_op, 0, (uintptr_t)lookup);
if (suffix != _NOP) {
ADD_OP(suffix, 2, 0);
}
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
_Py_BloomFilter_Add(dependencies, type);
return sym_new_const(ctx, lookup);
Expand Down
34 changes: 13 additions & 21 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,
bool insert_mode);
uint16_t prefix, uint16_t load_op, uint16_t suffix);
Comment thread
NekoAsakura marked this conversation as resolved.
Outdated

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);
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _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);
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _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, _INSERT_1_LOAD_CONST_INLINE_BORROW);
_NOP, _LOAD_CONST_INLINE_BORROW, _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, _INSERT_1_LOAD_CONST_INLINE_BORROW);
_NOP, _LOAD_CONST_INLINE_BORROW, _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);
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _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, _INSERT_1_LOAD_CONST_INLINE_BORROW);
_NOP, _LOAD_CONST_INLINE_BORROW, _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);
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _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);
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _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);
_POP_TOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _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, _INSERT_1_LOAD_CONST_INLINE_BORROW, _INSERT_1_LOAD_CONST_INLINE);
_NOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _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, _INSERT_1_LOAD_CONST_INLINE_BORROW, _INSERT_1_LOAD_CONST_INLINE);
_NOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _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, _INSERT_1_LOAD_CONST_INLINE_BORROW, _INSERT_1_LOAD_CONST_INLINE);
_NOP, _LOAD_CONST_INLINE_BORROW, _LOAD_CONST_INLINE, _SWAP);
self = owner;
}

Expand Down Expand Up @@ -2019,10 +2019,6 @@ dummy_func(void) {
}
if (ctx->frame->globals_checked_version != 0 && ctx->frame->globals_watched) {
cnst = convert_global_to_const(this_instr, builtins);
if (cnst != NULL && this_instr->oparg & 1) {
assert(this_instr[1].opcode == _PUSH_NULL_CONDITIONAL);
assert(this_instr[1].oparg & 1);
}
}
}
if (cnst == NULL) {
Expand Down Expand Up @@ -2062,10 +2058,6 @@ dummy_func(void) {
}
if (ctx->frame->globals_checked_version == version) {
cnst = convert_global_to_const(this_instr, globals);
if (cnst != NULL && this_instr->oparg & 1) {
assert(this_instr[1].opcode == _PUSH_NULL_CONDITIONAL);
assert(this_instr[1].oparg & 1);
}
}
}
}
Expand Down
32 changes: 12 additions & 20 deletions Python/optimizer_cases.c.h

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

You are viewing a condensed version of this merge commit. You can view the full changes here.