Skip to content

Commit aca749e

Browse files
committedApr 23, 2024·
Auto merge of #121801 - zetanumbers:async_drop_glue, r=oli-obk
Add simple async drop glue generation This is a prototype of the async drop glue generation for some simple types. Async drop glue is intended to behave very similar to the regular drop glue except for being asynchronous. Currently it does not execute synchronous drops but only calls user implementations of `AsyncDrop::async_drop` associative function and awaits the returned future. It is not complete as it only recurses into arrays, slices, tuples, and structs and does not have same sensible restrictions as the old `Drop` trait implementation like having the same bounds as the type definition, while code assumes their existence (requires a future work). This current design uses a workaround as it does not create any custom async destructor state machine types for ADTs, but instead uses types defined in the std library called future combinators (deferred_async_drop, chain, ready_unit). Also I recommend reading my [explainer](https://zetanumbers.github.io/book/async-drop-design.html). This is a part of the [MCP: Low level components for async drop](rust-lang/compiler-team#727) work. Feature completeness: - [x] `AsyncDrop` trait - [ ] `async_drop_in_place_raw`/async drop glue generation support for - [x] Trivially destructible types (integers, bools, floats, string slices, pointers, references, etc.) - [x] Arrays and slices (array pointer is unsized into slice pointer) - [x] ADTs (enums, structs, unions) - [x] tuple-like types (tuples, closures) - [ ] Dynamic types (`dyn Trait`, see explainer's [proposed design](https://github.com/zetanumbers/posts/blob/main/async-drop-design.md#async-drop-glue-for-dyn-trait)) - [ ] coroutines (#123948) - [x] Async drop glue includes sync drop glue code - [x] Cleanup branch generation for `async_drop_in_place_raw` - [ ] Union rejects non-trivially async destructible fields - [ ] `AsyncDrop` implementation requires same bounds as type definition - [ ] Skip trivially destructible fields (optimization) - [ ] New [`TyKind::AdtAsyncDestructor`](https://github.com/zetanumbers/posts/blob/main/async-drop-design.md#adt-async-destructor-types) and get rid of combinators - [ ] [Synchronously undroppable types](https://github.com/zetanumbers/posts/blob/main/async-drop-design.md#exclusively-async-drop) - [ ] Automatic async drop at the end of the scope in async context
2 parents 9cf10bc + 67980dd commit aca749e

File tree

44 files changed

+1916
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1916
-24
lines changed
 

‎compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+41
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,24 @@ fn exported_symbols_provider_local(
363363
},
364364
));
365365
}
366+
MonoItem::Fn(Instance {
367+
def: InstanceDef::AsyncDropGlueCtorShim(def_id, Some(ty)),
368+
args,
369+
}) => {
370+
// A little sanity-check
371+
debug_assert_eq!(
372+
args.non_erasable_generics(tcx, def_id).skip(1).next(),
373+
Some(GenericArgKind::Type(ty))
374+
);
375+
symbols.push((
376+
ExportedSymbol::AsyncDropGlueCtorShim(ty),
377+
SymbolExportInfo {
378+
level: SymbolExportLevel::Rust,
379+
kind: SymbolExportKind::Text,
380+
used: false,
381+
},
382+
));
383+
}
366384
_ => {
367385
// Any other symbols don't qualify for sharing
368386
}
@@ -385,6 +403,7 @@ fn upstream_monomorphizations_provider(
385403
let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();
386404

387405
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
406+
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
388407

389408
for &cnum in cnums.iter() {
390409
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
@@ -399,6 +418,18 @@ fn upstream_monomorphizations_provider(
399418
continue;
400419
}
401420
}
421+
ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
422+
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
423+
(
424+
async_drop_in_place_fn_def_id,
425+
tcx.mk_args(&[tcx.lifetimes.re_erased.into(), ty.into()]),
426+
)
427+
} else {
428+
// `drop_in_place` in place does not exist, don't try
429+
// to use it.
430+
continue;
431+
}
432+
}
402433
ExportedSymbol::NonGeneric(..)
403434
| ExportedSymbol::ThreadLocalShim(..)
404435
| ExportedSymbol::NoDefId(..) => {
@@ -534,6 +565,13 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
534565
Instance::resolve_drop_in_place(tcx, ty),
535566
instantiating_crate,
536567
),
568+
ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
569+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
570+
tcx,
571+
Instance::resolve_async_drop_in_place(tcx, ty),
572+
instantiating_crate,
573+
)
574+
}
537575
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
538576
}
539577
}
@@ -582,6 +620,9 @@ pub fn linking_symbol_name_for_instance_in_crate<'tcx>(
582620
// DropGlue always use the Rust calling convention and thus follow the target's default
583621
// symbol decoration scheme.
584622
ExportedSymbol::DropGlue(..) => None,
623+
// AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
624+
// target's default symbol decoration scheme.
625+
ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
585626
// NoDefId always follow the target's default symbol decoration scheme.
586627
ExportedSymbol::NoDefId(..) => None,
587628
// ThreadLocalShim always follow the target's default symbol decoration scheme.

‎compiler/rustc_codegen_ssa/src/mir/block.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
835835

836836
let def = instance.map(|i| i.def);
837837

838-
if let Some(ty::InstanceDef::DropGlue(_, None)) = def {
838+
if let Some(
839+
ty::InstanceDef::DropGlue(_, None) | ty::InstanceDef::AsyncDropGlueCtorShim(_, None),
840+
) = def
841+
{
839842
// Empty drop glue; a no-op.
840843
let target = target.unwrap();
841844
return helper.funclet_br(self, bx, target, mergeable_succ);

0 commit comments

Comments
 (0)