cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Constant evaluate/propagate pure ops automatically

Open Fidget-Spinner opened this issue 9 months ago • 2 comments

Feature or enhancement

Proposal:

@brandtbucher pointed out that we could automatically evaluate pure ops in the JIT.

Back when I originally envisioned this in the JIT optimizer, it was way simpler. Right now we have stackrefs which complicate things a little.

The main idea is that fully pure bytecodes can just run their body in the JIT optimizer, allowing for automatic constant prop/evaluation. This also cuts down code duplication and also the amount of human error from manually writing constant propagation code.

We'd have to replace all the stackref functions with the appropriate sym accessing functions, either through macros or the cases generator.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-132733
  • gh-136033
  • gh-136040
  • gh-136048
  • gh-136050

Fidget-Spinner avatar Apr 19 '25 16:04 Fidget-Spinner

Two follow-ups to this:

  • pure_guard, which evaluates guards in the optimizer and removes the op if it doesn't deopt.
  • Wholesale replacing of pure instructions with _POP_TOP_*_LOAD_CONST_INLINE_BORROW or whatever if the result is immortal.
  • Replacing all constant outputs like above, even if they're not immortal (requires executors to hold references to this new constant pool).

brandtbucher avatar Apr 24 '25 22:04 brandtbucher

Just talked with @savannahostrowski, she's going to modify the cases generator to replace the entire instruction when constant evaluation produces an immortal value.

brandtbucher avatar Jun 27 '25 19:06 brandtbucher

@Fidget-Spinner @brandtbucher Are we good to close this out after #137577 or is there more I should work on here? 😈

savannahostrowski avatar Sep 15 '25 15:09 savannahostrowski

@savannahostrowski we still have this left https://github.com/python/cpython/pull/136048 . The problem is with these constant propagation, the optimizer can now error, and we need to handle that properly.

Fidget-Spinner avatar Sep 15 '25 15:09 Fidget-Spinner

Ah! I missed that. I'll reassign this issue to you, and you can close it out when that lands.

savannahostrowski avatar Sep 15 '25 16:09 savannahostrowski

This is done! Thanks Savannah!

Fidget-Spinner avatar Sep 15 '25 16:09 Fidget-Spinner