Refactor and optimize the flat type calculations (#4708)

* Optimize flat type representation calculations

Previously calculating the flat type representation would be done
recursively for an entire type tree every time it was visited.
Additionally the flat type representation was entirely built only to be
thrown away if it was too large at the end. This chiefly presented a
source of recursion based on the type structure in the component model
which fuzzing does not like as it reports stack overflows.

This commit overhauls the representation of flat types in Wasmtime by
caching the representation for each type in the compile-time
`ComponentTypesBuilder` structure. This avoids recalculating each time
the flat representation is queried and additionally allows opportunity
to have more short-circuiting to avoid building overly-large vectors.

* Remove duplicate flat count calculation in wasmtime

Roughly share the infrastructure in the `wasmtime-environ` crate, namely
the non-recursive and memoizing nature of the calculation.

* Fix component fuzz build

* Fix example compile
This commit is contained in:
Alex Crichton
2022-08-16 13:31:47 -05:00
committed by GitHub
parent 3c1490dd59
commit bc8e36a6af
14 changed files with 561 additions and 279 deletions

View File

@@ -1,9 +1,10 @@
//! Size, align, and flattening information about component model types.
use crate::component::{ComponentTypes, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS};
use crate::component::{
ComponentTypesBuilder, FlatType, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS,
};
use crate::fact::{AdapterOptions, Context, Options};
use wasm_encoder::ValType;
use wasmtime_component_util::FlagsSize;
/// Metadata about a core wasm signature which is created for a component model
/// signature.
@@ -23,7 +24,7 @@ pub struct Signature {
pub results_indirect: bool,
}
impl ComponentTypes {
impl ComponentTypesBuilder {
/// Calculates the core wasm function signature for the component function
/// type specified within `Context`.
///
@@ -34,31 +35,39 @@ impl ComponentTypes {
let ty = &self[options.ty];
let ptr_ty = options.options.ptr();
let mut params = self.flatten_types(&options.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 params = match self.flatten_types(
&options.options,
MAX_FLAT_PARAMS,
ty.params.iter().map(|(_, ty)| *ty),
) {
Some(list) => list,
None => {
params_indirect = true;
vec![ptr_ty]
}
};
let mut results = self.flatten_types(&options.options, [ty.result]);
let mut results_indirect = false;
if results.len() > MAX_FLAT_RESULTS {
results_indirect = true;
match context {
// For a lifted function too-many-results gets translated to a
// returned pointer where results are read from. The callee
// allocates space here.
Context::Lift => results = vec![ptr_ty],
// For a lowered function too-many-results becomes a return
// pointer which is passed as the last argument. The caller
// allocates space here.
Context::Lower => {
results.truncate(0);
params.push(ptr_ty);
let results = match self.flatten_types(&options.options, MAX_FLAT_RESULTS, [ty.result]) {
Some(list) => list,
None => {
results_indirect = true;
match context {
// For a lifted function too-many-results gets translated to a
// returned pointer where results are read from. The callee
// allocates space here.
Context::Lift => vec![ptr_ty],
// For a lowered function too-many-results becomes a return
// pointer which is passed as the last argument. The caller
// allocates space here.
Context::Lower => {
params.push(ptr_ty);
Vec::new()
}
}
}
}
};
Signature {
params,
results,
@@ -72,115 +81,31 @@ impl ComponentTypes {
pub(super) fn flatten_types(
&self,
opts: &Options,
max: usize,
tys: impl IntoIterator<Item = InterfaceType>,
) -> Vec<ValType> {
let mut result = Vec::new();
) -> Option<Vec<ValType>> {
let mut dst = Vec::new();
for ty in tys {
self.push_flat(opts, &ty, &mut result);
}
result
}
fn push_flat(&self, opts: &Options, ty: &InterfaceType, dst: &mut Vec<ValType>) {
match ty {
InterfaceType::Unit => {}
InterfaceType::Bool
| InterfaceType::S8
| InterfaceType::U8
| InterfaceType::S16
| InterfaceType::U16
| InterfaceType::S32
| InterfaceType::U32
| InterfaceType::Char => dst.push(ValType::I32),
InterfaceType::S64 | InterfaceType::U64 => dst.push(ValType::I64),
InterfaceType::Float32 => dst.push(ValType::F32),
InterfaceType::Float64 => dst.push(ValType::F64),
InterfaceType::String | InterfaceType::List(_) => {
dst.push(opts.ptr());
dst.push(opts.ptr());
}
InterfaceType::Record(r) => {
for field in self[*r].fields.iter() {
self.push_flat(opts, &field.ty, dst);
let flat = self.flat_types(&ty)?;
let types = if opts.memory64 {
flat.memory64
} else {
flat.memory32
};
for ty in types {
let ty = match ty {
FlatType::I32 => ValType::I32,
FlatType::I64 => ValType::I64,
FlatType::F32 => ValType::F32,
FlatType::F64 => ValType::F64,
};
if dst.len() == max {
return None;
}
}
InterfaceType::Tuple(t) => {
for ty in self[*t].types.iter() {
self.push_flat(opts, ty, dst);
}
}
InterfaceType::Flags(f) => {
let flags = &self[*f];
match FlagsSize::from_count(flags.names.len()) {
FlagsSize::Size0 => {}
FlagsSize::Size1 | FlagsSize::Size2 => dst.push(ValType::I32),
FlagsSize::Size4Plus(n) => {
dst.extend((0..n).map(|_| ValType::I32));
}
}
}
InterfaceType::Enum(_) => dst.push(ValType::I32),
InterfaceType::Option(t) => {
dst.push(ValType::I32);
self.push_flat(opts, &self[*t].ty, dst);
}
InterfaceType::Variant(t) => {
dst.push(ValType::I32);
let pos = dst.len();
let mut tmp = Vec::new();
for case in self[*t].cases.iter() {
self.push_flat_variant(opts, &case.ty, pos, &mut tmp, dst);
}
}
InterfaceType::Union(t) => {
dst.push(ValType::I32);
let pos = dst.len();
let mut tmp = Vec::new();
for ty in self[*t].types.iter() {
self.push_flat_variant(opts, ty, pos, &mut tmp, dst);
}
}
InterfaceType::Expected(t) => {
dst.push(ValType::I32);
let e = &self[*t];
let pos = dst.len();
let mut tmp = Vec::new();
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(opts, ty, tmp);
for (i, a) in tmp.iter().enumerate() {
match dst.get_mut(pos + i) {
Some(b) => join(*a, b),
None => dst.push(*a),
}
}
fn join(a: ValType, b: &mut ValType) {
if a == *b {
return;
}
match (a, *b) {
(ValType::I32, ValType::F32) | (ValType::F32, ValType::I32) => *b = ValType::I32,
_ => *b = ValType::I64,
dst.push(ty);
}
}
Some(dst)
}
pub(super) fn align(&self, opts: &Options, ty: &InterfaceType) -> u32 {