Skip to content

Commit 17634a8

Browse files
Push fix noticed by Mark and Brandt
1 parent 53ce10f commit 17634a8

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ typedef struct _JitOptContext {
260260
extern bool _Py_uop_sym_is_null(JitOptSymbol *sym);
261261
extern bool _Py_uop_sym_is_not_null(JitOptSymbol *sym);
262262
extern bool _Py_uop_sym_is_const(JitOptContext *ctx, JitOptSymbol *sym);
263+
extern bool _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptSymbol *sym);
263264
extern PyObject *_Py_uop_sym_get_const(JitOptContext *ctx, JitOptSymbol *sym);
264265
extern JitOptSymbol *_Py_uop_sym_new_const_steal(JitOptContext *ctx, PyObject *const_val);
265266
extern _PyStackRef _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptSymbol *sym);

Lib/test/test_generated_cases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,7 +2270,7 @@ def test_pure_uop_body_copied_in(self):
22702270
JitOptSymbol *res;
22712271
foo = stack_pointer[-1];
22722272
if (
2273-
sym_is_const(ctx, foo)
2273+
sym_is_safe_const(ctx, foo)
22742274
) {
22752275
JitOptSymbol *foo_sym = foo;
22762276
_PyStackRef foo = sym_get_const_as_stackref(ctx, foo_sym);
@@ -2312,7 +2312,7 @@ def test_pure_uop_body_copied_in_complex(self):
23122312
JitOptSymbol *res;
23132313
foo = stack_pointer[-1];
23142314
if (
2315-
sym_is_const(ctx, foo)
2315+
sym_is_safe_const(ctx, foo)
23162316
) {
23172317
JitOptSymbol *foo_sym = foo;
23182318
_PyStackRef foo = sym_get_const_as_stackref(ctx, foo_sym);

Python/optimizer_analysis.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
319319
/* Shortened forms for convenience, used in optimizer_bytecodes.c */
320320
#define sym_is_not_null _Py_uop_sym_is_not_null
321321
#define sym_is_const _Py_uop_sym_is_const
322+
#define sym_is_safe_const _Py_uop_sym_is_safe_const
322323
#define sym_get_const _Py_uop_sym_get_const
323324
#define sym_new_const_steal _Py_uop_sym_new_const_steal
324325
#define sym_get_const_as_stackref _Py_uop_sym_get_const_as_stackref

Python/optimizer_cases.c.h

Lines changed: 15 additions & 15 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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptSymbol *sym)
159159
return PyStackRef_FromPyObjectImmortalUnchecked(const_val);
160160
}
161161

162+
/*
163+
Indicates whether the constant is safe to constant evaluate
164+
(without side effects).
165+
*/
166+
bool
167+
_Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptSymbol *sym)
168+
{
169+
PyObject *const_val = _Py_uop_sym_get_const(ctx, sym);
170+
if (const_val == NULL) {
171+
return false;
172+
}
173+
PyTypeObject *typ = Py_TYPE(const_val);
174+
return (typ == &PyLong_Type) ||
175+
(typ == &PyUnicode_Type) ||
176+
(typ == &PyFloat_Type) ||
177+
(typ == &PyDict_Type) ||
178+
(typ == &PyTuple_Type) ||
179+
(typ == &PyList_Type);
180+
}
181+
162182
void
163183
_Py_uop_sym_set_type(JitOptContext *ctx, JitOptSymbol *sym, PyTypeObject *typ)
164184
{

Tools/cases_generator/optimizer_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ def write_uop_pure_evaluation_region_header(
187187
emitter.emit("if (\n")
188188
assert len(uop.stack.inputs) > 0, "Pure operations must have at least 1 input"
189189
for inp in uop.stack.inputs[:-1]:
190-
emitter.emit(f"sym_is_const(ctx, {inp.name}) &&\n")
191-
emitter.emit(f"sym_is_const(ctx, {uop.stack.inputs[-1].name})\n")
190+
emitter.emit(f"sym_is_safe_const(ctx, {inp.name}) &&\n")
191+
emitter.emit(f"sym_is_safe_const(ctx, {uop.stack.inputs[-1].name})\n")
192192
emitter.emit(') {\n')
193193
# Declare variables, before they are shadowed.
194194
for inp in uop.stack.inputs:

0 commit comments

Comments
 (0)