Skip to content

Prereq5 for async drop - AsyncDropGlue & FutureDropPoll instances preparation #129740

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Prereq5 for async drop - AsyncDropGlue & FutureDropPoll instances pre…
…paration
  • Loading branch information
azhogin committed Apr 6, 2025
commit 9a0fb73d1d06e65931812af35c646d6c906f57da
30 changes: 27 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ fn exported_symbols_provider_local(
));
}
MonoItem::Fn(Instance {
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),
def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
args,
}) => {
// A little sanity-check
Expand All @@ -388,6 +388,16 @@ fn exported_symbols_provider_local(
},
));
}
MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(_, ty), args: _ }) => {
symbols.push((
ExportedSymbol::AsyncDropGlue(ty),
SymbolExportInfo {
level: SymbolExportLevel::Rust,
kind: SymbolExportKind::Text,
used: false,
},
));
}
_ => {
// Any other symbols don't qualify for sharing
}
Expand All @@ -411,6 +421,7 @@ fn upstream_monomorphizations_provider(

let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
let async_drop_in_place_poll_fn_def_id = tcx.lang_items().async_drop_in_place_poll_fn();

for &cnum in cnums.iter() {
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
Expand All @@ -429,8 +440,13 @@ fn upstream_monomorphizations_provider(
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
(async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
} else {
// `drop_in_place` in place does not exist, don't try
// to use it.
continue;
}
}
ExportedSymbol::AsyncDropGlue(ty) => {
if let Some(poll_fn_def_id) = async_drop_in_place_poll_fn_def_id {
(poll_fn_def_id, tcx.mk_args(&[ty.into()]))
} else {
continue;
}
}
Expand Down Expand Up @@ -582,6 +598,13 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
instantiating_crate,
)
}
ExportedSymbol::AsyncDropGlue(ty) => {
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::resolve_async_drop_in_place_poll(tcx, ty),
instantiating_crate,
)
}
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
}
}
Expand All @@ -604,6 +627,7 @@ fn calling_convention_for_symbol<'tcx>(
// AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
// target's default symbol decoration scheme.
ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
ExportedSymbol::AsyncDropGlue(..) => None,
// NoDefId always follow the target's default symbol decoration scheme.
ExportedSymbol::NoDefId(..) => None,
// ThreadLocalShim always follow the target's default symbol decoration scheme.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_const_eval/src/interpret/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
| ty::InstanceKind::FnPtrAddrShim(..)
| ty::InstanceKind::ThreadLocalShim(..)
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
| ty::InstanceKind::AsyncDropGlue(..)
| ty::InstanceKind::FutureDropPollShim(..)
| ty::InstanceKind::Item(_) => {
// We need MIR for this fn.
// Note that this can be an intrinsic, if we are executing its fallback body.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ language_item_table! {
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::None;
AsyncDropInPlace, sym::async_drop_in_place, async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
AsyncDropInPlacePoll, sym::async_drop_in_place_poll, async_drop_in_place_poll_fn, Target::Closure, GenericRequirement::Exact(1);
FutureDropPoll, sym::future_drop_poll, future_drop_poll_fn, Target::Fn, GenericRequirement::Exact(1);

CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
DispatchFromDyn, sym::dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/middle/exported_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum ExportedSymbol<'tcx> {
Generic(DefId, GenericArgsRef<'tcx>),
DropGlue(Ty<'tcx>),
AsyncDropGlueCtorShim(Ty<'tcx>),
AsyncDropGlue(Ty<'tcx>),
ThreadLocalShim(DefId),
NoDefId(ty::SymbolName<'tcx>),
}
Expand All @@ -63,6 +64,9 @@ impl<'tcx> ExportedSymbol<'tcx> {
ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
tcx.symbol_name(ty::Instance::resolve_async_drop_in_place(tcx, ty))
}
ExportedSymbol::AsyncDropGlue(ty) => {
tcx.symbol_name(ty::Instance::resolve_async_drop_in_place_poll(tcx, ty))
}
ExportedSymbol::ThreadLocalShim(def_id) => tcx.symbol_name(ty::Instance {
def: ty::InstanceKind::ThreadLocalShim(def_id),
args: ty::GenericArgs::empty(),
Expand Down
30 changes: 29 additions & 1 deletion compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ pub struct CoroutineInfo<'tcx> {
/// Coroutine drop glue. This field is populated after the state transform pass.
pub coroutine_drop: Option<Body<'tcx>>,

/// The layout of a coroutine. This field is populated after the state transform pass.
/// Coroutine async drop glue.
pub coroutine_drop_async: Option<Body<'tcx>>,

/// When coroutine has sync drop, this is async proxy calling `coroutine_drop` sync impl.
pub coroutine_drop_proxy_async: Option<Body<'tcx>>,

/// The layout of a coroutine. Produced by the state transformation.
pub coroutine_layout: Option<CoroutineLayout<'tcx>>,

/// If this is a coroutine then record the type of source expression that caused this coroutine
Expand All @@ -220,6 +226,8 @@ impl<'tcx> CoroutineInfo<'tcx> {
yield_ty: Some(yield_ty),
resume_ty: Some(resume_ty),
coroutine_drop: None,
coroutine_drop_async: None,
coroutine_drop_proxy_async: None,
coroutine_layout: None,
}
}
Expand Down Expand Up @@ -587,6 +595,26 @@ impl<'tcx> Body<'tcx> {
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop.as_ref())
}

#[inline]
pub fn coroutine_drop_async(&self) -> Option<&Body<'tcx>> {
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop_async.as_ref())
}

#[inline]
pub fn coroutine_requires_async_drop(&self) -> bool {
self.coroutine_drop_async().is_some()
}

#[inline]
pub fn future_drop_poll(&self) -> Option<&Body<'tcx>> {
self.coroutine.as_ref().and_then(|coroutine| {
coroutine
.coroutine_drop_async
.as_ref()
.or(coroutine.coroutine_drop_proxy_async.as_ref())
})
}

#[inline]
pub fn coroutine_kind(&self) -> Option<CoroutineKind> {
self.coroutine.as_ref().map(|coroutine| coroutine.coroutine_kind)
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ impl<'tcx> CodegenUnit<'tcx> {
| InstanceKind::CloneShim(..)
| InstanceKind::ThreadLocalShim(..)
| InstanceKind::FnPtrAddrShim(..)
| InstanceKind::AsyncDropGlue(..)
| InstanceKind::FutureDropPollShim(..)
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
}
}
Expand Down
32 changes: 29 additions & 3 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,20 @@ fn dump_path<'tcx>(
}));
s
}
ty::InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)) => {
// Unfortunately, pretty-printed typed are not very filename-friendly.
// We dome some filtering.
ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) => {
let mut s = ".".to_owned();
s.extend(ty.to_string().chars().filter_map(|c| match c {
' ' => None,
':' | '<' | '>' => Some('_'),
c => Some(c),
}));
s
}
ty::InstanceKind::AsyncDropGlue(_, ty) => {
let ty::Coroutine(_, args) = ty.kind() else {
bug!();
};
let ty = args.first().unwrap().expect_ty();
let mut s = ".".to_owned();
s.extend(ty.to_string().chars().filter_map(|c| match c {
' ' => None,
Expand All @@ -264,6 +275,21 @@ fn dump_path<'tcx>(
}));
s
}
ty::InstanceKind::FutureDropPollShim(_, proxy_cor, impl_cor) => {
let mut s = ".".to_owned();
s.extend(proxy_cor.to_string().chars().filter_map(|c| match c {
' ' => None,
':' | '<' | '>' => Some('_'),
c => Some(c),
}));
s.push('.');
s.extend(impl_cor.to_string().chars().filter_map(|c| match c {
' ' => None,
':' | '<' | '>' => Some('_'),
c => Some(c),
}));
s
}
_ => String::new(),
};

Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,21 @@ macro_rules! make_mir_visitor {
coroutine_closure_def_id: _def_id,
receiver_by_ref: _,
}
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, None)
| ty::InstanceKind::DropGlue(_def_id, None) => {}

ty::InstanceKind::FnPtrShim(_def_id, ty)
| ty::InstanceKind::DropGlue(_def_id, Some(ty))
| ty::InstanceKind::CloneShim(_def_id, ty)
| ty::InstanceKind::FnPtrAddrShim(_def_id, ty)
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, Some(ty)) => {
| ty::InstanceKind::AsyncDropGlue(_def_id, ty)
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, ty) => {
// FIXME(eddyb) use a better `TyContext` here.
self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
}
ty::InstanceKind::FutureDropPollShim(_def_id, proxy_ty, impl_ty) => {
self.visit_ty($(& $mutability)? *proxy_ty, TyContext::Location(location));
self.visit_ty($(& $mutability)? *impl_ty, TyContext::Location(location));
}
}
self.visit_args(callee_args, location);
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,10 @@ impl<'tcx> TyCtxt<'tcx> {
self.coroutine_kind(def_id).is_some()
}

pub fn is_templated_coroutine(self, def_id: DefId) -> bool {
Some(def_id) == self.lang_items().async_drop_in_place_poll_fn()
}

/// Returns the movability of the coroutine of `def_id`, or panics
/// if given a `def_id` that is not a coroutine.
pub fn coroutine_movability(self, def_id: DefId) -> hir::Movability {
Expand Down
Loading
Loading