Skip to content

Commit

Permalink
Update wasm-tools dependencies (#1036)
Browse files Browse the repository at this point in the history
* Update wasm-tools dependencies

This notably updates to handle bytecodealliance/wasm-tools#1711 but this
is done in a way that is not intended to replace #1035 but instead makes
the update "as easy as possible" by just adding calls to
`.{size,align}_wasm32()` where needed. Full support is expected to land
in #1035.

* More sizing updates
  • Loading branch information
alexcrichton authored Aug 23, 2024
1 parent dd41e41 commit 04cef2f
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 188 deletions.
327 changes: 186 additions & 141 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ indexmap = "2.0.0"
prettyplease = "0.2.20"
syn = { version = "2.0", features = ["printing"] }

wasmparser = "0.215.0"
wasm-encoder = "0.215.0"
wasm-metadata = "0.215.0"
wit-parser = "0.215.0"
wit-component = "0.215.0"
wasmparser = "0.216.0"
wasm-encoder = "0.216.0"
wasm-metadata = "0.216.0"
wit-parser = "0.216.0"
wit-component = "0.216.0"

wit-bindgen-core = { path = 'crates/core', version = '0.30.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.30.0' }
Expand Down
2 changes: 1 addition & 1 deletion crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3028,7 +3028,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwriteln!(self.src, "uint8_t *{ptr} = {};", operands[0]);
let i = self.locals.tmp("i");
uwriteln!(self.src, "for (size_t {i} = 0; {i} < {len}; {i}++) {{");
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
uwriteln!(self.src, "uint8_t *base = {ptr} + {i} * {size};");
uwriteln!(self.src, "(void) base;");
uwrite!(self.src, "{body}");
Expand Down
68 changes: 46 additions & 22 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,31 +800,33 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// ... otherwise if parameters are indirect space is
// allocated from them and each argument is lowered
// individually into memory.
let (size, align) = self
let info = self
.bindgen
.sizes()
.record(func.params.iter().map(|t| &t.1));
let ptr = match self.variant {
// When a wasm module calls an import it will provide
// space that isn't explicitly deallocated.
AbiVariant::GuestImport => self.bindgen.return_pointer(size, align),
AbiVariant::GuestImport => self
.bindgen
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32()),
// When calling a wasm module from the outside, though,
// malloc needs to be called.
AbiVariant::GuestExport => {
self.emit(&Instruction::Malloc {
realloc: "cabi_realloc",
size,
align,
size: info.size.size_wasm32(),
align: info.align.align_wasm32(),
});
self.stack.pop().unwrap()
}
};
let mut offset = 0usize;
for (nth, (_, ty)) in func.params.iter().enumerate() {
self.emit(&Instruction::GetArg { nth });
offset = align_to(offset, self.bindgen.sizes().align(ty));
offset = align_to(offset, self.bindgen.sizes().align(ty).align_wasm32());
self.write_to_memory(ty, ptr.clone(), offset as i32);
offset += self.bindgen.sizes().size(ty);
offset += self.bindgen.sizes().size(ty).size_wasm32();
}

self.stack.push(ptr);
Expand All @@ -833,8 +835,10 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// If necessary we may need to prepare a return pointer for
// this ABI.
if self.variant == AbiVariant::GuestImport && sig.retptr {
let (size, align) = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self.bindgen.return_pointer(size, align);
let info = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self
.bindgen
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32());
self.return_pointer = Some(ptr.clone());
self.stack.push(ptr);
}
Expand Down Expand Up @@ -904,9 +908,9 @@ impl<'a, B: Bindgen> Generator<'a, B> {
self.emit(&Instruction::GetArg { nth: 0 });
let ptr = self.stack.pop().unwrap();
for (_, ty) in func.params.iter() {
offset = align_to(offset, self.bindgen.sizes().align(ty));
offset = align_to(offset, self.bindgen.sizes().align(ty).align_wasm32());
self.read_from_memory(ty, ptr.clone(), offset as i32);
offset += self.bindgen.sizes().size(ty);
offset += self.bindgen.sizes().size(ty).size_wasm32();
}
}

Expand All @@ -917,12 +921,15 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// it's been read by the guest we need to deallocate it.
if let AbiVariant::GuestExport = self.variant {
if sig.indirect_params {
let (size, align) = self
let info = self
.bindgen
.sizes()
.record(func.params.iter().map(|t| &t.1));
self.emit(&Instruction::GetArg { nth: 0 });
self.emit(&Instruction::GuestDeallocate { size, align });
self.emit(&Instruction::GuestDeallocate {
size: info.size.size_wasm32(),
align: info.align.align_wasm32(),
});
}
}

Expand Down Expand Up @@ -959,9 +966,10 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// (statically) and then write the result into that
// memory, returning the pointer at the end.
AbiVariant::GuestExport => {
let (size, align) =
self.bindgen.sizes().params(func.results.iter_types());
let ptr = self.bindgen.return_pointer(size, align);
let info = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self
.bindgen
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32());
self.write_params_to_memory(func.results.iter_types(), ptr.clone(), 0);
self.stack.push(ptr);
}
Expand Down Expand Up @@ -998,6 +1006,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
.sizes()
.field_offsets(func.results.iter_types())
{
let offset = offset.size_wasm32();
let offset = i32::try_from(offset).unwrap();
self.deallocate(ty, addr.clone(), offset);
}
Expand Down Expand Up @@ -1535,8 +1544,12 @@ impl<'a, B: Bindgen> Generator<'a, B> {
tag: Int,
cases: impl IntoIterator<Item = Option<&'b Type>> + Clone,
) {
let payload_offset =
offset + (self.bindgen.sizes().payload_offset(tag, cases.clone()) as i32);
let payload_offset = offset
+ (self
.bindgen
.sizes()
.payload_offset(tag, cases.clone())
.size_wasm32() as i32);
for (i, ty) in cases.into_iter().enumerate() {
self.push_block();
self.emit(&Instruction::VariantPayloadName);
Expand Down Expand Up @@ -1581,6 +1594,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
.zip(fields)
{
self.stack.push(op);
let field_offset = field_offset.size_wasm32();
self.write_to_memory(ty, addr.clone(), offset + (field_offset as i32));
}
}
Expand Down Expand Up @@ -1715,8 +1729,12 @@ impl<'a, B: Bindgen> Generator<'a, B> {
) {
self.stack.push(addr.clone());
self.load_intrepr(offset, tag);
let payload_offset =
offset + (self.bindgen.sizes().payload_offset(tag, cases.clone()) as i32);
let payload_offset = offset
+ (self
.bindgen
.sizes()
.payload_offset(tag, cases.clone())
.size_wasm32() as i32);
for ty in cases {
self.push_block();
if let Some(ty) = ty {
Expand All @@ -1743,7 +1761,8 @@ impl<'a, B: Bindgen> Generator<'a, B> {
offset: i32,
) {
for (field_offset, ty) in self.bindgen.sizes().field_offsets(tys).iter() {
self.read_from_memory(ty, addr.clone(), offset + (*field_offset as i32));
let field_offset = field_offset.size_wasm32();
self.read_from_memory(ty, addr.clone(), offset + (field_offset as i32));
}
}

Expand Down Expand Up @@ -1882,8 +1901,12 @@ impl<'a, B: Bindgen> Generator<'a, B> {
) {
self.stack.push(addr.clone());
self.load_intrepr(offset, tag);
let payload_offset =
offset + (self.bindgen.sizes().payload_offset(tag, cases.clone()) as i32);
let payload_offset = offset
+ (self
.bindgen
.sizes()
.payload_offset(tag, cases.clone())
.size_wasm32() as i32);
for ty in cases {
self.push_block();
if let Some(ty) = ty {
Expand All @@ -1895,6 +1918,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {

fn deallocate_fields(&mut self, tys: &[Type], addr: B::Operand, offset: i32) {
for (field_offset, ty) in self.bindgen.sizes().field_offsets(tys) {
let field_offset = field_offset.size_wasm32();
self.deallocate(ty, addr.clone(), offset + (field_offset as i32));
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let address = self.locals.tmp("address");
let buffer = self.locals.tmp("buffer");
let gc_handle = self.locals.tmp("gcHandle");
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
uwrite!(
self.src,
"
Expand Down Expand Up @@ -2597,7 +2597,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
assert!(block_results.is_empty());

let list = &operands[0];
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let ty = self.gen.type_name_with_qualifier(element, true);
let index = self.locals.tmp("index");

Expand Down Expand Up @@ -2641,7 +2641,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let length = &operands[1];
let array = self.locals.tmp("array");
let ty = self.gen.type_name_with_qualifier(element, true);
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let index = self.locals.tmp("index");

let result = match &block_results[..] {
Expand Down
8 changes: 4 additions & 4 deletions crates/moonbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2026,8 +2026,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
assert!(block_results.is_empty());

let op = &operands[0];
let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();
let address = self.locals.tmp("address");
let ty = self.gen.type_name(element, true);
let index = self.locals.tmp("index");
Expand Down Expand Up @@ -2067,7 +2067,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let length = &operands[1];
let array = self.locals.tmp("array");
let ty = self.gen.type_name(element, true);
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
// let align = self.gen.gen.sizes.align(element);
let index = self.locals.tmp("index");

Expand Down Expand Up @@ -2364,7 +2364,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let address = &operands[0];
let length = &operands[1];

let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
// let align = self.gen.gen.sizes.align(element);

if !body.trim().is_empty() {
Expand Down
12 changes: 6 additions & 6 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
operand0 = operands[0]
));
self.push_str(&format!("let {len} = {vec}.len();\n"));
let size = self.gen.sizes.size(element);
let align = self.gen.sizes.align(element);
let size = self.gen.sizes.size(element).size_wasm32();
let align = self.gen.sizes.align(element).align_wasm32();
self.push_str(&format!(
"let {layout} = {alloc}::Layout::from_size_align_unchecked({vec}.len() * {size}, {align});\n",
));
Expand Down Expand Up @@ -746,8 +746,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::ListLift { element, .. } => {
let body = self.blocks.pop().unwrap();
let tmp = self.tmp();
let size = self.gen.sizes.size(element);
let align = self.gen.sizes.align(element);
let size = self.gen.sizes.size(element).size_wasm32();
let align = self.gen.sizes.align(element).align_wasm32();
let len = format!("len{tmp}");
let base = format!("base{tmp}");
let result = format!("result{tmp}");
Expand Down Expand Up @@ -1038,8 +1038,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::GuestDeallocateList { element } => {
let body = self.blocks.pop().unwrap();
let tmp = self.tmp();
let size = self.gen.sizes.size(element);
let align = self.gen.sizes.align(element);
let size = self.gen.sizes.size(element).size_wasm32();
let align = self.gen.sizes.align(element).align_wasm32();
let len = format!("len{tmp}");
let base = format!("base{tmp}");
self.push_str(&format!(
Expand Down
12 changes: 6 additions & 6 deletions crates/teavm-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,8 +1667,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
assert!(block_results.is_empty());

let op = &operands[0];
let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();
let address = self.locals.tmp("address");
let ty = self.gen.type_name(element);
let index = self.locals.tmp("index");
Expand Down Expand Up @@ -1708,8 +1708,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let length = &operands[1];
let array = self.locals.tmp("array");
let ty = self.gen.type_name(element);
let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();
let index = self.locals.tmp("index");

let result = match &block_results[..] {
Expand Down Expand Up @@ -2006,8 +2006,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let address = &operands[0];
let length = &operands[1];

let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();

if !body.trim().is_empty() {
let index = self.locals.tmp("index");
Expand Down

0 comments on commit 04cef2f

Please sign in to comment.