Skip to content

Commit 476535e

Browse files
JIT: Strip references when roundtripping through str or tuple
1 parent 1ff2cbb commit 476535e

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ PyJitRef_Wrap(JitOptSymbol *sym)
251251
return (JitOptRef){.bits=(uintptr_t)sym};
252252
}
253253

254+
static inline JitOptRef
255+
PyJitRef_StripReferenceInfo(JitOptRef ref)
256+
{
257+
return PyJitRef_Wrap(PyJitRef_Unwrap(ref));
258+
}
259+
254260
static inline JitOptRef
255261
PyJitRef_Borrow(JitOptRef ref)
256262
{

Python/optimizer_bytecodes.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,8 @@ dummy_func(void) {
762762
}
763763

764764
op(_RETURN_VALUE, (retval -- res)) {
765-
// We wrap and unwrap the value to mimic PyStackRef_MakeHeapSafe
766-
// in bytecodes.c
767-
JitOptRef temp = PyJitRef_Wrap(PyJitRef_Unwrap(retval));
765+
// Mimics PyStackRef_MakeHeapSafe in the interpreter.
766+
JitOptRef temp = PyJitRef_StripReferenceInfo(retval);
768767
DEAD(retval);
769768
SAVE_STACK();
770769
ctx->frame->stack_pointer = stack_pointer;
@@ -925,7 +924,9 @@ dummy_func(void) {
925924
op(_CALL_STR_1, (unused, unused, arg -- res)) {
926925
if (sym_matches_type(arg, &PyUnicode_Type)) {
927926
// e.g. str('foo') or str(foo) where foo is known to be a string
928-
res = arg;
927+
// Note: we must strip the reference information because it goes
928+
// through str() which strips the reference information from it.
929+
res = PyJitRef_StripReferenceInfo(arg);
929930
}
930931
else {
931932
res = sym_new_type(ctx, &PyUnicode_Type);
@@ -1065,7 +1066,9 @@ dummy_func(void) {
10651066
op(_CALL_TUPLE_1, (callable, null, arg -- res)) {
10661067
if (sym_matches_type(arg, &PyTuple_Type)) {
10671068
// e.g. tuple((1, 2)) or tuple(foo) where foo is known to be a tuple
1068-
res = arg;
1069+
// Note: we must strip the reference information because it goes
1070+
// through tuple() which strips the reference information from it.
1071+
res = PyJitRef_StripReferenceInfo(arg);
10691072
}
10701073
else {
10711074
res = sym_new_type(ctx, &PyTuple_Type);

Python/optimizer_cases.c.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)