Constant evaluate/propagate pure ops automatically
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
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
pureinstructions with_POP_TOP_*_LOAD_CONST_INLINE_BORROWor 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).
Just talked with @savannahostrowski, she's going to modify the cases generator to replace the entire instruction when constant evaluation produces an immortal value.
@Fidget-Spinner @brandtbucher Are we good to close this out after #137577 or is there more I should work on here? 😈
@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.
Ah! I missed that. I'll reassign this issue to you, and you can close it out when that lands.
This is done! Thanks Savannah!