-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better constant narrowing in the JIT optimizer #130415
Comments
Wait I'm a bit confused, why can't we narrow it at the point of the guard, instead of looking ahead by 1? I don't generally like peeking ahead opcodes as it breaks the encapsulation of each opcode in the DSL. |
|
Another option could be to have new symbolic types like At the very least, this gets something simple working and adds some tests for the desired behavior that we can improve the implementation of later. |
I think we should |
I think you might be focusing too much on the bool example. For most ops, we've already transformed the original value into an abstract bool, and don't have it anymore. Your suggestion wouldn't work for |
Talked to Brandt in private. Sadly, have to agree here that we either need to peek or need a new symbol. |
What we could do for narrowing type based on boolean guards is this:
That shouldn't be that much code, and the optimizer code for |
Yeah, after talking to @markshannon we decided that I'll get a PR up with the necessary infrastructure, and we'll update the PRs to use it once it's in. |
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-boolI'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_*_POP
_TO_BOOL_BOOL + _GUARD_IS_*_POP
_TO_BOOL_INT + _GUARD_IS_*_POP
_TO_BOOL_LIST + _GUARD_IS_*_POP
_TO_BOOL_STR + _GUARD_IS_*_POP
Narrow types to constants in branches involving comparisons with a constant:
_COMPARE_OP + _GUARD_IS_*_POP
(==
,!=
)_COMPARE_OP_FLOAT + _GUARD_IS_*_POP
(==
,!=
)_COMPARE_OP_INT + _GUARD_IS_*_POP
(==
,!=
)_COMPARE_OP_STR + _GUARD_IS_*_POP
(==
,!=
)_CONTAINS_OP + _GUARD_IS_*_POP
(in
,not in
)_IS_OP + _GUARD_IS_*_POP
(is
,is not
)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 teachremove_unneeded_uops
about this new instruction._COMPARE_OP
(==
,!=
,<
,>
,<=
,>=
)_COMPARE_OP_FLOAT
(==
,!=
,<
,>
,<=
,>=
)_COMPARE_OP_INT
(==
,!=
,<
,>
,<=
,>=
)_COMPARE_OP_STR
(==
,!=
,<
,>
,<=
,>=
)_CONTAINS_OP
(in
,not in
)_IS_OP
(is
,is not
)Linked PRs
str
to""
based on boolean tests #130476int
to0
based on boolean tests #130772The text was updated successfully, but these errors were encountered: