cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Interaction of refcount elimination and constant propagation is complex to handle

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

Feature or enhancement

Proposal:

With refcount elimination, ops may have more than one output. E.g.

op(_BINARY_OP_ADD_INT, (lhs, rhs -- res))

is now

op(_BINARY_OP_ADD_INT, (lhs, rhs -- res, l, r))

This introduces a problem in the optimizer --- REPLACE_OPCODE_IF_EVALUATES_PURE cannot deal with >1 output.

The solution is not too complex: Emit the op as _SWAP_TWO_LOAD_CONST_INLINE_BORROW, with signature:

op(_SWAP_TWO_LOAD_CONST_INLINE_BORROW, (res/4, lhs, rhs -- res, l, r))

and add the res to REPLACE_OPCODE_IF_EVALUATES_PURE to tell it which results to check. So REPLACE_OPCODE_IF_EVALUATES_PURE(lhs, rhs) becomes REPLACE_OPCODE_IF_EVALUATES_PURE(lhs, rhs, res)

Then we have everything we need and can reuse the current code.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-143335

Fidget-Spinner avatar Dec 15 '25 20:12 Fidget-Spinner