Stream: general

Topic: duplicate imports


view this post on Zulip Lann Martin (Aug 27 2024 at 19:53):

Is it valid for a module to import the same symbol multiple times?

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:05):

So long as they all have the same signature that should be fine yeah

view this post on Zulip Lann Martin (Aug 27 2024 at 20:05):

Should wit-component be able to handle that?

view this post on Zulip Lann Martin (Aug 27 2024 at 20:06):

It seems to explicitly validate against it: https://github.com/bytecodealliance/wasm-tools/blob/2fdc2ed0de54e55f57ae0bc88273d2b674ce9bf4/crates/wit-component/src/validation.rs#L172

CLI and Rust libraries for low-level manipulation of WebAssembly modules - bytecodealliance/wasm-tools

view this post on Zulip Lann Martin (Aug 27 2024 at 20:08):

context: Grain 0.6.6 has duplicate imports for a hello world module

view this post on Zulip Lann Martin (Aug 27 2024 at 20:11):

causing this to fail: https://github.com/fermyon/spin/blob/82008342a3ec8de7d0ac6f3850828b463c692160/crates/componentize/src/lib.rs#L174-L178

Spin is the open source developer tool for building and running serverless applications powered by WebAssembly. - fermyon/spin

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:31):

Ah ok different layer, no at the component level core modules must have a single import per module/name combo

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:32):

I thought you meant the LLD/linking level where this is allowed

view this post on Zulip Lann Martin (Aug 27 2024 at 20:34):

OK so this is just something you can't handle by adapting?

view this post on Zulip Lann Martin (Aug 27 2024 at 20:34):

wasmtime run can run the (preview1) module directly

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:35):

correct, the component model has a validation predicate for core wasm modules which disallows this

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:35):

yes that's where this is a valid core wasm thing just not a valid thing in the component model

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:37):

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

(This issue captures the presentation at CG-03-02, with the goal of getting more feedback in preparation for a poll at some future CG meeting.) The Module Linking proposal proposes to recast two-le...

view this post on Zulip Lann Martin (Aug 27 2024 at 20:43):

I don't suppose there is existing code to dedupe imports?

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:43):

not currently, no, but with wasm-encoder's new support for reencoding it might not be too too hard to whip up something

view this post on Zulip Lann Martin (Aug 27 2024 at 20:44):

:thumbs_up: conceptually it shouldn't be hard for this particular case; all the duplicate imports have the same type index

view this post on Zulip Alex Crichton (Aug 27 2024 at 20:57):

this is a simple possible implementation, although I've not tested it much but shows the shape of what such a transform might be

GitHub Gist: instantly share code, notes, and snippets.

view this post on Zulip Lann Martin (Aug 27 2024 at 20:59):

Thanks!

view this post on Zulip Lann Martin (Aug 27 2024 at 21:00):

Should I take that as an invitation to PR this into wasm-tools? :smile:

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:00):

heh perhaps yeah, I haven't thought really about this in the context of wasm-tools

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:01):

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

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:01):

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

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:02):

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)

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:02):

or maybe --pass dedupe-imports,validate,...

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:03):

where each step would take wasm an output wasm and that way we could pack future things in there relatively easily

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:05):

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

view this post on Zulip Alex Crichton (Aug 27 2024 at 21:05):

https://crates.io/crates/wasm-gc


Last updated: Dec 23 2024 at 12:05 UTC