Skip to content
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

Support Visual Studio heap profiler in MSVC targets #68076

Open
jdm opened this issue Jan 9, 2020 · 4 comments
Open

Support Visual Studio heap profiler in MSVC targets #68076

jdm opened this issue Jan 9, 2020 · 4 comments
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jdm
Copy link
Contributor

jdm commented Jan 9, 2020

Cland and MSVC support adding special debug info that can be recognized by the MSVC debugging tools for performing heap profiling. Right now all of the allocations from Rust code are put into an "Unresolved allocations" bucket with no stack frames attached to them, which makes them quite useless. Since LLVM 9 the backend supports generating the appropriate codeview information if the frontend creates the right metadata, so it would be great to allow rustc to generate that metadata.

Based on:

Clang frontend work:

We might be able to tie this into the existing GlobalAllocator support. This work may need to wait until #67759 has merged, however.

@jdm jdm added A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) O-windows-msvc Toolchain: MSVC, Operating system: Windows labels Jan 9, 2020
@jdm
Copy link
Contributor Author

jdm commented Jan 22, 2020

Given #68030, it should be possible to work on this now.

@jdm
Copy link
Contributor Author

jdm commented Feb 8, 2020

Important points for clang's implementation:

  • need a known set of allocator functions
  • need a way to tag callsites of the allocator functions
  • need to notice when the type returned by an allocator is cast to a different pointer type and update the tag
  • set the "heapallocsite" metadata on the LLVM call instruction to a MDNode that corresponds to the pointer type of the allocation

@jdm
Copy link
Contributor Author

jdm commented Apr 13, 2020

https://github.com/jdm/rust/tree/heap-profiler is a branch that causes heapallocsite debug metadata to be added to LLVM call instructions for heap allocator functions. I haven't tested the effect on Windows yet, but the IR output is at least different now.

Note to self: write some simple C and look at the LLVM IR for the presence of heapallocsite to compare.

@jdm
Copy link
Contributor Author

jdm commented May 4, 2020

I had a chance to test the branch. I created a cargo library that generated a cdylib, exported a C API from it that has methods for boxing and freeing Rust structures, and pushes values into vectors and creates String values out of C strings. I then created a VS console project that links with this DLL and invokes the C API, while also including some C++ std::string and dynamically allocated arrays.

Using the latest MSVC nightly, enabling heap profiling showed entries for char[] and int[] for the C++ allocations as expected. If I disabled the "Hide unresolved allocations", I could see the Rust allocations bucketed with many other unrelated allocations, since fundamentally liballoc ends up calling the win32 allocation APIs that the heap profiler understands.

With my heap-profiler branch and the "Hide unresolved allocations" option re-enabled, the Rust allocations are now bucketed under unsigned char *[] and are no longer classified as unresolved. This is clearly an improvement, since you no longer have to wade through lots of unrelated win32-internal allocation stacks, but I'm hoping that including higher-level allocation gateways like exchange_malloc will allow us to bucket the resulting allocations in more granular ways.

@Nilstrieb Nilstrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants