-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
GH-128939: Refactor JIT optimize structs #128940
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
Changes from 5 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,72 @@ 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 = 0, | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| typedef struct _jit_opt_tuple { | ||||||||||||||||||||||||||||||||||
| uint8_t tag; | ||||||||||||||||||||||||||||||||||
| uint8_t length; | ||||||||||||||||||||||||||||||||||
| uint16_t items[6]; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| uint16_t items[6]; | |
| uint16_t items[7]; |
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.
Could this be:
| typedef union _jit_opt_symbol { | |
| uint8_t tag; | |
| JitOptKnownClass cls; | |
| JitOptKnownValue value; | |
| JitOptKnownVersion version; | |
| JitOptTuple tuple; | |
| } JitOptSymbol; | |
| typedef struct { | |
| uint8_t tag; | |
| union { | |
| JitOptKnownClass cls; | |
| JitOptKnownValue value; | |
| JitOptKnownVersion version; | |
| JitOptTuple tuple; | |
| }; | |
| } JitOptSymbol; |
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 comment
The 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.
| 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_tagis the version tag of the value's class now.sym->version.versionis the version tag it would have should execution reach this point in the code. Not quite the same thing.Uh oh!
There was an error while loading. Please reload this page.
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.versionthat's out-of-sync withsym->cls.type->tp_version_tagshould result in the bottom type, then, right? It's not possible in future code, since the cached tag is stale.