gh-143414: Implement unique reference tracking for JIT, optimize unpacking of such tuples (GH-144300)

Co-authored-by: Ken Jin <kenjin4096@gmail.com>
This commit is contained in:
reiden 2026-03-23 00:57:23 +08:00 committed by GitHub
parent 1ceb1fb284
commit e36f8db7e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 1414 additions and 911 deletions

View file

@ -93,7 +93,19 @@ dummy_func(void) {
if (sym_is_immortal(PyJitRef_Unwrap(value))) {
ADD_OP(_NOP, 0, 0);
}
value = PyJitRef_StripReferenceInfo(value);
value = PyJitRef_StripBorrowInfo(value);
}
op(_COPY_FREE_VARS, (--)) {
PyCodeObject *co = get_current_code_object(ctx);
if (co == NULL) {
ctx->done = true;
break;
}
int offset = co->co_nlocalsplus - oparg;
for (int i = 0; i < oparg; ++i) {
ctx->frame->locals[offset + i] = sym_new_not_null(ctx);
}
}
op(_LOAD_FAST_CHECK, (-- value)) {
@ -102,20 +114,24 @@ dummy_func(void) {
if (sym_is_null(value)) {
ctx->done = true;
}
assert(!PyJitRef_IsUnique(value));
}
op(_LOAD_FAST, (-- value)) {
value = GETLOCAL(oparg);
assert(!PyJitRef_IsUnique(value));
}
op(_LOAD_FAST_BORROW, (-- value)) {
value = PyJitRef_Borrow(GETLOCAL(oparg));
assert(!PyJitRef_IsUnique(value));
}
op(_LOAD_FAST_AND_CLEAR, (-- value)) {
value = GETLOCAL(oparg);
JitOptRef temp = sym_new_null(ctx);
GETLOCAL(oparg) = temp;
assert(!PyJitRef_IsUnique(value));
}
op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner -- o)) {
@ -132,7 +148,7 @@ dummy_func(void) {
op(_SWAP_FAST, (value -- trash)) {
JitOptRef tmp = GETLOCAL(oparg);
GETLOCAL(oparg) = value;
GETLOCAL(oparg) = PyJitRef_RemoveUnique(value);
trash = tmp;
}
@ -713,6 +729,7 @@ dummy_func(void) {
op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
assert(oparg > 0);
bottom = PyJitRef_RemoveUnique(bottom);
top = bottom;
}
@ -1311,6 +1328,7 @@ dummy_func(void) {
op(_BUILD_TUPLE, (values[oparg] -- tup)) {
tup = sym_new_tuple(ctx, oparg, values);
tup = PyJitRef_MakeUnique(tup);
}
op(_BUILD_LIST, (values[oparg] -- list)) {
@ -1339,11 +1357,20 @@ dummy_func(void) {
}
op(_UNPACK_SEQUENCE_TWO_TUPLE, (seq -- val1, val0)) {
if (PyJitRef_IsUnique(seq) && sym_tuple_length(seq) == 2) {
ADD_OP(_UNPACK_SEQUENCE_UNIQUE_TWO_TUPLE, oparg, 0);
}
val0 = sym_tuple_getitem(ctx, seq, 0);
val1 = sym_tuple_getitem(ctx, seq, 1);
}
op(_UNPACK_SEQUENCE_TUPLE, (seq -- values[oparg])) {
if (PyJitRef_IsUnique(seq) && sym_tuple_length(seq) == 3) {
ADD_OP(_UNPACK_SEQUENCE_UNIQUE_THREE_TUPLE, oparg, 0);
}
else if (PyJitRef_IsUnique(seq) && sym_tuple_length(seq) == oparg) {
ADD_OP(_UNPACK_SEQUENCE_UNIQUE_TUPLE, oparg, 0);
}
for (int i = 0; i < oparg; i++) {
values[i] = sym_tuple_getitem(ctx, seq, oparg - i - 1);
}