Skip to content

Commit

Permalink
pythongh-115685: Type/values propagate for TO_BOOL in tier 2 (pythonG…
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidget-Spinner authored and diegorusso committed Apr 17, 2024
1 parent bf5ddf1 commit ce60550
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 30 deletions.
41 changes: 21 additions & 20 deletions Include/internal/pycore_uop_ids.h

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

2 changes: 2 additions & 0 deletions Include/internal/pycore_uop_metadata.h

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

5 changes: 5 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,11 @@ dummy_func(
value = ptr;
}

tier2 pure op (_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
Py_DECREF(pop);
value = ptr;
}

tier2 pure op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) {
value = Py_NewRef(ptr);
null = NULL;
Expand Down
11 changes: 11 additions & 0 deletions Python/executor_cases.c.h

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

61 changes: 61 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,67 @@ dummy_func(void) {
}
}

op(_TO_BOOL, (value -- res)) {
(void)value;
res = sym_new_type(ctx, &PyBool_Type);
OUT_OF_SPACE_IF_NULL(res);
}

op(_TO_BOOL_BOOL, (value -- value)) {
if (sym_matches_type(value, &PyBool_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
else {
if(!sym_set_type(value, &PyBool_Type)) {
goto hit_bottom;
}
}
}

op(_TO_BOOL_INT, (value -- res)) {
if (sym_is_const(value) && sym_matches_type(value, &PyLong_Type)) {
PyObject *load = _PyLong_IsZero((PyLongObject *)sym_get_const(value))
? Py_False : Py_True;
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
}
if(!sym_set_type(value, &PyLong_Type)) {
goto hit_bottom;
}
}

op(_TO_BOOL_LIST, (value -- res)) {
if(!sym_set_type(value, &PyList_Type)) {
goto hit_bottom;
}
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
}

op(_TO_BOOL_NONE, (value -- res)) {
if (sym_get_const(value) == Py_None) {
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)Py_False);
}
sym_set_const(value, Py_None);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, Py_False));
}

op(_TO_BOOL_STR, (value -- res)) {
if (sym_is_const(value) && sym_matches_type(value, &PyUnicode_Type)) {
PyObject *load = sym_get_const(value) == &_Py_STR(empty) ? Py_False : Py_True;
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
}
if(!sym_set_type(value, &PyUnicode_Type)) {
goto hit_bottom;
}
}

op(_LOAD_CONST, (-- value)) {
// There should be no LOAD_CONST. It should be all
// replaced by peephole_opt.
Expand Down
73 changes: 63 additions & 10 deletions Python/optimizer_cases.c.h

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

0 comments on commit ce60550

Please sign in to comment.