Skip to content
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
1 change: 1 addition & 0 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ extern JitOptRef _Py_uop_sym_new_type(
extern JitOptRef _Py_uop_sym_new_const(JitOptContext *ctx, PyObject *const_val);
extern JitOptRef _Py_uop_sym_new_const_steal(JitOptContext *ctx, PyObject *const_val);
bool _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef sym);
bool _Py_uop_sym_is_container(JitOptRef sym);
_PyStackRef _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptRef sym);
extern JitOptRef _Py_uop_sym_new_null(JitOptContext *ctx);
extern bool _Py_uop_sym_has_type(JitOptRef sym);
Expand Down
1 change: 1 addition & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
#define sym_is_not_null _Py_uop_sym_is_not_null
#define sym_is_const _Py_uop_sym_is_const
#define sym_is_safe_const _Py_uop_sym_is_safe_const
#define sym_is_container _Py_uop_sym_is_container
#define sym_get_const _Py_uop_sym_get_const
#define sym_new_const_steal _Py_uop_sym_new_const_steal
#define sym_get_const_as_stackref _Py_uop_sym_get_const_as_stackref
Expand Down
6 changes: 4 additions & 2 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ dummy_func(void) {
res = sym_new_not_null(ctx);
ds = dict_st;
ss = sub_st;
if (sym_matches_type(dict_st, &PyFrozenDict_Type)) {
if (!sym_is_container(sub_st) &&
sym_matches_type(dict_st, &PyFrozenDict_Type)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(dict_st, sub_st, res);
}
}
Expand Down Expand Up @@ -706,7 +707,8 @@ dummy_func(void) {
b = sym_new_type(ctx, &PyBool_Type);
l = left;
r = right;
if (sym_matches_type(right, &PyFrozenSet_Type)) {
if (!sym_is_container(left) &&
sym_matches_type(right, &PyFrozenSet_Type)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b);
}
}
Expand Down
6 changes: 4 additions & 2 deletions Python/optimizer_cases.c.h

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

15 changes: 15 additions & 0 deletions Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef sym)
(typ == &PyFrozenSet_Type);
}

bool
_Py_uop_sym_is_container(JitOptRef sym)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's better to make this an allowlist, so we should say sym_is_not_container and check for the usual int float str etc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Otherwise custom containers might be let through

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done :)

{
PyTypeObject *typ = _Py_uop_sym_get_type(sym);
if (typ == NULL) {
return false;
}
return (typ == &PyFrozenSet_Type) ||
(typ == &PyFrozenDict_Type) ||
(typ == &PySet_Type) ||
(typ == &PyDict_Type) ||
(typ == &PyList_Type) ||
(typ == &PyTuple_Type);
}

void
_Py_uop_sym_set_type(JitOptContext *ctx, JitOptRef ref, PyTypeObject *typ)
{
Expand Down
Loading