Skip to content

Commit 3a09234

Browse files
committed
Remove sym_is_const
1 parent ff97d94 commit 3a09234

File tree

5 files changed

+163
-160
lines changed

5 files changed

+163
-160
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ typedef struct _JitOptContext {
258258

259259
extern bool _Py_uop_sym_is_null(JitOptSymbol *sym);
260260
extern bool _Py_uop_sym_is_not_null(JitOptSymbol *sym);
261-
extern bool _Py_uop_sym_is_const(JitOptContext *ctx, JitOptSymbol *sym);
262261
extern PyObject *_Py_uop_sym_get_const(JitOptContext *ctx, JitOptSymbol *sym);
263262
extern JitOptSymbol *_Py_uop_sym_new_unknown(JitOptContext *ctx);
264263
extern JitOptSymbol *_Py_uop_sym_new_not_null(JitOptContext *ctx);

Python/optimizer_analysis.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
316316

317317
/* Shortened forms for convenience, used in optimizer_bytecodes.c */
318318
#define sym_is_not_null _Py_uop_sym_is_not_null
319-
#define sym_is_const _Py_uop_sym_is_const
320319
#define sym_get_const _Py_uop_sym_get_const
321320
#define sym_new_unknown _Py_uop_sym_new_unknown
322321
#define sym_new_not_null _Py_uop_sym_new_not_null

Python/optimizer_bytecodes.c

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
1010

1111
/* Shortened forms for convenience */
1212
#define sym_is_not_null _Py_uop_sym_is_not_null
13-
#define sym_is_const _Py_uop_sym_is_const
1413
#define sym_get_const _Py_uop_sym_get_const
1514
#define sym_new_unknown _Py_uop_sym_new_unknown
1615
#define sym_new_not_null _Py_uop_sym_new_not_null
@@ -184,7 +183,7 @@ dummy_func(void) {
184183
// Case C:
185184
res = sym_new_type(ctx, &PyFloat_Type);
186185
}
187-
else if (!sym_is_const(ctx, rhs)) {
186+
else if (!sym_get_const(ctx, rhs)) {
188187
// Case A or B... can't know without the sign of the RHS:
189188
res = sym_new_unknown(ctx);
190189
}
@@ -209,11 +208,12 @@ dummy_func(void) {
209208
}
210209

211210
op(_BINARY_OP_ADD_INT, (left, right -- res)) {
212-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
213-
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
214-
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
215-
PyObject *temp = _PyLong_Add((PyLongObject *)sym_get_const(ctx, left),
216-
(PyLongObject *)sym_get_const(ctx, right));
211+
PyLongObject *lhs = (PyLongObject *)sym_get_const(ctx, left);
212+
PyLongObject *rhs = (PyLongObject *)sym_get_const(ctx, right);
213+
if (lhs && rhs) {
214+
assert(PyLong_CheckExact(lhs));
215+
assert(PyLong_CheckExact(rhs));
216+
PyObject *temp = _PyLong_Add(lhs, rhs);
217217
if (temp == NULL) {
218218
goto error;
219219
}
@@ -228,11 +228,12 @@ dummy_func(void) {
228228
}
229229

230230
op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) {
231-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
232-
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
233-
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
234-
PyObject *temp = _PyLong_Subtract((PyLongObject *)sym_get_const(ctx, left),
235-
(PyLongObject *)sym_get_const(ctx, right));
231+
PyLongObject *lhs = (PyLongObject *)sym_get_const(ctx, left);
232+
PyLongObject *rhs = (PyLongObject *)sym_get_const(ctx, right);
233+
if (lhs && rhs) {
234+
assert(PyLong_CheckExact(lhs));
235+
assert(PyLong_CheckExact(rhs));
236+
PyObject *temp = _PyLong_Subtract(lhs, rhs);
236237
if (temp == NULL) {
237238
goto error;
238239
}
@@ -247,11 +248,12 @@ dummy_func(void) {
247248
}
248249

249250
op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
250-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
251-
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
252-
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
253-
PyObject *temp = _PyLong_Multiply((PyLongObject *)sym_get_const(ctx, left),
254-
(PyLongObject *)sym_get_const(ctx, right));
251+
PyLongObject *lhs = (PyLongObject *)sym_get_const(ctx, left);
252+
PyLongObject *rhs = (PyLongObject *)sym_get_const(ctx, right);
253+
if (lhs && rhs) {
254+
assert(PyLong_CheckExact(lhs));
255+
assert(PyLong_CheckExact(rhs));
256+
PyObject *temp = _PyLong_Multiply(lhs, rhs);
255257
if (temp == NULL) {
256258
goto error;
257259
}
@@ -266,12 +268,14 @@ dummy_func(void) {
266268
}
267269

268270
op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
269-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
270-
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
271-
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
271+
PyObject *lhs = sym_get_const(ctx, left);
272+
PyObject *rhs = sym_get_const(ctx, right);
273+
if (lhs && rhs) {
274+
assert(PyFloat_CheckExact(lhs));
275+
assert(PyFloat_CheckExact(rhs));
272276
PyObject *temp = PyFloat_FromDouble(
273-
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) +
274-
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
277+
PyFloat_AS_DOUBLE(lhs) +
278+
PyFloat_AS_DOUBLE(rhs));
275279
if (temp == NULL) {
276280
goto error;
277281
}
@@ -286,12 +290,14 @@ dummy_func(void) {
286290
}
287291

288292
op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
289-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
290-
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
291-
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
293+
PyObject *lhs = sym_get_const(ctx, left);
294+
PyObject *rhs = sym_get_const(ctx, right);
295+
if (lhs && rhs) {
296+
assert(PyFloat_CheckExact(lhs));
297+
assert(PyFloat_CheckExact(rhs));
292298
PyObject *temp = PyFloat_FromDouble(
293-
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) -
294-
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
299+
PyFloat_AS_DOUBLE(lhs) -
300+
PyFloat_AS_DOUBLE(rhs));
295301
if (temp == NULL) {
296302
goto error;
297303
}
@@ -306,12 +312,14 @@ dummy_func(void) {
306312
}
307313

308314
op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
309-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
310-
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
311-
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
315+
PyObject *lhs = sym_get_const(ctx, left);
316+
PyObject *rhs = sym_get_const(ctx, right);
317+
if (lhs && rhs) {
318+
assert(PyFloat_CheckExact(lhs));
319+
assert(PyFloat_CheckExact(rhs));
312320
PyObject *temp = PyFloat_FromDouble(
313-
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) *
314-
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
321+
PyFloat_AS_DOUBLE(lhs) *
322+
PyFloat_AS_DOUBLE(rhs));
315323
if (temp == NULL) {
316324
goto error;
317325
}
@@ -326,10 +334,12 @@ dummy_func(void) {
326334
}
327335

328336
op(_BINARY_OP_ADD_UNICODE, (left, right -- res)) {
329-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
330-
assert(PyUnicode_CheckExact(sym_get_const(ctx, left)));
331-
assert(PyUnicode_CheckExact(sym_get_const(ctx, right)));
332-
PyObject *temp = PyUnicode_Concat(sym_get_const(ctx, left), sym_get_const(ctx, right));
337+
PyObject *lhs = sym_get_const(ctx, left);
338+
PyObject *rhs = sym_get_const(ctx, right);
339+
if (lhs && rhs) {
340+
assert(PyUnicode_CheckExact(lhs));
341+
assert(PyUnicode_CheckExact(rhs));
342+
PyObject *temp = PyUnicode_Concat(lhs, rhs);
333343
if (temp == NULL) {
334344
goto error;
335345
}
@@ -343,10 +353,12 @@ dummy_func(void) {
343353

344354
op(_BINARY_OP_INPLACE_ADD_UNICODE, (left, right -- )) {
345355
JitOptSymbol *res;
346-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
347-
assert(PyUnicode_CheckExact(sym_get_const(ctx, left)));
348-
assert(PyUnicode_CheckExact(sym_get_const(ctx, right)));
349-
PyObject *temp = PyUnicode_Concat(sym_get_const(ctx, left), sym_get_const(ctx, right));
356+
PyObject *lhs = sym_get_const(ctx, left);
357+
PyObject *rhs = sym_get_const(ctx, right);
358+
if (lhs && rhs) {
359+
assert(PyUnicode_CheckExact(lhs));
360+
assert(PyUnicode_CheckExact(rhs));
361+
PyObject *temp = PyUnicode_Concat(lhs, rhs);
350362
if (temp == NULL) {
351363
goto error;
352364
}
@@ -370,10 +382,11 @@ dummy_func(void) {
370382
}
371383

372384
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) {
385+
PyObject *sub_st_o = sym_get_const(ctx, sub_st);
373386
assert(sym_matches_type(tuple_st, &PyTuple_Type));
374-
if (sym_is_const(ctx, sub_st)) {
375-
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
376-
long index = PyLong_AsLong(sym_get_const(ctx, sub_st));
387+
if (sub_st_o) {
388+
assert(PyLong_CheckExact(sub_st_o));
389+
long index = PyLong_AsLong(sub_st_o);
377390
assert(index >= 0);
378391
int tuple_length = sym_tuple_length(tuple_st);
379392
if (tuple_length == -1) {
@@ -464,12 +477,12 @@ dummy_func(void) {
464477
}
465478

466479
op(_COMPARE_OP_INT, (left, right -- res)) {
467-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
468-
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
469-
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
470-
PyObject *tmp = PyObject_RichCompare(sym_get_const(ctx, left),
471-
sym_get_const(ctx, right),
472-
oparg >> 5);
480+
PyObject *lhs = sym_get_const(ctx, left);
481+
PyObject *rhs = sym_get_const(ctx, right);
482+
if (lhs && rhs) {
483+
assert(PyLong_CheckExact(lhs));
484+
assert(PyLong_CheckExact(rhs));
485+
PyObject *tmp = PyObject_RichCompare(lhs, rhs, oparg >> 5);
473486
if (tmp == NULL) {
474487
goto error;
475488
}
@@ -570,8 +583,8 @@ dummy_func(void) {
570583
(void)dict_version;
571584
(void)index;
572585
attr = NULL;
573-
if (sym_is_const(ctx, owner)) {
574-
PyModuleObject *mod = (PyModuleObject *)sym_get_const(ctx, owner);
586+
PyModuleObject *mod = (PyModuleObject *)sym_get_const(ctx, owner);
587+
if (mod) {
575588
if (PyModule_CheckExact(mod)) {
576589
PyObject *dict = mod->md_dict;
577590
uint64_t watched_mutations = get_mutations(dict);
@@ -652,19 +665,21 @@ dummy_func(void) {
652665
}
653666

654667
op(_CHECK_FUNCTION_VERSION, (func_version/2, callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
655-
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyFunction_Type)) {
656-
assert(PyFunction_Check(sym_get_const(ctx, callable)));
668+
PyObject *callable_o = sym_get_const(ctx, callable);
669+
if (callable_o && sym_matches_type(callable, &PyFunction_Type)) {
670+
assert(PyFunction_Check(callable_o));
657671
REPLACE_OP(this_instr, _CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
658-
this_instr->operand1 = (uintptr_t)sym_get_const(ctx, callable);
672+
this_instr->operand1 = (uintptr_t)callable_o;
659673
}
660674
sym_set_type(callable, &PyFunction_Type);
661675
}
662676

663677
op(_CHECK_FUNCTION_EXACT_ARGS, (callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
664678
assert(sym_matches_type(callable, &PyFunction_Type));
665-
if (sym_is_const(ctx, callable)) {
679+
PyObject *callable_o = sym_get_const(ctx, callable);
680+
if (callable_o) {
666681
if (sym_is_null(self_or_null) || sym_is_not_null(self_or_null)) {
667-
PyFunctionObject *func = (PyFunctionObject *)sym_get_const(ctx, callable);
682+
PyFunctionObject *func = (PyFunctionObject *)callable_o;
668683
PyCodeObject *co = (PyCodeObject *)func->func_code;
669684
if (co->co_argcount == oparg + !sym_is_null(self_or_null)) {
670685
REPLACE_OP(this_instr, _NOP, 0 ,0);
@@ -891,26 +906,26 @@ dummy_func(void) {
891906
}
892907

893908
op(_GUARD_IS_TRUE_POP, (flag -- )) {
894-
if (sym_is_const(ctx, flag)) {
895-
PyObject *value = sym_get_const(ctx, flag);
909+
PyObject *value = sym_get_const(ctx, flag);
910+
if (value) {
896911
assert(value != NULL);
897912
eliminate_pop_guard(this_instr, value != Py_True);
898913
}
899914
sym_set_const(flag, Py_True);
900915
}
901916

902917
op(_GUARD_IS_FALSE_POP, (flag -- )) {
903-
if (sym_is_const(ctx, flag)) {
904-
PyObject *value = sym_get_const(ctx, flag);
918+
PyObject *value = sym_get_const(ctx, flag);
919+
if (value) {
905920
assert(value != NULL);
906921
eliminate_pop_guard(this_instr, value != Py_False);
907922
}
908923
sym_set_const(flag, Py_False);
909924
}
910925

911926
op(_GUARD_IS_NONE_POP, (val -- )) {
912-
if (sym_is_const(ctx, val)) {
913-
PyObject *value = sym_get_const(ctx, val);
927+
PyObject *value = sym_get_const(ctx, val);
928+
if (value) {
914929
assert(value != NULL);
915930
eliminate_pop_guard(this_instr, !Py_IsNone(value));
916931
}
@@ -922,8 +937,8 @@ dummy_func(void) {
922937
}
923938

924939
op(_GUARD_IS_NOT_NONE_POP, (val -- )) {
925-
if (sym_is_const(ctx, val)) {
926-
PyObject *value = sym_get_const(ctx, val);
940+
PyObject *value = sym_get_const(ctx, val);
941+
if (value) {
927942
assert(value != NULL);
928943
eliminate_pop_guard(this_instr, Py_IsNone(value));
929944
}

0 commit comments

Comments
 (0)