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

Avoid locking the global context across the after_expansion callback #107740

Merged
merged 1 commit into from
Feb 8, 2023
Merged
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
10 changes: 5 additions & 5 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,16 @@ fn run_compiler(
}
}

let mut gctxt = queries.global_ctxt()?;
// Make sure name resolution and macro expansion is run.
queries.global_ctxt()?;

if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
return early_exit();
}

// Make sure the `output_filenames` query is run for its side
// effects of writing the dep-info and reporting errors.
gctxt.enter(|tcx| tcx.output_filenames(()));
queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(()));

if sess.opts.output_types.contains_key(&OutputType::DepInfo)
&& sess.opts.output_types.len() == 1
Expand All @@ -345,7 +347,7 @@ fn run_compiler(
return early_exit();
}

gctxt.enter(|tcx| {
queries.global_ctxt()?.enter(|tcx| {
let result = tcx.analysis(());
if sess.opts.unstable_opts.save_analysis {
let crate_name = tcx.crate_name(LOCAL_CRATE);
Expand All @@ -362,8 +364,6 @@ fn run_compiler(
result
})?;

drop(gctxt);
Copy link
Contributor

Choose a reason for hiding this comment

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

This was explicitly added so that GlobalCtxt could be freed before invoking the linker to reduce peak memory usage. Has this been intentionally changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I added this just to release the lock before after_analysis: 261bbd7

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the one I added in #56732 was a couple of lines below. I wonder when that went away. It seems like that could easily slip by perf as I assume it doesn't track total memory usage regressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are tracked by perf, but only really checked by looking at the over-time graphs

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure? I though it only tracked the rustc process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

How would this affect memory usage? AFAICT, gctxt is a QueryResult<QueryContext>. QueryContext is just a wrapper for a &'tcx GlobalCtxt. Also, Queries::ongoing_codegen and Queries::linker both call self.global_ctxt(), so the query is repeated there anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

This PR just happened to remind me of an earlier state of rustc_interface where queries.global_ctxt() returned an owned copy which was explictly dropped before the linker was run. The current state of the compiler looks good.


if callbacks.after_analysis(compiler, queries) == Compilation::Stop {
return early_exit();
}
Expand Down