Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Fidget-Spinner merged 4 commits intopython:mainfromtomasr8:jit-subscr-tuple
Apr 26, 2025

Conversation

tomasr8
Copy link
Member

@tomasr8tomasr8 commentedApr 26, 2025
edited by bedevere-appbot
Loading

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 thatx is1.

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

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
MemberAuthor

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 theDEOPT_IF checks into a separate guard for it to work?

Choose a reason for hiding this comment

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

Check out the code forsym_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.

tomasr8 reacted with heart emoji
Copy link
MemberAuthor

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);

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
MemberAuthor

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-SpinnerFidget-Spinner merged commit5e96e4f intopython:mainApr 26, 2025
62 checks passed
@Fidget-Spinner
Copy link
Member

Thanks!

tomasr8 reacted with rocket emoji

@tomasr8tomasr8 deleted the jit-subscr-tuple branchApril 26, 2025 19:15
Comment on lines +375 to +391
if (sym_is_const(ctx, sub_st)) {
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
long index = PyLong_AsLong(sym_get_const(ctx, sub_st));
assert(index >= 0);
int tuple_length = sym_tuple_length(tuple_st);
if (tuple_length == -1) {
// Unknown length
res = sym_new_not_null(ctx);
}
else {
assert(index < tuple_length);
res = sym_tuple_getitem(ctx, tuple_st, index);
}
}
else {
res = sym_new_not_null(ctx);
}
Copy link
Member

Choose a reason for hiding this comment

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

@tomasr8, this can be improved to handle abstract tuples that may not be constant (and it cleans things up, since it includes a length check):

Suggested change
if (sym_is_const(ctx,sub_st)) {
assert(PyLong_CheckExact(sym_get_const(ctx,sub_st)));
longindex=PyLong_AsLong(sym_get_const(ctx,sub_st));
assert(index >=0);
inttuple_length=sym_tuple_length(tuple_st);
if (tuple_length==-1) {
// Unknown length
res=sym_new_not_null(ctx);
}
else {
assert(index<tuple_length);
res=sym_tuple_getitem(ctx,tuple_st,index);
}
}
else {
res=sym_new_not_null(ctx);
}
longindex=PyLong_AsLong(sym_get_const(ctx,
sub_st));
// ...PyLong_AsLong can fail, need to check for error here...
res=sym_tuple_getitem(ctx,tuple_st,index)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

this can be improved to handle abstract tuples that may not be constant

@brandtbucher, I think this already handles abstract tuples? Assuming that abstract means those that haveJIT_SYM_TUPLE_TAG rather thanJIT_SYM_KNOWN_VALUE_TAG? For tuples that just haveJIT_SYM_KNOWN_CLASS_TAG we don't know the length so there's nothing we can do I believe.

But yeah, we can simplify the code since_Py_uop_sym_tuple_getitem can handle unknown length. The only difference is that it sets the type tounknown rather thannon null as we do here. Is that a problem? Shouldn't_Py_uop_sym_tuple_getitem always return at leastJIT_SYM_NON_NULL_TAG?

Copy link
Member

Choose a reason for hiding this comment

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

Your current code is gated onsym_is_const, which abstract tuples fail. The rest of your understanding is correct, though.

Is that a problem? Shouldn't_Py_uop_sym_tuple_getitem always return at leastJIT_SYM_NON_NULL_TAG?

Sounds like you found your next PR. :)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I completely misunderstood, sorry. I thought you were checking whether the tuple was const, not the index. Disregard!

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

no worries :) I think we can still simplify the code a bit though!

Comment on lines +380 to +383
if (tuple_length == -1) {
// Unknown length
res = sym_new_not_null(ctx);
}
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

@brandtbucher
Following theprevious discussion, I'm wondering if it is correct to returnsym_new_not_null here rather thansym_new_unknown?

We have an abstract tuple and we don't know its length so it is impossible to be sure that the index is not out of bounds for the tuple. Given that, I think it is incorrect to returnsym_new_not_null here as regular Python code could raise anIndexError. Should we change this tosym_new_unknown?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@brandtbucherbrandtbucherbrandtbucher left review comments

@Fidget-SpinnerFidget-SpinnerFidget-Spinner approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@tomasr8@Fidget-Spinner@brandtbucher

[8]ページ先頭

©2009-2025 Movatter.jp