GH-131798: Remove type checks for _TO_BOOL_STR (GH-131816)

This commit is contained in:
Amit Lavon 2025-03-30 16:07:25 -07:00 committed by GitHub
parent 39fa19a4cc
commit 685fd74f81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 185 additions and 125 deletions

View file

@ -512,10 +512,14 @@ dummy_func(
res = PyStackRef_False;
}
inst(TO_BOOL_STR, (unused/1, unused/2, value -- res)) {
op(_GUARD_TOS_UNICODE, (value -- value)) {
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
EXIT_IF(!PyUnicode_CheckExact(value_o));
}
op(_TO_BOOL_STR, (value -- res)) {
STAT_INC(TO_BOOL, hit);
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
if (value_o == &_Py_STR(empty)) {
assert(_Py_IsImmortal(value_o));
DEAD(value);
@ -528,6 +532,9 @@ dummy_func(
}
}
macro(TO_BOOL_STR) =
_GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR;
op(_REPLACE_WITH_TRUE, (value -- res)) {
PyStackRef_CLOSE(value);
res = PyStackRef_True;

View file

@ -653,16 +653,23 @@
break;
}
case _TO_BOOL_STR: {
case _GUARD_TOS_UNICODE: {
_PyStackRef value;
_PyStackRef res;
value = stack_pointer[-1];
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
if (!PyUnicode_CheckExact(value_o)) {
UOP_STAT_INC(uopcode, miss);
JUMP_TO_JUMP_TARGET();
}
break;
}
case _TO_BOOL_STR: {
_PyStackRef value;
_PyStackRef res;
value = stack_pointer[-1];
STAT_INC(TO_BOOL, hit);
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
if (value_o == &_Py_STR(empty)) {
assert(_Py_IsImmortal(value_o));
res = PyStackRef_False;

View file

@ -11616,30 +11616,37 @@
static_assert(INLINE_CACHE_ENTRIES_TO_BOOL == 3, "incorrect cache size");
_PyStackRef value;
_PyStackRef res;
// _GUARD_TOS_UNICODE
{
value = stack_pointer[-1];
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
if (!PyUnicode_CheckExact(value_o)) {
UPDATE_MISS_STATS(TO_BOOL);
assert(_PyOpcode_Deopt[opcode] == (TO_BOOL));
JUMP_TO_PREDICTED(TO_BOOL);
}
}
/* Skip 1 cache entry */
/* Skip 2 cache entries */
value = stack_pointer[-1];
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
if (!PyUnicode_CheckExact(value_o)) {
UPDATE_MISS_STATS(TO_BOOL);
assert(_PyOpcode_Deopt[opcode] == (TO_BOOL));
JUMP_TO_PREDICTED(TO_BOOL);
}
STAT_INC(TO_BOOL, hit);
if (value_o == &_Py_STR(empty)) {
assert(_Py_IsImmortal(value_o));
res = PyStackRef_False;
}
else {
assert(Py_SIZE(value_o));
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(value);
stack_pointer = _PyFrame_GetStackPointer(frame);
res = PyStackRef_True;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
// _TO_BOOL_STR
{
STAT_INC(TO_BOOL, hit);
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
if (value_o == &_Py_STR(empty)) {
assert(_Py_IsImmortal(value_o));
res = PyStackRef_False;
}
else {
assert(Py_SIZE(value_o));
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(value);
stack_pointer = _PyFrame_GetStackPointer(frame);
res = PyStackRef_True;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
}
}
stack_pointer[-1] = res;
DISPATCH();

View file

@ -414,10 +414,16 @@ dummy_func(void) {
}
}
op(_GUARD_TOS_UNICODE, (value -- value)) {
if (sym_matches_type(value, &PyUnicode_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_type(value, &PyUnicode_Type);
}
op(_TO_BOOL_STR, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
res = sym_new_truthiness(ctx, value, true);
sym_set_type(value, &PyUnicode_Type);
}
}

View file

@ -208,13 +208,22 @@
break;
}
case _GUARD_TOS_UNICODE: {
JitOptSymbol *value;
value = stack_pointer[-1];
if (sym_matches_type(value, &PyUnicode_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_type(value, &PyUnicode_Type);
break;
}
case _TO_BOOL_STR: {
JitOptSymbol *value;
JitOptSymbol *res;
value = stack_pointer[-1];
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
res = sym_new_truthiness(ctx, value, true);
sym_set_type(value, &PyUnicode_Type);
}
stack_pointer[-1] = res;
break;