-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-143732: Add tier2 specialization for TO_BOOL #148271
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
d35071a
44f5c49
b80a79b
ba88f27
29311d8
e9c1e24
9de1c2c
91334af
08a4535
05c6089
d655709
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -559,6 +559,17 @@ dummy_func(void) { | |
| int already_bool = optimize_to_bool(this_instr, ctx, value, &res, | ||
| _POP_TOP, _NOP); | ||
| if (!already_bool) { | ||
| PyTypeObject *tp = sym_get_type(value); | ||
| if (tp == &PyDict_Type) { | ||
|
eendebakpt marked this conversation as resolved.
|
||
| REPLACE_OP(this_instr, _TO_BOOL_DICT, 0, 0); | ||
| } | ||
| else if (tp == &PyTuple_Type || | ||
| tp == &PySet_Type || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect for set as it does not uses
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I updated the PR to handle the set/frozenset separately. We can also use your suggestion to fold everything into the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think so, in the JIT the offset would be burned into the machine code itself so the offset is fixed and not looked up at runtime. |
||
| tp == &PyFrozenSet_Type || | ||
| tp == &PyBytes_Type || | ||
| tp == &PyByteArray_Type) { | ||
| REPLACE_OP(this_instr, _TO_BOOL_SIZED, 0, 0); | ||
| } | ||
| res = sym_new_truthiness(ctx, value, true); | ||
| } | ||
| } | ||
|
|
@@ -626,6 +637,23 @@ dummy_func(void) { | |
| } | ||
| } | ||
|
|
||
| op(_TO_BOOL_DICT, (value -- res)) { | ||
|
eendebakpt marked this conversation as resolved.
Outdated
|
||
| int already_bool = optimize_to_bool(this_instr, ctx, value, &res, | ||
| _POP_TOP, _NOP); | ||
| if (!already_bool) { | ||
| sym_set_type(value, &PyDict_Type); | ||
| res = sym_new_truthiness(ctx, value, true); | ||
| } | ||
| } | ||
|
|
||
| op(_TO_BOOL_SIZED, (value -- res)) { | ||
| int already_bool = optimize_to_bool(this_instr, ctx, value, &res, | ||
| _POP_TOP, _NOP); | ||
| if (!already_bool) { | ||
| res = sym_new_truthiness(ctx, value, true); | ||
| } | ||
| } | ||
|
|
||
| op(_UNARY_NOT, (value -- res)) { | ||
| REPLACE_OPCODE_IF_EVALUATES_PURE(value, res); | ||
| sym_set_type(value, &PyBool_Type); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can merge this with
_TO_BOOL_SIZEDby using the fact that both do a fixed offset lookup.In tier2 optimizer you can set the offset for where the size is stored and do
size = (Py_ssize_t)((char *)obj + offset)and check that directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean. It goes into the internals of
PyDict(e.g. not usingPyDict_GET_SIZE, but doing manual offset calculations) and we also need to store the offset somewhere. So I think this is too much of a complication to get rid of a tier2 opcode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can store it in the instruction operand0.