8000 GH-130415: Narrow `str` to `""` based on boolean tests by fluhus · Pull Request #130476 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-130415: Narrow str to "" based on boolean tests #130476

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 9 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Assign truthiness to empty strings in JIT
  • Loading branch information
fluhus committed Mar 3, 2025
commit 601392df5fadcc94c1ef91e570123bdac1b9d3f2
15 changes: 1 addition & 14 deletions Python/optimizer_bytecodes.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -426,22 +426,9 @@ dummy_func(void) {

op(_TO_BOOL_STR, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
res = sym_new_type(ctx, &PyBool_Type);
res = sym_new_truthiness(ctx, value, true);
sym_set_type(value, &PyUnicode_Type);
}
if (!sym_is_const(value)) {
assert(sym_matches_type(value, &PyUnicode_Type));
int next_opcode = (this_instr + 1)->opcode;
assert(next_opcode == _CHECK_VALIDITY_AND_SET_IP);
next_opcode = (this_instr + 2)->opcode;
// If the next uop is a guard, we can narrow value. However, we
// *can't* narrow res, since that would cause the guard to be
// removed and the narrowed value to be invalid:
if (next_opcode == _GUARD_IS_FALSE_POP) {
sym_set_const(value, Py_GetConstant(Py_CONSTANT_EMPTY_STR));
res = sym_new_type(ctx, &PyBool_Type);
}
}
}

op(_UNARY_NOT, (value -- res)) {
Expand Down
16 changes: 1 addition & 15 deletions Python/optimizer_cases.c.h

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

3 changes: 3 additions & 0 deletions Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ _Py_uop_sym_set_const(JitOptContext *ctx, JitOptSymbol *sym, PyObject *const_val
else if (type == &PyBool_Type) {
_Py_uop_sym_set_const(ctx, value, Py_False);
}
else if (type == &PyUnicode_Type) {
_Py_uop_sym_set_const(ctx, value, Py_GetConstant(Py_CONSTANT_EMPTY_STR));
}
// TODO: More types (GH-130415)!
make_const(sym, const_val);
return;
Expand Down
Loading
0