-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
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
62fae72
52f7cc6
05c6089
d655709
3984a31
0606e89
578a6e0
1e01851
bb35e19
f9a8bc5
ee0325c
c7daa03
e9a1e9a
09c07a3
c64d69d
06247eb
a30ca62
fa17d1d
cd540b8
729ecc9
5d3090a
43c54d4
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -601,6 +601,23 @@ dummy_func( | |
| _REPLACE_WITH_TRUE + | ||
| POP_TOP; | ||
|
|
||
| tier2 op(_TO_BOOL_DICT, (value -- res)) { | ||
|
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. You can merge this with
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. I see what you mean. It goes into the internals of
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.
You can store it in the instruction operand0. |
||
| PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); | ||
| assert(PyDict_CheckExact(value_o)); | ||
| STAT_INC(TO_BOOL, hit); | ||
| res = PyDict_GET_SIZE(value_o) ? PyStackRef_True : PyStackRef_False; | ||
| PyStackRef_CLOSE(value); | ||
| } | ||
|
|
||
| tier2 op(_TO_BOOL_SIZED, (value -- res)) { | ||
| /* Covers types whose truthiness is Py_SIZE(obj) != 0: | ||
| tuple, set, frozenset, bytes, bytearray. */ | ||
| PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); | ||
| STAT_INC(TO_BOOL, hit); | ||
| res = Py_SIZE(value_o) ? PyStackRef_True : PyStackRef_False; | ||
| PyStackRef_CLOSE(value); | ||
| } | ||
|
|
||
| macro(UNARY_INVERT) = _UNARY_INVERT + POP_TOP; | ||
|
|
||
| op(_UNARY_INVERT, (value -- res, v)) { | ||
|
|
||
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.
Outdated
kumaraditya303 marked this conversation as resolved.
Outdated
|
||
| 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.
These tests are too verbose, I would prefer if you combine several of them into one, not each case needs a different test.
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.
Done!
I also looked into refactoring the optimizer code to use a table based approach. The result is
e121f12
If you want me to pull it into the branch give me a thumbs up.