cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Make the JIT optimizer buffer add to a new buffer, not in-place

Open Fidget-Spinner opened this issue 1 month ago • 1 comments

Feature or enhancement

Proposal:

Currently the JIT uop buffer modifies uops traces in place.

I think we should make it add to a new buffer. Having modifications be in-place is making our code messy and full of special cases.

So

REPLACE_OP(this_instr, ...)

should become

ADD_OP(ctx, ...)

in optimizer_bytecodes.c,

Also, the default generated case of optimizer_bytecodes.c should be to ADD_OP(pc, ...)

This is also blocking optimization in https://github.com/python/cpython/issues/143414, as ocassionally we need to insert extra guards to check the uniqueness of an object for example.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Fidget-Spinner avatar Jan 04 '26 21:01 Fidget-Spinner

I agree, but let's refactor the JIT structs to be heap allocated first.

markshannon avatar Jan 05 '26 14:01 markshannon

I agree, but let's refactor the JIT structs to be heap allocated first.

Assigning this to @cocolato

Fidget-Spinner avatar Jan 06 '26 10:01 Fidget-Spinner

How should we handle the following case? It appears that we cannot simply use ADD_OP as a substitute. Perhaps we still need a REPLACE_OP to handle the new buffer.

https://github.com/python/cpython/blob/e7f5ffa0de2476828d78b8d39caefc38d797c206/Python/optimizer_bytecodes.c#L1512 and https://github.com/python/cpython/blob/e7f5ffa0de2476828d78b8d39caefc38d797c206/Python/optimizer_bytecodes.c#L1024

cocolato avatar Jan 10 '26 10:01 cocolato

@cocolato make REPLACE_OP operate on the new buffer instead of the old one. Ideally you should have two buffers. An old buffer containing the current uops, and a new buffer, containing the uops being written.

If the optimizer stops early, you can just insert an _EXIT_TRACE at that location in the new buffer.

Fidget-Spinner avatar Jan 10 '26 10:01 Fidget-Spinner