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-127809: Fix the JIT's understanding of ** #127844

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import itertools
import sys
import textwrap
import unittest
Expand Down Expand Up @@ -1511,6 +1512,49 @@ def test_jit_error_pops(self):
with self.assertRaises(TypeError):
{item for item in items}

def test_power_type_depends_on_input_values(self):
template = textwrap.dedent("""
import _testinternalcapi

L, R, X, Y = {l}, {r}, {x}, {y}

def check(actual: complex, expected: complex) -> None:
assert actual == expected, (actual, expected)
assert type(actual) is type(expected), (actual, expected)

def f(l: complex, r: complex) -> None:
expected_local_local = pow(l, r) + pow(l, r)
expected_const_local = pow(L, r) + pow(L, r)
expected_local_const = pow(l, R) + pow(l, R)
expected_const_const = pow(L, R) + pow(L, R)
for _ in range(_testinternalcapi.TIER2_THRESHOLD):
# Narrow types:
l + l, r + r
# The powers produce results, and the addition is unguarded:
check(l ** r + l ** r, expected_local_local)
check(L ** r + L ** r, expected_const_local)
check(l ** R + l ** R, expected_local_const)
check(L ** R + L ** R, expected_const_const)

# JIT for one pair of values...
f(L, R)
# ...then run with another:
f(X, Y)
""")
interesting = [
(1, 1), # int ** int -> int
(1, -1), # int ** int -> float
(1, 1.0), # int ** float -> float
(-1, 0.1), # int ** float -> complex
(1.0, 1), # float ** int -> float
(1.0, 1.0), # float ** float -> float
(-1.0, 0.1), # float ** float -> complex
]
for (l, r), (x, y) in itertools.product(interesting, repeat=2):
s = template.format(l=l, r=r, x=x, y=y)
with self.subTest(l=l, r=r, x=x, y=y):
script_helper.assert_python_ok("-c", s)


def global_identity(x):
return x
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an issue where the experimental JIT may infer an incorrect result type
for exponentiation (``**`` and ``**=``), leading to bugs or crashes.
16 changes: 16 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ dummy_func(
pure op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyLong_CheckExact(left_o));
assert(PyLong_CheckExact(right_o));

STAT_INC(BINARY_OP, hit);
PyObject *res_o = _PyLong_Multiply((PyLongObject *)left_o, (PyLongObject *)right_o);
Expand All @@ -524,6 +526,8 @@ dummy_func(
pure op(_BINARY_OP_ADD_INT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyLong_CheckExact(left_o));
assert(PyLong_CheckExact(right_o));

STAT_INC(BINARY_OP, hit);
PyObject *res_o = _PyLong_Add((PyLongObject *)left_o, (PyLongObject *)right_o);
Expand All @@ -537,6 +541,8 @@ dummy_func(
pure op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyLong_CheckExact(left_o));
assert(PyLong_CheckExact(right_o));

STAT_INC(BINARY_OP, hit);
PyObject *res_o = _PyLong_Subtract((PyLongObject *)left_o, (PyLongObject *)right_o);
Expand Down Expand Up @@ -574,6 +580,8 @@ dummy_func(
pure op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));

STAT_INC(BINARY_OP, hit);
double dres =
Expand All @@ -588,6 +596,8 @@ dummy_func(
pure op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));

STAT_INC(BINARY_OP, hit);
double dres =
Expand All @@ -602,6 +612,8 @@ dummy_func(
pure op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));

STAT_INC(BINARY_OP, hit);
double dres =
Expand Down Expand Up @@ -631,6 +643,8 @@ dummy_func(
pure op(_BINARY_OP_ADD_UNICODE, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyUnicode_CheckExact(left_o));
assert(PyUnicode_CheckExact(right_o));

STAT_INC(BINARY_OP, hit);
PyObject *res_o = PyUnicode_Concat(left_o, right_o);
Expand All @@ -653,6 +667,8 @@ dummy_func(
op(_BINARY_OP_INPLACE_ADD_UNICODE, (left, right --)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyUnicode_CheckExact(left_o));
assert(PyUnicode_CheckExact(right_o));

int next_oparg;
#if TIER_ONE
Expand Down
16 changes: 16 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.

16 changes: 16 additions & 0 deletions Python/generated_cases.c.h

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

81 changes: 69 additions & 12 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,81 @@ dummy_func(void) {
}

op(_BINARY_OP, (left, right -- res)) {
PyTypeObject *ltype = sym_get_type(left);
PyTypeObject *rtype = sym_get_type(right);
if (ltype != NULL && (ltype == &PyLong_Type || ltype == &PyFloat_Type) &&
rtype != NULL && (rtype == &PyLong_Type || rtype == &PyFloat_Type))
{
if (oparg != NB_TRUE_DIVIDE && oparg != NB_INPLACE_TRUE_DIVIDE &&
ltype == &PyLong_Type && rtype == &PyLong_Type) {
/* If both inputs are ints and the op is not division the result is an int */
res = sym_new_type(ctx, &PyLong_Type);
bool lhs_int = sym_matches_type(left, &PyLong_Type);
Copy link
Member

Choose a reason for hiding this comment

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

Could you please leave a comment at the top explaining what this is? Something like

            (1, 1),  # int ** int -> int
            (1, -1),  # int ** int -> float
            (1, 1.0),  # int ** float -> float
            (-1, 0.1),  # int ** float -> complex
            (1.0, 1),  # float ** int -> float
            (1.0, 1.0),  # float ** float -> float
            (-1.0, 0.1),  # float ** float -> complex

would suffice.

bool rhs_int = sym_matches_type(right, &PyLong_Type);
bool lhs_float = sym_matches_type(left, &PyFloat_Type);
bool rhs_float = sym_matches_type(right, &PyFloat_Type);
if ((!lhs_int && !lhs_float) || (!rhs_int && !rhs_float)) {
res = sym_new_unknown(ctx);
goto binary_op_done;
}
if (oparg == NB_POWER || oparg == NB_INPLACE_POWER) {
// This one's fun: the *type* of the result depends on the *values*
Copy link
Member

@markshannon markshannon Dec 12, 2024

Choose a reason for hiding this comment

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

I'm not convinced that this is worth the complexity for anything other than integer constant right hand sides.
It the rhs is an int constant, then the result is the type of the lhs (for ints and floats) otherwise, "not known".
Calculating any other result is so slow that the cost of the guard we could potentially remove is negligible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't a negative int rhs produce a float?

>>> 3 ** -1
0.3333333333333333

Your point may still stand that it's too expensive, but unfortunately the assumption that if rhs is an int we can determine the result without looking at the value doesn't hold.

// being exponentiated. But exponents with one constant part are
// reasonably common, so it's probably worth trying to be precise:
PyObject *lhs_const = sym_get_const(left);
PyObject *rhs_const = sym_get_const(right);
if (lhs_int && rhs_int) {
if (rhs_const == NULL) {
// Unknown RHS means either int or float:
res = sym_new_unknown(ctx);
goto binary_op_done;
}
if (!_PyLong_IsNegative((PyLongObject *)rhs_const)) {
// Non-negative RHS means int:
res = sym_new_type(ctx, &PyLong_Type);
goto binary_op_done;
}
// Negative RHS uses float_pow...
}
else {
/* For any other op combining ints/floats the result is a float */
// Negative LHS *and* non-integral RHS means complex. So we need to
// disprove at least one to prove a float result:
if (rhs_int) {
// Integral RHS means float:
res = sym_new_type(ctx, &PyFloat_Type);
goto binary_op_done;
}
if (rhs_const) {
double rhs_double = PyFloat_AS_DOUBLE(rhs_const);
if (rhs_double == floor(rhs_double)) {
// Integral RHS means float:
res = sym_new_type(ctx, &PyFloat_Type);
goto binary_op_done;
}
}
if (lhs_const) {
if (lhs_int) {
if (!_PyLong_IsNegative((PyLongObject *)lhs_const)) {
// Non-negative LHS means float:
res = sym_new_type(ctx, &PyFloat_Type);
goto binary_op_done;
}
}
else if (0.0 <= PyFloat_AS_DOUBLE(lhs_const)) {
// Non-negative LHS means float:
res = sym_new_type(ctx, &PyFloat_Type);
goto binary_op_done;
}
if (rhs_const) {
// If we have two constants and failed to disprove that it's
// complex, then it's complex:
res = sym_new_type(ctx, &PyComplex_Type);
goto binary_op_done;
}
}
// Couldn't prove anything. It's either float or complex:
res = sym_new_unknown(ctx);
}
else if (oparg == NB_TRUE_DIVIDE || oparg == NB_INPLACE_TRUE_DIVIDE) {
res = sym_new_type(ctx, &PyFloat_Type);
}
else if (lhs_int && rhs_int) {
res = sym_new_type(ctx, &PyLong_Type);
}
else {
res = sym_new_unknown(ctx);
res = sym_new_type(ctx, &PyFloat_Type);
}
binary_op_done:
}

op(_BINARY_OP_ADD_INT, (left, right -- res)) {
Expand Down
Loading
Loading