-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
GH-127809: Fix the JIT's understanding of **
#127844
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
Changes from 4 commits
a082766
43d8a3d
5e6088e
222b2c8
2833dd4
e7076cc
bef3cc8
4c23d2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
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* | ||
|
||
// 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)) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.