mirror of https://github.com/python/cpython.git
GH-115819: Eliminate Boolean guards when value is known (GH-116355)
This commit is contained in:
parent
c91bdf86ef
commit
0c81ce1360
|
@ -90,6 +90,7 @@ extern _Py_UopsSymbol *_Py_uop_sym_new_type(
|
||||||
_Py_UOpsContext *ctx, PyTypeObject *typ);
|
_Py_UOpsContext *ctx, PyTypeObject *typ);
|
||||||
extern _Py_UopsSymbol *_Py_uop_sym_new_const(_Py_UOpsContext *ctx, PyObject *const_val);
|
extern _Py_UopsSymbol *_Py_uop_sym_new_const(_Py_UOpsContext *ctx, PyObject *const_val);
|
||||||
extern _Py_UopsSymbol *_Py_uop_sym_new_null(_Py_UOpsContext *ctx);
|
extern _Py_UopsSymbol *_Py_uop_sym_new_null(_Py_UOpsContext *ctx);
|
||||||
|
extern bool _Py_uop_sym_has_type(_Py_UopsSymbol *sym);
|
||||||
extern bool _Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ);
|
extern bool _Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ);
|
||||||
extern bool _Py_uop_sym_set_null(_Py_UopsSymbol *sym);
|
extern bool _Py_uop_sym_set_null(_Py_UopsSymbol *sym);
|
||||||
extern bool _Py_uop_sym_set_non_null(_Py_UopsSymbol *sym);
|
extern bool _Py_uop_sym_set_non_null(_Py_UopsSymbol *sym);
|
||||||
|
|
|
@ -331,7 +331,8 @@ def testfunc(a):
|
||||||
ex = get_first_executor(testfunc)
|
ex = get_first_executor(testfunc)
|
||||||
self.assertIsNotNone(ex)
|
self.assertIsNotNone(ex)
|
||||||
uops = get_opnames(ex)
|
uops = get_opnames(ex)
|
||||||
self.assertIn("_GUARD_IS_NOT_NONE_POP", uops)
|
self.assertNotIn("_GUARD_IS_NONE_POP", uops)
|
||||||
|
self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops)
|
||||||
|
|
||||||
def test_pop_jump_if_not_none(self):
|
def test_pop_jump_if_not_none(self):
|
||||||
def testfunc(a):
|
def testfunc(a):
|
||||||
|
@ -347,7 +348,8 @@ def testfunc(a):
|
||||||
ex = get_first_executor(testfunc)
|
ex = get_first_executor(testfunc)
|
||||||
self.assertIsNotNone(ex)
|
self.assertIsNotNone(ex)
|
||||||
uops = get_opnames(ex)
|
uops = get_opnames(ex)
|
||||||
self.assertIn("_GUARD_IS_NONE_POP", uops)
|
self.assertNotIn("_GUARD_IS_NONE_POP", uops)
|
||||||
|
self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops)
|
||||||
|
|
||||||
def test_pop_jump_if_true(self):
|
def test_pop_jump_if_true(self):
|
||||||
def testfunc(n):
|
def testfunc(n):
|
||||||
|
|
|
@ -292,6 +292,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
|
||||||
#define sym_is_null _Py_uop_sym_is_null
|
#define sym_is_null _Py_uop_sym_is_null
|
||||||
#define sym_new_const _Py_uop_sym_new_const
|
#define sym_new_const _Py_uop_sym_new_const
|
||||||
#define sym_new_null _Py_uop_sym_new_null
|
#define sym_new_null _Py_uop_sym_new_null
|
||||||
|
#define sym_has_type _Py_uop_sym_has_type
|
||||||
#define sym_matches_type _Py_uop_sym_matches_type
|
#define sym_matches_type _Py_uop_sym_matches_type
|
||||||
#define sym_set_null _Py_uop_sym_set_null
|
#define sym_set_null _Py_uop_sym_set_null
|
||||||
#define sym_set_non_null _Py_uop_sym_set_non_null
|
#define sym_set_non_null _Py_uop_sym_set_non_null
|
||||||
|
@ -324,6 +325,16 @@ optimize_to_bool(
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
eliminate_pop_guard(_PyUOpInstruction *this_instr, bool exit)
|
||||||
|
{
|
||||||
|
REPLACE_OP(this_instr, _POP_TOP, 0, 0);
|
||||||
|
if (exit) {
|
||||||
|
REPLACE_OP((this_instr+1), _EXIT_TRACE, 0, 0);
|
||||||
|
this_instr[1].target = this_instr->target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 1 for success, 0 for not ready, cannot error at the moment. */
|
/* 1 for success, 0 for not ready, cannot error at the moment. */
|
||||||
static int
|
static int
|
||||||
optimize_uops(
|
optimize_uops(
|
||||||
|
|
|
@ -21,6 +21,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
|
||||||
#define sym_new_const _Py_uop_sym_new_const
|
#define sym_new_const _Py_uop_sym_new_const
|
||||||
#define sym_new_null _Py_uop_sym_new_null
|
#define sym_new_null _Py_uop_sym_new_null
|
||||||
#define sym_matches_type _Py_uop_sym_matches_type
|
#define sym_matches_type _Py_uop_sym_matches_type
|
||||||
|
#define sym_has_type _Py_uop_sym_has_type
|
||||||
#define sym_set_null _Py_uop_sym_set_null
|
#define sym_set_null _Py_uop_sym_set_null
|
||||||
#define sym_set_non_null _Py_uop_sym_set_non_null
|
#define sym_set_non_null _Py_uop_sym_set_non_null
|
||||||
#define sym_set_type _Py_uop_sym_set_type
|
#define sym_set_type _Py_uop_sym_set_type
|
||||||
|
@ -36,6 +37,8 @@ optimize_to_bool(
|
||||||
_Py_UopsSymbol *value,
|
_Py_UopsSymbol *value,
|
||||||
_Py_UopsSymbol **result_ptr);
|
_Py_UopsSymbol **result_ptr);
|
||||||
|
|
||||||
|
extern void
|
||||||
|
eliminate_pop_guard(_PyUOpInstruction *this_instr, bool exit)
|
||||||
|
|
||||||
static int
|
static int
|
||||||
dummy_func(void) {
|
dummy_func(void) {
|
||||||
|
@ -557,7 +560,45 @@ dummy_func(void) {
|
||||||
(void)iter;
|
(void)iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
op(_GUARD_IS_TRUE_POP, (flag -- )) {
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, value != Py_True);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
op(_GUARD_IS_FALSE_POP, (flag -- )) {
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, value != Py_False);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
op(_GUARD_IS_NONE_POP, (flag -- )) {
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, !Py_IsNone(value));
|
||||||
|
}
|
||||||
|
else if (sym_has_type(flag)) {
|
||||||
|
assert(!sym_matches_type(flag, &_PyNone_Type));
|
||||||
|
eliminate_pop_guard(this_instr, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
op(_GUARD_IS_NOT_NONE_POP, (flag -- )) {
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, Py_IsNone(value));
|
||||||
|
}
|
||||||
|
else if (sym_has_type(flag)) {
|
||||||
|
assert(!sym_matches_type(flag, &_PyNone_Type));
|
||||||
|
eliminate_pop_guard(this_instr, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// END BYTECODES //
|
// END BYTECODES //
|
||||||
|
|
|
@ -1803,21 +1803,57 @@
|
||||||
/* _INSTRUMENTED_POP_JUMP_IF_NOT_NONE is not a viable micro-op for tier 2 */
|
/* _INSTRUMENTED_POP_JUMP_IF_NOT_NONE is not a viable micro-op for tier 2 */
|
||||||
|
|
||||||
case _GUARD_IS_TRUE_POP: {
|
case _GUARD_IS_TRUE_POP: {
|
||||||
|
_Py_UopsSymbol *flag;
|
||||||
|
flag = stack_pointer[-1];
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, value != Py_True);
|
||||||
|
}
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case _GUARD_IS_FALSE_POP: {
|
case _GUARD_IS_FALSE_POP: {
|
||||||
|
_Py_UopsSymbol *flag;
|
||||||
|
flag = stack_pointer[-1];
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, value != Py_False);
|
||||||
|
}
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case _GUARD_IS_NONE_POP: {
|
case _GUARD_IS_NONE_POP: {
|
||||||
|
_Py_UopsSymbol *flag;
|
||||||
|
flag = stack_pointer[-1];
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, !Py_IsNone(value));
|
||||||
|
}
|
||||||
|
else if (sym_has_type(flag)) {
|
||||||
|
assert(!sym_matches_type(flag, &_PyNone_Type));
|
||||||
|
eliminate_pop_guard(this_instr, true);
|
||||||
|
}
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case _GUARD_IS_NOT_NONE_POP: {
|
case _GUARD_IS_NOT_NONE_POP: {
|
||||||
|
_Py_UopsSymbol *flag;
|
||||||
|
flag = stack_pointer[-1];
|
||||||
|
if (sym_is_const(flag)) {
|
||||||
|
PyObject *value = sym_get_const(flag);
|
||||||
|
assert(value != NULL);
|
||||||
|
eliminate_pop_guard(this_instr, Py_IsNone(value));
|
||||||
|
}
|
||||||
|
else if (sym_has_type(flag)) {
|
||||||
|
assert(!sym_matches_type(flag, &_PyNone_Type));
|
||||||
|
eliminate_pop_guard(this_instr, false);
|
||||||
|
}
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,6 +231,15 @@ _Py_uop_sym_new_null(_Py_UOpsContext *ctx)
|
||||||
return null_sym;
|
return null_sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
_Py_uop_sym_has_type(_Py_UopsSymbol *sym)
|
||||||
|
{
|
||||||
|
if (_Py_uop_sym_is_bottom(sym)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return sym->typ != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
_Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ)
|
_Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue