Is it valid for a module to import the same symbol multiple times?
So long as they all have the same signature that should be fine yeah
Should wit-component
be able to handle that?
It seems to explicitly validate against it: https://github.com/bytecodealliance/wasm-tools/blob/2fdc2ed0de54e55f57ae0bc88273d2b674ce9bf4/crates/wit-component/src/validation.rs#L172
context: Grain 0.6.6 has duplicate imports for a hello world module
causing this to fail: https://github.com/fermyon/spin/blob/82008342a3ec8de7d0ac6f3850828b463c692160/crates/componentize/src/lib.rs#L174-L178
Ah ok different layer, no at the component level core modules must have a single import per module/name combo
I thought you meant the LLD/linking level where this is allowed
OK so this is just something you can't handle by adapting?
wasmtime run
can run the (preview1) module directly
correct, the component model has a validation predicate for core wasm modules which disallows this
yes that's where this is a valid core wasm thing just not a valid thing in the component model
there's quite a long discussion about this at https://github.com/WebAssembly/design/issues/1402 which ended up with today's rule where core wasm wasn't changed but the component model has extra validation
I don't suppose there is existing code to dedupe imports?
not currently, no, but with wasm-encoder's new support for reencoding it might not be too too hard to whip up something
:thumbs_up: conceptually it shouldn't be hard for this particular case; all the duplicate imports have the same type index
this is a simple possible implementation, although I've not tested it much but shows the shape of what such a transform might be
Thanks!
Should I take that as an invitation to PR this into wasm-tools? :smile:
heh perhaps yeah, I haven't thought really about this in the context of wasm-tools
there's all sorts of possible transformations you can do on wasm files and I'm not sure we'd want to pack them all into one tool which would probably have relatively sparse testing
that being said I also don't really see a reason to not do it either, and it's easy enough to add tests and it shouldn't be too much of a burden to keep things compiling
it might make sense to implement this as like wasm-tools transform foo.wasm --name --of --various --passes --to --apply
where CLI flags such as --dedupe-imports
would be allowed (or something like that)
or maybe --pass dedupe-imports,validate,...
where each step would take wasm an output wasm and that way we could pack future things in there relatively easily
one thing I implemented long long ago for wasm modules was a "gc" pass which would remove unneeded imports/functions/globals/etc and that might be kind of neat to reimplement with wasm-encoder nowadays and it might be much easier
https://crates.io/crates/wasm-gc
Last updated: Nov 22 2024 at 16:03 UTC