Skip to content

Commit 72d8304

Browse files
committed
remove obsolete post-return functions and fields
Now that post-return calls are handled internally without requiring explicit action by the embedder, we can avoid unnecessary bookkeeping.
1 parent bd015ab commit 72d8304

File tree

7 files changed

+22
-154
lines changed

7 files changed

+22
-154
lines changed

crates/environ/src/component/vmcomponent_offsets.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ pub const VMCOMPONENT_MAGIC: u32 = u32::from_le_bytes(*b"comp");
2929
/// canonical ABI flag `may_leave`
3030
pub const FLAG_MAY_LEAVE: i32 = 1 << 0;
3131

32-
/// Flag for the `VMComponentContext::flags` field which is set whenever a
33-
/// function is called to indicate that `post_return` must be called next.
34-
pub const FLAG_NEEDS_POST_RETURN: i32 = 1 << 2;
35-
3632
/// Runtime offsets within a `VMComponentContext` for a specific component.
3733
#[derive(Debug, Clone, Copy)]
3834
pub struct VMComponentOffsets<P> {

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,14 +1518,14 @@ impl StoreOpaque {
15181518
/// - `self` has not been poisoned due to a trap.
15191519
pub(crate) fn may_enter(&mut self, instance: RuntimeInstance) -> bool {
15201520
if !self.concurrency_support() {
1521-
return self.may_enter_at_all(instance);
1521+
return !self.trapped();
15221522
}
15231523
let state = self.concurrent_state_mut();
15241524
if let Some(caller) = state.guest_thread {
15251525
instance != state.get_mut(caller.task).unwrap().instance
15261526
&& self.may_enter_from_caller(caller.task, instance)
15271527
} else {
1528-
self.may_enter_at_all(instance)
1528+
!self.trapped()
15291529
}
15301530
}
15311531

@@ -1543,7 +1543,7 @@ impl StoreOpaque {
15431543
mut guest_task: TableId<GuestTask>,
15441544
instance: RuntimeInstance,
15451545
) -> bool {
1546-
self.may_enter_at_all(instance) && {
1546+
!self.trapped() && {
15471547
let state = self.concurrent_state_mut();
15481548
let guest_instance = instance.instance;
15491549
loop {

crates/wasmtime/src/runtime/component/concurrent_disabled.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl StoreOpaque {
172172
Ok(())
173173
}
174174

175-
pub(crate) fn may_enter(&mut self, instance: RuntimeInstance) -> bool {
176-
self.may_enter_at_all(instance)
175+
pub(crate) fn may_enter(&mut self, _instance: RuntimeInstance) -> bool {
176+
!self.trapped()
177177
}
178178
}

crates/wasmtime/src/runtime/component/func.rs

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl Func {
439439
MAX_FLAT_PARAMS,
440440
false,
441441
move |func, store, params_out| {
442-
func.with_lower_context(store, true, |cx, ty| {
442+
func.with_lower_context(store, |cx, ty| {
443443
Self::lower_args(cx, &params, ty, params_out)
444444
})
445445
},
@@ -490,7 +490,7 @@ impl Func {
490490
// safe in Rust, however, due to `ValRaw` being a `union`. The
491491
// contents should dynamically not be read due to the type of the
492492
// function used here matching the actual lift.
493-
unsafe {
493+
let (_, post_return_arg) = unsafe {
494494
self.call_raw(
495495
store.as_context_mut(),
496496
|cx, ty, dst: &mut MaybeUninit<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>| {
@@ -510,9 +510,9 @@ impl Func {
510510
Ok(())
511511
},
512512
)?
513-
}
513+
};
514514

515-
self.post_return_impl(store)
515+
self.post_return_impl(store, post_return_arg)
516516
}
517517

518518
pub(crate) fn lifted_core_func(&self, store: &mut StoreOpaque) -> NonNull<VMFuncRef> {
@@ -587,7 +587,7 @@ impl Func {
587587
&mut MaybeUninit<LowerParams>,
588588
) -> Result<()>,
589589
lift: impl FnOnce(&mut LiftContext<'_>, InterfaceType, &LowerReturn) -> Result<Return>,
590-
) -> Result<Return>
590+
) -> Result<(Return, ValRaw)>
591591
where
592592
LowerParams: Copy,
593593
LowerReturn: Copy,
@@ -631,7 +631,7 @@ impl Func {
631631
assert!(mem::align_of_val(map_maybe_uninit!(space.params)) == val_align);
632632
assert!(mem::align_of_val(map_maybe_uninit!(space.ret)) == val_align);
633633

634-
self.with_lower_context(store.as_context_mut(), false, |cx, ty| {
634+
self.with_lower_context(store.as_context_mut(), |cx, ty| {
635635
cx.enter_call();
636636
lower(cx, ty, map_maybe_uninit!(space.params))
637637
})?;
@@ -671,23 +671,20 @@ impl Func {
671671
// is currently required to be 0 or 1 values according to the
672672
// canonical ABI, is saved within the `Store`'s `FuncData`. This'll
673673
// later get used in post-return.
674-
// flags.set_needs_post_return(true);
675674
let val = self.with_lift_context(store.0, |cx, ty| lift(cx, ty, ret))?;
676675

677676
// SAFETY: it's a contract of this function that `LowerReturn` is an
678677
// appropriate representation of the result of this function.
679678
let ret_slice = unsafe { storage_as_slice(ret) };
680679

681-
self.instance.id().get_mut(store.0).post_return_arg_set(
682-
self.index,
680+
Ok((
681+
val,
683682
match ret_slice.len() {
684683
0 => ValRaw::i32(0),
685684
1 => ret_slice[0],
686685
_ => unreachable!(),
687686
},
688-
);
689-
690-
return Ok(val);
687+
))
691688
}
692689

693690
#[doc(hidden)]
@@ -703,7 +700,7 @@ impl Func {
703700
Ok(())
704701
}
705702

706-
pub(crate) fn post_return_impl(&self, mut store: impl AsContextMut) -> Result<()> {
703+
pub(crate) fn post_return_impl(&self, mut store: impl AsContextMut, arg: ValRaw) -> Result<()> {
707704
let mut store = store.as_context_mut();
708705

709706
let index = self.index;
@@ -712,33 +709,9 @@ impl Func {
712709
let (_ty, _def, options) = component.export_lifted_function(index);
713710
let post_return = self.post_return_core_func(store.0);
714711
let flags = vminstance.instance_flags(component.env_component().options[options].instance);
715-
let mut instance = self.instance.id().get_mut(store.0);
716-
let post_return_arg = instance.as_mut().post_return_arg_take(index);
717712

718713
unsafe {
719-
// First assert that the instance is in a "needs post return" state.
720-
// This will ensure that the previous action on the instance was a
721-
// function call above. This flag is only set after a component
722-
// function returns so this also can't be called (as expected)
723-
// during a host import for example.
724-
//
725-
// Note, though, that this assert is not sufficient because it just
726-
// means some function on this instance needs its post-return
727-
// called. We need a precise post-return for a particular function
728-
// which is the second assert here (the `.expect`). That will assert
729-
// that this function itself needs to have its post-return called.
730-
//
731-
// The theory at least is that these two asserts ensure component
732-
// model semantics are upheld where the host properly calls
733-
// `post_return` on the right function despite the call being a
734-
// separate step in the API.
735-
assert!(
736-
flags.needs_post_return(),
737-
"post_return can only be called after a function has previously been called",
738-
);
739-
let post_return_arg = post_return_arg.expect("calling post_return on wrong function");
740-
741-
call_post_return(&mut store, post_return, post_return_arg, flags)?;
714+
call_post_return(&mut store, post_return, arg, flags)?;
742715

743716
let (calls, host_table, _, instance) = store
744717
.0
@@ -866,11 +839,9 @@ impl Func {
866839
fn with_lower_context<T>(
867840
self,
868841
mut store: StoreContextMut<T>,
869-
call_post_return_automatically: bool,
870842
lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>,
871843
) -> Result<()> {
872-
let (options_idx, mut flags, ty, options) = self.abi_info(store.0);
873-
let async_ = options.async_;
844+
let (options_idx, mut flags, ty, _) = self.abi_info(store.0);
874845

875846
// Perform the actual lowering, where while this is running the
876847
// component is forbidden from calling imports.
@@ -882,17 +853,7 @@ impl Func {
882853
let param_ty = InterfaceType::Tuple(cx.types[ty].params);
883854
let result = lower(&mut cx, param_ty);
884855
unsafe { flags.set_may_leave(true) };
885-
result?;
886-
887-
// If needed, flag a post-return call being required as we're about to
888-
// enter wasm and afterwards need a post-return.
889-
unsafe {
890-
if !(call_post_return_automatically && async_) {
891-
flags.set_needs_post_return(true);
892-
}
893-
}
894-
895-
Ok(())
856+
result
896857
}
897858

898859
/// Creates a `LiftContext` using the configuration values with this lifted
@@ -951,11 +912,6 @@ pub(crate) unsafe fn call_post_return(
951912
mut flags: InstanceFlags,
952913
) -> Result<()> {
953914
unsafe {
954-
// Unset the "needs post return" flag now that post-return is being
955-
// processed. This will cause future invocations of this method to
956-
// panic, even if the function call below traps.
957-
flags.set_needs_post_return(false);
958-
959915
// Post return functions are forbidden from calling imports or
960916
// intrinsics.
961917
flags.set_may_leave(false);
@@ -966,7 +922,7 @@ pub(crate) unsafe fn call_post_return(
966922
crate::Func::call_unchecked_raw(
967923
&mut store.as_context_mut(),
968924
func,
969-
std::slice::from_ref(&arg).into(),
925+
core::slice::from_ref(&arg).into(),
970926
)?;
971927
}
972928

crates/wasmtime/src/runtime/component/func/typed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ where
380380
param_count,
381381
host_future_present,
382382
move |func, store, params_out| {
383-
func.with_lower_context(store, true, |cx, ty| lower(cx, ty, params_out))
383+
func.with_lower_context(store, |cx, ty| lower(cx, ty, params_out))
384384
},
385385
move |func, store, results| {
386386
let result = if Return::flatten_count() <= max_results {
@@ -439,7 +439,7 @@ where
439439
// safety requirements of `Lift` and `Lower` on `Params` and `Return` in
440440
// combination with checking the various possible branches here and
441441
// dispatching to appropriately typed functions.
442-
let result = unsafe {
442+
let (result, post_return_arg) = unsafe {
443443
// This type is used as `LowerParams` for `call_raw` which is either
444444
// `Params::Lower` or `ValRaw` representing it's either on the stack
445445
// or it's on the heap. This allocates 1 extra `ValRaw` on the stack
@@ -473,7 +473,7 @@ where
473473
}
474474
}?;
475475

476-
self.func.post_return_impl(store)?;
476+
self.func.post_return_impl(store, post_return_arg)?;
477477

478478
Ok(result)
479479
}

crates/wasmtime/src/runtime/component/store.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,6 @@ impl StoreOpaque {
3434
pub(crate) fn set_trapped(&mut self) {
3535
self.store_data_mut().components.trapped = true;
3636
}
37-
38-
/// Returns `false` if the specified instance may not be entered, regardless
39-
/// of what's on a task's call stack.
40-
///
41-
/// If this returns `true`, the instance may be entered as long as it isn't
42-
/// on the task's call stack, if applicable.
43-
pub(crate) fn may_enter_at_all(&mut self, instance: RuntimeInstance) -> bool {
44-
if self.trapped() {
45-
return false;
46-
}
47-
48-
let flags = self
49-
.component_instance(instance.instance)
50-
.instance_flags(instance.index);
51-
52-
unsafe { !flags.needs_post_return() }
53-
}
5437
}
5538

5639
impl StoreData {

crates/wasmtime/src/runtime/vm/component.rs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,6 @@ pub struct ComponentInstance {
148148
/// Self-pointer back to `Store<T>` and its functions.
149149
store: VMStoreRawPtr,
150150

151-
/// Cached ABI return value from the last-invoked function call along with
152-
/// the function index that was invoked.
153-
///
154-
/// Used in `post_return_arg_set` and `post_return_arg_take` below.
155-
post_return_arg: Option<(ExportIndex, ValRaw)>,
156-
157151
/// Required by `InstanceLayout`, also required to be the last field (with
158152
/// repr(C))
159153
vmctx: OwnedVMContext<VMComponentContext>,
@@ -341,7 +335,6 @@ impl ComponentInstance {
341335
resource_types,
342336
imports: imports.clone(),
343337
store: VMStoreRawPtr(store),
344-
post_return_arg: None,
345338
vmctx: OwnedVMContext::new(),
346339
})?;
347340
unsafe {
@@ -954,50 +947,6 @@ impl ComponentInstance {
954947
}
955948
}
956949

957-
/// Sets the cached argument for the canonical ABI option `post-return` to
958-
/// the `arg` specified.
959-
///
960-
/// This function is used in conjunction with function calls to record,
961-
/// after a function call completes, the optional ABI return value. This
962-
/// return value is cached within this instance for future use when the
963-
/// `post_return` Rust-API-level function is invoked.
964-
///
965-
/// Note that `index` here is the index of the export that was just
966-
/// invoked, and this is used to ensure that `post_return` is called on the
967-
/// same function afterwards. This restriction technically isn't necessary
968-
/// though and may be one we want to lift in the future.
969-
///
970-
/// # Panics
971-
///
972-
/// This function will panic if `post_return_arg` is already set to `Some`.
973-
pub fn post_return_arg_set(self: Pin<&mut Self>, index: ExportIndex, arg: ValRaw) {
974-
assert!(self.post_return_arg.is_none());
975-
*self.post_return_arg_mut() = Some((index, arg));
976-
}
977-
978-
/// Re-acquires the value originally saved via `post_return_arg_set`.
979-
///
980-
/// This function will take a function `index` that's having its
981-
/// `post_return` function called. If an argument was previously stored and
982-
/// `index` matches the index that was stored then `Some(arg)` is returned.
983-
/// Otherwise `None` is returned.
984-
pub fn post_return_arg_take(self: Pin<&mut Self>, index: ExportIndex) -> Option<ValRaw> {
985-
let post_return_arg = self.post_return_arg_mut();
986-
let (expected_index, arg) = post_return_arg.take()?;
987-
if index != expected_index {
988-
*post_return_arg = Some((expected_index, arg));
989-
None
990-
} else {
991-
Some(arg)
992-
}
993-
}
994-
995-
fn post_return_arg_mut(self: Pin<&mut Self>) -> &mut Option<(ExportIndex, ValRaw)> {
996-
// SAFETY: we've chosen the `Pin` guarantee of `Self` to not apply to
997-
// the map returned.
998-
unsafe { &mut self.get_unchecked_mut().post_return_arg }
999-
}
1000-
1001950
pub(crate) fn check_may_leave(
1002951
&self,
1003952
instance: RuntimeComponentInstanceIndex,
@@ -1124,22 +1073,6 @@ impl InstanceFlags {
11241073
}
11251074
}
11261075

1127-
#[inline]
1128-
pub unsafe fn needs_post_return(&self) -> bool {
1129-
unsafe { *self.as_raw().as_ref().as_i32() & FLAG_NEEDS_POST_RETURN != 0 }
1130-
}
1131-
1132-
#[inline]
1133-
pub unsafe fn set_needs_post_return(&mut self, val: bool) {
1134-
unsafe {
1135-
if val {
1136-
*self.as_raw().as_mut().as_i32_mut() |= FLAG_NEEDS_POST_RETURN;
1137-
} else {
1138-
*self.as_raw().as_mut().as_i32_mut() &= !FLAG_NEEDS_POST_RETURN;
1139-
}
1140-
}
1141-
}
1142-
11431076
#[inline]
11441077
pub fn as_raw(&self) -> NonNull<VMGlobalDefinition> {
11451078
self.0.as_non_null()

0 commit comments

Comments
 (0)