Skip to content

gh-131798: JIT: Propagate the result in _BINARY_OP_SUBSCR_TUPLE_INT #133003

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

Merged
merged 4 commits into from
Apr 26, 2025

Conversation

tomasr8
Copy link
Member

@tomasr8 tomasr8 commented Apr 26, 2025

More context: #132851 (comment)

This propagates the information about tuple elements after _BINARY_OP_SUBSCR_TUPLE_INT when the RHS is a constant.
For example in

foo = (1, 2)
x = foo[0]  # _BINARY_OP_SUBSCR_TUPLE_INT

we can now deduce that x is 1.

Comment on lines 378 to 379
assert(index >= 0);
assert(index < sym_tuple_length(left));
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure these asserts are actually needed. The instruction will deopt when this is not true so perhaps we can remove them?

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting, we are actually hitting the assertion:

Python/optimizer_cases.c.h:630: optimize_uops: Assertion `index < sym_tuple_length(left)' failed.

I guess I need to move the DEOPT_IF checks into a separate guard for it to work?

Copy link
Member

Choose a reason for hiding this comment

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

Check out the code for sym_tuple_length. It can return -1 if the length is not known. So for example if you propagate a PyTuple_Type, but not the length, it will fail.

You need to check that the length is not -1.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it, thanks! I somehow didn't realize you can know something is a tuple but not know its length..

long index = PyLong_AsLong(sym_get_const(ctx, right));
assert(index >= 0);
assert(index < sym_tuple_length(left));
res = sym_tuple_getitem(ctx, left, index);
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit unfortunate. I would wish we could automatically evaluate this, but it seems tuples are a special category so we can't.

#132733

Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't fully digested your PR yet, but maybe there is some way to extend it to support tuples as well?

@Fidget-Spinner Fidget-Spinner merged commit 5e96e4f into python:main Apr 26, 2025
62 checks passed
@Fidget-Spinner
Copy link
Member

Thanks!

@tomasr8 tomasr8 deleted the jit-subscr-tuple branch April 26, 2025 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants