Skip to content

Commit fb7373f

Browse files
committed
Subpart11 for async drop (major5) - shims codegen
1 parent 3973bb0 commit fb7373f

File tree

4 files changed

+585
-13
lines changed

4 files changed

+585
-13
lines changed

compiler/rustc_interface/src/passes.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,13 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
911911
tcx.ensure_ok().check_coroutine_obligations(
912912
tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
913913
);
914-
// Eagerly check the unsubstituted layout for cycles.
915-
tcx.ensure_ok().layout_of(
916-
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
917-
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
918-
);
914+
if !tcx.is_templated_coroutine(def_id.to_def_id()) {
915+
// Eagerly check the unsubstituted layout for cycles.
916+
tcx.ensure_ok().layout_of(
917+
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
918+
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
919+
);
920+
}
919921
}
920922
});
921923
});

compiler/rustc_mir_transform/src/remove_zsts.rs

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ fn trivially_zst<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<bool> {
5959
| ty::RawPtr(..)
6060
| ty::Ref(..)
6161
| ty::FnPtr(..) => Some(false),
62+
ty::Coroutine(def_id, _) => {
63+
if tcx.is_templated_coroutine(*def_id) {
64+
Some(false)
65+
} else {
66+
None
67+
}
68+
}
6269
// check `layout_of` to see (including unreachable things we won't actually see)
6370
_ => None,
6471
}

compiler/rustc_mir_transform/src/shim.rs

+79-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::def_id::DefId;
77
use rustc_hir::lang_items::LangItem;
88
use rustc_index::{Idx, IndexVec};
99
use rustc_middle::mir::patch::MirPatch;
10+
use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
1011
use rustc_middle::mir::*;
1112
use rustc_middle::query::Providers;
1213
use rustc_middle::ty::adjustment::PointerCoercion;
@@ -15,19 +16,56 @@ use rustc_middle::ty::{
1516
};
1617
use rustc_middle::{bug, span_bug};
1718
use rustc_mir_dataflow::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle};
18-
use rustc_span::source_map::Spanned;
19+
use rustc_span::source_map::{Spanned, dummy_spanned};
1920
use rustc_span::{DUMMY_SP, Span};
2021
use tracing::{debug, instrument};
2122

2223
use crate::{
2324
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator, inline,
24-
instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,
25+
instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads,
26+
run_optimization_passes, simplify,
2527
};
2628

29+
mod async_destructor_ctor;
30+
2731
pub(super) fn provide(providers: &mut Providers) {
2832
providers.mir_shims = make_shim;
2933
}
3034

35+
// Replace Pin<&mut ImplCoroutine> accesses (_1.0) into Pin<&mut ProxyCoroutine> acceses
36+
struct FixProxyFutureDropVisitor<'tcx> {
37+
tcx: TyCtxt<'tcx>,
38+
replace_to: Local,
39+
}
40+
41+
impl<'tcx> MutVisitor<'tcx> for FixProxyFutureDropVisitor<'tcx> {
42+
fn tcx(&self) -> TyCtxt<'tcx> {
43+
self.tcx
44+
}
45+
46+
fn visit_place(
47+
&mut self,
48+
place: &mut Place<'tcx>,
49+
_context: PlaceContext,
50+
_location: Location,
51+
) {
52+
if place.local == Local::from_u32(1) {
53+
if place.projection.len() == 1 {
54+
assert!(matches!(
55+
place.projection.first(),
56+
Some(ProjectionElem::Field(FieldIdx::ZERO, _))
57+
));
58+
*place = Place::from(self.replace_to);
59+
} else if place.projection.len() == 2 {
60+
assert!(matches!(place.projection[0], ProjectionElem::Field(FieldIdx::ZERO, _)));
61+
assert!(matches!(place.projection[1], ProjectionElem::Deref));
62+
*place =
63+
Place::from(self.replace_to).project_deeper(&[ProjectionElem::Deref], self.tcx);
64+
}
65+
}
66+
}
67+
}
68+
3169
fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<'tcx> {
3270
debug!("make_shim({:?})", instance);
3371

@@ -128,14 +166,47 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
128166
ty::InstanceKind::ThreadLocalShim(..) => build_thread_local_shim(tcx, instance),
129167
ty::InstanceKind::CloneShim(def_id, ty) => build_clone_shim(tcx, def_id, ty),
130168
ty::InstanceKind::FnPtrAddrShim(def_id, ty) => build_fn_ptr_addr_shim(tcx, def_id, ty),
131-
ty::InstanceKind::FutureDropPollShim(_def_id, _proxy_ty, _impl_ty) => {
132-
todo!()
169+
ty::InstanceKind::FutureDropPollShim(def_id, proxy_ty, impl_ty) => {
170+
let mut body =
171+
async_destructor_ctor::build_future_drop_poll_shim(tcx, def_id, proxy_ty, impl_ty);
172+
173+
pm::run_passes(
174+
tcx,
175+
&mut body,
176+
&[
177+
&mentioned_items::MentionedItems,
178+
&abort_unwinding_calls::AbortUnwindingCalls,
179+
&add_call_guards::CriticalCallEdges,
180+
],
181+
Some(MirPhase::Runtime(RuntimePhase::Optimized)),
182+
pm::Optimizations::Allowed,
183+
);
184+
debug!("make_shim({:?}) = {:?}", instance, body);
185+
return body;
133186
}
134-
ty::InstanceKind::AsyncDropGlue(_def_id, _ty) => {
135-
todo!()
187+
ty::InstanceKind::AsyncDropGlue(def_id, ty) => {
188+
let mut body = async_destructor_ctor::build_async_drop_shim(tcx, def_id, ty);
189+
190+
pm::run_passes(
191+
tcx,
192+
&mut body,
193+
&[
194+
&mentioned_items::MentionedItems,
195+
&simplify::SimplifyCfg::MakeShim,
196+
&crate::coroutine::StateTransform,
197+
],
198+
Some(MirPhase::Runtime(RuntimePhase::PostCleanup)),
199+
pm::Optimizations::Allowed,
200+
);
201+
run_optimization_passes(tcx, &mut body);
202+
debug!("make_shim({:?}) = {:?}", instance, body);
203+
return body;
136204
}
137-
ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, _ty) => {
138-
bug!("AsyncDropGlueCtorShim in re-working ({:?})", instance)
205+
206+
ty::InstanceKind::AsyncDropGlueCtorShim(def_id, ty) => {
207+
let body = async_destructor_ctor::build_async_destructor_ctor_shim(tcx, def_id, ty);
208+
debug!("make_shim({:?}) = {:?}", instance, body);
209+
return body;
139210
}
140211
ty::InstanceKind::Virtual(..) => {
141212
bug!("InstanceKind::Virtual ({:?}) is for direct calls only", instance)

0 commit comments

Comments
 (0)