Skip to content
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

gh-115859: Re-enable T2 optimizer pass by default #116062

Merged
merged 9 commits into from
Feb 28, 2024
2 changes: 2 additions & 0 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ extern void _Py_uop_sym_set_null(_Py_UopsSymbol *sym);
extern void _Py_uop_sym_set_non_null(_Py_UopsSymbol *sym);
extern void _Py_uop_sym_set_type(_Py_UopsSymbol *sym, PyTypeObject *typ);
extern void _Py_uop_sym_set_const(_Py_UopsSymbol *sym, PyObject *const_val);
extern bool _Py_uop_sym_is_bottom(_Py_UopsSymbol *sym);


extern int _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx);
extern void _Py_uop_abstractcontext_fini(_Py_UOpsContext *ctx);
Expand Down
21 changes: 20 additions & 1 deletion Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def f():
exe = get_first_executor(f)
self.assertIsNone(exe)


@unittest.skipIf(os.getenv("PYTHON_UOPS_OPTIMIZE") == "0", "Needs uop optimizer to run.")
class TestUops(unittest.TestCase):

def test_basic_loop(self):
Expand Down Expand Up @@ -570,7 +572,7 @@ def testfunc(n):
self.assertLessEqual(count, 2)


@unittest.skipIf(os.getenv("PYTHONUOPSOPTIMIZE", default=0) == 0, "Needs uop optimizer to run.")
@unittest.skipIf(os.getenv("PYTHON_UOPS_OPTIMIZE") == "0", "Needs uop optimizer to run.")
class TestUopsOptimization(unittest.TestCase):

def _run_with_optimizer(self, testfunc, arg):
Expand Down Expand Up @@ -890,5 +892,22 @@ def testfunc(n):
self.assertLessEqual(len(guard_both_float_count), 1)
self.assertIn("_COMPARE_OP_STR", uops)

def test_type_inconsistency(self):
def testfunc(n):
for i in range(n):
x = _test_global + _test_global
# Must be a real global else it won't be optimized to _LOAD_CONST_INLINE
global _test_global
_test_global = 0
_, ex = self._run_with_optimizer(testfunc, 16)
self.assertIsNone(ex)
_test_global = 1.2
_, ex = self._run_with_optimizer(testfunc, 16)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_GUARD_BOTH_INT", uops)
self.assertIn("_BINARY_OP_ADD_INT", uops)


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,8 +1008,8 @@ uop_optimize(
return err;
}
OPT_STAT_INC(traces_created);
char *uop_optimize = Py_GETENV("PYTHONUOPSOPTIMIZE");
if (uop_optimize == NULL || *uop_optimize > '0') {
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
err = _Py_uop_analyze_and_optimize(frame, buffer,
UOP_MAX_TRACE_LENGTH,
curr_stackentries, &dependencies);
Expand Down
10 changes: 4 additions & 6 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_set_non_null _Py_uop_sym_set_non_null
#define sym_set_type _Py_uop_sym_set_type
#define sym_set_const _Py_uop_sym_set_const
#define sym_is_bottom _Py_uop_sym_is_bottom
#define frame_new _Py_uop_frame_new
#define frame_pop _Py_uop_frame_pop

Expand Down Expand Up @@ -510,12 +511,9 @@ _Py_uop_analyze_and_optimize(

peephole_opt(frame, buffer, buffer_size);

char *uop_optimize = Py_GETENV("PYTHONUOPSOPTIMIZE");
if (uop_optimize != NULL && *uop_optimize > '0') {
err = optimize_uops(
(PyCodeObject *)frame->f_executable, buffer,
buffer_size, curr_stacklen, dependencies);
}
err = optimize_uops(
(PyCodeObject *)frame->f_executable, buffer,
buffer_size, curr_stacklen, dependencies);

if (err == 0) {
goto not_ready;
Expand Down
37 changes: 28 additions & 9 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
#define sym_set_non_null _Py_uop_sym_set_non_null
#define sym_set_type _Py_uop_sym_set_type
#define sym_set_const _Py_uop_sym_set_const
#define sym_is_bottom _Py_uop_sym_is_bottom
#define frame_new _Py_uop_frame_new
#define frame_pop _Py_uop_frame_pop

Expand Down Expand Up @@ -107,7 +108,9 @@ dummy_func(void) {
}

op(_BINARY_OP_ADD_INT, (left, right -- res)) {
if (sym_is_const(left) && sym_is_const(right)) {
if (sym_is_const(left) && sym_is_const(right) &&
sym_matches_type(left, &PyLong_Type) && sym_matches_type(right, &PyLong_Type))
{
assert(PyLong_CheckExact(sym_get_const(left)));
assert(PyLong_CheckExact(sym_get_const(right)));
PyObject *temp = _PyLong_Add((PyLongObject *)sym_get_const(left),
Expand All @@ -118,14 +121,17 @@ dummy_func(void) {
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to decref it here too on this error branch? Or am I remembering the ownership wrongly? Same for everything below.

Might be better to write it as:

res = sym_new_const(ctx, temp);
Py_DECREF(temp);
OUT_OF_SPACE_IF_NULL(res);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
Py_DECREF(temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type));
}
}

op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) {
if (sym_is_const(left) && sym_is_const(right)) {
if (sym_is_const(left) && sym_is_const(right) &&
sym_matches_type(left, &PyLong_Type) && sym_matches_type(right, &PyLong_Type))
{
assert(PyLong_CheckExact(sym_get_const(left)));
assert(PyLong_CheckExact(sym_get_const(right)));
PyObject *temp = _PyLong_Subtract((PyLongObject *)sym_get_const(left),
Expand All @@ -136,14 +142,17 @@ dummy_func(void) {
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
Py_DECREF(temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type));
}
}

op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
if (sym_is_const(left) && sym_is_const(right)) {
if (sym_is_const(left) && sym_is_const(right) &&
sym_matches_type(left, &PyLong_Type) && sym_matches_type(right, &PyLong_Type))
{
assert(PyLong_CheckExact(sym_get_const(left)));
assert(PyLong_CheckExact(sym_get_const(right)));
PyObject *temp = _PyLong_Multiply((PyLongObject *)sym_get_const(left),
Expand All @@ -154,14 +163,17 @@ dummy_func(void) {
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
Py_DECREF(temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type));
}
}

op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
if (sym_is_const(left) && sym_is_const(right)) {
if (sym_is_const(left) && sym_is_const(right) &&
sym_matches_type(left, &PyFloat_Type) && sym_matches_type(right, &PyFloat_Type))
{
assert(PyFloat_CheckExact(sym_get_const(left)));
assert(PyFloat_CheckExact(sym_get_const(right)));
PyObject *temp = PyFloat_FromDouble(
Expand All @@ -170,17 +182,20 @@ dummy_func(void) {
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
Py_DECREF(temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type));
}
}

op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
if (sym_is_const(left) && sym_is_const(right)) {
if (sym_is_const(left) && sym_is_const(right) &&
sym_matches_type(left, &PyFloat_Type) && sym_matches_type(right, &PyFloat_Type))
{
assert(PyFloat_CheckExact(sym_get_const(left)));
assert(PyFloat_CheckExact(sym_get_const(right)));
PyObject *temp = PyFloat_FromDouble(
Expand All @@ -189,17 +204,20 @@ dummy_func(void) {
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
Py_DECREF(temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type));
}
}

op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
if (sym_is_const(left) && sym_is_const(right)) {
if (sym_is_const(left) && sym_is_const(right) &&
sym_matches_type(left, &PyFloat_Type) && sym_matches_type(right, &PyFloat_Type))
{
assert(PyFloat_CheckExact(sym_get_const(left)));
assert(PyFloat_CheckExact(sym_get_const(right)));
PyObject *temp = PyFloat_FromDouble(
Expand All @@ -208,9 +226,10 @@ dummy_func(void) {
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
Py_DECREF(temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type));
Expand Down
36 changes: 27 additions & 9 deletions Python/optimizer_cases.c.h

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

2 changes: 1 addition & 1 deletion Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ sym_set_bottom(_Py_UopsSymbol *sym)
Py_CLEAR(sym->const_val);
}

static inline bool
bool
_Py_uop_sym_is_bottom(_Py_UopsSymbol *sym)
{
if ((sym->flags & IS_NULL) && (sym->flags & NOT_NULL)) {
Expand Down
Loading