-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Add -Zannotate-moves for profiler visibility of move/copy operations (codegen) #147803
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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in compiler/rustc_codegen_ssa Some changes occurred in compiler/rustc_codegen_gcc |
This implements a new unstable compiler flag `-Zannotate-moves` that makes move and copy operations visible in profilers by creating synthetic debug information. This is achieved with zero runtime cost by manipulating debug info scopes to make moves/copies appear as calls to `compiler_move<T, SIZE>` and `compiler_copy<T, SIZE>` marker functions in profiling tools. This allows developers to identify expensive move/copy operations in their code using standard profiling tools, without requiring specialized tooling or runtime instrumentation. The implementation works at codegen time. When processing MIR operands (`Operand::Move` and `Operand::Copy`), the codegen creates an `OperandRef` with an optional `move_annotation` field containing an `Instance` of the appropriate profiling marker function. When storing the operand, `store_with_annotation()` wraps the store operation in a synthetic debug scope that makes it appear inlined from the marker. Two marker functions (`compiler_move` and `compiler_copy`) are defined in `library/core/src/profiling.rs`. These are never actually called - they exist solely as debug info anchors. Operations are only annotated if the type: - Meets the size threshold (default: 65 bytes, configurable via `-Zannotate-moves=SIZE`) - Has a non-scalar backend representation (scalars use registers, not memcpy) This has a very small size impact on object file size. With the default limit it's well under 0.1%, and even with a very small limit of 8 bytes it's still ~1.5%. This could be enabled by default.
c72e0b5
to
06baf13
Compare
MCP filed rust-lang/compiler-team#928 |
Does this support cranelift? The advantage to doing such things in MIR is that it is much easier to support cranelift. |
Yeah, that's a good point which I mentioned in the MCP. I had assumed that cranelift and gcc used the same framework as llvm so supporting them would just be a matter of implementing I didn't check whether the MIR transform approach did actually work for cranelift or gcc (does gcc support debuginfo in general?). But if it does, then I wonder if there's a middle ground between MIR and codegen to get the best of both? |
Ah, I'll respond on the MCP Zulip thread. |
Note: this is an alternative implementation of #147206; rather than being a MIR transform, it adds the annotations closer to codegen. It's functionally the same but the implementation is lower impact and it could be more correct.
This implements a new unstable compiler flag
-Zannotate-moves
that makes move and copy operations visible in profilers by creating synthetic debug information. This is achieved with zero runtime cost by manipulating debug info scopes to make moves/copies appear as calls tocompiler_move<T, SIZE>
andcompiler_copy<T, SIZE>
marker functions in profiling tools.This allows developers to identify expensive move/copy operations in their code using standard profiling tools, without requiring specialized tooling or runtime instrumentation.
The implementation works at codegen time. When processing MIR operands (
Operand::Move
andOperand::Copy
), the codegen creates anOperandRef
with an optionalmove_annotation
field containing anInstance
of the appropriate profiling marker function. When storing the operand,store_with_annotation()
wraps the store operation in a synthetic debug scope that makes it appear inlined from the marker.Two marker functions (
compiler_move
andcompiler_copy
) are defined inlibrary/core/src/profiling.rs
. These are never actually called - they exist solely as debug info anchors.Operations are only annotated if the type:
-Zannotate-moves=SIZE
)This has a very small size impact on object file size. With the default limit it's well under 0.1%, and even with a very small limit of 8 bytes it's still ~1.5%. This could be enabled by default.