Implement fused adapters for (list T) types (#4558)
* Implement fused adapters for `(list T)` types This commit implements one of the two remaining types for adapter fusion, lists. This implementation is particularly tricky for a number of reasons: * Lists have a number of validity checks which need to be carefully implemented. For example the byte length of the list passed to allocation in the destination module could overflow the 32-bit index space. Additionally lists in 32-bit memories need a check that their final address is in-bounds in the address space. * In the effort to go ahead and support memory64 at the lowest layers this is where much of the magic happens. Lists are naturally always stored in memory and shifting between 64/32-bit address spaces is done here. This notably required plumbing an `Options` around during flattening/size/alignment calculations due to the size/types of lists changing depending on the memory configuration. I've also added a small `factc` program in this commit which should hopefully assist in exploring and debugging adapter modules. This takes as input a component (text or binary format) and then generates an adapter module for all component function signatures found internally. This commit notably does not include tests for lists. I tried to figure out a good way to add these but I felt like there were too many cases to test and the tests would otherwise be extremely verbose. Instead I think the best testing strategy for this commit will be through #4537 which should be relatively extensible to testing adapters between modules in addition to host-based lifting/lowering. * Improve handling of lists of 0-size types * Skip overflow checks on byte sizes for 0-size types * Skip the copy loop entirely when src/dst are both 0 * Skip the increments of src/dst pointers if either is 0-size * Update semantics for zero-sized lists/strings When a list/string has a 0-byte-size the base pointer is no longer verified to be in-bounds to match the supposedly desired adapter semantics where no trap happens because no turn of the loop happens.
This commit is contained in:
@@ -38,14 +38,14 @@ impl Module<'_> {
|
||||
let ty = &self.types[options.ty];
|
||||
let ptr_ty = options.ptr();
|
||||
|
||||
let mut params = self.flatten_types(ty.params.iter().map(|(_, ty)| *ty));
|
||||
let mut params = self.flatten_types(options, ty.params.iter().map(|(_, ty)| *ty));
|
||||
let mut params_indirect = false;
|
||||
if params.len() > MAX_FLAT_PARAMS {
|
||||
params = vec![ptr_ty];
|
||||
params_indirect = true;
|
||||
}
|
||||
|
||||
let mut results = self.flatten_types([ty.result]);
|
||||
let mut results = self.flatten_types(options, [ty.result]);
|
||||
let mut results_indirect = false;
|
||||
if results.len() > MAX_FLAT_RESULTS {
|
||||
results_indirect = true;
|
||||
@@ -73,18 +73,19 @@ impl Module<'_> {
|
||||
|
||||
/// Pushes the flat version of a list of component types into a final result
|
||||
/// list.
|
||||
pub(crate) fn flatten_types(
|
||||
pub(super) fn flatten_types(
|
||||
&self,
|
||||
opts: &Options,
|
||||
tys: impl IntoIterator<Item = InterfaceType>,
|
||||
) -> Vec<ValType> {
|
||||
let mut result = Vec::new();
|
||||
for ty in tys {
|
||||
self.push_flat(&ty, &mut result);
|
||||
self.push_flat(opts, &ty, &mut result);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn push_flat(&self, ty: &InterfaceType, dst: &mut Vec<ValType>) {
|
||||
fn push_flat(&self, opts: &Options, ty: &InterfaceType, dst: &mut Vec<ValType>) {
|
||||
match ty {
|
||||
InterfaceType::Unit => {}
|
||||
|
||||
@@ -103,17 +104,17 @@ impl Module<'_> {
|
||||
InterfaceType::Float64 => dst.push(ValType::F64),
|
||||
|
||||
InterfaceType::String | InterfaceType::List(_) => {
|
||||
dst.push(ValType::I32);
|
||||
dst.push(ValType::I32);
|
||||
dst.push(opts.ptr());
|
||||
dst.push(opts.ptr());
|
||||
}
|
||||
InterfaceType::Record(r) => {
|
||||
for field in self.types[*r].fields.iter() {
|
||||
self.push_flat(&field.ty, dst);
|
||||
self.push_flat(opts, &field.ty, dst);
|
||||
}
|
||||
}
|
||||
InterfaceType::Tuple(t) => {
|
||||
for ty in self.types[*t].types.iter() {
|
||||
self.push_flat(ty, dst);
|
||||
self.push_flat(opts, ty, dst);
|
||||
}
|
||||
}
|
||||
InterfaceType::Flags(f) => {
|
||||
@@ -126,14 +127,14 @@ impl Module<'_> {
|
||||
InterfaceType::Enum(_) => dst.push(ValType::I32),
|
||||
InterfaceType::Option(t) => {
|
||||
dst.push(ValType::I32);
|
||||
self.push_flat(&self.types[*t], dst);
|
||||
self.push_flat(opts, &self.types[*t], dst);
|
||||
}
|
||||
InterfaceType::Variant(t) => {
|
||||
dst.push(ValType::I32);
|
||||
let pos = dst.len();
|
||||
let mut tmp = Vec::new();
|
||||
for case in self.types[*t].cases.iter() {
|
||||
self.push_flat_variant(&case.ty, pos, &mut tmp, dst);
|
||||
self.push_flat_variant(opts, &case.ty, pos, &mut tmp, dst);
|
||||
}
|
||||
}
|
||||
InterfaceType::Union(t) => {
|
||||
@@ -141,7 +142,7 @@ impl Module<'_> {
|
||||
let pos = dst.len();
|
||||
let mut tmp = Vec::new();
|
||||
for ty in self.types[*t].types.iter() {
|
||||
self.push_flat_variant(ty, pos, &mut tmp, dst);
|
||||
self.push_flat_variant(opts, ty, pos, &mut tmp, dst);
|
||||
}
|
||||
}
|
||||
InterfaceType::Expected(t) => {
|
||||
@@ -149,21 +150,22 @@ impl Module<'_> {
|
||||
let e = &self.types[*t];
|
||||
let pos = dst.len();
|
||||
let mut tmp = Vec::new();
|
||||
self.push_flat_variant(&e.ok, pos, &mut tmp, dst);
|
||||
self.push_flat_variant(&e.err, pos, &mut tmp, dst);
|
||||
self.push_flat_variant(opts, &e.ok, pos, &mut tmp, dst);
|
||||
self.push_flat_variant(opts, &e.err, pos, &mut tmp, dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn push_flat_variant(
|
||||
&self,
|
||||
opts: &Options,
|
||||
ty: &InterfaceType,
|
||||
pos: usize,
|
||||
tmp: &mut Vec<ValType>,
|
||||
dst: &mut Vec<ValType>,
|
||||
) {
|
||||
tmp.truncate(0);
|
||||
self.push_flat(ty, tmp);
|
||||
self.push_flat(opts, ty, tmp);
|
||||
for (i, a) in tmp.iter().enumerate() {
|
||||
match dst.get_mut(pos + i) {
|
||||
Some(b) => join(*a, b),
|
||||
@@ -182,8 +184,8 @@ impl Module<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn align(&self, ty: &InterfaceType) -> usize {
|
||||
self.size_align(ty).1
|
||||
pub(super) fn align(&self, opts: &Options, ty: &InterfaceType) -> usize {
|
||||
self.size_align(opts, ty).1
|
||||
}
|
||||
|
||||
/// Returns a (size, align) pair corresponding to the byte-size and
|
||||
@@ -191,7 +193,7 @@ impl Module<'_> {
|
||||
//
|
||||
// TODO: this is probably inefficient to entire recalculate at all phases,
|
||||
// seems like it would be best to intern this in some sort of map somewhere.
|
||||
pub(crate) fn size_align(&self, ty: &InterfaceType) -> (usize, usize) {
|
||||
pub(super) fn size_align(&self, opts: &Options, ty: &InterfaceType) -> (usize, usize) {
|
||||
match ty {
|
||||
InterfaceType::Unit => (0, 1),
|
||||
InterfaceType::Bool | InterfaceType::S8 | InterfaceType::U8 => (1, 1),
|
||||
@@ -201,12 +203,14 @@ impl Module<'_> {
|
||||
| InterfaceType::Char
|
||||
| InterfaceType::Float32 => (4, 4),
|
||||
InterfaceType::S64 | InterfaceType::U64 | InterfaceType::Float64 => (8, 8),
|
||||
InterfaceType::String | InterfaceType::List(_) => (8, 4),
|
||||
InterfaceType::String | InterfaceType::List(_) => {
|
||||
((2 * opts.ptr_size()).into(), opts.ptr_size().into())
|
||||
}
|
||||
|
||||
InterfaceType::Record(r) => {
|
||||
self.record_size_align(self.types[*r].fields.iter().map(|f| &f.ty))
|
||||
self.record_size_align(opts, self.types[*r].fields.iter().map(|f| &f.ty))
|
||||
}
|
||||
InterfaceType::Tuple(t) => self.record_size_align(self.types[*t].types.iter()),
|
||||
InterfaceType::Tuple(t) => self.record_size_align(opts, self.types[*t].types.iter()),
|
||||
InterfaceType::Flags(f) => match self.types[*f].names.len() {
|
||||
n if n <= 8 => (1, 1),
|
||||
n if n <= 16 => (2, 2),
|
||||
@@ -216,27 +220,28 @@ impl Module<'_> {
|
||||
InterfaceType::Enum(t) => self.discrim_size_align(self.types[*t].names.len()),
|
||||
InterfaceType::Option(t) => {
|
||||
let ty = &self.types[*t];
|
||||
self.variant_size_align([&InterfaceType::Unit, ty].into_iter())
|
||||
self.variant_size_align(opts, [&InterfaceType::Unit, ty].into_iter())
|
||||
}
|
||||
InterfaceType::Variant(t) => {
|
||||
self.variant_size_align(self.types[*t].cases.iter().map(|c| &c.ty))
|
||||
self.variant_size_align(opts, self.types[*t].cases.iter().map(|c| &c.ty))
|
||||
}
|
||||
InterfaceType::Union(t) => self.variant_size_align(self.types[*t].types.iter()),
|
||||
InterfaceType::Union(t) => self.variant_size_align(opts, self.types[*t].types.iter()),
|
||||
InterfaceType::Expected(t) => {
|
||||
let e = &self.types[*t];
|
||||
self.variant_size_align([&e.ok, &e.err].into_iter())
|
||||
self.variant_size_align(opts, [&e.ok, &e.err].into_iter())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn record_size_align<'a>(
|
||||
pub(super) fn record_size_align<'a>(
|
||||
&self,
|
||||
opts: &Options,
|
||||
fields: impl Iterator<Item = &'a InterfaceType>,
|
||||
) -> (usize, usize) {
|
||||
let mut size = 0;
|
||||
let mut align = 1;
|
||||
for ty in fields {
|
||||
let (fsize, falign) = self.size_align(ty);
|
||||
let (fsize, falign) = self.size_align(opts, ty);
|
||||
size = align_to(size, falign) + fsize;
|
||||
align = align.max(falign);
|
||||
}
|
||||
@@ -245,12 +250,13 @@ impl Module<'_> {
|
||||
|
||||
fn variant_size_align<'a>(
|
||||
&self,
|
||||
opts: &Options,
|
||||
cases: impl ExactSizeIterator<Item = &'a InterfaceType>,
|
||||
) -> (usize, usize) {
|
||||
let (discrim_size, mut align) = self.discrim_size_align(cases.len());
|
||||
let mut payload_size = 0;
|
||||
for ty in cases {
|
||||
let (csize, calign) = self.size_align(ty);
|
||||
let (csize, calign) = self.size_align(opts, ty);
|
||||
payload_size = payload_size.max(csize);
|
||||
align = align.max(calign);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user