Skip to content

Add tracing in Miri for tcx.layout_of calls #142721

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Stypox
Copy link
Contributor

@Stypox Stypox commented Jun 19, 2025

This PR is meant for Miri, but requires a few changes in rustc code, hence why it's here. It adds tracing capabilities to the layout_of function in tcx by overriding the layout_of query (under local_providers) with a wrapper that opens a tracing span and then calls the actual layout_of. To make this possible, I had to make rustc_ty_utils::layout::layout_of public. I added an assert to ensure the providers.layout_of value I am replacing is actually rustc_ty_utils::layout::layout_of, just in case.

I also considered taking the previous value in providers.layout_of and calling that one instead, to avoid making layout_of public. But then the closure would not be castable to a function pointer anymore (providers.layout_of is a function pointer), because it would depend on the local variable storing the previous value of providers.layout_of. Using a global variable would work but would rely on unsafe or on Mutexes, so I wanted to avoid it.

This PR also adds a tracing call in instantiate_from_frame_and_normalize_erasing_regions.

Here is some tracing output when Miri is run on src/tools/miri/tests/pass/hello.rs, visualizable in https://ui.perfetto.dev: trace-1750338860374637.json

Another place where I could have added tracing calls is to the rustc_middle::ty::layout::LayoutCx struct / spanned_layout_of() function, however there is no simple way to disable the tracing calls with compile-time boolean constants there (since LayoutCx::new() is used everywhere and referenced directly), and in any case it seems like spanned_layout_of() just calls tcx.layout_of() anyway. For completeness' sake, here is tracing output for when a tracing call is added to spanned_layout_of(): trace-1750340887920584.json

Note: this PR is not ready yet as it requires rust-lang/miri#4406 to disable the changes in the Miri code when "tracing" is disabled. I will also add more comments then, if you think my approach is ok.

r? @RalfJung

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants