-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Fidget-Spinner
wants to merge
5
commits into
python:main
Choose a base branch
from
Fidget-Spinner:fix_redundancy_refleaks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
05a604f
Only allow immortal constants in tier 2
Fidget-Spinner 38ee21c
Allow borrowed ref for _LOAD_CONST_INLINE
Fidget-Spinner c75030d
update comments
Fidget-Spinner 3ff15c7
make flags simpler
Fidget-Spinner c447000
Merge remote-tracking branch 'upstream/main' into fix_redundancy_refl…
Fidget-Spinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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; | ||
} | ||
|
@@ -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. | ||
static inline _Py_UOpsSymType* | ||
sym_new_const(_Py_UOpsAbstractInterpContext *ctx, PyObject *const_val) | ||
{ | ||
|
@@ -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) | ||
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. 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); | ||
Fidget-Spinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sym_set_flag(temp, NOT_NULL); | ||
sym_set_flag(temp, TRUE_CONST); | ||
return temp; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.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.
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
but rather becomes
Then we need to repeat that on every call site.
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.
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.
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.
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).