mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-18 07:21:24 +00:00
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations This commit starts adding a test suite which checks in, to the repository, test assertions for both the JS and wasm file outputs of a Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be exhaustive or large scale tests, but rather micro-tests to help observe the changes in `wasm-bindgen`'s output over time. The motivation for this commit is basically overhauling how all the GC passes work in `wasm-bindgen` today. The reorganization is also included in this commit as well. Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes of `walrus` in a bunch of places to ensure that less "garbage" was seen by future passes. This not only was a source of slowdown but it also was pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked through. The strategy taken in this commit is to have one precise location for a GC pass, and everything goes through there. This is achieved by: * All internal exports are removed immediately when generating the nonstandard wasm interface types section. Internal exports, intrinsics, and runtime support are all referenced by the various instructions and/or sections that use them. This means that we now have precise tracking of what an adapter uses. * This in turn enables us to implement the `add_gc_roots` function for `walrus` custom sections, which in turn allows walrus GC passes to do what `unexport_unused_intrinsics` did before. That function is now no longer necessary, but effectively works the same way. All intrinsics are unexported at the beginning and then they're selectively re-imported and re-exported through the JS glue generation pass as necessary and defined by the bindings. * Passes like the `anyref` pass are now much more precise about the intrinsics that they work with. The `anyref` pass also deletes any internal intrinsics found and also does some rewriting of the adapters aftewards now to hook up calls to the heap count import to the heap count intrinsic in the wasm module. * Fix handling of __wbindgen_realloc The final user of the `require_internal_export` function was `__wbindgen_realloc`. This usage has now been removed by updating how we handle usage of the `realloc` function. The wasm interface types standard doesn't have a `realloc` function slot, nor do I think it ever will. This means that as a polyfill for wasm interface types we'll always have to support the lack of `realloc`. For direct Rust to JS, however, we can still optionally handle `realloc`. This is all handled with a few internal changes. * Custom `StringToMemory` instructions now exist. These have an extra `realloc` slot to store an intrinsic, if found. * Our custom instructions are lowered to the standard instructions when generating an interface types section. * The `realloc` function, if present, is passed as an argument like the malloc function when passing strings to wasm. If it's not present we use a slower fallback, but if it's present we use the faster implementation. This should mean that there's little-to-no impact on existing users of `wasm-bindgen`, but this should continue to still work for wasm interface types polyfills and such. Additionally the GC passes now work in that they don't delete `__wbindgen_realloc` which we later try to reference. * Add an empty test for the anyref pass * Precisely track I32FromOptionAnyref's dependencies This depends on the anyref table and a function to allocate an index if the anyref pass is running, so be sure to track that in the instruction itself for GC rooting. * Trim extraneous exports from nop anyref module Or if you're otherwise not using anyref slices, don't force some intrinsics to exist. * Remove globals from reference tests Looks like these values adjust in slight but insignificant ways over time * Update the anyref xform tests
This commit is contained in:
@ -1,3 +1,6 @@
|
||||
use crate::descriptor::VectorKind;
|
||||
use crate::intrinsic::Intrinsic;
|
||||
use crate::wit::AuxImport;
|
||||
use crate::wit::{AdapterKind, Instruction, NonstandardWitSection};
|
||||
use crate::wit::{AdapterType, InstructionData, StackChange, WasmBindgenAux};
|
||||
use anyhow::Error;
|
||||
@ -17,7 +20,7 @@ pub fn process(module: &mut Module) -> Result<(), Error> {
|
||||
.implements
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|(core, adapter)| (adapter, core))
|
||||
.map(|(core, _, adapter)| (adapter, core))
|
||||
.collect::<HashMap<_, _>>();
|
||||
|
||||
// Transform all exported functions in the module, using the bindings listed
|
||||
@ -45,13 +48,73 @@ pub fn process(module: &mut Module) -> Result<(), Error> {
|
||||
|
||||
let meta = cfg.run(module)?;
|
||||
|
||||
let mut aux = module
|
||||
.customs
|
||||
.delete_typed::<WasmBindgenAux>()
|
||||
.expect("wit custom section should exist");
|
||||
let section = module
|
||||
.customs
|
||||
.get_typed_mut::<WasmBindgenAux>()
|
||||
.get_typed_mut::<NonstandardWitSection>()
|
||||
.expect("wit custom section should exist");
|
||||
section.anyref_table = Some(meta.table);
|
||||
section.anyref_alloc = meta.alloc;
|
||||
section.anyref_drop_slice = meta.drop_slice;
|
||||
|
||||
// If the module looks like it's going to use some of these exports, store
|
||||
// them in the aux section to get used.
|
||||
//
|
||||
// FIXME: this is not great, we should ideally have precise tracking of what
|
||||
// requires what. These are used by catch clauses and anyref slices going
|
||||
// in/out of wasm. The catch clauses are a bit weird but anyref slices
|
||||
// should ideally track in their own instructions what table/functions
|
||||
// they're referencing. This doesn't fit well in today's model of
|
||||
// slice-related instructions, though, so let's just cop out and only enable
|
||||
// these coarsely.
|
||||
aux.anyref_table = Some(meta.table);
|
||||
if module_needs_anyref_metadata(&aux, section) {
|
||||
aux.anyref_alloc = meta.alloc;
|
||||
aux.anyref_drop_slice = meta.drop_slice;
|
||||
}
|
||||
|
||||
// Additonally we may need to update some adapter instructions other than
|
||||
// those found for the anyref pass. These are some general "fringe support"
|
||||
// things necessary to get absolutely everything working.
|
||||
for (_, adapter) in section.adapters.iter_mut() {
|
||||
let instrs = match &mut adapter.kind {
|
||||
AdapterKind::Local { instructions } => instructions,
|
||||
AdapterKind::Import { .. } => continue,
|
||||
};
|
||||
for instr in instrs {
|
||||
match instr.instr {
|
||||
// Calls to the heap live count intrinsic are now routed to the
|
||||
// actual wasm function which keeps track of this.
|
||||
Instruction::CallAdapter(adapter) => {
|
||||
let id = match meta.live_count {
|
||||
Some(id) => id,
|
||||
None => continue,
|
||||
};
|
||||
let import = match aux.import_map.get(&adapter) {
|
||||
Some(import) => import,
|
||||
None => continue,
|
||||
};
|
||||
match import {
|
||||
AuxImport::Intrinsic(Intrinsic::AnyrefHeapLiveCount) => {}
|
||||
_ => continue,
|
||||
}
|
||||
instr.instr = Instruction::Standard(wit_walrus::Instruction::CallCore(id));
|
||||
}
|
||||
|
||||
// Optional anyref values are now managed in the wasm module, so
|
||||
// we need to store where they're managed.
|
||||
Instruction::I32FromOptionAnyref {
|
||||
ref mut table_and_alloc,
|
||||
} => {
|
||||
*table_and_alloc = meta.alloc.map(|id| (meta.table, id));
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.customs.add(*aux);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -267,3 +330,55 @@ fn export_xform(cx: &mut Context, export: Export, instrs: &mut Vec<InstructionDa
|
||||
instrs.remove(idx);
|
||||
}
|
||||
}
|
||||
|
||||
/// This function shouldn't need to exist, see the fixme at the call-site.
|
||||
fn module_needs_anyref_metadata(aux: &WasmBindgenAux, section: &NonstandardWitSection) -> bool {
|
||||
use Instruction::*;
|
||||
|
||||
// our `handleError` intrinsic uses a few pieces of metadata to store
|
||||
// indices directly into the wasm module.
|
||||
if aux.imports_with_catch.len() > 0 {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Look for any instructions which may use `VectorKind::Anyref`. If there
|
||||
// are any then we'll need our intrinsics/tables/etc, otherwise we shouldn't
|
||||
// ever need them.
|
||||
section.adapters.iter().any(|(_, adapter)| {
|
||||
let instructions = match &adapter.kind {
|
||||
AdapterKind::Local { instructions } => instructions,
|
||||
AdapterKind::Import { .. } => return false,
|
||||
};
|
||||
instructions.iter().any(|instr| match instr.instr {
|
||||
VectorToMemory {
|
||||
kind: VectorKind::Anyref,
|
||||
..
|
||||
}
|
||||
| MutableSliceToMemory {
|
||||
kind: VectorKind::Anyref,
|
||||
..
|
||||
}
|
||||
| OptionVector {
|
||||
kind: VectorKind::Anyref,
|
||||
..
|
||||
}
|
||||
| VectorLoad {
|
||||
kind: VectorKind::Anyref,
|
||||
..
|
||||
}
|
||||
| OptionVectorLoad {
|
||||
kind: VectorKind::Anyref,
|
||||
..
|
||||
}
|
||||
| View {
|
||||
kind: VectorKind::Anyref,
|
||||
..
|
||||
}
|
||||
| OptionView {
|
||||
kind: VectorKind::Anyref,
|
||||
..
|
||||
} => true,
|
||||
_ => false,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user