Skip to content

Commit ed1204e

Browse files
committed
gh-131798: Narrow the return type of _FORMAT_SIMPLE and _FORMAT_WITH_SPEC to str for built-in types
1 parent c1b9a83 commit ed1204e

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ extern JitOptRef _Py_uop_sym_new_type(
393393

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);
396+
extern bool _Py_uop_sym_is_safe_type(JitOptRef sym);
396397
bool _Py_uop_sym_is_safe_const(JitOptContext *ctx, 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);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Allow the JIT to remove unicode guards after ``_FORMAT_SIMPLE`` and
2-
``_FORMAT_WITH_SPEC`` by setting the return type to string.
2+
``_FORMAT_WITH_SPEC`` when the input type is a known built-in type.

Python/optimizer_analysis.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
250250
/* Shortened forms for convenience, used in optimizer_bytecodes.c */
251251
#define sym_is_not_null _Py_uop_sym_is_not_null
252252
#define sym_is_const _Py_uop_sym_is_const
253+
#define sym_is_safe_type _Py_uop_sym_is_safe_type
253254
#define sym_is_safe_const _Py_uop_sym_is_safe_const
254255
#define sym_get_const _Py_uop_sym_get_const
255256
#define sym_new_const_steal _Py_uop_sym_new_const_steal

Python/optimizer_bytecodes.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,11 +1552,19 @@ dummy_func(void) {
15521552
}
15531553

15541554
op(_FORMAT_SIMPLE, (value -- res)) {
1555-
res = sym_new_type(ctx, &PyUnicode_Type);
1555+
if (sym_is_safe_type(value)) {
1556+
res = sym_new_type(ctx, &PyUnicode_Type);
1557+
} else {
1558+
res = sym_new_not_null(ctx);
1559+
}
15561560
}
15571561

15581562
op(_FORMAT_WITH_SPEC, (value, fmt_spec -- res)) {
1559-
res = sym_new_type(ctx, &PyUnicode_Type);
1563+
if (sym_is_safe_type(value)) {
1564+
res = sym_new_type(ctx, &PyUnicode_Type);
1565+
} else {
1566+
res = sym_new_not_null(ctx);
1567+
}
15601568
}
15611569

15621570
op(_SET_UPDATE, (set, unused[oparg-1], iterable -- set, unused[oparg-1], i)) {

Python/optimizer_cases.c.h

Lines changed: 14 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,25 @@ _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptRef sym)
264264
return PyStackRef_FromPyObjectBorrow(const_val);
265265
}
266266

267+
/*
268+
Indicates whether the type is a known built-in type
269+
that is safe to narrow.
270+
*/
271+
bool
272+
_Py_uop_sym_is_safe_type(JitOptRef sym)
273+
{
274+
PyTypeObject *typ = _Py_uop_sym_get_type(sym);
275+
if (typ == NULL) {
276+
return false;
277+
}
278+
return (typ == &PyLong_Type) ||
279+
(typ == &PyUnicode_Type) ||
280+
(typ == &PyFloat_Type) ||
281+
(typ == &_PyNone_Type) ||
282+
(typ == &PyBool_Type) ||
283+
(typ == &PyFrozenDict_Type);
284+
}
285+
267286
/*
268287
Indicates whether the constant is safe to constant evaluate
269288
(without side effects).

0 commit comments

Comments
 (0)