New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better WebAssembly integration #2099
Comments
This would be great in my opinion. I agree inlining the WebAssembly is not ideal. I had some vague idea about doing a similar thing in |
What do you think about having first-class support vs a plugin? I'm sure that WebAssembly will become be more and more popular, a plugin could add some friction. Also having an general view of the project is important because you can download/instantiate the wasm modules in parallel. It seems difficult to handle in a plugin. I made several tools for WebAssembly, you can find them here https://github.com/xtuc/webassemblyjs. In the future we could enable DCE (based on the real usage in JS), AOT optimizations like inlining constants. I would be happy to send a PR btw. |
@xtuc thanks for opening this, your tools and approach would integrate with Rollup really well definitely. I've followed some of your work in the Webpack integration and your PR to start function rewriting for execution ordering was really nice to see too. How would you see an initial integration working here? There's no doubt that this is the direction things should be going and contributions here would be very welcome. I'm just wondering how best to start to be practical today while ensuring a good foundation for the cross-format DCE work. Transparent integration of wasm should definitely be supported in Rollup by default in future - whether through a bundled plugin or core itself. Help getting us there would be really amazing! From a high level it feels like Rollup definitely should integrate this into core, but when I dive into the details I wonder if starting from plugin hooks might be a better proving ground for this work at least for now. Some thoughts here:
Do you see other potential benefits of core integration over direct export pruning? Would a plugin hook like the one suggested help to make progress here, or do you strongly feel a core integration is needed already? |
😊 thanks.
The initial integration is pretty straightforward I think. The plugin https://github.com/xtuc/rollup-plugin-streaming-wasm is a working prototype.
It's safer to do it as a plugin at the beginning and move it into the core later. I don't have enough context to see where would be the limitation of that approach.
You can learn more about the different instantiation methods here: https://docs.google.com/document/d/1YB2fy3gMgy2NZcOVJhRGOxcM8bDAtC3nUizDRcPmE-I Basically
I wrote a static analyzer to collect the usage https://github.com/xtuc/wasm-dce/blob/feat-cli/src/used-exports.js. We can adapt it to the syntax we'll use here and I think that DCE will still be possible.
That's indeed an alternative, the
In the plugin I just wrote in the root but I guess that we can use the In addition to the document I linked above, inlining has the downside of having to manipulate/represent an arbitrary size binary (nothing prevents to have +100Mb).
The Here's a well-explained comment WebAssembly/esm-integration#1 (comment) about the WASM<>ESM integration issues. Rollup is actually not impacted because it inlines the imported values into the first module. Which means that every imported JS value is already evaluated and available! I hope I answered all your questions clearly enough. |
This is the sort of direction exactly. If you're interested in working on this problem in Rollup we're happy to assist.
In a single-file output you can read from the
Note this doesn't apply to
It could well be worth seeing if we can create something like these plugin hooks in the mean time for when it is.
Certainly. It's worth making sure we're playing to Rollup's strengths in the use cases to find where we can have the highest impact here. Perhaps dynamic import export usage tracing + a plugin hook to your wasm plugin would be that shortest path of greatest impact to get movement here? Although it would depend on how ready your project is there. Otherwise let me know what you think is the best focus. |
Sure, in fact I have done it again here xtuc/webassemblyjs#223 during the day.
That's a good point. We can consider every following and the wasm itself as a splitted bundle (which is the case with the dynamic import anyway?). _ It seems that DCE is important for Rollup. I would personally not allow it for wasm currently, we just need to make clear that it's something experimental. I don't want to force you to introduce a new temporary API.
Just to clarify, it's only the |
If everything is clear to you, I'll continue my work in the plugin. We could also setup a video call if needed. |
@xtuc I've been thinking about this a bit further an in order to properly handle wasm imports and the rewriting of wasm imports, a core Rollup module type definitely seems like the best approach. Also as soon as browsers support So I do think we should start integrating WASM into core now actually, or at least in the next couple of months or so. Then we can also start to align the architecture around the needs of tree shaking both formats together. @lukastaegert @Rich-Harris let me know if you disagree that WASM should be integrated as a core WasmModule.ts construct? @xtuc so, perhaps we can start planning how such an integration might work? |
Admittedly I do not have a lot of experience with WASM yet. Is this a real use case that WASM files reference JS files? In the examples e.g. here, imports always seem to be passed to the WASM code by the JS code which means the WASM code does not need to be aware of how files are named and we maybe just need to extend the plugin API a little. But I would trust your judgement. |
Yes, WASM files always need to import from a JS file that defines basically the "syscalls" for the WASM to properly be configured in the environment (eg things like printf -> console.log). The standard pattern here is a |
Correction - env.js in the previous example. |
The |
What do you mean? I don't think we need to rewrite them in Rollup.
Well if the user don't use any result value we can allow it and instantiate it asynchronously. Unlike: Note that in some cases a user might want to explicitly use the sync instantiation, for example in Node.
Yes exactly, the ImportObject is more a dynamic linking phase. It happen to map well to ES module and removes boilerplate code from the user. You will find more information here WebAssembly/esm-integration#5 |
Say you have a file
There is no way to support this apart from outputting Note that SystemJS output can be made equivalently here that would then support the wasm instantiation through the system production loader. The AMD and CommonJS output cases wouldn't work though, without "inlining" the instantiation (in which case dynamic import is better suited anyway). Perhaps we need a way to distinguish between cases where we have a "native wasm loader" we can use and cases where we need to inline a WASM instantiation process like you do in the plugin. |
Trying to think how we might attach something here, although further suggestions welcome - Here are some of the integration points:
As I try to think through this it does seem it's a new level of abstraction that needs to bump quite a few things. Accepted that the starting point will be hacky to get something going, and I'm happy to dive in myself to help find the right abstractions further to refactor towards something cleaner as we go. I hope that gives at least some rough overview to start though. |
(Also we should create a "wasm" branch for this work to happen off mainline I think until it stabilizes) |
Ok I've created a wasm branch, and also based this off the typing changes in #2108 to avoid the conflicts when that merges. We can track any PR work against this branch and then merge aggressively (into the wasm branch) to get some experimental collaboration going on this hopefully. |
I started a PR here #2113.
Actually, that means you have the wasm file as entrypoint? Currently you JS to require it so I don't think that's needed. |
@xtuc I'm just stepping into this and playing catch up. Are there any blockers we're looking at that are holding this up? |
Thanks @shellscape we have some points in the PR thread: #2113, mostly about the integration of binary modules into the module graph. |
Hi guys, It looks like you did a nice job. What is the current status of this feature? |
FWIW, Node 12 (12.4.0) now has import support for Wasm, albeit behind a flag: https://nodejs.org/api/esm.html#esm_experimental_wasm_modules |
Hey folks. This is a saved-form message, but rest assured we mean every word. The Rollup team is attempting to clean up the Issues backlog in the hopes that the active and still-needed, still-relevant issues bubble up to the surface. With that, we're closing issues that have been open for an eon or two, and have gone stale like pirate hard-tack without activity. We really appreciate the folks have taken the time to open and comment on this issue. Please don't confuse this closure with us not caring or dismissing your issue, feature request, discussion, or report. The issue will still be here, just in a closed state. If the issue pertains to a bug, please re-test for the bug on the latest version of Rollup and if present, please tag @shellscape and request a re-open, and we'll be happy to oblige. |
I think it would be worth keeping a tracking issue open here, since Wasm tree-shaking support in future is very much a thing that would simply need support to move forward from an interested contributor / team / company. |
It would be nice to have this. import * as wasm from './index_bg.wasm'; This is already available in the Webpack plugin. |
To be clear, rollup-plugin-rust does not need esm-integration, it works perfectly fine without it. But esm-integration would make things a bit more convenient for users, since they wouldn't need to load the Alternatively, if top level |
👋
I'm currently working on the WebAssembly integration in Webpack and I was wondering if Rollup would be interested into a first-class support for bundling wasm as well?
I know that https://github.com/rollup/rollup-plugin-wasm already exist but inline the wasm module into the bundle is not ideal. cc @jamen
In order to provide the best integration we want wasm to be part of the ES module graph, and treated as any other ES module. It has some issues at the moment, you can find more details here https://github.com/WebAssembly/esm-integration.
Rollup inlines the imports and fixes most of our integration issues!
I already experimented with a plugin, here's an example:
Complet example here https://github.com/xtuc/rollup-plugin-streaming-wasm/blob/master/example/src/
The dynamic import makes it more friendly for the developer (see old version).
The text was updated successfully, but these errors were encountered: