Skip to content

Commit e2a3e4b

Browse files
authored
gh-115816: Improve internal symbols API in optimizer (#116028)
- Any `sym_set_...` call that attempts to set conflicting information cause the symbol to become `bottom` (contradiction). - All `sym_is...` and similar calls return false or NULL for `bottom`. - Everything's tested. - The tests still pass with `PYTHONUOPSOPTIMIZE=1`.
1 parent f58f8ce commit e2a3e4b

File tree

4 files changed

+191
-67
lines changed

4 files changed

+191
-67
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ extern PyTypeObject _PyUOpExecutor_Type;
2727
extern PyTypeObject _PyUOpOptimizer_Type;
2828

2929
/* Symbols */
30+
/* See explanation in optimizer_symbols.c */
3031

3132
struct _Py_UopsSymbol {
32-
int flags;
33-
PyTypeObject *typ;
34-
// constant propagated value (might be NULL)
35-
PyObject *const_val;
33+
int flags; // 0 bits: Top; 2 or more bits: Bottom
34+
PyTypeObject *typ; // Borrowed reference
35+
PyObject *const_val; // Owned reference (!)
3636
};
3737

3838
// Holds locals, stack, locals, stack ... co_consts (in that order)
@@ -92,7 +92,9 @@ extern _Py_UopsSymbol *_Py_uop_sym_new_const(_Py_UOpsContext *ctx, PyObject *con
9292
extern _Py_UopsSymbol *_Py_uop_sym_new_null(_Py_UOpsContext *ctx);
9393
extern bool _Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ);
9494
extern void _Py_uop_sym_set_null(_Py_UopsSymbol *sym);
95-
extern void _Py_uop_sym_set_type(_Py_UopsSymbol *sym, PyTypeObject *tp);
95+
extern void _Py_uop_sym_set_non_null(_Py_UopsSymbol *sym);
96+
extern void _Py_uop_sym_set_type(_Py_UopsSymbol *sym, PyTypeObject *typ);
97+
extern void _Py_uop_sym_set_const(_Py_UopsSymbol *sym, PyObject *const_val);
9698

9799
extern int _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx);
98100
extern void _Py_uop_abstractcontext_fini(_Py_UOpsContext *ctx);

Python/optimizer_analysis.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
294294
#define sym_new_null _Py_uop_sym_new_null
295295
#define sym_matches_type _Py_uop_sym_matches_type
296296
#define sym_set_null _Py_uop_sym_set_null
297+
#define sym_set_non_null _Py_uop_sym_set_non_null
297298
#define sym_set_type _Py_uop_sym_set_type
299+
#define sym_set_const _Py_uop_sym_set_const
298300
#define frame_new _Py_uop_frame_new
299301
#define frame_pop _Py_uop_frame_pop
300302

Python/optimizer_bytecodes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
2222
#define sym_new_null _Py_uop_sym_new_null
2323
#define sym_matches_type _Py_uop_sym_matches_type
2424
#define sym_set_null _Py_uop_sym_set_null
25+
#define sym_set_non_null _Py_uop_sym_set_non_null
2526
#define sym_set_type _Py_uop_sym_set_type
27+
#define sym_set_const _Py_uop_sym_set_const
2628
#define frame_new _Py_uop_frame_new
2729
#define frame_pop _Py_uop_frame_pop
2830

0 commit comments

Comments
 (0)