-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
GH-130415: Improve the JIT's unneeded uop removal pass #132333
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 all commits
edb68e1
aada255
e0ad739
97dbcce
9b64c17
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Improve the JIT's ability to remove unused constant and local variable | ||
loads, and fix an issue where deallocating unused values could cause JIT | ||
code to crash or behave incorrectly. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -555,28 +555,47 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size) | |
} | ||
break; | ||
case _POP_TOP: | ||
case _POP_TOP_LOAD_CONST_INLINE: | ||
case _POP_TOP_LOAD_CONST_INLINE_BORROW: | ||
case _POP_TWO_LOAD_CONST_INLINE_BORROW: | ||
optimize_pop_top_again: | ||
{ | ||
_PyUOpInstruction *last = &buffer[pc-1]; | ||
while (last->opcode == _NOP) { | ||
last--; | ||
} | ||
if (last->opcode == _LOAD_CONST_INLINE || | ||
last->opcode == _LOAD_CONST_INLINE_BORROW || | ||
last->opcode == _LOAD_FAST || | ||
last->opcode == _LOAD_FAST_BORROW || | ||
last->opcode == _COPY | ||
) { | ||
last->opcode = _NOP; | ||
buffer[pc].opcode = _NOP; | ||
} | ||
if (last->opcode == _REPLACE_WITH_TRUE) { | ||
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. Are we not optimizing this any more? I would have thought it would go in the same case as 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. Nice catch. Though these should probably just be turned into I'll leave it this way and open up another PR for that. Having the hardcoded constant makes things more difficult, since we'd need to start writing operands instead of operating purely on opcodes. 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. Actually, I just added that change as part of this PR. |
||
last->opcode = _NOP; | ||
switch (last->opcode) { | ||
case _POP_TWO_LOAD_CONST_INLINE_BORROW: | ||
last->opcode = _POP_TOP; | ||
break; | ||
case _POP_TOP_LOAD_CONST_INLINE: | ||
case _POP_TOP_LOAD_CONST_INLINE_BORROW: | ||
last->opcode = _NOP; | ||
goto optimize_pop_top_again; | ||
case _COPY: | ||
case _LOAD_CONST_INLINE: | ||
case _LOAD_CONST_INLINE_BORROW: | ||
case _LOAD_FAST: | ||
case _LOAD_FAST_BORROW: | ||
case _LOAD_SMALL_INT: | ||
last->opcode = _NOP; | ||
if (opcode == _POP_TOP) { | ||
opcode = buffer[pc].opcode = _NOP; | ||
} | ||
else if (opcode == _POP_TOP_LOAD_CONST_INLINE) { | ||
opcode = buffer[pc].opcode = _LOAD_CONST_INLINE; | ||
} | ||
else if (opcode == _POP_TOP_LOAD_CONST_INLINE_BORROW) { | ||
opcode = buffer[pc].opcode = _LOAD_CONST_INLINE_BORROW; | ||
} | ||
else { | ||
assert(opcode == _POP_TWO_LOAD_CONST_INLINE_BORROW); | ||
opcode = buffer[pc].opcode = _POP_TOP_LOAD_CONST_INLINE_BORROW; | ||
goto optimize_pop_top_again; | ||
} | ||
} | ||
break; | ||
_Py_FALLTHROUGH; | ||
} | ||
case _JUMP_TO_TOP: | ||
case _EXIT_TRACE: | ||
return pc + 1; | ||
default: | ||
{ | ||
/* _PUSH_FRAME doesn't escape or error, but it | ||
|
@@ -591,7 +610,11 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size) | |
buffer[last_set_ip].opcode = _SET_IP; | ||
last_set_ip = -1; | ||
} | ||
break; | ||
} | ||
case _JUMP_TO_TOP: | ||
case _EXIT_TRACE: | ||
return pc + 1; | ||
} | ||
} | ||
Py_UNREACHABLE(); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Why do we have two
_CHECK_VALIDITY
s now?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.
There used to be one validity check, after the
setattr
call. This adds an additional one after the_POP_TOP
following the call, since in theory the function could return an object with a destructor that invalidates the JIT code when it's popped.Because of the old structure of this optimization pass, we weren't actually treating
_POP_TOP
as escaping, even though it is (this is handled in thedefault
case, which we fall through to now).