Skip to content

GH-116596: Better determination of escaping uops. #116597

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

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 12 additions & 12 deletions Include/internal/pycore_opcode_metadata.h

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

28 changes: 14 additions & 14 deletions Include/internal/pycore_uop_metadata.h

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

1 change: 1 addition & 0 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,7 @@ uop_optimize(
break;
}
assert(_PyOpcode_uop_name[buffer[pc].opcode]);
assert(strncmp(_PyOpcode_uop_name[buffer[pc].opcode], _PyOpcode_uop_name[opcode], strlen(_PyOpcode_uop_name[opcode])) == 0);
}
_PyExecutorObject *executor = make_executor_from_uops(buffer, &dependencies);
if (executor == NULL) {
Expand Down
8 changes: 4 additions & 4 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,15 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
int opcode = buffer[pc].opcode;
switch (opcode) {
case _SET_IP:
buffer[pc].opcode = NOP;
buffer[pc].opcode = _NOP;
last_set_ip = pc;
break;
case _CHECK_VALIDITY:
if (may_have_escaped) {
may_have_escaped = false;
}
else {
buffer[pc].opcode = NOP;
buffer[pc].opcode = _NOP;
}
break;
case _CHECK_VALIDITY_AND_SET_IP:
Expand All @@ -447,7 +447,7 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
buffer[pc].opcode = _CHECK_VALIDITY;
}
else {
buffer[pc].opcode = NOP;
buffer[pc].opcode = _NOP;
}
last_set_ip = pc;
break;
Expand All @@ -463,7 +463,7 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
last->opcode == _COPY
) {
last->opcode = _NOP;
buffer[pc].opcode = NOP;
buffer[pc].opcode = _NOP;
Copy link
Contributor

Choose a reason for hiding this comment

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

NOP vs. _NOP... I hope the difference between those two is documented somewhere1 😄

Footnotes

  1. After a quick git grep I could not figure out the semantic difference between them.

Copy link
Member

Choose a reason for hiding this comment

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

In Tier 1 it's NOP. In Tier 2 it's _NOP. The original code worked because they are currently the same value (as are all opcodes that are shared between T1 and T2). But eventually we may want all the Tier 2 opcodes to be distinct.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the clarification!

}
if (last->opcode == _REPLACE_WITH_TRUE) {
last->opcode = _NOP;
Expand Down
16 changes: 16 additions & 0 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ def is_infallible(op: parser.InstDef) -> bool:
"_PyDictOrValues_IsValues",
"_PyObject_DictOrValuesPointer",
"_PyDictOrValues_GetValues",
"_PyDictValues_AddToInsertionOrder",
"_PyObject_MakeInstanceAttributesFromDict",
"Py_DECREF",
"_Py_DECREF_SPECIALIZED",
Expand All @@ -355,8 +356,10 @@ def is_infallible(op: parser.InstDef) -> bool:
"_PyLong_IsCompact",
"_PyLong_IsNonNegativeCompact",
"_PyLong_CompactValue",
"_PyLong_DigitCount",
"_Py_NewRef",
"_Py_IsImmortal",
"PyLong_FromLong",
"_Py_STR",
"_PyLong_Add",
"_PyLong_Multiply",
Expand All @@ -368,6 +371,17 @@ def is_infallible(op: parser.InstDef) -> bool:
"_Py_atomic_load_uintptr_relaxed",
"_PyFrame_GetCode",
"_PyThreadState_HasStackSpace",
"_PyUnicode_Equal",
"_PyFrame_SetStackPointer",
"_PyType_HasFeature",
"PyUnicode_Concat",
"_PyList_FromArraySteal",
"_PyTuple_FromArraySteal",
"PySlice_New",
"_Py_LeaveRecursiveCallPy",
"CALL_STAT_INC",
"maybe_lltrace_resume_frame",
"_PyUnicode_JoinArray",
)

ESCAPING_FUNCTIONS = (
Expand All @@ -379,6 +393,8 @@ def is_infallible(op: parser.InstDef) -> bool:
def makes_escaping_api_call(instr: parser.InstDef) -> bool:
if "CALL_INTRINSIC" in instr.name:
return True
if instr.name == "_BINARY_OP":
return True
tkns = iter(instr.tokens)
for tkn in tkns:
if tkn.kind != lexer.IDENTIFIER:
Expand Down