Skip to content

Override component model Lower::store_list and Lift::load_list for f32/f64 #9892

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

Merged
Merged
Changes from all commits
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
55 changes: 55 additions & 0 deletions crates/wasmtime/src/runtime/component/func/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{AsContextMut, StoreContext, StoreContextMut, ValRaw};
use alloc::borrow::Cow;
use alloc::sync::Arc;
use core::fmt;
use core::iter;
use core::marker;
use core::mem::{self, MaybeUninit};
use core::ptr::NonNull;
Expand Down Expand Up @@ -942,6 +943,39 @@ macro_rules! floats {
*ptr = self.to_bits().to_le_bytes();
Ok(())
}

fn store_list<T>(
cx: &mut LowerContext<'_, T>,
ty: InterfaceType,
offset: usize,
items: &[Self],
) -> Result<()> {
debug_assert!(matches!(ty, InterfaceType::$ty));

// Double-check that the CM alignment is at least the host's
// alignment for this type which should be true for all
// platforms.
assert!((Self::ALIGN32 as usize) >= mem::align_of::<Self>());

// Slice `cx`'s memory to the window that we'll be modifying.
// This should all have already been verified in terms of
// alignment and sizing meaning that these assertions here are
// not truly necessary but are instead double-checks.
let dst = &mut cx.as_slice_mut()[offset..][..items.len() * Self::SIZE32];
assert!(dst.as_ptr().cast::<Self>().is_aligned());

// And with all that out of the way perform the copying loop.
// This is not a `copy_from_slice` because endianness needs to
// be handled here, but LLVM should pretty easily transform this
// into a memcpy on little-endian platforms.
// TODO use `as_chunks` when https://github.com/rust-lang/rust/issues/74985
// is stabilized
for (dst, src) in iter::zip(dst.chunks_exact_mut(Self::SIZE32), items) {
let dst: &mut [u8; Self::SIZE32] = dst.try_into().unwrap();
*dst = src.to_le_bytes();
}
Ok(())
}
}

unsafe impl Lift for $float {
Expand All @@ -957,6 +991,27 @@ macro_rules! floats {
debug_assert!((bytes.as_ptr() as usize) % Self::SIZE32 == 0);
Ok($float::from_le_bytes(bytes.try_into().unwrap()))
}

fn load_list(cx: &mut LiftContext<'_>, list: &WasmList<Self>) -> Result<Vec<Self>> where Self: Sized {
// See comments in `WasmList::get` for the panicking indexing
let byte_size = list.len * mem::size_of::<Self>();
let bytes = &cx.memory()[list.ptr..][..byte_size];

// The canonical ABI requires that everything is aligned to its
// own size, so this should be an aligned array.
assert!(bytes.as_ptr().cast::<Self>().is_aligned());

// Copy the resulting slice to a new Vec, handling endianness
// in the process
// TODO use `as_chunks` when https://github.com/rust-lang/rust/issues/74985
// is stabilized
Ok(
bytes
.chunks_exact(Self::SIZE32)
.map(|i| $float::from_le_bytes(i.try_into().unwrap()))
.collect()
)
}
}
};)*)
}
Expand Down