Skip to content

Commit 853dafe

Browse files
authored
gh-148083: Prevent constant folding when lhs is container types (gh-148090)
1 parent c398490 commit 853dafe

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ extern JitOptRef _Py_uop_sym_new_type(
394394
extern JitOptRef _Py_uop_sym_new_const(JitOptContext *ctx, PyObject *const_val);
395395
extern JitOptRef _Py_uop_sym_new_const_steal(JitOptContext *ctx, PyObject *const_val);
396396
bool _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef sym);
397+
bool _Py_uop_sym_is_not_container(JitOptRef sym);
397398
_PyStackRef _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptRef sym);
398399
extern JitOptRef _Py_uop_sym_new_null(JitOptContext *ctx);
399400
extern bool _Py_uop_sym_has_type(JitOptRef sym);

Python/optimizer_analysis.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
252252
#define sym_is_not_null _Py_uop_sym_is_not_null
253253
#define sym_is_const _Py_uop_sym_is_const
254254
#define sym_is_safe_const _Py_uop_sym_is_safe_const
255+
#define sym_is_not_container _Py_uop_sym_is_not_container
255256
#define sym_get_const _Py_uop_sym_get_const
256257
#define sym_new_const_steal _Py_uop_sym_new_const_steal
257258
#define sym_get_const_as_stackref _Py_uop_sym_get_const_as_stackref

Python/optimizer_bytecodes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ dummy_func(void) {
515515
res = sym_new_not_null(ctx);
516516
ds = dict_st;
517517
ss = sub_st;
518-
if (sym_matches_type(dict_st, &PyFrozenDict_Type)) {
518+
if (sym_is_not_container(sub_st) &&
519+
sym_matches_type(dict_st, &PyFrozenDict_Type)) {
519520
REPLACE_OPCODE_IF_EVALUATES_PURE(dict_st, sub_st, res);
520521
}
521522
}
@@ -706,7 +707,8 @@ dummy_func(void) {
706707
b = sym_new_type(ctx, &PyBool_Type);
707708
l = left;
708709
r = right;
709-
if (sym_matches_type(right, &PyFrozenSet_Type)) {
710+
if (sym_is_not_container(left) &&
711+
sym_matches_type(right, &PyFrozenSet_Type)) {
710712
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b);
711713
}
712714
}

Python/optimizer_cases.c.h

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_symbols.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef sym)
287287
(typ == &PyFrozenSet_Type);
288288
}
289289

290+
bool
291+
_Py_uop_sym_is_not_container(JitOptRef sym)
292+
{
293+
PyTypeObject *typ = _Py_uop_sym_get_type(sym);
294+
if (typ == NULL) {
295+
return false;
296+
}
297+
return (typ == &PyLong_Type) ||
298+
(typ == &PyFloat_Type) ||
299+
(typ == &PyUnicode_Type) ||
300+
(typ == &_PyNone_Type) ||
301+
(typ == &PyBool_Type);
302+
}
303+
290304
void
291305
_Py_uop_sym_set_type(JitOptContext *ctx, JitOptRef ref, PyTypeObject *typ)
292306
{

0 commit comments

Comments
 (0)