Skip to content

Commit 36ed446

Browse files
Stop using a special inner body for the coroutine by-move body for async closures
1 parent 99322d8 commit 36ed446

File tree

24 files changed

+188
-230
lines changed

24 files changed

+188
-230
lines changed

compiler/rustc_const_eval/src/interpret/terminator.rs

-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
608608
| ty::InstanceKind::ReifyShim(..)
609609
| ty::InstanceKind::ClosureOnceShim { .. }
610610
| ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
611-
| ty::InstanceKind::CoroutineKindShim { .. }
612611
| ty::InstanceKind::FnPtrShim(..)
613612
| ty::InstanceKind::DropGlue(..)
614613
| ty::InstanceKind::CloneShim(..)

compiler/rustc_hir_analysis/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
199199
}
200200
});
201201

202-
// Freeze definitions as we don't add new ones at this point. This improves performance by
203-
// allowing lock-free access to them.
204-
tcx.untracked().definitions.freeze();
205-
206202
// FIXME: Remove this when we implement creating `DefId`s
207203
// for anon constants during their parents' typeck.
208204
// Typeck all body owners in parallel will produce queries

compiler/rustc_interface/src/passes.rs

+7
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,13 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
816816
);
817817
}
818818
});
819+
// Freeze definitions as we don't add new ones at this point.
820+
// We need to wait until now since we synthesize a by-move body
821+
// This improves performance by allowing lock-free access to them.
822+
// FIXME(async_closures): We could force `coroutine_by_move_body_def_id`
823+
// immediately after typeck, then freeze after that.
824+
tcx.untracked().definitions.freeze();
825+
819826
sess.time("layout_testing", || layout_test::test_layout(tcx));
820827
sess.time("abi_testing", || abi_test::test_abi(tcx));
821828
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13661366
let def_span = tcx.def_span(local_id);
13671367
record!(self.tables.def_span[def_id] <- def_span);
13681368
}
1369-
if should_encode_attrs(def_kind) {
1369+
// FIXME(async_closures): We should just use `tcx.attrs` rather than going
1370+
// through the HIR. Historically, though, this has been inefficient apparently.
1371+
// For now, it's kind of pointless to fix, because coroutine-closures' coroutine
1372+
// bodies have no attrs anyways.
1373+
if should_encode_attrs(def_kind) && !tcx.is_synthetic_mir(local_id) {
13701374
self.encode_attrs(local_id);
13711375
}
13721376
if should_encode_expn_that_defined(def_kind) {

compiler/rustc_middle/src/mir/mod.rs

-17
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,6 @@ pub struct CoroutineInfo<'tcx> {
267267
/// Coroutine drop glue. This field is populated after the state transform pass.
268268
pub coroutine_drop: Option<Body<'tcx>>,
269269

270-
/// The body of the coroutine, modified to take its upvars by move rather than by ref.
271-
///
272-
/// This is used by coroutine-closures, which must return a different flavor of coroutine
273-
/// when called using `AsyncFnOnce::call_once`. It is produced by the `ByMoveBody` pass which
274-
/// is run right after building the initial MIR, and will only be populated for coroutines
275-
/// which come out of the async closure desugaring.
276-
///
277-
/// This body should be processed in lockstep with the containing body -- any optimization
278-
/// passes, etc, should be applied to this body as well. This is done automatically if
279-
/// using `run_passes`.
280-
pub by_move_body: Option<Body<'tcx>>,
281-
282270
/// The layout of a coroutine. This field is populated after the state transform pass.
283271
pub coroutine_layout: Option<CoroutineLayout<'tcx>>,
284272

@@ -298,7 +286,6 @@ impl<'tcx> CoroutineInfo<'tcx> {
298286
coroutine_kind,
299287
yield_ty: Some(yield_ty),
300288
resume_ty: Some(resume_ty),
301-
by_move_body: None,
302289
coroutine_drop: None,
303290
coroutine_layout: None,
304291
}
@@ -663,10 +650,6 @@ impl<'tcx> Body<'tcx> {
663650
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop.as_ref())
664651
}
665652

666-
pub fn coroutine_by_move_body(&self) -> Option<&Body<'tcx>> {
667-
self.coroutine.as_ref()?.by_move_body.as_ref()
668-
}
669-
670653
#[inline]
671654
pub fn coroutine_kind(&self) -> Option<CoroutineKind> {
672655
self.coroutine.as_ref().map(|coroutine| coroutine.coroutine_kind)

compiler/rustc_middle/src/mir/mono.rs

-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ impl<'tcx> CodegenUnit<'tcx> {
415415
| InstanceKind::Virtual(..)
416416
| InstanceKind::ClosureOnceShim { .. }
417417
| InstanceKind::ConstructCoroutineInClosureShim { .. }
418-
| InstanceKind::CoroutineKindShim { .. }
419418
| InstanceKind::DropGlue(..)
420419
| InstanceKind::CloneShim(..)
421420
| InstanceKind::ThreadLocalShim(..)

compiler/rustc_middle/src/mir/visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ macro_rules! make_mir_visitor {
349349
coroutine_closure_def_id: _def_id,
350350
receiver_by_ref: _,
351351
} |
352-
ty::InstanceKind::CoroutineKindShim { coroutine_def_id: _def_id } |
353352
ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, None) |
354353
ty::InstanceKind::DropGlue(_def_id, None) => {}
355354

compiler/rustc_middle/src/query/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ rustc_queries! {
327327
query predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
328328
desc { |tcx| "computing predicates of `{}`", tcx.def_path_str(key) }
329329
cache_on_disk_if { key.is_local() }
330+
feedable
330331
}
331332

332333
query opaque_types_defined_by(
@@ -488,13 +489,19 @@ rustc_queries! {
488489
separate_provide_extern
489490
}
490491

492+
query is_synthetic_mir(key: LocalDefId) -> bool {
493+
desc { |tcx| "checking if `{}` is synthetic", tcx.def_path_str(key) }
494+
feedable
495+
}
496+
491497
/// Build the MIR for a given `DefId` and prepare it for const qualification.
492498
///
493499
/// See the [rustc dev guide] for more info.
494500
///
495501
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/construction.html
496502
query mir_built(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
497503
desc { |tcx| "building MIR for `{}`", tcx.def_path_str(key) }
504+
feedable
498505
}
499506

500507
/// Try to build an abstract representation of the given constant.
@@ -739,6 +746,7 @@ rustc_queries! {
739746
query constness(key: DefId) -> hir::Constness {
740747
desc { |tcx| "checking if item is const: `{}`", tcx.def_path_str(key) }
741748
separate_provide_extern
749+
feedable
742750
}
743751

744752
query asyncness(key: DefId) -> ty::Asyncness {
@@ -757,10 +765,22 @@ rustc_queries! {
757765
desc { |tcx| "checking if item is promotable: `{}`", tcx.def_path_str(key) }
758766
}
759767

768+
/// The body of the coroutine, modified to take its upvars by move rather than by ref.
769+
///
770+
/// This is used by coroutine-closures, which must return a different flavor of coroutine
771+
/// when called using `AsyncFnOnce::call_once`. It is produced by the `ByMoveBody` pass which
772+
/// is run right after building the initial MIR, and will only be populated for coroutines
773+
/// which come out of the async closure desugaring.
774+
query coroutine_by_move_body_def_id(def_id: DefId) -> DefId {
775+
desc { |tcx| "looking up the coroutine by-move body for `{}`", tcx.def_path_str(def_id) }
776+
separate_provide_extern
777+
}
778+
760779
/// Returns `Some(coroutine_kind)` if the node pointed to by `def_id` is a coroutine.
761780
query coroutine_kind(def_id: DefId) -> Option<hir::CoroutineKind> {
762781
desc { |tcx| "looking up coroutine kind of `{}`", tcx.def_path_str(def_id) }
763782
separate_provide_extern
783+
feedable
764784
}
765785

766786
query coroutine_for_closure(def_id: DefId) -> DefId {

compiler/rustc_middle/src/ty/instance.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,6 @@ pub enum InstanceKind<'tcx> {
141141
receiver_by_ref: bool,
142142
},
143143

144-
/// `<[coroutine] as Future>::poll`, but for coroutines produced when `AsyncFnOnce`
145-
/// is called on a coroutine-closure whose closure kind greater than `FnOnce`, or
146-
/// similarly for `AsyncFnMut`.
147-
///
148-
/// This will select the body that is produced by the `ByMoveBody` transform, and thus
149-
/// take and use all of its upvars by-move rather than by-ref.
150-
CoroutineKindShim { coroutine_def_id: DefId },
151-
152144
/// Compiler-generated accessor for thread locals which returns a reference to the thread local
153145
/// the `DefId` defines. This is used to export thread locals from dylibs on platforms lacking
154146
/// native support.
@@ -248,7 +240,6 @@ impl<'tcx> InstanceKind<'tcx> {
248240
coroutine_closure_def_id: def_id,
249241
receiver_by_ref: _,
250242
}
251-
| ty::InstanceKind::CoroutineKindShim { coroutine_def_id: def_id }
252243
| InstanceKind::DropGlue(def_id, _)
253244
| InstanceKind::CloneShim(def_id, _)
254245
| InstanceKind::FnPtrAddrShim(def_id, _)
@@ -270,7 +261,6 @@ impl<'tcx> InstanceKind<'tcx> {
270261
| InstanceKind::Intrinsic(..)
271262
| InstanceKind::ClosureOnceShim { .. }
272263
| ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
273-
| ty::InstanceKind::CoroutineKindShim { .. }
274264
| InstanceKind::DropGlue(..)
275265
| InstanceKind::AsyncDropGlueCtorShim(..)
276266
| InstanceKind::CloneShim(..)
@@ -377,7 +367,6 @@ impl<'tcx> InstanceKind<'tcx> {
377367
| InstanceKind::AsyncDropGlueCtorShim(_, Some(_)) => false,
378368
InstanceKind::ClosureOnceShim { .. }
379369
| InstanceKind::ConstructCoroutineInClosureShim { .. }
380-
| InstanceKind::CoroutineKindShim { .. }
381370
| InstanceKind::DropGlue(..)
382371
| InstanceKind::AsyncDropGlueCtorShim(..)
383372
| InstanceKind::Item(_)
@@ -452,7 +441,6 @@ pub fn fmt_instance(
452441
InstanceKind::FnPtrShim(_, ty) => write!(f, " - shim({ty})"),
453442
InstanceKind::ClosureOnceShim { .. } => write!(f, " - shim"),
454443
InstanceKind::ConstructCoroutineInClosureShim { .. } => write!(f, " - shim"),
455-
InstanceKind::CoroutineKindShim { .. } => write!(f, " - shim"),
456444
InstanceKind::DropGlue(_, None) => write!(f, " - shim(None)"),
457445
InstanceKind::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({ty}))"),
458446
InstanceKind::CloneShim(_, ty) => write!(f, " - shim({ty})"),
@@ -850,7 +838,9 @@ impl<'tcx> Instance<'tcx> {
850838
Some(Instance { def: ty::InstanceKind::Item(coroutine_def_id), args })
851839
} else {
852840
Some(Instance {
853-
def: ty::InstanceKind::CoroutineKindShim { coroutine_def_id },
841+
def: ty::InstanceKind::Item(
842+
tcx.coroutine_by_move_body_def_id(coroutine_def_id),
843+
),
854844
args,
855845
})
856846
}

compiler/rustc_middle/src/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,6 @@ impl<'tcx> TyCtxt<'tcx> {
17551755
| ty::InstanceKind::Virtual(..)
17561756
| ty::InstanceKind::ClosureOnceShim { .. }
17571757
| ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
1758-
| ty::InstanceKind::CoroutineKindShim { .. }
17591758
| ty::InstanceKind::DropGlue(..)
17601759
| ty::InstanceKind::CloneShim(..)
17611760
| ty::InstanceKind::ThreadLocalShim(..)
@@ -1873,7 +1872,8 @@ impl<'tcx> TyCtxt<'tcx> {
18731872
identity_kind_ty.to_opt_closure_kind(),
18741873
Some(ClosureKind::Fn | ClosureKind::FnMut)
18751874
);
1876-
mir.coroutine_by_move_body().unwrap().coroutine_layout_raw()
1875+
self.optimized_mir(self.coroutine_by_move_body_def_id(def_id))
1876+
.coroutine_layout_raw()
18771877
}
18781878
}
18791879
}

compiler/rustc_mir_transform/src/coroutine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
mod by_move_body;
5454
use std::{iter, ops};
5555

56-
pub use by_move_body::ByMoveBody;
56+
pub use by_move_body::coroutine_by_move_body_def_id;
5757
use rustc_data_structures::fx::FxHashSet;
5858
use rustc_errors::pluralize;
5959
use rustc_hir as hir;

0 commit comments

Comments
 (0)