8000 GH-130415: Use boolean guards to narrow types to values in the JIT by brandtbucher · Pull Request #130659 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-130415: Use boolean guards to narrow types to values in the JIT #130659

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

Merged
merged 13 commits into from
Mar 2, 2025
Prev Previous commit
Next Next commit
Use the new truth symbols
  • Loading branch information
brandtbucher committed Feb 28, 2025
commit 41e77a88757ddd8439cdd87fb446837a3a3c0259
10 changes: 8 additions & 2 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
#define sym_tuple_getitem _Py_uop_sym_tuple_getitem
#define sym_tuple_length _Py_uop_sym_tuple_length
#define sym_is_immortal _Py_uop_sym_is_immortal
#define sym_new_truth _Py_uop_sym_new_truth

extern int
optimize_to_bool(
Expand Down Expand Up @@ -391,14 +392,14 @@ dummy_func(void) {

op(_TO_BOOL, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
res = sym_new_type(ctx, &PyBool_Type);
res = sym_new_truth(ctx, value, false);
}
}

op(_TO_BOOL_BOOL, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
sym_set_type(value, &PyBool_Type);
res = value;
res = sym_new_truth(ctx, value, false);
}
}

Expand Down Expand Up @@ -430,6 +431,11 @@ dummy_func(void) {
}
}

op(_UNARY_NOT, (value -- res)) {
sym_set_type(value, &PyBool_Type);
res = sym_new_truth(ctx, value, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From casual reading, this looks like an error as the true argument implies that this has the value True.

Maybe rename sym_new_truth as sym_from_truthiness and have false as the argument for NOT and true as the argument for TO_BOOL?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or symbolic consts, like TO_BOOL and INVERT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid a double-negative in the arg name (since inverting it from its current meaning would mean something like "not not"). What if I make it an int and use 0 and 1 instead? I feel like a big part of the friction in reading this is the use of true and false as arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 /1 or false/true doesn't matter. Good symbolic names is what is needed.
Maybe even use two different functions with different names?

}

op(_COMPARE_OP, (left, right -- res)) {< 728A /span>
if (oparg & 16) {
res = sym_new_type(ctx, &PyBool_Type);
Expand Down
9 changes: 6 additions & 3 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0