Skip to content

GH-115819: Eliminate Boolean guards when value is known #116355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extern _Py_UopsSymbol *_Py_uop_sym_new_type(
_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_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_set_null(_Py_UopsSymbol *sym);
extern bool _Py_uop_sym_set_non_null(_Py_UopsSymbol *sym);
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ def testfunc(a):
ex = get_first_executor(testfunc)
self.assertIsNotNone(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 testfunc(a):
Expand All @@ -347,7 +348,8 @@ def testfunc(a):
ex = get_first_executor(testfunc)
self.assertIsNotNone(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 testfunc(n):
Expand Down
11 changes: 11 additions & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_is_null _Py_uop_sym_is_null
#define sym_new_const _Py_uop_sym_new_const
#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_set_null _Py_uop_sym_set_null
#define sym_set_non_null _Py_uop_sym_set_non_null
Expand Down Expand Up @@ -324,6 +325,16 @@ optimize_to_bool(
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. */
static int
optimize_uops(
Expand Down
41 changes: 41 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
#define sym_new_const _Py_uop_sym_new_const
#define sym_new_null _Py_uop_sym_new_null
#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_non_null _Py_uop_sym_set_non_null
#define sym_set_type _Py_uop_sym_set_type
Expand All @@ -36,6 +37,8 @@ optimize_to_bool(
_Py_UopsSymbol *value,
_Py_UopsSymbol **result_ptr);

extern void
eliminate_pop_guard(_PyUOpInstruction *this_instr, bool exit)

static int
dummy_func(void) {
Expand Down Expand Up @@ -557,7 +560,45 @@ dummy_func(void) {
(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 //
Expand Down
36 changes: 36 additions & 0 deletions Python/optimizer_cases.c.h

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

9 changes: 9 additions & 0 deletions Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ _Py_uop_sym_new_null(_Py_UOpsContext *ctx)
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
_Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ)
{
Expand Down