Skip to content

GH-135379: Support limited scalar replacement for replicated uops in the JIT code generator. #135563

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

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Jun 16, 2025

This PR does three things:

  • Performs limited replacement of array stack items with scalars for uops.
  • Modify replicate to support ranges starting from values other than zero
  • Replicates COPY and SWAP to take advantage of that.

With TOS caching this should allow COPY and SWAP to be implemented as register-to-register moves.

…nerator. Use it to support efficient specializations of COPY and SWAP in the JIT.
@Fidget-Spinner
Copy link
Member

Can you call the PR something else instead of "scalar replacement"? Scalar replacement is the other term for escape analysis in the JVM https://devblogs.microsoft.com/java/improving-openjdk-scalar-replacement-part-1-3/

I know CS has a lot of overlapping terms, but I think it would be clearer to call it "change replicates to ranges" or something.

@Fidget-Spinner
Copy link
Member

scalarize works too.

] + items[index+1:]

def scalarize_stack(stack: StackEffect, oparg: int) -> StackEffect:
# Only scalarize if no more than one input or output is array
Copy link
Member

Choose a reason for hiding this comment

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

This comment doesn't seem to belong to this code segment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I refactored the code but didn't move the comment.

@markshannon
Copy link
Member Author

This is scalar replacement. We are replacing a composite object (an array in this case) with a set of scalar values.

In the JVM, that means breaking down an object into its parts. The JVM version is a lot more complex and powerful, but it is doing the same thing, and the motivation is the same: register allocation.

@Fidget-Spinner
Copy link
Member

This is scalar replacement. We are replacing a composite object (an array in this case) with a set of scalar values.

In the JVM, that means breaking down an object into its parts. The JVM version is a lot more complex and powerful, but it is doing the same thing, and the motivation is the same: register allocation.

It's somewhat confusing because it's scalar replacement in the JIT, but not in the default interpreter. Though you subtly implied it with the "uops".

@markshannon markshannon changed the title GH-135379: Support limited scalar replacement for replicated uops in the code generator. GH-135379: Support limited scalar replacement for replicated uops in the JIT code generator. Jun 16, 2025
Comment on lines +873 to +880
index = -1
for i, item in enumerate(items):
if "oparg" in item.size:
if index >= 0:
return items
index = i
if index < 0:
return items
Copy link
Member

Choose a reason for hiding this comment

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

This entire part is somewhat confusing, but I'll let it slide since there's a comment up top :).

@markshannon markshannon merged commit 8dd8b5c into python:main Jun 17, 2025
69 checks passed
lkollar pushed a commit to lkollar/cpython that referenced this pull request Jun 19, 2025
…ps in the JIT code generator. (pythonGH-135563)

* Use it to support efficient specializations of COPY and SWAP in the JIT.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants