Skip to content

gh-114058: Only allow immortal constants in tier 2 #115817

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ abstractcontext_fini(_Py_UOpsAbstractInterpContext *ctx)
return;
}
ctx->curr_frame_depth = 0;
int tys = ctx->t_arena.ty_curr_number;
for (int i = 0; i < tys; i++) {
Py_CLEAR(ctx->t_arena.arena[i].const_val);
}
}

static int
Expand Down Expand Up @@ -218,7 +214,8 @@ ctx_frame_pop(
}


// Takes a borrowed reference to const_val, turns that into a strong reference.
// Only accepts immortal constants or borrowed constants that will be kept alive
// elsewhere.
static _Py_UOpsSymType*
sym_new(_Py_UOpsAbstractInterpContext *ctx,
PyObject *const_val)
Expand All @@ -230,13 +227,9 @@ sym_new(_Py_UOpsAbstractInterpContext *ctx,
return NULL;
}
ctx->t_arena.ty_curr_number++;
self->const_val = NULL;
self->typ = NULL;
self->flags = 0;

if (const_val != NULL) {
self->const_val = Py_NewRef(const_val);
}
self->const_val = const_val;

return self;
}
Expand Down Expand Up @@ -317,7 +310,7 @@ sym_new_known_type(_Py_UOpsAbstractInterpContext *ctx,
return res;
}

// Takes a borrowed reference to const_val.
// Decrefs const_val if it is not immortal and discards it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an interface that is hard to use. Let the caller worry about mortality.
This also contradicts the comment on sym_new that it is OK to pass in mortal objects as long as the caller retains a strong reference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way is more complex to handle error conditions.
E.g
For newly created objects, out of space means we should decref it, but the code isn't as clean as

OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));

but rather becomes

if (res = sym_new_const(ctx, temp) == NULL) {
    Py_DECREF(temp);
    goto out_of_space;
}

Then we need to repeat that on every call site.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either temp is immortal, in which case the caller need do nothing, or it is mortal in which case it needs proper handling.

Correctness is paramount here, as bugs are going to be very hard to reproduce. Reproducing bugs in the T1 specializer is hard enough. We need to extra careful here. A bit of verbosity is a small price to pay for being able to reason about the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed code here would totally invalidate _BINARY_OP_ADD_FLOAT and other float-producing uops (as well as int-producing uops if the value is not a cached short int -- which is a distinction easily overlooked when testing).

static inline _Py_UOpsSymType*
sym_new_const(_Py_UOpsAbstractInterpContext *ctx, PyObject *const_val)
{
Expand All @@ -330,9 +323,36 @@ sym_new_const(_Py_UOpsAbstractInterpContext *ctx, PyObject *const_val)
return NULL;
}
sym_set_type(temp, Py_TYPE(const_val));
sym_set_flag(temp, TRUE_CONST);
sym_set_flag(temp, KNOWN);
sym_set_flag(temp, NOT_NULL);
// If the constant is not immortal, discard it.
if (_Py_IsImmortal(const_val)) {
sym_set_flag(temp, TRUE_CONST);
}
else {
Py_DECREF(const_val);
temp->const_val = NULL;
}
return temp;
}

// Same as sym_new_const, but borrows const_val, assuming it will be kept alive.
// Only safe if we know const_val has a strong reference elsewhere.
static inline _Py_UOpsSymType*
sym_new_const_borrow(_Py_UOpsAbstractInterpContext *ctx, PyObject *const_val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should scrap this, and leave it to the caller to handle lifetimes.

{
assert(const_val != NULL);
_Py_UOpsSymType *temp = sym_new(
ctx,
const_val
);
if (temp == NULL) {
return NULL;
}
sym_set_type(temp, Py_TYPE(const_val));
sym_set_flag(temp, KNOWN);
sym_set_flag(temp, NOT_NULL);
sym_set_flag(temp, TRUE_CONST);
return temp;
}

Expand Down
4 changes: 2 additions & 2 deletions Python/tier2_redundancy_eliminator_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ dummy_func(void) {
}

op(_LOAD_CONST_INLINE, (ptr/4 -- value)) {
OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr));
OUT_OF_SPACE_IF_NULL(value = sym_new_const_borrow(ctx, ptr));
}

op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) {
OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr));
}

op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) {
OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr));
OUT_OF_SPACE_IF_NULL(value = sym_new_const_borrow(ctx, ptr));
OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx));
}

Expand Down
4 changes: 2 additions & 2 deletions Python/tier2_redundancy_eliminator_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.