-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-128939: Refactor JIT optimize structs #128940
Changes from all commits
219db3d
e82764d
50803e7
85da4d5
307ba6c
56149ae
8b19379
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -148,15 +148,6 @@ extern PyTypeObject _PyDefaultOptimizer_Type; | |||||||||||||||||||||||||||||||||
extern PyTypeObject _PyUOpExecutor_Type; | ||||||||||||||||||||||||||||||||||
extern PyTypeObject _PyUOpOptimizer_Type; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/* Symbols */ | ||||||||||||||||||||||||||||||||||
/* See explanation in optimizer_symbols.c */ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
struct _Py_UopsSymbol { | ||||||||||||||||||||||||||||||||||
int flags; // 0 bits: Top; 2 or more bits: Bottom | ||||||||||||||||||||||||||||||||||
PyTypeObject *typ; // Borrowed reference | ||||||||||||||||||||||||||||||||||
PyObject *const_val; // Owned reference (!) | ||||||||||||||||||||||||||||||||||
unsigned int type_version; // currently stores type version | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
#define UOP_FORMAT_TARGET 0 | ||||||||||||||||||||||||||||||||||
#define UOP_FORMAT_JUMP 1 | ||||||||||||||||||||||||||||||||||
|
@@ -193,27 +184,74 @@ static inline uint16_t uop_get_error_target(const _PyUOpInstruction *inst) | |||||||||||||||||||||||||||||||||
// handle before rejoining the rest of the program. | ||||||||||||||||||||||||||||||||||
#define MAX_CHAIN_DEPTH 4 | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct _Py_UopsSymbol _Py_UopsSymbol; | ||||||||||||||||||||||||||||||||||
/* Symbols */ | ||||||||||||||||||||||||||||||||||
/* See explanation in optimizer_symbols.c */ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef enum _JitSymType { | ||||||||||||||||||||||||||||||||||
JIT_SYM_UNKNOWN_TAG = 1, | ||||||||||||||||||||||||||||||||||
JIT_SYM_NULL_TAG = 2, | ||||||||||||||||||||||||||||||||||
JIT_SYM_NON_NULL_TAG = 3, | ||||||||||||||||||||||||||||||||||
JIT_SYM_BOTTOM_TAG = 4, | ||||||||||||||||||||||||||||||||||
JIT_SYM_TYPE_VERSION_TAG = 5, | ||||||||||||||||||||||||||||||||||
JIT_SYM_KNOWN_CLASS_TAG = 6, | ||||||||||||||||||||||||||||||||||
JIT_SYM_KNOWN_VALUE_TAG = 7, | ||||||||||||||||||||||||||||||||||
JIT_SYM_TUPLE_TAG = 8, | ||||||||||||||||||||||||||||||||||
} JitSymType; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct _jit_opt_known_class { | ||||||||||||||||||||||||||||||||||
uint8_t tag; | ||||||||||||||||||||||||||||||||||
uint32_t version; | ||||||||||||||||||||||||||||||||||
PyTypeObject *type; | ||||||||||||||||||||||||||||||||||
} JitOptKnownClass; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct _jit_opt_known_version { | ||||||||||||||||||||||||||||||||||
uint8_t tag; | ||||||||||||||||||||||||||||||||||
uint32_t version; | ||||||||||||||||||||||||||||||||||
} JitOptKnownVersion; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct _jit_opt_known_value { | ||||||||||||||||||||||||||||||||||
uint8_t tag; | ||||||||||||||||||||||||||||||||||
PyObject *value; | ||||||||||||||||||||||||||||||||||
} JitOptKnownValue; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
#define MAX_SYMBOLIC_TUPLE_SIZE 7 | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct _jit_opt_tuple { | ||||||||||||||||||||||||||||||||||
uint8_t tag; | ||||||||||||||||||||||||||||||||||
uint8_t length; | ||||||||||||||||||||||||||||||||||
uint16_t items[MAX_SYMBOLIC_TUPLE_SIZE]; | ||||||||||||||||||||||||||||||||||
} JitOptTuple; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef union _jit_opt_symbol { | ||||||||||||||||||||||||||||||||||
uint8_t tag; | ||||||||||||||||||||||||||||||||||
JitOptKnownClass cls; | ||||||||||||||||||||||||||||||||||
JitOptKnownValue value; | ||||||||||||||||||||||||||||||||||
JitOptKnownVersion version; | ||||||||||||||||||||||||||||||||||
JitOptTuple tuple; | ||||||||||||||||||||||||||||||||||
} JitOptSymbol; | ||||||||||||||||||||||||||||||||||
Comment on lines
+226
to
+232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be:
Suggested change
Then we wouldn't need to repeat the tag in each struct. Or does the current scheme potentially pack better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It packs better. Because structs align according to the alignment of their members, we would end up using 8 bytes for the tag if it weren't part of each struct. |
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
struct _Py_UOpsAbstractFrame { | ||||||||||||||||||||||||||||||||||
// Max stacklen | ||||||||||||||||||||||||||||||||||
int stack_len; | ||||||||||||||||||||||||||||||||||
int locals_len; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
_Py_UopsSymbol **stack_pointer; | ||||||||||||||||||||||||||||||||||
_Py_UopsSymbol **stack; | ||||||||||||||||||||||||||||||||||
_Py_UopsSymbol **locals; | ||||||||||||||||||||||||||||||||||
JitOptSymbol **stack_pointer; | ||||||||||||||||||||||||||||||||||
JitOptSymbol **stack; | ||||||||||||||||||||||||||||||||||
JitOptSymbol **locals; | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct ty_arena { | ||||||||||||||||||||||||||||||||||
int ty_curr_number; | ||||||||||||||||||||||||||||||||||
int ty_max_number; | ||||||||||||||||||||||||||||||||||
_Py_UopsSymbol arena[TY_ARENA_SIZE]; | ||||||||||||||||||||||||||||||||||
JitOptSymbol arena[TY_ARENA_SIZE]; | ||||||||||||||||||||||||||||||||||
} ty_arena; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
struct _Py_UOpsContext { | ||||||||||||||||||||||||||||||||||
typedef struct _JitOptContext { | ||||||||||||||||||||||||||||||||||
char done; | ||||||||||||||||||||||||||||||||||
char out_of_space; | ||||||||||||||||||||||||||||||||||
bool contradiction; | ||||||||||||||||||||||||||||||||||
|
@@ -225,46 +263,47 @@ struct _Py_UOpsContext { | |||||||||||||||||||||||||||||||||
// Arena for the symbolic types. | ||||||||||||||||||||||||||||||||||
ty_arena t_arena; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
_Py_UopsSymbol **n_consumed; | ||||||||||||||||||||||||||||||||||
_Py_UopsSymbol **limit; | ||||||||||||||||||||||||||||||||||
_Py_UopsSymbol *locals_and_stack[MAX_ABSTRACT_INTERP_SIZE]; | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
typedef struct _Py_UOpsContext _Py_UOpsContext; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_null(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_not_null(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_const(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern PyObject *_Py_uop_sym_get_const(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern _Py_UopsSymbol *_Py_uop_sym_new_unknown(_Py_UOpsContext *ctx); | ||||||||||||||||||||||||||||||||||
extern _Py_UopsSymbol *_Py_uop_sym_new_not_null(_Py_UOpsContext *ctx); | ||||||||||||||||||||||||||||||||||
extern _Py_UopsSymbol *_Py_uop_sym_new_type( | ||||||||||||||||||||||||||||||||||
_Py_UOpsContext *ctx, PyTypeObject *typ); | ||||||||||||||||||||||||||||||||||
extern _Py_UopsSymbol *_Py_uop_sym_new_const(_Py_UOpsContext *ctx, PyObject *const_val); | ||||||||||||||||||||||||||||||||||
extern _Py_UopsSymbol *_Py_uop_sym_new_null(_Py_UOpsContext *ctx); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_has_type(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_matches_type_version(_Py_UopsSymbol *sym, unsigned int version); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_non_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_type(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyTypeObject *typ); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_set_type_version(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, unsigned int version); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_const(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyObject *const_val); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_bottom(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern int _Py_uop_sym_truthiness(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern PyTypeObject *_Py_uop_sym_get_type(_Py_UopsSymbol *sym); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
extern void _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_abstractcontext_fini(_Py_UOpsContext *ctx); | ||||||||||||||||||||||||||||||||||
JitOptSymbol **n_consumed; | ||||||||||||||||||||||||||||||||||
JitOptSymbol **limit; | ||||||||||||||||||||||||||||||||||
JitOptSymbol *locals_and_stack[MAX_ABSTRACT_INTERP_SIZE]; | ||||||||||||||||||||||||||||||||||
} JitOptContext; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_null(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_not_null(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_const(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern PyObject *_Py_uop_sym_get_const(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern JitOptSymbol *_Py_uop_sym_new_unknown(JitOptContext *ctx); | ||||||||||||||||||||||||||||||||||
extern JitOptSymbol *_Py_uop_sym_new_not_null(JitOptContext *ctx); | ||||||||||||||||||||||||||||||||||
extern JitOptSymbol *_Py_uop_sym_new_type( | ||||||||||||||||||||||||||||||||||
JitOptContext *ctx, PyTypeObject *typ); | ||||||||||||||||||||||||||||||||||
extern JitOptSymbol *_Py_uop_sym_new_const(JitOptContext *ctx, PyObject *const_val); | ||||||||||||||||||||||||||||||||||
extern JitOptSymbol *_Py_uop_sym_new_null(JitOptContext *ctx); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_has_type(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_matches_type(JitOptSymbol *sym, PyTypeObject *typ); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_matches_type_version(JitOptSymbol *sym, unsigned int version); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_null(JitOptContext *ctx, JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_non_null(JitOptContext *ctx, JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_type(JitOptContext *ctx, JitOptSymbol *sym, PyTypeObject *typ); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_set_type_version(JitOptContext *ctx, JitOptSymbol *sym, unsigned int version); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_sym_set_const(JitOptContext *ctx, JitOptSymbol *sym, PyObject *const_val); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_bottom(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern int _Py_uop_sym_truthiness(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern PyTypeObject *_Py_uop_sym_get_type(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern bool _Py_uop_sym_is_immortal(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
extern JitOptSymbol *_Py_uop_sym_new_tuple(JitOptContext *ctx, int size, JitOptSymbol **args); | ||||||||||||||||||||||||||||||||||
extern JitOptSymbol *_Py_uop_sym_tuple_getitem(JitOptContext *ctx, JitOptSymbol *sym, int item); | ||||||||||||||||||||||||||||||||||
extern int _Py_uop_sym_tuple_length(JitOptSymbol *sym); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
extern void _Py_uop_abstractcontext_init(JitOptContext *ctx); | ||||||||||||||||||||||||||||||||||
extern void _Py_uop_abstractcontext_fini(JitOptContext *ctx); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
extern _Py_UOpsAbstractFrame *_Py_uop_frame_new( | ||||||||||||||||||||||||||||||||||
_Py_UOpsContext *ctx, | ||||||||||||||||||||||||||||||||||
JitOptContext *ctx, | ||||||||||||||||||||||||||||||||||
PyCodeObject *co, | ||||||||||||||||||||||||||||||||||
int curr_stackentries, | ||||||||||||||||||||||||||||||||||
_Py_UopsSymbol **args, | ||||||||||||||||||||||||||||||||||
JitOptSymbol **args, | ||||||||||||||||||||||||||||||||||
int arg_len); | ||||||||||||||||||||||||||||||||||
extern int _Py_uop_frame_pop(_Py_UOpsContext *ctx); | ||||||||||||||||||||||||||||||||||
extern int _Py_uop_frame_pop(JitOptContext *ctx); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
PyAPI_FUNC(PyObject *) _Py_uop_symbols_test(PyObject *self, PyObject *ignored); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1465,6 +1465,25 @@ def f(l: complex, r: complex) -> None: | |
with self.subTest(l=l, r=r, x=x, y=y): | ||
script_helper.assert_python_ok("-c", s) | ||
|
||
def test_symbols_flow_through_tuples(self): | ||
def testfunc(n): | ||
for _ in range(n): | ||
a = 1 | ||
b = 2 | ||
t = a, b | ||
x, y = t | ||
r = x + y | ||
return r | ||
|
||
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
self.assertEqual(res, 3) | ||
self.assertIsNotNone(ex) | ||
uops = get_opnames(ex) | ||
self.assertIn("_BINARY_OP_ADD_INT", uops) | ||
self.assertNotIn("_GUARD_BOTH_INT", uops) | ||
self.assertNotIn("_GUARD_NOS_INT", uops) | ||
self.assertNotIn("_GUARD_TOS_INT", uops) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This kind of check can easily pass by accident if there's typo or bytecodes change. Would be good to have a helper that asserts also that the thing not in is an actual uop id. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would. |
||
|
||
def test_decref_escapes(self): | ||
class Convert9999ToNone: | ||
def __del__(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably just get the version by doing
cls.type->tp_version_tag
, right? Having both here just creates opportunities for them to be stale or out-of-sync, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sym->cls.type->tp_version_tag
is the version tag of the value's class now.sym->version.version
is the version tag it would have should execution reach this point in the code. Not quite the same thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would imagine that a
sym->cls.version
that's out-of-sync withsym->cls.type->tp_version_tag
should result in the bottom type, then, right? It's not possible in future code, since the cached tag is stale.