Repository navigation
Description
Please don't work on this. I'm planning on sprinting on this with new contributors at an event this weekend.
Currently, the JIT optimizer uses the _COMPARE_OP family, _CONTAINS_OP, _IS_OP, and the _TO_BOOL family to narrow the types of the input values and the type of the output value.
However, by "peeking ahead" and seeing how a value will be used, we can narrow these types to constants as well. As a simple example, consider _TO_BOOL_INT + _GUARD_IS_FALSE_CHECK on an unknown value. After the _TO_BOOL_INT, it can be narrowed to a known class, int (we do this today). However, after the _GUARD_IS_FALSE_CHECK, we can actually narrow it to a constant value, 0.
An example implementation of this idea for _TO_BOOL_BOOL is here: main...brandtbucher:cpython:hack-night-to-bool-bool
I've divided this work into 3 "waves" of increasing complexity. Tasks in bold are probably a bit harder, tasks in italics are probably a bit easier.
Narrow types to constants in branches involving truthiness:
-
_TO_BOOL + _GUARD_IS_*_POPgh-130659 -
_TO_BOOL_BOOL + _GUARD_IS_*_POPgh-130659 -
_TO_BOOL_INT + _GUARD_IS_*_POPgh-130772 -
_TO_BOOL_LIST + _GUARD_IS_*_POP -
_TO_BOOL_STR + _GUARD_IS_*_POPgh-130476
Narrow types to constants in branches involving comparisons with a constant:
-
_COMPARE_OP + _GUARD_IS_*_POP(==,!=) -
_COMPARE_OP_FLOAT + _GUARD_IS_*_POP(==,!=) gh-130415: Narrow types to constants in branches involving specialized comparisons with a constant #144150 -
_COMPARE_OP_INT + _GUARD_IS_*_POP(==,!=) gh-130415: Narrow types to constants in branches involving specialized comparisons with a constant #144150 -
_COMPARE_OP_STR + _GUARD_IS_*_POP(==,!=) gh-130415: Narrow types to constants in branches involving specialized comparisons with a constant #144150 -
_CONTAINS_OP + _GUARD_IS_*_POP(in,not in) -
_IS_OP + _GUARD_IS_*_POP(is,is not) gh-130415: Narrowing to constants in branches involvingiscomparisons with a constant #143895
Evaluate comparisons involving two constants:
This is related, but a bit more involved, since we need a way to pop two values from the stack and push a constant (_POP_TWO_LOAD_CONST_INLINE_BORROW). We should also teach remove_unneeded_uops about this new instruction.
-
_COMPARE_OP(==,!=,<,>,<=,>=) -
_COMPARE_OP_FLOAT(==,!=,<,>,<=,>=) gh-137062 -
_COMPARE_OP_INT(==,!=,<,>,<=,>=) gh-131489 -
_COMPARE_OP_STR(==,!=,<,>,<=,>=) gh-137062 -
_CONTAINS_OP(in,not in) -
_IS_OP(is,is not)
Linked PRs
- GH-130415: Narrow
strto""based on boolean tests #130476 - GH-130415: Optimize JIT path for _TO_BOOL_INT branching #130477
- GH-130415: Use boolean guards to narrow types to values in the JIT #130659
- GH-130415: Narrow
intto0based on boolean tests #130772 - GH-130415: Optimize constant comparison in JIT builds #131489
- GH-130415: Remove redundant
sym_matches_typecalls in the JIT optimizer #131778 - GH-130415: Improve the JIT's unneeded uop removal pass #132333
- gh-130415: Eliminate guards for constant CALL_BUILTIN_O/FAST #132708
- GH-130415: Use
POP_TWO_LOAD_CONST_INLINE_BORROW#134241 - GH-132732: Use pure op machinery to optimize
COMPARE_OP_INT/FLOAT/STR#137062 - gh-130415: Narrowing to constants in branches involving
iscomparisons with a constant #143895 - gh-130415: Narrow types to constants in branches involving specialized comparisons with a constant #144150
Activity
GH-130415: Use boolean guards to narrow types to values in the JIT (G…
GH-130415: Narrow int to 0 based on boolean tests (GH-130772)
GH-130415: Narrow str to "" based on boolean tests (GH-130476)
GH-130415: Optimize constant comparison in JIT builds (GH-131489)
GH-130415: Remove redundant sym_matches_type calls in the JIT optimiz…
tomasr8 commentedon Mar 30, 2025
Hi @brandtbucher! Are any of the tasks up for grabs still? I haven't worked on the JIT before, but I'd like to try :)
picnixz commentedon Mar 30, 2025
@tomasr8 I've updated the issue with the related PRs and what they were solving so you have enough tasks to pick up I think :')
pythonGH-130415: Remove redundant sym_matches_type calls in the JIT o…
brandtbucher commentedon Apr 3, 2025
Thanks for your interest, @tomasr8! I think we actually may have some good issues over on GH-131798.
Would you like to add a case to
optimizer_bytecodes.cfor_CONTAINS_OP_SET, which sets the output type to&PyBool_Type? That way, the JIT knows that code likefoo in some_setreturns abool. It should look just like the existing handler for_CONTAINS_OP. (Don't forget to runmake regen-cases!)This allows the JIT to automatically remove an unnecessary
_TO_BOOL_BOOLinstruction when people docondition = foo in some_set; if condition: .... The test for this should probably look something liketest_compare_pop_two_load_const_inline_borrow, except you'll need to write a newtestfuncand assert that_CONTAINS_OP_SETis in theuops, but_TO_BOOL_BOOLisn't. The latter check should fail before your change, and pass after.Let me know if you start working on it, and feel free to ping me anywhere (here, Discord, email) if you get stuck.
tomasr8 commentedon Apr 3, 2025
Sounds good! I will take a look :) Thanks for the detailed explanation of the issue, that is super helpful and it's pretty clear to me now what I need to do.
GH-130415: Improve the JIT's unneeded uop removal pass (GH-132333)
Fidget-Spinner commentedon May 19, 2025
I think we should pause this for now until #132733 is merged. Adding constant propagation code is error-prone and tedious. With that PR, contributors can just mark a bytecode as
pureand get it for free for everything. Then we could just add more to the cases generator to improve on the constant propagation code, rather than writing it manually ourselves.brandtbucher commentedon Jun 6, 2025
That makes sense for the third category ("evaluate comparisons involving two constants"). The second category ("narrow types to constants in branches involving comparisons with a constant") is probably still worth doing, but will require some new symbolic values to represent equality and inequality (similar to the new truthiness symbols).
Fidget-Spinner commentedon Jun 27, 2025
This PR is landed. So things are no longer blocked!
savannahostrowski commentedon Sep 18, 2025
Just crossed out
IS_OPsince @brandtbucher and I had discussed this and decided it was too complicated to handle.gh-130415: Narrowing to constants in branches involving `is` comparis…
gh-130415: Narrow types to constants in branches involving specialize…
Fidget-Spinner commentedon Jan 24, 2026
_COMPARE_OPis a little difficult as it's an overloaded op, and can call arbitrary Python code._CONTAINS_OPas well.Thanks to @reidenong , we completed IS_OP and COMPARE_OP_(TYPE). In the future, when we start recording types/values, we can narrow the LHS if its comparison is side-effect free #144179
This issue is done. Thanks everyone!
pythongh-130415: Narrowing to constants in branches involving `is` co…
pythongh-130415: Narrow types to constants in branches involving spec…