Skip to content

Commit

Permalink
Try moving DefId freeze earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Aug 2, 2024
1 parent 776dc68 commit bca0ea9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
21 changes: 15 additions & 6 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,22 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
}
);
});

rustc_hir_analysis::check_crate(tcx);
sess.time("MIR_coroutine_by_move_body", || {
tcx.hir().par_body_owners(|def_id| {
if tcx.needs_coroutine_by_move_body_def_id(def_id) {
tcx.ensure_with_value().coroutine_by_move_body_def_id(def_id);
}
});
});
// Freeze definitions as we don't add new ones at this point.
// We need to wait until now since we synthesize a by-move body
// This improves performance by allowing lock-free access to them.
// FIXME(async_closures): We could force `coroutine_by_move_body_def_id`
// immediately after typeck, then freeze after that.
tcx.untracked().definitions.freeze();

sess.time("MIR_borrow_checking", || {
tcx.hir().par_body_owners(|def_id| {
// Run unsafety check because it's responsible for stealing and
Expand Down Expand Up @@ -816,12 +831,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
);
}
});
// Freeze definitions as we don't add new ones at this point.
// We need to wait until now since we synthesize a by-move body
// This improves performance by allowing lock-free access to them.
// FIXME(async_closures): We could force `coroutine_by_move_body_def_id`
// immediately after typeck, then freeze after that.
tcx.untracked().definitions.freeze();

sess.time("layout_testing", || layout_test::test_layout(tcx));
sess.time("abi_testing", || abi_test::test_abi(tcx));
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3176,6 +3176,18 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn impl_polarity(self, def_id: impl IntoQueryParam<DefId>) -> ty::ImplPolarity {
self.impl_trait_header(def_id).map_or(ty::ImplPolarity::Positive, |h| h.polarity)
}

pub fn needs_coroutine_by_move_body_def_id(self, def_id: LocalDefId) -> bool {
if let Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure)) =
self.coroutine_kind(def_id)
&& let ty::Coroutine(_, args) = self.type_of(def_id).instantiate_identity().kind()
&& args.as_coroutine().kind_ty().to_opt_closure_kind() != Some(ty::ClosureKind::FnOnce)
{
true
} else {
false
}
}
}

/// Parameter attributes that can only be determined by examining the body of a function instead
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,7 @@ fn mir_promoted(
tcx.ensure_with_value().has_ffi_unwind_calls(def);

// the `by_move_body` query uses the raw mir, so make sure it is run.
if matches!(
tcx.coroutine_kind(def),
Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure))
) && let ty::Coroutine(_, args) = tcx.type_of(def).instantiate_identity().kind()
&& args.as_coroutine().kind_ty().to_opt_closure_kind() != Some(ty::ClosureKind::FnOnce)
{
if tcx.needs_coroutine_by_move_body_def_id(def) {
tcx.ensure_with_value().coroutine_by_move_body_def_id(def);
}

Expand Down

0 comments on commit bca0ea9

Please sign in to comment.