-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[mypyc] Optionally log a sampled operation trace to a file #19457
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """This optional pass adds logging of various executed operations. | ||
|
|
||
| Some subset of the executed operations are logged to the mypyc_trace.txt file. | ||
|
|
||
| This is useful for performance analysis. For example, it's possible | ||
| to identify how frequently various primitive functions are called, | ||
| and in which code locations they are called. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from mypyc.ir.func_ir import FuncIR | ||
| from mypyc.ir.ops import Call, CallC, CString, LoadLiteral, LoadStatic, Op, PrimitiveOp, Value | ||
| from mypyc.irbuild.ll_builder import LowLevelIRBuilder | ||
| from mypyc.options import CompilerOptions | ||
| from mypyc.primitives.misc_ops import log_trace_event | ||
| from mypyc.transform.ir_transform import IRTransform | ||
|
|
||
|
|
||
| def insert_event_trace_logging(fn: FuncIR, options: CompilerOptions) -> None: | ||
| builder = LowLevelIRBuilder(None, options) | ||
| transform = LogTraceEventTransform(builder, fn.decl.fullname) | ||
| transform.transform_blocks(fn.blocks) | ||
| fn.blocks = builder.blocks | ||
|
|
||
|
|
||
| def get_load_global_name(op: CallC) -> str | None: | ||
| name = op.function_name | ||
| if name == "CPyDict_GetItem": | ||
| arg = op.args[0] | ||
| if ( | ||
| isinstance(arg, LoadStatic) | ||
| and arg.namespace == "static" | ||
| and arg.identifier == "globals" | ||
| and isinstance(op.args[1], LoadLiteral) | ||
| ): | ||
| return str(op.args[1].value) | ||
| return None | ||
|
|
||
|
|
||
| class LogTraceEventTransform(IRTransform): | ||
| def __init__(self, builder: LowLevelIRBuilder, fullname: str) -> None: | ||
| super().__init__(builder) | ||
| self.fullname = fullname.encode("utf-8") | ||
|
|
||
| def visit_call(self, op: Call) -> Value: | ||
| # TODO: Use different op name when constructing an instance | ||
| return self.log(op, "call", op.fn.fullname) | ||
|
|
||
| def visit_primitive_op(self, op: PrimitiveOp) -> Value: | ||
| self.log(op, "primitive_op", op.desc.name) | ||
| return self.add(op) | ||
|
|
||
| def visit_call_c(self, op: CallC) -> Value: | ||
| if global_name := get_load_global_name(op): | ||
| return self.log(op, "globals_dict_get_item", global_name) | ||
|
|
||
| func_name = op.function_name | ||
| if func_name == "PyObject_Vectorcall" and isinstance(op.args[0], CallC): | ||
| if global_name := get_load_global_name(op.args[0]): | ||
| return self.log(op, "python_call_global", global_name) | ||
| elif func_name == "CPyObject_GetAttr" and isinstance(op.args[1], LoadLiteral): | ||
| return self.log(op, "python_get_attr", str(op.args[1].value)) | ||
| elif func_name == "PyObject_VectorcallMethod" and isinstance(op.args[0], LoadLiteral): | ||
| return self.log(op, "python_call_method", str(op.args[0].value)) | ||
|
|
||
| return self.log(op, "call_c", func_name) | ||
|
|
||
| def log(self, op: Op, name: str, details: str) -> Value: | ||
| if op.line >= 0: | ||
| line_str = str(op.line) | ||
| else: | ||
| line_str = "" | ||
| self.builder.primitive_op( | ||
| log_trace_event, | ||
| [ | ||
| CString(self.fullname), | ||
| CString(line_str.encode("ascii")), | ||
| CString(name.encode("utf-8")), | ||
| CString(details.encode("utf-8")), | ||
| ], | ||
| op.line, | ||
| ) | ||
| return self.add(op) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.