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

Conversation

brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Dec 11, 2024

It's more complex (ha) than we originally thought.

I think it's probably worth trying to do the "smart" thing here, since exponents with at least one constant part are reasonably common. But if others think the additional logic in the optimizer is too much, we can just say that the result of ** an **= is unknown (even when it may technically be knowable):

        if (oparg == NB_POWER || oparg == NB_INPLACE_POWER) {
            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 (sym_matches_type(left, &PyLong_Type) && sym_matches_type(left, &PyLong_Type)) {
            res = sym_new_type(ctx, &PyLong_Type);
        }
        else {
            res = sym_new_type(ctx, &PyFloat_Type);
        }

The runtime cost would be an additional guard in JIT code following a ** or **=.

@brandtbucher brandtbucher added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump topic-JIT labels Dec 11, 2024
@brandtbucher brandtbucher self-assigned this Dec 11, 2024
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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-JIT type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants